shell: merge spawn into sudo

This commit is contained in:
Girish Ramakrishnan
2024-02-22 12:43:23 +01:00
parent c4b8d3b832
commit e6aef755e3
+30 -46
View File
@@ -61,61 +61,45 @@ async function exec(tag, cmd, options) {
return await execArgs(tag, file, args, options);
}
// use this when you are afraid of how arguments will split up
function spawn(tag, file, args, options, callback) {
assert.strictEqual(typeof tag, 'string');
assert.strictEqual(typeof file, 'string');
assert(Array.isArray(args));
assert.strictEqual(typeof options, 'object');
assert.strictEqual(typeof callback, 'function');
callback = once(callback); // exit may or may not be called after an 'error'
if (options.ipc) options.stdio = ['pipe', 'pipe', 'pipe', 'ipc'];
debug(tag + ' spawn: %s %s', file, args.join(' ').replace(/\n/g, '\\n'));
const cp = child_process.spawn(file, args, options);
let stdoutResult = '';
cp.stdout.on('data', function (data) {
debug(tag + ' (stdout): %s', data.toString('utf8'));
stdoutResult += data.toString('utf8');
});
cp.stderr.on('data', function (data) {
debug(tag + ' (stderr): %s', data.toString('utf8'));
});
cp.on('exit', function (code, signal) {
if (code || signal) debug(tag + ' code: %s, signal: %s', code, signal);
if (code === 0) return callback(null, stdoutResult);
let e = new BoxError(BoxError.SHELL_ERROR, `${tag} exited with code ${code} signal ${signal}`);
e.code = code;
e.signal = signal;
callback(e);
});
cp.on('error', function (error) {
debug(tag + ' code: %s, signal: %s', error.code, error.signal);
let e = new BoxError(BoxError.SHELL_ERROR, `${tag} errored with code ${error.code} message ${error.message}`);
callback(e);
});
return cp;
}
function sudo(tag, args, options, callback) {
assert.strictEqual(typeof tag, 'string');
assert(Array.isArray(args));
assert.strictEqual(typeof options, 'object');
assert.strictEqual(typeof callback, 'function');
let sudoArgs = [ '-S' ]; // -S makes sudo read stdin for password
const sudoArgs = [ '-S' ]; // -S makes sudo read stdin for password
if (options.preserveEnv) sudoArgs.push('-E'); // -E preserves environment
if (options.ipc) sudoArgs.push('--close-from=4'); // keep the ipc open. requires closefrom_override in sudoers file
const cp = spawn(tag, SUDO, sudoArgs.concat(args), options, callback);
callback = once(callback); // exit may or may not be called after an 'error'
if (options.ipc) options.stdio = ['pipe', 'pipe', 'pipe', 'ipc'];
const spawnArgs = sudoArgs.concat(args);
debug(`${tag} ${SUDO} ${spawnArgs.join(' ').replace(/\n/g, '\\n')}`);
const cp = child_process.spawn(SUDO, spawnArgs, options);
let stdoutResult = '';
cp.stdout.on('data', (data) => stdoutResult += data.toString('utf8'));
cp.on('exit', function (code, signal) {
if (code === 0) return callback(null, stdoutResult);
const e = new BoxError(BoxError.SHELL_ERROR, `${tag} exited with code ${code} signal ${signal}`);
e.code = code;
e.signal = signal;
debug(`${tag}: ${SUDO} ${spawnArgs.join(' ').replace(/\n/g, '\\n')} errored`, e);
callback(e);
});
cp.on('error', function (error) {
debug(`${tag}: ${SUDO} ${spawnArgs.join(' ').replace(/\n/g, '\\n')} errored`, error);
const e = new BoxError(BoxError.SHELL_ERROR, `${tag} errored with code ${error.code} message ${error.message}`);
callback(e);
});
cp.stdin.end();
if (options.onMessage) cp.on('message', options.onMessage);
return cp;