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 * ---------------------------------------------------
14 #include <windows.h>
\r
19 extern int smain(int argc, char** argv);
\r
20 extern const char* dlerror();
\r
22 static SERVICE_STATUS_HANDLE serviceStatusHandle;
\r
23 static HANDLE hThreadEvent;
\r
24 static HANDLE killServiceEvent;
\r
25 static int serviceCurrentStatus;
\r
27 // This is used to define ChangeServiceConf2() as we can't link
\r
28 // directly against this symbol (see below where it is used)
\r
29 typedef BOOL (CALLBACK* SETSERVDESC)(SC_HANDLE,DWORD,LPVOID);
\r
31 /* A commandline parameter handler for service specific commandline parameters */
\r
32 typedef void (*CommandlineParameterHandler)(void);
\r
34 /* Represents a commandline and its handler */
\r
38 CommandlineParameterHandler Handler;
\r
42 SETSERVDESC ChangeServiceConf; // A function pointer for dynamic linking tricks
\r
47 /* FIXME: This should set a flag in the mainloop for shutting down */
\r
48 SetEvent(hThreadEvent);
\r
50 SetEvent(killServiceEvent);
\r
53 DWORD WINAPI WorkerThread(LPDWORD param)
\r
55 // *** REAL MAIN HERE ***
\r
56 char modname[MAX_PATH];
\r
57 GetModuleFileName(NULL, modname, sizeof(modname));
\r
58 char* argv[] = { modname, "--nofork", "--debug" };
\r
64 void StartServiceThread()
\r
67 CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)WorkerThread,NULL,0,&dwd);
\r
71 BOOL UpdateSCMStatus (DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwServiceSpecificExitCode, DWORD dwCheckPoint, DWORD dwWaitHint)
\r
74 SERVICE_STATUS serviceStatus;
\r
75 serviceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
\r
76 serviceStatus.dwCurrentState = dwCurrentState;
\r
78 if (dwCurrentState == SERVICE_START_PENDING)
\r
80 serviceStatus.dwControlsAccepted = 0;
\r
84 serviceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
\r
87 if (dwServiceSpecificExitCode == 0)
\r
89 serviceStatus.dwWin32ExitCode = dwWin32ExitCode;
\r
93 serviceStatus.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;
\r
95 serviceStatus.dwServiceSpecificExitCode = dwServiceSpecificExitCode;
\r
96 serviceStatus.dwCheckPoint = dwCheckPoint;
\r
97 serviceStatus.dwWaitHint = dwWaitHint;
\r
99 success = SetServiceStatus (serviceStatusHandle, &serviceStatus);
\r
108 void terminateService (int code, int wincode)
\r
110 UpdateSCMStatus(SERVICE_STOPPED,wincode?wincode:ERROR_SERVICE_SPECIFIC_ERROR,(wincode)?0:code,0,0);
\r
115 VOID ServiceCtrlHandler (DWORD controlCode)
\r
117 switch(controlCode)
\r
119 case SERVICE_CONTROL_INTERROGATE:
\r
121 case SERVICE_CONTROL_SHUTDOWN:
\r
122 case SERVICE_CONTROL_STOP:
\r
123 serviceCurrentStatus = SERVICE_STOP_PENDING;
\r
124 UpdateSCMStatus(SERVICE_STOP_PENDING, NO_ERROR, 0, 1, 5000);
\r
126 UpdateSCMStatus(SERVICE_STOPPED, NO_ERROR, 0, 0, 0);
\r
131 UpdateSCMStatus(serviceCurrentStatus, NO_ERROR, 0, 0, 0);
\r
135 VOID ServiceMain(DWORD argc, LPTSTR *argv)
\r
138 DWORD type=0, size=0;
\r
140 serviceStatusHandle = RegisterServiceCtrlHandler("InspIRCd", (LPHANDLER_FUNCTION)ServiceCtrlHandler);
\r
141 if (!serviceStatusHandle)
\r
143 terminateService(1, GetLastError());
\r
147 success = UpdateSCMStatus(SERVICE_START_PENDING, NO_ERROR, 0, 1, 1000);
\r
150 terminateService(2, GetLastError());
\r
154 killServiceEvent = CreateEvent(NULL,true,false,NULL);
\r
155 hThreadEvent = CreateEvent(NULL,true,false,NULL);
\r
157 if (!killServiceEvent || !hThreadEvent)
\r
159 terminateService(99, GetLastError());
\r
163 success = UpdateSCMStatus(SERVICE_START_PENDING, NO_ERROR, 0, 2, 1000);
\r
166 terminateService(2, GetLastError());
\r
170 StartServiceThread();
\r
171 serviceCurrentStatus = SERVICE_RUNNING;
\r
172 success = UpdateSCMStatus(SERVICE_RUNNING, NO_ERROR, 0, 0, 0);
\r
175 terminateService(6, GetLastError());
\r
178 WaitForSingleObject (killServiceEvent, INFINITE);
\r
181 void InstallService()
\r
183 SC_HANDLE myService, scm;
\r
184 SERVICE_DESCRIPTION svDesc;
\r
185 HINSTANCE advapi32;
\r
187 char modname[MAX_PATH];
\r
188 GetModuleFileName(NULL, modname, sizeof(modname));
\r
190 scm = OpenSCManager(0,0,SC_MANAGER_CREATE_SERVICE);
\r
193 printf("Unable to open service control manager: %s\n", dlerror());
\r
197 myService = CreateService(scm,"InspIRCd","Inspire IRC Daemon", SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
\r
198 SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, modname, 0, 0, 0, NULL, NULL);
\r
202 printf("Unable to create service: %s\n", dlerror());
\r
203 CloseServiceHandle(scm);
\r
207 // *** Set service description ***
\r
208 // this is supported from 5.0 (win2k) onwards only, so we can't link to the definition of
\r
209 // this function in advapi32.lib, otherwise the program will not run on windows NT 4. We
\r
210 // must use LoadLibrary and GetProcAddress to export the function name from advapi32.dll
\r
211 advapi32 = LoadLibrary("advapi32.dll");
\r
214 ChangeServiceConf = (SETSERVDESC)GetProcAddress(advapi32,"ChangeServiceConfig2A");
\r
215 if (ChangeServiceConf)
\r
217 char desc[] = "The Inspire Internet Relay Chat Daemon hosts IRC channels and conversations.\
\r
218 If this service is stopped, the IRC server will not run.";
\r
219 svDesc.lpDescription = desc;
\r
220 BOOL success = ChangeServiceConf(myService,SERVICE_CONFIG_DESCRIPTION, &svDesc);
\r
223 printf("Unable to set service description: %s\n", dlerror());
\r
224 CloseServiceHandle(myService);
\r
225 CloseServiceHandle(scm);
\r
229 FreeLibrary(advapi32);
\r
232 printf("Service installed.\n");
\r
233 CloseServiceHandle(myService);
\r
234 CloseServiceHandle(scm);
\r
237 void RemoveService()
\r
239 SC_HANDLE myService, scm;
\r
241 scm = OpenSCManager(0,0,SC_MANAGER_CREATE_SERVICE);
\r
244 printf("Unable to open service control manager: %s\n", dlerror());
\r
248 myService = OpenService(scm,"InspIRCd",SERVICE_ALL_ACCESS);
\r
251 printf("Unable to open service: %s\n", dlerror());
\r
252 CloseServiceHandle(scm);
\r
256 if (!DeleteService(myService))
\r
258 printf("Unable to delete service: %s\n", dlerror());
\r
259 CloseServiceHandle(myService);
\r
260 CloseServiceHandle(scm);
\r
264 printf("Service removed.\n");
\r
265 CloseServiceHandle(myService);
\r
266 CloseServiceHandle(scm);
\r
269 /* In windows, our main() flows through here, before calling the 'real' main, smain() in inspircd.cpp */
\r
270 int main(int argc, char** argv)
\r
273 Commandline params[] = {
\r
274 { "--installservice", InstallService },
\r
275 { "--removeservice", RemoveService },
\r
279 /* Check for parameters */
\r
282 for (int z = 0; params[z].Switch; ++z)
\r
284 if (!_stricmp(argv[1], params[z].Switch))
\r
286 params[z].Handler();
\r
292 /* First, check if the service is installed.
\r
293 * if it is not, or we're starting as non-administrator,
\r
294 * just call smain() and start as normal non-service
\r
297 SC_HANDLE myService, scm;
\r
298 scm = OpenSCManager(0,0,SC_MANAGER_CREATE_SERVICE);
\r
301 myService = OpenService(scm,"InspIRCd",SERVICE_ALL_ACCESS);
\r
304 /* Service not installed or no permission to modify it */
\r
305 CloseServiceHandle(scm);
\r
306 return smain(argc, argv);
\r
311 /* Not enough privileges to open the SCM */
\r
312 return smain(argc, argv);
\r
315 CloseServiceHandle(myService);
\r
316 CloseServiceHandle(scm);
\r
318 /* Check if the process is running interactively. InspIRCd does not run interactively
\r
319 * as a service so if this is true, we just run the non-service inspircd.
\r
321 USEROBJECTFLAGS uoflags;
\r
322 HWINSTA winstation = GetProcessWindowStation();
\r
323 if (GetUserObjectInformation(winstation, UOI_FLAGS, &uoflags, sizeof(uoflags), NULL))
\r
325 if (uoflags.dwFlags == WSF_VISIBLE)
\r
326 return smain(argc, argv);
\r
329 /* If we get here, we know the service is installed so we can start it */
\r
331 SERVICE_TABLE_ENTRY serviceTable[] =
\r
333 {"InspIRCd", (LPSERVICE_MAIN_FUNCTION) ServiceMain },
\r
337 StartServiceCtrlDispatcher(serviceTable);
\r