2020-01-10 10:18:55 +00:00
|
|
|
const lodashOnce = require('lodash.once')
|
2019-12-18 10:01:59 +00:00
|
|
|
const childProcess = require('child_process')
|
2021-07-12 16:47:19 +00:00
|
|
|
const Settings = require('@overleaf/settings')
|
2019-12-18 10:01:59 +00:00
|
|
|
const { ConversionsDisabledError, FailedCommandError } = require('./Errors')
|
2019-12-16 10:24:35 +00:00
|
|
|
|
|
|
|
// execute a command in the same way as 'exec' but with a timeout that
|
|
|
|
// kills all child processes
|
|
|
|
//
|
|
|
|
// we spawn the command with 'detached:true' to make a new process
|
|
|
|
// group, then we can kill everything in that process group.
|
|
|
|
|
2019-12-18 10:01:59 +00:00
|
|
|
module.exports = safeExec
|
|
|
|
module.exports.promises = safeExecPromise
|
|
|
|
|
|
|
|
// options are {timeout: number-of-milliseconds, killSignal: signal-name}
|
|
|
|
function safeExec(command, options, callback) {
|
2019-12-16 10:42:31 +00:00
|
|
|
if (!Settings.enableConversions) {
|
2019-12-18 10:01:59 +00:00
|
|
|
return callback(
|
|
|
|
new ConversionsDisabledError('image conversions are disabled')
|
|
|
|
)
|
2019-12-16 10:42:31 +00:00
|
|
|
}
|
2019-12-16 10:24:35 +00:00
|
|
|
|
2019-12-18 10:01:59 +00:00
|
|
|
const [cmd, ...args] = command
|
2019-12-16 10:24:35 +00:00
|
|
|
|
2019-12-18 10:01:59 +00:00
|
|
|
const child = childProcess.spawn(cmd, args, { detached: true })
|
2019-12-16 10:42:31 +00:00
|
|
|
let stdout = ''
|
|
|
|
let stderr = ''
|
2019-12-16 10:24:35 +00:00
|
|
|
|
2019-12-18 10:01:59 +00:00
|
|
|
let killTimer
|
2019-12-16 10:24:35 +00:00
|
|
|
|
2020-08-10 16:01:12 +00:00
|
|
|
const cleanup = lodashOnce(function (err) {
|
2020-01-07 21:19:26 +00:00
|
|
|
if (killTimer) {
|
|
|
|
clearTimeout(killTimer)
|
|
|
|
}
|
|
|
|
callback(err, stdout, stderr)
|
|
|
|
})
|
|
|
|
|
2019-12-18 10:01:59 +00:00
|
|
|
if (options.timeout) {
|
2020-08-10 16:01:12 +00:00
|
|
|
killTimer = setTimeout(function () {
|
2019-12-16 10:42:31 +00:00
|
|
|
try {
|
|
|
|
// use negative process id to kill process group
|
2019-12-18 10:01:59 +00:00
|
|
|
process.kill(-child.pid, options.killSignal || 'SIGTERM')
|
2019-12-16 10:42:31 +00:00
|
|
|
} catch (error) {
|
2020-01-07 21:19:26 +00:00
|
|
|
cleanup(
|
2020-04-30 12:20:40 +00:00
|
|
|
new FailedCommandError('failed to kill process after timeout', {
|
|
|
|
command,
|
|
|
|
options,
|
2021-07-13 11:04:46 +00:00
|
|
|
pid: child.pid,
|
2020-01-07 21:19:26 +00:00
|
|
|
})
|
2019-12-16 10:42:31 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}, options.timeout)
|
|
|
|
}
|
2019-12-16 10:24:35 +00:00
|
|
|
|
2020-08-10 16:01:12 +00:00
|
|
|
child.on('close', function (code, signal) {
|
2019-12-18 10:01:59 +00:00
|
|
|
if (code || signal) {
|
|
|
|
return cleanup(
|
|
|
|
new FailedCommandError(command, code || signal, stdout, stderr)
|
|
|
|
)
|
|
|
|
}
|
2019-12-16 10:24:35 +00:00
|
|
|
|
2019-12-18 10:01:59 +00:00
|
|
|
cleanup()
|
|
|
|
})
|
|
|
|
|
2021-07-13 11:04:46 +00:00
|
|
|
child.on('error', err => {
|
2019-12-18 10:01:59 +00:00
|
|
|
cleanup(err)
|
|
|
|
})
|
2021-07-13 11:04:46 +00:00
|
|
|
child.stdout.on('data', chunk => {
|
2019-12-18 10:01:59 +00:00
|
|
|
stdout += chunk
|
|
|
|
})
|
2021-07-13 11:04:46 +00:00
|
|
|
child.stderr.on('data', chunk => {
|
2019-12-18 10:01:59 +00:00
|
|
|
stderr += chunk
|
|
|
|
})
|
|
|
|
}
|
2019-12-16 10:24:35 +00:00
|
|
|
|
2019-12-18 10:01:59 +00:00
|
|
|
function safeExecPromise(command, options) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
safeExec(command, options, (err, stdout, stderr) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err)
|
|
|
|
}
|
|
|
|
resolve({ stdout, stderr })
|
|
|
|
})
|
|
|
|
})
|
2019-12-16 10:42:31 +00:00
|
|
|
}
|