From 7f57980c20b3f99a80def1e9b2f2e16bc46d23ef Mon Sep 17 00:00:00 2001 From: mikhail4021 Date: Sat, 22 Feb 2014 12:01:51 +0400 Subject: [PATCH] win32_service --- Queue.h | 9 ++++- Transports.cpp | 7 +++- Win32/Win32Service.cpp | 77 ++++++++++-------------------------- Win32/Win32Service.h | 3 -- i2p.cpp | 88 +++++++++++++++++++++++++----------------- 5 files changed, 86 insertions(+), 98 deletions(-) diff --git a/Queue.h b/Queue.h index 316084a9..d103c57e 100644 --- a/Queue.h +++ b/Queue.h @@ -99,6 +99,13 @@ namespace util public: MsgQueue (): m_Thread (std::bind (&MsgQueue::Run, this)) , running(1) {}; + /* + ~MsgQueue() + { + WakeUp(); + Stop(); + } + */ void Stop() { running = 0; @@ -109,7 +116,7 @@ namespace util void Run () { Msg * msg = nullptr; - while ((msg = Queue::GetNext ()) != nullptr && running) + while (((msg = Queue::GetNext ()) != nullptr) && running) { msg->Process (); delete msg; diff --git a/Transports.cpp b/Transports.cpp index 8ea6c05f..32d24dcc 100644 --- a/Transports.cpp +++ b/Transports.cpp @@ -66,8 +66,11 @@ namespace i2p m_NTCPSessions.clear (); delete m_NTCPAcceptor; - m_Timer->cancel (); - delete m_Timer; + if (m_Timer) + { + m_Timer->cancel(); + delete m_Timer; + } if (m_SSUServer) { diff --git a/Win32/Win32Service.cpp b/Win32/Win32Service.cpp index 4fd53361..9f6482a3 100644 --- a/Win32/Win32Service.cpp +++ b/Win32/Win32Service.cpp @@ -3,6 +3,7 @@ #include #include +#include "Log.h" #include "Transports.h" #include "NTCPSession.h" #include "Tunnel.h" @@ -116,13 +117,13 @@ void I2PService::Start(DWORD dwArgc, PSTR *pszArgv) } catch (DWORD dwError) { - WriteErrorLogEntry("Service Start", dwError); + LogPrint("Service Start", dwError); SetServiceStatus(SERVICE_STOPPED, dwError); } catch (...) { - WriteEventLogEntry("Service failed to start.", EVENTLOG_ERROR_TYPE); + LogPrint("Service failed to start.", EVENTLOG_ERROR_TYPE); SetServiceStatus(SERVICE_STOPPED); } @@ -131,7 +132,7 @@ void I2PService::Start(DWORD dwArgc, PSTR *pszArgv) void I2PService::OnStart(DWORD dwArgc, PSTR *pszArgv) { - WriteEventLogEntry("CppWindowsService in OnStart", + LogPrint("CppWindowsService in OnStart", EVENTLOG_INFORMATION_TYPE); i2p::util::config::OptionParser(dwArgc, pszArgv); @@ -141,16 +142,16 @@ void I2PService::OnStart(DWORD dwArgc, PSTR *pszArgv) _httpServer = new i2p::util::HTTPServer(i2p::util::config::GetArg("-httpport", 7070)); _httpServer->Start(); - WriteEventLogEntry("HTTPServer started", + LogPrint("HTTPServer started", EVENTLOG_INFORMATION_TYPE); i2p::data::netdb.Start(); - WriteEventLogEntry("NetDB started", + LogPrint("NetDB started", EVENTLOG_INFORMATION_TYPE); i2p::transports.Start(); - WriteEventLogEntry("Transports started", + LogPrint("Transports started", EVENTLOG_INFORMATION_TYPE); i2p::tunnel::tunnels.Start(); - WriteEventLogEntry("Tunnels started", + LogPrint("Tunnels started", EVENTLOG_INFORMATION_TYPE); _worker = new std::thread(std::bind(&I2PService::WorkerThread, this)); } @@ -181,13 +182,13 @@ void I2PService::Stop() } catch (DWORD dwError) { - WriteErrorLogEntry("Service Stop", dwError); + LogPrint("Service Stop", dwError); SetServiceStatus(dwOriginalState); } catch (...) { - WriteEventLogEntry("Service failed to stop.", EVENTLOG_ERROR_TYPE); + LogPrint("Service failed to stop.", EVENTLOG_ERROR_TYPE); SetServiceStatus(dwOriginalState); } @@ -197,20 +198,20 @@ void I2PService::Stop() void I2PService::OnStop() { // Log a service stop message to the Application log. - WriteEventLogEntry("CppWindowsService in OnStop", + LogPrint("CppWindowsService in OnStop", EVENTLOG_INFORMATION_TYPE); i2p::tunnel::tunnels.Stop(); - WriteEventLogEntry("Tunnels stoped", + LogPrint("Tunnels stoped", EVENTLOG_INFORMATION_TYPE); i2p::transports.Stop(); - WriteEventLogEntry("Transports stoped", + LogPrint("Transports stoped", EVENTLOG_INFORMATION_TYPE); i2p::data::netdb.Stop(); - WriteEventLogEntry("NetDB stoped", + LogPrint("NetDB stoped", EVENTLOG_INFORMATION_TYPE); _httpServer->Stop(); - WriteEventLogEntry("HTTPServer stoped", + LogPrint("HTTPServer stoped", EVENTLOG_INFORMATION_TYPE); delete _httpServer; @@ -236,13 +237,13 @@ void I2PService::Pause() } catch (DWORD dwError) { - WriteErrorLogEntry("Service Pause", dwError); + LogPrint("Service Pause", dwError); SetServiceStatus(SERVICE_RUNNING); } catch (...) { - WriteEventLogEntry("Service failed to pause.", EVENTLOG_ERROR_TYPE); + LogPrint("Service failed to pause.", EVENTLOG_ERROR_TYPE); SetServiceStatus(SERVICE_RUNNING); } @@ -266,13 +267,13 @@ void I2PService::Continue() } catch (DWORD dwError) { - WriteErrorLogEntry("Service Continue", dwError); + LogPrint("Service Continue", dwError); SetServiceStatus(SERVICE_PAUSED); } catch (...) { - WriteEventLogEntry("Service failed to resume.", EVENTLOG_ERROR_TYPE); + LogPrint("Service failed to resume.", EVENTLOG_ERROR_TYPE); SetServiceStatus(SERVICE_PAUSED); } @@ -294,11 +295,11 @@ void I2PService::Shutdown() } catch (DWORD dwError) { - WriteErrorLogEntry("Service Shutdown", dwError); + LogPrint("Service Shutdown", dwError); } catch (...) { - WriteEventLogEntry("Service failed to shut down.", EVENTLOG_ERROR_TYPE); + LogPrint("Service failed to shut down.", EVENTLOG_ERROR_TYPE); } } @@ -327,42 +328,6 @@ void I2PService::SetServiceStatus(DWORD dwCurrentState, ::SetServiceStatus(m_statusHandle, &m_status); } - -void I2PService::WriteEventLogEntry(PSTR pszMessage, WORD wType) -{ - HANDLE hEventSource = NULL; - LPCSTR lpszStrings[2] = { NULL, NULL }; - - hEventSource = RegisterEventSource(NULL, m_name); - if (hEventSource) - { - lpszStrings[0] = m_name; - lpszStrings[1] = pszMessage; - - ReportEvent(hEventSource, // Event log handle - wType, // Event type - 0, // Event category - 0, // Event identifier - NULL, // No security identifier - 2, // Size of lpszStrings array - 0, // No binary data - lpszStrings, // Array of strings - NULL // No binary data - ); - - DeregisterEventSource(hEventSource); - } -} - - -void I2PService::WriteErrorLogEntry(PSTR pszFunction, DWORD dwError) -{ - char szMessage[260]; - StringCchPrintf(szMessage, ARRAYSIZE(szMessage), - "%s failed w/err 0x%08lx", pszFunction, dwError); - WriteEventLogEntry(szMessage, EVENTLOG_ERROR_TYPE); -} - //***************************************************************************** void FreeHandles(SC_HANDLE schSCManager, SC_HANDLE schService) diff --git a/Win32/Win32Service.h b/Win32/Win32Service.h index 1fb8fa50..46635f53 100644 --- a/Win32/Win32Service.h +++ b/Win32/Win32Service.h @@ -30,9 +30,6 @@ protected: void SetServiceStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode = NO_ERROR, DWORD dwWaitHint = 0); - void WriteEventLogEntry(PSTR pszMessage, WORD wType); - void WriteErrorLogEntry(PSTR pszFunction, - DWORD dwError = GetLastError()); private: diff --git a/i2p.cpp b/i2p.cpp index d50f3d44..3339b4ee 100644 --- a/i2p.cpp +++ b/i2p.cpp @@ -82,7 +82,7 @@ void handle_signal(int sig) int main( int argc, char* argv[] ) { i2p::util::config::OptionParser(argc,argv); - isDaemon = i2p::util::config::GetArg("-daemon", 1); + isDaemon = i2p::util::config::GetArg("-daemon", 0); #ifdef _WIN32 setlocale(LC_CTYPE, ""); SetConsoleCP(1251); @@ -95,6 +95,50 @@ int main( int argc, char* argv[] ) LogPrint("data directory: ", i2p::util::filesystem::GetDataDir().string()); i2p::util::filesystem::ReadConfigFile(i2p::util::config::mapArgs, i2p::util::config::mapMultiArgs); +#ifdef _WIN32 + std::string serviceControl = i2p::util::config::GetArg("-service", "none"); + if (serviceControl == "install") + { + InstallService( + SERVICE_NAME, // Name of service + SERVICE_DISPLAY_NAME, // Name to display + SERVICE_START_TYPE, // Service start type + SERVICE_DEPENDENCIES, // Dependencies + SERVICE_ACCOUNT, // Service running account + SERVICE_PASSWORD // Password of the account + ); + return 0; + } + else if (serviceControl == "remove") + { + UninstallService(SERVICE_NAME); + return 0; + } + else if (serviceControl != "none") + { + printf(" --service=install to install the service.\n"); + printf(" --service=remove to remove the service.\n"); + return 0; + } + else if (isDaemon) + { + std::string logfile = i2p::util::filesystem::GetDataDir().string(); + logfile.append("\\debug.log"); + FILE* openResult = freopen(logfile.c_str(), "a", stdout); + if (!openResult) + { + return -17; + } + LogPrint("Service logging enabled."); + I2PService service(SERVICE_NAME); + if (!I2PService::Run(service)) + { + LogPrint("Service failed to run w/err 0x%08lx\n", GetLastError()); + } + return 0; + } +#endif + int isLogging = i2p::util::config::GetArg("-log", 0); if (isLogging == 1) { @@ -104,7 +148,13 @@ int main( int argc, char* argv[] ) #else logfile.append("\\debug.log"); #endif - freopen(logfile.c_str(),"a",stdout); + FILE* openResult = freopen(logfile.c_str(),"a",stdout); + // It seems that we need to add FLUSH() for LogPrint and call it in some important places + if (!openResult) + { + LogPrint("Can't do [freopen()]."); + return -17; + } LogPrint("Logging to file enabled."); } @@ -161,40 +211,6 @@ int main( int argc, char* argv[] ) sigaction(SIGABRT,&sa,0); sigaction(SIGTERM,&sa,0); sigaction(SIGINT,&sa,0); -#else - std::string serviceControl = i2p::util::config::GetArg("-service", "none"); - if (serviceControl == "install") - { - InstallService( - SERVICE_NAME, // Name of service - SERVICE_DISPLAY_NAME, // Name to display - SERVICE_START_TYPE, // Service start type - SERVICE_DEPENDENCIES, // Dependencies - SERVICE_ACCOUNT, // Service running account - SERVICE_PASSWORD // Password of the account - ); - return 0; - } - else if (serviceControl == "remove") - { - UninstallService(SERVICE_NAME); - return 0; - } - else if (serviceControl != "none") - { - LogPrint(" --service=install to install the service."); - LogPrint(" --service=remove to remove the service."); - return -1; - } - else if (isDaemon) - { - I2PService service(SERVICE_NAME); - if (!I2PService::Run(service)) - { - LogPrint("Service failed to run w/err 0x%08lx\n", GetLastError()); - } - return 0; - } #endif //TODO: This is an ugly workaround. fix it.