|
|
|
@ -222,8 +222,9 @@ func PidExistsWithContext(ctx context.Context, pid int32) (bool, error) {
|
|
|
|
|
|
|
|
|
|
func (p *Process) PpidWithContext(ctx context.Context) (int32, error) {
|
|
|
|
|
// if cached already, return from cache
|
|
|
|
|
if p.parent != 0 {
|
|
|
|
|
return p.parent, nil
|
|
|
|
|
cachedPpid := p.getPpid()
|
|
|
|
|
if cachedPpid != 0 {
|
|
|
|
|
return cachedPpid, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ppid, _, _, err := getFromSnapProcess(p.Pid)
|
|
|
|
@ -231,8 +232,8 @@ func (p *Process) PpidWithContext(ctx context.Context) (int32, error) {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if no errors, cache it
|
|
|
|
|
p.parent = ppid
|
|
|
|
|
// no errors and not cached already, so cache it
|
|
|
|
|
p.setPpid(ppid)
|
|
|
|
|
|
|
|
|
|
return ppid, nil
|
|
|
|
|
}
|
|
|
|
@ -243,8 +244,11 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) {
|
|
|
|
|
return "", fmt.Errorf("could not get Name: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if no errors, cache ppid
|
|
|
|
|
// if no errors and not cached already, cache ppid
|
|
|
|
|
p.parent = ppid
|
|
|
|
|
if 0 == p.getPpid() {
|
|
|
|
|
p.setPpid(ppid)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return name, nil
|
|
|
|
|
}
|
|
|
|
@ -441,8 +445,11 @@ func (p *Process) NumThreadsWithContext(ctx context.Context) (int32, error) {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if no errors, cache ppid
|
|
|
|
|
// if no errors and not cached already, cache ppid
|
|
|
|
|
p.parent = ppid
|
|
|
|
|
if 0 == p.getPpid() {
|
|
|
|
|
p.setPpid(ppid)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
|
}
|
|
|
|
@ -594,6 +601,21 @@ func (p *Process) KillWithContext(ctx context.Context) error {
|
|
|
|
|
return process.Kill()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// retrieve Ppid in a thread-safe manner
|
|
|
|
|
func (p *Process) getPpid() int32 {
|
|
|
|
|
p.parentMutex.RLock()
|
|
|
|
|
defer p.parentMutex.RUnlock()
|
|
|
|
|
return p.parent
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// cache Ppid in a thread-safe manner (WINDOWS ONLY)
|
|
|
|
|
// see https://psutil.readthedocs.io/en/latest/#psutil.Process.ppid
|
|
|
|
|
func (p *Process) setPpid(ppid int32) {
|
|
|
|
|
p.parentMutex.Lock()
|
|
|
|
|
defer p.parentMutex.Unlock()
|
|
|
|
|
p.parent = ppid
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getFromSnapProcess(pid int32) (int32, int32, string, error) {
|
|
|
|
|
snap, err := windows.CreateToolhelp32Snapshot(windows.TH32CS_SNAPPROCESS, uint32(pid))
|
|
|
|
|
if err != nil {
|
|
|
|
|