-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
Conversation
6de7d1a
to
32d657a
Compare
32d657a
to
446dde7
Compare
/// Read /proc/pid/stat at fourth column to determine | ||
/// the parent process id | ||
/// | ||
pid_t GetParentProcessId(pid_t pid) |
There was a problem hiding this comment.
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#?
There was a problem hiding this comment.
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.
7d5bb02
to
9fc9904
Compare
I think that this property will rarely be used. May be better to expose the method |
9fc9904
to
81cc8f9
Compare
ba2ec10
to
13be807
Compare
There was a problem hiding this 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 | ||
|
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add Newline
.
So what is reasonable tests for this? |
I would start with: powershell.exe -Command { [???]::GetParentProcess($pid) } | Should Be $pid |
Look for other tests that run PowerShell.exe to see how we run cross platform using $pshome. |
@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. |
13be807
to
f0bfc19
Compare
Sorry for the delay. On sabbatical. Added the test as suggested. |
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. |
f0bfc19
to
1c39358
Compare
@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? |
@vors Can you help with Mac? |
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. |
Is this a viable alternative?
|
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. |
How about something like this in libpsl-native
And maybe switch the Linux version to using that implementation too? |
1 implementation that works on Mac/Linux is ideal, so if it works, go for it. |
65b9306
to
42b5d25
Compare
2558824
to
a50c69b
Compare
I moved some code around (to Platform.Unix). |
src/libpsl-native/src/getppid.cpp
Outdated
#else | ||
return UINT_MAX; | ||
#endif | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add new line.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add new line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added
a50c69b
to
17441c7
Compare
@andschwa - it'd be great if you can review the native Linux/Mac code here. |
{ | ||
return invalidPid; | ||
} | ||
return Int32.Parse(parts[3]); |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
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.