1 | /*
|
---|
2 | * NT Service interface Utility.
|
---|
3 | * Based on code written by
|
---|
4 | * Chas Woodfield, Fretwell Downing Datasystems.
|
---|
5 | * $Log$
|
---|
6 | * Revision 1.1 2000/08/03 03:11:42 johnmcp
|
---|
7 | * Added the YAZ toolkit source to the packages directory (for z39.50 stuff)
|
---|
8 | *
|
---|
9 | * Revision 1.3 1999/06/10 11:45:30 adam
|
---|
10 | * Added bend_start, bend_stop handlers and removed pre_init.
|
---|
11 | * Handlers bend_start/bend_stop are called when service/daemon is
|
---|
12 | * started/stopped.
|
---|
13 | *
|
---|
14 | * Revision 1.2 1999/02/02 13:57:36 adam
|
---|
15 | * Uses preprocessor define WIN32 instead of WINDOWS to build code
|
---|
16 | * for Microsoft WIN32.
|
---|
17 | *
|
---|
18 | * Revision 1.1 1997/11/07 13:31:52 adam
|
---|
19 | * Added NT Service name part of statserv_options_block. Moved NT
|
---|
20 | * service utility to server library.
|
---|
21 | *
|
---|
22 | * Revision 1.6 1997/09/18 08:49:14 adam
|
---|
23 | * Option -runnormal no needed to run server in standalone mode.
|
---|
24 | *
|
---|
25 | * Revision 1.5 1997/09/17 12:10:43 adam
|
---|
26 | * YAZ version 1.4.
|
---|
27 | *
|
---|
28 | * Revision 1.4 1997/09/09 10:10:20 adam
|
---|
29 | * Another MSV5.0 port. Changed projects to include proper
|
---|
30 | * library/include paths.
|
---|
31 | * Server starts server in test-mode when no options are given.
|
---|
32 | *
|
---|
33 | * Revision 1.3 1997/09/04 13:50:30 adam
|
---|
34 | * Bug fix in ztest.
|
---|
35 | *
|
---|
36 | */
|
---|
37 |
|
---|
38 | /************************************************************/
|
---|
39 | /* Note this file is shared by all processes */
|
---|
40 | /* Should really put it somewhere other than here */
|
---|
41 | /* For some strange reason it won't work when part of a lib */
|
---|
42 | /************************************************************/
|
---|
43 |
|
---|
44 | #ifdef WIN32
|
---|
45 |
|
---|
46 | #include <windows.h>
|
---|
47 | #include <stdio.h>
|
---|
48 | #include <tchar.h>
|
---|
49 |
|
---|
50 | #include "service.h"
|
---|
51 |
|
---|
52 | static AppService *pService = NULL;
|
---|
53 | static BOOL bRunAsService = TRUE;
|
---|
54 | static void *pAppHandle = NULL;
|
---|
55 |
|
---|
56 | /* Private functions to this module */
|
---|
57 | void Service_Create(LPTSTR pAppName, LPTSTR pServiceName, LPTSTR pServiceDisplayName, LPTSTR pDependancies, int argc, char **argv);
|
---|
58 | void Service_Delete();
|
---|
59 | void Service_Initialize();
|
---|
60 | BOOL NotifyServiceController();
|
---|
61 | BOOL UpdateServiceStatus(DWORD Status);
|
---|
62 | void FailServiceStart(DWORD Win32Code, DWORD PrivateCode);
|
---|
63 | void CmdInstallService(int argc, char *argv[], BOOL bAutoStart);
|
---|
64 | void CmdRemoveService();
|
---|
65 | LPTSTR GetLastErrorText(LPTSTR lpszBuf, DWORD dwSize);
|
---|
66 | BOOL CheckServiceArguments(int argc, char *argv[]);
|
---|
67 |
|
---|
68 | /* Callback functions for thee service manager */
|
---|
69 | void WINAPI ServiceMain(DWORD argc, LPTSTR argv[]);
|
---|
70 | void WINAPI ServiceControlHandler(DWORD fdwControl);
|
---|
71 |
|
---|
72 | /* Function to handle Ctrl + C etc... */
|
---|
73 | BOOL EventHandlerRoutine(DWORD dwCtrlType);
|
---|
74 |
|
---|
75 | void Service_Create(LPTSTR pAppName, LPTSTR pServiceName, LPTSTR pServiceDisplayName, LPTSTR pDependancies, int argc, char **argv)
|
---|
76 | {
|
---|
77 | pService = malloc(sizeof(AppService));
|
---|
78 | pService->pAppName = pAppName;
|
---|
79 | pService->pServiceName = pServiceName;
|
---|
80 | pService->pServiceDisplayName = pServiceDisplayName;
|
---|
81 | pService->pDependancies = pDependancies;
|
---|
82 | pService->hService = 0;
|
---|
83 | pService->ServiceTable[0].lpServiceName = pServiceName;
|
---|
84 | pService->ServiceTable[0].lpServiceProc = ServiceMain;
|
---|
85 | pService->ServiceTable[1].lpServiceName = NULL;
|
---|
86 | pService->ServiceTable[1].lpServiceProc = NULL;
|
---|
87 | pService->argc = argc;
|
---|
88 | pService->argv = argv;
|
---|
89 | }
|
---|
90 |
|
---|
91 | void Service_Delete()
|
---|
92 | {
|
---|
93 | if (pService != NULL)
|
---|
94 | {
|
---|
95 | /* Mark the service as stopping */
|
---|
96 | UpdateServiceStatus(SERVICE_STOP_PENDING);
|
---|
97 |
|
---|
98 | /* Stop the service */
|
---|
99 | StopAppService(pAppHandle);
|
---|
100 |
|
---|
101 | /* Service has now stopped */
|
---|
102 | UpdateServiceStatus(SERVICE_STOPPED);
|
---|
103 |
|
---|
104 | /* Free the memory */
|
---|
105 | free(pService);
|
---|
106 | pService = NULL;
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | void Service_Initialize()
|
---|
111 | {
|
---|
112 | if (pService != NULL)
|
---|
113 | {
|
---|
114 | /* Register ourselves with the control dispatcher */
|
---|
115 | StartServiceCtrlDispatcher(pService->ServiceTable);
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | void WINAPI ServiceMain(DWORD argc, LPTSTR argv[])
|
---|
120 | {
|
---|
121 | if (pService != NULL)
|
---|
122 | {
|
---|
123 | if (NotifyServiceController())
|
---|
124 | {
|
---|
125 | /* Set the status to pending */
|
---|
126 | UpdateServiceStatus(SERVICE_START_PENDING);
|
---|
127 |
|
---|
128 | /* Lets attempt to start the service */
|
---|
129 | if (StartAppService(pAppHandle, pService->argc, pService->argv))
|
---|
130 | {
|
---|
131 | /* Service is now up and running */
|
---|
132 | UpdateServiceStatus(SERVICE_RUNNING);
|
---|
133 |
|
---|
134 | /* Lets wait for our clients */
|
---|
135 | RunAppService(pAppHandle);
|
---|
136 | }
|
---|
137 | else
|
---|
138 | {
|
---|
139 | FailServiceStart(GetLastError(), 0);
|
---|
140 | Service_Delete();
|
---|
141 | }
|
---|
142 | }
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | BOOL NotifyServiceController()
|
---|
147 | {
|
---|
148 | if (pService == NULL)
|
---|
149 | {
|
---|
150 | return(FALSE);
|
---|
151 | }
|
---|
152 | else
|
---|
153 | {
|
---|
154 | if (bRunAsService)
|
---|
155 | {
|
---|
156 | pService->ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
|
---|
157 | pService->ServiceStatus.dwCurrentState = SERVICE_STOPPED;
|
---|
158 | pService->ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
|
---|
159 | pService->ServiceStatus.dwWin32ExitCode = 0;
|
---|
160 | pService->ServiceStatus.dwServiceSpecificExitCode = 0;
|
---|
161 | pService->ServiceStatus.dwCheckPoint = 0;
|
---|
162 | pService->ServiceStatus.dwWaitHint = 0;
|
---|
163 | pService->hService = RegisterServiceCtrlHandler(pService->pServiceName, ServiceControlHandler);
|
---|
164 |
|
---|
165 | if (pService->hService)
|
---|
166 | UpdateServiceStatus(SERVICE_START_PENDING);
|
---|
167 | else
|
---|
168 | return(FALSE);
|
---|
169 | }
|
---|
170 | return(TRUE);
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | void WINAPI ServiceControlHandler(DWORD fdwControl)
|
---|
175 | {
|
---|
176 | if (pService != NULL)
|
---|
177 | {
|
---|
178 | switch (fdwControl)
|
---|
179 | {
|
---|
180 | case SERVICE_CONTROL_STOP:
|
---|
181 | /* Update the service status to be pending */
|
---|
182 | Service_Delete();
|
---|
183 | break;
|
---|
184 |
|
---|
185 | case SERVICE_CONTROL_INTERROGATE:
|
---|
186 | UpdateServiceStatus(pService->ServiceStatus.dwCurrentState);
|
---|
187 | break;
|
---|
188 |
|
---|
189 | default:
|
---|
190 | break;
|
---|
191 | }
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | BOOL UpdateServiceStatus(DWORD Status)
|
---|
196 | {
|
---|
197 | if (pService != NULL)
|
---|
198 | {
|
---|
199 | if (pService->hService)
|
---|
200 | {
|
---|
201 | pService->ServiceStatus.dwCurrentState = Status;
|
---|
202 | if ((Status == SERVICE_START_PENDING) || (Status == SERVICE_STOP_PENDING))
|
---|
203 | {
|
---|
204 | pService->ServiceStatus.dwCheckPoint ++;
|
---|
205 | pService->ServiceStatus.dwWaitHint = 5000; /* 5 sec.*/
|
---|
206 | }
|
---|
207 | else
|
---|
208 | {
|
---|
209 | pService->ServiceStatus.dwCheckPoint = 0;
|
---|
210 | pService->ServiceStatus.dwWaitHint = 0;
|
---|
211 | }
|
---|
212 |
|
---|
213 | return(SetServiceStatus(pService->hService, &pService->ServiceStatus));
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | return(FALSE);
|
---|
218 | }
|
---|
219 |
|
---|
220 | void FailServiceStart(DWORD Win32Code, DWORD PrivateCode)
|
---|
221 | {
|
---|
222 | if (pService != NULL)
|
---|
223 | {
|
---|
224 | pService->ServiceStatus.dwWin32ExitCode = Win32Code;
|
---|
225 | pService->ServiceStatus.dwServiceSpecificExitCode = PrivateCode;
|
---|
226 | UpdateServiceStatus(SERVICE_STOPPED);
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
230 | void CmdInstallService(int argc, char *argv[], BOOL bAutoStart)
|
---|
231 | {
|
---|
232 | if (pService != NULL)
|
---|
233 | {
|
---|
234 | SC_HANDLE schService;
|
---|
235 | SC_HANDLE schSCManager;
|
---|
236 |
|
---|
237 | TCHAR szPath[1024];
|
---|
238 |
|
---|
239 | if (GetModuleFileName(NULL, szPath, 512) == 0)
|
---|
240 | {
|
---|
241 | _tprintf(TEXT("Unable to install %s - %s\n"), TEXT(pService->pServiceDisplayName), GetLastErrorText(pService->szErr, 256));
|
---|
242 | }
|
---|
243 | else
|
---|
244 | {
|
---|
245 | int i;
|
---|
246 |
|
---|
247 | strcat (szPath, TEXT(" -runservice"));
|
---|
248 | for (i = 1; i < argc; i++)
|
---|
249 | {
|
---|
250 | /* We will add the given command line arguments to the command */
|
---|
251 | /* We are not interested in the install and remove options */
|
---|
252 | if ((stricmp("-install", argv[i]) != 0) &&
|
---|
253 | (stricmp("-installa", argv[i]) != 0) &&
|
---|
254 | (stricmp("-remove", argv[i]) != 0))
|
---|
255 | {
|
---|
256 | strcat(szPath, TEXT(" "));
|
---|
257 | strcat(szPath, argv[i]);
|
---|
258 | }
|
---|
259 | }
|
---|
260 |
|
---|
261 | schSCManager = OpenSCManager(NULL, /* machine (NULL == local) */
|
---|
262 | NULL, /* database (NULL == default) */
|
---|
263 | SC_MANAGER_ALL_ACCESS); /* access required */
|
---|
264 | if (schSCManager)
|
---|
265 | {
|
---|
266 | schService = CreateService(schSCManager, /* SCManager database */
|
---|
267 | TEXT(pService->pServiceName), /* name of service */
|
---|
268 | TEXT(pService->pServiceDisplayName), /* name to display */
|
---|
269 | SERVICE_ALL_ACCESS, /* desired access */
|
---|
270 | SERVICE_WIN32_OWN_PROCESS, /* service type */
|
---|
271 | bAutoStart ? SERVICE_AUTO_START :
|
---|
272 | SERVICE_DEMAND_START, /* start type */
|
---|
273 | SERVICE_ERROR_NORMAL, /* error control type */
|
---|
274 | szPath, /* service's binary */
|
---|
275 | NULL, /* no load ordering group */
|
---|
276 | NULL, /* no tag identifier */
|
---|
277 | TEXT(pService->pDependancies), /* dependencies */
|
---|
278 | NULL, /* LocalSystem account */
|
---|
279 | NULL); /* no password */
|
---|
280 |
|
---|
281 | if (schService)
|
---|
282 | {
|
---|
283 | _tprintf(TEXT("%s installed.\n"), TEXT(pService->pServiceDisplayName));
|
---|
284 | CloseServiceHandle(schService);
|
---|
285 | }
|
---|
286 | else
|
---|
287 | {
|
---|
288 | _tprintf(TEXT("CreateService failed - %s\n"), GetLastErrorText(pService->szErr, 256));
|
---|
289 | }
|
---|
290 |
|
---|
291 | CloseServiceHandle(schSCManager);
|
---|
292 | }
|
---|
293 | else
|
---|
294 | _tprintf(TEXT("OpenSCManager failed - %s\n"), GetLastErrorText(pService->szErr,256));
|
---|
295 | }
|
---|
296 | }
|
---|
297 | }
|
---|
298 |
|
---|
299 | void CmdRemoveService()
|
---|
300 | {
|
---|
301 | if (pService != NULL)
|
---|
302 | {
|
---|
303 | SC_HANDLE schService;
|
---|
304 | SC_HANDLE schSCManager;
|
---|
305 |
|
---|
306 | schSCManager = OpenSCManager(NULL, /* machine (NULL == local) */
|
---|
307 | NULL, /* database (NULL == default) */
|
---|
308 | SC_MANAGER_ALL_ACCESS); /* access required */
|
---|
309 | if (schSCManager)
|
---|
310 | {
|
---|
311 | schService = OpenService(schSCManager, TEXT(pService->pServiceName), SERVICE_ALL_ACCESS);
|
---|
312 |
|
---|
313 | if (schService)
|
---|
314 | {
|
---|
315 | /* try to stop the service */
|
---|
316 | if (ControlService(schService, SERVICE_CONTROL_STOP, &pService->ServiceStatus))
|
---|
317 | {
|
---|
318 | _tprintf(TEXT("Stopping %s."), TEXT(pService->pServiceDisplayName));
|
---|
319 | Sleep(1000);
|
---|
320 |
|
---|
321 | while (QueryServiceStatus(schService, &pService->ServiceStatus))
|
---|
322 | {
|
---|
323 | if (pService->ServiceStatus.dwCurrentState == SERVICE_STOP_PENDING)
|
---|
324 | {
|
---|
325 | _tprintf(TEXT("."));
|
---|
326 | Sleep( 1000 );
|
---|
327 | }
|
---|
328 | else
|
---|
329 | break;
|
---|
330 | }
|
---|
331 |
|
---|
332 | if (pService->ServiceStatus.dwCurrentState == SERVICE_STOPPED)
|
---|
333 | _tprintf(TEXT("\n%s stopped.\n"), TEXT(pService->pServiceDisplayName));
|
---|
334 | else
|
---|
335 | _tprintf(TEXT("\n%s failed to stop.\n"), TEXT(pService->pServiceDisplayName));
|
---|
336 |
|
---|
337 | }
|
---|
338 |
|
---|
339 | /* now remove the service */
|
---|
340 | if(DeleteService(schService))
|
---|
341 | _tprintf(TEXT("%s removed.\n"), TEXT(pService->pServiceDisplayName));
|
---|
342 | else
|
---|
343 | _tprintf(TEXT("DeleteService failed - %s\n"), GetLastErrorText(pService->szErr,256));
|
---|
344 |
|
---|
345 | CloseServiceHandle(schService);
|
---|
346 | }
|
---|
347 | else
|
---|
348 | _tprintf(TEXT("OpenService failed - %s\n"), GetLastErrorText(pService->szErr,256));
|
---|
349 |
|
---|
350 | CloseServiceHandle(schSCManager);
|
---|
351 | }
|
---|
352 | else
|
---|
353 | _tprintf(TEXT("OpenSCManager failed - %s\n"), GetLastErrorText(pService->szErr,256));
|
---|
354 | }
|
---|
355 | }
|
---|
356 |
|
---|
357 | LPTSTR GetLastErrorText(LPTSTR lpszBuf, DWORD dwSize)
|
---|
358 | {
|
---|
359 | DWORD dwRet;
|
---|
360 | LPTSTR lpszTemp = NULL;
|
---|
361 |
|
---|
362 | dwRet = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_ARGUMENT_ARRAY,
|
---|
363 | NULL,
|
---|
364 | GetLastError(),
|
---|
365 | LANG_NEUTRAL,
|
---|
366 | (LPTSTR)&lpszTemp,
|
---|
367 | 0,
|
---|
368 | NULL);
|
---|
369 |
|
---|
370 | /* supplied buffer is not long enough */
|
---|
371 | if (!dwRet || ((long)dwSize < (long)dwRet + 14))
|
---|
372 | lpszBuf[0] = TEXT('\0');
|
---|
373 | else
|
---|
374 | {
|
---|
375 | lpszTemp[lstrlen(lpszTemp)-2] = TEXT('\0'); /* remove cr and newline character */
|
---|
376 | _stprintf(lpszBuf, TEXT("%s (0x%x)"), lpszTemp, GetLastError());
|
---|
377 | }
|
---|
378 |
|
---|
379 | if (lpszTemp)
|
---|
380 | LocalFree((HLOCAL)lpszTemp);
|
---|
381 |
|
---|
382 | return(lpszBuf);
|
---|
383 | }
|
---|
384 |
|
---|
385 | BOOL CheckServiceArguments(int argc, char *argv[])
|
---|
386 | {
|
---|
387 | int i;
|
---|
388 |
|
---|
389 | /* Lets process the arguments */
|
---|
390 | for (i = 1; i < argc; i++)
|
---|
391 | {
|
---|
392 | if (stricmp("-install", argv[i]) == 0)
|
---|
393 | {
|
---|
394 | /* They want to install the service */
|
---|
395 | CmdInstallService(argc, argv, FALSE);
|
---|
396 |
|
---|
397 | /* We don't carry on, after we have installed the service */
|
---|
398 | return(FALSE);
|
---|
399 | }
|
---|
400 | else if (stricmp("-installa", argv[i]) == 0)
|
---|
401 | {
|
---|
402 | /* They want to install the service */
|
---|
403 | CmdInstallService(argc, argv, TRUE);
|
---|
404 |
|
---|
405 | /* We don't carry on, after we have installed the service */
|
---|
406 | return(FALSE);
|
---|
407 | }
|
---|
408 | else if (stricmp("-remove", argv[i]) == 0)
|
---|
409 | {
|
---|
410 | /* Here they want to remove it */
|
---|
411 | CmdRemoveService();
|
---|
412 |
|
---|
413 | /* We don't carry on, after we have removed the service */
|
---|
414 | return(FALSE);
|
---|
415 | }
|
---|
416 | else if (stricmp ("-runservice", argv[i]) == 0)
|
---|
417 | {
|
---|
418 | /* We can carry on, if we reached here */
|
---|
419 | argv[i] = "";
|
---|
420 | return(TRUE);
|
---|
421 | }
|
---|
422 | }
|
---|
423 | bRunAsService = FALSE;
|
---|
424 | return(TRUE);
|
---|
425 | }
|
---|
426 |
|
---|
427 | BOOL SetupService(int argc, char *argv[], void *pHandle, LPTSTR pAppName, LPTSTR pServiceName, LPTSTR pServiceDisplayName, LPTSTR pDependancies)
|
---|
428 | {
|
---|
429 | BOOL bDeleteService = TRUE;
|
---|
430 | BOOL bResult = FALSE;
|
---|
431 |
|
---|
432 | /* Save the handle for later use */
|
---|
433 | pAppHandle = pHandle;
|
---|
434 |
|
---|
435 | /* Create our service class */
|
---|
436 | Service_Create(pAppName, pServiceName, pServiceDisplayName, pDependancies, argc, argv);
|
---|
437 |
|
---|
438 | if (CheckServiceArguments(argc, argv))
|
---|
439 | {
|
---|
440 | if (bRunAsService)
|
---|
441 | {
|
---|
442 | /* No need to set the console control handler, as the service manager handles all this for us */
|
---|
443 | Service_Initialize();
|
---|
444 | bDeleteService = FALSE;
|
---|
445 | }
|
---|
446 | else
|
---|
447 | {
|
---|
448 | /* Set the console control handler for exiting the program */
|
---|
449 | SetConsoleCtrlHandler((PHANDLER_ROUTINE)EventHandlerRoutine, TRUE);
|
---|
450 |
|
---|
451 | /* Now do the main work */
|
---|
452 | ServiceMain(argc, argv);
|
---|
453 | }
|
---|
454 |
|
---|
455 | /* We have been successful initializing, so let the caller know */
|
---|
456 | bResult = TRUE;
|
---|
457 | }
|
---|
458 |
|
---|
459 | if (bDeleteService)
|
---|
460 | {
|
---|
461 | /* Finished with the service now */
|
---|
462 | Service_Delete();
|
---|
463 | }
|
---|
464 | return(bResult);
|
---|
465 | }
|
---|
466 |
|
---|
467 | BOOL EventHandlerRoutine(DWORD dwCtrlType)
|
---|
468 | {
|
---|
469 | /* This routine dosn't seem to get called all the time, Why ??? */
|
---|
470 | switch (dwCtrlType)
|
---|
471 | {
|
---|
472 | case CTRL_C_EVENT: /* A CTRL+C signal was received, either from keyboard input or from a signal generated by the GenerateConsoleCtrlEvent function.*/
|
---|
473 | case CTRL_BREAK_EVENT: /* A CTRL+BREAK signal was received, either from keyboard input or from a signal generated by GenerateConsoleCtrlEvent.*/
|
---|
474 | case CTRL_CLOSE_EVENT: /* A signal that the system sends to all processes attached to a console when the user closes the console (either by choosing the Close command from the console window's System menu, or by choosing the End Task command from the Task List).*/
|
---|
475 | case CTRL_LOGOFF_EVENT: /* A signal that the system sends to all console processes when a user is logging off. This signal does not indicate which user is logging off, so no assumptions can be made.*/
|
---|
476 | case CTRL_SHUTDOWN_EVENT: /* A signal that the system sends to all console processes when the system */
|
---|
477 | /* We are basically shutting down, so call Service_Delete */
|
---|
478 | Service_Delete();
|
---|
479 | return(FALSE);
|
---|
480 | break;
|
---|
481 |
|
---|
482 | default:
|
---|
483 | /* we are not handling this one, so return FALSE */
|
---|
484 | return(FALSE);
|
---|
485 | }
|
---|
486 | }
|
---|
487 | #endif
|
---|