Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions src/System.Management.Automation/CoreCLR/CorePsPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -883,20 +883,40 @@ public static int GetProcFSParentPid(int pid)
{
const int invalidPid = -1;

// read /proc/<pid>/stat
// 4th column will contain the ppid, 92 in the example below
// ex: 93 (bash) S 92 93 2 4294967295 ...
var path = $"/proc/{pid}/stat";
// read /proc/<pid>/status
// Row beginning with PPid: \d is the parent process id.
// This used to check /proc/<pid>/stat but that file was meant
// to be a space delimited line but it contains a value which
// could contain spaces itself. Using the status file is a lot
// simpler because each line contains a record with a simple
// label.
// https://github.com/PowerShell/PowerShell/issues/17541#issuecomment-1159911577
var path = $"/proc/{pid}/status";
try
{
var stat = System.IO.File.ReadAllText(path);
var parts = stat.Split(' ', 5);
if (parts.Length < 5)
using FileStream fs = File.OpenRead(path);
using StreamReader sr = new(fs);
string line;
while ((line = sr.ReadLine()) != null)
Comment thread
daxian-dbw marked this conversation as resolved.
Resolved
{
return invalidPid;
if (!line.StartsWith("PPid:\t", StringComparison.OrdinalIgnoreCase))
{
continue;
}

string[] lineSplit = line.Split('\t', 2, StringSplitOptions.RemoveEmptyEntries);
if (lineSplit.Length != 2)
Comment thread
jborean93 marked this conversation as resolved.
Resolved
{
continue;
}

if (int.TryParse(lineSplit[1].Trim(), out var ppid))
{
return ppid;
}
}

return int.Parse(parts[3]);
return invalidPid;
}
catch (Exception)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Describe "Get-Process for admin" -Tags @('CI', 'RequireAdminOnWindows') {
$pwshVersion.FileBuildPart | Should -BeExactly $PSVersionTable.PSVersion.Patch
$gitCommitId = $PSVersionTable.GitCommitId
if ($gitCommitId.StartsWith("v")) { $gitCommitId = $gitCommitId.Substring(1) }
$productVersion = $pwshVersion.ProductVersion.Replace(" Commits: ","-").Replace(" SHA: ","-g")
$productVersion = $pwshVersion.ProductVersion.Replace(" Commits: ", "-").Replace(" SHA: ", "-g")
$productVersion | Should -Match $gitCommitId
} else {
$pwshVersion.FileVersion | Should -BeNullOrEmpty
Expand All @@ -46,7 +46,7 @@ Describe "Get-Process" -Tags "CI" {
$idleProcessPid = 0
}
It "Should return a type of Object[] for Get-Process cmdlet" -Pending:$IsMacOS {
,$ps | Should -BeOfType System.Object[]
, $ps | Should -BeOfType System.Object[]
}

It "Should have not empty Name flags set for Get-Process object" -Pending:$IsMacOS {
Expand Down Expand Up @@ -78,7 +78,7 @@ Describe "Get-Process" -Tags "CI" {
(Get-Process -Id $PID).Id | Should -BeExactly $PID
}

It "Should fail to run Get-Process with -IncludeUserName without admin" -Skip:(!$IsWindows) {
It "Should fail to run Get-Process with -IncludeUserName without admin" -Skip:(!$IsWindows) {
{ Get-Process -IncludeUserName } | Should -Throw -ErrorId "IncludeUserNameRequiresElevation,Microsoft.PowerShell.Commands.GetProcessCommand"
}

Expand All @@ -95,7 +95,7 @@ Describe "Get-Process" -Tags "CI" {
{ Get-Process -FileVersionInfo -ErrorAction Stop } | Should -Throw -ErrorId "CouldNotEnumerateFileVer,Microsoft.PowerShell.Commands.GetProcessCommand"
}

It "Should return CommandLine property" -Skip:($IsMacOS) {
It "Should return CommandLine property" -Skip:($IsMacOS) {
$command = "(Get-Process -Id `$pid).CommandLine"
$result = & "$PSHOME/pwsh" -NoProfile -NonInteractive -Command $command
$result | Should -BeLike "*$command*"
Expand All @@ -105,19 +105,18 @@ Describe "Get-Process" -Tags "CI" {
Describe "Get-Process Formatting" -Tags "Feature" {
BeforeAll {
$skip = $false
if ($IsWindows)
{
if ($IsWindows) {
# on Windows skip this test until issue #11016 is resolved
$skip = $true
}
}

It "Should not have Handle in table format header" -Skip:$skip {
$types = "System.Diagnostics.Process","System.Diagnostics.Process#IncludeUserName"
$types = "System.Diagnostics.Process", "System.Diagnostics.Process#IncludeUserName"

foreach ($type in $types) {
$formatData = Get-FormatData -TypeName $type -PowerShellVersion $PSVersionTable.PSVersion
$tableControls = $formatData.FormatViewDefinition | Where-Object {$_.Control -is "System.Management.Automation.TableControl"}
$tableControls = $formatData.FormatViewDefinition | Where-Object { $_.Control -is "System.Management.Automation.TableControl" }
foreach ($tableControl in $tableControls) {
$tableControl.Control.Headers.Label -match "Handle*" | Should -BeNullOrEmpty
# verify that rows without headers isn't the handlecount (as PowerShell will create a header that matches the property name)
Expand All @@ -138,5 +137,26 @@ Describe "Process Parent property" -Tags "CI" {
$powershellexe = (Get-Process -Id $PID).mainmodule.filename
& $powershellexe -noprofile -command '(Get-Process -Id $PID).Parent.Id' | Should -Be $PID
}

It "Can find parent with spaces and parenthesis in the name on non-Windows" -Skip:($IsWindows) {
# Bug. See https://github.com/PowerShell/PowerShell/issues/12908
$commandName = 't ( e ( s ) t )'

$script = @'
#!/bin/sh

while true; do sleep 1; done
'@

# Can't use testdrive: as unelevated user doesn't have perms in test in CI

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please reword:

Suggested change
# Can't use testdrive: as unelevated user doesn't have perms in test in CI
# Can't use 'Testdrive:' as unelevated user doesn't have permissions to do 'chmod'.

I guess it is because .Net 7 RC1 has a breaking change in Directory.Create() where chmod mask was changed. If so, it might still break something in PowerShell or Pester.
/cc Jakub Jareš (@nohwnd)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assumed this was just a byproduct of how PowerShell CI is set up. I've not looked into it but I believe the unelevated user may potentially be inheriting the same tmpdir as the elevated account and thus cannot set the execute bit on files in that dir. This is just a guess so your assumption could also be true.

Set-Content -Path /tmp/$commandName -Value $script
chmod +x (Resolve-Path -Path /tmp/$commandName -Relative)
try {
$p = Start-Process -FilePath /tmp/$commandName -PassThru
$p.Parent.Id | Should -Be $pid
} finally {
$p | Stop-Process
}
}
}