Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

support non-default mount point for /proc filesystem #38

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.vagrant/
.idea/
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
language: go

go:
- 1.2.1
- 1.11.x
- 1.12.x
- 1.13.x
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ DLL methods for Windows, cgo for Darwin, etc.
How it works:

* **Darwin** uses the `sysctl` syscall to retrieve the process table.
* **Unix** uses the procfs at `/proc` to inspect the process tree.
* **Unix** uses the procfs at `/proc` (can be redefined by `HOST_PROC` env. var) to inspect the process
* **Windows** uses the Windows API, and methods such as
`CreateToolhelp32Snapshot` to get a point-in-time snapshot of
the process table.
Expand Down
3 changes: 2 additions & 1 deletion process_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import (
"fmt"
"io/ioutil"
"strings"

)

// Refresh reloads all the data associated with this process.
func (p *UnixProcess) Refresh() error {
statPath := fmt.Sprintf("/proc/%d/stat", p.pid)
statPath := fmt.Sprintf("%s/%d/stat",GetEnv("HOST_PROC","/proc"), p.pid)
dataBytes, err := ioutil.ReadFile(statPath)
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion process_solaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ type psinfo_t struct {
func (p *UnixProcess) Refresh() error {
var psinfo psinfo_t

path := fmt.Sprintf("/proc/%d/psinfo", p.pid)
path := fmt.Sprintf("%s/%d/psinfo",GetEnv("HOST_PROC","/proc"), p.pid)

fh, err := os.Open(path)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions process_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (p *UnixProcess) Executable() string {
}

func findProcess(pid int) (Process, error) {
dir := fmt.Sprintf("/proc/%d", pid)
dir := fmt.Sprintf("%s/%d",GetEnv("HOST_PROC","/proc"), pid)
_, err := os.Stat(dir)
if err != nil {
if os.IsNotExist(err) {
Expand All @@ -48,7 +48,7 @@ func findProcess(pid int) (Process, error) {
}

func processes() ([]Process, error) {
d, err := os.Open("/proc")
d, err := os.Open(GetEnv("HOST_PROC","/proc"))
if err != nil {
return nil, err
}
Expand Down
12 changes: 12 additions & 0 deletions uxutils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ps

import "os"

func GetEnv(key, defaultValue string) string {

value := os.Getenv(key)
if len(value) == 0 {
return defaultValue
}
return value
}