Skip to content

Commit

Permalink
Add and use Environment.ProcessId (#38908)
Browse files Browse the repository at this point in the history
* Add and use Environment.ProcessId

* Split newly added tests
  • Loading branch information
stephentoub committed Jul 9, 2020
1 parent 0716913 commit ea2b09b
Show file tree
Hide file tree
Showing 32 changed files with 86 additions and 211 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

4 changes: 2 additions & 2 deletions src/libraries/System.Console/tests/CancelKeyPress.Unix.cs
Expand Up @@ -61,7 +61,7 @@ public void ExitDetectionNotBlockedByHandler()
};
// Generate CancelKeyPress
Assert.Equal(0, kill(Process.GetCurrentProcess().Id, SIGINT));
Assert.Equal(0, kill(Environment.ProcessId, SIGINT));
// Wait till we block CancelKeyPress
Assert.True(tcs.Task.Wait(WaitFailTestTimeoutSeconds * 1000));
Expand Down Expand Up @@ -101,7 +101,7 @@ private void HandlerInvokedForSignal(int signalOuter)
try
{
int signalInner = int.Parse(signalStr);
Assert.Equal(0, kill(Process.GetCurrentProcess().Id, signalInner));
Assert.Equal(0, kill(Environment.ProcessId, signalInner));
Assert.True(tcs.Task.Wait(WaitFailTestTimeoutSeconds * 1000));
Assert.Equal(
signalInner == SIGINT ? ConsoleSpecialKey.ControlC : ConsoleSpecialKey.ControlBreak,
Expand Down
Expand Up @@ -152,8 +152,6 @@
Link="Common\Interop\Windows\Advapi32\Interop.AdjustTokenPrivileges.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GetComputerName.cs"
Link="Common\Interop\Windows\Kernel32\Interop.GetComputerName.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GetCurrentProcessId.cs"
Link="Common\Interop\Windows\Kernel32\Interop.GetCurrentProcessId.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GetConsoleCP.cs"
Link="Common\Interop\Windows\Kernel32\Interop.GetConsoleCP.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GetConsoleOutputCP.cs"
Expand Down Expand Up @@ -243,8 +241,6 @@
Link="Common\Interop\Unix\Interop.GetGroupList.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.GetLine.cs"
Link="Common\Interop\Unix\Interop.GetLine.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.GetPid.cs"
Link="Common\Interop\Unix\Interop.GetPid.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.GetPwUid.cs"
Link="Common\Interop\Unix\Interop.GetPwUid.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.GetSetPriority.cs"
Expand Down
Expand Up @@ -59,7 +59,7 @@ private unsafe IntPtr ProcessorAffinityCore
private void GetWorkingSetLimits(out IntPtr minWorkingSet, out IntPtr maxWorkingSet)
{
// We can only do this for the current process on OS X
if (_processId != Interop.Sys.GetPid())
if (_processId != Environment.ProcessId)
throw new PlatformNotSupportedException(SR.OsxExternalProcessWorkingSetNotSupported);

// Minimum working set (or resident set, as it is called on *nix) doesn't exist so set to 0
Expand Down Expand Up @@ -88,7 +88,7 @@ private void GetWorkingSetLimits(out IntPtr minWorkingSet, out IntPtr maxWorking
private void SetWorkingSetLimitsCore(IntPtr? newMin, IntPtr? newMax, out IntPtr resultingMin, out IntPtr resultingMax)
{
// We can only do this for the current process on OS X
if (_processId != Interop.Sys.GetPid())
if (_processId != Environment.ProcessId)
throw new PlatformNotSupportedException(SR.OsxExternalProcessWorkingSetNotSupported);

// There isn't a way to set the minimum working set, so throw an exception here
Expand Down
Expand Up @@ -104,7 +104,7 @@ private unsafe int ParentProcessId
/// <summary>Gets the path to the current executable, or null if it could not be retrieved.</summary>
private static string? GetExePath()
{
return Interop.Process.GetProcPath(Interop.Sys.GetPid());
return Interop.Process.GetProcPath(Environment.ProcessId);
}

// ----------------------------------
Expand Down
Expand Up @@ -94,7 +94,7 @@ private int ParentProcessId
/// <summary>Gets the path to the current executable, or null if it could not be retrieved.</summary>
private static string GetExePath()
{
return Interop.libproc.proc_pidpath(Interop.Sys.GetPid());
return Interop.libproc.proc_pidpath(Environment.ProcessId);
}

// ----------------------------------
Expand All @@ -103,7 +103,7 @@ private static string GetExePath()

private Interop.libproc.rusage_info_v3 GetCurrentProcessRUsage()
{
return Interop.libproc.proc_pid_rusage(Interop.Sys.GetPid());
return Interop.libproc.proc_pid_rusage(Environment.ProcessId);
}
}
}
Expand Up @@ -305,12 +305,6 @@ private ProcessPriorityClass PriorityClassCore
}
}

/// <summary>Gets the ID of the current process.</summary>
private static int GetCurrentProcessId()
{
return Interop.Sys.GetPid();
}

/// <summary>Checks whether the argument is a direct child of this process.</summary>
private bool IsParentOf(Process possibleChildProcess) =>
Id == possibleChildProcess.ParentProcessId;
Expand Down
Expand Up @@ -367,12 +367,6 @@ private IntPtr ProcessorAffinityCore
}
}

