mirror of
https://github.com/Ylianst/MeshAgent
synced 2026-01-21 11:53:26 +00:00
Updated service-manager.enumerateService() for Linux, to include state and pid if available
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -124,6 +124,79 @@ function parseServiceStatus(token)
|
|||||||
return (j);
|
return (j);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (process.platform == 'linux')
|
||||||
|
{
|
||||||
|
function _upstart_GetServiceTable()
|
||||||
|
{
|
||||||
|
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("initctl list | tr '\n' '`' | awk -F'`' '");
|
||||||
|
child.stdin.write('{');
|
||||||
|
child.stdin.write(' printf "{"; ');
|
||||||
|
child.stdin.write(' for(i=1;i<NF;++i) ');
|
||||||
|
child.stdin.write(' {');
|
||||||
|
child.stdin.write(' c=split($i,name,","); ');
|
||||||
|
child.stdin.write(' c2=split(name[1],state," "); ');
|
||||||
|
child.stdin.write(' sname=substr(name[1],0,length(name[1])-length(state[c2])-1); ');
|
||||||
|
child.stdin.write(' split(state[c2],rstate,"/"); ');
|
||||||
|
child.stdin.write(' rs = rstate[2]=="running"?"RUNNING":"STOPPED";');
|
||||||
|
child.stdin.write(' spid=""; ');
|
||||||
|
child.stdin.write(' if(c==2) ');
|
||||||
|
child.stdin.write(' { ');
|
||||||
|
child.stdin.write(' split(name[2],pid," "); ');
|
||||||
|
child.stdin.write(' spid=pid[2]; ');
|
||||||
|
child.stdin.write(' } ');
|
||||||
|
child.stdin.write(' printf "%s\\"%s\\": {\\"state\\": \\"%s\\", \\"pid\\":\\"%s\\"}",(i==1?"":","),sname,rs,spid; ');
|
||||||
|
child.stdin.write(' } ');
|
||||||
|
child.stdin.write(' printf "}"; ');
|
||||||
|
child.stdin.write("}'\nexit\n");
|
||||||
|
child.waitExit();
|
||||||
|
var ret = {};
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ret = JSON.parse(child.stdout.str);
|
||||||
|
}
|
||||||
|
catch(e)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
return (ret);
|
||||||
|
}
|
||||||
|
function _systemd_GetServiceTable()
|
||||||
|
{
|
||||||
|
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("systemctl --type=service --state=running | grep .service | tr '\n' '`' | awk -F'`' '");
|
||||||
|
child.stdin.write('{');
|
||||||
|
child.stdin.write(' printf "{"; ');
|
||||||
|
child.stdin.write(' for(i=1;i<NF;++i) ');
|
||||||
|
child.stdin.write(' {');
|
||||||
|
child.stdin.write(' c=split($i,name," "); ');
|
||||||
|
child.stdin.write(' printf "%s\\"%s\\": \\"%s\\"", (i==1?"":","), name[1], name[4]; ');
|
||||||
|
child.stdin.write(' } ');
|
||||||
|
child.stdin.write(' printf "}"; ');
|
||||||
|
child.stdin.write("}'\nexit\n");
|
||||||
|
child.waitExit();
|
||||||
|
|
||||||
|
var ret = {};
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ret = JSON.parse(child.stdout.str);
|
||||||
|
}
|
||||||
|
catch (e)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
return (ret);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (process.platform == 'darwin')
|
if (process.platform == 'darwin')
|
||||||
{
|
{
|
||||||
function getOSVersion()
|
function getOSVersion()
|
||||||
@@ -1444,6 +1517,7 @@ function serviceManager()
|
|||||||
{
|
{
|
||||||
var results = [];
|
var results = [];
|
||||||
var paths = [];
|
var paths = [];
|
||||||
|
var runtable = {};
|
||||||
switch(process.platform)
|
switch(process.platform)
|
||||||
{
|
{
|
||||||
case 'linux':
|
case 'linux':
|
||||||
@@ -1454,10 +1528,12 @@ function serviceManager()
|
|||||||
break;
|
break;
|
||||||
case 'upstart':
|
case 'upstart':
|
||||||
paths.push('/etc/init');
|
paths.push('/etc/init');
|
||||||
|
runtable = _upstart_GetServiceTable();
|
||||||
break;
|
break;
|
||||||
case 'systemd':
|
case 'systemd':
|
||||||
paths.push('/lib/systemd/system');
|
paths.push('/lib/systemd/system');
|
||||||
paths.push('/usr/lib/systemd/system');
|
paths.push('/usr/lib/systemd/system');
|
||||||
|
runtable = _systemd_GetServiceTable();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
paths.push('/usr/local/mesh_daemons');
|
paths.push('/usr/local/mesh_daemons');
|
||||||
@@ -1499,6 +1575,20 @@ function serviceManager()
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
results.push(this.getService(files[j].split('.conf')[0], 'upstart'));
|
results.push(this.getService(files[j].split('.conf')[0], 'upstart'));
|
||||||
|
if(runtable[results.peek().name])
|
||||||
|
{
|
||||||
|
results.peek().state = runtable[results.peek().name].state;
|
||||||
|
if(runtable[results.peek().name].pid != '')
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
results.peek().pid = parseInt(runtable[results.peek().name].pid);
|
||||||
|
}
|
||||||
|
catch(px)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (e)
|
catch (e)
|
||||||
{
|
{
|
||||||
@@ -1511,6 +1601,7 @@ function serviceManager()
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
results.push(this.getService(files[j].split('.service')[0], 'systemd'));
|
results.push(this.getService(files[j].split('.service')[0], 'systemd'));
|
||||||
|
if (runtable[results.peek().conf.split('/').pop()]) { results.peek().state = 'RUNNING'; }
|
||||||
}
|
}
|
||||||
catch(e)
|
catch(e)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user