mirror of
https://github.com/Ylianst/MeshCentral.git
synced 2025-03-09 15:40:18 +00:00
New MeshCmd, fixes MeshAgent registry values
This commit is contained in:
parent
8287997b2d
commit
835e03225d
10 changed files with 142 additions and 279 deletions
|
@ -1,22 +1,20 @@
|
|||
|
||||
var SERVICE_WIN32 = 0x00000010 | 0x00000020;
|
||||
var SERVICE_STATE = { STOPPED: 0x00000001, SERVICE_START_PENDING: 0x00000002, SERVICE_STOP_PENDING: 0x00000003, RUNNING: 0x00000004 };
|
||||
var SERVICE_ACCEPT = {SERVICE_ACCEPT_STOP: 0x00000001, SERVICE_ACCEPT_SHUTDOWN: 0x00000004};
|
||||
var SERVICE_CONTROL = {SERVICE_CONTROL_SHUTDOWN: 0x00000005, SERVICE_CONTROL_STOP: 0x00000001};
|
||||
var SERVICE_ACCEPT = { SERVICE_ACCEPT_STOP: 0x00000001, SERVICE_ACCEPT_SHUTDOWN: 0x00000004 };
|
||||
var SERVICE_CONTROL = { SERVICE_CONTROL_SHUTDOWN: 0x00000005, SERVICE_CONTROL_STOP: 0x00000001 };
|
||||
var NO_ERROR = 0;
|
||||
|
||||
var serviceManager = require('serviceManager');
|
||||
|
||||
function serviceHost(serviceName)
|
||||
{
|
||||
function serviceHost(serviceName) {
|
||||
this._ObjectID = 'serviceHost';
|
||||
var emitterUtils = require('events').inherits(this);
|
||||
emitterUtils.createEvent('serviceStart');
|
||||
emitterUtils.createEvent('serviceStop');
|
||||
emitterUtils.createEvent('normalStart');
|
||||
|
||||
if (process.platform == 'win32')
|
||||
{
|
||||
if (process.platform == 'win32') {
|
||||
this.GM = require('_GenericMarshal');
|
||||
this.Advapi = this.GM.CreateNativeProxy('Advapi32.dll');
|
||||
this.Advapi.CreateMethod({ method: 'StartServiceCtrlDispatcherA', threadDispatch: 1 });
|
||||
|
@ -33,8 +31,7 @@ function serviceHost(serviceName)
|
|||
this._ServiceMain = this.GM.GetGenericGlobalCallback(2);
|
||||
this._ServiceMain.Parent = this;
|
||||
this._ServiceMain.GM = this.GM;
|
||||
this._ServiceMain.on('GlobalCallback', function onGlobalCallback(argc, argv)
|
||||
{
|
||||
this._ServiceMain.on('GlobalCallback', function onGlobalCallback(argc, argv) {
|
||||
//ToDo: Check to make sure this is for us
|
||||
|
||||
this.Parent._ServiceStatus = this.GM.CreateVariable(28);
|
||||
|
@ -52,8 +49,7 @@ function serviceHost(serviceName)
|
|||
this.Parent._ServiceStatus.toBuffer().writeUInt32LE(SERVICE_WIN32);
|
||||
this.Parent._ServiceStatus.toBuffer().writeUInt32LE(SERVICE_STATE.SERVICE_STOPPED, 4);
|
||||
this.Parent._ServiceStatusHandle = this.Parent.Advapi.RegisterServiceCtrlHandlerA(this.Parent._ServiceName, this.Parent._ServiceControlHandler);
|
||||
if(this.Parent._ServiceStatusHandle.Val == 0)
|
||||
{
|
||||
if (this.Parent._ServiceStatusHandle.Val == 0) {
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
|
@ -67,8 +63,7 @@ function serviceHost(serviceName)
|
|||
this.Parent.Advapi.SetServiceStatus(this.Parent._ServiceStatusHandle, this.Parent._ServiceStatus);
|
||||
|
||||
this.Parent.Ole32.CoInitializeEx(0, 2);
|
||||
this.Parent.on('~', function OnServiceHostFinalizer()
|
||||
{
|
||||
this.Parent.on('~', function OnServiceHostFinalizer() {
|
||||
var GM = require('_GenericMarshal');
|
||||
var Advapi = GM.CreateNativeProxy('Advapi32.dll');
|
||||
Advapi.CreateMethod('SetServiceStatus');
|
||||
|
@ -92,10 +87,8 @@ function serviceHost(serviceName)
|
|||
this._ServiceControlHandler = this.GM.GetGenericGlobalCallback(1);
|
||||
this._ServiceControlHandler.Parent = this;
|
||||
this._ServiceControlHandler.GM = this.GM;
|
||||
this._ServiceControlHandler.on('GlobalCallback', function onServiceControlHandler(code)
|
||||
{
|
||||
switch (code.Val)
|
||||
{
|
||||
this._ServiceControlHandler.on('GlobalCallback', function onServiceControlHandler(code) {
|
||||
switch (code.Val) {
|
||||
case SERVICE_CONTROL.SERVICE_CONTROL_SHUTDOWN:
|
||||
case SERVICE_CONTROL.SERVICE_CONTROL_STOP:
|
||||
this.Parent.emit('serviceStop');
|
||||
|
@ -113,54 +106,42 @@ function serviceHost(serviceName)
|
|||
{
|
||||
throw ('Must specify either ServiceName or Options');
|
||||
}
|
||||
if (!this._ServiceOptions.servicePath)
|
||||
{
|
||||
if (!this._ServiceOptions.servicePath) {
|
||||
this._ServiceOptions.servicePath = process.execPath;
|
||||
}
|
||||
|
||||
this.run = function run()
|
||||
{
|
||||
|
||||
this.run = function run() {
|
||||
var serviceOperation = 0;
|
||||
|
||||
for(var i = 0; i<process.argv.length; ++i)
|
||||
{
|
||||
switch(process.argv[i])
|
||||
{
|
||||
case '-install':
|
||||
for (var i = 0; i < process.argv.length; ++i) {
|
||||
switch (process.argv[i].toLowerCase()) {
|
||||
case 'install':
|
||||
if (!this._svcManager) { this._svcManager = new serviceManager(); }
|
||||
try
|
||||
{
|
||||
try {
|
||||
this._svcManager.installService(this._ServiceOptions);
|
||||
}
|
||||
catch(e)
|
||||
{
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
process.exit();
|
||||
}
|
||||
if (process.platform == 'win32')
|
||||
{
|
||||
if (process.platform == 'win32') {
|
||||
// Only do this on Windows, becuase Linux is async... It'll complete later
|
||||
console.log(this._ServiceOptions.name + ' installed');
|
||||
console.log(this._ServiceOptions.name + ' installed.');
|
||||
process.exit();
|
||||
}
|
||||
i = process.argv.length;
|
||||
serviceOperation = 1;
|
||||
break;
|
||||
case '-uninstall':
|
||||
case 'uninstall':
|
||||
if (!this._svcManager) { this._svcManager = new serviceManager(); }
|
||||
try
|
||||
{
|
||||
try {
|
||||
this._svcManager.uninstallService(this._ServiceOptions);
|
||||
}
|
||||
catch(e)
|
||||
{
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
process.exit();
|
||||
}
|
||||
if (process.platform == 'win32')
|
||||
{
|
||||
if (process.platform == 'win32') {
|
||||
// Only do this on Windows, becuase Linux is async... It'll complete later
|
||||
console.log(this._ServiceOptions.name + ' uninstalled');
|
||||
console.log(this._ServiceOptions.name + ' uninstalled.');
|
||||
process.exit();
|
||||
}
|
||||
i = process.argv.length;
|
||||
|
@ -170,55 +151,46 @@ function serviceHost(serviceName)
|
|||
case '-d':
|
||||
if (!this._svcManager) { this._svcManager = new serviceManager(); }
|
||||
this._svcManager.getService(this._ServiceOptions.name).start();
|
||||
console.log(this._ServiceOptions.name + ' starting...');
|
||||
console.log(this._ServiceOptions.name + ' starting.');
|
||||
process.exit();
|
||||
break;
|
||||
case 'stop':
|
||||
case '-s':
|
||||
if (!this._svcManager) { this._svcManager = new serviceManager(); }
|
||||
this._svcManager.getService(this._ServiceOptions.name).stop();
|
||||
console.log(this._ServiceOptions.name + ' stopping...');
|
||||
console.log(this._ServiceOptions.name + ' stopping.');
|
||||
process.exit();
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if (process.platform == 'win32')
|
||||
{
|
||||
if (process.platform == 'win32') {
|
||||
var serviceTable = this.GM.CreateVariable(4 * this.GM.PointerSize);
|
||||
this._ServiceName.pointerBuffer().copy(serviceTable.toBuffer());
|
||||
this._ServiceMain.pointerBuffer().copy(serviceTable.toBuffer(), this.GM.PointerSize);
|
||||
this._sscd = this.Advapi.StartServiceCtrlDispatcherA(serviceTable);
|
||||
this._sscd.parent = this;
|
||||
this._sscd.on('done', function OnStartServiceCtrlDispatcherA(retVal) {
|
||||
if (retVal.Val == 0)
|
||||
{
|
||||
this.parent.emit('normalStart');
|
||||
}
|
||||
if (retVal.Val == 0) { this.parent.emit('normalStart'); }
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
var moduleName = this._ServiceOptions ? this._ServiceOptions.name : process.execPath.substring(1 + process.execPath.lastIndexOf('/'));
|
||||
|
||||
for (var i = 0; i < process.argv.length; ++i)
|
||||
{
|
||||
switch(process.argv[i])
|
||||
{
|
||||
for (var i = 0; i < process.argv.length; ++i) {
|
||||
switch (process.argv[i].toLowerCase()) {
|
||||
case 'start':
|
||||
case '-d':
|
||||
var child = require('child_process').execFile(process.execPath, [moduleName], { type: require('child_process').SpawnTypes.DETACHED });
|
||||
var pstream = null;
|
||||
try
|
||||
{
|
||||
try {
|
||||
pstream = require('fs').createWriteStream('/var/run/' + moduleName + '.pid', { flags: 'w' });
|
||||
}
|
||||
catch(e)
|
||||
{
|
||||
catch (e) {
|
||||
}
|
||||
if (pstream == null)
|
||||
{
|
||||
if (pstream == null) {
|
||||
pstream = require('fs').createWriteStream('.' + moduleName + '.pid', { flags: 'w' });
|
||||
}
|
||||
pstream.end(child.pid.toString());
|
||||
|
@ -229,33 +201,26 @@ function serviceHost(serviceName)
|
|||
case 'stop':
|
||||
case '-s':
|
||||
var pid = null;
|
||||
try
|
||||
{
|
||||
try {
|
||||
pid = parseInt(require('fs').readFileSync('/var/run/' + moduleName + '.pid', { flags: 'r' }));
|
||||
require('fs').unlinkSync('/var/run/' + moduleName + '.pid');
|
||||
}
|
||||
catch(e)
|
||||
{
|
||||
catch (e) {
|
||||
}
|
||||
if(pid == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (pid == null) {
|
||||
try {
|
||||
pid = parseInt(require('fs').readFileSync('.' + moduleName + '.pid', { flags: 'r' }));
|
||||
require('fs').unlinkSync('.' + moduleName + '.pid');
|
||||
}
|
||||
catch(e)
|
||||
{
|
||||
catch (e) {
|
||||
}
|
||||
}
|
||||
|
||||
if(pid)
|
||||
{
|
||||
if (pid) {
|
||||
process.kill(pid);
|
||||
console.log(moduleName + ' stopped');
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
console.log(moduleName + ' not running');
|
||||
}
|
||||
process.exit();
|
||||
|
@ -263,56 +228,30 @@ function serviceHost(serviceName)
|
|||
}
|
||||
}
|
||||
|
||||
if(serviceOperation == 0)
|
||||
{
|
||||
if (serviceOperation == 0) {
|
||||
// This is non-windows, so we need to check how this binary was started to determine if this was a service start
|
||||
|
||||
// Start by checking if we were started with start/stop
|
||||
var pid = null;
|
||||
try
|
||||
{
|
||||
pid = parseInt(require('fs').readFileSync('/var/run/' + moduleName + '.pid', { flags: 'r' }));
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
}
|
||||
if (pid == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
pid = parseInt(require('fs').readFileSync('.' + moduleName + '.pid', { flags: 'r' }));
|
||||
}
|
||||
catch (e)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
if (pid != null && pid == process.pid)
|
||||
{
|
||||
try { pid = parseInt(require('fs').readFileSync('/var/run/' + moduleName + '.pid', { flags: 'r' })); } catch (e) { }
|
||||
if (pid == null) { try { pid = parseInt(require('fs').readFileSync('.' + moduleName + '.pid', { flags: 'r' })); } catch (e) { } }
|
||||
|
||||
if (pid != null && pid == process.pid) {
|
||||
this.emit('serviceStart');
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// Now we need to check if we were started with systemd
|
||||
if (require('processManager').getProcessInfo(1).Name == 'systemd')
|
||||
{
|
||||
if (require('processManager').getProcessInfo(1).Name == 'systemd') {
|
||||
this._checkpid = require('child_process').execFile('/bin/sh', ['sh'], { type: require('child_process').SpawnTypes.TERM });
|
||||
this._checkpid.result = '';
|
||||
this._checkpid.parent = this;
|
||||
this._checkpid.on('exit', function onCheckPIDExit()
|
||||
{
|
||||
this._checkpid.on('exit', function onCheckPIDExit() {
|
||||
var lines = this.result.split('\r\n');
|
||||
for (i in lines)
|
||||
{
|
||||
if(lines[i].startsWith(' Main PID:'))
|
||||
{
|
||||
for (i in lines) {
|
||||
if (lines[i].startsWith(' Main PID:')) {
|
||||
var tokens = lines[i].split(' ');
|
||||
if (parseInt(tokens[3]) == process.pid)
|
||||
{
|
||||
if (parseInt(tokens[3]) == process.pid) {
|
||||
this.parent.emit('serviceStart');
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
this.parent.emit('normalStart');
|
||||
}
|
||||
delete this.parent._checkpid;
|
||||
|
@ -326,8 +265,7 @@ function serviceHost(serviceName)
|
|||
this._checkpid.stdin.write("systemctl status " + moduleName + " | grep 'Main PID:'\n");
|
||||
this._checkpid.stdin.write('exit\n');
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
// This isn't even a systemd platform, so this couldn't have been a service start
|
||||
this.emit('normalStart');
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue