forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessManager.Unix.cs
More file actions
79 lines (71 loc) · 3.33 KB
/
ProcessManager.Unix.cs
File metadata and controls
79 lines (71 loc) · 3.33 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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.Win32.SafeHandles;
using System.Collections.Generic;
using System.Runtime.Versioning;
using System.Text;
namespace System.Diagnostics
{
internal static partial class ProcessManager
{
/// <summary>Gets whether the process with the specified ID on the specified machine is currently running.</summary>
/// <param name="processId">The process ID.</param>
/// <param name="machineName">The machine name.</param>
/// <returns>true if the process is running; otherwise, false.</returns>
public static bool IsProcessRunning(int processId, string machineName)
{
ThrowIfRemoteMachine(machineName);
return IsProcessRunning(processId);
}
/// <summary>Gets whether the process with the specified ID is currently running.</summary>
/// <param name="processId">The process ID.</param>
/// <returns>true if the process is running; otherwise, false.</returns>
public static bool IsProcessRunning(int processId)
{
// kill with signal==0 means to not actually send a signal.
// If we get back 0, the process is still alive.
int output = Interop.Sys.Kill(processId, Interop.Sys.Signals.None);
// If kill set errno=EPERM, assume querying process is alive.
return 0 == output || (-1 == output && Interop.Error.EPERM == Interop.Sys.GetLastError());
}
/// <summary>Gets the ProcessInfo for the specified process ID on the specified machine.</summary>
/// <param name="processId">The process ID.</param>
/// <param name="machineName">The machine name.</param>
/// <returns>The ProcessInfo for the process if it could be found; otherwise, null.</returns>
public static ProcessInfo? GetProcessInfo(int processId, string machineName)
{
ThrowIfRemoteMachine(machineName);
return CreateProcessInfo(processId);
}
/// <summary>Gets the IDs of all processes on the specified machine.</summary>
/// <param name="machineName">The machine to examine.</param>
/// <returns>An array of process IDs from the specified machine.</returns>
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
public static int[] GetProcessIds(string machineName)
{
ThrowIfRemoteMachine(machineName);
return GetProcessIds();
}
/// <summary>Gets the ID of a process from a handle to the process.</summary>
/// <param name="processHandle">The handle.</param>
/// <returns>The process ID.</returns>
public static int GetProcessIdFromHandle(SafeProcessHandle processHandle)
{
return processHandle.ProcessId;
}
private static bool IsRemoteMachineCore(string machineName)
{
return
machineName != "." &&
machineName != Interop.Sys.GetHostName();
}
internal static void ThrowIfRemoteMachine(string machineName)
{
if (IsRemoteMachine(machineName))
{
throw new PlatformNotSupportedException(SR.RemoteMachinesNotSupported);
}
}
}
}