Skip to content
Closed
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
6 changes: 3 additions & 3 deletions src/System.Management.Automation/CoreCLR/CorePsPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -976,13 +976,13 @@ public static int GetProcFSParentPid(int pid)
try
{
var stat = System.IO.File.ReadAllText(path);
var parts = stat.Split(' ', 5);
if (parts.Length < 5)
var parts = stat.Substring(stat.LastIndexOf(')')).Split(' ', 4);
if (parts.Length < 4)
{
return invalidPid;
}

return Int32.Parse(parts[3]);
return Int32.Parse(parts[2]);
}
catch (Exception)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,17 @@ Describe "Process Parent property" -Tags "CI" {
& $powershellexe -noprofile -command '(Get-Process -Id $PID).Parent' | Should -Not -BeNullOrEmpty
}

It "Has Parent process property in linux" -Skip:(-not $IsLinux) {
# Added new test to check for bug as described in issue https://github.com/PowerShell/PowerShell/issue/12908
$powershellexe = (Get-Process -Name "$((Get-Process -Id $PID).Name)").mainmodule.filename
& $powershellexe -noprofile -command "Set-Content -Path .\'t ( e ( s ) t ).txt' -Value '#!/bin/bash`r`n./$PSHOME/pwsh`r`nchmod +x ./ " + "'t ( e ( s ) t )\'"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You don't and shouldn't create a new pwsh to run these commands. Here's what I recommend changing this to (you'll need to test this as I'm typing it freehand in this edit box):

$commandName = 't ( e ( s ) t )'

$script = @"
#!/bin/sh
pwsh -noprofile -command 'while ($true) { Start-Sleep -Seconds 10 }'
"@

Set-Content -Path testdrive:/$commandName -Value $script
chmod +x (resolve-path testdrive:/$commandName -Relative)
try {
  $p = start-process -FilePath testdrive:/$commandName -PassThru
  (Get-Process $commandName).Parent | Should -Be $pid
}
finally {
  $p | Stop-Process
}

& $powershellexe -noprofile -command "./'t ( e ( s ) t )'"
& $powershellexe -noprofile -command '(Get-Process -Name "t ( e ( s ) t )").Parent' | Should -Not -BeNullOrEmpty
}

It "Has valid parent process ID property" -Pending {
# Bug. See https://github.com/PowerShell/PowerShell/issues/12908
$powershellexe = (Get-Process -Id $PID).mainmodule.filename
& $powershellexe -noprofile -command '(Get-Process -Id $PID).Parent.Id' | Should -Be $PID
}
}