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>
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 /* Initialize the dirpath package so can access local bosserver binary */
277 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
278 /* sw install directory probably not in registry; can not continue */
279 (void) ReportErrorEventAlt(AFSEVT_SVR_BCS_NO_INSTALL_DIR, 0, NULL);
280 (void) BosCtlStatusUpdate(SERVICE_STOPPED, 0, TRUE);
284 /* Install SIGCHLD handler to catch bosserver restarts and failures */
285 childAction.sa_handler = AsyncSignalCatcher;
286 sigfillset(&childAction.sa_mask);
287 childAction.sa_flags = 0;
289 if (sigaction(SIGCHLD, &childAction, NULL)) {
290 /* handler install should never fail, but can't continue if it does */
292 (void) ReportErrorEventAlt(AFSEVT_SVR_BCS_INTERNAL_ERROR,
294 (void) BosCtlStatusUpdate(SERVICE_STOPPED, status, FALSE);
298 /* Run the AFS bosserver, handling stop and exit events */
299 BosserverRun(argc, argv, &status, &isWin32Code);
301 (void) BosCtlStatusUpdate(SERVICE_STOPPED, status, isWin32Code);
306 * BosserverDoStopEvent() -- Handle a stop event for the AFS bosserver.
309 BosserverDoStopEvent(pid_t cpid, DWORD *stopStatus, BOOL *isWin32Code)
311 (void) BosCtlStatusUpdate(SERVICE_STOP_PENDING, 0, TRUE);
313 if (kill(cpid, SIGQUIT) == 0) {
314 /* bosserver has been told to stop; wait for this to happen */
315 BOOL gotWaitStatus = FALSE;
316 time_t timeStart = time(NULL);
322 if (waitpid(cpid, &waitStatus, WNOHANG) == cpid) {
323 /* bosserver status available */
324 if (WIFEXITED(waitStatus) && WEXITSTATUS(waitStatus) == 0) {
325 /* bosserver exited w/o any error */
329 *stopStatus = waitStatus;
330 *isWin32Code = FALSE;
332 gotWaitStatus = TRUE;
336 /* wait for bosserver status to become available;
337 * update BOS control service status periodically.
339 status = WaitForSingleObject(bosCtlEvent[BOS_EXIT_EVENT],
340 BOSSERVER_WAIT_TIME_HINT * 1000 / 2);
341 if (status == WAIT_FAILED) {
342 /* failed to wait on event; should never happen */
343 Sleep(2000); /* sleep to avoid tight loop if event problem */
345 (void) BosCtlStatusUpdate(SERVICE_STOP_PENDING, 0, TRUE);
346 } while (difftime(time(NULL), timeStart) < BOSSERVER_STOP_TIME_MAX);
348 if (!gotWaitStatus) {
349 /* timed out waiting to get bosserver status */
351 *isWin32Code = FALSE;
353 (void) ReportWarningEventAlt(AFSEVT_SVR_BCS_BOSSERVER_STOP_TIMEOUT,
354 (int)*stopStatus, NULL);
358 /* can't tell bosserver to stop; should never happen */
360 *isWin32Code = FALSE;
362 (void) ReportErrorEventAlt(AFSEVT_SVR_BCS_BOSSERVER_STOP_FAILED,
363 (int)*stopStatus, NULL);
369 * BosserverDoExitEvent() -- Handle an exit event for the AFS bosserver.
371 * The output arguments for this function are set as follows:
372 * Case 1: bosserver did not exit (spurious SIGCHLD);
373 * *doWait is set to TRUE.
374 * Case 2: bosserver exited with restart code;
375 * *doRestart is set to TRUE, restartArgv[] is defined.
376 * Case 3: bosserver exited with non-restart code;
377 * *stopStatus and *isWin32Code are defined.
380 BosserverDoExitEvent(pid_t cpid,
392 if (waitpid(cpid, &waitStatus, WNOHANG) == cpid) {
393 /* bosserver status available */
395 if (WIFEXITED(waitStatus)) {
396 /* bosserver exited normally; check for restart code */
397 int exitCode = WEXITSTATUS(waitStatus);
399 if (BOSEXIT_DORESTART(exitCode)) {
400 /* bosserver requests restart */
404 /* set up bosserver argument list */
405 restartArgv[0] = (char *)AFSDIR_SERVER_BOSVR_FILEPATH;
408 if (exitCode & BOSEXIT_NOAUTH_FLAG) {
409 /* pass "-noauth" to new bosserver */
410 restartArgv[i] = "-noauth";
413 if (exitCode & BOSEXIT_LOGGING_FLAG) {
414 /* pass "-log" to new bosserver */
415 restartArgv[i] = "-log";
418 restartArgv[i] = NULL;
423 /* bosserver exited with non-restart code; set status */
424 *stopStatus = waitStatus;
425 *isWin32Code = FALSE;
427 (void) ReportWarningEventAlt(AFSEVT_SVR_BCS_BOSSERVER_EXIT,
428 (int)*stopStatus, NULL);
432 /* bosserver status NOT available; assume spurious SIGCHLD */
439 * BosserverRun() -- Run the AFS bosserver, handling stop and exit events.
441 * Input args are those passed to the service's main function (BosCtlMain).
442 * Output args are the stop status and status type of the bosserver.
445 BosserverRun(DWORD argc,
451 BOOL doRestart, doWait;
454 /* Display bosserver startup (legal) message; first start only */
455 /* BosserverStartupMsgDisplay(); */
457 /* Set env variable forcing process mgmt lib to spawn processes detached */
458 (void)putenv(PMGT_SPAWN_DETACHED_ENV_NAME "=1");
460 /* Alloc block with room for at least BOSSERVER_RESTART_ARG_MAX args */
461 i = max((argc + 1), (BOSSERVER_RESTART_ARG_MAX + 2));
462 spawn_argv = (char **)malloc(i * sizeof(char *));
464 if (spawn_argv == NULL) {
465 /* failed to malloc required space; can not continue */
466 *stopStatus = ENOMEM;
467 *isWin32Code = FALSE;
469 (void) ReportErrorEventAlt(AFSEVT_SVR_BCS_INSUFFICIENT_RESOURCES,
470 (int)*stopStatus, NULL);
474 /* Set initial bosserver args to those supplied via StartService() */
475 spawn_argv[0] = (char *)AFSDIR_SERVER_BOSVR_FILEPATH;
477 for (i = 1; i < argc; i++) {
478 spawn_argv[i] = argv[i];
480 spawn_argv[i] = NULL;
482 /* Start/restart bosserver and wait for either a stop or exit event */
489 /* restarting bosserver; log informational message */
490 (void) ReportInformationEventAlt(AFSEVT_SVR_BCS_BOSSERVER_RESTART,
495 cpid = spawnprocve(spawn_argv[0], spawn_argv, NULL, 0);
497 if (cpid == (pid_t)-1) {
498 /* failed to start/restart the bosserver process */
500 *isWin32Code = FALSE;
502 (void) ReportErrorEventAlt(AFSEVT_SVR_BCS_BOSSERVER_START_FAILED,
503 (int)*stopStatus, NULL);
507 if (status = BosCtlStatusUpdate(SERVICE_RUNNING, 0, TRUE)) {
508 /* can't tell SCM we're running so quit; should never occur */
509 (void) ReportErrorEventAlt(AFSEVT_SVR_BCS_SCM_COMM_FAILED,
511 (void) SetEvent(bosCtlEvent[BOS_STOP_EVENT]);
514 /* bosserver is running; wait for an event of interest */
516 Sleep(5000); /* bosserver needs time to register signal handler */
521 status = WaitForMultipleObjects(BOS_EVENT_COUNT,
522 bosCtlEvent, FALSE, INFINITE);
524 if ((status - WAIT_OBJECT_0) == BOS_STOP_EVENT) {
525 /* stop event signaled */
526 BosserverDoStopEvent(cpid, stopStatus, isWin32Code);
528 } else if ((status - WAIT_OBJECT_0) == BOS_EXIT_EVENT) {
529 /* exit event signaled; see function comment for outcomes */
530 BosserverDoExitEvent(cpid,
532 &doRestart, spawn_argv,
533 stopStatus, isWin32Code);
536 /* failed to wait on events; should never happen */
537 Sleep(2000); /* sleep to avoid tight loop if event problem */
548 * BosserverStartupMsgDisplay() -- display Windows version of AFS bosserver
549 * startup (legal) message.
552 BosserverStartupMsgDisplay(void)
556 if (!ConstructLocalBinPath(BOSSERVER_STARTMSG_EXE, &msgPath)) {
557 /* Use C runtime spawn; don't need all the machinery in the
558 * process management library.
560 (void)_spawnl(_P_DETACH, msgPath, BOSSERVER_STARTMSG_EXE, NULL);
567 * main() -- start dispatcher thread for BOS control service
571 SERVICE_TABLE_ENTRY dispatchTable[] = {{AFSREG_SVR_SVC_NAME, BosCtlMain},
574 (void) ReportInformationEventAlt(AFSEVT_SVR_BCS_STARTED, NULL);
576 if (!StartServiceCtrlDispatcher(dispatchTable)) {
577 /* unable to connect to SCM */
578 (void) ReportErrorEventAlt(AFSEVT_SVR_BCS_SCM_COMM_FAILED,
579 (int)GetLastError(), NULL);
582 (void) ReportInformationEventAlt(AFSEVT_SVR_BCS_STOPPED, NULL);