2019-12-16 10:42:31 +00:00
|
|
|
const _ = require('underscore')
|
|
|
|
const logger = require('logger-sharelatex')
|
2019-12-18 10:01:59 +00:00
|
|
|
const childProcess = require('child_process')
|
2019-12-16 10:42:31 +00:00
|
|
|
const Settings = require('settings-sharelatex')
|
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
|
|
|
|
2019-12-18 10:01:59 +00:00
|
|
|
if (options.timeout) {
|
|
|
|
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) {
|
2019-12-18 10:01:59 +00:00
|
|
|
logger.log(
|
2019-12-16 10:42:31 +00:00
|
|
|
{ process: child.pid, kill_error: error },
|
|
|
|
'error killing process'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}, options.timeout)
|
|
|
|
}
|
2019-12-16 10:24:35 +00:00
|
|
|
|
2019-12-18 10:01:59 +00:00
|
|
|
const cleanup = _.once(function(err) {
|
|
|
|
if (killTimer) {
|
|
|
|
clearTimeout(killTimer)
|
|
|
|
}
|
|
|
|
callback(err, stdout, stderr)
|
2019-12-16 10:42:31 +00:00
|
|
|
})
|
2019-12-16 10:24:35 +00:00
|
|
|
|
2019-12-18 10:01:59 +00:00
|
|
|
child.on('close', function(code, signal) {
|
|
|
|
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()
|
|
|
|
})
|
|
|
|
|
|
|
|
child.on('error', err => {
|
|
|
|
cleanup(err)
|
|
|
|
})
|
|
|
|
child.stdout.on('data', chunk => {
|
|
|
|
stdout += chunk
|
|
|
|
})
|
|
|
|
child.stderr.on('data', chunk => {
|
|
|
|
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
|
|
|
}
|