Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/mitchellh/go-ps
module github.com/jgbooks/go-ps

go 1.13
9 changes: 8 additions & 1 deletion process.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ type Process interface {
// PPid is the parent process ID for this process.
PPid() int

// Pgrp is the process group ID of the process
Pgrp() int

// 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
Expand All @@ -33,7 +40,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)
Expand Down
20 changes: 20 additions & 0 deletions process_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
type DarwinProcess struct {
pid int
ppid int
pgrp int
sid int
ttyNr uint64
binary string
}

Expand All @@ -23,10 +26,23 @@ 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
}


func (p *DarwinProcess) TtyNr() uint64 {
return p.ttyNr
}

func findProcess(pid int) (Process, error) {
ps, err := processes()
if err != nil {
Expand Down Expand Up @@ -63,9 +79,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),
}
}
Expand Down
11 changes: 11 additions & 0 deletions process_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,21 @@ 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
}

func (p *UnixProcess) TtyNr() uint64 {
return p.ttyNr
}
// Refresh reloads all the data associated with this process.
func (p *UnixProcess) Refresh() error {

Expand Down
5 changes: 3 additions & 2 deletions process_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion process_solaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions process_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package ps

import (
"log"
"os"
"testing"
"time"
)

func TestFindProcess(t *testing.T) {
Expand All @@ -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) {
Expand Down
21 changes: 16 additions & 5 deletions process_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type UnixProcess struct {
state rune
pgrp int
sid int

ttyNr uint64
binary string
}

Expand All @@ -29,6 +29,18 @@ 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) TtyNr() uint64 {
return p.ttyNr
}

func (p *UnixProcess) Executable() string {
return p.binary
}
Expand All @@ -37,10 +49,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
}

Expand Down
13 changes: 13 additions & 0 deletions process_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type PROCESSENTRY32 struct {
type WindowsProcess struct {
pid int
ppid int
pgrp int
sid int
exe string
}

Expand All @@ -53,10 +55,21 @@ 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
}

func (p *WindowsProcess) TtyNr() uint64 {
return p.ttyNr
}
func newWindowsProcess(e *PROCESSENTRY32) *WindowsProcess {
// Find when the string ends for decoding
end := 0
Expand Down