forked from ststeiger/ChromeDevTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessUtils.cs
More file actions
145 lines (114 loc) · 4.88 KB
/
ProcessUtils.cs
File metadata and controls
145 lines (114 loc) · 4.88 KB
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
namespace MasterDevs.ChromeDevTools.Sample
{
internal class WindowsProcess
{
// Define an extension method for type System.Process that returns the command
// line via WMI.
public static string GetCommandLine(System.Diagnostics.Process process)
{
string cmdLine = null;
using (System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher(
$"SELECT CommandLine FROM Win32_Process WHERE ProcessId = {process.Id}"))
{
// By definition, the query returns at most 1 match, because the process
// is looked up by ID (which is unique by definition).
System.Management.ManagementObjectCollection.ManagementObjectEnumerator matchEnum = searcher.Get().GetEnumerator();
if (matchEnum.MoveNext()) // Move to the 1st item.
{
cmdLine = matchEnum.Current["CommandLine"]?.ToString();
}
}
/*
if (cmdLine == null)
{
// Not having found a command line implies 1 of 2 exceptions, which the
// WMI query masked:
// An "Access denied" exception due to lack of privileges.
// A "Cannot process request because the process (<pid>) has exited."
// exception due to the process having terminated.
// We provoke the same exception again simply by accessing process.MainModule.
var dummy = process.MainModule; // Provoke exception.
}
*/
return cmdLine;
}
/// <summary>
/// Kill a process, and all of its children, grandchildren, etc.
/// </summary>
/// <param name="pid">Process ID.</param>
public static void KillProcessAndChildren(int pid)
{
// Cannot close 'system idle process'.
if (pid == 0)
{
return;
}
System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher
("Select * From Win32_Process Where ParentProcessID=" + pid);
System.Management.ManagementObjectCollection moc = searcher.Get();
foreach (System.Management.ManagementObject mo in moc)
{
KillProcessAndChildren(System.Convert.ToInt32(mo["ProcessID"]));
}
try
{
System.Diagnostics.Process proc = System.Diagnostics.Process.GetProcessById(pid);
proc.Kill();
}
catch (System.ArgumentException)
{
// Process already exited.
}
} // End Sub KillProcessAndChildren
// https://stackoverflow.com/questions/5901679/kill-process-tree-programmatically-in-c-sharp
private static void EndProcessTree(string imageName)
{
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo
{
FileName = "taskkill",
Arguments = $"/im {imageName} /f /t",
CreateNoWindow = true,
UseShellExecute = false
}).WaitForExit();
} // End Sub EndProcessTree
}
internal class UnixProcess
{
public static string GetCommandLine(System.Diagnostics.Process process)
{
string file = $"/proc/{process.Id}/cmdline";
string commandLine = System.IO.File.ReadAllText(file, System.Text.Encoding.UTF8);
if (string.IsNullOrEmpty(commandLine))
return commandLine;
commandLine = commandLine.Trim('\f', '\v', '\t', ' ', '\r', '\n');
return commandLine;
}
public static void KillProcessAndChildren(int pid)
{
// https://stackoverflow.com/questions/392022/whats-the-best-way-to-send-a-signal-to-all-members-of-a-process-group
// kill -TERM -PID
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("kill", $"-TERM -{pid}"));
}
}
class ProcessUtils
{
public static string GetCommandLine(System.Diagnostics.Process process)
{
if (System.Environment.OSVersion.Platform == System.PlatformID.Unix)
return UnixProcess.GetCommandLine(process);
return WindowsProcess.GetCommandLine(process);
}
public static void KillProcessAndChildren(int pid)
{
if (System.Environment.OSVersion.Platform == System.PlatformID.Unix)
UnixProcess.KillProcessAndChildren(pid);
else
WindowsProcess.KillProcessAndChildren(pid);
}
public static void KillProcessAndChildren(System.Diagnostics.Process proc)
{
KillProcessAndChildren(proc.Id);
}
}
}