Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,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 @@ -100,7 +100,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
}
}
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
Comment thread
stephentoub marked this conversation as resolved.
Outdated

if (!Interop.Kernel32.IsWow64Process(hCurProcess, out bool sourceProcessIsWow64))
{
Expand Down
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,6 @@
<Compile Include="System\Diagnostics\DelimitedListTraceListener.cs" />

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not need to have OS specific build anymore.

<TargetFrameworks>$(NetCoreAppCurrent)<TargetFrameworks>

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I tried that, but I got errors that I wasn't sure what to do with, and so decided to leave that for a follow-up, e.g.

C:\Users\stoub\.nuget\packages\microsoft.dotnet.build.tasks.targetframework.sdk\5.0.0-beta.20316.1\build\Microsoft.DotNet.Build.Tasks.TargetFramework.Sdk.targets(74,5): error : Not able to find a compatible supported target framework for net5.0 in Project System.Diagnostics.Process.csproj. The Supported Configurations are net5.0-Windows_NT, net5.0-FreeBSD, net5.0-Linux, net5.0-OSX, net5.0-iOS, net5.0-tvOS [D:\repos\runtime\src\libraries\System.Diagnostics.TraceSource\src\System.Diagnostics.TraceSource.csproj]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove the reference to System.Diagnostics.Process? Is its still used anywhere? IIRC there was a case for Process.GetCurrentProcess().ProcessName which was unused in some cases. Perhaps we can use a different pattern to obtain the name of the process, like AppDomain.CurrentDomain.FriendlyName 🤮 or add Environment.ProcessName.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ericstj, I can remove the Process reference, but then the error just becomes:

C:\Users\stoub\.nuget\packages\microsoft.dotnet.build.tasks.targetframework.sdk\5.0.0-beta.20316.1\build\Microsoft.DotNet.Build.Tasks.TargetFramework.Sdk.targets(74,5): error : Not able to find a compatible supported target framework for net5.0 in Project System.IO.FileSystem.csproj. The Supported Configurations are net5.0-Windows_NT, net5.0-Unix [D:\repos\runtime\src\libraries\System.Diagnostics.TraceSource\src\System.Diagnostics.TraceSource.csproj]

for FileSystem.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks to me like you're talking about a different project? This comment is on TextWriterTraceListener and the error mentions System.Diagnostics.TraceSource.csproj and I don't see any project references in TextWriterTraceListener.

In the case of TraceSource you might be able to get away with changing from a ProjectReference to a simple Reference. ProjectReferences are only needed when you need to use a type that lives in System.Private.CoreLib at runtime (to avoid the duplicate type error from compiler).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks to me like you're talking about a different project? This comment is on TextWriterTraceListener and the error mentions System.Diagnostics.TraceSource.csproj and I don't see any project references in TextWriterTraceListener.

I was talking about TraceSource (the below "Same here" comment was applying to TraceSource as well, so I've just been using that as a guinea pig). For TextWriterTraceListener, it does still use Process, to get the current process name.

In the case of TraceSource you might be able to get away with changing from a ProjectReference to a simple Reference.

The purpose of this PR is to add Environment.ProcessId. I'm not going to hold it up right now fiddling with the csproj. Please open an issue about consolidating the target frameworks if there isn't already one tracking that. Thanks!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, that's fine by me. /cc @Anipik

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can follow up with this and see where we can leverage this api and remove cross targeting

<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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,15 @@
<Compile Include="System\Diagnostics\TraceSwitch.cs" />
Comment thread
stephentoub marked this conversation as resolved.
Outdated
<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
Original file line number Diff line number Diff line change
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
Loading