-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathptab.go
233 lines (195 loc) · 4.59 KB
/
ptab.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//go:build !windows
package main
import (
"fmt"
"io/ioutil"
"os/exec"
"regexp"
"strconv"
"strings"
"time"
)
// process table generation
var validProcessNumber = regexp.MustCompile(`^[0-9/]+$`)
func isNumberString(s string) bool {
match := validProcessNumber.FindStringSubmatch(s)
if match == nil {
return false
}
return true
}
func init() {
detectSystem()
}
var Sysname string
func detectSystem() {
c := exec.Command("uname", "-s")
o, err := c.Output()
if err == nil {
Sysname = strings.Trim(string(o), " \n\t")
}
}
func ProcessTable() *map[int]bool {
switch Sysname {
case "Linux":
return LinuxPs()
case "Darwin":
return DarwinPs()
}
m := make(map[int]bool)
return &m
}
var regexDarwinPs = regexp.MustCompile(`^[^0-9]*[0-9]+[^0-9]+([0-9]+)`)
func DarwinPs() *map[int]bool {
res := make(map[int]bool)
o, err := exec.Command("/bin/ps", "-ef").Output()
if err != nil {
panic(err)
}
s := string(o)
lines := strings.Split(s, "\n")
for i := range lines {
match := regexDarwinPs.FindStringSubmatch(lines[i])
if match != nil && len(match) == 2 {
//fmt.Printf("match = %#v\n", match[1])
num, err := strconv.Atoi(match[1])
if err == nil {
res[num] = true
}
}
}
return &res
}
func LinuxPs() *map[int]bool {
res := make(map[int]bool)
fileInfoSlice, err := ioutil.ReadDir("/proc")
if err != nil {
panic(err)
}
for i := range fileInfoSlice {
if fileInfoSlice[i].IsDir() {
if isNumberString(fileInfoSlice[i].Name()) {
num, err := strconv.Atoi(fileInfoSlice[i].Name())
if err != nil {
panic("isNumberString regex failed to filter down to only numbers")
}
res[num] = true
}
}
}
return &res
}
func OpenFiles(pid int) []string {
switch Sysname {
case "Linux":
return LinuxOpenFiles(pid)
case "Darwin":
return DarwinOpenFiles(pid)
}
return []string{}
}
func LinuxOpenFiles(pid int) []string {
// wait until process shows up (in /proc or ps)
waited := 0
for {
pt := *ProcessTable()
_, jsAlive := pt[pid]
if jsAlive {
break
}
time.Sleep(50 * time.Millisecond)
waited++
if waited > 10 {
panic(fmt.Sprintf("jobserv with expected pid %d did not show up in /proc after 10 waits", pid))
}
}
fmt.Printf("\njobserv with expected pid %d was *found* in /proc after %d waits of 50msec\n", pid, waited)
/*
// read all the open files
fileInfoSlice, err := ioutil.ReadDir(fmt.Sprintf("/proc/%d/fd", pid))
if err != nil {
panic(err)
}
res := make([]string, len(fileInfoSlice))
for i := range fileInfoSlice {
res[i] = fileInfoSlice[i].Name()
}
return res
*/
spid := fmt.Sprintf("%d", pid)
//fmt.Printf("about to do: lsof -p %s -Fnt\n", spid)
o, err := exec.Command("/usr/bin/lsof", "-p", spid, "-Fnt").Output()
if err != nil {
panic(err)
}
s := strings.Split(string(o), "\n")
// join the name and type together on one line
// skip the header line
s = s[1:]
r := make([]string, 0)
n := len(s) - 1 // empty string at end
//fmt.Printf("len(s) = %d; s='%#v'\n", len(s), s)
for i := 0; i < n; i += 2 {
//fmt.Printf("on i = %d, s[i]='%s', s[i+i]='%s'\n", i, s[i], s[i+1])
r = append(r, s[i]+":"+s[i+1])
}
//fmt.Printf("LinuxOpenFiles got back from lsof:\n")
//ShowStrings(r)
return r
}
func DarwinOpenFiles(pid int) []string {
// wait until process shows up (in /proc or ps)
waited := 0
for {
pt := *ProcessTable()
_, jsAlive := pt[pid]
if jsAlive {
break
}
time.Sleep(50 * time.Millisecond)
waited++
if waited > 10 {
panic(fmt.Sprintf("jobserv with expected pid %d did not show up in /proc after 10 waits", pid))
}
}
fmt.Printf("\njobserv with expected pid %d was *found* in /proc after %d waits of 50msec\n", pid, waited)
spid := fmt.Sprintf("%d", pid)
//fmt.Printf("about to do: lsof -p %s -Fnt\n", spid)
o, err := exec.Command("/usr/sbin/lsof", "-p", spid, "-Fnt").Output()
if err != nil {
panic(err)
}
s := strings.Split(string(o), "\n")
// join the name and type together on one line
// skip the header line
s = s[1:]
r := make([]string, 0)
n := len(s) - 1 // empty string at end
//fmt.Printf("len(s) = %d; s='%#v'\n", len(s), s)
for i := 0; i < n; i += 2 {
//fmt.Printf("on i = %d, s[i]='%s', s[i+i]='%s'\n", i, s[i], s[i+1])
r = append(r, s[i]+":"+s[i+1])
}
//fmt.Printf("DarwinOpenFiles got back from lsof:\n")
//ShowStrings(r)
return r
}
func setDiff(a, b []string) []string {
res := make([]string, 0)
m := make(map[string]bool)
for i := range a {
m[a[i]] = true
}
for i := range b {
delete(m, b[i])
}
for s := range m {
res = append(res, s)
}
return res
}
func showStrings(s []string) {
for _, line := range s {
fmt.Printf("%s\n", line)
}
}