|
|
|
@ -2,24 +2,44 @@ const childProcess = require('child_process');
|
|
|
|
|
|
|
|
|
|
function getProcessInfo(pid) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
const process = childProcess.spawn('/bin/ps', ['-opid=,ppid=,command=', '-p', pid]);
|
|
|
|
|
let cmd, args, parseOutput;
|
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
|
cmd = 'wmic';
|
|
|
|
|
args = [
|
|
|
|
|
'process',
|
|
|
|
|
'where',
|
|
|
|
|
`ProcessId=${pid}`,
|
|
|
|
|
'get',
|
|
|
|
|
'ProcessId,ParentProcessId,CommandLine',
|
|
|
|
|
'/format:value'
|
|
|
|
|
];
|
|
|
|
|
parseOutput = parseWmicOutput;
|
|
|
|
|
} else {
|
|
|
|
|
cmd = '/bin/ps';
|
|
|
|
|
args = ['-opid=,ppid=,command=', '-p', pid];
|
|
|
|
|
parseOutput = parsePsOutput;
|
|
|
|
|
}
|
|
|
|
|
const ps = childProcess.spawn(cmd, args);
|
|
|
|
|
|
|
|
|
|
const data = [];
|
|
|
|
|
process.stdout.on('data', (chunk) => data.push(chunk));
|
|
|
|
|
ps.stdout.on('data', (chunk) => data.push(chunk));
|
|
|
|
|
|
|
|
|
|
process.on('close', () => {
|
|
|
|
|
ps.on('close', () => {
|
|
|
|
|
const output = Buffer.concat(data).toString();
|
|
|
|
|
try {
|
|
|
|
|
const result = parsePsOutput(output);
|
|
|
|
|
const result = parseOutput(output);
|
|
|
|
|
if (result.pid !== pid) {
|
|
|
|
|
throw new Error(`PS pid mismatch: ${result.pid} <> ${pid}`);
|
|
|
|
|
throw new Error(`PID mismatch: ${result.pid} <> ${pid}`);
|
|
|
|
|
}
|
|
|
|
|
if (!result.commandLine) {
|
|
|
|
|
throw new Error(`Could not get command line for process ${pid}`);
|
|
|
|
|
}
|
|
|
|
|
resolve(result);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
reject(e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
process.on('error', (e) => {
|
|
|
|
|
ps.on('error', (e) => {
|
|
|
|
|
reject(e);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
@ -37,4 +57,24 @@ function parsePsOutput(output) {
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function parseWmicOutput(output) {
|
|
|
|
|
const result = {};
|
|
|
|
|
const keyMap = {
|
|
|
|
|
ProcessId: 'pid',
|
|
|
|
|
ParentProcessId: 'parentPid',
|
|
|
|
|
CommandLine: 'commandLine'
|
|
|
|
|
};
|
|
|
|
|
for (const line of output.split(/\n/)) {
|
|
|
|
|
const match = line.trim().match(/^([^=]+)=(.*)$/);
|
|
|
|
|
if (match) {
|
|
|
|
|
const [, key, value] = match;
|
|
|
|
|
const mapped = keyMap[key];
|
|
|
|
|
if (mapped) {
|
|
|
|
|
result[mapped] = mapped.endsWith('id') ? value | 0 : value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = { getProcessInfo };
|
|
|
|
|