2020-01-10 05:18:55 -05:00
|
|
|
const lodashOnce = require('lodash.once')
|
2024-11-08 05:21:56 -05:00
|
|
|
const childProcess = require('node:child_process')
|
2021-07-12 12:47:19 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2019-12-18 05:01:59 -05:00
|
|
|
const { ConversionsDisabledError, FailedCommandError } = require('./Errors')
|
2019-12-16 05:24:35 -05: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 05:01:59 -05: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 05:42:31 -05:00
|
|
|
if (!Settings.enableConversions) {
|
2019-12-18 05:01:59 -05:00
|
|
|
return callback(
|
|
|
|
new ConversionsDisabledError('image conversions are disabled')
|
|
|
|
)
|
2019-12-16 05:42:31 -05:00
|
|
|
}
|
2019-12-16 05:24:35 -05:00
|
|
|
|
2019-12-18 05:01:59 -05:00
|
|
|
const [cmd, ...args] = command
|
2019-12-16 05:24:35 -05:00
|
|
|
|
2019-12-18 05:01:59 -05:00
|
|
|
const child = childProcess.spawn(cmd, args, { detached: true })
|
2019-12-16 05:42:31 -05:00
|
|
|
let stdout = ''
|
|
|
|
let stderr = ''
|
2019-12-16 05:24:35 -05:00
|
|
|
|
2019-12-18 05:01:59 -05:00
|
|
|
let killTimer
|
2019-12-16 05:24:35 -05:00
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
const cleanup = lodashOnce(function (err) {
|
2020-01-07 16:19:26 -05:00
|
|
|
if (killTimer) {
|
|
|
|
clearTimeout(killTimer)
|
|
|
|
}
|
|
|
|
callback(err, stdout, stderr)
|
|
|
|
})
|
|
|
|
|
2019-12-18 05:01:59 -05:00
|
|
|
if (options.timeout) {
|
2020-08-10 12:01:12 -04:00
|
|
|
killTimer = setTimeout(function () {
|
2019-12-16 05:42:31 -05:00
|
|
|
try {
|
|
|
|
// use negative process id to kill process group
|
2019-12-18 05:01:59 -05:00
|
|
|
process.kill(-child.pid, options.killSignal || 'SIGTERM')
|
2019-12-16 05:42:31 -05:00
|
|
|
} catch (error) {
|
2020-01-07 16:19:26 -05:00
|
|
|
cleanup(
|
2020-04-30 08:20:40 -04:00
|
|
|
new FailedCommandError('failed to kill process after timeout', {
|
|
|
|
command,
|
|
|
|
options,
|
2021-07-13 07:04:46 -04:00
|
|
|
pid: child.pid,
|
2020-01-07 16:19:26 -05:00
|
|
|
})
|
2019-12-16 05:42:31 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}, options.timeout)
|
|
|
|
}
|
2019-12-16 05:24:35 -05:00
|
|
|
|
2020-08-10 12:01:12 -04:00
|
|
|
child.on('close', function (code, signal) {
|
2019-12-18 05:01:59 -05:00
|
|
|
if (code || signal) {
|
|
|
|
return cleanup(
|
|
|
|
new FailedCommandError(command, code || signal, stdout, stderr)
|
|
|
|
)
|
|
|
|
}
|
2019-12-16 05:24:35 -05:00
|
|
|
|
2019-12-18 05:01:59 -05:00
|
|
|
cleanup()
|
|
|
|
})
|
|
|
|
|
2021-07-13 07:04:46 -04:00
|
|
|
child.on('error', err => {
|
2019-12-18 05:01:59 -05:00
|
|
|
cleanup(err)
|
|
|
|
})
|
2021-07-13 07:04:46 -04:00
|
|
|
child.stdout.on('data', chunk => {
|
2019-12-18 05:01:59 -05:00
|
|
|
stdout += chunk
|
|
|
|
})
|
2021-07-13 07:04:46 -04:00
|
|
|
child.stderr.on('data', chunk => {
|
2019-12-18 05:01:59 -05:00
|
|
|
stderr += chunk
|
|
|
|
})
|
|
|
|
}
|
2019-12-16 05:24:35 -05:00
|
|
|
|
2019-12-18 05:01:59 -05: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 05:42:31 -05:00
|
|
|
}
|