-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_linux.go
More file actions
85 lines (71 loc) · 1.32 KB
/
process_linux.go
File metadata and controls
85 lines (71 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//go:build linux
/*
Original Repository
https://github.com/mitchellh/go-ps
This version is just for personal usages.. "wink wink"
So, No support for this
*/
package process
import (
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
"strings"
)
type UnixProcess struct {
pid int
binary string
}
func (p *UnixProcess) Pid() int {
return p.pid
}
func (p *UnixProcess) Executable() string {
return p.binary
}
func processes() ([]Process, error) {
d, err := os.Open("/proc")
if err != nil {
return nil, err
}
defer d.Close()
results := make([]Process, 0, 50)
for {
names, err := d.Readdirnames(10)
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
for _, name := range names {
if name[0] < '0' || name[0] > '9' {
continue
}
pid, err := strconv.ParseInt(name, 10, 0)
if err != nil {
continue
}
p, err := newUnixProcess(int(pid))
if err != nil {
continue
}
results = append(results, p)
}
}
return results, nil
}
func newUnixProcess(pid int) (*UnixProcess, error) {
p := &UnixProcess{pid: pid}
return p, p.Refresh()
}
func (p *UnixProcess) Refresh() error {
statPath := fmt.Sprintf("/proc/%d/cmdline", p.pid)
dataBytes, err := ioutil.ReadFile(statPath)
if err != nil {
return err
}
p.binary = strings.Replace(string(dataBytes), "\x00", " ", -1)
return nil
}