-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproc.go
More file actions
158 lines (136 loc) · 2.71 KB
/
proc.go
File metadata and controls
158 lines (136 loc) · 2.71 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
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
149
150
151
152
153
154
155
156
157
158
package proc
import (
"fmt"
"github.com/gogather/com"
"io/ioutil"
"os"
"strconv"
"strings"
)
type Process struct {
pid int64
status string
pp *Process
cp []*Process
info map[string]string
}
func (pro *Process) GetPid() int64 {
return pro.pid
}
func (pro *Process) GetName() string {
return pro.info["Name"]
}
func (pro *Process) GetParentProc() *Process {
return pro.pp
}
func (pro *Process) GetChildrenProc() []*Process {
return pro.cp
}
// kill process chain with reverse order
func (pro *Process) KillProcChainReverse() {
if pro.pid == 1 {
return
}
for {
pro.killNoSonTailProc()
if len(pro.cp) <= 0 {
break
}
}
pro.killNoSonTailProc()
}
// kill process which without son process in the tails
func (pro *Process) killNoSonTailProc() {
cp := pro.cp
if len(cp) > 0 {
for i := 0; i < len(cp); i++ {
cp[i].killNoSonTailProc()
}
} else {
pro.Kill()
}
}
func (pro *Process) Kill() {
proc, err := os.FindProcess(int(pro.pid))
if err != nil {
return
}
proc.Kill()
pro.pp.removeChildProcessInfoByPid(pro.pid)
}
// remove process info from parents process in cp array
func (pro *Process) removeChildProcessInfoByPid(pid int64) {
var ncps []*Process
cps := pro.cp
for i := 0; i < len(cps); i++ {
if cps[i].pid != pid {
ncps = append(ncps, cps[i])
}
}
pro.cp = ncps
}
func (pro *Process) parseProcInfo(content string) {
lines := strings.Split(content, "\n")
for i := 0; i < len(lines); i++ {
line := lines[i]
kv := strings.Split(line, ":")
if len(kv) >= 2 {
key := com.Strim(strings.TrimSpace(kv[0]))
value := com.Strim(strings.TrimSpace(kv[1]))
if key != "" {
pro.info[key] = value
}
}
}
}
var (
procMap map[int64]*Process
procSlice []Process
)
func reloadProcessTree() {
scanProc()
scanRelative()
}
func newProc(pid int64) {
content, err := com.ReadFileString(fmt.Sprintf("/proc/%d/status", pid))
if err != nil {
content = ""
}
proc := Process{pid: pid, info: map[string]string{}}
proc.parseProcInfo(content)
procSlice = append(procSlice, proc)
procMap[pid] = &procSlice[len(procSlice)-1]
}
func scanProc() {
files, _ := ioutil.ReadDir(`/proc/`)
procMap = map[int64]*Process{}
for _, file := range files {
pid, err := strconv.ParseInt(file.Name(), 10, 64)
if file.IsDir() && err == nil {
newProc(pid)
}
}
}
func scanRelative() {
for _, proc := range procMap {
s_ppid := proc.info["PPid"]
ppid, err := strconv.ParseInt(s_ppid, 10, 64)
if err == nil {
_, ok := procMap[ppid]
if ok {
proc.pp = procMap[ppid]
procMap[ppid].cp = append(procMap[ppid].cp, proc)
}
}
}
}
// get process
func GetProc(pid int64) *Process {
reloadProcessTree()
p, ok := procMap[pid]
if ok {
return p
} else {
return nil
}
}