]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - win/win32service.cpp
Merge pull request #109 from Justasic/insp20
[user/henk/code/inspircd.git] / win / win32service.cpp
index 5923c1458cf2660f9ee0738343710105b0b8fb65..1d4cbad3bb6a7a85893e53e990d03113b03df036 100644 (file)
@@ -1,17 +1,25 @@
-/*       +------------------------------------+
- *       | Inspire Internet Relay Chat Daemon |
- *       +------------------------------------+
+/*
+ * InspIRCd -- Internet Relay Chat Daemon
  *
- *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
- * See: http://www.inspircd.org/wiki/index.php/Credits
+ *   Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
  *
- * This program is free but copyrighted software; see
- *         the file COPYING for details.
+ * This file is part of InspIRCd.  InspIRCd is free software: you can
+ * redistribute it and/or modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation, version 2.
  *
- * ---------------------------------------------------
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
+
+
 #include "inspircd_config.h"
 #include "inspircd.h"
+#include "exitcodes.h"
 #include <windows.h>
 #include <stdlib.h>
 #include <string.h>
@@ -27,6 +35,9 @@ static int serviceCurrentStatus;
  */
 typedef BOOL (CALLBACK* SETSERVDESC)(SC_HANDLE,DWORD,LPVOID);
 
+BOOL UpdateSCMStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwServiceSpecificExitCode, DWORD dwCheckPoint, DWORD dwWaitHint);
+void terminateService(int code, int wincode);
+
 /* A commandline parameter handler for service specific commandline parameters */
 typedef void (*CommandlineParameterHandler)(void);
 
@@ -40,7 +51,8 @@ struct Commandline
 /* A function pointer for dynamic linking tricks */
 SETSERVDESC ChangeServiceConf;
 
-bool IsAService();
+/* Returns true if this program is running as a service, false if it is running interactive */
+bool IsAService()
 {
        USEROBJECTFLAGS uoflags;
        HWINSTA winstation = GetProcessWindowStation();
@@ -65,8 +77,8 @@ DWORD WINAPI WorkerThread(LPDWORD param)
 {
        char modname[MAX_PATH];
        GetModuleFileName(NULL, modname, sizeof(modname));
-       char* argv[] = { modname, "--nofork", "--debug" };
-       smain(3, argv);
+       char* argv[] = { modname, "--nofork" };
+       smain(2, argv);
        KillService();
        return 0;
 }
@@ -78,10 +90,10 @@ void SetServiceRunning()
                return;
 
        serviceCurrentStatus = SERVICE_RUNNING;
-       success = UpdateSCMStatus(SERVICE_RUNNING, NO_ERROR, 0, 0, 0);
+       BOOL success = UpdateSCMStatus(SERVICE_RUNNING, NO_ERROR, 0, 0, 0);
        if (!success)
        {
-               terminateService(6, GetLastError());
+               terminateService(EXIT_STATUS_UPDATESCM_FAILED, GetLastError());
                return;
        }
 }
@@ -97,7 +109,7 @@ void StartServiceThread()
 /** This function updates the status of the service in the SCM
  * (service control manager, the services.msc applet)
  */
-BOOL UpdateSCMStatus (DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwServiceSpecificExitCode, DWORD dwCheckPoint, DWORD dwWaitHint)
+BOOL UpdateSCMStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwServiceSpecificExitCode, DWORD dwCheckPoint, DWORD dwWaitHint)
 {
        BOOL success;
        SERVICE_STATUS serviceStatus;
@@ -134,14 +146,14 @@ BOOL UpdateSCMStatus (DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwServi
 }
 
 /** This function is called by us when the service is being shut down or when it can't be started */
-void terminateService (int code, int wincode)
+void terminateService(int code, int wincode)
 {
        UpdateSCMStatus(SERVICE_STOPPED, wincode ? wincode : ERROR_SERVICE_SPECIFIC_ERROR, wincode ? 0 : code, 0, 0);
        return;
 }
 
-/* In windows we hook this to exit() */
-void newexit(int status)
+/* In windows we hook this to InspIRCd::Exit() */
+void SetServiceStopped(int status)
 {
        if (!IsAService())
                exit(status);
@@ -153,7 +165,7 @@ void newexit(int status)
 }
 
 /** This callback is called by windows when the state of the service has been changed */
-VOID ServiceCtrlHandler (DWORD controlCode)
+VOID ServiceCtrlHandler(DWORD controlCode)
 {
        switch(controlCode)
        {
@@ -176,19 +188,18 @@ VOID ServiceCtrlHandler (DWORD controlCode)
 VOID ServiceMain(DWORD argc, LPTSTR *argv)
 {
        BOOL success;
-       DWORD type=0, size=0;
 
        serviceStatusHandle = RegisterServiceCtrlHandler("InspIRCd", (LPHANDLER_FUNCTION)ServiceCtrlHandler);
        if (!serviceStatusHandle)
        {
-               terminateService(1, GetLastError());
+               terminateService(EXIT_STATUS_RSCH_FAILED, GetLastError());
                return;
        }
 
        success = UpdateSCMStatus(SERVICE_START_PENDING, NO_ERROR, 0, 1, 1000);
        if (!success)
        {
-               terminateService(2, GetLastError());
+               terminateService(EXIT_STATUS_UPDATESCM_FAILED, GetLastError());
                return;
        }
 
@@ -197,14 +208,14 @@ VOID ServiceMain(DWORD argc, LPTSTR *argv)
 
        if (!killServiceEvent || !hThreadEvent)
        {
-               terminateService(99, GetLastError());
+               terminateService(EXIT_STATUS_CREATE_EVENT_FAILED, GetLastError());
                return;
        }
 
        success = UpdateSCMStatus(SERVICE_START_PENDING, NO_ERROR, 0, 2, 1000);
        if (!success)
        {
-               terminateService(2, GetLastError());
+               terminateService(EXIT_STATUS_UPDATESCM_FAILED, GetLastError());
                return;
        }
 
@@ -355,7 +366,7 @@ int main(int argc, char** argv)
         * as a service so if this is true, we just run the non-service inspircd.
         */
        if (!IsAService())
-               return smain(argv, argc);
+               return smain(argc, argv);
 
        /* If we get here, we know the service is installed so we can start it */