Windows: remove trailing whitespace
[openafs.git] / src / WINNT / bosctlsvc / bosctlsvc.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /* This file implements the AFS BOS control service.  Basically, it provides
11  * a mechanism to start and stop the AFS bosserver via the NT SCM; it also
12  * supports bosserver restart.
13  */
14
15
16 #include <afs/param.h>
17 #include <afs/stds.h>
18
19 #include <stddef.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <errno.h>
23 #include <windows.h>
24 #include <time.h>
25 #include <process.h>
26
27 #include <WINNT/afsevent.h>
28 #include <WINNT/afsreg.h>
29 #include <afs/procmgmt.h>
30 #include <afs/dirpath.h>
31 #include <afs/bnode.h>
32 #include <afs/afsicf.h>
33
34 /* Define globals */
35
36 #define BOSSERVER_STARTMSG_EXE  "afslegal.exe"
37
38 #define BOSSERVER_RESTART_ARG_MAX  2  /* "-noauth", "-log" */
39 #define BOSSERVER_WAIT_TIME_HINT  60  /* seconds */
40 #define BOSSERVER_STOP_TIME_MAX  (FSSDTIME + 60)  /* seconds */
41
42 #define BOS_CONTROLS_ACCEPTED  SERVICE_ACCEPT_STOP
43
44 static CRITICAL_SECTION bosCtlStatusLock;  /* protects bosCtlStatus */
45 static SERVICE_STATUS bosCtlStatus;
46 static SERVICE_STATUS_HANDLE bosCtlStatusHandle;
47
48 /* note: events arranged in priority order in case multiple signaled */
49 #define BOS_STOP_EVENT 0
50 #define BOS_EXIT_EVENT 1
51 #define BOS_EVENT_COUNT 2
52 static HANDLE bosCtlEvent[BOS_EVENT_COUNT];
53
54
55 /* Declare local functions */
56
57 static void AsyncSignalCatcher(int signo);
58
59 static void BosCtlStatusInit(DWORD initState);
60
61 static DWORD BosCtlStatusUpdate(DWORD newState,
62                                 DWORD exitCode,
63                                 BOOL isWin32Code);
64
65 static DWORD BosCtlStatusReport(void);
66
67 static void WINAPI BosCtlHandler(DWORD controlCode);
68
69 static void WINAPI BosCtlMain(DWORD argc,
70                               LPTSTR *argv);
71
72 static void BosserverDoStopEvent(pid_t cpid,
73                                  DWORD *stopStatus,
74                                  BOOL *isWin32Code);
75
76 static void BosserverDoExitEvent(pid_t cpid,
77                                  BOOL *doWait,
78                                  BOOL *doRestart,
79                                  char **restartArgv,
80                                  DWORD *stopStatus,
81                                  BOOL *isWin32Code);
82
83 static void BosserverRun(DWORD argc,
84                          LPTSTR *argv,
85                          DWORD *stopStatus,
86                          BOOL *isWin32Code);
87
88 static void BosserverStartupMsgDisplay(void);
89
90
91
92
93 /*
94  * AsyncSignalCatcher() -- Handle asynchronous signals sent to process
95  */
96 static void
97 AsyncSignalCatcher(int signo)
98 {
99     if (signo == SIGCHLD) {
100         (void) SetEvent(bosCtlEvent[BOS_EXIT_EVENT]);
101     }
102 }
103
104
105 /*
106  * BosCtlStatusInit() -- initialize BOS control service status structure
107  */
108 static void
109 BosCtlStatusInit(DWORD initState)
110 {
111     bosCtlStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
112     bosCtlStatus.dwCurrentState = initState;
113
114     if (initState == SERVICE_RUNNING) {
115         bosCtlStatus.dwControlsAccepted = BOS_CONTROLS_ACCEPTED;
116     } else {
117         bosCtlStatus.dwControlsAccepted = 0;
118     }
119
120     bosCtlStatus.dwWin32ExitCode = 0;
121     bosCtlStatus.dwServiceSpecificExitCode = 0;
122     bosCtlStatus.dwCheckPoint = 0;
123     bosCtlStatus.dwWaitHint = BOSSERVER_WAIT_TIME_HINT * 1000; /* millisecs */
124
125     InitializeCriticalSection(&bosCtlStatusLock);
126 }
127
128
129 /*
130  * BosCtlStatusUpdate() -- update BOS control service status and report to SCM
131  */
132 static DWORD
133 BosCtlStatusUpdate(DWORD newState, DWORD exitCode, BOOL isWin32Code)
134 {
135     DWORD rc = 0;
136
137     EnterCriticalSection(&bosCtlStatusLock);
138
139     /* SERVICE_STOPPED is a terminal state; never transition out of it */
140     if (bosCtlStatus.dwCurrentState != SERVICE_STOPPED) {
141
142         if ((bosCtlStatus.dwCurrentState == newState) &&
143             (newState == SERVICE_START_PENDING ||
144              newState == SERVICE_STOP_PENDING)) {
145             /* continuing a pending state; increment checkpoint value */
146             bosCtlStatus.dwCheckPoint++;
147         } else {
148             /* not continuing a pending state; reset checkpoint value */
149             bosCtlStatus.dwCheckPoint = 0;
150         }
151
152         bosCtlStatus.dwCurrentState = newState;
153
154         if (newState == SERVICE_RUNNING) {
155             bosCtlStatus.dwControlsAccepted = BOS_CONTROLS_ACCEPTED;
156         } else {
157             bosCtlStatus.dwControlsAccepted = 0;
158         }
159
160         if (isWin32Code) {
161             bosCtlStatus.dwWin32ExitCode = exitCode;
162             bosCtlStatus.dwServiceSpecificExitCode = 0;
163         } else {
164             bosCtlStatus.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;
165             bosCtlStatus.dwServiceSpecificExitCode = exitCode;
166         }
167     }
168
169     if (!SetServiceStatus(bosCtlStatusHandle, &bosCtlStatus)) {
170         rc = GetLastError();
171     }
172
173     LeaveCriticalSection(&bosCtlStatusLock);
174
175     return rc;
176 }
177
178
179 /*
180  * BosCtlStatusReport() -- report current BOS control service status to SCM
181  */
182 static DWORD
183 BosCtlStatusReport(void)
184 {
185     DWORD rc = 0;
186
187     EnterCriticalSection(&bosCtlStatusLock);
188
189     if (!SetServiceStatus(bosCtlStatusHandle, &bosCtlStatus)) {
190         rc = GetLastError();
191     }
192
193     LeaveCriticalSection(&bosCtlStatusLock);
194
195     return rc;
196 }
197
198
199 /*
200  * BosCtlHandler() -- control handler for BOS control service
201  */
202 static void WINAPI
203 BosCtlHandler(DWORD controlCode)
204 {
205     switch (controlCode) {
206       case SERVICE_CONTROL_STOP:
207         (void) SetEvent(bosCtlEvent[BOS_STOP_EVENT]);
208         (void) BosCtlStatusUpdate(SERVICE_STOP_PENDING, 0, TRUE);
209         break;
210
211       default:
212         (void) BosCtlStatusReport();
213         break;
214     }
215 }
216
217
218 /*
219  * BosCtlMain() -- main function for BOS control service
220  */
221 static void WINAPI
222 BosCtlMain(DWORD argc, LPTSTR *argv)
223 {
224     DWORD status;
225     BOOL isWin32Code;
226     struct sigaction childAction;
227
228     /* Initialize status structure */
229     BosCtlStatusInit(SERVICE_START_PENDING);
230
231     /* Create events used by service control handler and signal handler */
232     if ((bosCtlEvent[BOS_STOP_EVENT] = CreateEvent(NULL,
233                                                    FALSE /* manual reset */,
234                                                    FALSE /* initial state */,
235                                                    TEXT("BosCtlSvc Stop Event"))) == NULL) {
236         status = GetLastError();
237     }
238
239     if ((bosCtlEvent[BOS_EXIT_EVENT] = CreateEvent(NULL,
240                                                    FALSE /* manual reset */,
241                                                    FALSE /* initial state */,
242                                                    TEXT("BosCtlSvc Exit Event"))) == NULL) {
243         status = GetLastError();
244     }
245
246     /* Register service control handler */
247     bosCtlStatusHandle = RegisterServiceCtrlHandler(AFSREG_SVR_SVC_NAME,
248                                                     BosCtlHandler);
249     if (bosCtlStatusHandle == 0) {
250         /* failed to register control handler for service; can not continue */
251         (void) ReportErrorEventAlt(AFSEVT_SVR_BCS_HANDLER_REG_FAILED,
252                                    (int)GetLastError(), NULL);
253         /* can not report status to SCM w/o a valid bosCtlStatusHandle */
254         return;
255     }
256
257     /* Stop immediately if required system resources could not be obtained */
258     if (bosCtlEvent[BOS_STOP_EVENT] == NULL ||
259         bosCtlEvent[BOS_EXIT_EVENT] == NULL) {
260         (void) ReportErrorEventAlt(AFSEVT_SVR_BCS_INSUFFICIENT_RESOURCES,
261                                    (int)status, NULL);
262         (void) BosCtlStatusUpdate(SERVICE_STOPPED, status, TRUE);
263         return;
264     }
265
266     /* Report pending start status */
267     if (status = BosCtlStatusUpdate(SERVICE_START_PENDING, 0, TRUE)) {
268         /* can't inform SCM of pending start; give up before really start */
269         (void) ReportErrorEventAlt(AFSEVT_SVR_BCS_SCM_COMM_FAILED,
270                                    (int)status, NULL);
271         (void) BosCtlStatusUpdate(SERVICE_STOPPED, status, TRUE);
272         return;
273     }
274
275     /* For XP SP2 and above, open required ports */
276     icf_CheckAndAddAFSPorts(AFS_PORTSET_SERVER);
277
278     /* Initialize the dirpath package so can access local bosserver binary */
279     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
280         /* sw install directory probably not in registry; can not continue */
281         (void) ReportErrorEventAlt(AFSEVT_SVR_BCS_NO_INSTALL_DIR, 0, NULL);
282         (void) BosCtlStatusUpdate(SERVICE_STOPPED, 0, TRUE);
283         return;
284     }
285
286     /* Install SIGCHLD handler to catch bosserver restarts and failures */
287     childAction.sa_handler = AsyncSignalCatcher;
288     sigfillset(&childAction.sa_mask);
289     childAction.sa_flags = 0;
290
291     if (sigaction(SIGCHLD, &childAction, NULL)) {
292         /* handler install should never fail, but can't continue if it does */
293         status = errno;
294         (void) ReportErrorEventAlt(AFSEVT_SVR_BCS_INTERNAL_ERROR,
295                                    (int)status, NULL);
296         (void) BosCtlStatusUpdate(SERVICE_STOPPED, status, FALSE);
297         return;
298     }
299
300     /* Run the AFS bosserver, handling stop and exit events */
301     BosserverRun(argc, argv, &status, &isWin32Code);
302
303     (void) BosCtlStatusUpdate(SERVICE_STOPPED, status, isWin32Code);
304 }
305
306
307 /*
308  * BosserverDoStopEvent() -- Handle a stop event for the AFS bosserver.
309  */
310 static void
311 BosserverDoStopEvent(pid_t cpid, DWORD *stopStatus, BOOL *isWin32Code)
312 {
313     (void) BosCtlStatusUpdate(SERVICE_STOP_PENDING, 0, TRUE);
314
315     if (kill(cpid, SIGQUIT) == 0) {
316         /* bosserver has been told to stop; wait for this to happen */
317         BOOL gotWaitStatus = FALSE;
318         time_t timeStart = time(NULL);
319
320         do {
321             int waitStatus;
322             DWORD status;
323
324             if (waitpid(cpid, &waitStatus, WNOHANG) == cpid) {
325                 /* bosserver status available */
326                 if (WIFEXITED(waitStatus) && WEXITSTATUS(waitStatus) == 0) {
327                     /* bosserver exited w/o any error */
328                     *stopStatus = 0;
329                     *isWin32Code = TRUE;
330                 } else {
331                     *stopStatus = waitStatus;
332                     *isWin32Code = FALSE;
333                 }
334                 gotWaitStatus = TRUE;
335                 break;
336             }
337
338             /* wait for bosserver status to become available;
339              * update BOS control service status periodically.
340              */
341             status = WaitForSingleObject(bosCtlEvent[BOS_EXIT_EVENT],
342                                          BOSSERVER_WAIT_TIME_HINT * 1000 / 2);
343             if (status == WAIT_FAILED) {
344                 /* failed to wait on event; should never happen */
345                 Sleep(2000);  /* sleep to avoid tight loop if event problem */
346             }
347             (void) BosCtlStatusUpdate(SERVICE_STOP_PENDING, 0, TRUE);
348         } while (difftime(time(NULL), timeStart) < BOSSERVER_STOP_TIME_MAX);
349
350         if (!gotWaitStatus) {
351             /* timed out waiting to get bosserver status */
352             *stopStatus = EBUSY;
353             *isWin32Code = FALSE;
354
355             (void) ReportWarningEventAlt(AFSEVT_SVR_BCS_BOSSERVER_STOP_TIMEOUT,
356                                          (int)*stopStatus, NULL);
357         }
358
359     } else {
360         /* can't tell bosserver to stop; should never happen */
361         *stopStatus = errno;
362         *isWin32Code = FALSE;
363
364         (void) ReportErrorEventAlt(AFSEVT_SVR_BCS_BOSSERVER_STOP_FAILED,
365                                    (int)*stopStatus, NULL);
366     }
367 }
368
369
370 /*
371  * BosserverDoExitEvent() -- Handle an exit event for the AFS bosserver.
372  *
373  *     The output arguments for this function are set as follows:
374  *         Case 1: bosserver did not exit (spurious SIGCHLD);
375  *                 *doWait is set to TRUE.
376  *         Case 2: bosserver exited with restart code;
377  *                 *doRestart is set to TRUE, restartArgv[] is defined.
378  *         Case 3: bosserver exited with non-restart code;
379  *                 *stopStatus and *isWin32Code are defined.
380  */
381 static void
382 BosserverDoExitEvent(pid_t cpid,
383                      BOOL *doWait,
384                      BOOL *doRestart,
385                      char **restartArgv,
386                      DWORD *stopStatus,
387                      BOOL *isWin32Code)
388 {
389     int waitStatus;
390
391     *doWait = FALSE;
392     *doRestart = FALSE;
393
394     if (waitpid(cpid, &waitStatus, WNOHANG) == cpid) {
395         /* bosserver status available */
396
397         if (WIFEXITED(waitStatus)) {
398             /* bosserver exited normally; check for restart code */
399             int exitCode = WEXITSTATUS(waitStatus);
400
401             if (BOSEXIT_DORESTART(exitCode)) {
402                 /* bosserver requests restart */
403                 int i;
404                 *doRestart = TRUE;
405
406                 /* set up bosserver argument list */
407                 restartArgv[0] = (char *)AFSDIR_SERVER_BOSVR_FILEPATH;
408                 i = 1;
409
410                 if (exitCode & BOSEXIT_NOAUTH_FLAG) {
411                     /* pass "-noauth" to new bosserver */
412                     restartArgv[i] = "-noauth";
413                     i++;
414                 }
415                 if (exitCode & BOSEXIT_LOGGING_FLAG) {
416                     /* pass "-log" to new bosserver */
417                     restartArgv[i] = "-log";
418                     i++;
419                 }
420                 restartArgv[i] = NULL;
421             }
422         }
423
424         if (!(*doRestart)) {
425             /* bosserver exited with non-restart code; set status */
426             *stopStatus = waitStatus;
427             *isWin32Code = FALSE;
428
429             (void) ReportWarningEventAlt(AFSEVT_SVR_BCS_BOSSERVER_EXIT,
430                                          (int)*stopStatus, NULL);
431         }
432
433     } else {
434         /* bosserver status NOT available; assume spurious SIGCHLD */
435         *doWait = TRUE;
436     }
437 }
438
439
440 /*
441  * BosserverRun() -- Run the AFS bosserver, handling stop and exit events.
442  *
443  *     Input args are those passed to the service's main function (BosCtlMain).
444  *     Output args are the stop status and status type of the bosserver.
445  */
446 static void
447 BosserverRun(DWORD argc,
448              LPTSTR *argv,
449              DWORD *stopStatus,
450              BOOL *isWin32Code)
451 {
452     DWORD status, i;
453     BOOL doRestart, doWait;
454     char **spawn_argv;
455
456     /* Display bosserver startup (legal) message; first start only */
457     /* BosserverStartupMsgDisplay(); */
458
459     /* Set env variable forcing process mgmt lib to spawn processes detached */
460     (void)putenv(PMGT_SPAWN_DETACHED_ENV_NAME "=1");
461
462     /* Alloc block with room for at least BOSSERVER_RESTART_ARG_MAX args */
463     i = max((argc + 1), (BOSSERVER_RESTART_ARG_MAX + 2));
464     spawn_argv = (char **)malloc(i * sizeof(char *));
465
466     if (spawn_argv == NULL) {
467         /* failed to malloc required space; can not continue */
468         *stopStatus = ENOMEM;
469         *isWin32Code = FALSE;
470
471         (void) ReportErrorEventAlt(AFSEVT_SVR_BCS_INSUFFICIENT_RESOURCES,
472                                    (int)*stopStatus, NULL);
473         return;
474     }
475
476     /* Set initial bosserver args to those supplied via StartService() */
477     spawn_argv[0] = (char *)AFSDIR_SERVER_BOSVR_FILEPATH;
478
479     for (i = 1; i < argc; i++) {
480         spawn_argv[i] = argv[i];
481     }
482     spawn_argv[i] = NULL;
483
484     /* Start/restart bosserver and wait for either a stop or exit event */
485     doRestart = FALSE;
486
487     do {
488         pid_t cpid;
489
490         if (doRestart) {
491             /* restarting bosserver; log informational message */
492             (void) ReportInformationEventAlt(AFSEVT_SVR_BCS_BOSSERVER_RESTART,
493                                              NULL);
494             doRestart = FALSE;
495         }
496
497         cpid = spawnprocve(spawn_argv[0], spawn_argv, NULL, 0);
498
499         if (cpid == (pid_t)-1) {
500             /* failed to start/restart the bosserver process */
501             *stopStatus = errno;
502             *isWin32Code = FALSE;
503
504             (void) ReportErrorEventAlt(AFSEVT_SVR_BCS_BOSSERVER_START_FAILED,
505                                        (int)*stopStatus, NULL);
506             break;
507         }
508
509         if (status = BosCtlStatusUpdate(SERVICE_RUNNING, 0, TRUE)) {
510             /* can't tell SCM we're running so quit; should never occur */
511             (void) ReportErrorEventAlt(AFSEVT_SVR_BCS_SCM_COMM_FAILED,
512                                        (int)status, NULL);
513             (void) SetEvent(bosCtlEvent[BOS_STOP_EVENT]);
514         }
515
516         /* bosserver is running; wait for an event of interest */
517
518         Sleep(5000);  /* bosserver needs time to register signal handler */
519
520         do {
521             doWait = FALSE;
522
523             status = WaitForMultipleObjects(BOS_EVENT_COUNT,
524                                             bosCtlEvent, FALSE, INFINITE);
525
526             if ((status - WAIT_OBJECT_0) == BOS_STOP_EVENT) {
527                 /* stop event signaled */
528                 BosserverDoStopEvent(cpid, stopStatus, isWin32Code);
529
530             } else if ((status - WAIT_OBJECT_0) == BOS_EXIT_EVENT) {
531                 /* exit event signaled; see function comment for outcomes */
532                 BosserverDoExitEvent(cpid,
533                                      &doWait,
534                                      &doRestart, spawn_argv,
535                                      stopStatus, isWin32Code);
536
537             } else {
538                 /* failed to wait on events; should never happen */
539                 Sleep(2000);  /* sleep to avoid tight loop if event problem */
540                 doWait = TRUE;
541             }
542         } while (doWait);
543     } while(doRestart);
544
545     return;
546 }
547
548
549 /*
550  * BosserverStartupMsgDisplay() -- display Windows version of AFS bosserver
551  *     startup (legal) message.
552  */
553 static void
554 BosserverStartupMsgDisplay(void)
555 {
556     char *msgPath;
557
558     if (!ConstructLocalBinPath(BOSSERVER_STARTMSG_EXE, &msgPath)) {
559         /* Use C runtime spawn; don't need all the machinery in the
560          * process management library.
561          */
562         (void)_spawnl(_P_DETACH, msgPath, BOSSERVER_STARTMSG_EXE, NULL);
563         free(msgPath);
564     }
565 }
566
567
568 /*
569  * main() -- start dispatcher thread for BOS control service
570  */
571 int main(void)
572 {
573     SERVICE_TABLE_ENTRY dispatchTable[] = {{AFSREG_SVR_SVC_NAME, BosCtlMain},
574                                            {NULL, NULL}};
575
576     (void) ReportInformationEventAlt(AFSEVT_SVR_BCS_STARTED, NULL);
577
578     if (!StartServiceCtrlDispatcher(dispatchTable)) {
579         /* unable to connect to SCM */
580         (void) ReportErrorEventAlt(AFSEVT_SVR_BCS_SCM_COMM_FAILED,
581                                    (int)GetLastError(), NULL);
582     }
583
584     (void) ReportInformationEventAlt(AFSEVT_SVR_BCS_STOPPED, NULL);
585     return 0;
586 }