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>
26 #ifdef BOZO_SAVE_CORES
32 #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"
42 #define WCOREDUMP(x) ((x) & 0200)
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 int hdl_notifier(struct bnode_proc *tp);
63 /* Remember the name of the process, if any, that failed last */
65 RememberProcName(register struct bnode_proc *ap)
67 register struct bnode *tbnodep;
70 if (tbnodep->lastErrorName) {
71 free(tbnodep->lastErrorName);
72 tbnodep->lastErrorName = NULL;
75 tbnodep->lastErrorName = (char *)malloc(strlen(ap->coreName) + 1);
76 strcpy(tbnodep->lastErrorName, ap->coreName);
80 /* utility for use by BOP_HASCORE functions to determine where a core file might
84 bnode_CoreName(register struct bnode *abnode, char *acoreName, char *abuffer)
86 strcpy(abuffer, AFSDIR_SERVER_CORELOG_FILEPATH);
88 strcat(abuffer, acoreName);
91 strcat(abuffer, abnode->name);
95 /* save core file, if any */
97 SaveCore(register struct bnode *abnode, register struct bnode_proc
102 register afs_int32 code;
103 char *corefile = NULL;
104 #ifdef BOZO_SAVE_CORES
105 struct timeval Start;
106 struct tm *TimeFields;
110 /* Linux always appends the PID to core dumps from threaded processes, so
111 * we have to scan the directory to find core files under another name. */
112 code = stat(AFSDIR_SERVER_CORELOG_FILEPATH, &tstat);
119 logdir = opendir(AFSDIR_LOGS_DIR);
122 while ((file = readdir(logdir)) != NULL) {
123 if (strncmp(file->d_name, "core.", 5) != 0)
125 pid = atol(file->d_name + 5);
126 if (pid == aproc->pid) {
127 length = strlen(AFSDIR_LOGS_DIR) + strlen(file->d_name) + 2;
128 corefile = malloc(length);
129 if (corefile == NULL) {
133 snprintf(corefile, length, "%s/%s", AFSDIR_LOGS_DIR,
144 bnode_CoreName(abnode, aproc->coreName, tbuffer);
145 #ifdef BOZO_SAVE_CORES
146 FT_GetTimeOfDay(&Start, 0);
147 TimeFields = localtime(&Start.tv_sec);
148 sprintf(FileName, "%s.%d%02d%02d%02d%02d%02d", tbuffer,
149 TimeFields->tm_year + 1900, TimeFields->tm_mon + 1, TimeFields->tm_mday,
150 TimeFields->tm_hour, TimeFields->tm_min, TimeFields->tm_sec);
151 strcpy(tbuffer, FileName);
153 if (corefile == NULL)
154 code = renamefile(AFSDIR_SERVER_CORELOG_FILEPATH, tbuffer);
156 code = renamefile(corefile, tbuffer);
162 bnode_GetString(register struct bnode *abnode, register char *abuffer,
163 register afs_int32 alen)
165 return BOP_GETSTRING(abnode, abuffer, alen);
169 bnode_GetParm(register struct bnode *abnode, register afs_int32 aindex,
170 register char *abuffer, afs_int32 alen)
172 return BOP_GETPARM(abnode, aindex, abuffer, alen);
176 bnode_GetStat(register struct bnode *abnode, register afs_int32 * astatus)
178 return BOP_GETSTAT(abnode, astatus);
182 bnode_RestartP(register struct bnode *abnode)
184 return BOP_RESTARTP(abnode);
188 bnode_Check(register struct bnode *abnode)
190 if (abnode->flags & BNODE_WAIT) {
191 abnode->flags &= ~BNODE_WAIT;
192 LWP_NoYieldSignal(abnode);
197 /* tell if an instance has a core file */
199 bnode_HasCore(register struct bnode *abnode)
201 return BOP_HASCORE(abnode);
204 /* wait for all bnodes to stabilize */
208 register struct bnode *tb;
209 register afs_int32 code;
213 for (tb = allBnodes; tb; tb = tb->next) {
215 code = BOP_GETSTAT(tb, &stat);
220 if (stat != tb->goal) {
221 tb->flags |= BNODE_WAIT;
231 /* wait until bnode status is correct */
233 bnode_WaitStatus(register struct bnode *abnode, int astatus)
235 register afs_int32 code;
241 code = BOP_GETSTAT(abnode, &stat);
245 /* otherwise, check if we're done */
246 if (stat == astatus) {
247 bnode_Release(abnode);
250 if (astatus != abnode->goal) {
251 bnode_Release(abnode);
252 return -1; /* no longer our goal, don't keep waiting */
254 /* otherwise, block */
255 abnode->flags |= BNODE_WAIT;
256 LWP_WaitProcess(abnode);
261 bnode_SetStat(register struct bnode *abnode, register int agoal)
263 abnode->goal = agoal;
265 BOP_SETSTAT(abnode, agoal);
266 abnode->flags &= ~BNODE_ERRORSTOP;
271 bnode_SetGoal(register struct bnode *abnode, register int agoal)
273 abnode->goal = agoal;
279 bnode_SetFileGoal(register struct bnode *abnode, register int agoal)
281 if (abnode->fileGoal == agoal)
282 return 0; /* already done */
283 abnode->fileGoal = agoal;
288 /* apply a function to all bnodes in the system */
290 bnode_ApplyInstance(int (*aproc) (struct bnode *tb, void *), void *arock)
292 register struct bnode *tb, *nb;
293 register afs_int32 code;
295 for (tb = allBnodes; tb; tb = nb) {
297 code = (*aproc) (tb, arock);
305 bnode_FindInstance(register char *aname)
307 register struct bnode *tb;
309 for (tb = allBnodes; tb; tb = tb->next) {
310 if (!strcmp(tb->name, aname))
316 static struct bnode_type *
317 FindType(register char *aname)
319 register struct bnode_type *tt;
321 for (tt = allTypes; tt; tt = tt->next) {
322 if (!strcmp(tt->name, aname))
325 return (struct bnode_type *)0;
329 bnode_Register(char *atype, struct bnode_ops *aprocs, int anparms)
331 register struct bnode_type *tt;
333 for (tt = allTypes; tt; tt = tt->next) {
334 if (!strcmp(tt->name, atype))
338 tt = (struct bnode_type *)malloc(sizeof(struct bnode_type));
339 memset(tt, 0, sizeof(struct bnode_type));
349 bnode_Create(char *atype, char *ainstance, struct bnode ** abp, char *ap1,
350 char *ap2, char *ap3, char *ap4, char *ap5, char *notifier,
351 int fileGoal, int rewritefile)
353 struct bnode_type *type;
355 char *notifierpath = NULL;
358 if (bnode_FindInstance(ainstance))
360 type = FindType(atype);
364 if (notifier && strcmp(notifier, NONOTIFIER)) {
365 /* construct local path from canonical (wire-format) path */
366 if (ConstructLocalBinPath(notifier, ¬ifierpath)) {
367 bozo_Log("BNODE-Create: Notifier program path invalid '%s'\n",
372 if (stat(notifierpath, &tstat)) {
373 bozo_Log("BNODE-Create: Notifier program '%s' not found\n",
379 tb = (*type->ops->create) (ainstance, ap1, ap2, ap3, ap4, ap5);
384 tb->notifier = notifierpath;
388 /* The fs_create above calls bnode_InitBnode() which always sets the
389 ** fileGoal to BSTAT_NORMAL .... overwrite it with whatever is passed into
390 ** this function as a parameter... */
391 tb->fileGoal = fileGoal;
393 bnode_SetStat(tb, tb->goal); /* nudge it once */
395 if (rewritefile != 0)
402 bnode_DeleteName(char *ainstance)
404 register struct bnode *tb;
406 tb = bnode_FindInstance(ainstance);
410 return bnode_Delete(tb);
414 bnode_Hold(register struct bnode *abnode)
421 bnode_Release(register struct bnode *abnode)
424 if (abnode->refCount == 0 && abnode->flags & BNODE_DELETE) {
425 abnode->flags &= ~BNODE_DELETE; /* we're going for it */
426 bnode_Delete(abnode);
432 bnode_Delete(register struct bnode *abnode)
434 register afs_int32 code;
435 register struct bnode **lb, *ub;
438 if (abnode->refCount != 0) {
439 abnode->flags |= BNODE_DELETE;
443 /* make sure the bnode is idle before zapping */
445 code = BOP_GETSTAT(abnode, &temp);
446 bnode_Release(abnode);
449 if (temp != BSTAT_SHUTDOWN)
452 /* all clear to zap */
453 for (lb = &allBnodes, ub = *lb; ub; lb = &ub->next, ub = *lb) {
455 /* unthread it from the list */
460 free(abnode->name); /* do this first, since bnode fields may be bad after BOP_DELETE */
461 code = BOP_DELETE(abnode); /* don't play games like holding over this one */
466 /* function to tell if there's a timeout coming up */
468 bnode_PendingTimeout(register struct bnode *abnode)
470 return (abnode->flags & BNODE_NEEDTIMEOUT);
473 /* function called to set / clear periodic bnode wakeup times */
475 bnode_SetTimeout(register struct bnode *abnode, afs_int32 atimeout)
478 abnode->nextTimeout = FT_ApproxTime() + atimeout;
479 abnode->flags |= BNODE_NEEDTIMEOUT;
480 abnode->period = atimeout;
481 IOMGR_Cancel(bproc_pid);
483 abnode->flags &= ~BNODE_NEEDTIMEOUT;
488 /* used by new bnode creation code to format bnode header */
490 bnode_InitBnode(register struct bnode *abnode, struct bnode_ops *abnodeops,
493 struct bnode **lb, *nb;
495 /* format the bnode properly */
496 memset(abnode, 0, sizeof(struct bnode));
497 abnode->ops = abnodeops;
498 abnode->name = (char *)malloc(strlen(aname) + 1);
501 strcpy(abnode->name, aname);
502 abnode->flags = BNODE_ACTIVE;
503 abnode->fileGoal = BSTAT_NORMAL;
504 abnode->goal = BSTAT_SHUTDOWN;
506 /* put the bnode at the end of the list so we write bnode file in same order */
507 for (lb = &allBnodes, nb = *lb; nb; lb = &nb->next, nb = *lb);
514 DeleteProc(register struct bnode_proc *abproc)
516 register struct bnode_proc **pb, *tb;
517 struct bnode_proc *nb;
519 for (pb = &allProcs, tb = *pb; tb; pb = &tb->next, tb = nb) {
530 /* bnode lwp executes this code repeatedly */
534 register afs_int32 code;
535 register struct bnode *tb;
536 register afs_int32 temp;
537 register struct bnode_proc *tp;
539 int options; /* must not be register */
545 /* first figure out how long to sleep for */
546 temp = 0x7fffffff; /* afs_int32 time; maxint doesn't work in select */
548 for (tb = allBnodes; tb; tb = tb->next) {
549 if (tb->flags & BNODE_NEEDTIMEOUT) {
550 if (tb->nextTimeout < temp) {
552 temp = tb->nextTimeout;
556 /* now temp has the time at which we should wakeup next */
560 temp -= FT_ApproxTime(); /* how many seconds until next event */
566 code = IOMGR_Select(0, 0, 0, 0, &tv);
568 code = 0; /* fake timeout code */
570 /* figure out why we woke up; child exit or timeouts */
571 FT_GetTimeOfDay(&tv, 0); /* must do the real gettimeofday once and a while */
574 /* check all bnodes to see which ones need timeout events */
575 for (tb = allBnodes; tb; tb = nb) {
576 if ((tb->flags & BNODE_NEEDTIMEOUT) && temp > tb->nextTimeout) {
580 if (tb->flags & BNODE_NEEDTIMEOUT) { /* check again, BOP_TIMEOUT could change */
581 tb->nextTimeout = FT_ApproxTime() + tb->period;
584 bnode_Release(tb); /* delete may occur here */
590 /* signalled, probably by incoming signal */
593 bnode_waiting = options | 0x800000;
594 code = waitpid((pid_t) - 1, &status, options);
596 if (code == 0 || code == -1)
597 break; /* all done */
598 /* otherwise code has a process id, which we now search for */
599 for (tp = allProcs; tp; tp = tp->next)
607 /* count restarts in last 10 seconds */
608 if (temp > tb->rsTime + 30) {
609 /* it's been 10 seconds we've been counting */
614 if (WIFSIGNALED(status) == 0) {
615 /* exited, not signalled */
616 tp->lastExit = WEXITSTATUS(status);
619 tb->errorCode = tp->lastExit;
620 tb->lastErrorExit = FT_ApproxTime();
621 RememberProcName(tp);
625 bozo_Log("%s:%s exited with code %d\n", tb->name,
626 tp->coreName, tp->lastExit);
628 bozo_Log("%s exited with code %d\n", tb->name,
631 /* Signal occurred, perhaps spurious due to shutdown request.
632 * If due to a shutdown request, don't overwrite last error
635 tp->lastSignal = WTERMSIG(status);
637 if (tp->lastSignal != SIGQUIT
638 && tp->lastSignal != SIGTERM
639 && tp->lastSignal != SIGKILL) {
640 tb->errorSignal = tp->lastSignal;
641 tb->lastErrorExit = FT_ApproxTime();
642 RememberProcName(tp);
645 bozo_Log("%s:%s exited on signal %d%s\n",
646 tb->name, tp->coreName, tp->lastSignal,
647 WCOREDUMP(status) ? " (core dumped)" :
650 bozo_Log("%s exited on signal %d%s\n", tb->name,
652 WCOREDUMP(status) ? " (core dumped)" :
656 tb->lastAnyExit = FT_ApproxTime();
659 bozo_Log("BNODE: Notifier %s will be called\n",
663 BOP_PROCEXIT(tb, tp);
666 if (tb->rsCount++ > 10) {
667 /* 10 in 10 seconds */
668 tb->flags |= BNODE_ERRORSTOP;
669 bnode_SetGoal(tb, BSTAT_SHUTDOWN);
671 ("BNODE '%s' repeatedly failed to start, perhaps missing executable.\n",
674 bnode_Release(tb); /* bnode delete can happen here */
677 bnode_stats.weirdPids++;
685 SendNotifierData(register int fd, register struct bnode_proc *tp)
687 register struct bnode *tb = tp->bnode;
688 char buffer[1000], *bufp = buffer, *buf1;
692 * First sent out the bnode_proc struct
694 (void)sprintf(bufp, "BEGIN bnode_proc\n");
695 bufp += strlen(bufp);
696 (void)sprintf(bufp, "comLine: %s\n", tp->comLine);
697 bufp += strlen(bufp);
698 if (!(buf1 = tp->coreName))
700 (void)sprintf(bufp, "coreName: %s\n", buf1);
701 bufp += strlen(bufp);
702 (void)sprintf(bufp, "pid: %ld\n", afs_printable_int32_ld(tp->pid));
703 bufp += strlen(bufp);
704 (void)sprintf(bufp, "lastExit: %ld\n", afs_printable_int32_ld(tp->lastExit));
705 bufp += strlen(bufp);
707 (void)sprintf(bufp, "lastSignal: %ld\n", afs_printable_int32_ld(tp->lastSignal));
708 bufp += strlen(bufp);
710 (void)sprintf(bufp, "flags: %ld\n", afs_printable_int32_ld(tp->flags));
711 bufp += strlen(bufp);
712 (void)sprintf(bufp, "END bnode_proc\n");
713 bufp += strlen(bufp);
714 len = (int)(bufp - buffer);
715 if (write(fd, buffer, len) < 0) {
720 * Now sent out the bnode struct
723 (void)sprintf(bufp, "BEGIN bnode\n");
724 bufp += strlen(bufp);
725 (void)sprintf(bufp, "name: %s\n", tb->name);
726 bufp += strlen(bufp);
727 (void)sprintf(bufp, "rsTime: %ld\n", afs_printable_int32_ld(tb->rsTime));
728 bufp += strlen(bufp);
729 (void)sprintf(bufp, "rsCount: %ld\n", afs_printable_int32_ld(tb->rsCount));
730 bufp += strlen(bufp);
731 (void)sprintf(bufp, "procStartTime: %ld\n", afs_printable_int32_ld(tb->procStartTime));
732 bufp += strlen(bufp);
733 (void)sprintf(bufp, "procStarts: %ld\n", afs_printable_int32_ld(tb->procStarts));
734 bufp += strlen(bufp);
735 (void)sprintf(bufp, "lastAnyExit: %ld\n", afs_printable_int32_ld(tb->lastAnyExit));
736 bufp += strlen(bufp);
737 (void)sprintf(bufp, "lastErrorExit: %ld\n", afs_printable_int32_ld(tb->lastErrorExit));
738 bufp += strlen(bufp);
739 (void)sprintf(bufp, "errorCode: %ld\n", afs_printable_int32_ld(tb->errorCode));
740 bufp += strlen(bufp);
741 (void)sprintf(bufp, "errorSignal: %ld\n", afs_printable_int32_ld(tb->errorSignal));
742 bufp += strlen(bufp);
744 (void) sprintf(bufp, "lastErrorName: %s\n", tb->lastErrorName);
745 bufp += strlen(bufp);
747 (void)sprintf(bufp, "goal: %d\n", tb->goal);
748 bufp += strlen(bufp);
749 (void)sprintf(bufp, "END bnode\n");
750 bufp += strlen(bufp);
751 len = (int)(bufp - buffer);
752 if (write(fd, buffer, len) < 0) {
759 hdl_notifier(struct bnode_proc *tp)
761 #ifndef AFS_NT40_ENV /* NT notifier callout not yet implemented */
765 if (stat(tp->bnode->notifier, &tstat)) {
766 bozo_Log("BNODE: Failed to find notifier '%s'; ignored\n",
767 tp->bnode->notifier);
770 if ((pid = fork()) == 0) {
772 struct bnode *tb = tp->bnode;
775 #if defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_SGI51_ENV)
777 #elif defined(AFS_DARWIN90_ENV)
779 #elif defined(AFS_LINUX20_ENV) || defined(AFS_AIX_ENV)
784 fout = popen(tb->notifier, "w");
786 bozo_Log("BNODE: Failed to find notifier '%s'; ignored\n",
788 perror(tb->notifier);
791 code = SendNotifierData(fileno(fout), tp);
794 } else if (pid < 0) {
795 bozo_Log("Failed to fork creating process to handle notifier '%s'\n",
796 tp->bnode->notifier);
799 #endif /* AFS_NT40_ENV */
803 /* Called by IOMGR at low priority on IOMGR's stack shortly after a SIGCHLD
804 * occurs. Wakes up bproc do redo things */
806 bnode_SoftInt(void *param)
808 /* int asignal = (int) param; */
810 IOMGR_Cancel(bproc_pid);
814 /* Called at signal interrupt level; queues function to be called
815 * when IOMGR runs again.
818 bnode_Int(int asignal)
820 if (asignal == SIGQUIT) {
821 IOMGR_SoftSig(bozo_ShutdownAndExit, (void *)(intptr_t)asignal);
823 IOMGR_SoftSig(bnode_SoftInt, (void *)(intptr_t)asignal);
828 /* intialize the whole system */
833 register afs_int32 code;
834 struct sigaction newaction;
835 static int initDone = 0;
840 memset(&bnode_stats, 0, sizeof(bnode_stats));
841 LWP_InitializeProcessSupport(1, &junk); /* just in case */
843 code = LWP_CreateProcess(bproc, BNODE_LWP_STACKSIZE,
844 /* priority */ 1, (void *) /* parm */ 0,
845 "bnode-manager", &bproc_pid);
848 memset(&newaction, 0, sizeof(newaction));
849 newaction.sa_handler = bnode_Int;
850 code = sigaction(SIGCHLD, &newaction, NULL);
853 code = sigaction(SIGQUIT, &newaction, NULL);
859 /* free token list returned by parseLine */
861 bnode_FreeTokens(register struct bnode_token *alist)
863 register struct bnode_token *nlist;
864 for (; alist; alist = nlist) {
875 if (x == 0 || x == ' ' || x == '\t' || x == '\n')
882 bnode_ParseLine(char *aline, struct bnode_token **alist)
885 register char *tptr = NULL;
887 struct bnode_token *first, *last;
888 register struct bnode_token *ttok;
891 inToken = 0; /* not copying token chars at start */
892 first = (struct bnode_token *)0;
893 last = (struct bnode_token *)0;
896 if (tc == 0 || space(tc)) { /* terminating null gets us in here, too */
898 inToken = 0; /* end of this token */
901 (struct bnode_token *)malloc(sizeof(struct bnode_token));
902 ttok->next = (struct bnode_token *)0;
903 ttok->key = (char *)malloc(strlen(tbuffer) + 1);
904 strcpy(ttok->key, tbuffer);
914 /* an alpha character */
919 if (tptr - tbuffer >= sizeof(tbuffer))
920 return -1; /* token too long */
924 /* last token flushed 'cause space(0) --> true */
926 last->next = (struct bnode_token *)0;
936 bnode_NewProc(struct bnode *abnode, char *aexecString, char *coreName,
937 struct bnode_proc **aproc)
939 struct bnode_token *tlist, *tt;
941 struct bnode_proc *tp;
943 char *argv[MAXVARGS];
946 code = bnode_ParseLine(aexecString, &tlist); /* try parsing first */
949 tp = (struct bnode_proc *)malloc(sizeof(struct bnode_proc));
950 memset(tp, 0, sizeof(struct bnode_proc));
953 tp->comLine = aexecString;
954 tp->coreName = coreName; /* may be null */
955 abnode->procStartTime = FT_ApproxTime();
956 abnode->procStarts++;
958 /* convert linked list of tokens into argv structure */
959 for (tt = tlist, i = 0; i < (MAXVARGS - 1) && tt; tt = tt->next, i++) {
962 argv[i] = NULL; /* null-terminated */
964 cpid = spawnprocve(argv[0], argv, environ, -1);
965 osi_audit(BOSSpawnProcEvent, 0, AUD_STR, aexecString, AUD_END);
967 if (cpid == (pid_t) - 1) {
968 bozo_Log("Failed to spawn process for bnode '%s'\n", abnode->name);
969 bnode_FreeTokens(tlist);
974 bnode_FreeTokens(tlist);
978 tp->flags = BPROC_STARTED;
979 tp->flags &= ~BPROC_EXITED;
985 bnode_StopProc(register struct bnode_proc *aproc, int asignal)
988 if (!(aproc->flags & BPROC_STARTED) || (aproc->flags & BPROC_EXITED))
991 osi_audit(BOSStopProcEvent, 0, AUD_STR, (aproc ? aproc->comLine : NULL),
994 code = kill(aproc->pid, asignal);
995 bnode_Check(aproc->bnode);
1000 bnode_Deactivate(register struct bnode *abnode)
1002 register struct bnode **pb, *tb;
1004 if (!(abnode->flags & BNODE_ACTIVE))
1006 for (pb = &allBnodes, tb = *pb; tb; tb = nb) {
1010 tb->flags &= ~BNODE_ACTIVE;