summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/anytun.cpp4
-rw-r--r--src/anytun.vcproj8
-rw-r--r--src/signalController.cpp4
-rw-r--r--src/signalController.h6
-rw-r--r--src/win32/signalHandler.hpp2
-rw-r--r--src/win32/signalServiceHandler.hpp13
-rw-r--r--src/win32/winService.cpp19
-rw-r--r--src/win32/winService.h9
8 files changed, 34 insertions, 31 deletions
diff --git a/src/anytun.cpp b/src/anytun.cpp
index 2e1e49c..48ef739 100644
--- a/src/anytun.cpp
+++ b/src/anytun.cpp
@@ -353,13 +353,13 @@ int main(int argc, char* argv[])
}
}
-int real_main(int argc, char* argv[], WinService& service)
+int real_main(int argc, char* argv[], WinService* service)
{
bool daemonized=true;
#else
int main(int argc, char* argv[])
{
- DaemonService service;
+ DaemonService* service = NULL;
bool daemonized=false;
#endif
try
diff --git a/src/anytun.vcproj b/src/anytun.vcproj
index 519c5cf..2624304 100644
--- a/src/anytun.vcproj
+++ b/src/anytun.vcproj
@@ -1346,6 +1346,14 @@
>
</File>
<File
+ RelativePath=".\win32\signalHandler.hpp"
+ >
+ </File>
+ <File
+ RelativePath=".\win32\signalServiceHandler.hpp"
+ >
+ </File>
+ <File
RelativePath=".\syncBuffer.h"
>
</File>
diff --git a/src/signalController.cpp b/src/signalController.cpp
index ca6d044..244c32f 100644
--- a/src/signalController.cpp
+++ b/src/signalController.cpp
@@ -73,12 +73,12 @@ int SigErrorHandler(const SigNum& /*sig*/, const std::string& msg)
void SignalController::init()
{
- DaemonService service;
+ DaemonService* service = NULL;
registerSignalHandler(*this, service);
handler[SIGERROR] = boost::bind(SigErrorHandler, _1, _2);
}
-void SignalController::init(DaemonService& service)
+void SignalController::init(DaemonService* service)
{
registerSignalHandler(*this, service);
handler[SIGERROR] = boost::bind(SigErrorHandler, _1, _2);
diff --git a/src/signalController.h b/src/signalController.h
index f872888..c19f208 100644
--- a/src/signalController.h
+++ b/src/signalController.h
@@ -40,7 +40,7 @@
#include "threadUtils.hpp"
#ifdef WIN_SERVICE
#include "win32/winService.h"
-typedef WinService DaemonService;
+typedef class WinService DaemonService;
#else
typedef int DaemonService;
#endif
@@ -58,7 +58,7 @@ public:
static SignalController& instance();
void init();
- void init(DaemonService& service);
+ void init(DaemonService* service);
int run();
void inject(int sig, const std::string& msg = "");
@@ -88,7 +88,7 @@ private:
typedef std::map<CallbackType, ServiceCallback> CallbackMap;
CallbackMap callbacks;
- friend void registerSignalHandler(SignalController& ctrl, DaemonService& service);
+ friend void registerSignalHandler(SignalController& ctrl, DaemonService* service);
};
extern SignalController& gSignalController;
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