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>
39 #include "bosprototypes.h"
41 #if defined(AFS_AIX_ENV) || defined(AFS_SUN4_ENV)
42 /* All known versions of AIX lack WCOREDUMP but this works */
43 #define WCOREDUMP(x) ((x) & 0x80)
46 #define BNODE_LWP_STACKSIZE (16 * 1024)
48 int bnode_waiting = 0;
49 static PROCESS bproc_pid; /* pid of waker-upper */
50 static struct bnode *allBnodes = 0; /* list of all bnodes */
51 static struct bnode_proc *allProcs = 0; /* list of all processes for which we're waiting */
52 static struct bnode_type *allTypes = 0; /* list of registered type handlers */
54 static struct bnode_stats {
59 extern char **environ; /* env structure */
62 /* Remember the name of the process, if any, that failed last */
64 RememberProcName(register struct bnode_proc *ap)
66 register struct bnode *tbnodep;
69 if (tbnodep->lastErrorName) {
70 free(tbnodep->lastErrorName);
71 tbnodep->lastErrorName = NULL;
74 tbnodep->lastErrorName = (char *)malloc(strlen(ap->coreName) + 1);
75 strcpy(tbnodep->lastErrorName, ap->coreName);
79 /* utility for use by BOP_HASCORE functions to determine where a core file might
83 bnode_CoreName(register struct bnode *abnode, char *acoreName, char *abuffer)
85 strcpy(abuffer, AFSDIR_SERVER_CORELOG_FILEPATH);
87 strcat(abuffer, acoreName);
90 strcat(abuffer, abnode->name);
94 /* save core file, if any */
96 SaveCore(register struct bnode *abnode, register struct bnode_proc
101 register afs_int32 code;
102 #ifdef BOZO_SAVE_CORES
103 struct timeval Start;
104 struct tm *TimeFields;
108 code = stat(AFSDIR_SERVER_CORELOG_FILEPATH, &tstat);
112 bnode_CoreName(abnode, aproc->coreName, tbuffer);
113 #ifdef BOZO_SAVE_CORES
114 TM_GetTimeOfDay(&Start, 0);
115 TimeFields = localtime(&Start.tv_sec);
116 sprintf(FileName, "%s.%d%02d%02d%02d%02d%02d", tbuffer,
117 TimeFields->tm_year, TimeFields->tm_mon + 1, TimeFields->tm_mday,
118 TimeFields->tm_hour, TimeFields->tm_min, TimeFields->tm_sec);
119 strcpy(tbuffer, FileName);
121 code = renamefile(AFSDIR_SERVER_CORELOG_FILEPATH, tbuffer);
125 bnode_GetString(register struct bnode *abnode, register char *abuffer,
126 register afs_int32 alen)
128 return BOP_GETSTRING(abnode, abuffer, alen);
132 bnode_GetParm(register struct bnode *abnode, register afs_int32 aindex,
133 register char *abuffer, afs_int32 alen)
135 return BOP_GETPARM(abnode, aindex, abuffer, alen);
139 bnode_GetStat(register struct bnode *abnode, register afs_int32 * astatus)
141 return BOP_GETSTAT(abnode, astatus);
145 bnode_RestartP(register struct bnode *abnode)
147 return BOP_RESTARTP(abnode);
151 bnode_Check(register struct bnode *abnode)
153 if (abnode->flags & BNODE_WAIT) {
154 abnode->flags &= ~BNODE_WAIT;
155 LWP_NoYieldSignal(abnode);
160 /* tell if an instance has a core file */
162 bnode_HasCore(register struct bnode *abnode)
164 return BOP_HASCORE(abnode);
167 /* wait for all bnodes to stabilize */
171 register struct bnode *tb;
172 register afs_int32 code;
176 for (tb = allBnodes; tb; tb = tb->next) {
178 code = BOP_GETSTAT(tb, &stat);
183 if (stat != tb->goal) {
184 tb->flags |= BNODE_WAIT;
194 /* wait until bnode status is correct */
196 bnode_WaitStatus(register struct bnode *abnode, int astatus)
198 register afs_int32 code;
204 code = BOP_GETSTAT(abnode, &stat);
208 /* otherwise, check if we're done */
209 if (stat == astatus) {
210 bnode_Release(abnode);
213 if (astatus != abnode->goal) {
214 bnode_Release(abnode);
215 return -1; /* no longer our goal, don't keep waiting */
217 /* otherwise, block */
218 abnode->flags |= BNODE_WAIT;
219 LWP_WaitProcess(abnode);
224 bnode_SetStat(register struct bnode *abnode, register int agoal)
226 abnode->goal = agoal;
228 BOP_SETSTAT(abnode, agoal);
229 abnode->flags &= ~BNODE_ERRORSTOP;
234 bnode_SetGoal(register struct bnode *abnode, register int agoal)
236 abnode->goal = agoal;
242 bnode_SetFileGoal(register struct bnode *abnode, register int agoal)
244 if (abnode->fileGoal == agoal)
245 return 0; /* already done */
246 abnode->fileGoal = agoal;
251 /* apply a function to all bnodes in the system */
253 bnode_ApplyInstance(int (*aproc) (), char *arock)
255 register struct bnode *tb, *nb;
256 register afs_int32 code;
258 for (tb = allBnodes; tb; tb = nb) {
260 code = (*aproc) (tb, arock);
268 bnode_FindInstance(register char *aname)
270 register struct bnode *tb;
272 for (tb = allBnodes; tb; tb = tb->next) {
273 if (!strcmp(tb->name, aname))
279 static struct bnode_type *
280 FindType(register char *aname)
282 register struct bnode_type *tt;
284 for (tt = allTypes; tt; tt = tt->next) {
285 if (!strcmp(tt->name, aname))
288 return (struct bnode_type *)0;
292 bnode_Register(char *atype, struct bnode_ops *aprocs, int anparms)
294 register struct bnode_type *tt;
296 for (tt = allTypes; tt; tt = tt->next) {
297 if (!strcmp(tt->name, atype))
301 tt = (struct bnode_type *)malloc(sizeof(struct bnode_type));
302 memset(tt, 0, sizeof(struct bnode_type));
312 bnode_Create(char *atype, char *ainstance, struct bnode ** abp, char *ap1,
313 char *ap2, char *ap3, char *ap4, char *ap5, char *notifier,
314 int fileGoal, int rewritefile)
316 struct bnode_type *type;
318 char *notifierpath = NULL;
321 if (bnode_FindInstance(ainstance))
323 type = FindType(atype);
327 if (notifier && strcmp(notifier, NONOTIFIER)) {
328 /* construct local path from canonical (wire-format) path */
329 if (ConstructLocalBinPath(notifier, ¬ifierpath)) {
330 bozo_Log("BNODE-Create: Notifier program path invalid '%s'\n",
335 if (stat(notifierpath, &tstat)) {
336 bozo_Log("BNODE-Create: Notifier program '%s' not found\n",
342 tb = (*type->ops->create) (ainstance, ap1, ap2, ap3, ap4, ap5);
347 tb->notifier = notifierpath;
351 /* The fs_create above calls bnode_InitBnode() which always sets the
352 ** fileGoal to BSTAT_NORMAL .... overwrite it with whatever is passed into
353 ** this function as a parameter... */
354 tb->fileGoal = fileGoal;
356 bnode_SetStat(tb, tb->goal); /* nudge it once */
358 if (rewritefile != 0)
365 bnode_DeleteName(char *ainstance)
367 register struct bnode *tb;
369 tb = bnode_FindInstance(ainstance);
373 return bnode_Delete(tb);
377 bnode_Hold(register struct bnode *abnode)
384 bnode_Release(register struct bnode *abnode)
387 if (abnode->refCount == 0 && abnode->flags & BNODE_DELETE) {
388 abnode->flags &= ~BNODE_DELETE; /* we're going for it */
389 bnode_Delete(abnode);
395 bnode_Delete(register struct bnode *abnode)
397 register afs_int32 code;
398 register struct bnode **lb, *ub;
401 if (abnode->refCount != 0) {
402 abnode->flags |= BNODE_DELETE;
406 /* make sure the bnode is idle before zapping */
408 code = BOP_GETSTAT(abnode, &temp);
409 bnode_Release(abnode);
412 if (temp != BSTAT_SHUTDOWN)
415 /* all clear to zap */
416 for (lb = &allBnodes, ub = *lb; ub; lb = &ub->next, ub = *lb) {
418 /* unthread it from the list */
423 free(abnode->name); /* do this first, since bnode fields may be bad after BOP_DELETE */
424 code = BOP_DELETE(abnode); /* don't play games like holding over this one */
429 /* function to tell if there's a timeout coming up */
431 bnode_PendingTimeout(register struct bnode *abnode)
433 return (abnode->flags & BNODE_NEEDTIMEOUT);
436 /* function called to set / clear periodic bnode wakeup times */
438 bnode_SetTimeout(register struct bnode *abnode, afs_int32 atimeout)
441 abnode->nextTimeout = FT_ApproxTime() + atimeout;
442 abnode->flags |= BNODE_NEEDTIMEOUT;
443 abnode->period = atimeout;
444 IOMGR_Cancel(bproc_pid);
446 abnode->flags &= ~BNODE_NEEDTIMEOUT;
451 /* used by new bnode creation code to format bnode header */
453 bnode_InitBnode(register struct bnode *abnode, struct bnode_ops *abnodeops,
456 struct bnode **lb, *nb;
458 /* format the bnode properly */
459 memset(abnode, 0, sizeof(struct bnode));
460 abnode->ops = abnodeops;
461 abnode->name = (char *)malloc(strlen(aname) + 1);
464 strcpy(abnode->name, aname);
465 abnode->flags = BNODE_ACTIVE;
466 abnode->fileGoal = BSTAT_NORMAL;
467 abnode->goal = BSTAT_SHUTDOWN;
469 /* put the bnode at the end of the list so we write bnode file in same order */
470 for (lb = &allBnodes, nb = *lb; nb; lb = &nb->next, nb = *lb);
477 DeleteProc(register struct bnode_proc *abproc)
479 register struct bnode_proc **pb, *tb;
480 struct bnode_proc *nb;
482 for (pb = &allProcs, tb = *pb; tb; pb = &tb->next, tb = nb) {
493 /* bnode lwp executes this code repeatedly */
497 register afs_int32 code;
498 register struct bnode *tb;
499 register afs_int32 temp;
500 register struct bnode_proc *tp;
502 int options; /* must not be register */
508 /* first figure out how long to sleep for */
509 temp = 0x7fffffff; /* afs_int32 time; maxint doesn't work in select */
511 for (tb = allBnodes; tb; tb = tb->next) {
512 if (tb->flags & BNODE_NEEDTIMEOUT) {
513 if (tb->nextTimeout < temp) {
515 temp = tb->nextTimeout;
519 /* now temp has the time at which we should wakeup next */
523 temp -= FT_ApproxTime(); /* how many seconds until next event */
529 code = IOMGR_Select(0, 0, 0, 0, &tv);
531 code = 0; /* fake timeout code */
533 /* figure out why we woke up; child exit or timeouts */
534 FT_GetTimeOfDay(&tv, 0); /* must do the real gettimeofday once and a while */
537 /* check all bnodes to see which ones need timeout events */
538 for (tb = allBnodes; tb; tb = nb) {
539 if ((tb->flags & BNODE_NEEDTIMEOUT) && temp > tb->nextTimeout) {
543 if (tb->flags & BNODE_NEEDTIMEOUT) { /* check again, BOP_TIMEOUT could change */
544 tb->nextTimeout = FT_ApproxTime() + tb->period;
547 bnode_Release(tb); /* delete may occur here */
553 /* signalled, probably by incoming signal */
556 bnode_waiting = options | 0x800000;
557 code = waitpid((pid_t) - 1, &status, options);
559 if (code == 0 || code == -1)
560 break; /* all done */
561 /* otherwise code has a process id, which we now search for */
562 for (tp = allProcs; tp; tp = tp->next)
570 /* count restarts in last 10 seconds */
571 if (temp > tb->rsTime + 30) {
572 /* it's been 10 seconds we've been counting */
577 if (WIFSIGNALED(status) == 0) {
578 /* exited, not signalled */
579 tp->lastExit = WEXITSTATUS(status);
582 tb->errorCode = tp->lastExit;
583 tb->lastErrorExit = FT_ApproxTime();
584 RememberProcName(tp);
588 bozo_Log("%s:%s exited with code %d\n", tb->name,
589 tp->coreName, tp->lastExit);
591 bozo_Log("%s exited with code %d\n", tb->name,
594 /* Signal occurred, perhaps spurious due to shutdown request.
595 * If due to a shutdown request, don't overwrite last error
598 tp->lastSignal = WTERMSIG(status);
600 if (tp->lastSignal != SIGQUIT
601 && tp->lastSignal != SIGTERM
602 && tp->lastSignal != SIGKILL) {
603 tb->errorSignal = tp->lastSignal;
604 tb->lastErrorExit = FT_ApproxTime();
605 RememberProcName(tp);
608 bozo_Log("%s:%s exited on signal %d%s\n",
609 tb->name, tp->coreName, tp->lastSignal,
610 WCOREDUMP(status) ? " (core dumped)" :
613 bozo_Log("%s exited on signal %d%s\n", tb->name,
615 WCOREDUMP(status) ? " (core dumped)" :
619 tb->lastAnyExit = FT_ApproxTime();
622 bozo_Log("BNODE: Notifier %s will be called\n",
626 BOP_PROCEXIT(tb, tp);
629 if (tb->rsCount++ > 10) {
630 /* 10 in 10 seconds */
631 tb->flags |= BNODE_ERRORSTOP;
632 bnode_SetGoal(tb, BSTAT_SHUTDOWN);
634 ("BNODE '%s' repeatedly failed to start, perhaps missing executable.\n",
637 bnode_Release(tb); /* bnode delete can happen here */
640 bnode_stats.weirdPids++;
647 SendNotifierData(register int fd, register struct bnode_proc *tp)
649 register struct bnode *tb = tp->bnode;
650 char buffer[1000], *bufp = buffer, *buf1;
654 * First sent out the bnode_proc struct
656 (void)sprintf(bufp, "BEGIN bnode_proc\n");
657 bufp += strlen(bufp);
658 (void)sprintf(bufp, "comLine: %s\n", tp->comLine);
659 bufp += strlen(bufp);
660 if (!(buf1 = tp->coreName))
662 (void)sprintf(bufp, "coreName: %s\n", buf1);
663 bufp += strlen(bufp);
664 (void)sprintf(bufp, "pid: %ld\n", tp->pid);
665 bufp += strlen(bufp);
666 (void)sprintf(bufp, "lastExit: %ld\n", tp->lastExit);
667 bufp += strlen(bufp);
669 (void)sprintf(bufp, "lastSignal: %ld\n", tp->lastSignal);
670 bufp += strlen(bufp);
672 (void)sprintf(bufp, "flags: %ld\n", tp->flags);
673 bufp += strlen(bufp);
674 (void)sprintf(bufp, "END bnode_proc\n");
675 bufp += strlen(bufp);
676 len = (int)(bufp - buffer);
677 if (write(fd, buffer, len) < 0) {
682 * Now sent out the bnode struct
685 (void)sprintf(bufp, "BEGIN bnode\n");
686 bufp += strlen(bufp);
687 (void)sprintf(bufp, "name: %s\n", tb->name);
688 bufp += strlen(bufp);
689 (void)sprintf(bufp, "rsTime: %ld\n", tb->rsTime);
690 bufp += strlen(bufp);
691 (void)sprintf(bufp, "rsCount: %ld\n", tb->rsCount);
692 bufp += strlen(bufp);
693 (void)sprintf(bufp, "procStartTime: %ld\n", tb->procStartTime);
694 bufp += strlen(bufp);
695 (void)sprintf(bufp, "procStarts: %ld\n", tb->procStarts);
696 bufp += strlen(bufp);
697 (void)sprintf(bufp, "lastAnyExit: %ld\n", tb->lastAnyExit);
698 bufp += strlen(bufp);
699 (void)sprintf(bufp, "lastErrorExit: %ld\n", tb->lastErrorExit);
700 bufp += strlen(bufp);
701 (void)sprintf(bufp, "errorCode: %ld\n", tb->errorCode);
702 bufp += strlen(bufp);
703 (void)sprintf(bufp, "errorSignal: %ld\n", tb->errorSignal);
704 bufp += strlen(bufp);
706 (void) sprintf(bufp, "lastErrorName: %s\n", tb->lastErrorName);
707 bufp += strlen(bufp);
709 (void)sprintf(bufp, "goal: %d\n", tb->goal);
710 bufp += strlen(bufp);
711 (void)sprintf(bufp, "END bnode\n");
712 bufp += strlen(bufp);
713 len = (int)(bufp - buffer);
714 if (write(fd, buffer, len) < 0) {
720 hdl_notifier(struct bnode_proc *tp)
722 #ifndef AFS_NT40_ENV /* NT notifier callout not yet implemented */
723 int code, pid, status;
726 if (stat(tp->bnode->notifier, &tstat)) {
727 bozo_Log("BNODE: Failed to find notifier '%s'; ignored\n",
728 tp->bnode->notifier);
731 if ((pid = fork()) == 0) {
733 struct bnode *tb = tp->bnode;
736 #if defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_SGI51_ENV)
738 #elif defined(AFS_DARWIN90_ENV)
740 #elif defined(AFS_LINUX20_ENV) || defined(AFS_AIX_ENV)
745 fout = popen(tb->notifier, "w");
747 bozo_Log("BNODE: Failed to find notifier '%s'; ignored\n",
749 perror(tb->notifier);
752 code = SendNotifierData(fileno(fout), tp);
755 } else if (pid < 0) {
756 bozo_Log("Failed to fork creating process to handle notifier '%s'\n",
757 tp->bnode->notifier);
760 #endif /* AFS_NT40_ENV */
764 /* Called by IOMGR at low priority on IOMGR's stack shortly after a SIGCHLD
765 * occurs. Wakes up bproc do redo things */
767 bnode_SoftInt(int asignal)
769 IOMGR_Cancel(bproc_pid);
773 /* Called at signal interrupt level; queues function to be called
774 * when IOMGR runs again.
777 bnode_Int(int asignal)
779 extern void bozo_ShutdownAndExit();
781 if (asignal == SIGQUIT) {
782 IOMGR_SoftSig(bozo_ShutdownAndExit, (char *)asignal);
784 IOMGR_SoftSig(bnode_SoftInt, (char *)asignal);
789 /* intialize the whole system */
794 register afs_int32 code;
795 struct sigaction newaction;
801 memset(&bnode_stats, 0, sizeof(bnode_stats));
802 LWP_InitializeProcessSupport(1, &junk); /* just in case */
804 code = LWP_CreateProcess(bproc, BNODE_LWP_STACKSIZE,
805 /* priority */ 1, (void *) /* parm */ 0,
806 "bnode-manager", &bproc_pid);
809 memset((char *)&newaction, 0, sizeof(newaction));
810 newaction.sa_handler = bnode_Int;
811 code = sigaction(SIGCHLD, &newaction, NULL);
814 code = sigaction(SIGQUIT, &newaction, NULL);
820 /* free token list returned by parseLine */
822 bnode_FreeTokens(register struct bnode_token *alist)
824 register struct bnode_token *nlist;
825 for (; alist; alist = nlist) {
836 if (x == 0 || x == ' ' || x == '\t' || x == '\n')
843 bnode_ParseLine(char *aline, struct bnode_token **alist)
848 struct bnode_token *first, *last;
849 register struct bnode_token *ttok;
852 inToken = 0; /* not copying token chars at start */
853 first = (struct bnode_token *)0;
854 last = (struct bnode_token *)0;
857 if (tc == 0 || space(tc)) { /* terminating null gets us in here, too */
859 inToken = 0; /* end of this token */
862 (struct bnode_token *)malloc(sizeof(struct bnode_token));
863 ttok->next = (struct bnode_token *)0;
864 ttok->key = (char *)malloc(strlen(tbuffer) + 1);
865 strcpy(ttok->key, tbuffer);
875 /* an alpha character */
880 if (tptr - tbuffer >= sizeof(tbuffer))
881 return -1; /* token too long */
885 /* last token flushed 'cause space(0) --> true */
887 last->next = (struct bnode_token *)0;
896 bnode_NewProc(struct bnode *abnode, char *aexecString, char *coreName,
897 struct bnode_proc **aproc)
899 struct bnode_token *tlist, *tt;
901 struct bnode_proc *tp;
903 char *argv[MAXVARGS];
906 code = bnode_ParseLine(aexecString, &tlist); /* try parsing first */
909 tp = (struct bnode_proc *)malloc(sizeof(struct bnode_proc));
910 memset(tp, 0, sizeof(struct bnode_proc));
913 tp->comLine = aexecString;
914 tp->coreName = coreName; /* may be null */
915 abnode->procStartTime = FT_ApproxTime();
916 abnode->procStarts++;
918 /* convert linked list of tokens into argv structure */
919 for (tt = tlist, i = 0; i < (MAXVARGS - 1) && tt; tt = tt->next, i++) {
922 argv[i] = NULL; /* null-terminated */
924 cpid = spawnprocve(argv[0], argv, environ, -1);
925 osi_audit(BOSSpawnProcEvent, 0, AUD_STR, aexecString, AUD_END);
927 if (cpid == (pid_t) - 1) {
928 bozo_Log("Failed to spawn process for bnode '%s'\n", abnode->name);
929 bnode_FreeTokens(tlist);
934 bnode_FreeTokens(tlist);
938 tp->flags = BPROC_STARTED;
939 tp->flags &= ~BPROC_EXITED;
945 bnode_StopProc(register struct bnode_proc *aproc, int asignal)
948 if (!(aproc->flags & BPROC_STARTED) || (aproc->flags & BPROC_EXITED))
951 osi_audit(BOSStopProcEvent, 0, AUD_STR, (aproc ? aproc->comLine : NULL),
954 code = kill(aproc->pid, asignal);
955 bnode_Check(aproc->bnode);
960 bnode_Deactivate(register struct bnode *abnode)
962 register struct bnode **pb, *tb;
964 if (!(abnode->flags & BNODE_ACTIVE))
966 for (pb = &allBnodes, tb = *pb; tb; tb = nb) {
970 tb->flags &= ~BNODE_ACTIVE;