-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
print: default output based on parent process name (#338)
### Why We can do better about choosing the output than just checking the OS. There's a chance someone could be using bash on windows or powershell on mac ### How Use Windows API and for linux / mac use some shell code to climb up the process tree and get the proper name --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: DotNet Format Bot <[email protected]>
- Loading branch information
1 parent
a0bd992
commit 51d4caa
Showing
4 changed files
with
113 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Diagnostics; | ||
|
||
namespace D2L.Bmx; | ||
|
||
/** | ||
This file may eventually be replaced if the following dotnet runtime api proposals are implemented: | ||
- https://github.com/dotnet/runtime/issues/21941 | ||
- https://github.com/dotnet/runtime/issues/24423 | ||
*/ | ||
internal class UnixParentProcess { | ||
|
||
public static string GetParentProcessName() { | ||
int parentPid = GetParentProcessId( Environment.ProcessId ); | ||
return Process.GetProcessById( parentPid ).ProcessName; | ||
} | ||
|
||
private static int GetParentProcessId( int pid ) { | ||
var proccessStartInfo = new ProcessStartInfo { | ||
FileName = "ps", | ||
ArgumentList = { "-p", $"{pid}", "-o", "ppid=" }, | ||
RedirectStandardOutput = true, | ||
}; | ||
|
||
using var proc = Process.Start( proccessStartInfo ); | ||
if( proc is null ) { | ||
return -1; | ||
} | ||
|
||
string output = proc.StandardOutput.ReadToEnd(); | ||
proc.WaitForExit(); | ||
if( int.TryParse( output.Trim(), out int parentPid ) ) { | ||
return parentPid; | ||
} | ||
return -1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System.Diagnostics; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace D2L.Bmx; | ||
/** | ||
This file may eventually be replaced if the following dotnet runtime api proposals are implemented: | ||
- https://github.com/dotnet/runtime/issues/21941 | ||
- https://github.com/dotnet/runtime/issues/24423 | ||
Uses the same approach of calling NtQueryInformationProcess as in the PowerShell library | ||
https://github.com/PowerShell/PowerShell/blob/26f621952910e33840efb0c539fbef1e2a467a0d/src/System.Management.Automation/engine/ProcessCodeMethods.cs | ||
*/ | ||
internal partial class WindowsParentProcess { | ||
|
||
[LibraryImport( "ntdll.dll", EntryPoint = "NtQueryInformationProcess" )] | ||
internal static partial int NtQueryInformationProcess( | ||
IntPtr processHandle, | ||
int processInformationClass, | ||
ref PROCESS_BASIC_INFORMATION processInformation, | ||
int processInformationLength, | ||
out int returnLength | ||
); | ||
|
||
internal struct PROCESS_BASIC_INFORMATION { | ||
public IntPtr ExitStatus; | ||
public IntPtr PebBaseAddress; | ||
public IntPtr AffinityMask; | ||
public IntPtr BasePriority; | ||
public UIntPtr UniqueProcessId; | ||
public UIntPtr InheritedFromUniqueProcessId; | ||
} | ||
|
||
private static int GetParentProcessId() { | ||
var proc = Process.GetCurrentProcess(); | ||
var pbi = new PROCESS_BASIC_INFORMATION(); | ||
int status = NtQueryInformationProcess( | ||
proc.Handle, | ||
0, | ||
ref pbi, | ||
Marshal.SizeOf<PROCESS_BASIC_INFORMATION>(), | ||
out int infoLen | ||
); | ||
|
||
if( status != 0 ) { | ||
return -1; | ||
} | ||
return (int)pbi.InheritedFromUniqueProcessId; | ||
} | ||
|
||
public static string GetParentProcessName() { | ||
return Process.GetProcessById( GetParentProcessId() ).ProcessName; | ||
} | ||
} |