pax_global_header00006660000000000000000000000064135742331110014512gustar00rootroot0000000000000052 comment=cb478381547107f5c53362668533f634beff7e6e node-tree-kill-1.2.2/000077500000000000000000000000001357423311100143275ustar00rootroot00000000000000node-tree-kill-1.2.2/.npmignore000066400000000000000000000000041357423311100163200ustar00rootroot00000000000000testnode-tree-kill-1.2.2/LICENSE000066400000000000000000000020561357423311100153370ustar00rootroot00000000000000MIT License Copyright (c) 2018 Peter Krumins Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. node-tree-kill-1.2.2/README.md000066400000000000000000000036351357423311100156150ustar00rootroot00000000000000Tree Kill ========= Kill all processes in the process tree, including the root process. Examples ======= Kill all the descendent processes of the process with pid `1`, including the process with pid `1` itself: ```js var kill = require('tree-kill'); kill(1); ``` Send a signal other than SIGTERM.: ```js var kill = require('tree-kill'); kill(1, 'SIGKILL'); ``` Run a callback when done killing the processes. Passes an error argument if there was an error. ```js var kill = require('tree-kill'); kill(1, 'SIGKILL', function(err) { // Do things }); ``` You can also install tree-kill globally and use it as a command: ```sh tree-kill 1 # sends SIGTERM to process 1 and its descendents tree-kill 1 SIGTERM # same tree-kill 1 SIGKILL # sends KILL instead of TERMINATE ``` Methods ======= ## require('tree-kill')(pid, [signal], [callback]); Sends signal `signal` to all children processes of the process with pid `pid`, including `pid`. Signal defaults to `SIGTERM`. For Linux, this uses `ps -o pid --no-headers --ppid PID` to find the parent pids of `PID`. For Darwin/OSX, this uses `pgrep -P PID` to find the parent pids of `PID`. For Windows, this uses `'taskkill /pid PID /T /F'` to kill the process tree. Note that on Windows, sending the different kinds of POSIX signals is not possible. Install ======= With [npm](https://npmjs.org) do: ``` npm install tree-kill ``` License ======= MIT Changelog ========= ## [1.2.2] - 2019-12-11 ### Changed - security fix: sanitize `pid` parameter to fix arbitrary code execution vulnerability ## [1.2.1] - 2018-11-05 ### Changed - added missing LICENSE file - updated TypeScript definitions ## [1.2.0] - 2017-09-19 ### Added - TypeScript definitions ### Changed - `kill(pid, callback)` works. Before you had to use `kill(pid, signal, callback)` ## [1.1.0] - 2016-05-13 ### Added - A `tree-kill` CLI ## [1.0.0] - 2015-09-17 ### Added - optional callback - Darwin support node-tree-kill-1.2.2/cli.js000077500000000000000000000003641357423311100154420ustar00rootroot00000000000000#!/usr/bin/env node kill = require('.') try { kill(process.argv[2], process.argv[3], function(err){ if (err) { console.log(err.message) process.exit(1) } }) } catch (err) { console.log(err.message) process.exit(1) } node-tree-kill-1.2.2/index.d.ts000066400000000000000000000005751357423311100162370ustar00rootroot00000000000000/** * Kills process identified by `pid` and all its children * * @param pid * @param signal 'SIGTERM' by default * @param callback */ declare function treeKill(pid: number, callback?: (error?: Error) => void): void; declare function treeKill(pid: number, signal?: string | number, callback?: (error?: Error) => void): void; declare namespace treeKill {} export = treeKill; node-tree-kill-1.2.2/index.js000077500000000000000000000061121357423311100157770ustar00rootroot00000000000000'use strict'; var childProcess = require('child_process'); var spawn = childProcess.spawn; var exec = childProcess.exec; module.exports = function (pid, signal, callback) { if (typeof signal === 'function' && callback === undefined) { callback = signal; signal = undefined; } pid = parseInt(pid); if (Number.isNaN(pid)) { if (callback) { return callback(new Error("pid must be a number")); } else { throw new Error("pid must be a number"); } } 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': // buildProcessTreeSunOS(pid, tree, pidsToProcess, function () { // killAll(tree, signal, callback); // }); // 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) { try { process.kill(parseInt(pid, 10), signal); } catch (err) { if (err.code !== 'ESRCH') throw err; } } 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 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); } node-tree-kill-1.2.2/package.json000066400000000000000000000020021357423311100166070ustar00rootroot00000000000000{ "name": "tree-kill", "version": "1.2.2", "description": "kill trees of processes", "main": "index.js", "types": "index.d.ts", "bin": { "tree-kill": "cli.js" }, "scripts": { "test": "mocha" }, "repository": { "type": "git", "url": "git://github.com/pkrumins/node-tree-kill.git" }, "homepage": "https://github.com/pkrumins/node-tree-kill", "keywords": [ "tree", "trees", "process", "processes", "kill", "signal" ], "author": { "name": "Peteris Krumins", "email": "peteris.krumins@gmail.com", "url": "http://www.catonmat.net" }, "contributors": [ { "name": "Todd Wolfson", "email": "todd@twolfson.com", "url": "http://twolfson.com/" }, { "name": "William Hilton", "email": "wmhilton@gmail.com", "url": "http://wmhilton.com/" }, { "name": "Fabrício Matté", "url": "http://ultcombo.js.org/" } ], "license": "MIT", "devDependencies": { "mocha": "^2.2.5" } } node-tree-kill-1.2.2/test/000077500000000000000000000000001357423311100153065ustar00rootroot00000000000000node-tree-kill-1.2.2/test/spin.js000066400000000000000000000000571357423311100166170ustar00rootroot00000000000000setInterval(function(){ // Do nothing }, 100);node-tree-kill-1.2.2/test/test.js000066400000000000000000000026241357423311100166270ustar00rootroot00000000000000var assert = require('assert'); var fork = require('child_process').fork; var kill = require('..'); describe('kill()', function(){ it('should kill a process', function(done){ var p = fork('./test/spin') assert.ok(p.pid) p.on('exit', function(code, signal){ assert.ok(code || signal, 'should return an exit code') return done() }); kill(p.pid) }) it('should call a callback', function(done){ var p = fork('./test/spin') assert.ok(p.pid) kill(p.pid, null, function() { return done() }) }) it('should work if signal is left out', function(done){ var p = fork('./test/spin') assert.ok(p.pid) kill(p.pid, function() { return done() }) }) it('should reject invalid pid', function(done){ var p = fork('./test/spin') assert.ok(p.pid) kill('rm -rf /dev/null', function(err) { assert.ok(typeof err === 'object') return done() }) }) it('should reject invalid pids even if no callback', function(done){ var p = fork('./test/spin') assert.ok(p.pid) try { kill('rm -rf /dev/null') assert.fail('should have thrown') } catch (err) { assert.ok(typeof err === 'object') return done(); } }) }) node-tree-kill-1.2.2/test/types.ts000066400000000000000000000005201357423311100170170ustar00rootroot00000000000000// examples of valid usage // importing just the type definitions, not the actual code import * as kill from '../index.d'; kill(1); kill(1, 9); kill(1, 'SIGKILL'); kill(1, 'SIGKILL', () => { console.log('done'); }); kill(1, 'SIGKILL', (err) => { if (err) { console.log(err.message); } console.log('done'); });