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)
33 int bnode_waiting = 0;
34 static PROCESS bproc_pid; /* pid of waker-upper */
35 static struct bnode *allBnodes = 0; /* list of all bnodes */
36 static struct bnode_proc *allProcs = 0; /* list of all processes for which we're waiting */
37 static struct bnode_type *allTypes = 0; /* list of registered type handlers */
39 static struct bnode_stats {
43 extern const char *DoCore;
45 extern char **environ; /* env structure */
48 int hdl_notifier(struct bnode_proc *tp);
50 /* Remember the name of the process, if any, that failed last */
52 RememberProcName(struct bnode_proc *ap)
54 struct bnode *tbnodep;
57 if (tbnodep->lastErrorName) {
58 free(tbnodep->lastErrorName);
59 tbnodep->lastErrorName = NULL;
62 tbnodep->lastErrorName = (char *)malloc(strlen(ap->coreName) + 1);
63 strcpy(tbnodep->lastErrorName, 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);
115 const char *coredir = AFSDIR_LOGS_DIR;
120 logdir = opendir(coredir);
123 while ((file = readdir(logdir)) != NULL) {
124 if (strncmp(file->d_name, "core.", 5) != 0)
126 pid = atol(file->d_name + 5);
127 if (pid == aproc->pid) {
128 length = strlen(coredir) + strlen(file->d_name) + 2;
129 corefile = malloc(length);
130 if (corefile == NULL) {
134 snprintf(corefile, length, "%s/%s", coredir, file->d_name);
141 corefile = strdup(tbuffer);
146 bnode_CoreName(abnode, aproc->coreName, tbuffer);
147 #ifdef BOZO_SAVE_CORES
148 FT_GetTimeOfDay(&Start, 0);
149 TimeFields = localtime(&Start.tv_sec);
150 sprintf(FileName, "%s.%d%02d%02d%02d%02d%02d", tbuffer,
151 TimeFields->tm_year + 1900, TimeFields->tm_mon + 1, TimeFields->tm_mday,
152 TimeFields->tm_hour, TimeFields->tm_min, TimeFields->tm_sec);
153 strcpy(tbuffer, FileName);
155 code = renamefile(corefile, tbuffer);
160 bnode_GetString(struct bnode *abnode, char *abuffer,
163 return BOP_GETSTRING(abnode, abuffer, alen);
167 bnode_GetParm(struct bnode *abnode, afs_int32 aindex,
168 char *abuffer, afs_int32 alen)
170 return BOP_GETPARM(abnode, aindex, abuffer, alen);
174 bnode_GetStat(struct bnode *abnode, afs_int32 * astatus)
176 return BOP_GETSTAT(abnode, astatus);
180 bnode_RestartP(struct bnode *abnode)
182 return BOP_RESTARTP(abnode);
186 bnode_Check(struct bnode *abnode)
188 if (abnode->flags & BNODE_WAIT) {
189 abnode->flags &= ~BNODE_WAIT;
190 LWP_NoYieldSignal(abnode);
195 /* tell if an instance has a core file */
197 bnode_HasCore(struct bnode *abnode)
199 return BOP_HASCORE(abnode);
202 /* wait for all bnodes to stabilize */
211 for (tb = allBnodes; tb; tb = tb->next) {
213 code = BOP_GETSTAT(tb, &stat);
218 if (stat != tb->goal) {
219 tb->flags |= BNODE_WAIT;
229 /* wait until bnode status is correct */
231 bnode_WaitStatus(struct bnode *abnode, int astatus)
239 code = BOP_GETSTAT(abnode, &stat);
243 /* otherwise, check if we're done */
244 if (stat == astatus) {
245 bnode_Release(abnode);
248 if (astatus != abnode->goal) {
249 bnode_Release(abnode);
250 return -1; /* no longer our goal, don't keep waiting */
252 /* otherwise, block */
253 abnode->flags |= BNODE_WAIT;
254 LWP_WaitProcess(abnode);
259 bnode_SetStat(struct bnode *abnode, int agoal)
261 abnode->goal = agoal;
263 BOP_SETSTAT(abnode, agoal);
264 abnode->flags &= ~BNODE_ERRORSTOP;
269 bnode_SetGoal(struct bnode *abnode, int agoal)
271 abnode->goal = agoal;
277 bnode_SetFileGoal(struct bnode *abnode, int agoal)
279 if (abnode->fileGoal == agoal)
280 return 0; /* already done */
281 abnode->fileGoal = agoal;
286 /* apply a function to all bnodes in the system */
288 bnode_ApplyInstance(int (*aproc) (struct bnode *tb, void *), void *arock)
290 struct bnode *tb, *nb;
293 for (tb = allBnodes; tb; tb = nb) {
295 code = (*aproc) (tb, arock);
303 bnode_FindInstance(char *aname)
307 for (tb = allBnodes; tb; tb = tb->next) {
308 if (!strcmp(tb->name, aname))
314 static struct bnode_type *
315 FindType(char *aname)
317 struct bnode_type *tt;
319 for (tt = allTypes; tt; tt = tt->next) {
320 if (!strcmp(tt->name, aname))
323 return (struct bnode_type *)0;
327 bnode_Register(char *atype, struct bnode_ops *aprocs, int anparms)
329 struct bnode_type *tt;
331 for (tt = allTypes; tt; tt = tt->next) {
332 if (!strcmp(tt->name, atype))
336 tt = (struct bnode_type *)malloc(sizeof(struct bnode_type));
337 memset(tt, 0, sizeof(struct bnode_type));
347 bnode_Create(char *atype, char *ainstance, struct bnode ** abp, char *ap1,
348 char *ap2, char *ap3, char *ap4, char *ap5, char *notifier,
349 int fileGoal, int rewritefile)
351 struct bnode_type *type;
353 char *notifierpath = NULL;
356 if (bnode_FindInstance(ainstance))
358 type = FindType(atype);
362 if (notifier && strcmp(notifier, NONOTIFIER)) {
363 /* construct local path from canonical (wire-format) path */
364 if (ConstructLocalBinPath(notifier, ¬ifierpath)) {
365 bozo_Log("BNODE-Create: Notifier program path invalid '%s'\n",
370 if (stat(notifierpath, &tstat)) {
371 bozo_Log("BNODE-Create: Notifier program '%s' not found\n",
377 tb = (*type->ops->create) (ainstance, ap1, ap2, ap3, ap4, ap5);
382 tb->notifier = notifierpath;
386 /* The fs_create above calls bnode_InitBnode() which always sets the
387 ** fileGoal to BSTAT_NORMAL .... overwrite it with whatever is passed into
388 ** this function as a parameter... */
389 tb->fileGoal = fileGoal;
391 bnode_SetStat(tb, tb->goal); /* nudge it once */
393 if (rewritefile != 0)
400 bnode_DeleteName(char *ainstance)
404 tb = bnode_FindInstance(ainstance);
408 return bnode_Delete(tb);
412 bnode_Hold(struct bnode *abnode)
419 bnode_Release(struct bnode *abnode)
422 if (abnode->refCount == 0 && abnode->flags & BNODE_DELETE) {
423 abnode->flags &= ~BNODE_DELETE; /* we're going for it */
424 bnode_Delete(abnode);
430 bnode_Delete(struct bnode *abnode)
433 struct bnode **lb, *ub;
436 if (abnode->refCount != 0) {
437 abnode->flags |= BNODE_DELETE;
441 /* make sure the bnode is idle before zapping */
443 code = BOP_GETSTAT(abnode, &temp);
444 bnode_Release(abnode);
447 if (temp != BSTAT_SHUTDOWN)
450 /* all clear to zap */
451 for (lb = &allBnodes, ub = *lb; ub; lb = &ub->next, ub = *lb) {
453 /* unthread it from the list */
458 free(abnode->name); /* do this first, since bnode fields may be bad after BOP_DELETE */
459 code = BOP_DELETE(abnode); /* don't play games like holding over this one */
464 /* function to tell if there's a timeout coming up */
466 bnode_PendingTimeout(struct bnode *abnode)
468 return (abnode->flags & BNODE_NEEDTIMEOUT);
471 /* function called to set / clear periodic bnode wakeup times */
473 bnode_SetTimeout(struct bnode *abnode, afs_int32 atimeout)
476 abnode->nextTimeout = FT_ApproxTime() + atimeout;
477 abnode->flags |= BNODE_NEEDTIMEOUT;
478 abnode->period = atimeout;
479 IOMGR_Cancel(bproc_pid);
481 abnode->flags &= ~BNODE_NEEDTIMEOUT;
486 /* used by new bnode creation code to format bnode header */
488 bnode_InitBnode(struct bnode *abnode, struct bnode_ops *abnodeops,
491 struct bnode **lb, *nb;
493 /* format the bnode properly */
494 memset(abnode, 0, sizeof(struct bnode));
495 abnode->ops = abnodeops;
496 abnode->name = (char *)malloc(strlen(aname) + 1);
499 strcpy(abnode->name, aname);
500 abnode->flags = BNODE_ACTIVE;
501 abnode->fileGoal = BSTAT_NORMAL;
502 abnode->goal = BSTAT_SHUTDOWN;
504 /* put the bnode at the end of the list so we write bnode file in same order */
505 for (lb = &allBnodes, nb = *lb; nb; lb = &nb->next, nb = *lb);
512 DeleteProc(struct bnode_proc *abproc)
514 struct bnode_proc **pb, *tb;
515 struct bnode_proc *nb;
517 for (pb = &allProcs, tb = *pb; tb; pb = &tb->next, tb = nb) {
528 /* bnode lwp executes this code repeatedly */
535 struct bnode_proc *tp;
537 int options; /* must not be register */
543 /* first figure out how long to sleep for */
544 temp = 0x7fffffff; /* afs_int32 time; maxint doesn't work in select */
546 for (tb = allBnodes; tb; tb = tb->next) {
547 if (tb->flags & BNODE_NEEDTIMEOUT) {
548 if (tb->nextTimeout < temp) {
550 temp = tb->nextTimeout;
554 /* now temp has the time at which we should wakeup next */
558 temp -= FT_ApproxTime(); /* how many seconds until next event */
564 code = IOMGR_Select(0, 0, 0, 0, &tv);
566 code = 0; /* fake timeout code */
568 /* figure out why we woke up; child exit or timeouts */
569 FT_GetTimeOfDay(&tv, 0); /* must do the real gettimeofday once and a while */
572 /* check all bnodes to see which ones need timeout events */
573 for (tb = allBnodes; tb; tb = nb) {
574 if ((tb->flags & BNODE_NEEDTIMEOUT) && temp > tb->nextTimeout) {
578 if (tb->flags & BNODE_NEEDTIMEOUT) { /* check again, BOP_TIMEOUT could change */
579 tb->nextTimeout = FT_ApproxTime() + tb->period;
582 bnode_Release(tb); /* delete may occur here */
588 /* signalled, probably by incoming signal */
591 bnode_waiting = options | 0x800000;
592 code = waitpid((pid_t) - 1, &status, options);
594 if (code == 0 || code == -1)
595 break; /* all done */
596 /* otherwise code has a process id, which we now search for */
597 for (tp = allProcs; tp; tp = tp->next)
605 /* count restarts in last 10 seconds */
606 if (temp > tb->rsTime + 30) {
607 /* it's been 10 seconds we've been counting */
612 if (WIFSIGNALED(status) == 0) {
613 /* exited, not signalled */
614 tp->lastExit = WEXITSTATUS(status);
617 tb->errorCode = tp->lastExit;
618 tb->lastErrorExit = FT_ApproxTime();
619 RememberProcName(tp);
623 bozo_Log("%s:%s exited with code %d\n", tb->name,
624 tp->coreName, tp->lastExit);
626 bozo_Log("%s exited with code %d\n", tb->name,
629 /* Signal occurred, perhaps spurious due to shutdown request.
630 * If due to a shutdown request, don't overwrite last error
633 tp->lastSignal = WTERMSIG(status);
635 if (tp->lastSignal != SIGQUIT
636 && tp->lastSignal != SIGTERM
637 && tp->lastSignal != SIGKILL) {
638 tb->errorSignal = tp->lastSignal;
639 tb->lastErrorExit = FT_ApproxTime();
640 RememberProcName(tp);
643 bozo_Log("%s:%s exited on signal %d%s\n",
644 tb->name, tp->coreName, tp->lastSignal,
645 WCOREDUMP(status) ? " (core dumped)" :
648 bozo_Log("%s exited on signal %d%s\n", tb->name,
650 WCOREDUMP(status) ? " (core dumped)" :
654 tb->lastAnyExit = FT_ApproxTime();
657 bozo_Log("BNODE: Notifier %s will be called\n",
661 BOP_PROCEXIT(tb, tp);
664 if (tb->rsCount++ > 10) {
665 /* 10 in 10 seconds */
666 tb->flags |= BNODE_ERRORSTOP;
667 bnode_SetGoal(tb, BSTAT_SHUTDOWN);
669 ("BNODE '%s' repeatedly failed to start, perhaps missing executable.\n",
672 bnode_Release(tb); /* bnode delete can happen here */
675 bnode_stats.weirdPids++;
683 SendNotifierData(int fd, struct bnode_proc *tp)
685 struct bnode *tb = tp->bnode;
686 char buffer[1000], *bufp = buffer, *buf1;
690 * First sent out the bnode_proc struct
692 (void)sprintf(bufp, "BEGIN bnode_proc\n");
693 bufp += strlen(bufp);
694 (void)sprintf(bufp, "comLine: %s\n", tp->comLine);
695 bufp += strlen(bufp);
696 if (!(buf1 = tp->coreName))
698 (void)sprintf(bufp, "coreName: %s\n", buf1);
699 bufp += strlen(bufp);
700 (void)sprintf(bufp, "pid: %ld\n", afs_printable_int32_ld(tp->pid));
701 bufp += strlen(bufp);
702 (void)sprintf(bufp, "lastExit: %ld\n", afs_printable_int32_ld(tp->lastExit));
703 bufp += strlen(bufp);
705 (void)sprintf(bufp, "lastSignal: %ld\n", afs_printable_int32_ld(tp->lastSignal));
706 bufp += strlen(bufp);
708 (void)sprintf(bufp, "flags: %ld\n", afs_printable_int32_ld(tp->flags));
709 bufp += strlen(bufp);
710 (void)sprintf(bufp, "END bnode_proc\n");
711 bufp += strlen(bufp);
712 len = (int)(bufp - buffer);
713 if (write(fd, buffer, len) < 0) {
718 * Now sent out the bnode struct
721 (void)sprintf(bufp, "BEGIN bnode\n");
722 bufp += strlen(bufp);
723 (void)sprintf(bufp, "name: %s\n", tb->name);
724 bufp += strlen(bufp);
725 (void)sprintf(bufp, "rsTime: %ld\n", afs_printable_int32_ld(tb->rsTime));
726 bufp += strlen(bufp);
727 (void)sprintf(bufp, "rsCount: %ld\n", afs_printable_int32_ld(tb->rsCount));
728 bufp += strlen(bufp);
729 (void)sprintf(bufp, "procStartTime: %ld\n", afs_printable_int32_ld(tb->procStartTime));
730 bufp += strlen(bufp);
731 (void)sprintf(bufp, "procStarts: %ld\n", afs_printable_int32_ld(tb->procStarts));
732 bufp += strlen(bufp);
733 (void)sprintf(bufp, "lastAnyExit: %ld\n", afs_printable_int32_ld(tb->lastAnyExit));
734 bufp += strlen(bufp);
735 (void)sprintf(bufp, "lastErrorExit: %ld\n", afs_printable_int32_ld(tb->lastErrorExit));
736 bufp += strlen(bufp);
737 (void)sprintf(bufp, "errorCode: %ld\n", afs_printable_int32_ld(tb->errorCode));
738 bufp += strlen(bufp);
739 (void)sprintf(bufp, "errorSignal: %ld\n", afs_printable_int32_ld(tb->errorSignal));
740 bufp += strlen(bufp);
742 (void) sprintf(bufp, "lastErrorName: %s\n", tb->lastErrorName);
743 bufp += strlen(bufp);
745 (void)sprintf(bufp, "goal: %d\n", tb->goal);
746 bufp += strlen(bufp);
747 (void)sprintf(bufp, "END bnode\n");
748 bufp += strlen(bufp);
749 len = (int)(bufp - buffer);
750 if (write(fd, buffer, len) < 0) {
757 hdl_notifier(struct bnode_proc *tp)
759 #ifndef AFS_NT40_ENV /* NT notifier callout not yet implemented */
763 if (stat(tp->bnode->notifier, &tstat)) {
764 bozo_Log("BNODE: Failed to find notifier '%s'; ignored\n",
765 tp->bnode->notifier);
768 if ((pid = fork()) == 0) {
770 struct bnode *tb = tp->bnode;
773 #if defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_SGI51_ENV)
775 #elif defined(AFS_DARWIN90_ENV)
777 #elif defined(AFS_LINUX20_ENV) || defined(AFS_AIX_ENV)
782 fout = popen(tb->notifier, "w");
784 bozo_Log("BNODE: Failed to find notifier '%s'; ignored\n",
786 perror(tb->notifier);
789 code = SendNotifierData(fileno(fout), tp);
792 } else if (pid < 0) {
793 bozo_Log("Failed to fork creating process to handle notifier '%s'\n",
794 tp->bnode->notifier);
797 #endif /* AFS_NT40_ENV */
801 /* Called by IOMGR at low priority on IOMGR's stack shortly after a SIGCHLD
802 * occurs. Wakes up bproc do redo things */
804 bnode_SoftInt(void *param)
806 /* int asignal = (int) param; */
808 IOMGR_Cancel(bproc_pid);
812 /* Called at signal interrupt level; queues function to be called
813 * when IOMGR runs again.
816 bnode_Int(int asignal)
818 if (asignal == SIGQUIT || asignal == SIGTERM) {
819 IOMGR_SoftSig(bozo_ShutdownAndExit, (void *)(intptr_t)asignal);
821 IOMGR_SoftSig(bnode_SoftInt, (void *)(intptr_t)asignal);
826 /* intialize the whole system */
832 struct sigaction newaction;
833 static int initDone = 0;
838 memset(&bnode_stats, 0, sizeof(bnode_stats));
839 LWP_InitializeProcessSupport(1, &junk); /* just in case */
841 code = LWP_CreateProcess(bproc, BNODE_LWP_STACKSIZE,
842 /* priority */ 1, (void *) /* parm */ 0,
843 "bnode-manager", &bproc_pid);
846 memset(&newaction, 0, sizeof(newaction));
847 newaction.sa_handler = bnode_Int;
848 code = sigaction(SIGCHLD, &newaction, NULL);
851 code = sigaction(SIGQUIT, &newaction, NULL);
854 code = sigaction(SIGTERM, &newaction, NULL);
860 /* free token list returned by parseLine */
862 bnode_FreeTokens(struct bnode_token *alist)
864 struct bnode_token *nlist;
865 for (; alist; alist = nlist) {
876 if (x == 0 || x == ' ' || x == '\t' || x == '\n')
883 bnode_ParseLine(char *aline, struct bnode_token **alist)
888 struct bnode_token *first, *last;
889 struct bnode_token *ttok;
892 inToken = 0; /* not copying token chars at start */
893 first = (struct bnode_token *)0;
894 last = (struct bnode_token *)0;
897 if (tc == 0 || space(tc)) { /* terminating null gets us in here, too */
899 inToken = 0; /* end of this token */
902 (struct bnode_token *)malloc(sizeof(struct bnode_token));
903 ttok->next = (struct bnode_token *)0;
904 ttok->key = (char *)malloc(strlen(tbuffer) + 1);
905 strcpy(ttok->key, tbuffer);
915 /* an alpha character */
920 if (tptr - tbuffer >= sizeof(tbuffer))
921 return -1; /* token too long */
925 /* last token flushed 'cause space(0) --> true */
927 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(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(struct bnode *abnode)
1002 struct bnode **pb, *tb;
1004 if (!(abnode->flags & BNODE_ACTIVE))
1006 for (pb = &allBnodes, tb = *pb; tb; tb = nb) {
1010 tb->flags &= ~BNODE_ACTIVE;