Skip to content

Commit

Permalink
replaces tree-kill with custom implementation; fixes some security au…
Browse files Browse the repository at this point in the history
…dit things
  • Loading branch information
MrDiggles2 committed Apr 12, 2024
1 parent be854a6 commit 9182638
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
dist
*.tgz
logs
logs
.DS_Store
4 changes: 4 additions & 0 deletions example/nested-process.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var cp = require('child_process');

var child = cp.exec("node -e 'while (true);'");
console.log(`Running child with pid ${child.pid}`);
7 changes: 7 additions & 0 deletions mux.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ const config: MuxConfig = {
exec: `${dcBin} down`
}
},
{
name: 'nested-process',
run: {
dir: './example',
exec: 'node nested-process.js',
}
},
{
name: 'random number printer',
install: {
Expand Down
171 changes: 163 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
"dependencies": {
"chalk": "^4.1.0",
"node-pty": "^0.10.1",
"ps-tree": "^1.2.0",
"tail": "^2.2.6",
"terminal-kit": "^3.0.0",
"tree-kill": "^1.2.2",
"ts-node": "^10.9.1",
"typescript": "^4.8.4"
},
"devDependencies": {
"@types/ps-tree": "^1.1.6",
"@types/tail": "^2.2.1",
"@types/terminal-kit": "^2.5.0"
},
Expand Down
22 changes: 22 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import psTree from "ps-tree"

/**
* Sends the specified signal to the provided pid and all of its descendents.
*
* Custom implementation of 'tree-kill'. It had some issues with detecting and
* stopping grandchildren. It uses `pgrep -P PID` under the hood which does not
* return the full tree of pids.
*/
export const treeKill = (pid: number, signal: string = 'SIGTERM'): Promise<number[]> => {
return new Promise((resolve, reject) => {
psTree(pid, (err, children) => {
if (err) {
reject(err);
} else {
const childPids = children.map(child => Number(child.PID))
childPids.forEach(childPid => process.kill(childPid, signal))
resolve(childPids);
}
});
})
}
5 changes: 3 additions & 2 deletions src/process.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import path from "path";
import fs from 'fs';
import { Tail } from "tail";
import treeKill from "tree-kill";
import { MuxCommand, MuxProcessConfig, MuxLogger, MuxConfig } from "./config";
import { IPty, spawn } from 'node-pty';
import { treeKill } from "./helpers";

export class MuxProcess {
public name: string;
Expand Down Expand Up @@ -38,7 +38,8 @@ export class MuxProcess {
//
// So instead, use `treeKill` to send a SIGTERM to all child processes
// at once to ensure a clean exit.
treeKill(child.pid, 'SIGTERM');
const pids = await treeKill(child.pid, 'SIGTERM');
this.logger.debug(`${this.name}: sent SIGTERM to ${pids.join(', ')}`);
}
}

Expand Down

0 comments on commit 9182638

Please sign in to comment.