-
Notifications
You must be signed in to change notification settings - Fork 43
/
find_process.js
240 lines (216 loc) · 6.47 KB
/
find_process.js
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
234
235
236
237
238
239
240
/*
* @Author: zoujie.wzj
* @Date: 2016-01-23 18:25:37
* @Last Modified by: Sahel LUCAS--SAOUDI
* @Last Modified on: 2021-11-12
*/
'use strict'
const path = require('path')
const utils = require('./utils')
function matchName (text, name) {
if (!name) {
return true
}
// make sure text.match is valid, fix #30
if (text && text.match) {
return text.match(name)
}
return false
}
function fetchBin (cmd) {
const pieces = cmd.split(path.sep)
const last = pieces[pieces.length - 1]
if (last) {
pieces[pieces.length - 1] = last.split(' ')[0]
}
const fixed = []
for (const part of pieces) {
const optIdx = part.indexOf(' -')
if (optIdx >= 0) {
// case: /aaa/bbb/ccc -c
fixed.push(part.substring(0, optIdx).trim())
break
} else if (part.endsWith(' ')) {
// case: node /aaa/bbb/ccc.js
fixed.push(part.trim())
break
}
fixed.push(part)
}
return fixed.join(path.sep)
}
function fetchName (fullpath) {
if (process.platform === 'darwin') {
const idx = fullpath.indexOf('.app/')
if (idx >= 0) {
return path.basename(fullpath.substring(0, idx))
}
}
return path.basename(fullpath)
}
const finders = {
darwin (cond) {
return new Promise((resolve, reject) => {
let cmd
if ('pid' in cond) {
cmd = `ps -p ${cond.pid} -ww -o pid,ppid,uid,gid,args`
} else {
cmd = 'ps ax -ww -o pid,ppid,uid,gid,args'
}
utils.exec(cmd, function (err, stdout, stderr) {
if (err) {
if ('pid' in cond) {
// when pid not exists, call `ps -p ...` will cause error, we have to
// ignore the error and resolve with empty array
resolve([])
} else {
reject(err)
}
} else {
err = stderr.toString().trim()
if (err) {
reject(err)
return
}
const data = utils.stripLine(stdout.toString(), 1)
const columns = utils.extractColumns(data, [0, 1, 2, 3, 4], 5).filter(column => {
if (column[0] && cond.pid) {
return column[0] === String(cond.pid)
} else if (column[4] && cond.name) {
return matchName(column[4], cond.name)
} else {
return !!column[0]
}
})
let list = columns.map(column => {
const cmd = String(column[4])
const bin = fetchBin(cmd)
return {
pid: parseInt(column[0], 10),
ppid: parseInt(column[1], 10),
uid: parseInt(column[2], 10),
gid: parseInt(column[3], 10),
name: fetchName(bin),
bin: bin,
cmd: column[4]
}
})
if (cond.config.strict && cond.name) {
list = list.filter(item => item.name === cond.name)
}
resolve(list)
}
})
})
},
linux: 'darwin',
sunos: 'darwin',
freebsd: 'darwin',
win32 (cond) {
return new Promise((resolve, reject) => {
const cmd = 'Get-CimInstance -className win32_process | select Name,ProcessId,ParentProcessId,CommandLine,ExecutablePath'
const lines = []
const proc = utils.spawn('powershell.exe', ['/c', cmd], { detached: false, windowsHide: true })
proc.stdout.on('data', data => {
lines.push(data.toString())
})
proc.on('error', err => {
reject(new Error('Command \'' + cmd + '\' failed with reason: ' + err.toString()))
})
proc.on('close', code => {
if (code !== 0) {
return reject(new Error('Command \'' + cmd + '\' terminated with code: ' + code))
}
const list = utils.parseTable(lines.join(''))
.filter(row => {
if ('pid' in cond) {
return row.ProcessId === String(cond.pid)
} else if (cond.name) {
const rowName = row.Name || '' // fix #40
if (cond.config.strict) {
return rowName === cond.name || (rowName.endsWith('.exe') && rowName.slice(0, -4) === cond.name)
} else {
// fix #9
return matchName(row.CommandLine || rowName, cond.name)
}
} else {
return true
}
})
.map(row => ({
pid: parseInt(row.ProcessId, 10),
ppid: parseInt(row.ParentProcessId, 10),
// uid: void 0,
// gid: void 0,
bin: row.ExecutablePath,
name: row.Name || '',
cmd: row.CommandLine
}))
resolve(list)
})
})
},
android (cond) {
return new Promise((resolve, reject) => {
const cmd = 'ps'
utils.exec(cmd, function (err, stdout, stderr) {
if (err) {
if ('pid' in cond) {
// when pid not exists, call `ps -p ...` will cause error, we have to
// ignore the error and resolve with empty array
resolve([])
} else {
reject(err)
}
} else {
err = stderr.toString().trim()
if (err) {
reject(err)
return
}
const data = utils.stripLine(stdout.toString(), 1)
const columns = utils.extractColumns(data, [0, 3], 4).filter(column => {
if (column[0] && cond.pid) {
return column[0] === String(cond.pid)
} else if (column[1] && cond.name) {
return matchName(column[1], cond.name)
} else {
return !!column[0]
}
})
let list = columns.map(column => {
const cmd = String(column[1])
const bin = fetchBin(cmd)
return {
pid: parseInt(column[0], 10),
// ppid: void 0,
// uid: void 0,
// gid: void 0,
name: fetchName(bin),
bin,
cmd
}
})
if (cond.config.strict && cond.name) {
list = list.filter(item => item.name === cond.name)
}
resolve(list)
}
})
})
}
}
function findProcess (cond) {
const platform = process.platform
return new Promise((resolve, reject) => {
if (!(platform in finders)) {
return reject(new Error(`platform ${platform} is unsupported`))
}
let find = finders[platform]
if (typeof find === 'string') {
find = finders[find]
}
find(cond).then(resolve, reject)
})
}
module.exports = findProcess