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>
13 #include <afs/procmgmt.h>
20 #include <afs/audit.h>
21 #include <afs/afsutil.h>
22 #include <afs/fileutil.h>
25 #include "bosprototypes.h"
28 #define WCOREDUMP(x) ((x) & 0200)
31 #define BNODE_LWP_STACKSIZE (16 * 1024)
32 #define BNODE_ERROR_COUNT_MAX 16 /* maximum number of retries */
34 int bnode_waiting = 0;
35 static PROCESS bproc_pid; /* pid of waker-upper */
36 static struct bnode *allBnodes = 0; /* list of all bnodes */
37 static struct bnode_proc *allProcs = 0; /* list of all processes for which we're waiting */
38 static struct bnode_type *allTypes = 0; /* list of registered type handlers */
40 static struct bnode_stats {
44 extern const char *DoCore;
45 extern const char *DoPidFiles;
47 extern char **environ; /* env structure */
50 int hdl_notifier(struct bnode_proc *tp);
52 /* Remember the name of the process, if any, that failed last */
54 RememberProcName(struct bnode_proc *ap)
56 struct bnode *tbnodep;
59 if (tbnodep->lastErrorName) {
60 free(tbnodep->lastErrorName);
61 tbnodep->lastErrorName = NULL;
64 tbnodep->lastErrorName = strdup(ap->coreName);
67 /* utility for use by BOP_HASCORE functions to determine where a core file might
71 bnode_CoreName(struct bnode *abnode, char *acoreName, char *abuffer)
74 strcpy(abuffer, DoCore);
76 strcat(abuffer, AFSDIR_CORE_FILE);
78 strcpy(abuffer, AFSDIR_SERVER_CORELOG_FILEPATH);
80 strcat(abuffer, acoreName);
83 strcat(abuffer, abnode->name);
87 /* save core file, if any */
89 SaveCore(struct bnode *abnode, struct bnode_proc
95 char *corefile = NULL;
96 #ifdef BOZO_SAVE_CORES
98 struct tm *TimeFields;
102 /* Linux always appends the PID to core dumps from threaded processes, so
103 * we have to scan the directory to find core files under another name. */
105 strcpy(tbuffer, DoCore);
106 strcat(tbuffer, "/");
107 strcat(tbuffer, AFSDIR_CORE_FILE);
109 code = stat(AFSDIR_SERVER_CORELOG_FILEPATH, &tstat);
114 const char *coredir = AFSDIR_LOGS_DIR;
119 logdir = opendir(coredir);
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 asprintf(&corefile, "%s/%s", coredir, file->d_name);
128 if (corefile == NULL) {
138 corefile = strdup(tbuffer);
143 bnode_CoreName(abnode, aproc->coreName, tbuffer);
144 #ifdef BOZO_SAVE_CORES
145 FT_GetTimeOfDay(&Start, 0);
146 TimeFields = localtime(&Start.tv_sec);
147 sprintf(FileName, "%s.%d%02d%02d%02d%02d%02d", tbuffer,
148 TimeFields->tm_year + 1900, TimeFields->tm_mon + 1, TimeFields->tm_mday,
149 TimeFields->tm_hour, TimeFields->tm_min, TimeFields->tm_sec);
150 strcpy(tbuffer, FileName);
152 code = renamefile(corefile, tbuffer);
157 bnode_GetString(struct bnode *abnode, char *abuffer,
160 return BOP_GETSTRING(abnode, abuffer, alen);
164 bnode_GetParm(struct bnode *abnode, afs_int32 aindex,
165 char *abuffer, afs_int32 alen)
167 return BOP_GETPARM(abnode, aindex, abuffer, alen);
171 bnode_GetStat(struct bnode *abnode, afs_int32 * astatus)
173 return BOP_GETSTAT(abnode, astatus);
177 bnode_RestartP(struct bnode *abnode)
179 return BOP_RESTARTP(abnode);
183 bnode_Check(struct bnode *abnode)
185 if (abnode->flags & BNODE_WAIT) {
186 abnode->flags &= ~BNODE_WAIT;
187 LWP_NoYieldSignal(abnode);
192 /* tell if an instance has a core file */
194 bnode_HasCore(struct bnode *abnode)
196 return BOP_HASCORE(abnode);
199 /* wait for all bnodes to stabilize */
208 for (tb = allBnodes; tb; tb = tb->next) {
210 code = BOP_GETSTAT(tb, &stat);
215 if (stat != tb->goal) {
216 tb->flags |= BNODE_WAIT;
226 /* wait until bnode status is correct */
228 bnode_WaitStatus(struct bnode *abnode, int astatus)
236 code = BOP_GETSTAT(abnode, &stat);
240 /* otherwise, check if we're done */
241 if (stat == astatus) {
242 bnode_Release(abnode);
245 if (astatus != abnode->goal) {
246 bnode_Release(abnode);
247 return -1; /* no longer our goal, don't keep waiting */
249 /* otherwise, block */
250 abnode->flags |= BNODE_WAIT;
251 LWP_WaitProcess(abnode);
256 bnode_ResetErrorCount(struct bnode *abnode)
258 abnode->errorStopCount = 0;
259 abnode->errorStopDelay = 0;
264 bnode_SetStat(struct bnode *abnode, int agoal)
266 abnode->goal = agoal;
268 BOP_SETSTAT(abnode, agoal);
269 abnode->flags &= ~BNODE_ERRORSTOP;
274 bnode_SetGoal(struct bnode *abnode, int agoal)
276 abnode->goal = agoal;
282 bnode_SetFileGoal(struct bnode *abnode, int agoal)
284 if (abnode->fileGoal == agoal)
285 return 0; /* already done */
286 abnode->fileGoal = agoal;
291 /* apply a function to all bnodes in the system */
293 bnode_ApplyInstance(int (*aproc) (struct bnode *tb, void *), void *arock)
295 struct bnode *tb, *nb;
298 for (tb = allBnodes; tb; tb = nb) {
300 code = (*aproc) (tb, arock);
308 bnode_FindInstance(char *aname)
312 for (tb = allBnodes; tb; tb = tb->next) {
313 if (!strcmp(tb->name, aname))
319 static struct bnode_type *
320 FindType(char *aname)
322 struct bnode_type *tt;
324 for (tt = allTypes; tt; tt = tt->next) {
325 if (!strcmp(tt->name, aname))
328 return (struct bnode_type *)0;
332 bnode_Register(char *atype, struct bnode_ops *aprocs, int anparms)
334 struct bnode_type *tt;
336 for (tt = allTypes; tt; tt = tt->next) {
337 if (!strcmp(tt->name, atype))
341 tt = (struct bnode_type *)malloc(sizeof(struct bnode_type));
342 memset(tt, 0, sizeof(struct bnode_type));
352 bnode_Create(char *atype, char *ainstance, struct bnode ** abp, char *ap1,
353 char *ap2, char *ap3, char *ap4, char *ap5, char *notifier,
354 int fileGoal, int rewritefile)
356 struct bnode_type *type;
358 char *notifierpath = NULL;
361 if (bnode_FindInstance(ainstance))
363 type = FindType(atype);
367 if (notifier && strcmp(notifier, NONOTIFIER)) {
368 /* construct local path from canonical (wire-format) path */
369 if (ConstructLocalBinPath(notifier, ¬ifierpath)) {
370 bozo_Log("BNODE-Create: Notifier program path invalid '%s'\n",
375 if (stat(notifierpath, &tstat)) {
376 bozo_Log("BNODE-Create: Notifier program '%s' not found\n",
382 tb = (*type->ops->create) (ainstance, ap1, ap2, ap3, ap4, ap5);
387 tb->notifier = notifierpath;
391 /* The fs_create above calls bnode_InitBnode() which always sets the
392 ** fileGoal to BSTAT_NORMAL .... overwrite it with whatever is passed into
393 ** this function as a parameter... */
394 tb->fileGoal = fileGoal;
396 bnode_SetStat(tb, tb->goal); /* nudge it once */
398 if (rewritefile != 0)
405 bnode_DeleteName(char *ainstance)
409 tb = bnode_FindInstance(ainstance);
413 return bnode_Delete(tb);
417 bnode_Hold(struct bnode *abnode)
424 bnode_Release(struct bnode *abnode)
427 if (abnode->refCount == 0 && abnode->flags & BNODE_DELETE) {
428 abnode->flags &= ~BNODE_DELETE; /* we're going for it */
429 bnode_Delete(abnode);
435 bnode_Delete(struct bnode *abnode)
438 struct bnode **lb, *ub;
441 if (abnode->refCount != 0) {
442 abnode->flags |= BNODE_DELETE;
446 /* make sure the bnode is idle before zapping */
448 code = BOP_GETSTAT(abnode, &temp);
449 bnode_Release(abnode);
452 if (temp != BSTAT_SHUTDOWN)
455 /* all clear to zap */
456 for (lb = &allBnodes, ub = *lb; ub; lb = &ub->next, ub = *lb) {
458 /* unthread it from the list */
463 free(abnode->name); /* do this first, since bnode fields may be bad after BOP_DELETE */
464 code = BOP_DELETE(abnode); /* don't play games like holding over this one */
469 /* function to tell if there's a timeout coming up */
471 bnode_PendingTimeout(struct bnode *abnode)
473 return (abnode->flags & BNODE_NEEDTIMEOUT);
476 /* function called to set / clear periodic bnode wakeup times */
478 bnode_SetTimeout(struct bnode *abnode, afs_int32 atimeout)
481 abnode->nextTimeout = FT_ApproxTime() + atimeout;
482 abnode->flags |= BNODE_NEEDTIMEOUT;
483 abnode->period = atimeout;
484 IOMGR_Cancel(bproc_pid);
486 abnode->flags &= ~BNODE_NEEDTIMEOUT;
491 /* used by new bnode creation code to format bnode header */
493 bnode_InitBnode(struct bnode *abnode, struct bnode_ops *abnodeops,
496 struct bnode **lb, *nb;
498 /* format the bnode properly */
499 memset(abnode, 0, sizeof(struct bnode));
500 abnode->ops = abnodeops;
501 abnode->name = strdup(aname);
504 abnode->flags = BNODE_ACTIVE;
505 abnode->fileGoal = BSTAT_NORMAL;
506 abnode->goal = BSTAT_SHUTDOWN;
508 /* put the bnode at the end of the list so we write bnode file in same order */
509 for (lb = &allBnodes, nb = *lb; nb; lb = &nb->next, nb = *lb);
516 DeleteProc(struct bnode_proc *abproc)
518 struct bnode_proc **pb, *tb;
519 struct bnode_proc *nb;
521 for (pb = &allProcs, tb = *pb; tb; pb = &tb->next, tb = nb) {
532 /* bnode lwp executes this code repeatedly */
539 struct bnode_proc *tp;
541 int options; /* must not be register */
547 /* first figure out how long to sleep for */
548 temp = 0x7fffffff; /* afs_int32 time; maxint doesn't work in select */
550 for (tb = allBnodes; tb; tb = tb->next) {
551 if (tb->flags & BNODE_NEEDTIMEOUT) {
552 if (tb->nextTimeout < temp) {
554 temp = tb->nextTimeout;
558 /* now temp has the time at which we should wakeup next */
562 temp -= FT_ApproxTime(); /* how many seconds until next event */
568 code = IOMGR_Select(0, 0, 0, 0, &tv);
570 code = 0; /* fake timeout code */
572 /* figure out why we woke up; child exit or timeouts */
573 FT_GetTimeOfDay(&tv, 0); /* must do the real gettimeofday once and a while */
576 /* check all bnodes to see which ones need timeout events */
577 for (tb = allBnodes; tb; tb = nb) {
578 if ((tb->flags & BNODE_NEEDTIMEOUT) && temp > tb->nextTimeout) {
582 if (tb->flags & BNODE_NEEDTIMEOUT) { /* check again, BOP_TIMEOUT could change */
583 tb->nextTimeout = FT_ApproxTime() + tb->period;
586 bnode_Release(tb); /* delete may occur here */
592 /* signalled, probably by incoming signal */
595 bnode_waiting = options | 0x800000;
596 code = waitpid((pid_t) - 1, &status, options);
598 if (code == 0 || code == -1)
599 break; /* all done */
600 /* otherwise code has a process id, which we now search for */
601 for (tp = allProcs; tp; tp = tp->next)
609 /* count restarts in last 30 seconds */
610 if (temp > tb->rsTime + 30) {
611 /* it's been 30 seconds we've been counting */
617 if (WIFSIGNALED(status) == 0) {
618 /* exited, not signalled */
619 tp->lastExit = WEXITSTATUS(status);
622 tb->errorCode = tp->lastExit;
623 tb->lastErrorExit = FT_ApproxTime();
624 RememberProcName(tp);
628 bozo_Log("%s:%s exited with code %d\n", tb->name,
629 tp->coreName, tp->lastExit);
631 bozo_Log("%s exited with code %d\n", tb->name,
634 /* Signal occurred, perhaps spurious due to shutdown request.
635 * If due to a shutdown request, don't overwrite last error
638 tp->lastSignal = WTERMSIG(status);
640 if (tp->lastSignal != SIGQUIT
641 && tp->lastSignal != SIGTERM
642 && tp->lastSignal != SIGKILL) {
643 tb->errorSignal = tp->lastSignal;
644 tb->lastErrorExit = FT_ApproxTime();
645 RememberProcName(tp);
648 bozo_Log("%s:%s exited on signal %d%s\n",
649 tb->name, tp->coreName, tp->lastSignal,
650 WCOREDUMP(status) ? " (core dumped)" :
653 bozo_Log("%s exited on signal %d%s\n", tb->name,
655 WCOREDUMP(status) ? " (core dumped)" :
659 tb->lastAnyExit = FT_ApproxTime();
662 bozo_Log("BNODE: Notifier %s will be called\n",
667 if (tb->goal && tb->rsCount++ > 10) {
668 /* 10 in 30 seconds */
669 if (tb->errorStopCount >= BNODE_ERROR_COUNT_MAX) {
670 tb->errorStopDelay = 0; /* max reached, give up. */
672 tb->errorStopCount++;
673 if (!tb->errorStopDelay) {
674 tb->errorStopDelay = 1;
676 tb->errorStopDelay *= 2;
679 tb->flags |= BNODE_ERRORSTOP;
680 bnode_SetGoal(tb, BSTAT_SHUTDOWN);
682 ("BNODE '%s' repeatedly failed to start, perhaps missing executable.\n",
685 BOP_PROCEXIT(tb, tp);
687 bnode_Release(tb); /* bnode delete can happen here */
690 bnode_stats.weirdPids++;
698 SendNotifierData(int fd, struct bnode_proc *tp)
700 struct bnode *tb = tp->bnode;
701 char buffer[1000], *bufp = buffer, *buf1;
705 * First sent out the bnode_proc struct
707 (void)sprintf(bufp, "BEGIN bnode_proc\n");
708 bufp += strlen(bufp);
709 (void)sprintf(bufp, "comLine: %s\n", tp->comLine);
710 bufp += strlen(bufp);
711 if (!(buf1 = tp->coreName))
713 (void)sprintf(bufp, "coreName: %s\n", buf1);
714 bufp += strlen(bufp);
715 (void)sprintf(bufp, "pid: %ld\n", afs_printable_int32_ld(tp->pid));
716 bufp += strlen(bufp);
717 (void)sprintf(bufp, "lastExit: %ld\n", afs_printable_int32_ld(tp->lastExit));
718 bufp += strlen(bufp);
720 (void)sprintf(bufp, "lastSignal: %ld\n", afs_printable_int32_ld(tp->lastSignal));
721 bufp += strlen(bufp);
723 (void)sprintf(bufp, "flags: %ld\n", afs_printable_int32_ld(tp->flags));
724 bufp += strlen(bufp);
725 (void)sprintf(bufp, "END bnode_proc\n");
726 bufp += strlen(bufp);
727 len = (int)(bufp - buffer);
728 if (write(fd, buffer, len) < 0) {
733 * Now sent out the bnode struct
736 (void)sprintf(bufp, "BEGIN bnode\n");
737 bufp += strlen(bufp);
738 (void)sprintf(bufp, "name: %s\n", tb->name);
739 bufp += strlen(bufp);
740 (void)sprintf(bufp, "rsTime: %ld\n", afs_printable_int32_ld(tb->rsTime));
741 bufp += strlen(bufp);
742 (void)sprintf(bufp, "rsCount: %ld\n", afs_printable_int32_ld(tb->rsCount));
743 bufp += strlen(bufp);
744 (void)sprintf(bufp, "procStartTime: %ld\n", afs_printable_int32_ld(tb->procStartTime));
745 bufp += strlen(bufp);
746 (void)sprintf(bufp, "procStarts: %ld\n", afs_printable_int32_ld(tb->procStarts));
747 bufp += strlen(bufp);
748 (void)sprintf(bufp, "lastAnyExit: %ld\n", afs_printable_int32_ld(tb->lastAnyExit));
749 bufp += strlen(bufp);
750 (void)sprintf(bufp, "lastErrorExit: %ld\n", afs_printable_int32_ld(tb->lastErrorExit));
751 bufp += strlen(bufp);
752 (void)sprintf(bufp, "errorCode: %ld\n", afs_printable_int32_ld(tb->errorCode));
753 bufp += strlen(bufp);
754 (void)sprintf(bufp, "errorSignal: %ld\n", afs_printable_int32_ld(tb->errorSignal));
755 bufp += strlen(bufp);
757 (void) sprintf(bufp, "lastErrorName: %s\n", tb->lastErrorName);
758 bufp += strlen(bufp);
760 (void)sprintf(bufp, "goal: %d\n", tb->goal);
761 bufp += strlen(bufp);
762 (void)sprintf(bufp, "END bnode\n");
763 bufp += strlen(bufp);
764 len = (int)(bufp - buffer);
765 if (write(fd, buffer, len) < 0) {
772 hdl_notifier(struct bnode_proc *tp)
774 #ifndef AFS_NT40_ENV /* NT notifier callout not yet implemented */
778 if (stat(tp->bnode->notifier, &tstat)) {
779 bozo_Log("BNODE: Failed to find notifier '%s'; ignored\n",
780 tp->bnode->notifier);
783 if ((pid = fork()) == 0) {
785 struct bnode *tb = tp->bnode;
787 #if defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_SGI51_ENV)
789 #elif defined(AFS_DARWIN90_ENV)
791 #elif defined(AFS_LINUX20_ENV) || defined(AFS_AIX_ENV)
796 fout = popen(tb->notifier, "w");
798 bozo_Log("BNODE: Failed to find notifier '%s'; ignored\n",
800 perror(tb->notifier);
803 SendNotifierData(fileno(fout), tp);
806 } else if (pid < 0) {
807 bozo_Log("Failed to fork creating process to handle notifier '%s'\n",
808 tp->bnode->notifier);
811 #endif /* AFS_NT40_ENV */
815 /* Called by IOMGR at low priority on IOMGR's stack shortly after a SIGCHLD
816 * occurs. Wakes up bproc do redo things */
818 bnode_SoftInt(void *param)
820 /* int asignal = (int) param; */
822 IOMGR_Cancel(bproc_pid);
826 /* Called at signal interrupt level; queues function to be called
827 * when IOMGR runs again.
830 bnode_Int(int asignal)
832 if (asignal == SIGQUIT || asignal == SIGTERM) {
833 IOMGR_SoftSig(bozo_ShutdownAndExit, (void *)(intptr_t)asignal);
835 IOMGR_SoftSig(bnode_SoftInt, (void *)(intptr_t)asignal);
840 /* intialize the whole system */
846 struct sigaction newaction;
847 static int initDone = 0;
852 memset(&bnode_stats, 0, sizeof(bnode_stats));
853 LWP_InitializeProcessSupport(1, &junk); /* just in case */
855 code = LWP_CreateProcess(bproc, BNODE_LWP_STACKSIZE,
856 /* priority */ 1, (void *) /* parm */ 0,
857 "bnode-manager", &bproc_pid);
860 memset(&newaction, 0, sizeof(newaction));
861 newaction.sa_handler = bnode_Int;
862 code = sigaction(SIGCHLD, &newaction, NULL);
865 code = sigaction(SIGQUIT, &newaction, NULL);
868 code = sigaction(SIGTERM, &newaction, NULL);
874 /* free token list returned by parseLine */
876 bnode_FreeTokens(struct bnode_token *alist)
878 struct bnode_token *nlist;
879 for (; alist; alist = nlist) {
890 if (x == 0 || x == ' ' || x == '\t' || x == '\n')
897 bnode_ParseLine(char *aline, struct bnode_token **alist)
902 struct bnode_token *first, *last;
903 struct bnode_token *ttok;
906 inToken = 0; /* not copying token chars at start */
907 first = (struct bnode_token *)0;
908 last = (struct bnode_token *)0;
911 if (tc == 0 || space(tc)) { /* terminating null gets us in here, too */
913 inToken = 0; /* end of this token */
916 (struct bnode_token *)malloc(sizeof(struct bnode_token));
917 ttok->next = (struct bnode_token *)0;
918 ttok->key = strdup(tbuffer);
928 /* an alpha character */
933 if (tptr - tbuffer >= sizeof(tbuffer))
934 return -1; /* token too long */
938 /* last token flushed 'cause space(0) --> true */
940 last->next = (struct bnode_token *)0;
949 bnode_NewProc(struct bnode *abnode, char *aexecString, char *coreName,
950 struct bnode_proc **aproc)
952 struct bnode_token *tlist, *tt;
954 struct bnode_proc *tp;
956 char *argv[MAXVARGS];
959 code = bnode_ParseLine(aexecString, &tlist); /* try parsing first */
962 tp = (struct bnode_proc *)malloc(sizeof(struct bnode_proc));
963 memset(tp, 0, sizeof(struct bnode_proc));
966 tp->comLine = aexecString;
967 tp->coreName = coreName; /* may be null */
968 abnode->procStartTime = FT_ApproxTime();
969 abnode->procStarts++;
971 /* convert linked list of tokens into argv structure */
972 for (tt = tlist, i = 0; i < (MAXVARGS - 1) && tt; tt = tt->next, i++) {
975 argv[i] = NULL; /* null-terminated */
977 cpid = spawnprocve(argv[0], argv, environ, -1);
978 osi_audit(BOSSpawnProcEvent, 0, AUD_STR, aexecString, AUD_END);
980 if (cpid == (pid_t) - 1) {
981 bozo_Log("Failed to spawn process for bnode '%s'\n", abnode->name);
982 bnode_FreeTokens(tlist);
986 bozo_Log("%s started pid %ld: %s\n", abnode->name, cpid, aexecString);
988 bnode_FreeTokens(tlist);
992 tp->flags = BPROC_STARTED;
993 tp->flags &= ~BPROC_EXITED;
994 BOP_PROCSTARTED(abnode, tp);
1000 bnode_StopProc(struct bnode_proc *aproc, int asignal)
1003 if (!(aproc->flags & BPROC_STARTED) || (aproc->flags & BPROC_EXITED))
1006 osi_audit(BOSStopProcEvent, 0, AUD_STR, (aproc ? aproc->comLine : NULL),
1009 code = kill(aproc->pid, asignal);
1010 bnode_Check(aproc->bnode);
1015 bnode_Deactivate(struct bnode *abnode)
1017 struct bnode **pb, *tb;
1019 if (!(abnode->flags & BNODE_ACTIVE))
1021 for (pb = &allBnodes, tb = *pb; tb; tb = nb) {
1025 tb->flags &= ~BNODE_ACTIVE;