mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
# PR Summary Automated fix of [RCS1018](https://github.com/JosefPihrt/Roslynator/blob/master/docs/analyzers/RCS1018.md) ## PR Context ## PR Checklist - [x] [PR has a meaningful title](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission) - Use the present tense and imperative mood when describing your changes - [x] [Summarized changes](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission) - [x] [Make sure all `.h`, `.cpp`, `.cs`, `.ps1` and `.psm1` files have the correct copyright header](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission) - [x] This PR is ready to merge and is not [Work in Progress](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---work-in-progress). - If the PR is work in progress, please add the prefix `WIP:` or `[ WIP ]` to the beginning of the title (the `WIP` bot will keep its status check at `Pending` while the prefix is present) and remove the prefix when the PR is ready. - **[Breaking changes](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#making-breaking-changes)** - [x] None - **OR** - [ ] [Experimental feature(s) needed](https://github.com/MicrosoftDocs/PowerShell-Docs/blob/staging/reference/6/Microsoft.PowerShell.Core/About/about_Experimental_Features.md) - [ ] Experimental feature name(s): <!-- Experimental feature name(s) here --> - **User-facing changes** - [x] Not Applicable - **OR** - [ ] [Documentation needed](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission) - [ ] Issue filed: <!-- Number/link of that issue here --> - **Testing - New and feature** - [x] N/A or can only be tested interactively - **OR** - [ ] [Make sure you've added a new test if existing tests do not effectively test the code changed](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#before-submitting) - **Tooling** - [x] I have considered the user experience from a tooling perspective and don't believe tooling will be impacted. - **OR** - [ ] I have considered the user experience from a tooling perspective and enumerated concerns in the summary. This may include: - Impact on [PowerShell Editor Services](https://github.com/PowerShell/PowerShellEditorServices) which is used in the [PowerShell extension](https://github.com/PowerShell/vscode-powershell) for VSCode (which runs in a different PS Host). - Impact on Completions (both in the console and in editors) - one of PowerShell's most powerful features. - Impact on [PSScriptAnalyzer](https://github.com/PowerShell/PSScriptAnalyzer) (which provides linting & formatting in the editor extensions). - Impact on [EditorSyntax](https://github.com/PowerShell/EditorSyntax) (which provides syntax highlighting with in VSCode, GitHub, and many other editors).
93 lines
3.0 KiB
C#
93 lines
3.0 KiB
C#
// Copyright (c) Microsoft Corporation.
|
|
// Licensed under the MIT License.
|
|
|
|
using System;
|
|
using System.Diagnostics;
|
|
using System.Management.Automation;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace Microsoft.PowerShell
|
|
{
|
|
/// <summary>
|
|
/// Helper functions for process info.
|
|
/// </summary>
|
|
public static class ProcessCodeMethods
|
|
{
|
|
private const int InvalidProcessId = -1;
|
|
|
|
internal static Process GetParent(this Process process)
|
|
{
|
|
try
|
|
{
|
|
var pid = GetParentPid(process);
|
|
if (pid == InvalidProcessId)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var candidate = Process.GetProcessById(pid);
|
|
|
|
// if the candidate was started later than process, the pid has been recycled
|
|
return candidate.StartTime > process.StartTime ? null : candidate;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// CodeMethod for getting the parent process of a process.
|
|
/// </summary>
|
|
/// <param name="obj"></param>
|
|
/// <returns>The parent process, or null if the parent is no longer running.</returns>
|
|
public static object GetParentProcess(PSObject obj)
|
|
{
|
|
var process = PSObject.Base(obj) as Process;
|
|
return process?.GetParent();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the parent id of a process or -1 if it fails.
|
|
/// </summary>
|
|
/// <param name="process"></param>
|
|
/// <returns>The pid of the parent process.</returns>
|
|
#if UNIX
|
|
internal static int GetParentPid(Process process)
|
|
{
|
|
return Platform.NonWindowsGetProcessParentPid(process.Id);
|
|
}
|
|
#else
|
|
internal static int GetParentPid(Process process)
|
|
{
|
|
Diagnostics.Assert(process != null, "Ensure process is not null before calling");
|
|
PROCESS_BASIC_INFORMATION pbi;
|
|
int size;
|
|
var res = NtQueryInformationProcess(process.Handle, 0, out pbi, Marshal.SizeOf<PROCESS_BASIC_INFORMATION>(), out size);
|
|
|
|
return res != 0 ? InvalidProcessId : pbi.InheritedFromUniqueProcessId.ToInt32();
|
|
}
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
private struct PROCESS_BASIC_INFORMATION
|
|
{
|
|
public IntPtr ExitStatus;
|
|
public IntPtr PebBaseAddress;
|
|
public IntPtr AffinityMask;
|
|
public IntPtr BasePriority;
|
|
public IntPtr UniqueProcessId;
|
|
public IntPtr InheritedFromUniqueProcessId;
|
|
}
|
|
|
|
[DllImport("ntdll.dll", SetLastError = true)]
|
|
private static extern int NtQueryInformationProcess(
|
|
IntPtr processHandle,
|
|
int processInformationClass,
|
|
out PROCESS_BASIC_INFORMATION processInformation,
|
|
int processInformationLength,
|
|
out int returnLength);
|
|
#endif
|
|
|
|
}
|
|
}
|