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
30 typedef void (*CommandlineParameterHandler)(void);
\r
32 SETSERVDESC ChangeServiceConf; // A function pointer for dynamic linking tricks
\r
37 /* FIXME: This should set a flag in the mainloop for shutting down */
\r
38 SetEvent(hThreadEvent);
\r
40 SetEvent(killServiceEvent);
\r
43 DWORD WINAPI WorkerThread(LPDWORD param)
\r
45 // *** REAL MAIN HERE ***
\r
46 char modname[MAX_PATH];
\r
47 GetModuleFileName(NULL, modname, sizeof(modname));
\r
48 char* argv[] = { modname, "--nofork", "--debug" };
\r
54 void StartServiceThread()
\r
57 CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)WorkerThread,NULL,0,&dwd);
\r
61 BOOL UpdateSCMStatus (DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwServiceSpecificExitCode, DWORD dwCheckPoint, DWORD dwWaitHint)
\r
64 SERVICE_STATUS serviceStatus;
\r
65 serviceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
\r
66 serviceStatus.dwCurrentState = dwCurrentState;
\r
68 if (dwCurrentState == SERVICE_START_PENDING)
\r
70 serviceStatus.dwControlsAccepted = 0;
\r
74 serviceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
\r
77 if (dwServiceSpecificExitCode == 0)
\r
79 serviceStatus.dwWin32ExitCode = dwWin32ExitCode;
\r
83 serviceStatus.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;
\r
85 serviceStatus.dwServiceSpecificExitCode = dwServiceSpecificExitCode;
\r
86 serviceStatus.dwCheckPoint = dwCheckPoint;
\r
87 serviceStatus.dwWaitHint = dwWaitHint;
\r
89 success = SetServiceStatus (serviceStatusHandle, &serviceStatus);
\r
98 void terminateService (int code, int wincode)
\r
100 UpdateSCMStatus(SERVICE_STOPPED,wincode?wincode:ERROR_SERVICE_SPECIFIC_ERROR,(wincode)?0:code,0,0);
\r
105 VOID ServiceCtrlHandler (DWORD controlCode)
\r
107 switch(controlCode)
\r
109 case SERVICE_CONTROL_INTERROGATE:
\r
111 case SERVICE_CONTROL_SHUTDOWN:
\r
112 case SERVICE_CONTROL_STOP:
\r
113 serviceCurrentStatus = SERVICE_STOP_PENDING;
\r
114 UpdateSCMStatus(SERVICE_STOP_PENDING, NO_ERROR, 0, 1, 5000);
\r
116 UpdateSCMStatus(SERVICE_STOPPED, NO_ERROR, 0, 0, 0);
\r
121 UpdateSCMStatus(serviceCurrentStatus, NO_ERROR, 0, 0, 0);
\r
125 VOID ServiceMain(DWORD argc, LPTSTR *argv)
\r
128 DWORD type=0, size=0;
\r
130 serviceStatusHandle = RegisterServiceCtrlHandler("InspIRCd", (LPHANDLER_FUNCTION)ServiceCtrlHandler);
\r
131 if (!serviceStatusHandle)
\r
133 terminateService(1, GetLastError());
\r
137 success = UpdateSCMStatus(SERVICE_START_PENDING, NO_ERROR, 0, 1, 1000);
\r
140 terminateService(2, GetLastError());
\r
144 killServiceEvent = CreateEvent(NULL,true,false,NULL);
\r
145 hThreadEvent = CreateEvent(NULL,true,false,NULL);
\r
147 if (!killServiceEvent || !hThreadEvent)
\r
149 terminateService(99, GetLastError());
\r
153 success = UpdateSCMStatus(SERVICE_START_PENDING, NO_ERROR, 0, 2, 1000);
\r
156 terminateService(2, GetLastError());
\r
160 StartServiceThread();
\r
161 serviceCurrentStatus = SERVICE_RUNNING;
\r
162 success = UpdateSCMStatus(SERVICE_RUNNING, NO_ERROR, 0, 0, 0);
\r
165 terminateService(6, GetLastError());
\r
168 WaitForSingleObject (killServiceEvent, INFINITE);
\r
171 void InstallService()
\r
173 SC_HANDLE myService, scm;
\r
174 SERVICE_DESCRIPTION svDesc;
\r
175 HINSTANCE advapi32;
\r
177 char modname[MAX_PATH];
\r
178 GetModuleFileName(NULL, modname, sizeof(modname));
\r
180 scm = OpenSCManager(0,0,SC_MANAGER_CREATE_SERVICE);
\r
183 printf("Unable to open service control manager: %s\n", dlerror());
\r
187 myService = CreateService(scm,"InspIRCd","Inspire IRC Daemon", SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
\r
188 SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, modname, 0, 0, 0, NULL, NULL);
\r
192 printf("Unable to create service: %s\n", dlerror());
\r
193 CloseServiceHandle(scm);
\r
197 // *** Set service description ***
\r
198 // this is supported from 5.0 (win2k) onwards only, so we can't link to the definition of
\r
199 // this function in advapi32.lib, otherwise the program will not run on windows NT 4. We
\r
200 // must use LoadLibrary and GetProcAddress to export the function name from advapi32.dll
\r
201 advapi32 = LoadLibrary("advapi32.dll");
\r
204 ChangeServiceConf = (SETSERVDESC)GetProcAddress(advapi32,"ChangeServiceConfig2A");
\r
205 if (ChangeServiceConf)
\r
207 char desc[] = "The Inspire Internet Relay Chat Daemon hosts IRC channels and conversations.\
\r
208 If this service is stopped, the IRC server will not run.";
\r
209 svDesc.lpDescription = desc;
\r
210 BOOL success = ChangeServiceConf(myService,SERVICE_CONFIG_DESCRIPTION, &svDesc);
\r
213 printf("Unable to set service description: %s\n", dlerror());
\r
214 CloseServiceHandle(myService);
\r
215 CloseServiceHandle(scm);
\r
219 FreeLibrary(advapi32);
\r
222 printf("Service installed.\n");
\r
223 CloseServiceHandle(myService);
\r
224 CloseServiceHandle(scm);
\r
227 void RemoveService()
\r
229 SC_HANDLE myService, scm;
\r
231 scm = OpenSCManager(0,0,SC_MANAGER_CREATE_SERVICE);
\r
234 printf("Unable to open service control manager: %s\n", dlerror());
\r
238 myService = OpenService(scm,"InspIRCd",SERVICE_ALL_ACCESS);
\r
241 printf("Unable to open service: %s\n", dlerror());
\r
242 CloseServiceHandle(scm);
\r
246 if (!DeleteService(myService))
\r
248 printf("Unable to delete service: %s\n", dlerror());
\r
249 CloseServiceHandle(myService);
\r
250 CloseServiceHandle(scm);
\r
254 printf("Service removed.\n");
\r
255 CloseServiceHandle(myService);
\r
256 CloseServiceHandle(scm);
\r
261 const char* Switch;
\r
262 CommandlineParameterHandler Handler;
\r
265 /* In windows, our main() flows through here, before calling the 'real' main, smain() in inspircd.cpp */
\r
266 int main(int argc, char** argv)
\r
269 Commandline params[] = {
\r
270 { "--installservice", InstallService },
\r
271 { "--removeservice", RemoveService },
\r
275 /* Check for parameters */
\r
278 for (int z = 0; params[z].Switch; ++z)
\r
280 if (!_stricmp(argv[1], params[z].Switch))
\r
282 params[z].Handler();
\r
288 /* First, check if the service is installed.
\r
289 * if it is not, or we're starting as non-administrator,
\r
290 * just call smain() and start as normal non-service
\r
293 SC_HANDLE myService, scm;
\r
294 scm = OpenSCManager(0,0,SC_MANAGER_CREATE_SERVICE);
\r
297 myService = OpenService(scm,"InspIRCd",SERVICE_ALL_ACCESS);
\r
300 /* Service not installed or no permission to modify it */
\r
301 CloseServiceHandle(scm);
\r
307 /* Not enough privileges to open the SCM */
\r
311 CloseServiceHandle(myService);
\r
312 CloseServiceHandle(scm);
\r
314 /* If we get here, we know the service is installed so we can start it */
\r
316 SERVICE_TABLE_ENTRY serviceTable[] =
\r
318 {"InspIRCd", (LPSERVICE_MAIN_FUNCTION) ServiceMain },
\r
322 StartServiceCtrlDispatcher(serviceTable);
\r