From: Adeel Mujahid Date: Mon, 11 Nov 2019 16:55:54 +0000 (+0200) Subject: Read ParentProcessId from kinfo_proc for FreeBSD (dotnet/corefx#42517) X-Git-Tag: submit/tizen/20210909.063632~11031^2~38 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=7f823ccb877f35ddccd3a88d0ce5acf110d30955;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Read ParentProcessId from kinfo_proc for FreeBSD (dotnet/corefx#42517) * Read ParentProcessId from kinfo_proc for FreeBSD * Update src/System.Diagnostics.Process/src/System/Diagnostics/Process.FreeBSD.cs Co-Authored-By: Stephen Toub * Update src/System.Diagnostics.Process/src/System/Diagnostics/Process.FreeBSD.cs Co-Authored-By: Stephen Toub Commit migrated from https://github.com/dotnet/corefx/commit/7ff22479f523ea468577daf6819005458f475064 --- diff --git a/src/libraries/Common/src/Interop/FreeBSD/Interop.Process.cs b/src/libraries/Common/src/Interop/FreeBSD/Interop.Process.cs index affa726..5713ab2 100644 --- a/src/libraries/Common/src/Interop/FreeBSD/Interop.Process.cs +++ b/src/libraries/Common/src/Interop/FreeBSD/Interop.Process.cs @@ -213,15 +213,13 @@ internal static partial class Interop /// Returns a list of PIDs corresponding to all running processes 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 /// 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)); } diff --git a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.FreeBSD.cs b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.FreeBSD.cs index c2b41f3..929f125 100644 --- a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.FreeBSD.cs +++ b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.FreeBSD.cs @@ -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 } /// Gets parent process ID - 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); + } + } + } // Gets execution path private string GetPathToOpenFile()