2 * Copyright 2000, International Business Machines Corporation and others.
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
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.
16 #include <afs/param.h>
28 #include <WINNT/afsevent.h>
29 #include <WINNT/afsreg.h>
30 #include <afs/procmgmt.h>
31 #include <afs/dirpath.h>
32 #include <afs/bnode.h>
33 #include <afs/afsicf.h>
37 #define BOSSERVER_STARTMSG_EXE "afslegal.exe"
39 #define BOSSERVER_RESTART_ARG_MAX 2 /* "-noauth", "-log" */
40 #define BOSSERVER_WAIT_TIME_HINT 60 /* seconds */
41 #define BOSSERVER_STOP_TIME_MAX (FSSDTIME + 60) /* seconds */
43 #define BOS_CONTROLS_ACCEPTED SERVICE_ACCEPT_STOP
45 static CRITICAL_SECTION bosCtlStatusLock; /* protects bosCtlStatus */
46 static SERVICE_STATUS bosCtlStatus;
47 static SERVICE_STATUS_HANDLE bosCtlStatusHandle;
49 /* note: events arranged in priority order in case multiple signaled */
50 #define BOS_STOP_EVENT 0
51 #define BOS_EXIT_EVENT 1
52 #define BOS_EVENT_COUNT 2
53 static HANDLE bosCtlEvent[BOS_EVENT_COUNT];
56 /* Declare local functions */
58 static void AsyncSignalCatcher(int signo);
60 static void BosCtlStatusInit(DWORD initState);
62 static DWORD BosCtlStatusUpdate(DWORD newState,
66 static DWORD BosCtlStatusReport(void);
68 static void WINAPI BosCtlHandler(DWORD controlCode);
70 static void WINAPI BosCtlMain(DWORD argc,
73 static void BosserverDoStopEvent(pid_t cpid,
77 static void BosserverDoExitEvent(pid_t cpid,
84 static void BosserverRun(DWORD argc,
89 static void BosserverStartupMsgDisplay(void);
95 * AsyncSignalCatcher() -- Handle asynchronous signals sent to process
98 AsyncSignalCatcher(int signo)
100 if (signo == SIGCHLD) {
101 (void) SetEvent(bosCtlEvent[BOS_EXIT_EVENT]);
107 * BosCtlStatusInit() -- initialize BOS control service status structure
110 BosCtlStatusInit(DWORD initState)
112 bosCtlStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
113 bosCtlStatus.dwCurrentState = initState;
115 if (initState == SERVICE_RUNNING) {
116 bosCtlStatus.dwControlsAccepted = BOS_CONTROLS_ACCEPTED;
118 bosCtlStatus.dwControlsAccepted = 0;
121 bosCtlStatus.dwWin32ExitCode = 0;
122 bosCtlStatus.dwServiceSpecificExitCode = 0;
123 bosCtlStatus.dwCheckPoint = 0;
124 bosCtlStatus.dwWaitHint = BOSSERVER_WAIT_TIME_HINT * 1000; /* millisecs */
126 InitializeCriticalSection(&bosCtlStatusLock);
131 * BosCtlStatusUpdate() -- update BOS control service status and report to SCM
134 BosCtlStatusUpdate(DWORD newState, DWORD exitCode, BOOL isWin32Code)
138 EnterCriticalSection(&bosCtlStatusLock);
140 /* SERVICE_STOPPED is a terminal state; never transition out of it */
141 if (bosCtlStatus.dwCurrentState != SERVICE_STOPPED) {
143 if ((bosCtlStatus.dwCurrentState == newState) &&
144 (newState == SERVICE_START_PENDING ||
145 newState == SERVICE_STOP_PENDING)) {
146 /* continuing a pending state; increment checkpoint value */
147 bosCtlStatus.dwCheckPoint++;
149 /* not continuing a pending state; reset checkpoint value */
150 bosCtlStatus.dwCheckPoint = 0;
153 bosCtlStatus.dwCurrentState = newState;
155 if (newState == SERVICE_RUNNING) {
156 bosCtlStatus.dwControlsAccepted = BOS_CONTROLS_ACCEPTED;
158 bosCtlStatus.dwControlsAccepted = 0;
162 bosCtlStatus.dwWin32ExitCode = exitCode;
163 bosCtlStatus.dwServiceSpecificExitCode = 0;
165 bosCtlStatus.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;
166 bosCtlStatus.dwServiceSpecificExitCode = exitCode;
170 if (!SetServiceStatus(bosCtlStatusHandle, &bosCtlStatus)) {
174 LeaveCriticalSection(&bosCtlStatusLock);
181 * BosCtlStatusReport() -- report current BOS control service status to SCM
184 BosCtlStatusReport(void)
188 EnterCriticalSection(&bosCtlStatusLock);
190 if (!SetServiceStatus(bosCtlStatusHandle, &bosCtlStatus)) {
194 LeaveCriticalSection(&bosCtlStatusLock);
201 * BosCtlHandler() -- control handler for BOS control service
204 BosCtlHandler(DWORD controlCode)
206 switch (controlCode) {
207 case SERVICE_CONTROL_STOP:
208 (void) SetEvent(bosCtlEvent[BOS_STOP_EVENT]);
209 (void) BosCtlStatusUpdate(SERVICE_STOP_PENDING, 0, TRUE);
213 (void) BosCtlStatusReport();
220 * BosCtlMain() -- main function for BOS control service
223 BosCtlMain(DWORD argc, LPTSTR *argv)
227 struct sigaction childAction;
229 /* Initialize status structure */
230 BosCtlStatusInit(SERVICE_START_PENDING);
232 /* Create events used by service control handler and signal handler */
233 if ((bosCtlEvent[BOS_STOP_EVENT] = CreateEvent(NULL,
234 FALSE /* manual reset */,
235 FALSE /* initial state */,
236 TEXT("BosCtlSvc Stop Event"))) == NULL) {
237 status = GetLastError();
240 if ((bosCtlEvent[BOS_EXIT_EVENT] = CreateEvent(NULL,
241 FALSE /* manual reset */,
242 FALSE /* initial state */,
243 TEXT("BosCtlSvc Exit Event"))) == NULL) {
244 status = GetLastError();
247 /* Register service control handler */
248 bosCtlStatusHandle = RegisterServiceCtrlHandler(AFSREG_SVR_SVC_NAME,
250 if (bosCtlStatusHandle == 0) {
251 /* failed to register control handler for service; can not continue */
252 (void) ReportErrorEventAlt(AFSEVT_SVR_BCS_HANDLER_REG_FAILED,
253 (int)GetLastError(), NULL);
254 /* can not report status to SCM w/o a valid bosCtlStatusHandle */
258 /* Stop immediately if required system resources could not be obtained */
259 if (bosCtlEvent[BOS_STOP_EVENT] == NULL ||
260 bosCtlEvent[BOS_EXIT_EVENT] == NULL) {
261 (void) ReportErrorEventAlt(AFSEVT_SVR_BCS_INSUFFICIENT_RESOURCES,
263 (void) BosCtlStatusUpdate(SERVICE_STOPPED, status, TRUE);
267 /* Report pending start status */
268 if (status = BosCtlStatusUpdate(SERVICE_START_PENDING, 0, TRUE)) {
269 /* can't inform SCM of pending start; give up before really start */
270 (void) ReportErrorEventAlt(AFSEVT_SVR_BCS_SCM_COMM_FAILED,
272 (void) BosCtlStatusUpdate(SERVICE_STOPPED, status, TRUE);
276 /* For XP SP2 and above, open required ports */
277 icf_CheckAndAddAFSPorts(AFS_PORTSET_SERVER);
279 /* Initialize the dirpath package so can access local bosserver binary */
280 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
281 /* sw install directory probably not in registry; can not continue */
282 (void) ReportErrorEventAlt(AFSEVT_SVR_BCS_NO_INSTALL_DIR, 0, NULL);
283 (void) BosCtlStatusUpdate(SERVICE_STOPPED, 0, TRUE);
287 /* Install SIGCHLD handler to catch bosserver restarts and failures */
288 childAction.sa_handler = AsyncSignalCatcher;
289 sigfillset(&childAction.sa_mask);
290 childAction.sa_flags = 0;
292 if (sigaction(SIGCHLD, &childAction, NULL)) {
293 /* handler install should never fail, but can't continue if it does */
295 (void) ReportErrorEventAlt(AFSEVT_SVR_BCS_INTERNAL_ERROR,
297 (void) BosCtlStatusUpdate(SERVICE_STOPPED, status, FALSE);
301 /* Run the AFS bosserver, handling stop and exit events */
302 BosserverRun(argc, argv, &status, &isWin32Code);
304 (void) BosCtlStatusUpdate(SERVICE_STOPPED, status, isWin32Code);
309 * BosserverDoStopEvent() -- Handle a stop event for the AFS bosserver.
312 BosserverDoStopEvent(pid_t cpid, DWORD *stopStatus, BOOL *isWin32Code)
314 (void) BosCtlStatusUpdate(SERVICE_STOP_PENDING, 0, TRUE);
316 if (kill(cpid, SIGQUIT) == 0) {
317 /* bosserver has been told to stop; wait for this to happen */
318 BOOL gotWaitStatus = FALSE;
319 time_t timeStart = time(NULL);
325 if (waitpid(cpid, &waitStatus, WNOHANG) == cpid) {
326 /* bosserver status available */
327 if (WIFEXITED(waitStatus) && WEXITSTATUS(waitStatus) == 0) {
328 /* bosserver exited w/o any error */
332 *stopStatus = waitStatus;
333 *isWin32Code = FALSE;
335 gotWaitStatus = TRUE;
339 /* wait for bosserver status to become available;
340 * update BOS control service status periodically.
342 status = WaitForSingleObject(bosCtlEvent[BOS_EXIT_EVENT],
343 BOSSERVER_WAIT_TIME_HINT * 1000 / 2);
344 if (status == WAIT_FAILED) {
345 /* failed to wait on event; should never happen */
346 Sleep(2000); /* sleep to avoid tight loop if event problem */
348 (void) BosCtlStatusUpdate(SERVICE_STOP_PENDING, 0, TRUE);
349 } while (difftime(time(NULL), timeStart) < BOSSERVER_STOP_TIME_MAX);
351 if (!gotWaitStatus) {
352 /* timed out waiting to get bosserver status */
354 *isWin32Code = FALSE;
356 (void) ReportWarningEventAlt(AFSEVT_SVR_BCS_BOSSERVER_STOP_TIMEOUT,
357 (int)*stopStatus, NULL);
361 /* can't tell bosserver to stop; should never happen */
363 *isWin32Code = FALSE;
365 (void) ReportErrorEventAlt(AFSEVT_SVR_BCS_BOSSERVER_STOP_FAILED,
366 (int)*stopStatus, NULL);
372 * BosserverDoExitEvent() -- Handle an exit event for the AFS bosserver.
374 * The output arguments for this function are set as follows:
375 * Case 1: bosserver did not exit (spurious SIGCHLD);
376 * *doWait is set to TRUE.
377 * Case 2: bosserver exited with restart code;
378 * *doRestart is set to TRUE, restartArgv[] is defined.
379 * Case 3: bosserver exited with non-restart code;
380 * *stopStatus and *isWin32Code are defined.
383 BosserverDoExitEvent(pid_t cpid,
395 if (waitpid(cpid, &waitStatus, WNOHANG) == cpid) {
396 /* bosserver status available */
398 if (WIFEXITED(waitStatus)) {
399 /* bosserver exited normally; check for restart code */
400 int exitCode = WEXITSTATUS(waitStatus);
402 if (BOSEXIT_DORESTART(exitCode)) {
403 /* bosserver requests restart */
407 /* set up bosserver argument list */
408 restartArgv[0] = (char *)AFSDIR_SERVER_BOSVR_FILEPATH;
411 if (exitCode & BOSEXIT_NOAUTH_FLAG) {
412 /* pass "-noauth" to new bosserver */
413 restartArgv[i] = "-noauth";
416 if (exitCode & BOSEXIT_LOGGING_FLAG) {
417 /* pass "-log" to new bosserver */
418 restartArgv[i] = "-log";
421 restartArgv[i] = NULL;
426 /* bosserver exited with non-restart code; set status */
427 *stopStatus = waitStatus;
428 *isWin32Code = FALSE;
430 (void) ReportWarningEventAlt(AFSEVT_SVR_BCS_BOSSERVER_EXIT,
431 (int)*stopStatus, NULL);
435 /* bosserver status NOT available; assume spurious SIGCHLD */
442 * BosserverRun() -- Run the AFS bosserver, handling stop and exit events.
444 * Input args are those passed to the service's main function (BosCtlMain).
445 * Output args are the stop status and status type of the bosserver.
448 BosserverRun(DWORD argc,
454 BOOL doRestart, doWait;
457 /* Display bosserver startup (legal) message; first start only */
458 /* BosserverStartupMsgDisplay(); */
460 /* Set env variable forcing process mgmt lib to spawn processes detached */
461 (void)putenv(PMGT_SPAWN_DETACHED_ENV_NAME "=1");
463 /* Alloc block with room for at least BOSSERVER_RESTART_ARG_MAX args */
464 i = max((argc + 1), (BOSSERVER_RESTART_ARG_MAX + 2));
465 spawn_argv = (char **)malloc(i * sizeof(char *));
467 if (spawn_argv == NULL) {
468 /* failed to malloc required space; can not continue */
469 *stopStatus = ENOMEM;
470 *isWin32Code = FALSE;
472 (void) ReportErrorEventAlt(AFSEVT_SVR_BCS_INSUFFICIENT_RESOURCES,
473 (int)*stopStatus, NULL);
477 /* Set initial bosserver args to those supplied via StartService() */
478 spawn_argv[0] = (char *)AFSDIR_SERVER_BOSVR_FILEPATH;
480 for (i = 1; i < argc; i++) {
481 spawn_argv[i] = argv[i];
483 spawn_argv[i] = NULL;
485 /* Start/restart bosserver and wait for either a stop or exit event */
492 /* restarting bosserver; log informational message */
493 (void) ReportInformationEventAlt(AFSEVT_SVR_BCS_BOSSERVER_RESTART,
498 cpid = spawnprocve(spawn_argv[0], spawn_argv, NULL, 0);
500 if (cpid == (pid_t)-1) {
501 /* failed to start/restart the bosserver process */
503 *isWin32Code = FALSE;
505 (void) ReportErrorEventAlt(AFSEVT_SVR_BCS_BOSSERVER_START_FAILED,
506 (int)*stopStatus, NULL);
510 if (status = BosCtlStatusUpdate(SERVICE_RUNNING, 0, TRUE)) {
511 /* can't tell SCM we're running so quit; should never occur */
512 (void) ReportErrorEventAlt(AFSEVT_SVR_BCS_SCM_COMM_FAILED,
514 (void) SetEvent(bosCtlEvent[BOS_STOP_EVENT]);
517 /* bosserver is running; wait for an event of interest */
519 Sleep(5000); /* bosserver needs time to register signal handler */
524 status = WaitForMultipleObjects(BOS_EVENT_COUNT,
525 bosCtlEvent, FALSE, INFINITE);
527 if ((status - WAIT_OBJECT_0) == BOS_STOP_EVENT) {
528 /* stop event signaled */
529 BosserverDoStopEvent(cpid, stopStatus, isWin32Code);
531 } else if ((status - WAIT_OBJECT_0) == BOS_EXIT_EVENT) {
532 /* exit event signaled; see function comment for outcomes */
533 BosserverDoExitEvent(cpid,
535 &doRestart, spawn_argv,
536 stopStatus, isWin32Code);
539 /* failed to wait on events; should never happen */
540 Sleep(2000); /* sleep to avoid tight loop if event problem */
551 * BosserverStartupMsgDisplay() -- display Windows version of AFS bosserver
552 * startup (legal) message.
555 BosserverStartupMsgDisplay(void)
559 if (!ConstructLocalBinPath(BOSSERVER_STARTMSG_EXE, &msgPath)) {
560 /* Use C runtime spawn; don't need all the machinery in the
561 * process management library.
563 (void)_spawnl(_P_DETACH, msgPath, BOSSERVER_STARTMSG_EXE, NULL);
570 * main() -- start dispatcher thread for BOS control service
574 SERVICE_TABLE_ENTRY dispatchTable[] = {{AFSREG_SVR_SVC_NAME, BosCtlMain},
577 (void) ReportInformationEventAlt(AFSEVT_SVR_BCS_STARTED, NULL);
579 if (!StartServiceCtrlDispatcher(dispatchTable)) {
580 /* unable to connect to SCM */
581 (void) ReportErrorEventAlt(AFSEVT_SVR_BCS_SCM_COMM_FAILED,
582 (int)GetLastError(), NULL);
585 (void) ReportInformationEventAlt(AFSEVT_SVR_BCS_STOPPED, NULL);