From 482ca3af6d842055fe735f0db994896fca1e7505 Mon Sep 17 00:00:00 2001 From: Nick Kirsch Date: Thu, 4 Jan 2018 11:30:39 -0800 Subject: [PATCH 1/3] Parses the tgid field, which is the thread group id (aka user-space process id) on Linux. Returns error on other platforms. --- process/process.go | 2 ++ process/process_fallback.go | 3 +++ process/process_linux.go | 16 ++++++++++++++++ 3 files changed, 21 insertions(+) diff --git a/process/process.go b/process/process.go index df5da2e..0e089bb 100644 --- a/process/process.go +++ b/process/process.go @@ -30,6 +30,8 @@ type Process struct { lastCPUTimes *cpu.TimesStat lastCPUTime time.Time + + tgid int32 } type OpenFilesStat struct { diff --git a/process/process_fallback.go b/process/process_fallback.go index 12bd3a6..e8f545b 100644 --- a/process/process_fallback.go +++ b/process/process_fallback.go @@ -41,6 +41,9 @@ func (p *Process) Ppid() (int32, error) { func (p *Process) Name() (string, error) { return "", common.ErrNotImplementedError } +func (p *Process) Tgid() (int32, error) { + return 0, common.ErrNotImplementedError +} func (p *Process) Exe() (string, error) { return "", common.ErrNotImplementedError } diff --git a/process/process_linux.go b/process/process_linux.go index 9db1997..4b1ace1 100644 --- a/process/process_linux.go +++ b/process/process_linux.go @@ -100,6 +100,16 @@ func (p *Process) Name() (string, error) { return p.name, nil } +// Tgid returns tgid, a Linux-synonym for user-space Pid +func (p *Process) Tgid() (int32, error) { + if p.tgid == 0 { + if err := p.fillFromStatus(); err != nil { + return 0, err + } + } + return p.tgid, nil +} + // Exe returns executable path of the process. func (p *Process) Exe() (string, error) { return p.fillFromExe() @@ -820,6 +830,12 @@ func (p *Process) fillFromStatus() error { return err } p.parent = int32(pval) + case "Tgid": + pval, err := strconv.ParseInt(value, 10, 32) + if err != nil { + return err + } + p.tgid = int32(pval) case "Uid": p.uids = make([]int32, 0, 4) for _, i := range strings.Split(value, "\t") { From 6c35887d024f4257ed09809d3bab3f2fc50fe14f Mon Sep 17 00:00:00 2001 From: Nick Kirsch Date: Thu, 4 Jan 2018 11:51:49 -0800 Subject: [PATCH 2/3] Add ErrNotImplementedError to Darwin, FreeBSD, and OpenBSD. --- process/process_darwin.go | 3 +++ process/process_freebsd.go | 3 +++ process/process_openbsd.go | 3 +++ 3 files changed, 9 insertions(+) diff --git a/process/process_darwin.go b/process/process_darwin.go index cc289d2..2ed7a0b 100644 --- a/process/process_darwin.go +++ b/process/process_darwin.go @@ -83,6 +83,9 @@ func (p *Process) Name() (string, error) { return common.IntToString(k.Proc.P_comm[:]), nil } +func (p *Process) Tgid() (int32, error) { + return 0, common.ErrNotImplementedError +} func (p *Process) Exe() (string, error) { lsof_bin, err := exec.LookPath("lsof") if err != nil { diff --git a/process/process_freebsd.go b/process/process_freebsd.go index 9d75717..4db021d 100644 --- a/process/process_freebsd.go +++ b/process/process_freebsd.go @@ -50,6 +50,9 @@ func (p *Process) Name() (string, error) { return common.IntToString(k.Comm[:]), nil } +func (p *Process) Tgid() (int32, error) { + return 0, common.ErrNotImplementedError +} func (p *Process) Exe() (string, error) { return "", common.ErrNotImplementedError } diff --git a/process/process_openbsd.go b/process/process_openbsd.go index aca4ffa..088abef 100644 --- a/process/process_openbsd.go +++ b/process/process_openbsd.go @@ -53,6 +53,9 @@ func (p *Process) Name() (string, error) { return common.IntToString(k.Comm[:]), nil } +func (p *Process) Tgid() (int32, error) { + return 0, common.ErrNotImplementedError +} func (p *Process) Exe() (string, error) { return "", common.ErrNotImplementedError } From fb24c70d366b88caa381823e8e7e387784fcda8b Mon Sep 17 00:00:00 2001 From: Nick Kirsch Date: Fri, 5 Jan 2018 11:37:36 -0800 Subject: [PATCH 3/3] Add ErrNotImplementedError for Tgid support. --- process/process_windows.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/process/process_windows.go b/process/process_windows.go index 3ce840b..14d466e 100644 --- a/process/process_windows.go +++ b/process/process_windows.go @@ -154,6 +154,10 @@ func (p *Process) Name() (string, error) { return name, nil } +func (p *Process) Tgid() (int32, error) { + return 0, common.ErrNotImplementedError +} + func (p *Process) Exe() (string, error) { dst, err := GetWin32Proc(p.Pid) if err != nil {