-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Fix check pid existence when running in different process namespace (container) #821
Fix check pid existence when running in different process namespace (container) #821
Conversation
Indeed. It could break cgroup independency, but if a user does not want to allow a process can watch a process existance out side of a container, simply does not add environment variable. So I agree to this PR. so basically I agree to this great PR, but could you |
process/process_posix.go
Outdated
@@ -5,6 +5,7 @@ package process | |||
import ( | |||
"context" | |||
"fmt" | |||
"github.com/shirou/gopsutil/internal/common" |
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 follow our convention to separate stdlib and 3rd-party packages in import statements.
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.
Done, gofmt was surprisingly switched off :)
process/process_posix.go
Outdated
file, err := os.Open(common.HostProc(strconv.Itoa(int(pid)))) | ||
defer file.Close() | ||
if err!=nil{return false, err} else{return true, nil} | ||
}else{ //Assume that we are in the same process namespace, try to signal process |
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.
Useless else
as we always return in the if
.
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 and below, just restructured this part taking into consideration current and all of the comments below.
process/process_posix.go
Outdated
case syscall.EPERM: | ||
return true, nil | ||
|
||
if ! strings.HasPrefix(common.HostProc(),"/proc") { //Means that proc is mounted not to /proc, and we highly |
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 code is called on freebsd openbsd darwin where there is no procfs, could lead to downstream breakage if users blindly set the HOST_PROC
variable on every OS. Probably check for common.HostProc()
existence instead/also.
process/process_posix.go
Outdated
if ! strings.HasPrefix(common.HostProc(),"/proc") { //Means that proc is mounted not to /proc, and we highly | ||
// likely running inside container with a different process namespace (by default), so we check pid existence | ||
// based on /<HOST_PROC>/proc/<PID> folder | ||
file, err := os.Open(common.HostProc(strconv.Itoa(int(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.
Use os.Stat
instead as in this commit 2583438
process/process_posix.go
Outdated
return true, nil | ||
|
||
if ! strings.HasPrefix(common.HostProc(),"/proc") { //Means that proc is mounted not to /proc, and we highly | ||
// likely running inside container with a different process namespace (by default), so we check pid existence |
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.
Likewise, all your comments assume we're running on Linux, which is confusing because this code is common to all POSIX platforms.
4c34bd1
to
cf14906
Compare
Overall, I followed this logic:
|
defer file.Close() | ||
if err!=nil{return false, err} else{return true, nil} | ||
}else{ //Assume that we are in the same process namespace, try to signal process | ||
if _, err := os.Stat(common.HostProc()); err == nil { //Means that proc filesystem exist |
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.
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.
These os.Stat
target are different and second one is a fallback. so we can not merge.
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.
Well, they are. But maybe my point is not clear. What I'm trying to say that the os.Stat(common.HostProc()) call is useless to be made every time when we check pid existence, this is something that could be checked once at startup or init. Possible use case here is when you have an array of PIDs (let's call it pidArray
) and you check every entry in cycle for PID existence. In this case you have n
useless os.Stat
calls, where n=len(pidArray)
.
Thank you @Lomanic and @i-prudnikov . LGTM. If you don't have a comment, please merge this PR. |
@i-prudnikov please squash your commits as to preserve lines history. Or @shirou please enable this option for merges. Thanks. |
Oh, roger. I have enabled squash merge! |
cf14906
to
8dec3d8
Compare
LGTM now. Thank you for your contribution! |
This shouldn't have been merged as now the following test is failing:
A non-running process should not return an error. We really need a better CI (#734) to catch this kind of regressions. |
@Lomanic , could you please provide the commit SHA from which you run the failed test. I'll try to investigate what goes wrong. I see that I have mismatched process_test.go file in my source branch. |
@Lomanic, I've run test against master branch, the root case is the returned error from
I've opened a new merge request for that fix: #840 |
This fix is to address issue mentioned in #713, originally comes from influxdata/telegraf#6813