From 9c363297c80c00d5b940b8614fb1b4e375b45fe6 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Wed, 20 Jan 2010 08:19:16 +0000 Subject: winService works now together with signalController --- src/win32/signalHandler.hpp | 2 +- src/win32/signalServiceHandler.hpp | 13 ++++++++----- src/win32/winService.cpp | 19 ++++++------------- src/win32/winService.h | 9 ++++----- 4 files changed, 19 insertions(+), 24 deletions(-) (limited to 'src/win32') diff --git a/src/win32/signalHandler.hpp b/src/win32/signalHandler.hpp index a961621..aeafcb4 100644 --- a/src/win32/signalHandler.hpp +++ b/src/win32/signalHandler.hpp @@ -71,7 +71,7 @@ bool handleSignal(DWORD ctrlType) return true; } -void registerSignalHandler(SignalController& ctrl, DaemonService& /*service*/) +void registerSignalHandler(SignalController& ctrl, DaemonService* /*service*/) { if(!SetConsoleCtrlHandler((PHANDLER_ROUTINE)handleSignal, true)) AnytunError::throwErr() << "Error on SetConsoleCtrlhandler: " << AnytunErrno(GetLastError()); diff --git a/src/win32/signalServiceHandler.hpp b/src/win32/signalServiceHandler.hpp index 3c6be7e..67d0774 100644 --- a/src/win32/signalServiceHandler.hpp +++ b/src/win32/signalServiceHandler.hpp @@ -35,13 +35,16 @@ #include "winService.h" -void registerSignalHandler(SignalController& ctrl, WinService& service) +void registerSignalHandler(SignalController& ctrl, WinService* service) { - ctrl.handler[SERVICE_CONTROL_STOP] = boost::bind(WinService::handleCtrlSignal, &service, _1, _2); - ctrl.handler[SERVICE_CONTROL_INTERROGATE] = boost::bind(WinService::handleCtrlSignal, &service, _1, _2); + if(!service) + return; - ctrl.callbacks.insert(SignalController::CallbackMap::value_type(CALLB_RUNNING, boost::bind(&WinService::reportStatus, &service, SERVICE_RUNNING, NO_ERROR))); - ctrl.callbacks.insert(SignalController::CallbackMap::value_type(CALLB_STOPPING, boost::bind(&WinService::reportStatus, &service, SERVICE_STOP_PENDING, NO_ERROR))); + ctrl.handler[SERVICE_CONTROL_STOP] = boost::bind(&WinService::handleCtrlSignal, service, _1, _2); + ctrl.handler[SERVICE_CONTROL_INTERROGATE] = boost::bind(&WinService::handleCtrlSignal, service, _1, _2); + + ctrl.callbacks.insert(SignalController::CallbackMap::value_type(CALLB_RUNNING, boost::bind(&WinService::reportStatus, service, SERVICE_RUNNING, NO_ERROR))); + ctrl.callbacks.insert(SignalController::CallbackMap::value_type(CALLB_STOPPING, boost::bind(&WinService::reportStatus, service, SERVICE_STOP_PENDING, NO_ERROR))); } #endif diff --git a/src/win32/winService.cpp b/src/win32/winService.cpp index feb1aab..4e324b2 100644 --- a/src/win32/winService.cpp +++ b/src/win32/winService.cpp @@ -105,7 +105,7 @@ void WinService::start() AnytunError::throwErr() << "Error on StartServiceCtrlDispatcher: " << AnytunErrno(GetLastError()); } -int real_main(int argc, char* argv[], WinService& service); +int real_main(int argc, char* argv[], WinService* service); VOID WINAPI WinService::main(DWORD dwArgc, LPTSTR *lpszArgv) { @@ -120,35 +120,28 @@ VOID WINAPI WinService::main(DWORD dwArgc, LPTSTR *lpszArgv) service.status_.dwServiceSpecificExitCode = 0; service.reportStatus(SERVICE_START_PENDING, NO_ERROR); - service.stop_event_ = CreateEvent(NULL, true, false, NULL); - if(!service.stop_event_) { - cLog.msg(Log::PRIO_ERROR) << "WinService Error on CreateEvent: " << AnytunErrno(GetLastError()); - service.reportStatus(SERVICE_STOPPED, -1); - return; - } - - real_main(dwArgc, lpszArgv, service); + real_main(dwArgc, lpszArgv, &service); service.reportStatus(SERVICE_STOPPED, NO_ERROR); } VOID WINAPI WinService::ctrlHandler(DWORD dwCtrl) { - gSignalController.inject(ctrlType); - return true; + gSignalController.inject(dwCtrl); } -int handleCtrlSignal(const SigNum& sig, const std::string& msg) +int WinService::handleCtrlSignal(const SigNum& sig, const std::string& msg) { switch(sig) { case SERVICE_CONTROL_STOP: { reportStatus(SERVICE_STOP_PENDING, NO_ERROR); + cLog.msg(Log::PRIO_NOTICE) << "received service stop signal, exitting"; return 1; } case SERVICE_CONTROL_INTERROGATE: break; default: break; } - reportStatus(gWinService.status_.dwCurrentState, NO_ERROR); + reportStatus(status_.dwCurrentState, NO_ERROR); return 0; } diff --git a/src/win32/winService.h b/src/win32/winService.h index e2bab90..4cb485a 100644 --- a/src/win32/winService.h +++ b/src/win32/winService.h @@ -36,6 +36,7 @@ #ifdef WIN_SERVICE #include "../threadUtils.hpp" +#include "../signalController.h" class WinService { @@ -49,11 +50,11 @@ public: static VOID WINAPI ctrlHandler(DWORD dwCtrl); void reportStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode); - int handleCtrlSignal(const SigNum& sig, const std::string& msg); + int handleCtrlSignal(const int& sig, const std::string& msg); private: - WinService() : started_(false) {}; - ~WinService(); + WinService() {}; + ~WinService() {}; WinService(const WinService &w); void operator=(const WinService &w); @@ -61,8 +62,6 @@ private: SERVICE_STATUS_HANDLE status_handle_; }; -extern WinService& gWinService; - #endif #endif -- cgit v1.2.3