Skip to content
This repository was archived by the owner on Jan 22, 2025. 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
35 changes: 35 additions & 0 deletions process_dragonfly.go
Original file line number Diff line number Diff line change
@@ -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
}
41 changes: 41 additions & 0 deletions process_linux.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion process_nix_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build darwin linux
// +build darwin linux dragonfly

package ps

Expand Down
36 changes: 1 addition & 35 deletions process_unix.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion process_unix_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build linux
// +build linux dragonfly

package ps

Expand Down