1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
13 #include "inspircd_config.h"
\r
14 #include "inspircd.h"
\r
15 #include <windows.h>
\r
20 static SERVICE_STATUS_HANDLE serviceStatusHandle;
\r
21 static HANDLE hThreadEvent;
\r
22 static HANDLE killServiceEvent;
\r
23 static int serviceCurrentStatus;
\r
25 /** This is used to define ChangeServiceConf2() as we can't link
\r
26 * directly against this symbol (see below where it is used)
\r
28 typedef BOOL (CALLBACK* SETSERVDESC)(SC_HANDLE,DWORD,LPVOID);
\r
30 /* A commandline parameter handler for service specific commandline parameters */
\r
31 typedef void (*CommandlineParameterHandler)(void);
\r
33 /* Represents a commandline and its handler */
\r
37 CommandlineParameterHandler Handler;
\r
40 /* A function pointer for dynamic linking tricks */
\r
41 SETSERVDESC ChangeServiceConf;
\r
43 /* Kills the service by setting an event which the other thread picks up and exits */
\r
46 SetEvent(hThreadEvent);
\r
48 SetEvent(killServiceEvent);
\r
51 /** The main part of inspircd runs within this thread function. This allows the service part to run
\r
52 * seperately on its own and to be able to kill the worker thread when its time to quit.
\r
54 DWORD WINAPI WorkerThread(LPDWORD param)
\r
56 char modname[MAX_PATH];
\r
57 GetModuleFileName(NULL, modname, sizeof(modname));
\r
58 char* argv[] = { modname, "--nofork", "--debug" };
\r
64 /** Starts the worker thread above */
\r
65 void StartServiceThread()
\r
68 CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)WorkerThread,NULL,0,&dwd);
\r
71 /** This function updates the status of the service in the SCM
\r
72 * (service control manager, the services.msc applet)
\r
74 BOOL UpdateSCMStatus (DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwServiceSpecificExitCode, DWORD dwCheckPoint, DWORD dwWaitHint)
\r
77 SERVICE_STATUS serviceStatus;
\r
78 serviceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
\r
79 serviceStatus.dwCurrentState = dwCurrentState;
\r
81 if (dwCurrentState == SERVICE_START_PENDING)
\r
83 serviceStatus.dwControlsAccepted = 0;
\r
87 serviceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
\r
90 if (dwServiceSpecificExitCode == 0)
\r
92 serviceStatus.dwWin32ExitCode = dwWin32ExitCode;
\r
96 serviceStatus.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;
\r
98 serviceStatus.dwServiceSpecificExitCode = dwServiceSpecificExitCode;
\r
99 serviceStatus.dwCheckPoint = dwCheckPoint;
\r
100 serviceStatus.dwWaitHint = dwWaitHint;
\r
102 success = SetServiceStatus (serviceStatusHandle, &serviceStatus);
\r
110 /** This function is called by us when the service is being shut down or when it can't be started */
\r
111 void terminateService (int code, int wincode)
\r
113 UpdateSCMStatus(SERVICE_STOPPED,wincode?wincode:ERROR_SERVICE_SPECIFIC_ERROR,(wincode)?0:code,0,0);
\r
117 /** This callback is called by windows when the state of the service has been changed */
\r
118 VOID ServiceCtrlHandler (DWORD controlCode)
\r
120 switch(controlCode)
\r
122 case SERVICE_CONTROL_INTERROGATE:
\r
124 case SERVICE_CONTROL_SHUTDOWN:
\r
125 case SERVICE_CONTROL_STOP:
\r
126 serviceCurrentStatus = SERVICE_STOP_PENDING;
\r
127 UpdateSCMStatus(SERVICE_STOP_PENDING, NO_ERROR, 0, 1, 5000);
\r
129 UpdateSCMStatus(SERVICE_STOPPED, NO_ERROR, 0, 0, 0);
\r
134 UpdateSCMStatus(serviceCurrentStatus, NO_ERROR, 0, 0, 0);
\r
137 /** This callback is called by windows when the service is started */
\r
138 VOID ServiceMain(DWORD argc, LPTSTR *argv)
\r
141 DWORD type=0, size=0;
\r
143 serviceStatusHandle = RegisterServiceCtrlHandler("InspIRCd", (LPHANDLER_FUNCTION)ServiceCtrlHandler);
\r
144 if (!serviceStatusHandle)
\r
146 terminateService(1, GetLastError());
\r
150 success = UpdateSCMStatus(SERVICE_START_PENDING, NO_ERROR, 0, 1, 1000);
\r
153 terminateService(2, GetLastError());
\r
157 killServiceEvent = CreateEvent(NULL, true, false, NULL);
\r
158 hThreadEvent = CreateEvent(NULL, true, false, NULL);
\r
160 if (!killServiceEvent || !hThreadEvent)
\r
162 terminateService(99, GetLastError());
\r
166 success = UpdateSCMStatus(SERVICE_START_PENDING, NO_ERROR, 0, 2, 1000);
\r
169 terminateService(2, GetLastError());
\r
173 StartServiceThread();
\r
174 serviceCurrentStatus = SERVICE_RUNNING;
\r
175 success = UpdateSCMStatus(SERVICE_RUNNING, NO_ERROR, 0, 0, 0);
\r
178 terminateService(6, GetLastError());
\r
181 WaitForSingleObject (killServiceEvent, INFINITE);
\r
184 /** Install the windows service. This requires administrator privileges. */
\r
185 void InstallService()
\r
187 SC_HANDLE myService, scm;
\r
188 SERVICE_DESCRIPTION svDesc;
\r
189 HINSTANCE advapi32;
\r
191 char modname[MAX_PATH];
\r
192 GetModuleFileName(NULL, modname, sizeof(modname));
\r
194 scm = OpenSCManager(0,0,SC_MANAGER_CREATE_SERVICE);
\r
197 printf("Unable to open service control manager: %s\n", dlerror());
\r
201 myService = CreateService(scm,"InspIRCd","Inspire IRC Daemon", SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
\r
202 SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, modname, 0, 0, 0, NULL, NULL);
\r
206 printf("Unable to create service: %s\n", dlerror());
\r
207 CloseServiceHandle(scm);
\r
211 // *** Set service description ***
\r
212 // this is supported from 5.0 (win2k) onwards only, so we can't link to the definition of
\r
213 // this function in advapi32.lib, otherwise the program will not run on windows NT 4. We
\r
214 // must use LoadLibrary and GetProcAddress to export the function name from advapi32.dll
\r
215 advapi32 = LoadLibrary("advapi32.dll");
\r
218 ChangeServiceConf = (SETSERVDESC)GetProcAddress(advapi32,"ChangeServiceConfig2A");
\r
219 if (ChangeServiceConf)
\r
221 char desc[] = "The Inspire Internet Relay Chat Daemon hosts IRC channels and conversations.\
\r
222 If this service is stopped, the IRC server will not run.";
\r
223 svDesc.lpDescription = desc;
\r
224 BOOL success = ChangeServiceConf(myService,SERVICE_CONFIG_DESCRIPTION, &svDesc);
\r
227 printf("Unable to set service description: %s\n", dlerror());
\r
228 CloseServiceHandle(myService);
\r
229 CloseServiceHandle(scm);
\r
233 FreeLibrary(advapi32);
\r
236 printf("Service installed.\n");
\r
237 CloseServiceHandle(myService);
\r
238 CloseServiceHandle(scm);
\r
241 /** Remove the windows service. This requires administrator privileges. */
\r
242 void RemoveService()
\r
244 SC_HANDLE myService, scm;
\r
246 scm = OpenSCManager(0,0,SC_MANAGER_CREATE_SERVICE);
\r
249 printf("Unable to open service control manager: %s\n", dlerror());
\r
253 myService = OpenService(scm,"InspIRCd",SERVICE_ALL_ACCESS);
\r
256 printf("Unable to open service: %s\n", dlerror());
\r
257 CloseServiceHandle(scm);
\r
261 if (!DeleteService(myService))
\r
263 printf("Unable to delete service: %s\n", dlerror());
\r
264 CloseServiceHandle(myService);
\r
265 CloseServiceHandle(scm);
\r
269 printf("Service removed.\n");
\r
270 CloseServiceHandle(myService);
\r
271 CloseServiceHandle(scm);
\r
274 /* In windows, our main() flows through here, before calling the 'real' main, smain() in inspircd.cpp */
\r
275 int main(int argc, char** argv)
\r
277 /* List of parameters and handlers */
\r
278 Commandline params[] = {
\r
279 { "--installservice", InstallService },
\r
280 { "--removeservice", RemoveService },
\r
284 /* Check for parameters */
\r
287 for (int z = 0; params[z].Switch; ++z)
\r
289 if (!_stricmp(argv[1], params[z].Switch))
\r
291 params[z].Handler();
\r
297 /* First, check if the service is installed.
\r
298 * if it is not, or we're starting as non-administrator,
\r
299 * just call smain() and start as normal non-service
\r
302 SC_HANDLE myService, scm;
\r
303 scm = OpenSCManager(0,0,SC_MANAGER_CREATE_SERVICE);
\r
306 myService = OpenService(scm,"InspIRCd",SERVICE_ALL_ACCESS);
\r
309 /* Service not installed or no permission to modify it */
\r
310 CloseServiceHandle(scm);
\r
311 return smain(argc, argv);
\r
316 /* Not enough privileges to open the SCM */
\r
317 return smain(argc, argv);
\r
320 CloseServiceHandle(myService);
\r
321 CloseServiceHandle(scm);
\r
323 /* Check if the process is running interactively. InspIRCd does not run interactively
\r
324 * as a service so if this is true, we just run the non-service inspircd.
\r
326 USEROBJECTFLAGS uoflags;
\r
327 HWINSTA winstation = GetProcessWindowStation();
\r
328 if (GetUserObjectInformation(winstation, UOI_FLAGS, &uoflags, sizeof(uoflags), NULL))
\r
330 if (uoflags.dwFlags == WSF_VISIBLE)
\r
331 return smain(argc, argv);
\r
334 /* If we get here, we know the service is installed so we can start it */
\r
336 SERVICE_TABLE_ENTRY serviceTable[] =
\r
338 {"InspIRCd", (LPSERVICE_MAIN_FUNCTION) ServiceMain },
\r
342 StartServiceCtrlDispatcher(serviceTable);
\r