2018-04-20 17:23:41 -04:00
|
|
|
/*
|
|
|
|
Copyright 2018 Intel Corporation
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
2018-04-03 20:07:48 -04:00
|
|
|
|
2018-04-20 17:23:41 -04:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2018-04-03 20:07:48 -04:00
|
|
|
|
2018-04-20 17:23:41 -04:00
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// JavaScript source code
|
|
|
|
var GM = require('_GenericMarshal');
|
2018-04-03 20:07:48 -04:00
|
|
|
|
2018-04-20 17:23:41 -04:00
|
|
|
function processManager() {
|
2018-04-03 20:07:48 -04:00
|
|
|
this._ObjectID = 'processManager';
|
2018-04-20 17:23:41 -04:00
|
|
|
switch (process.platform) {
|
2018-04-03 20:07:48 -04:00
|
|
|
case 'win32':
|
|
|
|
this._kernel32 = GM.CreateNativeProxy('kernel32.dll');
|
|
|
|
this._kernel32.CreateMethod('GetLastError');
|
|
|
|
this._kernel32.CreateMethod('CreateToolhelp32Snapshot');
|
|
|
|
this._kernel32.CreateMethod('Process32First');
|
|
|
|
this._kernel32.CreateMethod('Process32Next');
|
|
|
|
break;
|
|
|
|
case 'linux':
|
|
|
|
this._childProcess = require('child_process');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw (process.platform + ' not supported');
|
|
|
|
}
|
2018-04-20 17:23:41 -04:00
|
|
|
this.getProcesses = function getProcesses(callback) {
|
|
|
|
switch (process.platform) {
|
2018-04-03 20:07:48 -04:00
|
|
|
default:
|
|
|
|
throw ('Enumerating processes on ' + process.platform + ' not supported');
|
|
|
|
case 'win32':
|
2018-04-20 19:05:06 -04:00
|
|
|
var retVal = {};
|
2018-04-03 20:07:48 -04:00
|
|
|
var h = this._kernel32.CreateToolhelp32Snapshot(2, 0);
|
|
|
|
var info = GM.CreateVariable(304);
|
|
|
|
info.toBuffer().writeUInt32LE(304, 0);
|
|
|
|
var nextProcess = this._kernel32.Process32First(h, info);
|
2018-04-20 17:23:41 -04:00
|
|
|
while (nextProcess.Val) {
|
2018-04-20 19:05:06 -04:00
|
|
|
retVal[info.Deref(8, 4).toBuffer().readUInt32LE(0)] = { cmd: info.Deref(GM.PointerSize == 4 ? 36 : 44, 260).String };
|
2018-04-03 20:07:48 -04:00
|
|
|
nextProcess = this._kernel32.Process32Next(h, info);
|
|
|
|
}
|
|
|
|
if (callback) { callback.apply(this, [retVal]); }
|
|
|
|
break;
|
|
|
|
case 'linux':
|
|
|
|
if (!this._psp) { this._psp = {}; }
|
|
|
|
var p = this._childProcess.execFile("/bin/ps", ["ps", "-uxa"], { type: this._childProcess.SpawnTypes.TERM });
|
|
|
|
this._psp[p.pid] = p;
|
|
|
|
p.Parent = this;
|
|
|
|
p.ps = '';
|
|
|
|
p.callback = callback;
|
|
|
|
p.args = [];
|
|
|
|
for (var i = 1; i < arguments.length; ++i) { p.args.push(arguments[i]); }
|
2018-04-20 17:23:41 -04:00
|
|
|
p.on('exit', function onGetProcesses() {
|
|
|
|
delete this.Parent._psp[this.pid];
|
2018-04-20 19:05:06 -04:00
|
|
|
var retVal = {}, lines = this.ps.split('\x0D\x0A'), key = {}, keyi = 0;
|
2018-04-20 17:23:41 -04:00
|
|
|
for (var i in lines) {
|
2018-04-03 20:07:48 -04:00
|
|
|
var tokens = lines[i].split(' ');
|
|
|
|
var tokenList = [];
|
2018-04-20 17:23:41 -04:00
|
|
|
for (var x in tokens) {
|
2018-04-03 20:07:48 -04:00
|
|
|
if (i == 0 && tokens[x]) { key[tokens[x]] = keyi++; }
|
2018-04-20 17:23:41 -04:00
|
|
|
if (i > 0 && tokens[x]) { tokenList.push(tokens[x]); }
|
2018-04-03 20:07:48 -04:00
|
|
|
}
|
2018-04-20 17:23:41 -04:00
|
|
|
if ((i > 0) && (tokenList[key.PID])) {
|
2018-04-20 19:05:06 -04:00
|
|
|
retVal[tokenList[key.PID]] = { user: tokenList[key.USER], cmd: tokenList[key.COMMAND] };
|
2018-04-03 20:07:48 -04:00
|
|
|
}
|
|
|
|
}
|
2018-04-20 17:23:41 -04:00
|
|
|
if (this.callback) {
|
2018-04-03 20:07:48 -04:00
|
|
|
this.args.unshift(retVal);
|
|
|
|
this.callback.apply(this.parent, this.args);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
p.stdout.on('data', function (chunk) { this.parent.ps += chunk.toString(); });
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
2018-04-20 17:23:41 -04:00
|
|
|
this.getProcessInfo = function getProcessInfo(pid) {
|
|
|
|
switch (process.platform) {
|
2018-04-03 20:07:48 -04:00
|
|
|
default:
|
|
|
|
throw ('getProcessInfo() not supported for ' + process.platform);
|
|
|
|
case 'linux':
|
2018-04-20 17:23:41 -04:00
|
|
|
var status = require('fs').readFileSync('/proc/' + pid + '/status'), info = {}, lines = status.toString().split('\n');
|
|
|
|
for (var i in lines) {
|
2018-04-03 20:07:48 -04:00
|
|
|
var tokens = lines[i].split(':');
|
|
|
|
if (tokens.length > 1) { tokens[1] = tokens[1].trim(); }
|
|
|
|
info[tokens[0]] = tokens[1];
|
|
|
|
}
|
|
|
|
return (info);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = new processManager();
|