Merge pull request #821 from devopsext/process_check_pid_exnistense_fix

Fix check pid existence when running in different process namespace (container)
This commit is contained in:
shirou 2020-02-09 01:25:28 +09:00 committed by GitHub
commit a9d510e7e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 0 deletions

View File

@ -12,6 +12,7 @@ import (
"strings"
"syscall"
"github.com/shirou/gopsutil/internal/common"
"golang.org/x/sys/unix"
)
@ -78,6 +79,16 @@ func PidExistsWithContext(ctx context.Context, pid int32) (bool, error) {
if err != nil {
return false, err
}
if _, err := os.Stat(common.HostProc()); err == nil { //Means that proc filesystem exist
// Checking PID existence based on existence of /<HOST_PROC>/proc/<PID> folder
// This covers the case when running inside container with a different process namespace (by default)
_, err := os.Stat(common.HostProc(strconv.Itoa(int(pid))))
return err == nil, err
}
//'/proc' filesystem is not exist, checking of PID existence is done via signalling the process
//Make sense only if we run in the same process namespace
err = proc.Signal(syscall.Signal(0))
if err == nil {
return true, nil
@ -95,6 +106,7 @@ func PidExistsWithContext(ctx context.Context, pid int32) (bool, error) {
case syscall.EPERM:
return true, nil
}
return false, err
}