diff --git a/src/mangosd/Main.cpp b/src/mangosd/Main.cpp index 4782ccbd7..fee8ac557 100644 --- a/src/mangosd/Main.cpp +++ b/src/mangosd/Main.cpp @@ -137,26 +137,7 @@ extern int main(int argc, char **argv) } } -#ifndef WIN32 // posix daemon commands need apply before config read - switch (serviceDaemonMode) - { - case 'r': - startDaemon(); - break; - case 's': - stopDaemon(); - break; - } -#endif - - if (!sConfig.SetSource(cfg_file)) - { - sLog.outError("Could not find configuration file %s.", cfg_file); - Log::WaitBeforeContinueIfNeed(); - return 1; - } - -#ifdef WIN32 // windows service command need execute after config read +#ifdef WIN32 // windows service command need execute before config read switch (serviceDaemonMode) { case 'i': @@ -173,6 +154,25 @@ extern int main(int argc, char **argv) } #endif + if (!sConfig.SetSource(cfg_file)) + { + sLog.outError("Could not find configuration file %s.", cfg_file); + Log::WaitBeforeContinueIfNeed(); + return 1; + } + +#ifndef WIN32 // posix daemon commands need apply after config read + switch (serviceDaemonMode) + { + case 'r': + startDaemon(); + break; + case 's': + stopDaemon(); + break; + } +#endif + sLog.outString( "%s [world-daemon]", _FULLVERSION(REVISION_DATE,REVISION_TIME,REVISION_NR,REVISION_ID) ); sLog.outString( " to stop.\n\n" ); diff --git a/src/realmd/Main.cpp b/src/realmd/Main.cpp index 2158fe553..f22a2f682 100644 --- a/src/realmd/Main.cpp +++ b/src/realmd/Main.cpp @@ -147,26 +147,7 @@ extern int main(int argc, char **argv) } } -#ifndef WIN32 // posix daemon commands need apply before config read - switch (serviceDaemonMode) - { - case 'r': - startDaemon(); - break; - case 's': - stopDaemon(); - break; - } -#endif - - if (!sConfig.SetSource(cfg_file)) - { - sLog.outError("Could not find configuration file %s.", cfg_file); - Log::WaitBeforeContinueIfNeed(); - return 1; - } - -#ifdef WIN32 // windows service command need execute after config read +#ifdef WIN32 // windows service command need execute before config read switch (serviceDaemonMode) { case 'i': @@ -183,6 +164,25 @@ extern int main(int argc, char **argv) } #endif + if (!sConfig.SetSource(cfg_file)) + { + sLog.outError("Could not find configuration file %s.", cfg_file); + Log::WaitBeforeContinueIfNeed(); + return 1; + } + +#ifndef WIN32 // posix daemon commands need apply after config read + switch (serviceDaemonMode) + { + case 'r': + startDaemon(); + break; + case 's': + stopDaemon(); + break; + } +#endif + sLog.Initialize(); sLog.outString( "%s [realm-daemon]", _FULLVERSION(REVISION_DATE,REVISION_TIME,REVISION_NR,REVISION_ID) ); diff --git a/src/shared/ServiceWin32.cpp b/src/shared/ServiceWin32.cpp index 0725958ed..62e2c0e45 100644 --- a/src/shared/ServiceWin32.cpp +++ b/src/shared/ServiceWin32.cpp @@ -51,71 +51,84 @@ bool WinServiceInstall() HMODULE advapi32; SC_HANDLE serviceControlManager = OpenSCManager(0, 0, SC_MANAGER_CREATE_SERVICE); - if (serviceControlManager) + if (!serviceControlManager) { - char path[_MAX_PATH + 10]; - if (GetModuleFileName( 0, path, sizeof(path)/sizeof(path[0]) ) > 0) - { - SC_HANDLE service; - std::strcat(path, " -s run"); - service = CreateService(serviceControlManager, - serviceName, // name of service - serviceLongName, // service name to display - SERVICE_ALL_ACCESS, // desired access - // service type - SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS, - SERVICE_AUTO_START, // start type - SERVICE_ERROR_IGNORE, // error control type - path, // service's binary - 0, // no load ordering group - 0, // no tag identifier - 0, // no dependencies - 0, // LocalSystem account - 0); // no password - if (service) - { - advapi32 = GetModuleHandle("ADVAPI32.DLL"); - if(!advapi32) - { - CloseServiceHandle(service); - CloseServiceHandle(serviceControlManager); - return false; - } - - ChangeService_Config2 = (CSD_T) GetProcAddress(advapi32, "ChangeServiceConfig2A"); - if (!ChangeService_Config2) - { - CloseServiceHandle(service); - CloseServiceHandle(serviceControlManager); - return false; - } - - SERVICE_DESCRIPTION sdBuf; - sdBuf.lpDescription = serviceDescription; - ChangeService_Config2( - service, // handle to service - SERVICE_CONFIG_DESCRIPTION, // change: description - &sdBuf); // new data - - SC_ACTION _action[1]; - _action[0].Type = SC_ACTION_RESTART; - _action[0].Delay = 10000; - SERVICE_FAILURE_ACTIONS sfa; - ZeroMemory(&sfa, sizeof(SERVICE_FAILURE_ACTIONS)); - sfa.lpsaActions = _action; - sfa.cActions = 1; - sfa.dwResetPeriod =INFINITE; - ChangeService_Config2( - service, // handle to service - SERVICE_CONFIG_FAILURE_ACTIONS, // information level - &sfa); // new data - - CloseServiceHandle(service); - - } - } - CloseServiceHandle(serviceControlManager); + sLog.outError("SERVICE: No access to service control manager."); + return false; } + + char path[_MAX_PATH + 10]; + if (!GetModuleFileName( 0, path, sizeof(path)/sizeof(path[0]) ) > 0) + { + CloseServiceHandle(serviceControlManager); + sLog.outError("SERVICE: Can't get service binary filename."); + return false; + } + + std::strcat(path, " -s run"); + + SC_HANDLE service = CreateService(serviceControlManager, + serviceName, // name of service + serviceLongName, // service name to display + SERVICE_ALL_ACCESS, // desired access + // service type + SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS, + SERVICE_AUTO_START, // start type + SERVICE_ERROR_IGNORE, // error control type + path, // service's binary + 0, // no load ordering group + 0, // no tag identifier + 0, // no dependencies + 0, // LocalSystem account + 0); // no password + + if (!service) + { + CloseServiceHandle(serviceControlManager); + sLog.outError("SERVICE: Can't register service for: %s", path); + return false; + } + + advapi32 = GetModuleHandle("ADVAPI32.DLL"); + if(!advapi32) + { + sLog.outError("SERVICE: Can't access ADVAPI32.DLL"); + CloseServiceHandle(service); + CloseServiceHandle(serviceControlManager); + return false; + } + + ChangeService_Config2 = (CSD_T) GetProcAddress(advapi32, "ChangeServiceConfig2A"); + if (!ChangeService_Config2) + { + sLog.outError("SERVICE: Can't access ChangeServiceConfig2A from ADVAPI32.DLL"); + CloseServiceHandle(service); + CloseServiceHandle(serviceControlManager); + return false; + } + + SERVICE_DESCRIPTION sdBuf; + sdBuf.lpDescription = serviceDescription; + ChangeService_Config2( + service, // handle to service + SERVICE_CONFIG_DESCRIPTION, // change: description + &sdBuf); // new data + + SC_ACTION _action[1]; + _action[0].Type = SC_ACTION_RESTART; + _action[0].Delay = 10000; + SERVICE_FAILURE_ACTIONS sfa; + ZeroMemory(&sfa, sizeof(SERVICE_FAILURE_ACTIONS)); + sfa.lpsaActions = _action; + sfa.cActions = 1; + sfa.dwResetPeriod =INFINITE; + ChangeService_Config2( + service, // handle to service + SERVICE_CONFIG_FAILURE_ACTIONS, // information level + &sfa); // new data + + CloseServiceHandle(service); + CloseServiceHandle(serviceControlManager); return true; } @@ -123,23 +136,31 @@ bool WinServiceUninstall() { SC_HANDLE serviceControlManager = OpenSCManager(0, 0, SC_MANAGER_CONNECT); - if (serviceControlManager) + if (!serviceControlManager) { - SC_HANDLE service = OpenService(serviceControlManager, - serviceName, SERVICE_QUERY_STATUS | DELETE); - if (service) - { - SERVICE_STATUS serviceStatus2; - if (QueryServiceStatus(service, &serviceStatus2)) - { - if (serviceStatus2.dwCurrentState == SERVICE_STOPPED) - DeleteService(service); - } - CloseServiceHandle(service); - } - - CloseServiceHandle(serviceControlManager); + sLog.outError("SERVICE: No access to service control manager."); + return false; } + + SC_HANDLE service = OpenService(serviceControlManager, + serviceName, SERVICE_QUERY_STATUS | DELETE); + + if (!service) + { + CloseServiceHandle(serviceControlManager); + sLog.outError("SERVICE: Service not found: %s", serviceName); + return false; + } + + SERVICE_STATUS serviceStatus2; + if (QueryServiceStatus(service, &serviceStatus2)) + { + if (serviceStatus2.dwCurrentState == SERVICE_STOPPED) + DeleteService(service); + } + + CloseServiceHandle(service); + CloseServiceHandle(serviceControlManager); return true; } diff --git a/src/shared/revision_nr.h b/src/shared/revision_nr.h index e5fc6c8c2..7d1ef7dc2 100644 --- a/src/shared/revision_nr.h +++ b/src/shared/revision_nr.h @@ -1,4 +1,4 @@ #ifndef __REVISION_NR_H__ #define __REVISION_NR_H__ - #define REVISION_NR "11519" + #define REVISION_NR "11520" #endif // __REVISION_NR_H__