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>
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>
36 #define BOSSERVER_STARTMSG_EXE "afslegal.exe"
38 #define BOSSERVER_WAIT_TIME_HINT 60 /* seconds */
39 #define BOSSERVER_STOP_TIME_MAX (FSSDTIME + 60) /* seconds */
41 #define BOS_CONTROLS_ACCEPTED SERVICE_ACCEPT_STOP
43 static CRITICAL_SECTION bosCtlStatusLock; /* protects bosCtlStatus */
44 static SERVICE_STATUS bosCtlStatus;
45 static SERVICE_STATUS_HANDLE bosCtlStatusHandle;
47 /* note: events arranged in priority order in case multiple signaled */
48 #define BOS_STOP_EVENT 0
49 #define BOS_EXIT_EVENT 1
50 #define BOS_EVENT_COUNT 2
51 static HANDLE bosCtlEvent[BOS_EVENT_COUNT];
54 /* Declare local functions */
56 static void AsyncSignalCatcher(int signo);
58 static void BosCtlStatusInit(DWORD initState);
60 static DWORD BosCtlStatusUpdate(DWORD newState,
64 static DWORD BosCtlStatusReport(void);
66 static void WINAPI BosCtlHandler(DWORD controlCode);
68 static void WINAPI BosCtlMain(DWORD argc,
71 static void BosserverDoStopEvent(pid_t cpid,
75 static void BosserverDoExitEvent(pid_t cpid,
81 static void BosserverRun(DWORD argc,
86 static void BosserverStartupMsgDisplay(void);
92 * AsyncSignalCatcher() -- Handle asynchronous signals sent to process
95 AsyncSignalCatcher(int signo)
97 if (signo == SIGCHLD) {
98 (void) SetEvent(bosCtlEvent[BOS_EXIT_EVENT]);
104 * BosCtlStatusInit() -- initialize BOS control service status structure
107 BosCtlStatusInit(DWORD initState)
109 bosCtlStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
110 bosCtlStatus.dwCurrentState = initState;
112 if (initState == SERVICE_RUNNING) {
113 bosCtlStatus.dwControlsAccepted = BOS_CONTROLS_ACCEPTED;
115 bosCtlStatus.dwControlsAccepted = 0;
118 bosCtlStatus.dwWin32ExitCode = 0;
119 bosCtlStatus.dwServiceSpecificExitCode = 0;
120 bosCtlStatus.dwCheckPoint = 0;
121 bosCtlStatus.dwWaitHint = BOSSERVER_WAIT_TIME_HINT * 1000; /* millisecs */
123 InitializeCriticalSection(&bosCtlStatusLock);
128 * BosCtlStatusUpdate() -- update BOS control service status and report to SCM
131 BosCtlStatusUpdate(DWORD newState, DWORD exitCode, BOOL isWin32Code)
135 EnterCriticalSection(&bosCtlStatusLock);
137 /* SERVICE_STOPPED is a terminal state; never transition out of it */
138 if (bosCtlStatus.dwCurrentState != SERVICE_STOPPED) {
140 if ((bosCtlStatus.dwCurrentState == newState) &&
141 (newState == SERVICE_START_PENDING ||
142 newState == SERVICE_STOP_PENDING)) {
143 /* continuing a pending state; increment checkpoint value */
144 bosCtlStatus.dwCheckPoint++;
146 /* not continuing a pending state; reset checkpoint value */
147 bosCtlStatus.dwCheckPoint = 0;
150 bosCtlStatus.dwCurrentState = newState;
152 if (newState == SERVICE_RUNNING) {
153 bosCtlStatus.dwControlsAccepted = BOS_CONTROLS_ACCEPTED;
155 bosCtlStatus.dwControlsAccepted = 0;
159 bosCtlStatus.dwWin32ExitCode = exitCode;
160 bosCtlStatus.dwServiceSpecificExitCode = 0;
162 bosCtlStatus.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR;
163 bosCtlStatus.dwServiceSpecificExitCode = exitCode;
167 if (!SetServiceStatus(bosCtlStatusHandle, &bosCtlStatus)) {
171 LeaveCriticalSection(&bosCtlStatusLock);
178 * BosCtlStatusReport() -- report current BOS control service status to SCM
181 BosCtlStatusReport(void)
185 EnterCriticalSection(&bosCtlStatusLock);
187 if (!SetServiceStatus(bosCtlStatusHandle, &bosCtlStatus)) {
191 LeaveCriticalSection(&bosCtlStatusLock);
198 * BosCtlHandler() -- control handler for BOS control service
201 BosCtlHandler(DWORD controlCode)
203 switch (controlCode) {
204 case SERVICE_CONTROL_STOP:
205 (void) SetEvent(bosCtlEvent[BOS_STOP_EVENT]);
206 (void) BosCtlStatusUpdate(SERVICE_STOP_PENDING, 0, TRUE);
210 (void) BosCtlStatusReport();
217 * BosCtlMain() -- main function for BOS control service
220 BosCtlMain(DWORD argc, LPTSTR *argv)
224 struct sigaction childAction;
226 /* Initialize status structure */
227 BosCtlStatusInit(SERVICE_START_PENDING);
229 /* Create events used by service control handler and signal handler */
230 if ((bosCtlEvent[BOS_STOP_EVENT] = CreateEvent(NULL,
231 FALSE /* manual reset */,
232 FALSE /* initial state */,
233 TEXT("BosCtlSvc Stop Event"))) == NULL) {
234 status = GetLastError();
237 if ((bosCtlEvent[BOS_EXIT_EVENT] = CreateEvent(NULL,
238 FALSE /* manual reset */,
239 FALSE /* initial state */,
240 TEXT("BosCtlSvc Exit Event"))) == NULL) {
241 status = GetLastError();
244 /* Register service control handler */
245 bosCtlStatusHandle = RegisterServiceCtrlHandler(AFSREG_SVR_SVC_NAME,
247 if (bosCtlStatusHandle == 0) {
248 /* failed to register control handler for service; can not continue */
249 (void) ReportErrorEventAlt(AFSEVT_SVR_BCS_HANDLER_REG_FAILED,
250 (int)GetLastError(), NULL);
251 /* can not report status to SCM w/o a valid bosCtlStatusHandle */
255 /* Stop immediately if required system resources could not be obtained */
256 if (bosCtlEvent[BOS_STOP_EVENT] == NULL ||
257 bosCtlEvent[BOS_EXIT_EVENT] == NULL) {
258 (void) ReportErrorEventAlt(AFSEVT_SVR_BCS_INSUFFICIENT_RESOURCES,
260 (void) BosCtlStatusUpdate(SERVICE_STOPPED, status, TRUE);
264 /* Report pending start status */
265 if (status = BosCtlStatusUpdate(SERVICE_START_PENDING, 0, TRUE)) {
266 /* can't inform SCM of pending start; give up before really start */
267 (void) ReportErrorEventAlt(AFSEVT_SVR_BCS_SCM_COMM_FAILED,
269 (void) BosCtlStatusUpdate(SERVICE_STOPPED, status, TRUE);
273 /* For XP SP2 and above, open required ports */
274 icf_CheckAndAddAFSPorts(AFS_PORTSET_SERVER);
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,
391 if (waitpid(cpid, &waitStatus, WNOHANG) == cpid) {
392 /* bosserver status available */
394 if (WIFEXITED(waitStatus)) {
395 /* bosserver exited normally; check for restart code */
396 int exitCode = WEXITSTATUS(waitStatus);
398 if (BOSEXIT_DORESTART(exitCode)) {
399 /* bosserver requests restart */
406 /* bosserver exited with non-restart code; set status */
407 *stopStatus = waitStatus;
408 *isWin32Code = FALSE;
410 (void) ReportWarningEventAlt(AFSEVT_SVR_BCS_BOSSERVER_EXIT,
411 (int)*stopStatus, NULL);
415 /* bosserver status NOT available; assume spurious SIGCHLD */
422 * BosserverRun() -- Run the AFS bosserver, handling stop and exit events.
424 * Input args are those passed to the service's main function (BosCtlMain).
425 * Output args are the stop status and status type of the bosserver.
428 BosserverRun(DWORD argc,
434 BOOL doRestart, doWait;
437 /* Display bosserver startup (legal) message; first start only */
438 /* BosserverStartupMsgDisplay(); */
440 /* Set env variable forcing process mgmt lib to spawn processes detached */
441 (void)putenv(PMGT_SPAWN_DETACHED_ENV_NAME "=1");
443 /* Alloc block with room for arguments plus a terminator */
444 spawn_argv = (char **)malloc((argc + 1) * sizeof(char *));
446 if (spawn_argv == NULL) {
447 /* failed to malloc required space; can not continue */
448 *stopStatus = ENOMEM;
449 *isWin32Code = FALSE;
451 (void) ReportErrorEventAlt(AFSEVT_SVR_BCS_INSUFFICIENT_RESOURCES,
452 (int)*stopStatus, NULL);
456 /* Set initial bosserver args to those supplied via StartService() */
457 spawn_argv[0] = (char *)AFSDIR_SERVER_BOSVR_FILEPATH;
459 for (i = 1; i < argc; i++) {
460 spawn_argv[i] = argv[i];
462 spawn_argv[i] = NULL;
464 /* Start/restart bosserver and wait for either a stop or exit event */
471 /* restarting bosserver; log informational message */
472 (void) ReportInformationEventAlt(AFSEVT_SVR_BCS_BOSSERVER_RESTART,
477 cpid = spawnprocve(spawn_argv[0], spawn_argv, NULL, 0);
479 if (cpid == (pid_t)-1) {
480 /* failed to start/restart the bosserver process */
482 *isWin32Code = FALSE;
484 (void) ReportErrorEventAlt(AFSEVT_SVR_BCS_BOSSERVER_START_FAILED,
485 (int)*stopStatus, NULL);
489 if (status = BosCtlStatusUpdate(SERVICE_RUNNING, 0, TRUE)) {
490 /* can't tell SCM we're running so quit; should never occur */
491 (void) ReportErrorEventAlt(AFSEVT_SVR_BCS_SCM_COMM_FAILED,
493 (void) SetEvent(bosCtlEvent[BOS_STOP_EVENT]);
496 /* bosserver is running; wait for an event of interest */
498 Sleep(5000); /* bosserver needs time to register signal handler */
503 status = WaitForMultipleObjects(BOS_EVENT_COUNT,
504 bosCtlEvent, FALSE, INFINITE);
506 if ((status - WAIT_OBJECT_0) == BOS_STOP_EVENT) {
507 /* stop event signaled */
508 BosserverDoStopEvent(cpid, stopStatus, isWin32Code);
510 } else if ((status - WAIT_OBJECT_0) == BOS_EXIT_EVENT) {
511 /* exit event signaled; see function comment for outcomes */
512 BosserverDoExitEvent(cpid,
515 stopStatus, isWin32Code);
518 /* failed to wait on events; should never happen */
519 Sleep(2000); /* sleep to avoid tight loop if event problem */
530 * BosserverStartupMsgDisplay() -- display Windows version of AFS bosserver
531 * startup (legal) message.
534 BosserverStartupMsgDisplay(void)
538 if (!ConstructLocalBinPath(BOSSERVER_STARTMSG_EXE, &msgPath)) {
539 /* Use C runtime spawn; don't need all the machinery in the
540 * process management library.
542 (void)_spawnl(_P_DETACH, msgPath, BOSSERVER_STARTMSG_EXE, NULL);
549 * main() -- start dispatcher thread for BOS control service
553 SERVICE_TABLE_ENTRY dispatchTable[] = {{AFSREG_SVR_SVC_NAME, BosCtlMain},
556 (void) ReportInformationEventAlt(AFSEVT_SVR_BCS_STARTED, NULL);
558 if (!StartServiceCtrlDispatcher(dispatchTable)) {
559 /* unable to connect to SCM */
560 (void) ReportErrorEventAlt(AFSEVT_SVR_BCS_SCM_COMM_FAILED,
561 (int)GetLastError(), NULL);
564 (void) ReportInformationEventAlt(AFSEVT_SVR_BCS_STOPPED, NULL);