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"
20 static SERVICE_STATUS_HANDLE serviceStatusHandle;
21 static HANDLE hThreadEvent;
22 static HANDLE killServiceEvent;
23 static int serviceCurrentStatus;
25 /** This is used to define ChangeServiceConf2() as we can't link
26 * directly against this symbol (see below where it is used)
28 typedef BOOL (CALLBACK* SETSERVDESC)(SC_HANDLE,DWORD,LPVOID);
30 /* A commandline parameter handler for service specific commandline parameters */
31 typedef void (*CommandlineParameterHandler)(void);
33 /* Represents a commandline and its handler */
37 CommandlineParameterHandler Handler;
40 /* A function pointer for dynamic linking tricks */
41 SETSERVDESC ChangeServiceConf;
45 USEROBJECTFLAGS uoflags;
46 HWINSTA winstation = GetProcessWindowStation();
47 if (GetUserObjectInformation(winstation, UOI_FLAGS, &uoflags, sizeof(uoflags), NULL))
48 return ((uoflags.dwFlags & WSF_VISIBLE) == 0);
53 /* Kills the service by setting an event which the other thread picks up and exits */
56 SetEvent(hThreadEvent);
58 SetEvent(killServiceEvent);
61 /** The main part of inspircd runs within this thread function. This allows the service part to run
62 * seperately on its own and to be able to kill the worker thread when its time to quit.
64 DWORD WINAPI WorkerThread(LPDWORD param)
66 char modname[MAX_PATH];
67 GetModuleFileName(NULL, modname, sizeof(modname));
68 char* argv[] = { modname, "--nofork", "--debug" };
74 /* This is called when all startup is done */
75 void SetServiceRunning()
80 serviceCurrentStatus = SERVICE_RUNNING;
81 success = UpdateSCMStatus(SERVICE_RUNNING, NO_ERROR, 0, 0, 0);
84 terminateService(6, GetLastError());
90 /** Starts the worker thread above */
91 void StartServiceThread()
94 CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)WorkerThread,NULL,0,&dwd);
97 /** This function updates the status of the service in the SCM
98 * (service control manager, the services.msc applet)
100 BOOL UpdateSCMStatus (DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwServiceSpecificExitCode, DWORD dwCheckPoint, DWORD dwWaitHint)
103 SERVICE_STATUS serviceStatus;
104 serviceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
105 serviceStatus.dwCurrentState = dwCurrentState;
107 if (dwCurrentState == SERVICE_START_PENDING)
109 serviceStatus.dwControlsAccepted = 0;
113 serviceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
116 if (dwServiceSpecificExitCode == 0)
118 serviceStatus.dwWin32ExitCode = dwWin32ExitCode;
122 serviceStatus.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;
124 serviceStatus.dwServiceSpecificExitCode = dwServiceSpecificExitCode;
125 serviceStatus.dwCheckPoint = dwCheckPoint;
126 serviceStatus.dwWaitHint = dwWaitHint;
128 success = SetServiceStatus (serviceStatusHandle, &serviceStatus);
136 /** This function is called by us when the service is being shut down or when it can't be started */
137 void terminateService (int code, int wincode)
139 UpdateSCMStatus(SERVICE_STOPPED, wincode ? wincode : ERROR_SERVICE_SPECIFIC_ERROR, wincode ? 0 : code, 0, 0);
143 /* In windows we hook this to exit() */
144 void newexit(int status)
149 /* Are we running as a service? If so, trigger the service specific exit code */
150 terminateService(status, 0);
155 /** This callback is called by windows when the state of the service has been changed */
156 VOID ServiceCtrlHandler (DWORD controlCode)
160 case SERVICE_CONTROL_INTERROGATE:
162 case SERVICE_CONTROL_SHUTDOWN:
163 case SERVICE_CONTROL_STOP:
164 serviceCurrentStatus = SERVICE_STOP_PENDING;
165 UpdateSCMStatus(SERVICE_STOP_PENDING, NO_ERROR, 0, 1, 5000);
167 UpdateSCMStatus(SERVICE_STOPPED, NO_ERROR, 0, 0, 0);
172 UpdateSCMStatus(serviceCurrentStatus, NO_ERROR, 0, 0, 0);
175 /** This callback is called by windows when the service is started */
176 VOID ServiceMain(DWORD argc, LPTSTR *argv)
179 DWORD type=0, size=0;
181 serviceStatusHandle = RegisterServiceCtrlHandler("InspIRCd", (LPHANDLER_FUNCTION)ServiceCtrlHandler);
182 if (!serviceStatusHandle)
184 terminateService(1, GetLastError());
188 success = UpdateSCMStatus(SERVICE_START_PENDING, NO_ERROR, 0, 1, 1000);
191 terminateService(2, GetLastError());
195 killServiceEvent = CreateEvent(NULL, true, false, NULL);
196 hThreadEvent = CreateEvent(NULL, true, false, NULL);
198 if (!killServiceEvent || !hThreadEvent)
200 terminateService(99, GetLastError());
204 success = UpdateSCMStatus(SERVICE_START_PENDING, NO_ERROR, 0, 2, 1000);
207 terminateService(2, GetLastError());
211 StartServiceThread();
212 WaitForSingleObject (killServiceEvent, INFINITE);
215 /** Install the windows service. This requires administrator privileges. */
216 void InstallService()
218 SC_HANDLE myService, scm;
219 SERVICE_DESCRIPTION svDesc;
222 char modname[MAX_PATH];
223 GetModuleFileName(NULL, modname, sizeof(modname));
225 scm = OpenSCManager(0,0,SC_MANAGER_CREATE_SERVICE);
228 printf("Unable to open service control manager: %s\n", dlerror());
232 myService = CreateService(scm,"InspIRCd","Inspire IRC Daemon", SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
233 SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, modname, 0, 0, 0, NULL, NULL);
237 printf("Unable to create service: %s\n", dlerror());
238 CloseServiceHandle(scm);
242 // *** Set service description ***
243 // this is supported from 5.0 (win2k) onwards only, so we can't link to the definition of
244 // this function in advapi32.lib, otherwise the program will not run on windows NT 4. We
245 // must use LoadLibrary and GetProcAddress to export the function name from advapi32.dll
246 advapi32 = LoadLibrary("advapi32.dll");
249 ChangeServiceConf = (SETSERVDESC)GetProcAddress(advapi32,"ChangeServiceConfig2A");
250 if (ChangeServiceConf)
252 char desc[] = "The Inspire Internet Relay Chat Daemon hosts IRC channels and conversations.\
253 If this service is stopped, the IRC server will not run.";
254 svDesc.lpDescription = desc;
255 BOOL success = ChangeServiceConf(myService,SERVICE_CONFIG_DESCRIPTION, &svDesc);
258 printf("Unable to set service description: %s\n", dlerror());
259 CloseServiceHandle(myService);
260 CloseServiceHandle(scm);
264 FreeLibrary(advapi32);
267 printf("Service installed.\n");
268 CloseServiceHandle(myService);
269 CloseServiceHandle(scm);
272 /** Remove the windows service. This requires administrator privileges. */
275 SC_HANDLE myService, scm;
277 scm = OpenSCManager(0,0,SC_MANAGER_CREATE_SERVICE);
280 printf("Unable to open service control manager: %s\n", dlerror());
284 myService = OpenService(scm,"InspIRCd",SERVICE_ALL_ACCESS);
287 printf("Unable to open service: %s\n", dlerror());
288 CloseServiceHandle(scm);
292 if (!DeleteService(myService))
294 printf("Unable to delete service: %s\n", dlerror());
295 CloseServiceHandle(myService);
296 CloseServiceHandle(scm);
300 printf("Service removed.\n");
301 CloseServiceHandle(myService);
302 CloseServiceHandle(scm);
305 /* In windows, our main() flows through here, before calling the 'real' main, smain() in inspircd.cpp */
306 int main(int argc, char** argv)
308 /* List of parameters and handlers */
309 Commandline params[] = {
310 { "--installservice", InstallService },
311 { "--removeservice", RemoveService },
315 /* Check for parameters */
318 for (int z = 0; params[z].Switch; ++z)
320 if (!_stricmp(argv[1], params[z].Switch))
328 /* First, check if the service is installed.
329 * if it is not, or we're starting as non-administrator,
330 * just call smain() and start as normal non-service
333 SC_HANDLE myService, scm;
334 scm = OpenSCManager(0,0,SC_MANAGER_CREATE_SERVICE);
337 myService = OpenService(scm,"InspIRCd",SERVICE_ALL_ACCESS);
340 /* Service not installed or no permission to modify it */
341 CloseServiceHandle(scm);
342 return smain(argc, argv);
347 /* Not enough privileges to open the SCM */
348 return smain(argc, argv);
351 CloseServiceHandle(myService);
352 CloseServiceHandle(scm);
354 /* Check if the process is running interactively. InspIRCd does not run interactively
355 * as a service so if this is true, we just run the non-service inspircd.
358 return smain(argv, argc);
360 /* If we get here, we know the service is installed so we can start it */
362 SERVICE_TABLE_ENTRY serviceTable[] =
364 {"InspIRCd", (LPSERVICE_MAIN_FUNCTION) ServiceMain },
368 StartServiceCtrlDispatcher(serviceTable);