8000 Adding Parent extended property to System.Diagnostics.Process by powercode · Pull Request #2850 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

Adding Parent extended property to System.Diagnostics.Process #2850

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 6, 2017

Conversation

powercode
Copy link
Collaborator
@powercode powercode commented Dec 7, 2016

Fixes #2763
System.Management.Automation:
Adding ProcessCodeMethods.cs to do the pinvokes and the mandatory timestamp check
for parent processes.
Adding 'Parent' CodeProperty to Process in Types_Ps1Xml.generates.cs
For unix, /proc/<pid>/stat is parsed.
Moving addition of process PSProperties to SMA ClrFacade.

@PowerShellTeam PowerShellTeam added the Review - Needed The PR is being reviewed label Dec 7, 2016
/// Read /proc/pid/stat at fourth column to determine
/// the parent process id
///
pid_t GetParentProcessId(pid_t pid)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why native code? Isn't reading /proc/id/stat simple in C#?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

But of course! Got pointed to getppid and started out on that path. Fixed.

@powercode powercode force-pushed the process-parentpid branch 3 times, most recently from 7d5bb02 to 9fc9904 Compare December 7, 2016 08:10
@iSazonov
Copy link
Collaborator
iSazonov commented Dec 7, 2016

I think that this property will rarely be used. May be better to expose the method GetParentProcessId rather than the property?

@lzybkr lzybkr self-assigned this Dec 9, 2016
@powercode powercode force-pushed the process-parentpid branch 2 times, most recently from ba2ec10 to 13be807 Compare December 31, 2016 11:32
Copy link
Contributor
@lzybkr lzybkr left a comment

Choose a reason for hiding this comment

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

The code changes look good. We should have a test.

#endif

}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please add Newline.

@powercode
Copy link
Collaborator Author
powercode commented Jan 27, 2017

So what is reasonable tests for this?
Starting another version of powershell, checking that Parent.ID -eq $pid and then kill the child?
That processes in general has parents? Some doesn't, and for some we don't have access.
PowerShell seems like the only process we can know will exist on the system under test.

@iSazonov
Copy link
Collaborator

I would start with:

powershell.exe -Command { [???]::GetParentProcess($pid) } | Should Be $pid

@lzybkr
Copy link
Contributor
lzybkr commented Jan 27, 2017

Look for other tests that run PowerShell.exe to see how we run cross platform using $pshome.

@lzybkr
Copy link
Contributor
lzybkr commented Feb 27, 2017

@powercode - can you either give us permission to push to your branch, or add a test like:

Describe "Process Parent property" -Tags "CI" {
    It "Parent process property" {
        $powershellexe = (get-process -id $PID).mainmodule.filename
        & $powershellexe -noprofile -command '(Get-Process -Id $pid).Parent.Id' | Should Be $pid
    }
}

If you add the test yourself, please rebase on master as we've fixed an issue that was causing many hangs in CI and we'll want that to test your change in CI.

@powercode
Copy link
Collaborator Author

Sorry for the delay. On sabbatical. Added the test as suggested.

@lzybkr
Copy link
Contributor
lzybkr commented Feb 27, 2017

Thanks for adding a test - the test fails on Mac. If you don't have a Mac to investigate, maybe we can find someone to help track down why it works on Linux but not Mac.

@lzybkr lzybkr removed the Stale label Feb 27, 2017
@iSazonov
Copy link
Collaborator

@powercode
Copy link
Collaborator Author
powercode commented Feb 27, 2017

@iSazonov No, I parse /proc/$pid. But maybe that info reflects the same information. Anyone has access to a mac to see how the output of /proc/$pid looks?

@iSazonov
Copy link
Collaborator

@vors Can you help with Mac?

@powercode
Copy link
Collaborator Author

http://superuser.com/questions/631693/where-is-the-proc-folder-on-mac-os-x#631696

OS X (which is based on BSD) does not implement a procfs.

@powercode
Copy link
Collaborator Author

Is this a viable alternative?

ps -o ppid --no-headers --pid $pid

@iSazonov
Copy link
Collaborator
iSazonov commented Feb 27, 2017

If Powershell is in sandbox we get empty as with sysctl.

I suggest don't block the PR, pending the test for Mac and open Issue to fix this problem.

@powercode
Copy link
Collaborator Author
powercode commented Mar 1, 2017

How about something like this in libpsl-native

pid_t GetPPid(pid_t pid)
{
    const pid_t PIDUnknown = UINT_MAX;
    struct kinfo_proc info;
    size_t length = sizeof(struct kinfo_proc);
    int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};
    if (sysctl(mib, 4, &info, &length, NULL, 0) < 0)
        return PIDUnknown;
    if (length == 0)
        return PIDUnknown;
    return info.kp_eproc.e_ppid;
}

And maybe switch the Linux version to using that implementation too?

@lzybkr
Copy link
Contributor
lzybkr commented Mar 1, 2017

1 implementation that works on Mac/Linux is ideal, so if it works, go for it.

@powercode powercode force-pushed the process-parentpid branch 3 times, most recently from 65b9306 to 42b5d25 Compare March 2, 2017 01:57
@powercode powercode force-pushed the process-parentpid branch 4 times, most recently from 2558824 to a50c69b Compare March 2, 2017 03:01
@powercode
Copy link
Collaborator Author

I moved some code around (to Platform.Unix).
All sysctl's aren't implemented across FreeBSD/Linux and the kinfo_proc seems to be one of them. So we have a sysctl for OSX and parsing of procfs for Linux.

#else
return UINT_MAX;
#endif
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please add new line.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

added


pid_t GetPPid(pid_t pid);

PAL_END_EXTERNC
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please add new line.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

added

@powercode powercode force-pushed the process-parentpid branch from a50c69b to 17441c7 Compare March 2, 2017 16:03
@lzybkr lzybkr requested a review from andyleejordan March 2, 2017 17:09
@lzybkr
Copy link
Contributor
lzybkr commented Mar 6, 2017

@andschwa - it'd be great if you can review the native Linux/Mac code here.

{
return invalidPid;
}
return Int32.Parse(parts[3]);
Copy link
Member

Choose a reason for hiding this comment

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

Tested this out, looks good to me.


#else

return UINT_MAX;
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps we should simply not compile this GetPPid function at all when not on Mac (i.e., move the #if defined ... #endif to surround the function definition and add one around the declaration). I'm honestly not sure which would be more maintainable: a "usable" (but not implemented) function on a platform always returning an invalid PID, or a runtime error from PowerShell not finding the symbol on attempted use because the function was never compiled; @lzybkr what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

An invalid PID is probably more user friendly than an obscure error

Copy link
Member
@andyleejordan andyleejordan Mar 6, 2017

Choose a reason for hiding this comment

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

This would be a developer problem: if a Linux dev accidentally tries to use GetPPid, as is, they function will be called correctly, but only ever return UINT_MAX, which to me seems like it may be more confusing than an exception stating it couldn't call the function at all (since there are several return paths for UINT_MAX). Either way I think they'll end up looking at the native source pretty quickly, and should be able to figure out their problem (to use the /proc version instead). But I don't think a user is going to run into this.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think an invalid pid is less confusing than an obscure error, but I can see your point of view too. The bottom line is - it shouldn't matter - if either leaks, we messed up somehow.

if (length == 0)
return PIDUnknown;

return info.kp_eproc.e_ppid;
Copy link
Member

Choose a reason for hiding this comment

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

👍

@lzybkr lzybkr merged commit b404987 into PowerShell:master Mar 6, 2017
@powercode powercode deleted the process-parentpid branch April 25, 2017 18:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants
0