sudo: add kill-child.sh

ultimately, a non-previlieged child cannot kill previlieged parent.
all the notes and research in shell.js are not useful.
This commit is contained in:
Girish Ramakrishnan
2025-07-16 20:37:13 +02:00
parent e4ceedcac6
commit e03beba9bc
3 changed files with 55 additions and 6 deletions
+6 -6
View File
@@ -9,6 +9,7 @@ const assert = require('assert'),
TransformStream = stream.Transform;
const LOGTAIL_CMD = path.join(__dirname, 'scripts/logtail.sh');
const KILL_CHILD_CMD = path.join(__dirname, 'scripts/kill-child.sh');
class LogStream extends TransformStream {
constructor(options) {
@@ -68,13 +69,12 @@ function tail(filePaths, options) {
if (options.follow) args.push('--follow');
if (options.sudo) {
const cp = child_process.spawn('/usr/bin/sudo', [ '-S', LOGTAIL_CMD, ...args, ...filePaths ]);
cp.terminate = () => { // see note in shell.js
child_process.spawn('kill', ['-SIGKILL', -cp.pid], { detached: true }, (error) => {
if (error) debug(`tail could not terminate`, error);
const cp = child_process.spawn('/usr/bin/sudo', [ LOGTAIL_CMD, ...args, ...filePaths ]);
cp.terminate = () => {
child_process.execFile('/usr/bin/sudo', [ KILL_CHILD_CMD, cp.pid, process.pid ], { encoding: 'utf8' }, (error, stdout, stderr) => {
if (error) debug(`tail: failed to kill children`, stdout, stderr);
});
};
cp.stdin.end();
return cp;
} else {
const cp = child_process.spawn('/usr/bin/tail', args.concat(filePaths));
@@ -96,7 +96,7 @@ function journalctl(unit, options) {
if (options.follow) args.push('--follow');
const cp = spawn('journalctl', args);
const cp = child_process.spawn('journalctl', args);
cp.terminate = () => cp.kill('SIGKILL');
return cp;
}