/// <summary>Gets the ID of the current process.</summary>
private static int GetCurrentProcessId()
{
return unchecked((int)Interop.Kernel32.GetCurrentProcessId());
}

/// <summary>
/// Gets a short-term handle to the process, with the given access. If a handle exists,
/// then it is reused. If the process has exited, it throws an exception.
Expand Down
Expand Up @@ -1069,7 +1069,7 @@ public static Process[] GetProcesses(string machineName)
/// </devdoc>
public static Process GetCurrentProcess()
{
return new Process(".", false, GetCurrentProcessId(), null);
return new Process(".", false, Environment.ProcessId, null);
}

/// <devdoc>
Expand Down
Expand Up @@ -96,7 +96,7 @@ private static ProcessModuleCollection GetModules(int processId, bool firstModul
SafeProcessHandle hCurProcess = SafeProcessHandle.InvalidHandle;
try
{
hCurProcess = ProcessManager.OpenProcess((int)Interop.Kernel32.GetCurrentProcessId(), Interop.Advapi32.ProcessOptions.PROCESS_QUERY_INFORMATION, true);
hCurProcess = ProcessManager.OpenProcess(Environment.ProcessId, Interop.Advapi32.ProcessOptions.PROCESS_QUERY_INFORMATION, true);

if (!Interop.Kernel32.IsWow64Process(hCurProcess, out bool sourceProcessIsWow64))
{
Expand Down
Expand Up @@ -10,9 +10,6 @@ namespace System.Diagnostics.Tests
{
internal partial class Interop
{
[DllImport("libc")]
internal static extern int getpid();

[DllImport("libc")]
internal static extern int getsid(int pid);
}
Expand Down
3 changes: 0 additions & 3 deletions src/libraries/System.Diagnostics.Process/tests/Interop.cs
Expand Up @@ -54,9 +54,6 @@ public struct SID_AND_ATTRIBUTES
[DllImport("kernel32.dll")]
public static extern bool GetProcessWorkingSetSizeEx(SafeProcessHandle hProcess, out IntPtr lpMinimumWorkingSetSize, out IntPtr lpMaximumWorkingSetSize, out uint flags);

[DllImport("kernel32.dll")]
internal static extern int GetCurrentProcessId();

[DllImport("kernel32.dll")]
internal static extern bool ProcessIdToSessionId(uint dwProcessId, out uint pSessionId);

Expand Down
16 changes: 10 additions & 6 deletions src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs
Expand Up @@ -408,11 +408,20 @@ public void StartTime_GetNotStarted_ThrowsInvalidOperationException()
Assert.Throws<InvalidOperationException>(() => process.StartTime);
}

[Fact]
public void GetCurrentProcess_Id_EqualsCurrentProcessId()
{
using Process current = Process.GetCurrentProcess();
Assert.Equal(Environment.ProcessId, current.Id);
}

[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
public void TestId()
{
CreateDefaultProcess();

Assert.NotEqual(Environment.ProcessId, _process.Id);

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Assert.Equal(_process.Id, Interop.GetProcessId(_process.SafeHandle));
Expand Down Expand Up @@ -1050,12 +1059,7 @@ public void TestGetCurrentProcess()
Process current = Process.GetCurrentProcess();
Assert.NotNull(current);

int currentProcessId =
#if TargetsWindows
Interop.GetCurrentProcessId();
#else
Interop.getpid();
#endif
int currentProcessId = Environment.ProcessId;

Assert.Equal(currentProcessId, current.Id);
}
Expand Down
Expand Up @@ -10,24 +10,6 @@
<Compile Include="System\Diagnostics\DelimitedListTraceListener.cs" />
<Compile Include="System\Diagnostics\TextWriterTraceListener.cs" />
<Compile Include="System\Diagnostics\XmlWriterTraceListener.cs" />
<Compile Include="$(CommonPath)System\Diagnostics\TraceListenerHelpers.cs"
Link="Common\System\Diagnostics\TraceListenerHelpers.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetsWindows)' == 'true'">
<Compile Include="$(CommonPath)System\Diagnostics\TraceListenerHelpers.Windows.cs"
Link="Common\System\Diagnostics\TraceListenerHelpers.Windows.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Interop.Libraries.cs"
Link="Common\Interop\Windows\Interop.Libraries.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GetCurrentProcessId.cs"
Link="Common\Interop\Windows\Interop.GetCurrentProcessId.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetsUnix)' == 'true' or '$(TargetsBrowser)' == 'true'">
<Compile Include="$(CommonPath)Interop\Unix\Interop.Libraries.cs"
Link="Common\Interop\Unix\Interop.Libraries.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.GetPid.cs"
Link="Common\Interop\Unix\Interop.GetPid.cs" />
<Compile Include="$(CommonPath)System\Diagnostics\TraceListenerHelpers.Unix.cs"
Link="Common\System\Diagnostics\TraceListenerHelpers.Unix.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.ComponentModel.Primitives" />
Expand Down
Expand Up @@ -15,6 +15,7 @@ public class XmlWriterTraceListener : TextWriterTraceListener
{
private const string FixedHeader = "<E2ETraceEvent xmlns=\"http://schemas.microsoft.com/2004/06/E2ETraceEvent\"><System xmlns=\"http://schemas.microsoft.com/2004/06/windows/eventlog/system\">";

private static volatile string? s_processName;
private readonly string _machineName = Environment.MachineName;
private StringBuilder? _strBldr;
private XmlTextWriter? _xmlBlobWriter;
Expand Down Expand Up @@ -254,14 +255,21 @@ private void WriteStartHeader(string source, TraceEventType eventType, int id, T
[ResourceConsumption(ResourceScope.Process, ResourceScope.Process)]
private void WriteEndHeader()
{
string? processName = s_processName;
if (processName is null)
{
using Process process = Process.GetCurrentProcess();
s_processName = processName = process.ProcessName;
}

InternalWrite("\" />");

InternalWrite("<Execution ProcessName=\"");
InternalWrite(TraceListenerHelpers.GetProcessName());
InternalWrite(processName);
InternalWrite("\" ProcessID=\"");
InternalWrite(((uint)TraceListenerHelpers.GetProcessId()).ToString(CultureInfo.InvariantCulture));
InternalWrite(((uint)Environment.ProcessId).ToString(CultureInfo.InvariantCulture));
InternalWrite("\" ThreadID=\"");
WriteEscaped(TraceListenerHelpers.GetThreadId().ToString(CultureInfo.InvariantCulture));
WriteEscaped(Environment.CurrentManagedThreadId.ToString(CultureInfo.InvariantCulture));
InternalWrite("\" />");

InternalWrite("<Channel/>");
Expand Down
Expand Up @@ -102,12 +102,9 @@ public void ListenerWithFilter()
{
// Ensure we use an arbitrary ID that doesn't match the process ID or thread ID.
int traceTransferId = 1;
using (Process p = Process.GetCurrentProcess())
while (traceTransferId == Environment.ProcessId || traceTransferId == Environment.CurrentManagedThreadId)
{
while (traceTransferId == p.Id || traceTransferId == Environment.CurrentManagedThreadId)
{
traceTransferId++;
}
traceTransferId++;
}

string file = GetTestFilePath();
Expand Down
Expand Up @@ -27,35 +27,15 @@
<Compile Include="System\Diagnostics\TraceSwitch.cs" />
<Compile Include="System\Diagnostics\SwitchAttribute.cs" />
<Compile Include="System\Diagnostics\SwitchLevelAttribute.cs" />
<Compile Include="$(CommonPath)System\Diagnostics\TraceListenerHelpers.cs"
Link="Common\System\Diagnostics\TraceListenerHelpers.cs" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetsWindows)' == 'true'">
<Compile Include="$(CommonPath)System\Diagnostics\TraceListenerHelpers.Windows.cs"
Link="Common\System\Diagnostics\TraceListenerHelpers.Windows.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Interop.Libraries.cs"
Link="Common\Interop\Windows\Interop.Libraries.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GetCurrentProcessId.cs"
Link="Common\Interop\Windows\Interop.GetCurrentProcessId.cs" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetsUnix)' == 'true' or '$(TargetsBrowser)' == 'true'">
<Compile Include="$(CommonPath)Interop\Unix\Interop.Libraries.cs"
Link="Common\Interop\Unix\Interop.Libraries.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.GetPid.cs"
Link="Common\Interop\Unix\Interop.GetPid.cs" />
<Compile Include="$(CommonPath)System\Diagnostics\TraceListenerHelpers.Unix.cs"
Link="Common\System\Diagnostics\TraceListenerHelpers.Unix.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\System.Collections\src\System.Collections.csproj" />
<ProjectReference Include="..\..\System.Collections.NonGeneric\src\System.Collections.NonGeneric.csproj" />
<ProjectReference Include="..\..\System.Collections.Specialized\src\System.Collections.Specialized.csproj " />
<ProjectReference Include="..\..\System.ComponentModel.Primitives\src\System.ComponentModel.Primitives.csproj " />
<ProjectReference Include="..\..\System.Diagnostics.Process\src\System.Diagnostics.Process.csproj " />
<ProjectReference Include="..\..\System.IO.FileSystem\src\System.IO.FileSystem.csproj " />
<ProjectReference Include="..\..\System.Runtime\src\System.Runtime.csproj" />
<ProjectReference Include="..\..\System.Runtime.Extensions\src\System.Runtime.Extensions.csproj" />
<ProjectReference Include="..\..\System.Runtime.InteropServices\src\System.Runtime.InteropServices.csproj" />
<ProjectReference Include="..\..\System.Threading\src\System.Threading.csproj" />
</ItemGroup>
<ItemGroup>
Expand Down
Expand Up @@ -29,15 +29,15 @@ public int ProcessId
{
get
{
return TraceListenerHelpers.GetProcessId();
return Environment.ProcessId;
}
}

public string ThreadId
{
get
{
return TraceListenerHelpers.GetThreadId().ToString(CultureInfo.InvariantCulture);
return Environment.CurrentManagedThreadId.ToString(CultureInfo.InvariantCulture);
}
}

Expand Down

0 comments on commit ea2b09b

Please sign in to comment.