forked from mitchellh/go-ps
/
process_unix.go
148 lines (122 loc) · 2.52 KB
/
process_unix.go
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// +build linux
package ps
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
)
// UnixProcess is an implementation of Process that contains Unix-specific
// fields and information.
type UnixProcess struct {
pid int
ppid int
//state rune
//pgrp int
//sid int
binary string
}
func (p *UnixProcess) Pid() int {
return p.pid
}
func (p *UnixProcess) PPid() int {
return p.ppid
}
func (p *UnixProcess) Executable() string {
return p.binary
}
// Refresh reloads all the data associated with this process.
func (p *UnixProcess) Refresh() error {
var (
procPath = fmt.Sprintf("/proc/%d/", p.pid)
data []byte
err error
)
data, err = ioutil.ReadFile(procPath + "stat")
if err != nil {
return err
}
data = data[bytes.IndexRune(data, ')')+2:]
_, err = fmt.Fscanf(bytes.NewReader(data), "%c %d", new(rune), &p.ppid)
if err != nil {
return err
}
data, err = ioutil.ReadFile(procPath + "cmdline")
if err != nil {
return err
}
if len(data) == 0 {
return errors.New("empty name")
}
// Remove arguments
if ind := bytes.IndexByte(data, 0); ind >= 0 {
data = data[:ind]
}
if ind := bytes.IndexByte(data, ' '); ind >= 0 {
data = data[:ind]
}
// Remove path to the executable
if ind := bytes.LastIndexByte(data, '/'); ind >= 0 {
data = data[ind+1:]
}
p.binary = string(data)
return nil
}
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
}
return nil, err
}
return newUnixProcess(pid)
}
func processes() ([]Process, error) {
d, err := os.Open("/proc")
if err != nil {
return nil, err
}
defer d.Close()
results := make([]Process, 0, 50)
for {
fis, err := d.Readdir(10)
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
for _, fi := range fis {
// We only care about directories, since all pids are dirs
if !fi.IsDir() {
continue
}
// We only care if the name starts with a numeric
name := fi.Name()
if name[0] < '0' || name[0] > '9' {
continue
}
// From this point forward, any errors we just ignore, because
// it might simply be that the process doesn't exist anymore.
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()
}