@@ -71,6 +71,25 @@ func getTerminalMap() (map[uint64]string, error) {
71
71
return ret , nil
72
72
}
73
73
74
+ // isMount is a port of python's os.path.ismount()
75
+ // https://github.com/python/cpython/blob/08ff4369afca84587b1c82034af4e9f64caddbf2/Lib/posixpath.py#L186-L216
76
+ // https://docs.python.org/3/library/os.path.html#os.path.ismount
77
+ func isMount (path string ) bool {
78
+ var stat1 unix.Stat_t
79
+ if err := unix .Lstat (path , & stat1 ); err != nil {
80
+ return false
81
+ }
82
+ if stat1 .Mode == unix .DT_LNK {
83
+ return false
84
+ }
85
+ parent := filepath .Join (path , ".." )
86
+ var stat2 unix.Stat_t
87
+ if err := unix .Lstat (parent , & stat2 ); err != nil {
88
+ return false
89
+ }
90
+ return stat1 .Dev != stat2 .Dev || stat1 .Ino == stat2 .Ino
91
+ }
92
+
74
93
func PidExistsWithContext (ctx context.Context , pid int32 ) (bool , error ) {
75
94
if pid <= 0 {
76
95
return false , fmt .Errorf ("invalid pid %v" , pid )
@@ -80,19 +99,15 @@ func PidExistsWithContext(ctx context.Context, pid int32) (bool, error) {
80
99
return false , err
81
100
}
82
101
83
- if _ , err := os .Stat (common .HostProc ()); err == nil { //Means that proc filesystem exist
84
- // Checking PID existence based on existence of /<HOST_PROC>/proc/<PID> folder
85
- // This covers the case when running inside container with a different process namespace (by default)
86
-
102
+ if isMount (common .HostProc ()) { // if /<HOST_PROC>/proc exists and is mounted, check if /<HOST_PROC>/proc/<PID> folder exists
87
103
_ , err := os .Stat (common .HostProc (strconv .Itoa (int (pid ))))
88
104
if os .IsNotExist (err ) {
89
105
return false , nil
90
106
}
91
107
return err == nil , err
92
108
}
93
109
94
- //'/proc' filesystem is not exist, checking of PID existence is done via signalling the process
95
- //Make sense only if we run in the same process namespace
110
+ // procfs does not exist or is not mounted, check PID existence by signalling the pid
96
111
err = proc .Signal (syscall .Signal (0 ))
97
112
if err == nil {
98
113
return true , nil
@@ -158,4 +173,3 @@ func (p *Process) UsernameWithContext(ctx context.Context) (string, error) {
158
173
}
159
174
return "" , nil
160
175
}
161
-
0 commit comments