Substitute async services by polling when Windows is Home edition

This commit is contained in:
fermin831 2017-09-27 18:12:07 +02:00
parent a49260e38b
commit fcd54a7853
2 changed files with 42 additions and 14 deletions

View File

@ -20,12 +20,12 @@
*/
#include "pandora_module_service.h"
#include "pandora_module_list.h"
#include "../windows/pandora_wmi.h"
#include "../pandora_strutils.h"
#include "../pandora_windows_service.h"
#include <algorithm>
#include <cctype>
#include <sys/stat.h>
using namespace Pandora;
using namespace Pandora_Modules;
@ -83,8 +83,6 @@ async_run (Pandora_Module_Service *module) {
HANDLE event_log;
HANDLE event;
DWORD result;
int res;
string str_res;
BYTE buffer[BUFFER_SIZE];
EVENTLOGRECORD *record;
DWORD read;
@ -93,10 +91,17 @@ async_run (Pandora_Module_Service *module) {
bool service_event;
string prev_res;
Pandora_Module_List *modules;
bool polling;
prev_res = module->getLatestOutput ();
modules = new Pandora_Module_List ();
modules->addModule (module);
struct stat st;
// Use polling if there is not local politics and events
// do not emit logs. It is a way to check if there is a
// Home Edition Windows distribution
polling = (stat("C:\\Windows\\System32\\gpedit.msc", &st) != 0);
while (1) {
event_log = OpenEventLog (NULL, "Service Control Manager");
@ -113,6 +118,12 @@ async_run (Pandora_Module_Service *module) {
if (result != WAIT_OBJECT_0) {
CloseHandle (event);
CloseEventLog (event_log);
// If time out and polling,
// check the service status actively
if (result == WAIT_TIMEOUT && polling) {
pandoraLog("Timeout. Polling");
module->execute_async_service(prev_res, module, modules);
}
continue;
}
@ -138,17 +149,7 @@ async_run (Pandora_Module_Service *module) {
/* A start/stop action was thrown */
if (service_event) {
res = Pandora_Wmi::isServiceRunning (module->getServiceName ());
str_res = inttostr (res);
if (str_res != prev_res) {
module->setOutput (str_res);
prev_res = str_res;
Pandora_Windows_Service::getInstance ()->sendXml (modules);
}
if (res == 0 && module->isWatchdog ()) {
Pandora_Wmi::startService (module->getServiceName ());
}
module->execute_async_service(prev_res, module, modules);
}
CloseHandle (event);
CloseEventLog (event_log);
@ -156,6 +157,31 @@ async_run (Pandora_Module_Service *module) {
delete modules;
}
/*
* Execute the service async task
*/
void
Pandora_Module_Service::execute_async_service(
string &prev_res, Pandora_Module_Service *module, Pandora_Module_List *modules
) {
string str_res;
int res;
res = Pandora_Wmi::isServiceRunning (module->getServiceName ());
str_res = inttostr (res);
if (str_res != prev_res) {
module->setOutput (str_res);
prev_res = str_res;
Pandora_Windows_Service::getInstance ()->sendXml (modules);
}
if (res == 0 && module->isWatchdog ()) {
pandoraLog("Starting service");
Pandora_Wmi::startService (module->getServiceName ());
}
}
void
Pandora_Module_Service::run () {
int res;

View File

@ -23,6 +23,7 @@
#define __PANDORA_MODULE_SERVICE_H__
#include "pandora_module.h"
#include "pandora_module_list.h"
namespace Pandora_Modules {
/**
@ -41,6 +42,7 @@ namespace Pandora_Modules {
void run ();
string getServiceName () const;
bool isWatchdog () const;
void execute_async_service (string &prev_res, Pandora_Module_Service *module, Pandora_Module_List *modules);
void setWatchdog (bool watchdog);
};