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>
19 #include <sys/types.h>
29 #ifdef BOZO_SAVE_CORES
35 #include <afs/procmgmt.h> /* signal(), kill(), wait(), etc. */
38 #include <afs/audit.h>
39 #include <afs/afsutil.h>
40 #include <afs/fileutil.h>
42 #include "bosprototypes.h"
45 #define WCOREDUMP(x) ((x) & 0200)
48 #define BNODE_LWP_STACKSIZE (16 * 1024)
50 int bnode_waiting = 0;
51 static PROCESS bproc_pid; /* pid of waker-upper */
52 static struct bnode *allBnodes = 0; /* list of all bnodes */
53 static struct bnode_proc *allProcs = 0; /* list of all processes for which we're waiting */
54 static struct bnode_type *allTypes = 0; /* list of registered type handlers */
56 static struct bnode_stats {
60 extern const char *DoCore;
62 extern char **environ; /* env structure */
65 int hdl_notifier(struct bnode_proc *tp);
67 /* Remember the name of the process, if any, that failed last */
69 RememberProcName(register struct bnode_proc *ap)
71 register struct bnode *tbnodep;
74 if (tbnodep->lastErrorName) {
75 free(tbnodep->lastErrorName);
76 tbnodep->lastErrorName = NULL;
79 tbnodep->lastErrorName = (char *)malloc(strlen(ap->coreName) + 1);
80 strcpy(tbnodep->lastErrorName, ap->coreName);
84 /* utility for use by BOP_HASCORE functions to determine where a core file might
88 bnode_CoreName(register struct bnode *abnode, char *acoreName, char *abuffer)
91 strcpy(abuffer, DoCore);
93 strcat(abuffer, AFSDIR_CORE_FILE);
95 strcpy(abuffer, AFSDIR_SERVER_CORELOG_FILEPATH);
97 strcat(abuffer, acoreName);
100 strcat(abuffer, abnode->name);
104 /* save core file, if any */
106 SaveCore(register struct bnode *abnode, register struct bnode_proc
111 register afs_int32 code = 0;
112 char *corefile = NULL;
113 #ifdef BOZO_SAVE_CORES
114 struct timeval Start;
115 struct tm *TimeFields;
119 /* Linux always appends the PID to core dumps from threaded processes, so
120 * we have to scan the directory to find core files under another name. */
122 strcpy(tbuffer, DoCore);
123 strcat(tbuffer, "/");
124 strcat(tbuffer, AFSDIR_CORE_FILE);
126 code = stat(AFSDIR_SERVER_CORELOG_FILEPATH, &tstat);
132 const char *coredir = AFSDIR_LOGS_DIR;
137 logdir = opendir(coredir);
140 while ((file = readdir(logdir)) != NULL) {
141 if (strncmp(file->d_name, "core.", 5) != 0)
143 pid = atol(file->d_name + 5);
144 if (pid == aproc->pid) {
145 length = strlen(coredir) + strlen(file->d_name) + 2;
146 corefile = malloc(length);
147 if (corefile == NULL) {
151 snprintf(corefile, length, "%s/%s", coredir, file->d_name);
158 corefile = strdup(tbuffer);
163 bnode_CoreName(abnode, aproc->coreName, tbuffer);
164 #ifdef BOZO_SAVE_CORES
165 FT_GetTimeOfDay(&Start, 0);
166 TimeFields = localtime(&Start.tv_sec);
167 sprintf(FileName, "%s.%d%02d%02d%02d%02d%02d", tbuffer,
168 TimeFields->tm_year + 1900, TimeFields->tm_mon + 1, TimeFields->tm_mday,
169 TimeFields->tm_hour, TimeFields->tm_min, TimeFields->tm_sec);
170 strcpy(tbuffer, FileName);
172 code = renamefile(corefile, tbuffer);
177 bnode_GetString(register struct bnode *abnode, register char *abuffer,
178 register afs_int32 alen)
180 return BOP_GETSTRING(abnode, abuffer, alen);
184 bnode_GetParm(register struct bnode *abnode, register afs_int32 aindex,
185 register char *abuffer, afs_int32 alen)
187 return BOP_GETPARM(abnode, aindex, abuffer, alen);
191 bnode_GetStat(register struct bnode *abnode, register afs_int32 * astatus)
193 return BOP_GETSTAT(abnode, astatus);
197 bnode_RestartP(register struct bnode *abnode)
199 return BOP_RESTARTP(abnode);
203 bnode_Check(register struct bnode *abnode)
205 if (abnode->flags & BNODE_WAIT) {
206 abnode->flags &= ~BNODE_WAIT;
207 LWP_NoYieldSignal(abnode);
212 /* tell if an instance has a core file */
214 bnode_HasCore(register struct bnode *abnode)
216 return BOP_HASCORE(abnode);
219 /* wait for all bnodes to stabilize */
223 register struct bnode *tb;
224 register afs_int32 code;
228 for (tb = allBnodes; tb; tb = tb->next) {
230 code = BOP_GETSTAT(tb, &stat);
235 if (stat != tb->goal) {
236 tb->flags |= BNODE_WAIT;
246 /* wait until bnode status is correct */
248 bnode_WaitStatus(register struct bnode *abnode, int astatus)
250 register afs_int32 code;
256 code = BOP_GETSTAT(abnode, &stat);
260 /* otherwise, check if we're done */
261 if (stat == astatus) {
262 bnode_Release(abnode);
265 if (astatus != abnode->goal) {
266 bnode_Release(abnode);
267 return -1; /* no longer our goal, don't keep waiting */
269 /* otherwise, block */
270 abnode->flags |= BNODE_WAIT;
271 LWP_WaitProcess(abnode);
276 bnode_SetStat(register struct bnode *abnode, register int agoal)
278 abnode->goal = agoal;
280 BOP_SETSTAT(abnode, agoal);
281 abnode->flags &= ~BNODE_ERRORSTOP;
286 bnode_SetGoal(register struct bnode *abnode, register int agoal)
288 abnode->goal = agoal;
294 bnode_SetFileGoal(register struct bnode *abnode, register int agoal)
296 if (abnode->fileGoal == agoal)
297 return 0; /* already done */
298 abnode->fileGoal = agoal;
303 /* apply a function to all bnodes in the system */
305 bnode_ApplyInstance(int (*aproc) (struct bnode *tb, void *), void *arock)
307 register struct bnode *tb, *nb;
308 register afs_int32 code;
310 for (tb = allBnodes; tb; tb = nb) {
312 code = (*aproc) (tb, arock);
320 bnode_FindInstance(register char *aname)
322 register struct bnode *tb;
324 for (tb = allBnodes; tb; tb = tb->next) {
325 if (!strcmp(tb->name, aname))
331 static struct bnode_type *
332 FindType(register char *aname)
334 register struct bnode_type *tt;
336 for (tt = allTypes; tt; tt = tt->next) {
337 if (!strcmp(tt->name, aname))
340 return (struct bnode_type *)0;
344 bnode_Register(char *atype, struct bnode_ops *aprocs, int anparms)
346 register struct bnode_type *tt;
348 for (tt = allTypes; tt; tt = tt->next) {
349 if (!strcmp(tt->name, atype))
353 tt = (struct bnode_type *)malloc(sizeof(struct bnode_type));
354 memset(tt, 0, sizeof(struct bnode_type));
364 bnode_Create(char *atype, char *ainstance, struct bnode ** abp, char *ap1,
365 char *ap2, char *ap3, char *ap4, char *ap5, char *notifier,
366 int fileGoal, int rewritefile)
368 struct bnode_type *type;
370 char *notifierpath = NULL;
373 if (bnode_FindInstance(ainstance))
375 type = FindType(atype);
379 if (notifier && strcmp(notifier, NONOTIFIER)) {
380 /* construct local path from canonical (wire-format) path */
381 if (ConstructLocalBinPath(notifier, ¬ifierpath)) {
382 bozo_Log("BNODE-Create: Notifier program path invalid '%s'\n",
387 if (stat(notifierpath, &tstat)) {
388 bozo_Log("BNODE-Create: Notifier program '%s' not found\n",
394 tb = (*type->ops->create) (ainstance, ap1, ap2, ap3, ap4, ap5);
399 tb->notifier = notifierpath;
403 /* The fs_create above calls bnode_InitBnode() which always sets the
404 ** fileGoal to BSTAT_NORMAL .... overwrite it with whatever is passed into
405 ** this function as a parameter... */
406 tb->fileGoal = fileGoal;
408 bnode_SetStat(tb, tb->goal); /* nudge it once */
410 if (rewritefile != 0)
417 bnode_DeleteName(char *ainstance)
419 register struct bnode *tb;
421 tb = bnode_FindInstance(ainstance);
425 return bnode_Delete(tb);
429 bnode_Hold(register struct bnode *abnode)
436 bnode_Release(register struct bnode *abnode)
439 if (abnode->refCount == 0 && abnode->flags & BNODE_DELETE) {
440 abnode->flags &= ~BNODE_DELETE; /* we're going for it */
441 bnode_Delete(abnode);
447 bnode_Delete(register struct bnode *abnode)
449 register afs_int32 code;
450 register struct bnode **lb, *ub;
453 if (abnode->refCount != 0) {
454 abnode->flags |= BNODE_DELETE;
458 /* make sure the bnode is idle before zapping */
460 code = BOP_GETSTAT(abnode, &temp);
461 bnode_Release(abnode);
464 if (temp != BSTAT_SHUTDOWN)
467 /* all clear to zap */
468 for (lb = &allBnodes, ub = *lb; ub; lb = &ub->next, ub = *lb) {
470 /* unthread it from the list */
475 free(abnode->name); /* do this first, since bnode fields may be bad after BOP_DELETE */
476 code = BOP_DELETE(abnode); /* don't play games like holding over this one */
481 /* function to tell if there's a timeout coming up */
483 bnode_PendingTimeout(register struct bnode *abnode)
485 return (abnode->flags & BNODE_NEEDTIMEOUT);
488 /* function called to set / clear periodic bnode wakeup times */
490 bnode_SetTimeout(register struct bnode *abnode, afs_int32 atimeout)
493 abnode->nextTimeout = FT_ApproxTime() + atimeout;
494 abnode->flags |= BNODE_NEEDTIMEOUT;
495 abnode->period = atimeout;
496 IOMGR_Cancel(bproc_pid);
498 abnode->flags &= ~BNODE_NEEDTIMEOUT;
503 /* used by new bnode creation code to format bnode header */
505 bnode_InitBnode(register struct bnode *abnode, struct bnode_ops *abnodeops,
508 struct bnode **lb, *nb;
510 /* format the bnode properly */
511 memset(abnode, 0, sizeof(struct bnode));
512 abnode->ops = abnodeops;
513 abnode->name = (char *)malloc(strlen(aname) + 1);
516 strcpy(abnode->name, aname);
517 abnode->flags = BNODE_ACTIVE;
518 abnode->fileGoal = BSTAT_NORMAL;
519 abnode->goal = BSTAT_SHUTDOWN;
521 /* put the bnode at the end of the list so we write bnode file in same order */
522 for (lb = &allBnodes, nb = *lb; nb; lb = &nb->next, nb = *lb);
529 DeleteProc(register struct bnode_proc *abproc)
531 register struct bnode_proc **pb, *tb;
532 struct bnode_proc *nb;
534 for (pb = &allProcs, tb = *pb; tb; pb = &tb->next, tb = nb) {
545 /* bnode lwp executes this code repeatedly */
549 register afs_int32 code;
550 register struct bnode *tb;
551 register afs_int32 temp;
552 register struct bnode_proc *tp;
554 int options; /* must not be register */
560 /* first figure out how long to sleep for */
561 temp = 0x7fffffff; /* afs_int32 time; maxint doesn't work in select */
563 for (tb = allBnodes; tb; tb = tb->next) {
564 if (tb->flags & BNODE_NEEDTIMEOUT) {
565 if (tb->nextTimeout < temp) {
567 temp = tb->nextTimeout;
571 /* now temp has the time at which we should wakeup next */
575 temp -= FT_ApproxTime(); /* how many seconds until next event */
581 code = IOMGR_Select(0, 0, 0, 0, &tv);
583 code = 0; /* fake timeout code */
585 /* figure out why we woke up; child exit or timeouts */
586 FT_GetTimeOfDay(&tv, 0); /* must do the real gettimeofday once and a while */
589 /* check all bnodes to see which ones need timeout events */
590 for (tb = allBnodes; tb; tb = nb) {
591 if ((tb->flags & BNODE_NEEDTIMEOUT) && temp > tb->nextTimeout) {
595 if (tb->flags & BNODE_NEEDTIMEOUT) { /* check again, BOP_TIMEOUT could change */
596 tb->nextTimeout = FT_ApproxTime() + tb->period;
599 bnode_Release(tb); /* delete may occur here */
605 /* signalled, probably by incoming signal */
608 bnode_waiting = options | 0x800000;
609 code = waitpid((pid_t) - 1, &status, options);
611 if (code == 0 || code == -1)
612 break; /* all done */
613 /* otherwise code has a process id, which we now search for */
614 for (tp = allProcs; tp; tp = tp->next)
622 /* count restarts in last 10 seconds */
623 if (temp > tb->rsTime + 30) {
624 /* it's been 10 seconds we've been counting */
629 if (WIFSIGNALED(status) == 0) {
630 /* exited, not signalled */
631 tp->lastExit = WEXITSTATUS(status);
634 tb->errorCode = tp->lastExit;
635 tb->lastErrorExit = FT_ApproxTime();
636 RememberProcName(tp);
640 bozo_Log("%s:%s exited with code %d\n", tb->name,
641 tp->coreName, tp->lastExit);
643 bozo_Log("%s exited with code %d\n", tb->name,
646 /* Signal occurred, perhaps spurious due to shutdown request.
647 * If due to a shutdown request, don't overwrite last error
650 tp->lastSignal = WTERMSIG(status);
652 if (tp->lastSignal != SIGQUIT
653 && tp->lastSignal != SIGTERM
654 && tp->lastSignal != SIGKILL) {
655 tb->errorSignal = tp->lastSignal;
656 tb->lastErrorExit = FT_ApproxTime();
657 RememberProcName(tp);
660 bozo_Log("%s:%s exited on signal %d%s\n",
661 tb->name, tp->coreName, tp->lastSignal,
662 WCOREDUMP(status) ? " (core dumped)" :
665 bozo_Log("%s exited on signal %d%s\n", tb->name,
667 WCOREDUMP(status) ? " (core dumped)" :
671 tb->lastAnyExit = FT_ApproxTime();
674 bozo_Log("BNODE: Notifier %s will be called\n",
678 BOP_PROCEXIT(tb, tp);
681 if (tb->rsCount++ > 10) {
682 /* 10 in 10 seconds */
683 tb->flags |= BNODE_ERRORSTOP;
684 bnode_SetGoal(tb, BSTAT_SHUTDOWN);
686 ("BNODE '%s' repeatedly failed to start, perhaps missing executable.\n",
689 bnode_Release(tb); /* bnode delete can happen here */
692 bnode_stats.weirdPids++;
700 SendNotifierData(register int fd, register struct bnode_proc *tp)
702 register struct bnode *tb = tp->bnode;
703 char buffer[1000], *bufp = buffer, *buf1;
707 * First sent out the bnode_proc struct
709 (void)sprintf(bufp, "BEGIN bnode_proc\n");
710 bufp += strlen(bufp);
711 (void)sprintf(bufp, "comLine: %s\n", tp->comLine);
712 bufp += strlen(bufp);
713 if (!(buf1 = tp->coreName))
715 (void)sprintf(bufp, "coreName: %s\n", buf1);
716 bufp += strlen(bufp);
717 (void)sprintf(bufp, "pid: %ld\n", afs_printable_int32_ld(tp->pid));
718 bufp += strlen(bufp);
719 (void)sprintf(bufp, "lastExit: %ld\n", afs_printable_int32_ld(tp->lastExit));
720 bufp += strlen(bufp);
722 (void)sprintf(bufp, "lastSignal: %ld\n", afs_printable_int32_ld(tp->lastSignal));
723 bufp += strlen(bufp);
725 (void)sprintf(bufp, "flags: %ld\n", afs_printable_int32_ld(tp->flags));
726 bufp += strlen(bufp);
727 (void)sprintf(bufp, "END bnode_proc\n");
728 bufp += strlen(bufp);
729 len = (int)(bufp - buffer);
730 if (write(fd, buffer, len) < 0) {
735 * Now sent out the bnode struct
738 (void)sprintf(bufp, "BEGIN bnode\n");
739 bufp += strlen(bufp);
740 (void)sprintf(bufp, "name: %s\n", tb->name);
741 bufp += strlen(bufp);
742 (void)sprintf(bufp, "rsTime: %ld\n", afs_printable_int32_ld(tb->rsTime));
743 bufp += strlen(bufp);
744 (void)sprintf(bufp, "rsCount: %ld\n", afs_printable_int32_ld(tb->rsCount));
745 bufp += strlen(bufp);
746 (void)sprintf(bufp, "procStartTime: %ld\n", afs_printable_int32_ld(tb->procStartTime));
747 bufp += strlen(bufp);
748 (void)sprintf(bufp, "procStarts: %ld\n", afs_printable_int32_ld(tb->procStarts));
749 bufp += strlen(bufp);
750 (void)sprintf(bufp, "lastAnyExit: %ld\n", afs_printable_int32_ld(tb->lastAnyExit));
751 bufp += strlen(bufp);
752 (void)sprintf(bufp, "lastErrorExit: %ld\n", afs_printable_int32_ld(tb->lastErrorExit));
753 bufp += strlen(bufp);
754 (void)sprintf(bufp, "errorCode: %ld\n", afs_printable_int32_ld(tb->errorCode));
755 bufp += strlen(bufp);
756 (void)sprintf(bufp, "errorSignal: %ld\n", afs_printable_int32_ld(tb->errorSignal));
757 bufp += strlen(bufp);
759 (void) sprintf(bufp, "lastErrorName: %s\n", tb->lastErrorName);
760 bufp += strlen(bufp);
762 (void)sprintf(bufp, "goal: %d\n", tb->goal);
763 bufp += strlen(bufp);
764 (void)sprintf(bufp, "END bnode\n");
765 bufp += strlen(bufp);
766 len = (int)(bufp - buffer);
767 if (write(fd, buffer, len) < 0) {
774 hdl_notifier(struct bnode_proc *tp)
776 #ifndef AFS_NT40_ENV /* NT notifier callout not yet implemented */
780 if (stat(tp->bnode->notifier, &tstat)) {
781 bozo_Log("BNODE: Failed to find notifier '%s'; ignored\n",
782 tp->bnode->notifier);
785 if ((pid = fork()) == 0) {
787 struct bnode *tb = tp->bnode;
790 #if defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_SGI51_ENV)
792 #elif defined(AFS_DARWIN90_ENV)
794 #elif defined(AFS_LINUX20_ENV) || defined(AFS_AIX_ENV)
799 fout = popen(tb->notifier, "w");
801 bozo_Log("BNODE: Failed to find notifier '%s'; ignored\n",
803 perror(tb->notifier);
806 code = SendNotifierData(fileno(fout), tp);
809 } else if (pid < 0) {
810 bozo_Log("Failed to fork creating process to handle notifier '%s'\n",
811 tp->bnode->notifier);
814 #endif /* AFS_NT40_ENV */
818 /* Called by IOMGR at low priority on IOMGR's stack shortly after a SIGCHLD
819 * occurs. Wakes up bproc do redo things */
821 bnode_SoftInt(void *param)
823 /* int asignal = (int) param; */
825 IOMGR_Cancel(bproc_pid);
829 /* Called at signal interrupt level; queues function to be called
830 * when IOMGR runs again.
833 bnode_Int(int asignal)
835 if (asignal == SIGQUIT || asignal == SIGTERM) {
836 IOMGR_SoftSig(bozo_ShutdownAndExit, (void *)(intptr_t)asignal);
838 IOMGR_SoftSig(bnode_SoftInt, (void *)(intptr_t)asignal);
843 /* intialize the whole system */
848 register afs_int32 code;
849 struct sigaction newaction;
850 static int initDone = 0;
855 memset(&bnode_stats, 0, sizeof(bnode_stats));
856 LWP_InitializeProcessSupport(1, &junk); /* just in case */
858 code = LWP_CreateProcess(bproc, BNODE_LWP_STACKSIZE,
859 /* priority */ 1, (void *) /* parm */ 0,
860 "bnode-manager", &bproc_pid);
863 memset(&newaction, 0, sizeof(newaction));
864 newaction.sa_handler = bnode_Int;
865 code = sigaction(SIGCHLD, &newaction, NULL);
868 code = sigaction(SIGQUIT, &newaction, NULL);
871 code = sigaction(SIGTERM, &newaction, NULL);
877 /* free token list returned by parseLine */
879 bnode_FreeTokens(register struct bnode_token *alist)
881 register struct bnode_token *nlist;
882 for (; alist; alist = nlist) {
893 if (x == 0 || x == ' ' || x == '\t' || x == '\n')
900 bnode_ParseLine(char *aline, struct bnode_token **alist)
903 register char *tptr = NULL;
905 struct bnode_token *first, *last;
906 register struct bnode_token *ttok;
909 inToken = 0; /* not copying token chars at start */
910 first = (struct bnode_token *)0;
911 last = (struct bnode_token *)0;
914 if (tc == 0 || space(tc)) { /* terminating null gets us in here, too */
916 inToken = 0; /* end of this token */
919 (struct bnode_token *)malloc(sizeof(struct bnode_token));
920 ttok->next = (struct bnode_token *)0;
921 ttok->key = (char *)malloc(strlen(tbuffer) + 1);
922 strcpy(ttok->key, tbuffer);
932 /* an alpha character */
937 if (tptr - tbuffer >= sizeof(tbuffer))
938 return -1; /* token too long */
942 /* last token flushed 'cause space(0) --> true */
944 last->next = (struct bnode_token *)0;
954 bnode_NewProc(struct bnode *abnode, char *aexecString, char *coreName,
955 struct bnode_proc **aproc)
957 struct bnode_token *tlist, *tt;
959 struct bnode_proc *tp;
961 char *argv[MAXVARGS];
964 code = bnode_ParseLine(aexecString, &tlist); /* try parsing first */
967 tp = (struct bnode_proc *)malloc(sizeof(struct bnode_proc));
968 memset(tp, 0, sizeof(struct bnode_proc));
971 tp->comLine = aexecString;
972 tp->coreName = coreName; /* may be null */
973 abnode->procStartTime = FT_ApproxTime();
974 abnode->procStarts++;
976 /* convert linked list of tokens into argv structure */
977 for (tt = tlist, i = 0; i < (MAXVARGS - 1) && tt; tt = tt->next, i++) {
980 argv[i] = NULL; /* null-terminated */
982 cpid = spawnprocve(argv[0], argv, environ, -1);
983 osi_audit(BOSSpawnProcEvent, 0, AUD_STR, aexecString, AUD_END);
985 if (cpid == (pid_t) - 1) {
986 bozo_Log("Failed to spawn process for bnode '%s'\n", abnode->name);
987 bnode_FreeTokens(tlist);
992 bnode_FreeTokens(tlist);
996 tp->flags = BPROC_STARTED;
997 tp->flags &= ~BPROC_EXITED;
1003 bnode_StopProc(register struct bnode_proc *aproc, int asignal)
1006 if (!(aproc->flags & BPROC_STARTED) || (aproc->flags & BPROC_EXITED))
1009 osi_audit(BOSStopProcEvent, 0, AUD_STR, (aproc ? aproc->comLine : NULL),
1012 code = kill(aproc->pid, asignal);
1013 bnode_Check(aproc->bnode);
1018 bnode_Deactivate(register struct bnode *abnode)
1020 register struct bnode **pb, *tb;
1022 if (!(abnode->flags & BNODE_ACTIVE))
1024 for (pb = &allBnodes, tb = *pb; tb; tb = nb) {
1028 tb->flags &= ~BNODE_ACTIVE;