mirror of
https://github.com/Ylianst/MeshAgent
synced 2025-12-19 17:53:28 +00:00
updated process-manager to include 'user' on FreeBSD and MacOS
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -101,60 +101,22 @@ function processManager() {
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
case 'darwin':
|
case 'darwin':
|
||||||
var promise = require('promise');
|
case 'freebsd':
|
||||||
var p = new promise(function (res, rej) { this._res = res; this._rej = rej; });
|
var p = require('child_process').execFile('/bin/sh', ['sh']);
|
||||||
p.pm = this;
|
p.stdout.str = ''; p.stdout.on('data', function (c) { this.str += c.toString(); });
|
||||||
p.callback = callback;
|
p.stderr.str = ''; p.stderr.on('data', function (c) { this.str += c.toString(); });
|
||||||
|
p.stdin.write('ps -axo pid -o user -o command | tr ' + "'\\n' '\\t' | awk -F" + '"\\t" \'{ printf "{"; for(i=2;i<NF;++i) { split($i,tok," "); pid=tok[1]; user=tok[2]; cmd=substr($i,length(tok[1])+length(tok[2])+2); gsub(/\\\\/,"\\\\\\\\&",cmd); gsub(/"/,"\\\\\\\\&",cmd); gsub(/^[ ]+/,"",cmd); printf "%s\\"%s\\":{\\"pid\\":\\"%s\\",\\"user\\":\\"%s\\",\\"cmd\\":\\"%s\\"}",(i!=2?",":""),pid,pid,user,cmd; } printf "}"; }\'\nexit\n');
|
||||||
|
p.waitExit();
|
||||||
|
|
||||||
|
if (callback)
|
||||||
|
{
|
||||||
p.args = [];
|
p.args = [];
|
||||||
for (var i = 1; i < arguments.length; ++i) { p.args.push(arguments[i]); }
|
for (var i = 1; i < arguments.length; ++i) { p.args.push(arguments[i]); }
|
||||||
p.child = this._childProcess.execFile("/bin/ps", ["ps", "-xa"]);
|
|
||||||
p.child.promise = p;
|
|
||||||
p.child.stdout.ps = '';
|
|
||||||
p.child.stdout.on('data', function (chunk) { this.ps += chunk.toString(); });
|
|
||||||
p.child.on('exit', function ()
|
|
||||||
{
|
|
||||||
var lines = this.stdout.ps.split('\n');
|
|
||||||
var pidX = lines[0].split('PID')[0].length + 3;
|
|
||||||
var cmdX = lines[0].split('CMD')[0].length;
|
|
||||||
var ret = {};
|
|
||||||
for (var i = 1; i < lines.length; ++i)
|
|
||||||
{
|
|
||||||
if (lines[i].length > 0)
|
|
||||||
{
|
|
||||||
ret[lines[i].substring(0, pidX).trim()] = { pid: lines[i].substring(0, pidX).trim(), cmd: lines[i].substring(cmdX) };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.promise._res(ret);
|
|
||||||
});
|
|
||||||
p.then(function (ps)
|
|
||||||
{
|
|
||||||
this.args.unshift(ps);
|
|
||||||
this.callback.apply(this.pm, this.args);
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
case 'freebsd':
|
|
||||||
var child = require('child_process').execFile('/bin/sh', ['sh']);
|
|
||||||
child.stderr.str = '';
|
|
||||||
child.stderr.on('data', function (c) {this.str += c.toString();});
|
|
||||||
child.stdout.str = '';
|
|
||||||
child.stdout.on('data', function (c) { this.str += c.toString(); });
|
|
||||||
child.stdin.write("ps -xa | awk '{ printf \"%s\", $1; $1=\"\"; $2=\"\"; $3=\"\"; $4=\"\"; printf \"%s\\n\", $0; }' | awk '{ printf \"%s\", $1; $1=\"\"; printf \"%s\\n\", $0; }'\nexit\n");
|
|
||||||
child.waitExit();
|
|
||||||
|
|
||||||
var tmp;
|
p.args.unshift(JSON.parse(p.stdout.str));
|
||||||
var ret = [];
|
callback.apply(this, p.args);
|
||||||
var lines = child.stdout.str.trim().split('\n');
|
|
||||||
for(var i in lines)
|
|
||||||
{
|
|
||||||
tmp = {pid: lines[i].split(' ').shift()};
|
|
||||||
tmp['cmd'] = lines[i].substring(tmp.pid.length + 1);
|
|
||||||
tmp['pid'] = parseInt(tmp['pid']);
|
|
||||||
if(!isNaN(tmp['pid']))
|
|
||||||
{
|
|
||||||
ret.push(tmp);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if(callback) { callback.apply(this, [ret]); }
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user