forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcess.Linux.cs
More file actions
370 lines (333 loc) · 14.9 KB
/
Process.Linux.cs
File metadata and controls
370 lines (333 loc) · 14.9 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
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Buffers;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Runtime.Versioning;
using System.Text;
namespace System.Diagnostics
{
public partial class Process : IDisposable
{
/// <summary>
/// Creates an array of <see cref="Process"/> components that are associated with process resources on a
/// remote computer. These process resources share the specified process name.
/// </summary>
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
[SupportedOSPlatform("maccatalyst")]
public static Process[] GetProcessesByName(string? processName, string machineName)
{
ProcessManager.ThrowIfRemoteMachine(machineName);
if (processName == null)
{
processName = string.Empty;
}
var processes = new List<Process>();
foreach (int pid in ProcessManager.EnumerateProcessIds())
{
if (Interop.procfs.TryReadStatFile(pid, out Interop.procfs.ParsedStat parsedStat) &&
string.Equals(processName, Process.GetUntruncatedProcessName(ref parsedStat), StringComparison.OrdinalIgnoreCase) &&
Interop.procfs.TryReadStatusFile(pid, out Interop.procfs.ParsedStatus parsedStatus))
{
ProcessInfo processInfo = ProcessManager.CreateProcessInfo(ref parsedStat, ref parsedStatus, processName);
processes.Add(new Process(machineName, false, processInfo.ProcessId, processInfo));
}
}
return processes.ToArray();
}
/// <summary>Gets the amount of time the process has spent running code inside the operating system core.</summary>
public TimeSpan PrivilegedProcessorTime
{
get
{
return TicksToTimeSpan(GetStat().stime);
}
}
/// <summary>Gets the time the associated process was started.</summary>
internal DateTime StartTimeCore
{
get
{
return BootTimeToDateTime(TicksToTimeSpan(GetStat().starttime));
}
}
/// <summary>Computes a time based on a number of ticks since boot.</summary>
/// <param name="timespanAfterBoot">The timespan since boot.</param>
/// <returns>The converted time.</returns>
internal static DateTime BootTimeToDateTime(TimeSpan timespanAfterBoot)
{
// And use that to determine the absolute time for timespan.
DateTime dt = BootTime + timespanAfterBoot;
// The return value is expected to be in the local time zone.
// It is converted here (rather than starting with DateTime.Now) to avoid DST issues.
return dt.ToLocalTime();
}
/// <summary>Gets the system boot time.</summary>
private static DateTime BootTime
{
get
{
// '/proc/stat -> btime' gets the boot time.
// btime is the time of system boot in seconds since the Unix epoch.
// It includes suspended time and is updated based on the system time (settimeofday).
const string StatFile = Interop.procfs.ProcStatFilePath;
string text = File.ReadAllText(StatFile);
int btimeLineStart = text.IndexOf("\nbtime ", StringComparison.Ordinal);
if (btimeLineStart >= 0)
{
int btimeStart = btimeLineStart + "\nbtime ".Length;
int btimeEnd = text.IndexOf('\n', btimeStart);
if (btimeEnd > btimeStart)
{
if (long.TryParse(text.AsSpan(btimeStart, btimeEnd - btimeStart), out long bootTimeSeconds))
{
return DateTime.UnixEpoch + TimeSpan.FromSeconds(bootTimeSeconds);
}
}
}
return DateTime.UtcNow;
}
}
/// <summary>Gets the parent process ID</summary>
private int ParentProcessId =>
GetStat().ppid;
/// <summary>Gets execution path</summary>
private string? GetPathToOpenFile()
{
string[] allowedProgramsToRun = { "xdg-open", "gnome-open", "kfmclient" };
foreach (var program in allowedProgramsToRun)
{
string? pathToProgram = FindProgramInPath(program);
if (!string.IsNullOrEmpty(pathToProgram))
{
return pathToProgram;
}
}
return null;
}
/// <summary>
/// Gets the amount of time the associated process has spent utilizing the CPU.
/// It is the sum of the <see cref='System.Diagnostics.Process.UserProcessorTime'/> and
/// <see cref='System.Diagnostics.Process.PrivilegedProcessorTime'/>.
/// </summary>
public TimeSpan TotalProcessorTime
{
get
{
Interop.procfs.ParsedStat stat = GetStat();
return TicksToTimeSpan(stat.utime + stat.stime);
}
}
/// <summary>
/// Gets the amount of time the associated process has spent running code
/// inside the application portion of the process (not the operating system core).
/// </summary>
public TimeSpan UserProcessorTime
{
get
{
return TicksToTimeSpan(GetStat().utime);
}
}
partial void EnsureHandleCountPopulated()
{
if (_processInfo!.HandleCount <= 0 && _haveProcessId)
{
// Don't get information for a PID that exited and has possibly been recycled.
if (GetHasExited(refresh: false))
{
return;
}
string path = Interop.procfs.GetFileDescriptorDirectoryPathForProcess(_processId);
if (Directory.Exists(path))
{
try
{
_processInfo.HandleCount = Directory.GetFiles(path, "*", SearchOption.TopDirectoryOnly).Length;
}
catch (DirectoryNotFoundException) // Occurs when the process is deleted between the Exists check and the GetFiles call.
{
}
}
}
}
/// <summary>
/// Gets or sets which processors the threads in this process can be scheduled to run on.
/// </summary>
private IntPtr ProcessorAffinityCore
{
get
{
EnsureState(State.HaveNonExitedId);
IntPtr set;
if (Interop.Sys.SchedGetAffinity(_processId, out set) != 0)
{
throw new Win32Exception(); // match Windows exception
}
return set;
}
set
{
EnsureState(State.HaveNonExitedId);
if (Interop.Sys.SchedSetAffinity(_processId, ref value) != 0)
{
throw new Win32Exception(); // match Windows exception
}
}
}
/// <summary>
/// Make sure we have obtained the min and max working set limits.
/// </summary>
private void GetWorkingSetLimits(out IntPtr minWorkingSet, out IntPtr maxWorkingSet)
{
minWorkingSet = IntPtr.Zero; // no defined limit available
// For max working set, try to respect container limits by reading
// from cgroup, but if it's unavailable, fall back to reading from procfs.
EnsureState(State.HaveNonExitedId);
if (!Interop.cgroups.TryGetMemoryLimit(out ulong rsslim))
{
rsslim = GetStat().rsslim;
}
// rsslim is a ulong, but maxWorkingSet is an IntPtr, so we need to cap rsslim
// at the max size of IntPtr. This often happens when there is no configured
// rsslim other than ulong.MaxValue, which without these checks would show up
// as a maxWorkingSet == -1.
switch (IntPtr.Size)
{
case 4:
if (rsslim > int.MaxValue)
rsslim = int.MaxValue;
break;
case 8:
if (rsslim > long.MaxValue)
rsslim = long.MaxValue;
break;
}
maxWorkingSet = (IntPtr)rsslim;
}
/// <summary>Sets one or both of the minimum and maximum working set limits.</summary>
/// <param name="newMin">The new minimum working set limit, or null not to change it.</param>
/// <param name="newMax">The new maximum working set limit, or null not to change it.</param>
/// <param name="resultingMin">The resulting minimum working set limit after any changes applied.</param>
/// <param name="resultingMax">The resulting maximum working set limit after any changes applied.</param>
private void SetWorkingSetLimitsCore(IntPtr? newMin, IntPtr? newMax, out IntPtr resultingMin, out IntPtr resultingMax)
{
// RLIMIT_RSS with setrlimit not supported on Linux > 2.4.30.
throw new PlatformNotSupportedException(SR.MinimumWorkingSetNotSupported);
}
/// <summary>Gets the path to the executable for the process, or null if it could not be retrieved.</summary>
/// <param name="processId">The pid for the target process, or -1 for the current process.</param>
internal static string? GetExePath(int processId = -1)
{
string exeFilePath = processId == -1 ?
Interop.procfs.SelfExeFilePath :
Interop.procfs.GetExeFilePathForProcess(processId);
return Interop.Sys.ReadLink(exeFilePath);
}
/// <summary>Gets the name that was used to start the process, or null if it could not be retrieved.</summary>
/// <param name="stat">The stat for the target process.</param>
internal static string GetUntruncatedProcessName(ref Interop.procfs.ParsedStat stat)
{
string cmdLineFilePath = Interop.procfs.GetCmdLinePathForProcess(stat.pid);
byte[]? rentedArray = null;
try
{
// bufferSize == 1 used to avoid unnecessary buffer in FileStream
using (var fs = new FileStream(cmdLineFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 1, useAsync: false))
{
Span<byte> buffer = stackalloc byte[512];
int bytesRead = 0;
while (true)
{
// Resize buffer if it was too small.
if (bytesRead == buffer.Length)
{
uint newLength = (uint)buffer.Length * 2;
byte[] tmp = ArrayPool<byte>.Shared.Rent((int)newLength);
buffer.CopyTo(tmp);
byte[]? toReturn = rentedArray;
buffer = rentedArray = tmp;
if (toReturn != null)
{
ArrayPool<byte>.Shared.Return(toReturn);
}
}
Debug.Assert(bytesRead < buffer.Length);
int n = fs.Read(buffer.Slice(bytesRead));
bytesRead += n;
// cmdline contains the argv array separated by '\0' bytes.
// stat.comm contains a possibly truncated version of the process name.
// When the program is a native executable, the process name will be in argv[0].
// When the program is a script, argv[0] contains the interpreter, and argv[1] contains the script name.
Span<byte> argRemainder = buffer.Slice(0, bytesRead);
int argEnd = argRemainder.IndexOf((byte)'\0');
if (argEnd != -1)
{
// Check if argv[0] has the process name.
string? name = GetUntruncatedNameFromArg(argRemainder.Slice(0, argEnd), prefix: stat.comm);
if (name != null)
{
return name;
}
// Check if argv[1] has the process name.
argRemainder = argRemainder.Slice(argEnd + 1);
argEnd = argRemainder.IndexOf((byte)'\0');
if (argEnd != -1)
{
name = GetUntruncatedNameFromArg(argRemainder.Slice(0, argEnd), prefix: stat.comm);
return name ?? stat.comm;
}
}
if (n == 0)
{
return stat.comm;
}
}
}
}
catch (IOException)
{
return stat.comm;
}
finally
{
if (rentedArray != null)
{
ArrayPool<byte>.Shared.Return(rentedArray);
}
}
static string? GetUntruncatedNameFromArg(Span<byte> arg, string prefix)
{
// Strip directory names from arg.
int nameStart = arg.LastIndexOf((byte)'/') + 1;
string argString = Encoding.UTF8.GetString(arg.Slice(nameStart));
if (argString.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
{
return argString;
}
else
{
return null;
}
}
}
// ----------------------------------
// ---- Unix PAL layer ends here ----
// ----------------------------------
/// <summary>Reads the stats information for this process from the procfs file system.</summary>
private Interop.procfs.ParsedStat GetStat()
{
EnsureState(State.HaveNonExitedId);
Interop.procfs.ParsedStat stat;
if (!Interop.procfs.TryReadStatFile(_processId, out stat))
{
throw new Win32Exception(SR.ProcessInformationUnavailable);
}
return stat;
}
}
}