Read ParentProcessId from kinfo_proc for FreeBSD (dotnet/corefx#42517)
authorAdeel Mujahid <adeelbm@outlook.com>
Mon, 11 Nov 2019 16:55:54 +0000 (18:55 +0200)
committerStephen Toub <stoub@microsoft.com>
Mon, 11 Nov 2019 16:55:54 +0000 (11:55 -0500)
* Read ParentProcessId from kinfo_proc for FreeBSD

* Update src/System.Diagnostics.Process/src/System/Diagnostics/Process.FreeBSD.cs

Co-Authored-By: Stephen Toub <stoub@microsoft.com>
* Update src/System.Diagnostics.Process/src/System/Diagnostics/Process.FreeBSD.cs

Co-Authored-By: Stephen Toub <stoub@microsoft.com>
Commit migrated from https://github.com/dotnet/corefx/commit/7ff22479f523ea468577daf6819005458f475064

src/libraries/Common/src/Interop/FreeBSD/Interop.Process.cs
src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.FreeBSD.cs

index affa726..5713ab2 100644 (file)
@@ -213,15 +213,13 @@ internal static partial class Interop
         /// <returns>Returns a list of PIDs corresponding to all running processes</returns>
         internal static unsafe int[] ListAllPids()
         {
-            int numProcesses = 0;
             int[] pids;
-            kinfo_proc * entries = null;
+            kinfo_proc * entries = GetProcInfo(0, false, out int numProcesses);
             int idx;
 
             try
             {
-                entries = GetProcInfo(0, false, out numProcesses);
-                if (entries == null || numProcesses <= 0)
+                if (numProcesses <= 0)
                 {
                     throw new Win32Exception(SR.CantGetAllPids);
                 }
@@ -340,20 +338,18 @@ internal static partial class Interop
         /// </returns>
         public static unsafe ProcessInfo GetProcessInfoById(int pid)
         {
-            kinfo_proc* kinfo = null;
-            int count;
-            ProcessInfo info;
-
             // Negative PIDs are invalid
             if (pid < 0)
             {
                 throw new ArgumentOutOfRangeException(nameof(pid));
             }
 
+            kinfo_proc* kinfo = GetProcInfo(pid, true, out int count);
+            ProcessInfo info;
+
             try
             {
-                kinfo = GetProcInfo(pid, true, out count);
-                if (kinfo == null || count < 1)
+                if (count < 1)
                 {
                     throw new ArgumentOutOfRangeException(nameof(pid));
                 }
index c2b41f3..929f125 100644 (file)
@@ -4,6 +4,7 @@
 
 using System.Collections.Generic;
 using System.ComponentModel;
+using System.Runtime.InteropServices;
 
 namespace System.Diagnostics
 {
@@ -64,8 +65,28 @@ namespace System.Diagnostics
         }
 
         /// <summary>Gets parent process ID</summary>
-        private int ParentProcessId =>
-            throw new PlatformNotSupportedException();
+        private unsafe int ParentProcessId
+        {
+            get
+            {
+                EnsureState(State.HaveNonExitedId);
+
+                Interop.Process.kinfo_proc* processInfo = Interop.Process.GetProcInfo(_processId, false, out int count);
+                try
+                {
+                    if (count <= 0)
+                    {
+                        throw new Win32Exception(SR.ProcessInformationUnavailable);
+                    }
+
+                    return processInfo->ki_ppid;
+                }
+                finally
+                {
+                    Marshal.FreeHGlobal((IntPtr) processInfo);
+                }
+            }
+        }
 
         // <summary>Gets execution path</summary>
         private string GetPathToOpenFile()