From 34a14d315939566035be249a0772f0e1921d911e Mon Sep 17 00:00:00 2001 From: Jinsu Mathew Date: Tue, 11 Mar 2025 22:27:51 -0500 Subject: [PATCH] Build process tree for macOS --- index.js | 49 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 8df6a0f..74f3add 100755 --- a/index.js +++ b/index.js @@ -29,10 +29,8 @@ module.exports = function (pid, signal, callback) { 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); + buildProcessTreeMacOS(pid, tree, pidsToProcess, function () { + killAll(tree, signal, callback); }); break; // case 'sunos': @@ -116,3 +114,46 @@ function buildProcessTree (parentPid, tree, pidsToProcess, spawnChildProcessesLi ps.on('close', onClose); } + +function buildProcessTreeMacOS(parentPid, tree, pidsToProcess, cb) { + try { + let stdout = ''; + try { + stdout = execSync('pgrep -P ' + parentPid, { + encoding: 'utf8', + timeout: 2000, + maxBuffer: 1024 * 1024 + }); + } catch (execError) { + if (execError.status !== 1) { + throw execError; + } + } + + delete pidsToProcess[parentPid]; + + const childPids = stdout.trim().split('\n').filter(Boolean); + + if (childPids.length === 0) { + if (Object.keys(pidsToProcess).length === 0) { + cb(); + } + return; + } + + childPids.forEach(function(pidStr) { + const pid = parseInt(pidStr, 10); + if (!isNaN(pid)) { + tree[parentPid].push(pid); + tree[pid] = []; + pidsToProcess[pid] = 1; + buildProcessTreeMacOS(pid, tree, pidsToProcess, cb); + } + }); + } catch (err) { + delete pidsToProcess[parentPid]; + if (Object.keys(pidsToProcess).length === 0) { + cb(); + } + } +} \ No newline at end of file