Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Merged
lzybkr merged 5 commits into
PowerShell:masterPowerShell/PowerShell:masterfrom
powercode:process-parentpidpowercode/PowerShell:process-parentpidCopy head branch name to clipboard
Mar 6, 2017
Merged

Adding Parent extended property to System.Diagnostics.Process#2850
lzybkr merged 5 commits into
PowerShell:masterPowerShell/PowerShell:masterfrom
powercode:process-parentpidpowercode/PowerShell:process-parentpidCopy head branch name to clipboard

Conversation

@powercode

@powercode powercode commented Dec 7, 2016

Copy link
Copy Markdown
Collaborator

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

Copy link
Copy Markdown
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
Copy Markdown
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

iSazonov commented Dec 7, 2016

Copy link
Copy Markdown
Collaborator

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

@lzybkr lzybkr left a comment

Copy link
Copy Markdown
Contributor

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.

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 add Newline.

@powercode

powercode commented Jan 27, 2017

Copy link
Copy Markdown
Collaborator Author

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
Copy Markdown
Collaborator

I would start with:

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

@lzybkr

lzybkr commented Jan 27, 2017

Copy link
Copy Markdown
Contributor

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

@lzybkr

lzybkr commented Feb 27, 2017

Copy link
Copy Markdown
Contributor

@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
Copy Markdown
Collaborator Author

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

@lzybkr

lzybkr commented Feb 27, 2017

Copy link
Copy Markdown
Contributor

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
Copy Markdown
Collaborator

@powercode

powercode commented Feb 27, 2017

Copy link
Copy Markdown
Collaborator Author

@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
Copy Markdown
Collaborator

@vors Can you help with Mac?

@powercode

Copy link
Copy Markdown
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
Copy Markdown
Collaborator Author

Is this a viable alternative?

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

@iSazonov

iSazonov commented Feb 27, 2017

Copy link
Copy Markdown
Collaborator

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

powercode commented Mar 1, 2017

Copy link
Copy Markdown
Collaborator Author

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

lzybkr commented Mar 1, 2017

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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.

Comment thread src/libpsl-native/src/getppid.cpp Outdated

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 add new line.

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.

added

Comment thread src/libpsl-native/src/getppid.h Outdated

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 add new line.

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.

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

lzybkr commented Mar 6, 2017

Copy link
Copy Markdown
Contributor

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

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

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.

Tested this out, looks good to me.


#else

return UINT_MAX;

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.

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
Copy Markdown
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

@andyleejordan andyleejordan Mar 6, 2017

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.

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
Copy Markdown
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
Copy Markdown
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

Morty Proxy This is a proxified and sanitized view of the page, visit original site.