-
Notifications
You must be signed in to change notification settings - Fork 46
/
ProcessManager.cs
468 lines (407 loc) · 17.7 KB
/
ProcessManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using Microsoft.DotNet.XHarness.iOS.Shared.Logging;
using Microsoft.DotNet.XHarness.iOS.Shared.Utilities;
using Microsoft.DotNet.XHarness.iOS.Shared.Execution.Mlaunch;
namespace Microsoft.DotNet.XHarness.iOS.Shared.Execution
{
public class ProcessManager : IProcessManager
{
static readonly Lazy<string> s_autoDetectedXcodeRoot = new Lazy<string>(DetectXcodePath, LazyThreadSafetyMode.PublicationOnly);
readonly string _xcodeRoot;
public string XcodeRoot => _xcodeRoot ?? s_autoDetectedXcodeRoot.Value;
public string MlaunchPath { get; }
Version _xcode_version;
public Version XcodeVersion
{
get
{
if (_xcode_version == null)
{
var doc = new XmlDocument();
doc.Load(Path.Combine(XcodeRoot, "Contents", "version.plist"));
_xcode_version = Version.Parse(doc.SelectSingleNode("//key[text() = 'CFBundleShortVersionString']/following-sibling::string").InnerText);
}
return _xcode_version;
}
}
public ProcessManager(string xcodeRoot = null, string mlaunchPath = "/Library/Frameworks/Xamarin.iOS.framework/Versions/Current/bin/mlaunch")
{
this._xcodeRoot = xcodeRoot;
MlaunchPath = mlaunchPath;
}
public async Task<ProcessExecutionResult> ExecuteCommandAsync(string filename,
IList<string> args,
ILog log,
TimeSpan timeout,
Dictionary<string, string> environmentVariables = null,
CancellationToken? cancellationToken = null)
{
return await ExecuteCommandAsync(filename, args, log, log, log, timeout, environmentVariables, cancellationToken);
}
public async Task<ProcessExecutionResult> ExecuteCommandAsync(string filename,
IList<string> args,
ILog log,
ILog stdout,
ILog stderr,
TimeSpan timeout,
Dictionary<string, string> environmentVariables = null,
CancellationToken? cancellationToken = null)
{
using var p = new Process();
p.StartInfo.FileName = filename ?? throw new ArgumentNullException(nameof(filename));
p.StartInfo.Arguments = StringUtils.FormatArguments(args);
return await RunAsync(p, log, stdout, stderr, timeout, environmentVariables, cancellationToken);
}
public async Task<ProcessExecutionResult> ExecuteCommandAsync(MlaunchArguments args,
ILog log,
TimeSpan timeout,
Dictionary<string, string> environmentVariables = null,
CancellationToken? cancellationToken = null)
{
using var p = new Process();
return await RunAsync(p, args, log, timeout, environmentVariables, cancellationToken);
}
public Task<ProcessExecutionResult> ExecuteXcodeCommandAsync(string executable, IList<string> args, ILog log, TimeSpan timeout)
{
string filename = Path.Combine(XcodeRoot, "Contents", "Developer", "usr", "bin", executable);
return ExecuteCommandAsync(filename, args, log, timeout: timeout);
}
[DllImport("/usr/lib/libc.dylib")]
internal static extern int kill(int pid, int sig);
public Task<ProcessExecutionResult> RunAsync(Process process,
ILog log,
TimeSpan? timeout = null,
Dictionary<string, string> environment_variables = null,
CancellationToken? cancellationToken = null,
bool? diagnostics = null)
{
return RunAsync(process, log, log, log, timeout, environment_variables, cancellationToken, diagnostics);
}
public Task<ProcessExecutionResult> RunAsync(Process process,
MlaunchArguments args,
ILog log,
TimeSpan? timeout = null,
Dictionary<string, string> environmentVariables = null,
CancellationToken? cancellationToken = null,
bool? diagnostics = null)
{
if (!args.Any(a => a is SdkRootArgument))
args.Prepend(new SdkRootArgument(XcodeRoot));
process.StartInfo.FileName = MlaunchPath;
process.StartInfo.Arguments = args.AsCommandLine();
return RunAsync(process, log, timeout, environmentVariables, cancellationToken, diagnostics);
}
public Task<ProcessExecutionResult> RunAsync(Process process,
ILog log,
ILog stdout,
ILog stderr,
TimeSpan? timeout = null,
Dictionary<string, string> environmentVariables = null,
CancellationToken? cancellationToken = null,
bool? diagnostics = null)
{
return RunAsyncInternal(process, log, stdout, stderr, timeout, environmentVariables, cancellationToken, diagnostics);
}
public Task KillTreeAsync(Process process, ILog log, bool? diagnostics = true)
{
return KillTreeAsyncInternal(process.Id, log, diagnostics);
}
public Task KillTreeAsync(int pid, ILog log, bool? diagnostics = true)
{
return KillTreeAsyncInternal(pid, log, diagnostics);
}
static async Task<ProcessExecutionResult> RunAsyncInternal(Process process,
ILog log,
ILog stdout,
ILog stderr,
TimeSpan? timeout = null,
Dictionary<string, string> environmentVariables = null,
CancellationToken? cancellationToken = null,
bool? diagnostics = null)
{
var stdout_completion = new TaskCompletionSource<bool>();
var stderr_completion = new TaskCompletionSource<bool>();
var rv = new ProcessExecutionResult();
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
// Make cute emojiis show up as cute emojiis in the output instead of ugly text symbols!
process.StartInfo.StandardOutputEncoding = Encoding.UTF8;
process.StartInfo.StandardErrorEncoding = Encoding.UTF8;
process.StartInfo.UseShellExecute = false;
if (environmentVariables != null)
{
foreach (var kvp in environmentVariables)
{
if (kvp.Value == null)
{
process.StartInfo.EnvironmentVariables.Remove(kvp.Key);
}
else
{
process.StartInfo.EnvironmentVariables[kvp.Key] = kvp.Value;
}
}
}
process.OutputDataReceived += (sender, e) =>
{
if (e.Data != null)
{
lock (stdout)
{
stdout.WriteLine(e.Data);
stdout.Flush();
}
}
else
{
stdout_completion.TrySetResult(true);
}
};
process.ErrorDataReceived += (sender, e) =>
{
if (e.Data != null)
{
lock (stderr)
{
stderr.WriteLine(e.Data);
stderr.Flush();
}
}
else
{
stderr_completion.TrySetResult(true);
}
};
var sb = new StringBuilder();
if (process.StartInfo.EnvironmentVariables != null)
{
var currentEnvironment = Environment.GetEnvironmentVariables().Cast<System.Collections.DictionaryEntry>().ToDictionary((v) => (string)v.Key, (v) => (string)v.Value, StringComparer.Ordinal);
var processEnvironment = process.StartInfo.EnvironmentVariables.Cast<System.Collections.DictionaryEntry>().ToDictionary((v) => (string)v.Key, (v) => (string)v.Value, StringComparer.Ordinal);
var allKeys = currentEnvironment.Keys.Union(processEnvironment.Keys).Distinct();
foreach (var key in allKeys)
{
string a = null, b = null;
currentEnvironment.TryGetValue(key, out a);
processEnvironment.TryGetValue(key, out b);
if (a != b)
sb.Append($"{key}={StringUtils.Quote(b)} ");
}
}
sb.Append($"{StringUtils.Quote(process.StartInfo.FileName)} {process.StartInfo.Arguments}");
log.WriteLine(sb);
process.Start();
var pid = process.Id;
process.BeginErrorReadLine();
process.BeginOutputReadLine();
cancellationToken?.Register(() =>
{
var hasExited = false;
try
{
hasExited = process.HasExited;
}
catch
{
// Process.HasExited can sometimes throw exceptions, so
// just ignore those and to be safe treat it as the
// process didn't exit (the safe option being to not leave
// processes behind).
}
if (!hasExited)
{
stderr.WriteLine($"Execution of {pid} was cancelled.");
kill(pid, 9);
}
});
if (timeout.HasValue)
{
if (!await WaitForExitAsync(process, timeout.Value))
{
await KillTreeAsyncInternal(process.Id, log, diagnostics ?? true);
rv.TimedOut = true;
lock (stderr)
log.WriteLine($"{pid} Execution timed out after {timeout.Value.TotalSeconds} seconds and the process was killed.");
}
}
await WaitForExitAsync(process);
Task.WaitAll(new Task[] { stderr_completion.Task, stdout_completion.Task }, TimeSpan.FromSeconds(1));
try
{
rv.ExitCode = process.ExitCode;
}
catch (Exception e)
{
rv.ExitCode = 12345678;
log.WriteLine($"Failed to get ExitCode: {e}");
}
return rv;
}
static async Task KillTreeAsyncInternal(int pid, ILog log, bool? diagnostics = true)
{
var pids = GetChildrenPS(log, pid);
if (diagnostics == true)
{
log.WriteLine($"Pids to kill: {string.Join(", ", pids.Select((v) => v.ToString()).ToArray())}");
using (var ps = new Process())
{
log.WriteLine("Writing process list:");
ps.StartInfo.FileName = "ps";
ps.StartInfo.Arguments = "-A -o pid,ruser,ppid,pgid,%cpu=%CPU,%mem=%MEM,flags=FLAGS,lstart,rss,vsz,tty,state,time,command";
await RunAsyncInternal(ps, log, log, log, TimeSpan.FromSeconds(5), diagnostics: false);
}
foreach (var diagnose_pid in pids)
{
var template = Path.GetTempFileName();
try
{
var commands = new StringBuilder();
using (var dbg = new Process())
{
commands.AppendLine($"process attach --pid {diagnose_pid}");
commands.AppendLine("thread list");
commands.AppendLine("thread backtrace all");
commands.AppendLine("detach");
commands.AppendLine("quit");
dbg.StartInfo.FileName = "/usr/bin/lldb";
dbg.StartInfo.Arguments = StringUtils.FormatArguments("--source", template);
File.WriteAllText(template, commands.ToString());
log.WriteLine($"Printing backtrace for pid={pid}");
await RunAsyncInternal(dbg, log, log, log, TimeSpan.FromSeconds(30), diagnostics: false);
}
}
finally
{
try
{
File.Delete(template);
}
catch
{
// Don't care
}
}
}
}
// Send SIGABRT since that produces a crash report
// lldb may fail to attach to system processes, but crash reports will still be produced with potentially helpful stack traces.
for (int i = 0; i < pids.Count; i++)
kill(pids[i], 6);
// send kill -9 anyway as a last resort
for (int i = 0; i < pids.Count; i++)
kill(pids[i], 9);
}
static async Task<bool> WaitForExitAsync(Process process, TimeSpan? timeout = null)
{
if (process.HasExited)
return true;
var tcs = new TaskCompletionSource<bool>();
void ProcessExited(object sender, EventArgs ea)
{
process.Exited -= ProcessExited;
tcs.TrySetResult(true);
}
process.Exited += ProcessExited;
process.EnableRaisingEvents = true;
// Check if process exited again, in case it exited after we checked
// the last time, but before we attached the event handler.
if (process.HasExited)
{
process.Exited -= ProcessExited;
tcs.TrySetResult(true);
return true;
}
if (timeout.HasValue)
{
return await tcs.Task.TimeoutAfter(timeout.Value);
}
else
{
await tcs.Task;
return true;
}
}
static List<int> GetChildrenPS(ILog log, int pid)
{
var list = new List<int>();
using (Process ps = new Process())
{
ps.StartInfo.FileName = "ps";
ps.StartInfo.Arguments = "-eo ppid,pid";
ps.StartInfo.UseShellExecute = false;
ps.StartInfo.RedirectStandardOutput = true;
ps.Start();
string stdout = ps.StandardOutput.ReadToEnd();
if (!ps.WaitForExit(1000))
{
log.WriteLine("ps didn't finish in a reasonable amount of time (1 second).");
return list;
}
if (ps.ExitCode != 0)
return list;
stdout = stdout.Trim();
if (string.IsNullOrEmpty(stdout))
return list;
var dict = new Dictionary<int, List<int>>();
foreach (string line in stdout.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries))
{
var l = line.Trim();
var space = l.IndexOf(' ');
if (space <= 0)
continue;
var parent = l.Substring(0, space);
var process = l.Substring(space + 1);
if (int.TryParse(parent, out var parent_id) && int.TryParse(process, out var process_id))
{
if (!dict.TryGetValue(parent_id, out var children))
dict[parent_id] = children = new List<int>();
children.Add(process_id);
}
}
var queue = new Queue<int>();
queue.Enqueue(pid);
do
{
var parent_id = queue.Dequeue();
list.Add(parent_id);
if (dict.TryGetValue(parent_id, out var children))
{
foreach (var child in children)
queue.Enqueue(child);
}
} while (queue.Count > 0);
}
return list;
}
static string DetectXcodePath()
{
using var process = new Process();
process.StartInfo.FileName = "xcode-select";
process.StartInfo.Arguments = "-p";
var log = new MemoryLog();
var stdout = new MemoryLog() { Timestamp = false };
var stderr = new ConsoleLog();
var timeout = TimeSpan.FromSeconds(30);
var result = RunAsyncInternal(process, log, stdout, stderr, timeout).GetAwaiter().GetResult();
if (!result.Succeeded)
throw new Exception("Failed to detect Xcode path from xcode-select!");
// Something like /Applications/Xcode114.app/Contents/Developers
var xcodeRoot = stdout.ToString().Trim();
if (string.IsNullOrEmpty(xcodeRoot))
throw new Exception("Failed to detect Xcode path from xcode-select!");
// We need /Applications/Xcode114.app only
return Path.GetDirectoryName(Path.GetDirectoryName(xcodeRoot));
}
}
}