From 8da2ad2b88b326e6edc1a9ef7495a5c05a66ec11 Mon Sep 17 00:00:00 2001 From: JGbooks <501466133@qq.com> Date: Fri, 17 Jul 2020 18:03:37 +0800 Subject: [PATCH 1/6] fixed If the process cannot be found, it is better to return an error #41 --- process.go | 2 +- process_unix.go | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/process.go b/process.go index 2b5e8ed..ccb4e69 100644 --- a/process.go +++ b/process.go @@ -33,7 +33,7 @@ func Processes() ([]Process, error) { // FindProcess looks up a single process by pid. // -// Process will be nil and error will be nil if a matching process is +// Process will be nil and error will be os.ErrNotExist if a matching process is // not found. func FindProcess(pid int) (Process, error) { return findProcess(pid) diff --git a/process_unix.go b/process_unix.go index cd217a8..6f18bb8 100644 --- a/process_unix.go +++ b/process_unix.go @@ -37,10 +37,9 @@ func findProcess(pid int) (Process, error) { dir := fmt.Sprintf("/proc/%d", pid) _, err := os.Stat(dir) if err != nil { - if os.IsNotExist(err) { - return nil, nil - } - + // if os.IsNotExist(err) { + // return nil, nil + // } return nil, err } From 22c1d37b7a541c9ae5e4e9b841dacceccbb20785 Mon Sep 17 00:00:00 2001 From: Sudhabindu Das Date: Sat, 1 May 2021 22:09:16 +0530 Subject: [PATCH 2/6] (WIP )adding pgrp and sid --- process.go | 6 ++++++ process_darwin.go | 10 ++++++++++ process_freebsd.go | 8 ++++++++ process_unix.go | 4 ++++ process_windows.go | 10 ++++++++++ 5 files changed, 38 insertions(+) diff --git a/process.go b/process.go index 2b5e8ed..987134b 100644 --- a/process.go +++ b/process.go @@ -16,6 +16,12 @@ type Process interface { // PPid is the parent process ID for this process. PPid() int + // Pgid is the process group ID of the process + Pgrp() int + + // Sid is the session ID of the process + Sid() int + // Executable name running this process. This is not a path to the // executable. Executable() string diff --git a/process_darwin.go b/process_darwin.go index 5ee87fb..d7192f1 100644 --- a/process_darwin.go +++ b/process_darwin.go @@ -12,6 +12,8 @@ import ( type DarwinProcess struct { pid int ppid int + pgrp int + sid int binary string } @@ -23,6 +25,14 @@ func (p *DarwinProcess) PPid() int { return p.ppid } +func (p *DarwinProcess) Pgrp() int { + return p.pgrp +} + +func (p *DarwinProcess) Sid() int { + return p.sid +} + func (p *DarwinProcess) Executable() string { return p.binary } diff --git a/process_freebsd.go b/process_freebsd.go index 130acbe..3fd712c 100644 --- a/process_freebsd.go +++ b/process_freebsd.go @@ -121,6 +121,14 @@ func (p *UnixProcess) PPid() int { return p.ppid } +func (p *UnixProcess) Pgrp() int { + return p.pgrp +} + +func (p *UnixProcess) Sid() int { + return p.sid +} + func (p *UnixProcess) Executable() string { return p.binary } diff --git a/process_unix.go b/process_unix.go index cd217a8..7f62ecd 100644 --- a/process_unix.go +++ b/process_unix.go @@ -29,6 +29,10 @@ func (p *UnixProcess) PPid() int { return p.ppid } +func (p *UnixProcess) Pgrp() int { + return p.pgrp +} + func (p *UnixProcess) Executable() string { return p.binary } diff --git a/process_windows.go b/process_windows.go index f151974..99c58ab 100644 --- a/process_windows.go +++ b/process_windows.go @@ -42,6 +42,8 @@ type PROCESSENTRY32 struct { type WindowsProcess struct { pid int ppid int + pgrp int + sid int exe string } @@ -53,6 +55,14 @@ func (p *WindowsProcess) PPid() int { return p.ppid } +func (p *WindowsProcess) Pgrp() int { + return p.pgrp +} + +func (p *WindowsProcess) Sid() { + return p.sid +} + func (p *WindowsProcess) Executable() string { return p.exe } From 4b0890f769e5238a4fe83ad5ee64a839f6b5f026 Mon Sep 17 00:00:00 2001 From: Sudhabindu Das Date: Sat, 1 May 2021 22:13:02 +0530 Subject: [PATCH 3/6] added Sid to unix_process --- process_unix.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/process_unix.go b/process_unix.go index 7f62ecd..7e16c21 100644 --- a/process_unix.go +++ b/process_unix.go @@ -33,6 +33,10 @@ func (p *UnixProcess) Pgrp() int { return p.pgrp } +func (p *UnixProcess) Sid() int { + return p.sid +} + func (p *UnixProcess) Executable() string { return p.binary } From 81116e4c8a66d617f21d3b1536dd35f0c8b48727 Mon Sep 17 00:00:00 2001 From: Sudhabindu Das Date: Sat, 1 May 2021 22:43:57 +0530 Subject: [PATCH 4/6] (temp) using syscall to get pgid and sid --- process.go | 2 +- process_darwin.go | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/process.go b/process.go index 987134b..57e1b8b 100644 --- a/process.go +++ b/process.go @@ -16,7 +16,7 @@ type Process interface { // PPid is the parent process ID for this process. PPid() int - // Pgid is the process group ID of the process + // Pgrp is the process group ID of the process Pgrp() int // Sid is the session ID of the process diff --git a/process_darwin.go b/process_darwin.go index d7192f1..8ce55ea 100644 --- a/process_darwin.go +++ b/process_darwin.go @@ -73,9 +73,13 @@ func processes() ([]Process, error) { darwinProcs := make([]Process, len(procs)) for i, p := range procs { + pgid, _ := syscall.Getpgid(int(p.Pid)) + sid, _ := syscall.Getsid(int(p.Pid)) darwinProcs[i] = &DarwinProcess{ pid: int(p.Pid), ppid: int(p.PPid), + pgrp: pgid, + sid: sid, binary: darwinCstring(p.Comm), } } From 9198b1ea8f05c765d7ec29fda149aceff35d4ecb Mon Sep 17 00:00:00 2001 From: jgbooks <31645894+jgbooks@users.noreply.github.com> Date: Wed, 13 Oct 2021 16:13:36 +0800 Subject: [PATCH 5/6] use github.com/jgbooks/go-ps --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 0343279..08c3099 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/mitchellh/go-ps +module github.com/jgbooks/go-ps go 1.13 From b667bb03029145c2bdcca151e2d5a27351750110 Mon Sep 17 00:00:00 2001 From: ma_jiangang Date: Sun, 13 Mar 2022 17:18:54 +0800 Subject: [PATCH 6/6] =?UTF-8?q?feat=EF=BC=9A=E6=96=B0=E5=A2=9E=E8=BF=9B?= =?UTF-8?q?=E7=A8=8Btty=20=E8=AE=BE=E5=A4=87id=20=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- process.go | 3 ++- process_darwin.go | 6 ++++++ process_freebsd.go | 3 +++ process_linux.go | 5 +++-- process_solaris.go | 2 +- process_test.go | 5 +++++ process_unix.go | 6 +++++- process_windows.go | 3 +++ 8 files changed, 28 insertions(+), 5 deletions(-) diff --git a/process.go b/process.go index ce169a1..d6aed72 100644 --- a/process.go +++ b/process.go @@ -21,7 +21,8 @@ type Process interface { // Sid is the session ID of the process Sid() int - + // TtyNr is the TTYID of the process + TtyNr() uint64 // Executable name running this process. This is not a path to the // executable. Executable() string diff --git a/process_darwin.go b/process_darwin.go index 8ce55ea..a10f874 100644 --- a/process_darwin.go +++ b/process_darwin.go @@ -14,6 +14,7 @@ type DarwinProcess struct { ppid int pgrp int sid int + ttyNr uint64 binary string } @@ -37,6 +38,11 @@ func (p *DarwinProcess) Executable() string { return p.binary } + +func (p *DarwinProcess) TtyNr() uint64 { + return p.ttyNr +} + func findProcess(pid int) (Process, error) { ps, err := processes() if err != nil { diff --git a/process_freebsd.go b/process_freebsd.go index 3fd712c..56bc5a0 100644 --- a/process_freebsd.go +++ b/process_freebsd.go @@ -133,6 +133,9 @@ func (p *UnixProcess) Executable() string { return p.binary } +func (p *UnixProcess) TtyNr() uint64 { + return p.ttyNr +} // Refresh reloads all the data associated with this process. func (p *UnixProcess) Refresh() error { diff --git a/process_linux.go b/process_linux.go index c1558f7..9f16c2a 100644 --- a/process_linux.go +++ b/process_linux.go @@ -25,11 +25,12 @@ func (p *UnixProcess) Refresh() error { // Move past the image name and start parsing the rest data = data[binStart+binEnd+2:] _, err = fmt.Sscanf(data, - "%c %d %d %d", + "%c %d %d %d %d", &p.state, &p.ppid, &p.pgrp, - &p.sid) + &p.sid, + &p.ttyNr) return err } diff --git a/process_solaris.go b/process_solaris.go index 014c416..425f70c 100644 --- a/process_solaris.go +++ b/process_solaris.go @@ -80,7 +80,7 @@ func (p *UnixProcess) Refresh() error { if err != nil { return err } - + p.ttyNr=uint64(psinfo.Pr_ttydev) p.ppid = int(psinfo.Pr_ppid) p.binary = toString(psinfo.Pr_fname[:], 16) return nil diff --git a/process_test.go b/process_test.go index 1bcbc32..92d3ef7 100644 --- a/process_test.go +++ b/process_test.go @@ -1,8 +1,10 @@ package ps import ( + "log" "os" "testing" + "time" ) func TestFindProcess(t *testing.T) { @@ -17,6 +19,9 @@ func TestFindProcess(t *testing.T) { if p.Pid() != os.Getpid() { t.Fatalf("bad: %#v", p.Pid()) } + time.Sleep(20*time.Second) + log.Println(p.Sid()) + log.Println(p.TtyNr()) } func TestProcesses(t *testing.T) { diff --git a/process_unix.go b/process_unix.go index 5039bc4..f801bf1 100644 --- a/process_unix.go +++ b/process_unix.go @@ -17,7 +17,7 @@ type UnixProcess struct { state rune pgrp int sid int - + ttyNr uint64 binary string } @@ -37,6 +37,10 @@ func (p *UnixProcess) Sid() int { return p.sid } +func (p *UnixProcess) TtyNr() uint64 { + return p.ttyNr +} + func (p *UnixProcess) Executable() string { return p.binary } diff --git a/process_windows.go b/process_windows.go index 99c58ab..75bfa65 100644 --- a/process_windows.go +++ b/process_windows.go @@ -67,6 +67,9 @@ func (p *WindowsProcess) Executable() string { return p.exe } +func (p *WindowsProcess) TtyNr() uint64 { + return p.ttyNr +} func newWindowsProcess(e *PROCESSENTRY32) *WindowsProcess { // Find when the string ends for decoding end := 0