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
21 static SERVICE_STATUS_HANDLE serviceStatusHandle;
\r
22 static HANDLE hThreadEvent;
\r
23 static HANDLE killServiceEvent;
\r
24 static int serviceCurrentStatus;
\r
26 // This is used to define ChangeServiceConf2() as we can't link
\r
27 // directly against this symbol (see below where it is used)
\r
28 typedef BOOL (CALLBACK* SETSERVDESC)(SC_HANDLE,DWORD,LPVOID);
\r
30 SETSERVDESC ChangeServiceConf; // A function pointer for dynamic linking tricks
\r
35 /* FIXME: This should set a flag in the mainloop for shutting down */
\r
36 SetEvent(hThreadEvent);
\r
38 SetEvent(killServiceEvent);
\r
41 DWORD WINAPI WorkerThread(LPDWORD param)
\r
43 // *** REAL MAIN HERE ***
\r
44 char modname[MAX_PATH];
\r
45 GetModuleFileName(NULL, modname, sizeof(modname));
\r
46 char* argv[] = { modname, "--nofork", "--debug" };
\r
52 void StartServiceThread()
\r
55 CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)WorkerThread,NULL,0,&dwd);
\r
59 BOOL UpdateSCMStatus (DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwServiceSpecificExitCode, DWORD dwCheckPoint, DWORD dwWaitHint)
\r
62 SERVICE_STATUS serviceStatus;
\r
63 serviceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
\r
64 serviceStatus.dwCurrentState = dwCurrentState;
\r
66 if (dwCurrentState == SERVICE_START_PENDING)
\r
68 serviceStatus.dwControlsAccepted = 0;
\r
72 serviceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
\r
75 if (dwServiceSpecificExitCode == 0)
\r
77 serviceStatus.dwWin32ExitCode = dwWin32ExitCode;
\r
81 serviceStatus.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;
\r
83 serviceStatus.dwServiceSpecificExitCode = dwServiceSpecificExitCode;
\r
84 serviceStatus.dwCheckPoint = dwCheckPoint;
\r
85 serviceStatus.dwWaitHint = dwWaitHint;
\r
87 success = SetServiceStatus (serviceStatusHandle, &serviceStatus);
\r
96 void terminateService (int code, int wincode)
\r
98 UpdateSCMStatus(SERVICE_STOPPED,wincode?wincode:ERROR_SERVICE_SPECIFIC_ERROR,(wincode)?0:code,0,0);
\r
103 VOID ServiceCtrlHandler (DWORD controlCode)
\r
105 switch(controlCode)
\r
107 case SERVICE_CONTROL_INTERROGATE:
\r
109 case SERVICE_CONTROL_SHUTDOWN:
\r
110 case SERVICE_CONTROL_STOP:
\r
111 serviceCurrentStatus = SERVICE_STOP_PENDING;
\r
112 UpdateSCMStatus(SERVICE_STOP_PENDING, NO_ERROR, 0, 1, 5000);
\r
114 UpdateSCMStatus(SERVICE_STOPPED, NO_ERROR, 0, 0, 0);
\r
119 UpdateSCMStatus(serviceCurrentStatus, NO_ERROR, 0, 0, 0);
\r
123 VOID ServiceMain(DWORD argc, LPTSTR *argv)
\r
126 DWORD type=0, size=0;
\r
128 serviceStatusHandle = RegisterServiceCtrlHandler("InspIRCd", (LPHANDLER_FUNCTION)ServiceCtrlHandler);
\r
129 if (!serviceStatusHandle)
\r
131 terminateService(1,GetLastError());
\r
135 success = UpdateSCMStatus(SERVICE_START_PENDING, NO_ERROR, 0, 1, 1000);
\r
138 terminateService(2,GetLastError());
\r
142 killServiceEvent = CreateEvent(NULL,true,false,NULL);
\r
143 hThreadEvent = CreateEvent(NULL,true,false,NULL);
\r
145 if (!killServiceEvent || !hThreadEvent)
\r
147 terminateService(99,GetLastError());
\r
151 success = UpdateSCMStatus(SERVICE_START_PENDING, NO_ERROR, 0, 2, 1000);
\r
154 terminateService(2,GetLastError());
\r
158 StartServiceThread();
\r
159 serviceCurrentStatus = SERVICE_RUNNING;
\r
160 success = UpdateSCMStatus(SERVICE_RUNNING, NO_ERROR, 0, 0, 0);
\r
163 terminateService(6,GetLastError());
\r
166 WaitForSingleObject (killServiceEvent, INFINITE);
\r
169 void InstallService(void)
\r
171 SC_HANDLE myService, scm;
\r
172 SERVICE_DESCRIPTION svDesc;
\r
173 HINSTANCE advapi32;
\r
175 char modname[MAX_PATH];
\r
176 GetModuleFileName(NULL, modname, sizeof(modname));
\r
178 scm = OpenSCManager(0,0,SC_MANAGER_CREATE_SERVICE);
\r
181 printf("Unable to open service control manager: %d\n", GetLastError());
\r
185 myService = CreateService(scm,"InspIRCd","Inspire IRC Daemon", SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
\r
186 SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, modname, 0, 0, 0, NULL, NULL);
\r
190 printf("Unable to create service: %d\n", GetLastError());
\r
191 CloseServiceHandle(scm);
\r
195 // *** Set service description ***
\r
196 // this is supported from 5.0 (win2k) onwards only, so we can't link to the definition of
\r
197 // this function in advapi32.lib, otherwise the program will not run on windows NT 4. We
\r
198 // must use LoadLibrary and GetProcAddress to export the function name from advapi32.dll
\r
199 advapi32 = LoadLibrary("advapi32.dll");
\r
202 ChangeServiceConf = (SETSERVDESC)GetProcAddress(advapi32,"ChangeServiceConfig2A");
\r
203 if (ChangeServiceConf)
\r
205 char desc[] = "The Inspire Internet Relay Chat Daemon hosts IRC channels and conversations. \
\r
206 If this service is stopped, the IRC server will not run.";
\r
207 svDesc.lpDescription = desc;
\r
208 BOOL success = ChangeServiceConf(myService,SERVICE_CONFIG_DESCRIPTION, &svDesc);
\r
211 printf("Unable to set service description: %d\n", GetLastError());
\r
212 CloseServiceHandle(myService);
\r
213 CloseServiceHandle(scm);
\r
217 FreeLibrary(advapi32);
\r
220 printf("Service installed.\n");
\r
221 CloseServiceHandle(myService);
\r
222 CloseServiceHandle(scm);
\r
225 void RemoveService(void)
\r
227 SC_HANDLE myService, scm;
\r
229 scm = OpenSCManager(0,0,SC_MANAGER_CREATE_SERVICE);
\r
232 printf("Unable to open service control manager: %d\n", GetLastError());
\r
236 myService = OpenService(scm,"InspIRCd",SERVICE_ALL_ACCESS);
\r
239 printf("Unable to open service: %d\n", GetLastError());
\r
240 CloseServiceHandle(scm);
\r
244 if (!DeleteService(myService))
\r
246 printf("Unable to delete service: %d\n", GetLastError());
\r
247 CloseServiceHandle(myService);
\r
248 CloseServiceHandle(scm);
\r
252 printf("Service removed.\n");
\r
253 CloseServiceHandle(myService);
\r
254 CloseServiceHandle(scm);
\r
257 /* In windows, our main() flows through here, before calling the 'real' main, smain() in inspircd.cpp */
\r
258 int main(int argc, char** argv)
\r
260 /* Check for parameters */
\r
263 if (!_stricmp(argv[1], "--installservice"))
\r
268 else if (!_stricmp(argv[1], "--removeservice"))
\r
275 /* First, check if the service is installed.
\r
276 * if it is not, or we're starting as non-administrator,
\r
277 * just call smain() and start as normal non-service
\r
280 SC_HANDLE myService, scm;
\r
281 scm = OpenSCManager(0,0,SC_MANAGER_CREATE_SERVICE);
\r
284 myService = OpenService(scm,"InspIRCd",SERVICE_ALL_ACCESS);
\r
287 /* Service not installed or no permission to modify it */
\r
288 CloseServiceHandle(scm);
\r
294 /* Not enough privileges to open the SCM */
\r
298 CloseServiceHandle(myService);
\r
299 CloseServiceHandle(scm);
\r
301 /* If we get here, we know the service is installed so we can start it */
\r
303 SERVICE_TABLE_ENTRY serviceTable[] =
\r
305 {"InspIRCd", (LPSERVICE_MAIN_FUNCTION) ServiceMain },
\r
309 StartServiceCtrlDispatcher(serviceTable);
\r