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 #include <afsconfig.h>
11 #include <afs/param.h>
20 #include <sys/types.h>
27 #ifdef BOZO_SAVE_CORES
40 #include <afs/procmgmt.h> /* signal(), kill(), wait(), etc. */
42 #include <afs/audit.h>
43 #include <afs/afsutil.h>
44 #include <afs/fileutil.h>
47 #if defined(AFS_AIX_ENV) || defined(AFS_SUN4_ENV)
48 /* All known versions of AIX lack WCOREDUMP but this works */
49 #define WCOREDUMP(x) ((x) & 0x80)
52 #define BNODE_LWP_STACKSIZE (16 * 1024)
54 int bnode_waiting = 0;
55 static PROCESS bproc_pid; /* pid of waker-upper */
56 static struct bnode *allBnodes = 0; /* list of all bnodes */
57 static struct bnode_proc *allProcs = 0; /* list of all processes for which we're waiting */
58 static struct bnode_type *allTypes = 0; /* list of registered type handlers */
60 static struct bnode_stats {
65 extern char **environ; /* env structure */
68 /* Remember the name of the process, if any, that failed last */
70 RememberProcName(register struct bnode_proc *ap)
72 register struct bnode *tbnodep;
75 if (tbnodep->lastErrorName) {
76 free(tbnodep->lastErrorName);
77 tbnodep->lastErrorName = NULL;
80 tbnodep->lastErrorName = (char *)malloc(strlen(ap->coreName) + 1);
81 strcpy(tbnodep->lastErrorName, ap->coreName);
85 /* utility for use by BOP_HASCORE functions to determine where a core file might
89 bnode_CoreName(register struct bnode *abnode, char *acoreName, char *abuffer)
91 strcpy(abuffer, AFSDIR_SERVER_CORELOG_FILEPATH);
93 strcat(abuffer, acoreName);
96 strcat(abuffer, abnode->name);
100 /* save core file, if any */
102 SaveCore(register struct bnode *abnode, register struct bnode_proc
107 register afs_int32 code;
108 #ifdef BOZO_SAVE_CORES
109 struct timeval Start;
110 struct tm *TimeFields;
114 code = stat(AFSDIR_SERVER_CORELOG_FILEPATH, &tstat);
118 bnode_CoreName(abnode, aproc->coreName, tbuffer);
119 #ifdef BOZO_SAVE_CORES
120 TM_GetTimeOfDay(&Start, 0);
121 TimeFields = localtime(&Start.tv_sec);
122 sprintf(FileName, "%s.%d%02d%02d%02d%02d%02d", tbuffer,
123 TimeFields->tm_year, TimeFields->tm_mon + 1, TimeFields->tm_mday,
124 TimeFields->tm_hour, TimeFields->tm_min, TimeFields->tm_sec);
125 strcpy(tbuffer, FileName);
127 code = renamefile(AFSDIR_SERVER_CORELOG_FILEPATH, tbuffer);
131 bnode_GetString(register struct bnode *abnode, register char *abuffer,
132 register afs_int32 alen)
134 return BOP_GETSTRING(abnode, abuffer, alen);
138 bnode_GetParm(register struct bnode *abnode, register afs_int32 aindex,
139 register char *abuffer, afs_int32 alen)
141 return BOP_GETPARM(abnode, aindex, abuffer, alen);
145 bnode_GetStat(register struct bnode *abnode, register afs_int32 * astatus)
147 return BOP_GETSTAT(abnode, astatus);
151 bnode_RestartP(register struct bnode *abnode)
153 return BOP_RESTARTP(abnode);
157 bnode_Check(register struct bnode *abnode)
159 if (abnode->flags & BNODE_WAIT) {
160 abnode->flags &= ~BNODE_WAIT;
161 LWP_NoYieldSignal(abnode);
166 /* tell if an instance has a core file */
168 bnode_HasCore(register struct bnode *abnode)
170 return BOP_HASCORE(abnode);
173 /* wait for all bnodes to stabilize */
177 register struct bnode *tb;
178 register afs_int32 code;
182 for (tb = allBnodes; tb; tb = tb->next) {
184 code = BOP_GETSTAT(tb, &stat);
189 if (stat != tb->goal) {
190 tb->flags |= BNODE_WAIT;
200 /* wait until bnode status is correct */
202 bnode_WaitStatus(register struct bnode *abnode, int astatus)
204 register afs_int32 code;
210 code = BOP_GETSTAT(abnode, &stat);
214 /* otherwise, check if we're done */
215 if (stat == astatus) {
216 bnode_Release(abnode);
219 if (astatus != abnode->goal) {
220 bnode_Release(abnode);
221 return -1; /* no longer our goal, don't keep waiting */
223 /* otherwise, block */
224 abnode->flags |= BNODE_WAIT;
225 LWP_WaitProcess(abnode);
230 bnode_SetStat(register struct bnode *abnode, register int agoal)
232 abnode->goal = agoal;
234 BOP_SETSTAT(abnode, agoal);
235 abnode->flags &= ~BNODE_ERRORSTOP;
240 bnode_SetGoal(register struct bnode *abnode, register int agoal)
242 abnode->goal = agoal;
248 bnode_SetFileGoal(register struct bnode *abnode, register int agoal)
250 if (abnode->fileGoal == agoal)
251 return 0; /* already done */
252 abnode->fileGoal = agoal;
257 /* apply a function to all bnodes in the system */
259 bnode_ApplyInstance(int (*aproc) (), char *arock)
261 register struct bnode *tb, *nb;
262 register afs_int32 code;
264 for (tb = allBnodes; tb; tb = nb) {
266 code = (*aproc) (tb, arock);
274 bnode_FindInstance(register char *aname)
276 register struct bnode *tb;
278 for (tb = allBnodes; tb; tb = tb->next) {
279 if (!strcmp(tb->name, aname))
285 static struct bnode_type *
286 FindType(register char *aname)
288 register struct bnode_type *tt;
290 for (tt = allTypes; tt; tt = tt->next) {
291 if (!strcmp(tt->name, aname))
294 return (struct bnode_type *)0;
298 bnode_Register(char *atype, struct bnode_ops *aprocs, int anparms)
300 register struct bnode_type *tt;
302 for (tt = allTypes; tt; tt = tt->next) {
303 if (!strcmp(tt->name, atype))
307 tt = (struct bnode_type *)malloc(sizeof(struct bnode_type));
308 memset(tt, 0, sizeof(struct bnode_type));
318 bnode_Create(char *atype, char *ainstance, struct bnode ** abp, char *ap1,
319 char *ap2, char *ap3, char *ap4, char *ap5, char *notifier,
320 int fileGoal, int rewritefile)
322 struct bnode_type *type;
324 char *notifierpath = NULL;
327 if (bnode_FindInstance(ainstance))
329 type = FindType(atype);
333 if (notifier && strcmp(notifier, NONOTIFIER)) {
334 /* construct local path from canonical (wire-format) path */
335 if (ConstructLocalBinPath(notifier, ¬ifierpath)) {
336 bozo_Log("BNODE-Create: Notifier program path invalid '%s'\n",
341 if (stat(notifierpath, &tstat)) {
342 bozo_Log("BNODE-Create: Notifier program '%s' not found\n",
348 tb = (*type->ops->create) (ainstance, ap1, ap2, ap3, ap4, ap5);
353 tb->notifier = notifierpath;
357 /* The fs_create above calls bnode_InitBnode() which always sets the
358 ** fileGoal to BSTAT_NORMAL .... overwrite it with whatever is passed into
359 ** this function as a parameter... */
360 tb->fileGoal = fileGoal;
362 bnode_SetStat(tb, tb->goal); /* nudge it once */
364 if (rewritefile != 0)
371 bnode_DeleteName(char *ainstance)
373 register struct bnode *tb;
375 tb = bnode_FindInstance(ainstance);
379 return bnode_Delete(tb);
383 bnode_Hold(register struct bnode *abnode)
390 bnode_Release(register struct bnode *abnode)
393 if (abnode->refCount == 0 && abnode->flags & BNODE_DELETE) {
394 abnode->flags &= ~BNODE_DELETE; /* we're going for it */
395 bnode_Delete(abnode);
401 bnode_Delete(register struct bnode *abnode)
403 register afs_int32 code;
404 register struct bnode **lb, *ub;
407 if (abnode->refCount != 0) {
408 abnode->flags |= BNODE_DELETE;
412 /* make sure the bnode is idle before zapping */
414 code = BOP_GETSTAT(abnode, &temp);
415 bnode_Release(abnode);
418 if (temp != BSTAT_SHUTDOWN)
421 /* all clear to zap */
422 for (lb = &allBnodes, ub = *lb; ub; lb = &ub->next, ub = *lb) {
424 /* unthread it from the list */
429 free(abnode->name); /* do this first, since bnode fields may be bad after BOP_DELETE */
430 code = BOP_DELETE(abnode); /* don't play games like holding over this one */
435 /* function to tell if there's a timeout coming up */
437 bnode_PendingTimeout(register struct bnode *abnode)
439 return (abnode->flags & BNODE_NEEDTIMEOUT);
442 /* function called to set / clear periodic bnode wakeup times */
444 bnode_SetTimeout(register struct bnode *abnode, afs_int32 atimeout)
447 abnode->nextTimeout = FT_ApproxTime() + atimeout;
448 abnode->flags |= BNODE_NEEDTIMEOUT;
449 abnode->period = atimeout;
450 IOMGR_Cancel(bproc_pid);
452 abnode->flags &= ~BNODE_NEEDTIMEOUT;
457 /* used by new bnode creation code to format bnode header */
459 bnode_InitBnode(register struct bnode *abnode, struct bnode_ops *abnodeops,
462 struct bnode **lb, *nb;
464 /* format the bnode properly */
465 memset(abnode, 0, sizeof(struct bnode));
466 abnode->ops = abnodeops;
467 abnode->name = (char *)malloc(strlen(aname) + 1);
470 strcpy(abnode->name, aname);
471 abnode->flags = BNODE_ACTIVE;
472 abnode->fileGoal = BSTAT_NORMAL;
473 abnode->goal = BSTAT_SHUTDOWN;
475 /* put the bnode at the end of the list so we write bnode file in same order */
476 for (lb = &allBnodes, nb = *lb; nb; lb = &nb->next, nb = *lb);
483 DeleteProc(register struct bnode_proc *abproc)
485 register struct bnode_proc **pb, *tb;
486 struct bnode_proc *nb;
488 for (pb = &allProcs, tb = *pb; tb; pb = &tb->next, tb = nb) {
499 /* bnode lwp executes this code repeatedly */
503 register afs_int32 code;
504 register struct bnode *tb;
505 register afs_int32 temp;
506 register struct bnode_proc *tp;
508 int options; /* must not be register */
514 /* first figure out how long to sleep for */
515 temp = 0x7fffffff; /* afs_int32 time; maxint doesn't work in select */
517 for (tb = allBnodes; tb; tb = tb->next) {
518 if (tb->flags & BNODE_NEEDTIMEOUT) {
519 if (tb->nextTimeout < temp) {
521 temp = tb->nextTimeout;
525 /* now temp has the time at which we should wakeup next */
529 temp -= FT_ApproxTime(); /* how many seconds until next event */
535 code = IOMGR_Select(0, 0, 0, 0, &tv);
537 code = 0; /* fake timeout code */
539 /* figure out why we woke up; child exit or timeouts */
540 FT_GetTimeOfDay(&tv, 0); /* must do the real gettimeofday once and a while */
543 /* check all bnodes to see which ones need timeout events */
544 for (tb = allBnodes; tb; tb = nb) {
545 if ((tb->flags & BNODE_NEEDTIMEOUT) && temp > tb->nextTimeout) {
549 if (tb->flags & BNODE_NEEDTIMEOUT) { /* check again, BOP_TIMEOUT could change */
550 tb->nextTimeout = FT_ApproxTime() + tb->period;
553 bnode_Release(tb); /* delete may occur here */
559 /* signalled, probably by incoming signal */
562 bnode_waiting = options | 0x800000;
563 code = waitpid((pid_t) - 1, &status, options);
565 if (code == 0 || code == -1)
566 break; /* all done */
567 /* otherwise code has a process id, which we now search for */
568 for (tp = allProcs; tp; tp = tp->next)
576 /* count restarts in last 10 seconds */
577 if (temp > tb->rsTime + 30) {
578 /* it's been 10 seconds we've been counting */
583 if (WIFSIGNALED(status) == 0) {
584 /* exited, not signalled */
585 tp->lastExit = WEXITSTATUS(status);
588 tb->errorCode = tp->lastExit;
589 tb->lastErrorExit = FT_ApproxTime();
590 RememberProcName(tp);
594 bozo_Log("%s:%s exited with code %d\n", tb->name,
595 tp->coreName, tp->lastExit);
597 bozo_Log("%s exited with code %d\n", tb->name,
600 /* Signal occurred, perhaps spurious due to shutdown request.
601 * If due to a shutdown request, don't overwrite last error
604 tp->lastSignal = WTERMSIG(status);
606 if (tp->lastSignal != SIGQUIT
607 && tp->lastSignal != SIGTERM
608 && tp->lastSignal != SIGKILL) {
609 tb->errorSignal = tp->lastSignal;
610 tb->lastErrorExit = FT_ApproxTime();
611 RememberProcName(tp);
614 bozo_Log("%s:%s exited on signal %d%s\n",
615 tb->name, tp->coreName, tp->lastSignal,
616 WCOREDUMP(status) ? " (core dumped)" :
619 bozo_Log("%s exited on signal %d%s\n", tb->name,
621 WCOREDUMP(status) ? " (core dumped)" :
625 tb->lastAnyExit = FT_ApproxTime();
628 bozo_Log("BNODE: Notifier %s will be called\n",
632 BOP_PROCEXIT(tb, tp);
635 if (tb->rsCount++ > 10) {
636 /* 10 in 10 seconds */
637 tb->flags |= BNODE_ERRORSTOP;
638 bnode_SetGoal(tb, BSTAT_SHUTDOWN);
640 ("BNODE '%s' repeatedly failed to start, perhaps missing executable.\n",
643 bnode_Release(tb); /* bnode delete can happen here */
646 bnode_stats.weirdPids++;
653 SendNotifierData(register int fd, register struct bnode_proc *tp)
655 register struct bnode *tb = tp->bnode;
656 char buffer[1000], *bufp = buffer, *buf1;
660 * First sent out the bnode_proc struct
662 (void)sprintf(bufp, "BEGIN bnode_proc\n");
663 bufp += strlen(bufp);
664 (void)sprintf(bufp, "comLine: %s\n", tp->comLine);
665 bufp += strlen(bufp);
666 if (!(buf1 = tp->coreName))
668 (void)sprintf(bufp, "coreName: %s\n", buf1);
669 bufp += strlen(bufp);
670 (void)sprintf(bufp, "pid: %ld\n", tp->pid);
671 bufp += strlen(bufp);
672 (void)sprintf(bufp, "lastExit: %ld\n", tp->lastExit);
673 bufp += strlen(bufp);
675 (void)sprintf(bufp, "lastSignal: %ld\n", tp->lastSignal);
676 bufp += strlen(bufp);
678 (void)sprintf(bufp, "flags: %ld\n", tp->flags);
679 bufp += strlen(bufp);
680 (void)sprintf(bufp, "END bnode_proc\n");
681 bufp += strlen(bufp);
682 len = (int)(bufp - buffer);
683 if (write(fd, buffer, len) < 0) {
688 * Now sent out the bnode struct
691 (void)sprintf(bufp, "BEGIN bnode\n");
692 bufp += strlen(bufp);
693 (void)sprintf(bufp, "name: %s\n", tb->name);
694 bufp += strlen(bufp);
695 (void)sprintf(bufp, "rsTime: %ld\n", tb->rsTime);
696 bufp += strlen(bufp);
697 (void)sprintf(bufp, "rsCount: %ld\n", tb->rsCount);
698 bufp += strlen(bufp);
699 (void)sprintf(bufp, "procStartTime: %ld\n", tb->procStartTime);
700 bufp += strlen(bufp);
701 (void)sprintf(bufp, "procStarts: %ld\n", tb->procStarts);
702 bufp += strlen(bufp);
703 (void)sprintf(bufp, "lastAnyExit: %ld\n", tb->lastAnyExit);
704 bufp += strlen(bufp);
705 (void)sprintf(bufp, "lastErrorExit: %ld\n", tb->lastErrorExit);
706 bufp += strlen(bufp);
707 (void)sprintf(bufp, "errorCode: %ld\n", tb->errorCode);
708 bufp += strlen(bufp);
709 (void)sprintf(bufp, "errorSignal: %ld\n", tb->errorSignal);
710 bufp += strlen(bufp);
712 (void) sprintf(bufp, "lastErrorName: %s\n", tb->lastErrorName);
713 bufp += strlen(bufp);
715 (void)sprintf(bufp, "goal: %d\n", tb->goal);
716 bufp += strlen(bufp);
717 (void)sprintf(bufp, "END bnode\n");
718 bufp += strlen(bufp);
719 len = (int)(bufp - buffer);
720 if (write(fd, buffer, len) < 0) {
726 hdl_notifier(struct bnode_proc *tp)
728 #ifndef AFS_NT40_ENV /* NT notifier callout not yet implemented */
729 int code, pid, status;
732 if (stat(tp->bnode->notifier, &tstat)) {
733 bozo_Log("BNODE: Failed to find notifier '%s'; ignored\n",
734 tp->bnode->notifier);
737 if ((pid = fork()) == 0) {
739 struct bnode *tb = tp->bnode;
742 #if defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_SGI51_ENV)
744 #elif defined(AFS_DARWIN90_ENV)
746 #elif defined(AFS_LINUX20_ENV) || defined(AFS_AIX_ENV)
751 fout = popen(tb->notifier, "w");
753 bozo_Log("BNODE: Failed to find notifier '%s'; ignored\n",
755 perror(tb->notifier);
758 code = SendNotifierData(fileno(fout), tp);
761 } else if (pid < 0) {
762 bozo_Log("Failed to fork creating process to handle notifier '%s'\n",
763 tp->bnode->notifier);
766 #endif /* AFS_NT40_ENV */
770 /* Called by IOMGR at low priority on IOMGR's stack shortly after a SIGCHLD
771 * occurs. Wakes up bproc do redo things */
773 bnode_SoftInt(int asignal)
775 IOMGR_Cancel(bproc_pid);
779 /* Called at signal interrupt level; queues function to be called
780 * when IOMGR runs again.
783 bnode_Int(int asignal)
785 extern void bozo_ShutdownAndExit();
787 if (asignal == SIGQUIT) {
788 IOMGR_SoftSig(bozo_ShutdownAndExit, (char *)asignal);
790 IOMGR_SoftSig(bnode_SoftInt, (char *)asignal);
795 /* intialize the whole system */
800 register afs_int32 code;
801 struct sigaction newaction;
807 memset(&bnode_stats, 0, sizeof(bnode_stats));
808 LWP_InitializeProcessSupport(1, &junk); /* just in case */
810 code = LWP_CreateProcess(bproc, BNODE_LWP_STACKSIZE,
811 /* priority */ 1, (void *) /* parm */ 0,
812 "bnode-manager", &bproc_pid);
815 memset((char *)&newaction, 0, sizeof(newaction));
816 newaction.sa_handler = bnode_Int;
817 code = sigaction(SIGCHLD, &newaction, NULL);
820 code = sigaction(SIGQUIT, &newaction, NULL);
826 /* free token list returned by parseLine */
828 bnode_FreeTokens(register struct bnode_token *alist)
830 register struct bnode_token *nlist;
831 for (; alist; alist = nlist) {
842 if (x == 0 || x == ' ' || x == '\t' || x == '\n')
849 bnode_ParseLine(char *aline, struct bnode_token **alist)
854 struct bnode_token *first, *last;
855 register struct bnode_token *ttok;
858 inToken = 0; /* not copying token chars at start */
859 first = (struct bnode_token *)0;
860 last = (struct bnode_token *)0;
863 if (tc == 0 || space(tc)) { /* terminating null gets us in here, too */
865 inToken = 0; /* end of this token */
868 (struct bnode_token *)malloc(sizeof(struct bnode_token));
869 ttok->next = (struct bnode_token *)0;
870 ttok->key = (char *)malloc(strlen(tbuffer) + 1);
871 strcpy(ttok->key, tbuffer);
881 /* an alpha character */
886 if (tptr - tbuffer >= sizeof(tbuffer))
887 return -1; /* token too long */
891 /* last token flushed 'cause space(0) --> true */
893 last->next = (struct bnode_token *)0;
902 bnode_NewProc(struct bnode *abnode, char *aexecString, char *coreName,
903 struct bnode_proc **aproc)
905 struct bnode_token *tlist, *tt;
907 struct bnode_proc *tp;
909 char *argv[MAXVARGS];
912 code = bnode_ParseLine(aexecString, &tlist); /* try parsing first */
915 tp = (struct bnode_proc *)malloc(sizeof(struct bnode_proc));
916 memset(tp, 0, sizeof(struct bnode_proc));
919 tp->comLine = aexecString;
920 tp->coreName = coreName; /* may be null */
921 abnode->procStartTime = FT_ApproxTime();
922 abnode->procStarts++;
924 /* convert linked list of tokens into argv structure */
925 for (tt = tlist, i = 0; i < (MAXVARGS - 1) && tt; tt = tt->next, i++) {
928 argv[i] = NULL; /* null-terminated */
930 cpid = spawnprocve(argv[0], argv, environ, -1);
931 osi_audit(BOSSpawnProcEvent, 0, AUD_STR, aexecString, AUD_END);
933 if (cpid == (pid_t) - 1) {
934 bozo_Log("Failed to spawn process for bnode '%s'\n", abnode->name);
935 bnode_FreeTokens(tlist);
940 bnode_FreeTokens(tlist);
944 tp->flags = BPROC_STARTED;
945 tp->flags &= ~BPROC_EXITED;
951 bnode_StopProc(register struct bnode_proc *aproc, int asignal)
954 if (!(aproc->flags & BPROC_STARTED) || (aproc->flags & BPROC_EXITED))
957 osi_audit(BOSStopProcEvent, 0, AUD_STR, (aproc ? aproc->comLine : NULL),
960 code = kill(aproc->pid, asignal);
961 bnode_Check(aproc->bnode);
966 bnode_Deactivate(register struct bnode *abnode)
968 register struct bnode **pb, *tb;
970 if (!(abnode->flags & BNODE_ACTIVE))
972 for (pb = &allBnodes, tb = *pb; tb; tb = nb) {
976 tb->flags &= ~BNODE_ACTIVE;