From b28f3aa74c3b94d0a88d396e8e469448b7f7fd5a Mon Sep 17 00:00:00 2001 From: Pierre-Alain TORET Date: Fri, 5 Jan 2018 16:05:26 +0100 Subject: [PATCH] Add DragonFlyBSD support Signed-off-by: Pierre-Alain TORET --- process_dragonfly.go | 35 +++++++++++++++++++++++++++++++++++ process_linux.go | 41 +++++++++++++++++++++++++++++++++++++++++ process_nix_test.go | 2 +- process_unix.go | 36 +----------------------------------- process_unix_test.go | 2 +- 5 files changed, 79 insertions(+), 37 deletions(-) create mode 100644 process_dragonfly.go create mode 100644 process_linux.go diff --git a/process_dragonfly.go b/process_dragonfly.go new file mode 100644 index 0000000..ad481ea --- /dev/null +++ b/process_dragonfly.go @@ -0,0 +1,35 @@ +// +build dragonfly + +package ps + +import ( + "fmt" + "io/ioutil" + "path/filepath" +) + +// Path returns path to process executable +func (p *UnixProcess) Path() (string, error) { + return filepath.EvalSymlinks(fmt.Sprintf("/proc/%d/file", p.pid)) +} + +// Refresh reloads all the data associated with this process. +func (p *UnixProcess) Refresh() error { + statPath := fmt.Sprintf("/proc/%d/status", p.pid) + dataBytes, err := ioutil.ReadFile(statPath) + if err != nil { + return err + } + + data := string(dataBytes) + + _, err = fmt.Sscanf(data, + "%s %d %d %d %d", + &p.binary, + &p.pid, + &p.ppid, + &p.pgrp, + &p.sid) + + return err +} diff --git a/process_linux.go b/process_linux.go new file mode 100644 index 0000000..0a1768e --- /dev/null +++ b/process_linux.go @@ -0,0 +1,41 @@ +// +build linux + +package ps + +import ( + "fmt" + "io/ioutil" + "strings" + "path/filepath" +) + +// Path returns path to process executable +func (p *UnixProcess) Path() (string, error) { + return filepath.EvalSymlinks(fmt.Sprintf("/proc/%d/exe", p.pid)) +} + +// Refresh reloads all the data associated with this process. +func (p *UnixProcess) Refresh() error { + statPath := fmt.Sprintf("/proc/%d/stat", p.pid) + dataBytes, err := ioutil.ReadFile(statPath) + if err != nil { + return err + } + + // First, parse out the image name + data := string(dataBytes) + binStart := strings.IndexRune(data, '(') + 1 + binEnd := strings.IndexRune(data[binStart:], ')') + p.binary = data[binStart : binStart+binEnd] + + // Move past the image name and start parsing the rest + data = data[binStart+binEnd+2:] + _, err = fmt.Sscanf(data, + "%c %d %d %d", + &p.state, + &p.ppid, + &p.pgrp, + &p.sid) + + return err +} diff --git a/process_nix_test.go b/process_nix_test.go index ed2aec2..a5d1620 100644 --- a/process_nix_test.go +++ b/process_nix_test.go @@ -1,4 +1,4 @@ -// +build darwin linux +// +build darwin linux dragonfly package ps diff --git a/process_unix.go b/process_unix.go index f4e8da2..abcbbea 100644 --- a/process_unix.go +++ b/process_unix.go @@ -1,15 +1,13 @@ -// +build linux +// +build linux dragonfly package ps import ( "fmt" "io" - "io/ioutil" "os" "path/filepath" "strconv" - "strings" ) // UnixProcess is an implementation of Process that contains Unix-specific @@ -44,38 +42,6 @@ func (p *UnixProcess) Executable() string { return filepath.Base(path) } -// Path returns path to process executable -func (p *UnixProcess) Path() (string, error) { - return filepath.EvalSymlinks(fmt.Sprintf("/proc/%d/exe", p.pid)) -} - -// Refresh reloads all the data associated with this process. -func (p *UnixProcess) Refresh() error { - statPath := fmt.Sprintf("/proc/%d/stat", p.pid) - dataBytes, err := ioutil.ReadFile(statPath) - if err != nil { - return err - } - - // First, parse out the image name - data := string(dataBytes) - binStart := strings.IndexRune(data, '(') + 1 - binEnd := strings.IndexRune(data[binStart:], ')') - p.binary = data[binStart : binStart+binEnd] - - // Move past the image name and start parsing the rest - // The name here might not be the full name - data = data[binStart+binEnd+2:] - _, err = fmt.Sscanf(data, - "%c %d %d %d", - &p.state, - &p.ppid, - &p.pgrp, - &p.sid) - - return err -} - func findProcess(pid int) (Process, error) { dir := fmt.Sprintf("/proc/%d", pid) _, err := os.Stat(dir) diff --git a/process_unix_test.go b/process_unix_test.go index 162366a..b3be48d 100644 --- a/process_unix_test.go +++ b/process_unix_test.go @@ -1,4 +1,4 @@ -// +build linux +// +build linux dragonfly package ps