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>
23 #include <opr/queue.h>
26 #include "bnode_internal.h"
27 #include "bosprototypes.h"
30 #define WCOREDUMP(x) ((x) & 0200)
33 #define BNODE_LWP_STACKSIZE (16 * 1024)
34 #define BNODE_ERROR_COUNT_MAX 16 /* maximum number of retries */
36 int bnode_waiting = 0;
37 static PROCESS bproc_pid; /* pid of waker-upper */
38 static struct bnode *allBnodes = 0; /* list of all bnodes */
39 static struct bnode_proc *allProcs = 0; /* list of all processes for which we're waiting */
40 static struct opr_queue allTypes; /**< List of all registered type handlers */
42 static struct bnode_stats {
46 extern const char *DoCore;
47 extern const char *DoPidFiles;
49 extern char **environ; /* env structure */
52 int hdl_notifier(struct bnode_proc *tp);
54 /* Remember the name of the process, if any, that failed last */
56 RememberProcName(struct bnode_proc *ap)
58 struct bnode *tbnodep;
61 if (tbnodep->lastErrorName) {
62 free(tbnodep->lastErrorName);
63 tbnodep->lastErrorName = NULL;
66 tbnodep->lastErrorName = strdup(ap->coreName);
69 /* utility for use by BOP_HASCORE functions to determine where a core file might
73 bnode_CoreName(struct bnode *abnode, char *acoreName, char *abuffer)
76 strcpy(abuffer, DoCore);
78 strcat(abuffer, AFSDIR_CORE_FILE);
80 strcpy(abuffer, AFSDIR_SERVER_CORELOG_FILEPATH);
82 strcat(abuffer, acoreName);
85 strcat(abuffer, abnode->name);
89 /* save core file, if any */
91 SaveCore(struct bnode *abnode, struct bnode_proc
97 char *corefile = NULL;
98 #ifdef BOZO_SAVE_CORES
100 struct tm *TimeFields;
104 /* Linux always appends the PID to core dumps from threaded processes, so
105 * we have to scan the directory to find core files under another name. */
107 strcpy(tbuffer, DoCore);
108 strcat(tbuffer, "/");
109 strcat(tbuffer, AFSDIR_CORE_FILE);
111 code = stat(AFSDIR_SERVER_CORELOG_FILEPATH, &tstat);
116 const char *coredir = AFSDIR_LOGS_DIR;
121 logdir = opendir(coredir);
124 while ((file = readdir(logdir)) != NULL) {
125 if (strncmp(file->d_name, "core.", 5) != 0)
127 pid = atol(file->d_name + 5);
128 if (pid == aproc->pid) {
129 asprintf(&corefile, "%s/%s", coredir, file->d_name);
130 if (corefile == NULL) {
140 corefile = strdup(tbuffer);
145 bnode_CoreName(abnode, aproc->coreName, tbuffer);
146 #ifdef BOZO_SAVE_CORES
147 FT_GetTimeOfDay(&Start, 0);
148 TimeFields = localtime(&Start.tv_sec);
149 sprintf(FileName, "%s.%d%02d%02d%02d%02d%02d", tbuffer,
150 TimeFields->tm_year + 1900, TimeFields->tm_mon + 1, TimeFields->tm_mday,
151 TimeFields->tm_hour, TimeFields->tm_min, TimeFields->tm_sec);
152 strcpy(tbuffer, FileName);
154 code = rk_rename(corefile, tbuffer);
159 bnode_GetString(struct bnode *abnode, char *abuffer,
162 return BOP_GETSTRING(abnode, abuffer, alen);
166 bnode_GetParm(struct bnode *abnode, afs_int32 aindex,
167 char *abuffer, afs_int32 alen)
169 return BOP_GETPARM(abnode, aindex, abuffer, alen);
173 bnode_GetStat(struct bnode *abnode, afs_int32 * astatus)
175 return BOP_GETSTAT(abnode, astatus);
179 bnode_RestartP(struct bnode *abnode)
181 return BOP_RESTARTP(abnode);
185 bnode_Check(struct bnode *abnode)
187 if (abnode->flags & BNODE_WAIT) {
188 abnode->flags &= ~BNODE_WAIT;
189 LWP_NoYieldSignal(abnode);
194 /* tell if an instance has a core file */
196 bnode_HasCore(struct bnode *abnode)
198 return BOP_HASCORE(abnode);
201 /* wait for all bnodes to stabilize */
210 for (tb = allBnodes; tb; tb = tb->next) {
212 code = BOP_GETSTAT(tb, &stat);
217 if (stat != tb->goal) {
218 tb->flags |= BNODE_WAIT;
228 /* wait until bnode status is correct */
230 bnode_WaitStatus(struct bnode *abnode, int astatus)
238 code = BOP_GETSTAT(abnode, &stat);
242 /* otherwise, check if we're done */
243 if (stat == astatus) {
244 bnode_Release(abnode);
247 if (astatus != abnode->goal) {
248 bnode_Release(abnode);
249 return -1; /* no longer our goal, don't keep waiting */
251 /* otherwise, block */
252 abnode->flags |= BNODE_WAIT;
253 LWP_WaitProcess(abnode);
258 bnode_ResetErrorCount(struct bnode *abnode)
260 abnode->errorStopCount = 0;
261 abnode->errorStopDelay = 0;
266 bnode_SetStat(struct bnode *abnode, int agoal)
268 abnode->goal = agoal;
270 BOP_SETSTAT(abnode, agoal);
271 abnode->flags &= ~BNODE_ERRORSTOP;
276 bnode_SetGoal(struct bnode *abnode, int agoal)
278 abnode->goal = agoal;
284 bnode_SetFileGoal(struct bnode *abnode, int agoal)
286 if (abnode->fileGoal == agoal)
287 return 0; /* already done */
288 abnode->fileGoal = agoal;
293 /* apply a function to all bnodes in the system */
295 bnode_ApplyInstance(int (*aproc) (struct bnode *tb, void *), void *arock)
297 struct bnode *tb, *nb;
300 for (tb = allBnodes; tb; tb = nb) {
302 code = (*aproc) (tb, arock);
310 bnode_FindInstance(char *aname)
314 for (tb = allBnodes; tb; tb = tb->next) {
315 if (!strcmp(tb->name, aname))
321 static struct bnode_type *
322 FindType(char *aname)
324 struct opr_queue *cursor;
326 for (opr_queue_Scan(&allTypes, cursor)) {
327 struct bnode_type *tt = opr_queue_Entry(cursor, struct bnode_type, q);
329 if (!strcmp(tt->name, aname))
336 bnode_Register(char *atype, struct bnode_ops *aprocs, int anparms)
338 struct opr_queue *cursor;
339 struct bnode_type *tt = NULL;
341 for (opr_queue_Scan(&allTypes, cursor), tt = NULL) {
342 tt = opr_queue_Entry(cursor, struct bnode_type, q);
343 if (!strcmp(tt->name, atype))
347 tt = calloc(1, sizeof(struct bnode_type));
348 opr_queue_Init(&tt->q);
349 opr_queue_Prepend(&allTypes, &tt->q);
357 bnode_Create(char *atype, char *ainstance, struct bnode ** abp, char *ap1,
358 char *ap2, char *ap3, char *ap4, char *ap5, char *notifier,
359 int fileGoal, int rewritefile)
361 struct bnode_type *type;
363 char *notifierpath = NULL;
366 if (bnode_FindInstance(ainstance))
368 type = FindType(atype);
372 if (notifier && strcmp(notifier, NONOTIFIER)) {
373 /* construct local path from canonical (wire-format) path */
374 if (ConstructLocalBinPath(notifier, ¬ifierpath)) {
375 bozo_Log("BNODE-Create: Notifier program path invalid '%s'\n",
380 if (stat(notifierpath, &tstat)) {
381 bozo_Log("BNODE-Create: Notifier program '%s' not found\n",
387 tb = (*type->ops->create) (ainstance, ap1, ap2, ap3, ap4, ap5);
392 tb->notifier = notifierpath;
396 /* The fs_create above calls bnode_InitBnode() which always sets the
397 ** fileGoal to BSTAT_NORMAL .... overwrite it with whatever is passed into
398 ** this function as a parameter... */
399 tb->fileGoal = fileGoal;
401 bnode_SetStat(tb, tb->goal); /* nudge it once */
403 if (rewritefile != 0)
410 bnode_DeleteName(char *ainstance)
414 tb = bnode_FindInstance(ainstance);
418 return bnode_Delete(tb);
422 bnode_Hold(struct bnode *abnode)
429 bnode_Release(struct bnode *abnode)
432 if (abnode->refCount == 0 && abnode->flags & BNODE_DELETE) {
433 abnode->flags &= ~BNODE_DELETE; /* we're going for it */
434 bnode_Delete(abnode);
440 bnode_Delete(struct bnode *abnode)
443 struct bnode **lb, *ub;
446 if (abnode->refCount != 0) {
447 abnode->flags |= BNODE_DELETE;
451 /* make sure the bnode is idle before zapping */
453 code = BOP_GETSTAT(abnode, &temp);
454 bnode_Release(abnode);
457 if (temp != BSTAT_SHUTDOWN)
460 /* all clear to zap */
461 for (lb = &allBnodes, ub = *lb; ub; lb = &ub->next, ub = *lb) {
463 /* unthread it from the list */
468 free(abnode->name); /* do this first, since bnode fields may be bad after BOP_DELETE */
469 code = BOP_DELETE(abnode); /* don't play games like holding over this one */
474 /* function to tell if there's a timeout coming up */
476 bnode_PendingTimeout(struct bnode *abnode)
478 return (abnode->flags & BNODE_NEEDTIMEOUT);
481 /* function called to set / clear periodic bnode wakeup times */
483 bnode_SetTimeout(struct bnode *abnode, afs_int32 atimeout)
486 abnode->nextTimeout = FT_ApproxTime() + atimeout;
487 abnode->flags |= BNODE_NEEDTIMEOUT;
488 abnode->period = atimeout;
489 IOMGR_Cancel(bproc_pid);
491 abnode->flags &= ~BNODE_NEEDTIMEOUT;
496 /* used by new bnode creation code to format bnode header */
498 bnode_InitBnode(struct bnode *abnode, struct bnode_ops *abnodeops,
501 struct bnode **lb, *nb;
503 /* format the bnode properly */
504 memset(abnode, 0, sizeof(struct bnode));
505 abnode->ops = abnodeops;
506 abnode->name = strdup(aname);
509 abnode->flags = BNODE_ACTIVE;
510 abnode->fileGoal = BSTAT_NORMAL;
511 abnode->goal = BSTAT_SHUTDOWN;
513 /* put the bnode at the end of the list so we write bnode file in same order */
514 for (lb = &allBnodes, nb = *lb; nb; lb = &nb->next, nb = *lb);
521 DeleteProc(struct bnode_proc *abproc)
523 struct bnode_proc **pb, *tb;
524 struct bnode_proc *nb;
526 for (pb = &allProcs, tb = *pb; tb; pb = &tb->next, tb = nb) {
537 /* bnode lwp executes this code repeatedly */
544 struct bnode_proc *tp;
546 int options; /* must not be register */
552 /* first figure out how long to sleep for */
553 temp = 0x7fffffff; /* afs_int32 time; maxint doesn't work in select */
555 for (tb = allBnodes; tb; tb = tb->next) {
556 if (tb->flags & BNODE_NEEDTIMEOUT) {
557 if (tb->nextTimeout < temp) {
559 temp = tb->nextTimeout;
563 /* now temp has the time at which we should wakeup next */
567 temp -= FT_ApproxTime(); /* how many seconds until next event */
573 code = IOMGR_Select(0, 0, 0, 0, &tv);
575 code = 0; /* fake timeout code */
577 /* figure out why we woke up; child exit or timeouts */
578 FT_GetTimeOfDay(&tv, 0); /* must do the real gettimeofday once and a while */
581 /* check all bnodes to see which ones need timeout events */
582 for (tb = allBnodes; tb; tb = nb) {
583 if ((tb->flags & BNODE_NEEDTIMEOUT) && temp > tb->nextTimeout) {
587 if (tb->flags & BNODE_NEEDTIMEOUT) { /* check again, BOP_TIMEOUT could change */
588 tb->nextTimeout = FT_ApproxTime() + tb->period;
591 bnode_Release(tb); /* delete may occur here */
597 /* signalled, probably by incoming signal */
600 bnode_waiting = options | 0x800000;
601 code = waitpid((pid_t) - 1, &status, options);
603 if (code == 0 || code == -1)
604 break; /* all done */
605 /* otherwise code has a process id, which we now search for */
606 for (tp = allProcs; tp; tp = tp->next)
614 /* count restarts in last 30 seconds */
615 if (temp > tb->rsTime + 30) {
616 /* it's been 30 seconds we've been counting */
622 if (WIFSIGNALED(status) == 0) {
623 /* exited, not signalled */
624 tp->lastExit = WEXITSTATUS(status);
627 tb->errorCode = tp->lastExit;
628 tb->lastErrorExit = FT_ApproxTime();
629 RememberProcName(tp);
633 bozo_Log("%s:%s exited with code %d\n", tb->name,
634 tp->coreName, tp->lastExit);
636 bozo_Log("%s exited with code %d\n", tb->name,
639 /* Signal occurred, perhaps spurious due to shutdown request.
640 * If due to a shutdown request, don't overwrite last error
643 tp->lastSignal = WTERMSIG(status);
645 if (tp->lastSignal != SIGQUIT
646 && tp->lastSignal != SIGTERM
647 && tp->lastSignal != SIGKILL) {
648 tb->errorSignal = tp->lastSignal;
649 tb->lastErrorExit = FT_ApproxTime();
650 RememberProcName(tp);
653 bozo_Log("%s:%s exited on signal %d%s\n",
654 tb->name, tp->coreName, tp->lastSignal,
655 WCOREDUMP(status) ? " (core dumped)" :
658 bozo_Log("%s exited on signal %d%s\n", tb->name,
660 WCOREDUMP(status) ? " (core dumped)" :
664 tb->lastAnyExit = FT_ApproxTime();
667 bozo_Log("BNODE: Notifier %s will be called\n",
672 if (tb->goal && tb->rsCount++ > 10) {
673 /* 10 in 30 seconds */
674 if (tb->errorStopCount >= BNODE_ERROR_COUNT_MAX) {
675 tb->errorStopDelay = 0; /* max reached, give up. */
677 tb->errorStopCount++;
678 if (!tb->errorStopDelay) {
679 tb->errorStopDelay = 1;
681 tb->errorStopDelay *= 2;
684 tb->flags |= BNODE_ERRORSTOP;
685 bnode_SetGoal(tb, BSTAT_SHUTDOWN);
687 ("BNODE '%s' repeatedly failed to start, perhaps missing executable.\n",
690 BOP_PROCEXIT(tb, tp);
692 bnode_Release(tb); /* bnode delete can happen here */
695 bnode_stats.weirdPids++;
703 SendNotifierData(int fd, struct bnode_proc *tp)
705 struct bnode *tb = tp->bnode;
706 char buffer[1000], *bufp = buffer, *buf1;
710 * First sent out the bnode_proc struct
712 (void)sprintf(bufp, "BEGIN bnode_proc\n");
713 bufp += strlen(bufp);
714 (void)sprintf(bufp, "comLine: %s\n", tp->comLine);
715 bufp += strlen(bufp);
716 if (!(buf1 = tp->coreName))
718 (void)sprintf(bufp, "coreName: %s\n", buf1);
719 bufp += strlen(bufp);
720 (void)sprintf(bufp, "pid: %ld\n", afs_printable_int32_ld(tp->pid));
721 bufp += strlen(bufp);
722 (void)sprintf(bufp, "lastExit: %ld\n", afs_printable_int32_ld(tp->lastExit));
723 bufp += strlen(bufp);
725 (void)sprintf(bufp, "lastSignal: %ld\n", afs_printable_int32_ld(tp->lastSignal));
726 bufp += strlen(bufp);
728 (void)sprintf(bufp, "flags: %ld\n", afs_printable_int32_ld(tp->flags));
729 bufp += strlen(bufp);
730 (void)sprintf(bufp, "END bnode_proc\n");
731 bufp += strlen(bufp);
732 len = (int)(bufp - buffer);
733 if (write(fd, buffer, len) < 0) {
738 * Now sent out the bnode struct
741 (void)sprintf(bufp, "BEGIN bnode\n");
742 bufp += strlen(bufp);
743 (void)sprintf(bufp, "name: %s\n", tb->name);
744 bufp += strlen(bufp);
745 (void)sprintf(bufp, "rsTime: %ld\n", afs_printable_int32_ld(tb->rsTime));
746 bufp += strlen(bufp);
747 (void)sprintf(bufp, "rsCount: %ld\n", afs_printable_int32_ld(tb->rsCount));
748 bufp += strlen(bufp);
749 (void)sprintf(bufp, "procStartTime: %ld\n", afs_printable_int32_ld(tb->procStartTime));
750 bufp += strlen(bufp);
751 (void)sprintf(bufp, "procStarts: %ld\n", afs_printable_int32_ld(tb->procStarts));
752 bufp += strlen(bufp);
753 (void)sprintf(bufp, "lastAnyExit: %ld\n", afs_printable_int32_ld(tb->lastAnyExit));
754 bufp += strlen(bufp);
755 (void)sprintf(bufp, "lastErrorExit: %ld\n", afs_printable_int32_ld(tb->lastErrorExit));
756 bufp += strlen(bufp);
757 (void)sprintf(bufp, "errorCode: %ld\n", afs_printable_int32_ld(tb->errorCode));
758 bufp += strlen(bufp);
759 (void)sprintf(bufp, "errorSignal: %ld\n", afs_printable_int32_ld(tb->errorSignal));
760 bufp += strlen(bufp);
762 (void) sprintf(bufp, "lastErrorName: %s\n", tb->lastErrorName);
763 bufp += strlen(bufp);
765 (void)sprintf(bufp, "goal: %d\n", tb->goal);
766 bufp += strlen(bufp);
767 (void)sprintf(bufp, "END bnode\n");
768 bufp += strlen(bufp);
769 len = (int)(bufp - buffer);
770 if (write(fd, buffer, len) < 0) {
777 hdl_notifier(struct bnode_proc *tp)
779 #ifndef AFS_NT40_ENV /* NT notifier callout not yet implemented */
783 if (stat(tp->bnode->notifier, &tstat)) {
784 bozo_Log("BNODE: Failed to find notifier '%s'; ignored\n",
785 tp->bnode->notifier);
788 if ((pid = fork()) == 0) {
790 struct bnode *tb = tp->bnode;
792 #if defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_SGI51_ENV)
794 #elif defined(AFS_DARWIN90_ENV)
796 #elif defined(AFS_LINUX20_ENV) || defined(AFS_AIX_ENV)
801 fout = popen(tb->notifier, "w");
803 bozo_Log("BNODE: Failed to find notifier '%s'; ignored\n",
805 perror(tb->notifier);
808 SendNotifierData(fileno(fout), tp);
811 } else if (pid < 0) {
812 bozo_Log("Failed to fork creating process to handle notifier '%s'\n",
813 tp->bnode->notifier);
816 #endif /* AFS_NT40_ENV */
820 /* Called by IOMGR at low priority on IOMGR's stack shortly after a SIGCHLD
821 * occurs. Wakes up bproc do redo things */
823 bnode_SoftInt(void *param)
825 /* int asignal = (int) param; */
827 IOMGR_Cancel(bproc_pid);
831 /* Called at signal interrupt level; queues function to be called
832 * when IOMGR runs again.
835 bnode_Int(int asignal)
837 if (asignal == SIGQUIT || asignal == SIGTERM) {
838 IOMGR_SoftSig(bozo_ShutdownAndExit, (void *)(intptr_t)asignal);
840 IOMGR_SoftSig(bnode_SoftInt, (void *)(intptr_t)asignal);
845 /* intialize the whole system */
851 struct sigaction newaction;
852 static int initDone = 0;
857 opr_queue_Init(&allTypes);
858 memset(&bnode_stats, 0, sizeof(bnode_stats));
859 LWP_InitializeProcessSupport(1, &junk); /* just in case */
861 code = LWP_CreateProcess(bproc, BNODE_LWP_STACKSIZE,
862 /* priority */ 1, (void *) /* parm */ 0,
863 "bnode-manager", &bproc_pid);
866 memset(&newaction, 0, sizeof(newaction));
867 newaction.sa_handler = bnode_Int;
868 code = sigaction(SIGCHLD, &newaction, NULL);
871 code = sigaction(SIGQUIT, &newaction, NULL);
874 code = sigaction(SIGTERM, &newaction, NULL);
880 /* free token list returned by parseLine */
882 bnode_FreeTokens(struct bnode_token *alist)
884 struct bnode_token *nlist;
885 for (; alist; alist = nlist) {
896 if (x == 0 || x == ' ' || x == '\t' || x == '\n')
903 bnode_ParseLine(char *aline, struct bnode_token **alist)
908 struct bnode_token *first, *last;
909 struct bnode_token *ttok;
912 inToken = 0; /* not copying token chars at start */
913 first = (struct bnode_token *)0;
914 last = (struct bnode_token *)0;
917 if (tc == 0 || space(tc)) { /* terminating null gets us in here, too */
919 inToken = 0; /* end of this token */
921 ttok = malloc(sizeof(struct bnode_token));
922 ttok->next = (struct bnode_token *)0;
923 ttok->key = strdup(tbuffer);
933 /* an alpha character */
938 if (tptr - tbuffer >= sizeof(tbuffer))
939 return -1; /* token too long */
943 /* last token flushed 'cause space(0) --> true */
945 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 = calloc(1, sizeof(struct bnode_proc));
970 tp->comLine = aexecString;
971 tp->coreName = coreName; /* may be null */
972 abnode->procStartTime = FT_ApproxTime();
973 abnode->procStarts++;
975 /* convert linked list of tokens into argv structure */
976 for (tt = tlist, i = 0; i < (MAXVARGS - 1) && tt; tt = tt->next, i++) {
979 argv[i] = NULL; /* null-terminated */
981 cpid = spawnprocve(argv[0], argv, environ, -1);
982 osi_audit(BOSSpawnProcEvent, 0, AUD_STR, aexecString, AUD_END);
984 if (cpid == (pid_t) - 1) {
985 bozo_Log("Failed to spawn process for bnode '%s'\n", abnode->name);
986 bnode_FreeTokens(tlist);
990 bozo_Log("%s started pid %ld: %s\n", abnode->name, cpid, aexecString);
992 bnode_FreeTokens(tlist);
996 tp->flags = BPROC_STARTED;
997 tp->flags &= ~BPROC_EXITED;
998 BOP_PROCSTARTED(abnode, tp);
1004 bnode_StopProc(struct bnode_proc *aproc, int asignal)
1007 if (!(aproc->flags & BPROC_STARTED) || (aproc->flags & BPROC_EXITED))
1010 osi_audit(BOSStopProcEvent, 0, AUD_STR, (aproc ? aproc->comLine : NULL),
1013 code = kill(aproc->pid, asignal);
1014 bnode_Check(aproc->bnode);
1019 bnode_Deactivate(struct bnode *abnode)
1021 struct bnode **pb, *tb;
1023 if (!(abnode->flags & BNODE_ACTIVE))
1025 for (pb = &allBnodes, tb = *pb; tb; tb = nb) {
1029 tb->flags &= ~BNODE_ACTIVE;