Files
cloudron-box/src/shell.js
T

148 lines
5.9 KiB
JavaScript
Raw Normal View History

'use strict';
2021-05-12 17:30:29 -07:00
const assert = require('assert'),
2019-12-04 10:29:06 -08:00
BoxError = require('./boxerror.js'),
child_process = require('child_process'),
2016-08-30 21:33:56 -07:00
debug = require('debug')('box:shell'),
2022-04-15 19:01:35 -05:00
once = require('./once.js'),
util = require('util');
2024-10-15 10:10:15 +02:00
exports = module.exports = shell;
function shell(tag) {
assert.strictEqual(typeof tag, 'string');
return {
2024-10-16 10:25:07 +02:00
bash: bash.bind(null, tag),
2024-10-15 10:10:15 +02:00
spawn: spawn.bind(null, tag),
sudo: sudo.bind(null, tag),
promises: { sudo: util.promisify(sudo.bind(null, tag)) }
};
}
2020-07-31 16:35:06 -07:00
const SUDO = '/usr/bin/sudo';
2024-10-15 10:10:15 +02:00
function spawn(tag, file, args, options) {
2024-02-21 13:09:59 +01:00
assert.strictEqual(typeof tag, 'string');
assert.strictEqual(typeof file, 'string');
assert(Array.isArray(args));
2024-10-16 10:25:07 +02:00
assert.strictEqual(typeof options, 'object'); // note: spawn() has no encoding option of it's own
2024-02-21 13:09:59 +01:00
2024-10-16 10:25:07 +02:00
debug(`${tag}: ${file} ${args.join(' ').replace(/\n/g, '\\n')}`);
2024-02-21 19:40:27 +01:00
return new Promise((resolve, reject) => {
2024-10-16 10:25:07 +02:00
const cp = child_process.spawn(file, args, options);
2024-10-15 10:10:15 +02:00
const stdoutBuffers = [], stderrBuffers = [];
2024-02-21 19:40:27 +01:00
2024-10-15 10:10:15 +02:00
cp.stdout.on('data', (data) => stdoutBuffers.push(data));
cp.stderr.on('data', (data) => stderrBuffers.push(data));
cp.on('close', function (code, signal) { // always called. after 'exit' or 'error'
const stdoutBuffer = Buffer.concat(stdoutBuffers);
const stdout = options.encoding ? stdoutBuffer.toString(options.encoding) : stdoutBuffer;
if (code === 0) return resolve(stdout);
const stderrBuffer = Buffer.concat(stderrBuffers);
const stderr = options.encoding ? stderrBuffer.toString(options.encoding) : stderrBuffer;
const e = new BoxError(BoxError.SHELL_ERROR, `${file} exited with code ${code} signal ${signal}`);
2024-02-21 13:09:59 +01:00
e.stdout = stdout; // when promisified, this is the way to get stdout
e.stderr = stderr; // when promisified, this is the way to get stderr
2024-10-15 10:10:15 +02:00
e.code = code;
e.signal = signal;
debug(`${tag}: ${file} ${args.join(' ').replace(/\n/g, '\\n')} errored`, e);
2024-02-21 19:40:27 +01:00
reject(e);
});
2024-02-21 13:09:59 +01:00
2024-10-15 10:10:15 +02:00
cp.on('error', function (error) { // when the command itself could not be started
debug(`${tag}: ${file} ${args.join(' ').replace(/\n/g, '\\n')} errored`, error);
});
2024-02-21 19:40:27 +01:00
// https://github.com/nodejs/node/issues/25231
if (options.input) {
cp.stdin.write(options.input);
cp.stdin.end();
}
2024-02-21 13:09:59 +01:00
});
}
2024-10-16 10:25:07 +02:00
async function bash(tag, script, options) {
2024-02-21 18:37:10 +01:00
assert.strictEqual(typeof tag, 'string');
2024-10-16 10:25:07 +02:00
assert.strictEqual(typeof script, 'string');
2024-02-21 18:37:10 +01:00
assert.strictEqual(typeof options, 'object');
2024-10-16 10:25:07 +02:00
return await spawn(tag, '/bin/bash', [ '-c', script ], options);
2024-02-21 18:37:10 +01:00
}
2024-02-22 12:43:23 +01:00
function sudo(tag, args, options, callback) {
assert.strictEqual(typeof tag, 'string');
2021-05-02 11:26:08 -07:00
assert(Array.isArray(args));
assert.strictEqual(typeof options, 'object');
2017-09-09 19:48:05 -07:00
assert.strictEqual(typeof callback, 'function');
2024-02-22 12:43:23 +01:00
const sudoArgs = [ '-S' ]; // -S makes sudo read stdin for password
if (options.preserveEnv) sudoArgs.push('-E'); // -E preserves environment
callback = once(callback); // exit may or may not be called after an 'error'
2024-10-14 18:26:16 +02:00
const logFunc = options.outputHasTimestamps ? process.stdout.write : debug;
2024-07-08 09:58:25 +02:00
if (options.ipc) {
sudoArgs.push('--close-from=4'); // keep the ipc open. requires closefrom_override in sudoers file
options.stdio = ['pipe', 'pipe', 'pipe', 'ipc'];
}
2024-02-22 12:43:23 +01:00
const spawnArgs = sudoArgs.concat(args);
2020-07-31 16:35:06 -07:00
2024-02-22 12:43:23 +01:00
debug(`${tag} ${SUDO} ${spawnArgs.join(' ').replace(/\n/g, '\\n')}`);
const cp = child_process.spawn(SUDO, spawnArgs, options);
let stdoutResult = '';
2024-02-21 13:35:56 +01:00
cp.stdout.on('data', (data) => {
if (options.captureStdout) stdoutResult += data.toString('utf8');
if (!options.quiet) logFunc(data.toString('utf8'));
});
cp.stderr.on('data', (data) => logFunc(data.toString('utf8')));
cp.on('exit', function (code, signal) {
2024-07-08 09:58:25 +02:00
if (code === 0) return callback(null, options.captureStdout ? stdoutResult : null);
2024-02-22 12:43:23 +01:00
const e = new BoxError(BoxError.SHELL_ERROR, `${tag} exited with code ${code} signal ${signal}`);
e.code = code;
e.signal = signal;
2024-02-22 12:43:23 +01:00
if (cp.terminated) {
debug(`${tag}: ${SUDO} ${spawnArgs.join(' ').replace(/\n/g, '\\n')} terminated`); // was killed by us
} else {
debug(`${tag}: ${SUDO} ${spawnArgs.join(' ').replace(/\n/g, '\\n')} errored`, e);
}
2024-02-22 12:43:23 +01:00
callback(e);
});
cp.on('error', function (error) {
2024-02-22 12:43:23 +01:00
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}`);
e.code = error.code;
e.signal = error.signal;
2019-12-05 09:54:29 -08:00
callback(e);
});
2024-02-24 15:26:48 +01:00
// sudo forks and execs the program. sudo also hangs around as the parent of the program waiting on the program and also forwarding signals.
// sudo does not forward signals when the originator comes from the same process group. recently, there has been a change where it will
// forward signals as long as sudo or the command is not the group leader (https://www.sudo.ws/repos/sudo/rev/d1bf60eac57f)
// for us, this means that calling kill from this node process doesn't work since it's in the same group (and ubuntu 22 doesn't have the above fix).
// the workaround is to invoke a kill from a different process group and this is done by starting detached
// another idea is: use "ps --pid cp.pid -o pid=" to get the pid of the command and then send it signal directly
cp.terminate = function () {
cp.terminated = true; // hint for better debug message in 'exit'
2024-02-24 15:26:48 +01:00
child_process.spawn('kill', ['-SIGTERM', cp.pid], { detached: true }, (error) => { if (error) debug(`${tag} could not terminate`, error); });
};
cp.stdin.end();
2022-04-28 21:29:11 -07:00
if (options.onMessage) cp.on('message', options.onMessage);
2024-02-24 15:26:48 +01:00
return cp;
}