This repository was archived by the owner on Jan 19, 2021. It is now read-only.
forked from pkrumins/node-tree-kill
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·328 lines (299 loc) · 8.4 KB
/
index.js
File metadata and controls
executable file
·328 lines (299 loc) · 8.4 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/* eslint func-style: 0, no-console: 0, max-statements: 0, no-use-before-define: 0,
space-before-function-paren: 0, no-unused-vars: 0, consistent-return: 0,
no-lonely-if: 0, max-depth: 0, no-shadow: 0, no-redeclare: 0,
callback-return: 0, no-unreachable: 0, max-params: 0 */
"use strict";
var childProcess = require("child_process");
var spawn = childProcess.spawn;
var exec = childProcess.exec;
var _process = process;
var _console = console;
var _setTimeout = setTimeout;
/* istanbul ignore next */
var _getDate = function () {
return Date.now();
};
// return child processes of pid that appear to be suspiciously hanging around
// within the scope of gracefulExitTimeout
function getZombieChildren (pid, gracefulExitTimeout, callback) {
var startTime = _getDate();
var SCAN_INTERVAL = 500;
// NOTE: on some platforms, the ps command itself appears as a child of pid.
// This is a short lived child so it doesn't count as a zombie, hence we're
// only likely to see it once. This COULD lead to false positives if we're
// on a system that is rotating through pids at an extremely high rate.
var seenPidCounts = {};
var numScans = 0;
var checkForZombies = function () {
var tree = getTree(pid, function (tree) {
numScans++;
var children = tree[pid];
if (children && children.length > 0) {
// 1. delete pids that we saw before but we DON'T see now
Object.keys(seenPidCounts).forEach(function (seenPid) {
if (children.filter(function (childPid) {
return childPid.toString() === seenPid.toString();
}).length === 0) {
// if seenPid is not in the latest tree, remove it from seenPids
delete seenPidCounts[seenPid.toString()];
}
});
// 2. add pids that we see now, increment ones we've seen before *and* see now
children.forEach(function (childPid) {
if (seenPidCounts.hasOwnProperty(childPid.toString())) {
seenPidCounts[childPid.toString()]++;
} else {
seenPidCounts[childPid.toString()] = 1;
}
});
// 3. determine if we have zombies
var zombies = Object.keys(seenPidCounts).filter(function (seenPid) {
return seenPidCounts[seenPid.toString()] > 1;
});
//
// 4:
// A) if NO children are found at all, return empty list
// B) if some children exist:
// if we have not scanned more than once, keep scanning. No conclusions yet.
// if we've scanned more than once:
// if zombies
// past the gracefulExitTimeout? return list of zombies
// else keep scanning
// no zombies?
// return empty list
//
if (children.length === 0) {
return callback([]);
} else {
if (numScans < 2) {
_setTimeout(checkForZombies, SCAN_INTERVAL);
} else {
if (zombies.length > 0) {
if (_getDate() - startTime > gracefulExitTimeout) {
return callback(zombies);
} else {
_setTimeout(checkForZombies, SCAN_INTERVAL);
}
} else {
return callback([]);
}
}
}
} else {
// we dont' see any child processes. We're good
return callback([]);
}
});
};
checkForZombies();
}
function showDebugInfo (pid, callback) {
var ps;
switch (_process.platform) {
case "darwin":
ps = spawn("pgrep", ["-P", pid, "-l"]);
break;
default:
ps = spawn("ps", ["--ppid", pid]);
break;
}
var allData = "";
ps.stdout.on("data", function (data) {
var data = data.toString("ascii");
allData += data;
});
ps.on("close", function () {
_console.log("ps info for " + pid);
_console.log(allData);
callback();
});
}
function killChildProcesses (pid, callback) {
getTree(pid, function (tree) {
var children = tree[pid.toString()];
var killNext = function () {
if (children.length > 0) {
treeKill(children.shift(), "SIGKILL", function () {
killNext();
});
} else {
callback();
}
};
/* istanbul ignore else */
if (children && children.length > 0) {
killNext();
} else {
callback();
}
});
}
function getTree (pid, callback) {
var tree = {};
var pidsToProcess = {};
tree[pid] = [];
pidsToProcess[pid] = 1;
switch (_process.platform) {
case "win32":
throw new Error("Operation unsupported on Windows");
/* istanbul ignore next */
break;
case "darwin":
buildProcessTree(pid, tree, pidsToProcess, function (parentPid) {
return spawn("pgrep", ["-P", parentPid]);
}, function () {
if (lib.debug) {
showDebugInfo(pid, function () {
callback(tree);
});
} else {
callback(tree);
}
});
break;
case "sunos":
throw new Error("Operation unsupported on SunOS");
/* istanbul ignore next */
break;
default: // Linux
buildProcessTree(pid, tree, pidsToProcess, function (parentPid) {
return spawn("ps", ["-o", "pid", "--no-headers", "--ppid", parentPid]);
}, function () {
if (lib.debug) {
showDebugInfo(pid, function () {
callback(tree);
});
} else {
callback(tree);
}
});
break;
}
}
function treeKill (pid, signal, callback) {
var tree = {};
var pidsToProcess = {};
tree[pid] = [];
pidsToProcess[pid] = 1;
switch (_process.platform) {
case "win32":
exec("taskkill /pid " + pid + " /T /F", callback);
break;
case "darwin":
buildProcessTree(pid, tree, pidsToProcess, function (parentPid) {
return spawn("pgrep", ["-P", parentPid]);
}, function () {
killAll(tree, signal, callback);
});
break;
case "sunos":
throw new Error("Operation unsupported on SunOS");
/* istanbul ignore next */
break;
default: // Linux
buildProcessTree(pid, tree, pidsToProcess, function (parentPid) {
return spawn("ps", ["-o", "pid", "--no-headers", "--ppid", parentPid]);
}, function () {
killAll(tree, signal, callback);
});
break;
}
}
function killAll (tree, signal, callback) {
var killed = {};
try {
Object.keys(tree).forEach(function (pid) {
tree[pid].forEach(function (pidpid) {
if (!killed[pidpid]) {
killPid(pidpid, signal);
killed[pidpid] = 1;
}
});
if (!killed[pid]) {
killPid(pid, signal);
killed[pid] = 1;
}
});
} catch (err) {
if (callback) {
return callback(err);
} else {
throw err;
}
}
if (callback) {
return callback();
}
}
function killPid(pid, signal) {
signal = signal ? signal : "SIGKILL";
try {
_process.kill(parseInt(pid, 10), signal);
} catch (err) {
if (err.code !== "ESRCH") {
throw err;
}
}
}
function killPids(pids, signal) {
pids.forEach(function (pid) {
killPid(pid, signal);
});
}
function buildProcessTree (parentPid, tree, pidsToProcess, spawnChildProcessesList, cb) {
var ps = spawnChildProcessesList(parentPid);
var allData = "";
ps.stdout.on("data", function (data) {
var data = data.toString("ascii");
allData += data;
});
var onClose = function (code) {
delete pidsToProcess[parentPid];
if (code !== 0) {
// no more parent processes
/* istanbul ignore else */
if (Object.keys(pidsToProcess).length === 0) {
cb();
}
return;
}
allData.match(/\d+/g).forEach(function (pid) {
pid = parseInt(pid, 10);
tree[parentPid].push(pid);
tree[pid] = [];
pidsToProcess[pid] = 1;
buildProcessTree(pid, tree, pidsToProcess, spawnChildProcessesList, cb);
});
};
ps.on("close", onClose);
}
var lib = {
debug: false,
kill: treeKill,
killPid: killPid,
getTree: getTree,
killChildProcesses: killChildProcesses,
getZombieChildren: getZombieChildren,
killPids: killPids,
setMocks: function(mocks) {
if (mocks.spawn) {
spawn = mocks.spawn;
}
if (mocks.exec) {
exec = mocks.exec;
}
if (mocks.process) {
_process = mocks.process;
}
if (mocks.console) {
_console = mocks.console;
}
if (mocks.date) {
_getDate = mocks.date;
}
if (mocks.setTimeout) {
_setTimeout = mocks.setTimeout;
}
}
};
module.exports = lib;