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
33 #include <afs/procmgmt.h> /* signal(), kill(), wait(), etc. */
35 #include <afs/audit.h>
36 #include <afs/afsutil.h>
37 #include <afs/fileutil.h>
40 #if defined(AFS_AIX_ENV) || defined(AFS_SUN4_ENV)
41 /* All known versions of AIX lack WCOREDUMP but this works */
42 #define WCOREDUMP(x) ((x) & 0x80)
45 #define BNODE_LWP_STACKSIZE (16 * 1024)
47 int bnode_waiting = 0;
48 static PROCESS bproc_pid; /* pid of waker-upper */
49 static struct bnode *allBnodes = 0; /* list of all bnodes */
50 static struct bnode_proc *allProcs = 0; /* list of all processes for which we're waiting */
51 static struct bnode_type *allTypes = 0; /* list of registered type handlers */
53 static struct bnode_stats {
58 extern char **environ; /* env structure */
61 /* Remember the name of the process, if any, that failed last */
63 RememberProcName(register struct bnode_proc *ap)
65 register struct bnode *tbnodep;
68 if (tbnodep->lastErrorName) {
69 free(tbnodep->lastErrorName);
70 tbnodep->lastErrorName = NULL;
73 tbnodep->lastErrorName = (char *)malloc(strlen(ap->coreName) + 1);
74 strcpy(tbnodep->lastErrorName, ap->coreName);
78 /* utility for use by BOP_HASCORE functions to determine where a core file might
82 bnode_CoreName(register struct bnode *abnode, char *acoreName, char *abuffer)
84 strcpy(abuffer, AFSDIR_SERVER_CORELOG_FILEPATH);
86 strcat(abuffer, acoreName);
89 strcat(abuffer, abnode->name);
93 /* save core file, if any */
95 SaveCore(register struct bnode *abnode, register struct bnode_proc
100 register afs_int32 code;
101 #ifdef BOZO_SAVE_CORES
102 struct timeval Start;
103 struct tm *TimeFields;
107 code = stat(AFSDIR_SERVER_CORELOG_FILEPATH, &tstat);
111 bnode_CoreName(abnode, aproc->coreName, tbuffer);
112 #ifdef BOZO_SAVE_CORES
113 TM_GetTimeOfDay(&Start, 0);
114 TimeFields = localtime(&Start.tv_sec);
115 sprintf(FileName, "%s.%d%02d%02d%02d%02d%02d", tbuffer,
116 TimeFields->tm_year, TimeFields->tm_mon + 1, TimeFields->tm_mday,
117 TimeFields->tm_hour, TimeFields->tm_min, TimeFields->tm_sec);
118 strcpy(tbuffer, FileName);
120 code = renamefile(AFSDIR_SERVER_CORELOG_FILEPATH, tbuffer);
124 bnode_GetString(register struct bnode *abnode, register char *abuffer,
125 register afs_int32 alen)
127 return BOP_GETSTRING(abnode, abuffer, alen);
131 bnode_GetParm(register struct bnode *abnode, register afs_int32 aindex,
132 register char *abuffer, afs_int32 alen)
134 return BOP_GETPARM(abnode, aindex, abuffer, alen);
138 bnode_GetStat(register struct bnode *abnode, register afs_int32 * astatus)
140 return BOP_GETSTAT(abnode, astatus);
144 bnode_RestartP(register struct bnode *abnode)
146 return BOP_RESTARTP(abnode);
150 bnode_Check(register struct bnode *abnode)
152 if (abnode->flags & BNODE_WAIT) {
153 abnode->flags &= ~BNODE_WAIT;
154 LWP_NoYieldSignal(abnode);
159 /* tell if an instance has a core file */
161 bnode_HasCore(register struct bnode *abnode)
163 return BOP_HASCORE(abnode);
166 /* wait for all bnodes to stabilize */
170 register struct bnode *tb;
171 register afs_int32 code;
175 for (tb = allBnodes; tb; tb = tb->next) {
177 code = BOP_GETSTAT(tb, &stat);
182 if (stat != tb->goal) {
183 tb->flags |= BNODE_WAIT;
193 /* wait until bnode status is correct */
195 bnode_WaitStatus(register struct bnode *abnode, int astatus)
197 register afs_int32 code;
203 code = BOP_GETSTAT(abnode, &stat);
207 /* otherwise, check if we're done */
208 if (stat == astatus) {
209 bnode_Release(abnode);
212 if (astatus != abnode->goal) {
213 bnode_Release(abnode);
214 return -1; /* no longer our goal, don't keep waiting */
216 /* otherwise, block */
217 abnode->flags |= BNODE_WAIT;
218 LWP_WaitProcess(abnode);
223 bnode_SetStat(register struct bnode *abnode, register int agoal)
225 abnode->goal = agoal;
227 BOP_SETSTAT(abnode, agoal);
228 abnode->flags &= ~BNODE_ERRORSTOP;
233 bnode_SetGoal(register struct bnode *abnode, register int agoal)
235 abnode->goal = agoal;
241 bnode_SetFileGoal(register struct bnode *abnode, register int agoal)
243 if (abnode->fileGoal == agoal)
244 return 0; /* already done */
245 abnode->fileGoal = agoal;
250 /* apply a function to all bnodes in the system */
252 bnode_ApplyInstance(int (*aproc) (), char *arock)
254 register struct bnode *tb, *nb;
255 register afs_int32 code;
257 for (tb = allBnodes; tb; tb = nb) {
259 code = (*aproc) (tb, arock);
267 bnode_FindInstance(register char *aname)
269 register struct bnode *tb;
271 for (tb = allBnodes; tb; tb = tb->next) {
272 if (!strcmp(tb->name, aname))
278 static struct bnode_type *
279 FindType(register char *aname)
281 register struct bnode_type *tt;
283 for (tt = allTypes; tt; tt = tt->next) {
284 if (!strcmp(tt->name, aname))
287 return (struct bnode_type *)0;
291 bnode_Register(char *atype, struct bnode_ops *aprocs, int anparms)
293 register struct bnode_type *tt;
295 for (tt = allTypes; tt; tt = tt->next) {
296 if (!strcmp(tt->name, atype))
300 tt = (struct bnode_type *)malloc(sizeof(struct bnode_type));
301 memset(tt, 0, sizeof(struct bnode_type));
311 bnode_Create(char *atype, char *ainstance, struct bnode ** abp, char *ap1,
312 char *ap2, char *ap3, char *ap4, char *ap5, char *notifier,
313 int fileGoal, int rewritefile)
315 struct bnode_type *type;
317 char *notifierpath = NULL;
320 if (bnode_FindInstance(ainstance))
322 type = FindType(atype);
326 if (notifier && strcmp(notifier, NONOTIFIER)) {
327 /* construct local path from canonical (wire-format) path */
328 if (ConstructLocalBinPath(notifier, ¬ifierpath)) {
329 bozo_Log("BNODE-Create: Notifier program path invalid '%s'\n",
334 if (stat(notifierpath, &tstat)) {
335 bozo_Log("BNODE-Create: Notifier program '%s' not found\n",
341 tb = (*type->ops->create) (ainstance, ap1, ap2, ap3, ap4, ap5);
346 tb->notifier = notifierpath;
350 /* The fs_create above calls bnode_InitBnode() which always sets the
351 ** fileGoal to BSTAT_NORMAL .... overwrite it with whatever is passed into
352 ** this function as a parameter... */
353 tb->fileGoal = fileGoal;
355 bnode_SetStat(tb, tb->goal); /* nudge it once */
357 if (rewritefile != 0)
364 bnode_DeleteName(char *ainstance)
366 register struct bnode *tb;
368 tb = bnode_FindInstance(ainstance);
372 return bnode_Delete(tb);
376 bnode_Hold(register struct bnode *abnode)
383 bnode_Release(register struct bnode *abnode)
386 if (abnode->refCount == 0 && abnode->flags & BNODE_DELETE) {
387 abnode->flags &= ~BNODE_DELETE; /* we're going for it */
388 bnode_Delete(abnode);
394 bnode_Delete(register struct bnode *abnode)
396 register afs_int32 code;
397 register struct bnode **lb, *ub;
400 if (abnode->refCount != 0) {
401 abnode->flags |= BNODE_DELETE;
405 /* make sure the bnode is idle before zapping */
407 code = BOP_GETSTAT(abnode, &temp);
408 bnode_Release(abnode);
411 if (temp != BSTAT_SHUTDOWN)
414 /* all clear to zap */
415 for (lb = &allBnodes, ub = *lb; ub; lb = &ub->next, ub = *lb) {
417 /* unthread it from the list */
422 free(abnode->name); /* do this first, since bnode fields may be bad after BOP_DELETE */
423 code = BOP_DELETE(abnode); /* don't play games like holding over this one */
428 /* function to tell if there's a timeout coming up */
430 bnode_PendingTimeout(register struct bnode *abnode)
432 return (abnode->flags & BNODE_NEEDTIMEOUT);
435 /* function called to set / clear periodic bnode wakeup times */
437 bnode_SetTimeout(register struct bnode *abnode, afs_int32 atimeout)
440 abnode->nextTimeout = FT_ApproxTime() + atimeout;
441 abnode->flags |= BNODE_NEEDTIMEOUT;
442 abnode->period = atimeout;
443 IOMGR_Cancel(bproc_pid);
445 abnode->flags &= ~BNODE_NEEDTIMEOUT;
450 /* used by new bnode creation code to format bnode header */
452 bnode_InitBnode(register struct bnode *abnode, struct bnode_ops *abnodeops,
455 struct bnode **lb, *nb;
457 /* format the bnode properly */
458 memset(abnode, 0, sizeof(struct bnode));
459 abnode->ops = abnodeops;
460 abnode->name = (char *)malloc(strlen(aname) + 1);
463 strcpy(abnode->name, aname);
464 abnode->flags = BNODE_ACTIVE;
465 abnode->fileGoal = BSTAT_NORMAL;
466 abnode->goal = BSTAT_SHUTDOWN;
468 /* put the bnode at the end of the list so we write bnode file in same order */
469 for (lb = &allBnodes, nb = *lb; nb; lb = &nb->next, nb = *lb);
476 DeleteProc(register struct bnode_proc *abproc)
478 register struct bnode_proc **pb, *tb;
479 struct bnode_proc *nb;
481 for (pb = &allProcs, tb = *pb; tb; pb = &tb->next, tb = nb) {
492 /* bnode lwp executes this code repeatedly */
496 register afs_int32 code;
497 register struct bnode *tb;
498 register afs_int32 temp;
499 register struct bnode_proc *tp;
501 int options; /* must not be register */
507 /* first figure out how long to sleep for */
508 temp = 0x7fffffff; /* afs_int32 time; maxint doesn't work in select */
510 for (tb = allBnodes; tb; tb = tb->next) {
511 if (tb->flags & BNODE_NEEDTIMEOUT) {
512 if (tb->nextTimeout < temp) {
514 temp = tb->nextTimeout;
518 /* now temp has the time at which we should wakeup next */
522 temp -= FT_ApproxTime(); /* how many seconds until next event */
528 code = IOMGR_Select(0, 0, 0, 0, &tv);
530 code = 0; /* fake timeout code */
532 /* figure out why we woke up; child exit or timeouts */
533 FT_GetTimeOfDay(&tv, 0); /* must do the real gettimeofday once and a while */
536 /* check all bnodes to see which ones need timeout events */
537 for (tb = allBnodes; tb; tb = nb) {
538 if ((tb->flags & BNODE_NEEDTIMEOUT) && temp > tb->nextTimeout) {
542 if (tb->flags & BNODE_NEEDTIMEOUT) { /* check again, BOP_TIMEOUT could change */
543 tb->nextTimeout = FT_ApproxTime() + tb->period;
546 bnode_Release(tb); /* delete may occur here */
552 /* signalled, probably by incoming signal */
555 bnode_waiting = options | 0x800000;
556 code = waitpid((pid_t) - 1, &status, options);
558 if (code == 0 || code == -1)
559 break; /* all done */
560 /* otherwise code has a process id, which we now search for */
561 for (tp = allProcs; tp; tp = tp->next)
569 /* count restarts in last 10 seconds */
570 if (temp > tb->rsTime + 30) {
571 /* it's been 10 seconds we've been counting */
576 if (WIFSIGNALED(status) == 0) {
577 /* exited, not signalled */
578 tp->lastExit = WEXITSTATUS(status);
581 tb->errorCode = tp->lastExit;
582 tb->lastErrorExit = FT_ApproxTime();
583 RememberProcName(tp);
587 bozo_Log("%s:%s exited with code %d\n", tb->name,
588 tp->coreName, tp->lastExit);
590 bozo_Log("%s exited with code %d\n", tb->name,
593 /* Signal occurred, perhaps spurious due to shutdown request.
594 * If due to a shutdown request, don't overwrite last error
597 tp->lastSignal = WTERMSIG(status);
599 if (tp->lastSignal != SIGQUIT
600 && tp->lastSignal != SIGTERM
601 && tp->lastSignal != SIGKILL) {
602 tb->errorSignal = tp->lastSignal;
603 tb->lastErrorExit = FT_ApproxTime();
604 RememberProcName(tp);
607 bozo_Log("%s:%s exited on signal %d%s\n",
608 tb->name, tp->coreName, tp->lastSignal,
609 WCOREDUMP(status) ? " (core dumped)" :
612 bozo_Log("%s exited on signal %d%s\n", tb->name,
614 WCOREDUMP(status) ? " (core dumped)" :
618 tb->lastAnyExit = FT_ApproxTime();
621 bozo_Log("BNODE: Notifier %s will be called\n",
625 BOP_PROCEXIT(tb, tp);
628 if (tb->rsCount++ > 10) {
629 /* 10 in 10 seconds */
630 tb->flags |= BNODE_ERRORSTOP;
631 bnode_SetGoal(tb, BSTAT_SHUTDOWN);
633 ("BNODE '%s' repeatedly failed to start, perhaps missing executable.\n",
636 bnode_Release(tb); /* bnode delete can happen here */
639 bnode_stats.weirdPids++;
646 SendNotifierData(register int fd, register struct bnode_proc *tp)
648 register struct bnode *tb = tp->bnode;
649 char buffer[1000], *bufp = buffer, *buf1;
653 * First sent out the bnode_proc struct
655 (void)sprintf(bufp, "BEGIN bnode_proc\n");
656 bufp += strlen(bufp);
657 (void)sprintf(bufp, "comLine: %s\n", tp->comLine);
658 bufp += strlen(bufp);
659 if (!(buf1 = tp->coreName))
661 (void)sprintf(bufp, "coreName: %s\n", buf1);
662 bufp += strlen(bufp);
663 (void)sprintf(bufp, "pid: %ld\n", tp->pid);
664 bufp += strlen(bufp);
665 (void)sprintf(bufp, "lastExit: %ld\n", tp->lastExit);
666 bufp += strlen(bufp);
668 (void)sprintf(bufp, "lastSignal: %ld\n", tp->lastSignal);
669 bufp += strlen(bufp);
671 (void)sprintf(bufp, "flags: %ld\n", tp->flags);
672 bufp += strlen(bufp);
673 (void)sprintf(bufp, "END bnode_proc\n");
674 bufp += strlen(bufp);
675 len = (int)(bufp - buffer);
676 if (write(fd, buffer, len) < 0) {
681 * Now sent out the bnode struct
684 (void)sprintf(bufp, "BEGIN bnode\n");
685 bufp += strlen(bufp);
686 (void)sprintf(bufp, "name: %s\n", tb->name);
687 bufp += strlen(bufp);
688 (void)sprintf(bufp, "rsTime: %ld\n", tb->rsTime);
689 bufp += strlen(bufp);
690 (void)sprintf(bufp, "rsCount: %ld\n", tb->rsCount);
691 bufp += strlen(bufp);
692 (void)sprintf(bufp, "procStartTime: %ld\n", tb->procStartTime);
693 bufp += strlen(bufp);
694 (void)sprintf(bufp, "procStarts: %ld\n", tb->procStarts);
695 bufp += strlen(bufp);
696 (void)sprintf(bufp, "lastAnyExit: %ld\n", tb->lastAnyExit);
697 bufp += strlen(bufp);
698 (void)sprintf(bufp, "lastErrorExit: %ld\n", tb->lastErrorExit);
699 bufp += strlen(bufp);
700 (void)sprintf(bufp, "errorCode: %ld\n", tb->errorCode);
701 bufp += strlen(bufp);
702 (void)sprintf(bufp, "errorSignal: %ld\n", tb->errorSignal);
703 bufp += strlen(bufp);
705 (void) sprintf(bufp, "lastErrorName: %s\n", tb->lastErrorName);
706 bufp += strlen(bufp);
708 (void)sprintf(bufp, "goal: %d\n", tb->goal);
709 bufp += strlen(bufp);
710 (void)sprintf(bufp, "END bnode\n");
711 bufp += strlen(bufp);
712 len = (int)(bufp - buffer);
713 if (write(fd, buffer, len) < 0) {
719 hdl_notifier(struct bnode_proc *tp)
721 #ifndef AFS_NT40_ENV /* NT notifier callout not yet implemented */
722 int code, pid, status;
725 if (stat(tp->bnode->notifier, &tstat)) {
726 bozo_Log("BNODE: Failed to find notifier '%s'; ignored\n",
727 tp->bnode->notifier);
730 if ((pid = fork()) == 0) {
732 struct bnode *tb = tp->bnode;
735 #if defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_SGI51_ENV)
737 #elif defined(AFS_DARWIN90_ENV)
739 #elif defined(AFS_LINUX20_ENV) || defined(AFS_AIX_ENV)
744 fout = popen(tb->notifier, "w");
746 bozo_Log("BNODE: Failed to find notifier '%s'; ignored\n",
748 perror(tb->notifier);
751 code = SendNotifierData(fileno(fout), tp);
754 } else if (pid < 0) {
755 bozo_Log("Failed to fork creating process to handle notifier '%s'\n",
756 tp->bnode->notifier);
759 #endif /* AFS_NT40_ENV */
763 /* Called by IOMGR at low priority on IOMGR's stack shortly after a SIGCHLD
764 * occurs. Wakes up bproc do redo things */
766 bnode_SoftInt(int asignal)
768 IOMGR_Cancel(bproc_pid);
772 /* Called at signal interrupt level; queues function to be called
773 * when IOMGR runs again.
776 bnode_Int(int asignal)
778 extern void bozo_ShutdownAndExit();
780 if (asignal == SIGQUIT) {
781 IOMGR_SoftSig(bozo_ShutdownAndExit, (char *)asignal);
783 IOMGR_SoftSig(bnode_SoftInt, (char *)asignal);
788 /* intialize the whole system */
793 register afs_int32 code;
794 struct sigaction newaction;
800 memset(&bnode_stats, 0, sizeof(bnode_stats));
801 LWP_InitializeProcessSupport(1, &junk); /* just in case */
803 code = LWP_CreateProcess(bproc, BNODE_LWP_STACKSIZE,
804 /* priority */ 1, (void *) /* parm */ 0,
805 "bnode-manager", &bproc_pid);
808 memset((char *)&newaction, 0, sizeof(newaction));
809 newaction.sa_handler = bnode_Int;
810 code = sigaction(SIGCHLD, &newaction, NULL);
813 code = sigaction(SIGQUIT, &newaction, NULL);
819 /* free token list returned by parseLine */
821 bnode_FreeTokens(register struct bnode_token *alist)
823 register struct bnode_token *nlist;
824 for (; alist; alist = nlist) {
835 if (x == 0 || x == ' ' || x == '\t' || x == '\n')
842 bnode_ParseLine(char *aline, struct bnode_token **alist)
847 struct bnode_token *first, *last;
848 register struct bnode_token *ttok;
851 inToken = 0; /* not copying token chars at start */
852 first = (struct bnode_token *)0;
853 last = (struct bnode_token *)0;
856 if (tc == 0 || space(tc)) { /* terminating null gets us in here, too */
858 inToken = 0; /* end of this token */
861 (struct bnode_token *)malloc(sizeof(struct bnode_token));
862 ttok->next = (struct bnode_token *)0;
863 ttok->key = (char *)malloc(strlen(tbuffer) + 1);
864 strcpy(ttok->key, tbuffer);
874 /* an alpha character */
879 if (tptr - tbuffer >= sizeof(tbuffer))
880 return -1; /* token too long */
884 /* last token flushed 'cause space(0) --> true */
886 last->next = (struct bnode_token *)0;
895 bnode_NewProc(struct bnode *abnode, char *aexecString, char *coreName,
896 struct bnode_proc **aproc)
898 struct bnode_token *tlist, *tt;
900 struct bnode_proc *tp;
902 char *argv[MAXVARGS];
905 code = bnode_ParseLine(aexecString, &tlist); /* try parsing first */
908 tp = (struct bnode_proc *)malloc(sizeof(struct bnode_proc));
909 memset(tp, 0, sizeof(struct bnode_proc));
912 tp->comLine = aexecString;
913 tp->coreName = coreName; /* may be null */
914 abnode->procStartTime = FT_ApproxTime();
915 abnode->procStarts++;
917 /* convert linked list of tokens into argv structure */
918 for (tt = tlist, i = 0; i < (MAXVARGS - 1) && tt; tt = tt->next, i++) {
921 argv[i] = NULL; /* null-terminated */
923 cpid = spawnprocve(argv[0], argv, environ, -1);
924 osi_audit(BOSSpawnProcEvent, 0, AUD_STR, aexecString, AUD_END);
926 if (cpid == (pid_t) - 1) {
927 bozo_Log("Failed to spawn process for bnode '%s'\n", abnode->name);
928 bnode_FreeTokens(tlist);
933 bnode_FreeTokens(tlist);
937 tp->flags = BPROC_STARTED;
938 tp->flags &= ~BPROC_EXITED;
944 bnode_StopProc(register struct bnode_proc *aproc, int asignal)
947 if (!(aproc->flags & BPROC_STARTED) || (aproc->flags & BPROC_EXITED))
950 osi_audit(BOSStopProcEvent, 0, AUD_STR, (aproc ? aproc->comLine : NULL),
953 code = kill(aproc->pid, asignal);
954 bnode_Check(aproc->bnode);
959 bnode_Deactivate(register struct bnode *abnode)
961 register struct bnode **pb, *tb;
963 if (!(abnode->flags & BNODE_ACTIVE))
965 for (pb = &allBnodes, tb = *pb; tb; tb = nb) {
969 tb->flags &= ~BNODE_ACTIVE;