Use platform specific implementation of GetCurrentProcessId (dotnet/coreclr#23084)
authorMarek Safar <marek.safar@gmail.com>
Fri, 8 Mar 2019 07:27:54 +0000 (08:27 +0100)
committerJan Kotas <jkotas@microsoft.com>
Fri, 8 Mar 2019 07:27:54 +0000 (23:27 -0800)
* Use platform specific implementation of GetCurrentProcessId

directly in shared location instead of relying on runtime export

* Move GetCurrentProcessId to Interop class

* Update ES_BUILD_STANDALONE build

Commit migrated from https://github.com/dotnet/coreclr/commit/0fee55abb6b7c742cb487b12706dc54080e9e128

src/coreclr/src/System.Private.CoreLib/System.Private.CoreLib.csproj
src/coreclr/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/EventPipeController.cs
src/libraries/System.Private.CoreLib/src/Interop/Unix/System.Native/Interop.GetPid.cs [new file with mode: 0644]
src/libraries/System.Private.CoreLib/src/Interop/Windows/Kernel32/Interop.GetCurrentProcessId.cs
src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems
src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs
src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/StubEnvironment.cs

index affbf3e..816d3ce 100644 (file)
     <Compile Include="$(BclSourcesRoot)\System\ValueType.cs" />
     <Compile Include="$(BclSourcesRoot)\System\WeakReference.cs" />
     <Compile Include="$(BclSourcesRoot)\System\WeakReferenceOfT.cs" />
-    <Compile Include="shared\Interop\Windows\Kernel32\Interop.GetCurrentProcessId.cs" />
     <Compile Include="shared\Interop\Windows\Kernel32\Interop.GetStdHandle.cs" />
     <Compile Include="shared\Interop\Windows\Kernel32\Interop.LocalAlloc.cs" />
     <Compile Include="shared\Interop\Windows\Kernel32\Interop.QueryUnbiasedInterruptTime.cs" />
index c03ba4f..cac4ac1 100644 (file)
@@ -212,7 +212,7 @@ namespace System.Diagnostics.Tracing
             {
                 // If set, bail out early if the specified process does not match the current process.
                 int processID = Convert.ToInt32(strProcessID);
-                if (processID != Interop.Kernel32.GetCurrentProcessId())
+                if (processID != (int)Interop.GetCurrentProcessId())
                 {
                     return null;
                 }
@@ -294,7 +294,7 @@ namespace System.Diagnostics.Tracing
 
         private static string BuildTraceFileName()
         {
-            return GetAppName() + "." + Interop.Kernel32.GetCurrentProcessId().ToString() + NetPerfFileExtension;
+            return GetAppName() + "." + Interop.GetCurrentProcessId().ToString() + NetPerfFileExtension;
         }
 
         private static string GetAppName()
diff --git a/src/libraries/System.Private.CoreLib/src/Interop/Unix/System.Native/Interop.GetPid.cs b/src/libraries/System.Private.CoreLib/src/Interop/Unix/System.Native/Interop.GetPid.cs
new file mode 100644 (file)
index 0000000..02d259d
--- /dev/null
@@ -0,0 +1,17 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+using System;
+using System.Runtime.InteropServices;
+
+internal static partial class Interop
+{
+    internal static partial class Sys
+    {
+        [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetPid")]
+        internal static extern int GetPid();
+    }
+
+    internal static uint GetCurrentProcessId() => (uint)Sys.GetPid();
+}
index a042bfe..3a1a353 100644 (file)
@@ -11,4 +11,6 @@ internal partial class Interop
         [DllImport(Libraries.Kernel32)]
         internal extern static uint GetCurrentProcessId();
     }
+
+    internal static uint GetCurrentProcessId() => Kernel32.GetCurrentProcessId();
 }
index ea3c7cc..a168f09 100644 (file)
     <Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.FlushFileBuffers.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.GetComputerName.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.GetCPInfo.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.GetCurrentProcessId.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.GetCurrentProcess_IntPtr.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.GetCurrentDirectory.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Interop\Windows\Kernel32\Interop.GetFileAttributesEx.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Interop\Unix\System.Native\Interop.GetCwd.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Interop\Unix\System.Native\Interop.GetEUid.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Interop\Unix\System.Native\Interop.GetHostName.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)Interop\Unix\System.Native\Interop.GetPid.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Interop\Unix\System.Native\Interop.GetPwUid.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Interop\Unix\System.Native\Interop.GetRandomBytes.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Interop\Unix\System.Native\Interop.GetSystemTimeAsTicks.cs" />
index de94f4f..3a18dce 100644 (file)
@@ -2698,7 +2698,7 @@ namespace System.Diagnostics.Tracing
                 // for non-BCL EventSource we must assert SecurityPermission
                 new SecurityPermission(PermissionState.Unrestricted).Assert();
 #endif
-                s_currentPid = Interop.Kernel32.GetCurrentProcessId();
+                s_currentPid = Interop.GetCurrentProcessId();
             }
         }
 
index 4a054c9..d376389 100644 (file)
@@ -377,5 +377,7 @@ internal static partial class Interop
         [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
         internal static extern uint GetCurrentProcessId();
     }
+
+    internal static uint GetCurrentProcessId() => Kernel32.GetCurrentProcessId();
 }
 #endif