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>
21 #include <sys/types.h>
28 #ifdef BOZO_SAVE_CORES
34 #include <afs/procmgmt.h> /* signal(), kill(), wait(), etc. */
37 #include <afs/audit.h>
38 #include <afs/afsutil.h>
39 #include <afs/fileutil.h>
41 #include "bosprototypes.h"
43 #if defined(AFS_AIX_ENV) || defined(AFS_SUN4_ENV)
44 /* All known versions of AIX lack WCOREDUMP but this works */
45 #define WCOREDUMP(x) ((x) & 0x80)
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 {
61 extern char **environ; /* env structure */
64 int hdl_notifier(struct bnode_proc *tp);
66 /* Remember the name of the process, if any, that failed last */
68 RememberProcName(register struct bnode_proc *ap)
70 register struct bnode *tbnodep;
73 if (tbnodep->lastErrorName) {
74 free(tbnodep->lastErrorName);
75 tbnodep->lastErrorName = NULL;
78 tbnodep->lastErrorName = (char *)malloc(strlen(ap->coreName) + 1);
79 strcpy(tbnodep->lastErrorName, ap->coreName);
83 /* utility for use by BOP_HASCORE functions to determine where a core file might
87 bnode_CoreName(register struct bnode *abnode, char *acoreName, char *abuffer)
89 strcpy(abuffer, AFSDIR_SERVER_CORELOG_FILEPATH);
91 strcat(abuffer, acoreName);
94 strcat(abuffer, abnode->name);
98 /* save core file, if any */
100 SaveCore(register struct bnode *abnode, register struct bnode_proc
105 register afs_int32 code;
106 char *corefile = NULL;
107 #ifdef BOZO_SAVE_CORES
108 struct timeval Start;
109 struct tm *TimeFields;
113 /* Linux always appends the PID to core dumps from threaded processes, so
114 * we have to scan the directory to find core files under another name. */
115 code = stat(AFSDIR_SERVER_CORELOG_FILEPATH, &tstat);
122 logdir = opendir(AFSDIR_LOGS_DIR);
125 while ((file = readdir(logdir)) != NULL) {
126 if (strncmp(file->d_name, "core.", 5) != 0)
128 pid = atol(file->d_name + 5);
129 if (pid == aproc->pid) {
130 length = strlen(AFSDIR_LOGS_DIR) + strlen(file->d_name) + 2;
131 corefile = malloc(length);
132 if (corefile == NULL) {
136 snprintf(corefile, length, "%s/%s", AFSDIR_LOGS_DIR,
147 bnode_CoreName(abnode, aproc->coreName, tbuffer);
148 #ifdef BOZO_SAVE_CORES
149 FT_GetTimeOfDay(&Start, 0);
150 TimeFields = localtime(&Start.tv_sec);
151 sprintf(FileName, "%s.%d%02d%02d%02d%02d%02d", tbuffer,
152 TimeFields->tm_year + 1900, TimeFields->tm_mon + 1, TimeFields->tm_mday,
153 TimeFields->tm_hour, TimeFields->tm_min, TimeFields->tm_sec);
154 strcpy(tbuffer, FileName);
156 if (corefile == NULL)
157 code = renamefile(AFSDIR_SERVER_CORELOG_FILEPATH, tbuffer);
159 code = renamefile(corefile, tbuffer);
165 bnode_GetString(register struct bnode *abnode, register char *abuffer,
166 register afs_int32 alen)
168 return BOP_GETSTRING(abnode, abuffer, alen);
172 bnode_GetParm(register struct bnode *abnode, register afs_int32 aindex,
173 register char *abuffer, afs_int32 alen)
175 return BOP_GETPARM(abnode, aindex, abuffer, alen);
179 bnode_GetStat(register struct bnode *abnode, register afs_int32 * astatus)
181 return BOP_GETSTAT(abnode, astatus);
185 bnode_RestartP(register struct bnode *abnode)
187 return BOP_RESTARTP(abnode);
191 bnode_Check(register struct bnode *abnode)
193 if (abnode->flags & BNODE_WAIT) {
194 abnode->flags &= ~BNODE_WAIT;
195 LWP_NoYieldSignal(abnode);
200 /* tell if an instance has a core file */
202 bnode_HasCore(register struct bnode *abnode)
204 return BOP_HASCORE(abnode);
207 /* wait for all bnodes to stabilize */
211 register struct bnode *tb;
212 register afs_int32 code;
216 for (tb = allBnodes; tb; tb = tb->next) {
218 code = BOP_GETSTAT(tb, &stat);
223 if (stat != tb->goal) {
224 tb->flags |= BNODE_WAIT;
234 /* wait until bnode status is correct */
236 bnode_WaitStatus(register struct bnode *abnode, int astatus)
238 register afs_int32 code;
244 code = BOP_GETSTAT(abnode, &stat);
248 /* otherwise, check if we're done */
249 if (stat == astatus) {
250 bnode_Release(abnode);
253 if (astatus != abnode->goal) {
254 bnode_Release(abnode);
255 return -1; /* no longer our goal, don't keep waiting */
257 /* otherwise, block */
258 abnode->flags |= BNODE_WAIT;
259 LWP_WaitProcess(abnode);
264 bnode_SetStat(register struct bnode *abnode, register int agoal)
266 abnode->goal = agoal;
268 BOP_SETSTAT(abnode, agoal);
269 abnode->flags &= ~BNODE_ERRORSTOP;
274 bnode_SetGoal(register struct bnode *abnode, register int agoal)
276 abnode->goal = agoal;
282 bnode_SetFileGoal(register struct bnode *abnode, register 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 register struct bnode *tb, *nb;
296 register afs_int32 code;
298 for (tb = allBnodes; tb; tb = nb) {
300 code = (*aproc) (tb, arock);
308 bnode_FindInstance(register char *aname)
310 register struct bnode *tb;
312 for (tb = allBnodes; tb; tb = tb->next) {
313 if (!strcmp(tb->name, aname))
319 static struct bnode_type *
320 FindType(register char *aname)
322 register 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 register 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)
407 register struct bnode *tb;
409 tb = bnode_FindInstance(ainstance);
413 return bnode_Delete(tb);
417 bnode_Hold(register struct bnode *abnode)
424 bnode_Release(register 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(register struct bnode *abnode)
437 register afs_int32 code;
438 register 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(register struct bnode *abnode)
473 return (abnode->flags & BNODE_NEEDTIMEOUT);
476 /* function called to set / clear periodic bnode wakeup times */
478 bnode_SetTimeout(register 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(register 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 = (char *)malloc(strlen(aname) + 1);
504 strcpy(abnode->name, aname);
505 abnode->flags = BNODE_ACTIVE;
506 abnode->fileGoal = BSTAT_NORMAL;
507 abnode->goal = BSTAT_SHUTDOWN;
509 /* put the bnode at the end of the list so we write bnode file in same order */
510 for (lb = &allBnodes, nb = *lb; nb; lb = &nb->next, nb = *lb);
517 DeleteProc(register struct bnode_proc *abproc)
519 register struct bnode_proc **pb, *tb;
520 struct bnode_proc *nb;
522 for (pb = &allProcs, tb = *pb; tb; pb = &tb->next, tb = nb) {
533 /* bnode lwp executes this code repeatedly */
537 register afs_int32 code;
538 register struct bnode *tb;
539 register afs_int32 temp;
540 register struct bnode_proc *tp;
542 int options; /* must not be register */
548 /* first figure out how long to sleep for */
549 temp = 0x7fffffff; /* afs_int32 time; maxint doesn't work in select */
551 for (tb = allBnodes; tb; tb = tb->next) {
552 if (tb->flags & BNODE_NEEDTIMEOUT) {
553 if (tb->nextTimeout < temp) {
555 temp = tb->nextTimeout;
559 /* now temp has the time at which we should wakeup next */
563 temp -= FT_ApproxTime(); /* how many seconds until next event */
569 code = IOMGR_Select(0, 0, 0, 0, &tv);
571 code = 0; /* fake timeout code */
573 /* figure out why we woke up; child exit or timeouts */
574 FT_GetTimeOfDay(&tv, 0); /* must do the real gettimeofday once and a while */
577 /* check all bnodes to see which ones need timeout events */
578 for (tb = allBnodes; tb; tb = nb) {
579 if ((tb->flags & BNODE_NEEDTIMEOUT) && temp > tb->nextTimeout) {
583 if (tb->flags & BNODE_NEEDTIMEOUT) { /* check again, BOP_TIMEOUT could change */
584 tb->nextTimeout = FT_ApproxTime() + tb->period;
587 bnode_Release(tb); /* delete may occur here */
593 /* signalled, probably by incoming signal */
596 bnode_waiting = options | 0x800000;
597 code = waitpid((pid_t) - 1, &status, options);
599 if (code == 0 || code == -1)
600 break; /* all done */
601 /* otherwise code has a process id, which we now search for */
602 for (tp = allProcs; tp; tp = tp->next)
610 /* count restarts in last 10 seconds */
611 if (temp > tb->rsTime + 30) {
612 /* it's been 10 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",
666 BOP_PROCEXIT(tb, tp);
669 if (tb->rsCount++ > 10) {
670 /* 10 in 10 seconds */
671 tb->flags |= BNODE_ERRORSTOP;
672 bnode_SetGoal(tb, BSTAT_SHUTDOWN);
674 ("BNODE '%s' repeatedly failed to start, perhaps missing executable.\n",
677 bnode_Release(tb); /* bnode delete can happen here */
680 bnode_stats.weirdPids++;
688 SendNotifierData(register int fd, register struct bnode_proc *tp)
690 register struct bnode *tb = tp->bnode;
691 char buffer[1000], *bufp = buffer, *buf1;
695 * First sent out the bnode_proc struct
697 (void)sprintf(bufp, "BEGIN bnode_proc\n");
698 bufp += strlen(bufp);
699 (void)sprintf(bufp, "comLine: %s\n", tp->comLine);
700 bufp += strlen(bufp);
701 if (!(buf1 = tp->coreName))
703 (void)sprintf(bufp, "coreName: %s\n", buf1);
704 bufp += strlen(bufp);
705 (void)sprintf(bufp, "pid: %ld\n", afs_cast_int32(tp->pid));
706 bufp += strlen(bufp);
707 (void)sprintf(bufp, "lastExit: %ld\n", afs_cast_int32(tp->lastExit));
708 bufp += strlen(bufp);
710 (void)sprintf(bufp, "lastSignal: %ld\n", afs_cast_int32(tp->lastSignal));
711 bufp += strlen(bufp);
713 (void)sprintf(bufp, "flags: %ld\n", afs_cast_int32(tp->flags));
714 bufp += strlen(bufp);
715 (void)sprintf(bufp, "END bnode_proc\n");
716 bufp += strlen(bufp);
717 len = (int)(bufp - buffer);
718 if (write(fd, buffer, len) < 0) {
723 * Now sent out the bnode struct
726 (void)sprintf(bufp, "BEGIN bnode\n");
727 bufp += strlen(bufp);
728 (void)sprintf(bufp, "name: %s\n", tb->name);
729 bufp += strlen(bufp);
730 (void)sprintf(bufp, "rsTime: %ld\n", afs_cast_int32(tb->rsTime));
731 bufp += strlen(bufp);
732 (void)sprintf(bufp, "rsCount: %ld\n", afs_cast_int32(tb->rsCount));
733 bufp += strlen(bufp);
734 (void)sprintf(bufp, "procStartTime: %ld\n", afs_cast_int32(tb->procStartTime));
735 bufp += strlen(bufp);
736 (void)sprintf(bufp, "procStarts: %ld\n", afs_cast_int32(tb->procStarts));
737 bufp += strlen(bufp);
738 (void)sprintf(bufp, "lastAnyExit: %ld\n", afs_cast_int32(tb->lastAnyExit));
739 bufp += strlen(bufp);
740 (void)sprintf(bufp, "lastErrorExit: %ld\n", afs_cast_int32(tb->lastErrorExit));
741 bufp += strlen(bufp);
742 (void)sprintf(bufp, "errorCode: %ld\n", afs_cast_int32(tb->errorCode));
743 bufp += strlen(bufp);
744 (void)sprintf(bufp, "errorSignal: %ld\n", afs_cast_int32(tb->errorSignal));
745 bufp += strlen(bufp);
747 (void) sprintf(bufp, "lastErrorName: %s\n", tb->lastErrorName);
748 bufp += strlen(bufp);
750 (void)sprintf(bufp, "goal: %d\n", tb->goal);
751 bufp += strlen(bufp);
752 (void)sprintf(bufp, "END bnode\n");
753 bufp += strlen(bufp);
754 len = (int)(bufp - buffer);
755 if (write(fd, buffer, len) < 0) {
762 hdl_notifier(struct bnode_proc *tp)
764 #ifndef AFS_NT40_ENV /* NT notifier callout not yet implemented */
768 if (stat(tp->bnode->notifier, &tstat)) {
769 bozo_Log("BNODE: Failed to find notifier '%s'; ignored\n",
770 tp->bnode->notifier);
773 if ((pid = fork()) == 0) {
775 struct bnode *tb = tp->bnode;
778 #if defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_SGI51_ENV)
780 #elif defined(AFS_DARWIN90_ENV)
782 #elif defined(AFS_LINUX20_ENV) || defined(AFS_AIX_ENV)
787 fout = popen(tb->notifier, "w");
789 bozo_Log("BNODE: Failed to find notifier '%s'; ignored\n",
791 perror(tb->notifier);
794 code = SendNotifierData(fileno(fout), tp);
797 } else if (pid < 0) {
798 bozo_Log("Failed to fork creating process to handle notifier '%s'\n",
799 tp->bnode->notifier);
802 #endif /* AFS_NT40_ENV */
806 /* Called by IOMGR at low priority on IOMGR's stack shortly after a SIGCHLD
807 * occurs. Wakes up bproc do redo things */
809 bnode_SoftInt(void *param)
811 /* int asignal = (int) param; */
813 IOMGR_Cancel(bproc_pid);
817 /* Called at signal interrupt level; queues function to be called
818 * when IOMGR runs again.
821 bnode_Int(int asignal)
823 if (asignal == SIGQUIT) {
824 IOMGR_SoftSig(bozo_ShutdownAndExit, (void *) asignal);
826 IOMGR_SoftSig(bnode_SoftInt, (void *) asignal);
831 /* intialize the whole system */
836 register afs_int32 code;
837 struct sigaction newaction;
838 static int initDone = 0;
843 memset(&bnode_stats, 0, sizeof(bnode_stats));
844 LWP_InitializeProcessSupport(1, &junk); /* just in case */
846 code = LWP_CreateProcess(bproc, BNODE_LWP_STACKSIZE,
847 /* priority */ 1, (void *) /* parm */ 0,
848 "bnode-manager", &bproc_pid);
851 memset((char *)&newaction, 0, sizeof(newaction));
852 newaction.sa_handler = bnode_Int;
853 code = sigaction(SIGCHLD, &newaction, NULL);
856 code = sigaction(SIGQUIT, &newaction, NULL);
862 /* free token list returned by parseLine */
864 bnode_FreeTokens(register struct bnode_token *alist)
866 register struct bnode_token *nlist;
867 for (; alist; alist = nlist) {
878 if (x == 0 || x == ' ' || x == '\t' || x == '\n')
885 bnode_ParseLine(char *aline, struct bnode_token **alist)
888 register char *tptr = NULL;
890 struct bnode_token *first, *last;
891 register struct bnode_token *ttok;
894 inToken = 0; /* not copying token chars at start */
895 first = (struct bnode_token *)0;
896 last = (struct bnode_token *)0;
899 if (tc == 0 || space(tc)) { /* terminating null gets us in here, too */
901 inToken = 0; /* end of this token */
904 (struct bnode_token *)malloc(sizeof(struct bnode_token));
905 ttok->next = (struct bnode_token *)0;
906 ttok->key = (char *)malloc(strlen(tbuffer) + 1);
907 strcpy(ttok->key, tbuffer);
917 /* an alpha character */
922 if (tptr - tbuffer >= sizeof(tbuffer))
923 return -1; /* token too long */
927 /* last token flushed 'cause space(0) --> true */
929 last->next = (struct bnode_token *)0;
939 bnode_NewProc(struct bnode *abnode, char *aexecString, char *coreName,
940 struct bnode_proc **aproc)
942 struct bnode_token *tlist, *tt;
944 struct bnode_proc *tp;
946 char *argv[MAXVARGS];
949 code = bnode_ParseLine(aexecString, &tlist); /* try parsing first */
952 tp = (struct bnode_proc *)malloc(sizeof(struct bnode_proc));
953 memset(tp, 0, sizeof(struct bnode_proc));
956 tp->comLine = aexecString;
957 tp->coreName = coreName; /* may be null */
958 abnode->procStartTime = FT_ApproxTime();
959 abnode->procStarts++;
961 /* convert linked list of tokens into argv structure */
962 for (tt = tlist, i = 0; i < (MAXVARGS - 1) && tt; tt = tt->next, i++) {
965 argv[i] = NULL; /* null-terminated */
967 cpid = spawnprocve(argv[0], argv, environ, -1);
968 osi_audit(BOSSpawnProcEvent, 0, AUD_STR, aexecString, AUD_END);
970 if (cpid == (pid_t) - 1) {
971 bozo_Log("Failed to spawn process for bnode '%s'\n", abnode->name);
972 bnode_FreeTokens(tlist);
977 bnode_FreeTokens(tlist);
981 tp->flags = BPROC_STARTED;
982 tp->flags &= ~BPROC_EXITED;
988 bnode_StopProc(register struct bnode_proc *aproc, int asignal)
991 if (!(aproc->flags & BPROC_STARTED) || (aproc->flags & BPROC_EXITED))
994 osi_audit(BOSStopProcEvent, 0, AUD_STR, (aproc ? aproc->comLine : NULL),
997 code = kill(aproc->pid, asignal);
998 bnode_Check(aproc->bnode);
1003 bnode_Deactivate(register struct bnode *abnode)
1005 register struct bnode **pb, *tb;
1007 if (!(abnode->flags & BNODE_ACTIVE))
1009 for (pb = &allBnodes, tb = *pb; tb; tb = nb) {
1013 tb->flags &= ~BNODE_ACTIVE;