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;
44 extern const char *DoPidFiles;
46 extern char **environ; /* env structure */
49 int hdl_notifier(struct bnode_proc *tp);
51 /* Remember the name of the process, if any, that failed last */
53 RememberProcName(struct bnode_proc *ap)
55 struct bnode *tbnodep;
58 if (tbnodep->lastErrorName) {
59 free(tbnodep->lastErrorName);
60 tbnodep->lastErrorName = NULL;
63 tbnodep->lastErrorName = (char *)malloc(strlen(ap->coreName) + 1);
64 strcpy(tbnodep->lastErrorName, ap->coreName);
68 /* utility for use by BOP_HASCORE functions to determine where a core file might
72 bnode_CoreName(struct bnode *abnode, char *acoreName, char *abuffer)
75 strcpy(abuffer, DoCore);
77 strcat(abuffer, AFSDIR_CORE_FILE);
79 strcpy(abuffer, AFSDIR_SERVER_CORELOG_FILEPATH);
81 strcat(abuffer, acoreName);
84 strcat(abuffer, abnode->name);
88 /* save core file, if any */
90 SaveCore(struct bnode *abnode, struct bnode_proc
96 char *corefile = NULL;
97 #ifdef BOZO_SAVE_CORES
99 struct tm *TimeFields;
103 /* Linux always appends the PID to core dumps from threaded processes, so
104 * we have to scan the directory to find core files under another name. */
106 strcpy(tbuffer, DoCore);
107 strcat(tbuffer, "/");
108 strcat(tbuffer, AFSDIR_CORE_FILE);
110 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 length = strlen(coredir) + strlen(file->d_name) + 2;
130 corefile = malloc(length);
131 if (corefile == NULL) {
135 snprintf(corefile, length, "%s/%s", coredir, file->d_name);
142 corefile = strdup(tbuffer);
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 code = renamefile(corefile, tbuffer);
161 bnode_GetString(struct bnode *abnode, char *abuffer,
164 return BOP_GETSTRING(abnode, abuffer, alen);
168 bnode_GetParm(struct bnode *abnode, afs_int32 aindex,
169 char *abuffer, afs_int32 alen)
171 return BOP_GETPARM(abnode, aindex, abuffer, alen);
175 bnode_GetStat(struct bnode *abnode, afs_int32 * astatus)
177 return BOP_GETSTAT(abnode, astatus);
181 bnode_RestartP(struct bnode *abnode)
183 return BOP_RESTARTP(abnode);
187 bnode_Check(struct bnode *abnode)
189 if (abnode->flags & BNODE_WAIT) {
190 abnode->flags &= ~BNODE_WAIT;
191 LWP_NoYieldSignal(abnode);
196 /* tell if an instance has a core file */
198 bnode_HasCore(struct bnode *abnode)
200 return BOP_HASCORE(abnode);
203 /* wait for all bnodes to stabilize */
212 for (tb = allBnodes; tb; tb = tb->next) {
214 code = BOP_GETSTAT(tb, &stat);
219 if (stat != tb->goal) {
220 tb->flags |= BNODE_WAIT;
230 /* wait until bnode status is correct */
232 bnode_WaitStatus(struct bnode *abnode, int astatus)
240 code = BOP_GETSTAT(abnode, &stat);
244 /* otherwise, check if we're done */
245 if (stat == astatus) {
246 bnode_Release(abnode);
249 if (astatus != abnode->goal) {
250 bnode_Release(abnode);
251 return -1; /* no longer our goal, don't keep waiting */
253 /* otherwise, block */
254 abnode->flags |= BNODE_WAIT;
255 LWP_WaitProcess(abnode);
260 bnode_SetStat(struct bnode *abnode, int agoal)
262 abnode->goal = agoal;
264 BOP_SETSTAT(abnode, agoal);
265 abnode->flags &= ~BNODE_ERRORSTOP;
270 bnode_SetGoal(struct bnode *abnode, int agoal)
272 abnode->goal = agoal;
278 bnode_SetFileGoal(struct bnode *abnode, int agoal)
280 if (abnode->fileGoal == agoal)
281 return 0; /* already done */
282 abnode->fileGoal = agoal;
287 /* apply a function to all bnodes in the system */
289 bnode_ApplyInstance(int (*aproc) (struct bnode *tb, void *), void *arock)
291 struct bnode *tb, *nb;
294 for (tb = allBnodes; tb; tb = nb) {
296 code = (*aproc) (tb, arock);
304 bnode_FindInstance(char *aname)
308 for (tb = allBnodes; tb; tb = tb->next) {
309 if (!strcmp(tb->name, aname))
315 static struct bnode_type *
316 FindType(char *aname)
318 struct bnode_type *tt;
320 for (tt = allTypes; tt; tt = tt->next) {
321 if (!strcmp(tt->name, aname))
324 return (struct bnode_type *)0;
328 bnode_Register(char *atype, struct bnode_ops *aprocs, int anparms)
330 struct bnode_type *tt;
332 for (tt = allTypes; tt; tt = tt->next) {
333 if (!strcmp(tt->name, atype))
337 tt = (struct bnode_type *)malloc(sizeof(struct bnode_type));
338 memset(tt, 0, sizeof(struct bnode_type));
348 bnode_Create(char *atype, char *ainstance, struct bnode ** abp, char *ap1,
349 char *ap2, char *ap3, char *ap4, char *ap5, char *notifier,
350 int fileGoal, int rewritefile)
352 struct bnode_type *type;
354 char *notifierpath = NULL;
357 if (bnode_FindInstance(ainstance))
359 type = FindType(atype);
363 if (notifier && strcmp(notifier, NONOTIFIER)) {
364 /* construct local path from canonical (wire-format) path */
365 if (ConstructLocalBinPath(notifier, ¬ifierpath)) {
366 bozo_Log("BNODE-Create: Notifier program path invalid '%s'\n",
371 if (stat(notifierpath, &tstat)) {
372 bozo_Log("BNODE-Create: Notifier program '%s' not found\n",
378 tb = (*type->ops->create) (ainstance, ap1, ap2, ap3, ap4, ap5);
383 tb->notifier = notifierpath;
387 /* The fs_create above calls bnode_InitBnode() which always sets the
388 ** fileGoal to BSTAT_NORMAL .... overwrite it with whatever is passed into
389 ** this function as a parameter... */
390 tb->fileGoal = fileGoal;
392 bnode_SetStat(tb, tb->goal); /* nudge it once */
394 if (rewritefile != 0)
401 bnode_DeleteName(char *ainstance)
405 tb = bnode_FindInstance(ainstance);
409 return bnode_Delete(tb);
413 bnode_Hold(struct bnode *abnode)
420 bnode_Release(struct bnode *abnode)
423 if (abnode->refCount == 0 && abnode->flags & BNODE_DELETE) {
424 abnode->flags &= ~BNODE_DELETE; /* we're going for it */
425 bnode_Delete(abnode);
431 bnode_Delete(struct bnode *abnode)
434 struct bnode **lb, *ub;
437 if (abnode->refCount != 0) {
438 abnode->flags |= BNODE_DELETE;
442 /* make sure the bnode is idle before zapping */
444 code = BOP_GETSTAT(abnode, &temp);
445 bnode_Release(abnode);
448 if (temp != BSTAT_SHUTDOWN)
451 /* all clear to zap */
452 for (lb = &allBnodes, ub = *lb; ub; lb = &ub->next, ub = *lb) {
454 /* unthread it from the list */
459 free(abnode->name); /* do this first, since bnode fields may be bad after BOP_DELETE */
460 code = BOP_DELETE(abnode); /* don't play games like holding over this one */
465 /* function to tell if there's a timeout coming up */
467 bnode_PendingTimeout(struct bnode *abnode)
469 return (abnode->flags & BNODE_NEEDTIMEOUT);
472 /* function called to set / clear periodic bnode wakeup times */
474 bnode_SetTimeout(struct bnode *abnode, afs_int32 atimeout)
477 abnode->nextTimeout = FT_ApproxTime() + atimeout;
478 abnode->flags |= BNODE_NEEDTIMEOUT;
479 abnode->period = atimeout;
480 IOMGR_Cancel(bproc_pid);
482 abnode->flags &= ~BNODE_NEEDTIMEOUT;
487 /* used by new bnode creation code to format bnode header */
489 bnode_InitBnode(struct bnode *abnode, struct bnode_ops *abnodeops,
492 struct bnode **lb, *nb;
494 /* format the bnode properly */
495 memset(abnode, 0, sizeof(struct bnode));
496 abnode->ops = abnodeops;
497 abnode->name = (char *)malloc(strlen(aname) + 1);
500 strcpy(abnode->name, aname);
501 abnode->flags = BNODE_ACTIVE;
502 abnode->fileGoal = BSTAT_NORMAL;
503 abnode->goal = BSTAT_SHUTDOWN;
505 /* put the bnode at the end of the list so we write bnode file in same order */
506 for (lb = &allBnodes, nb = *lb; nb; lb = &nb->next, nb = *lb);
513 DeleteProc(struct bnode_proc *abproc)
515 struct bnode_proc **pb, *tb;
516 struct bnode_proc *nb;
518 for (pb = &allProcs, tb = *pb; tb; pb = &tb->next, tb = nb) {
529 /* bnode lwp executes this code repeatedly */
536 struct bnode_proc *tp;
538 int options; /* must not be register */
544 /* first figure out how long to sleep for */
545 temp = 0x7fffffff; /* afs_int32 time; maxint doesn't work in select */
547 for (tb = allBnodes; tb; tb = tb->next) {
548 if (tb->flags & BNODE_NEEDTIMEOUT) {
549 if (tb->nextTimeout < temp) {
551 temp = tb->nextTimeout;
555 /* now temp has the time at which we should wakeup next */
559 temp -= FT_ApproxTime(); /* how many seconds until next event */
565 code = IOMGR_Select(0, 0, 0, 0, &tv);
567 code = 0; /* fake timeout code */
569 /* figure out why we woke up; child exit or timeouts */
570 FT_GetTimeOfDay(&tv, 0); /* must do the real gettimeofday once and a while */
573 /* check all bnodes to see which ones need timeout events */
574 for (tb = allBnodes; tb; tb = nb) {
575 if ((tb->flags & BNODE_NEEDTIMEOUT) && temp > tb->nextTimeout) {
579 if (tb->flags & BNODE_NEEDTIMEOUT) { /* check again, BOP_TIMEOUT could change */
580 tb->nextTimeout = FT_ApproxTime() + tb->period;
583 bnode_Release(tb); /* delete may occur here */
589 /* signalled, probably by incoming signal */
592 bnode_waiting = options | 0x800000;
593 code = waitpid((pid_t) - 1, &status, options);
595 if (code == 0 || code == -1)
596 break; /* all done */
597 /* otherwise code has a process id, which we now search for */
598 for (tp = allProcs; tp; tp = tp->next)
606 /* count restarts in last 10 seconds */
607 if (temp > tb->rsTime + 30) {
608 /* it's been 10 seconds we've been counting */
613 if (WIFSIGNALED(status) == 0) {
614 /* exited, not signalled */
615 tp->lastExit = WEXITSTATUS(status);
618 tb->errorCode = tp->lastExit;
619 tb->lastErrorExit = FT_ApproxTime();
620 RememberProcName(tp);
624 bozo_Log("%s:%s exited with code %d\n", tb->name,
625 tp->coreName, tp->lastExit);
627 bozo_Log("%s exited with code %d\n", tb->name,
630 /* Signal occurred, perhaps spurious due to shutdown request.
631 * If due to a shutdown request, don't overwrite last error
634 tp->lastSignal = WTERMSIG(status);
636 if (tp->lastSignal != SIGQUIT
637 && tp->lastSignal != SIGTERM
638 && tp->lastSignal != SIGKILL) {
639 tb->errorSignal = tp->lastSignal;
640 tb->lastErrorExit = FT_ApproxTime();
641 RememberProcName(tp);
644 bozo_Log("%s:%s exited on signal %d%s\n",
645 tb->name, tp->coreName, tp->lastSignal,
646 WCOREDUMP(status) ? " (core dumped)" :
649 bozo_Log("%s exited on signal %d%s\n", tb->name,
651 WCOREDUMP(status) ? " (core dumped)" :
655 tb->lastAnyExit = FT_ApproxTime();
658 bozo_Log("BNODE: Notifier %s will be called\n",
662 BOP_PROCEXIT(tb, tp);
665 if (tb->rsCount++ > 10) {
666 /* 10 in 10 seconds */
667 tb->flags |= BNODE_ERRORSTOP;
668 bnode_SetGoal(tb, BSTAT_SHUTDOWN);
670 ("BNODE '%s' repeatedly failed to start, perhaps missing executable.\n",
673 bnode_Release(tb); /* bnode delete can happen here */
676 bnode_stats.weirdPids++;
684 SendNotifierData(int fd, struct bnode_proc *tp)
686 struct bnode *tb = tp->bnode;
687 char buffer[1000], *bufp = buffer, *buf1;
691 * First sent out the bnode_proc struct
693 (void)sprintf(bufp, "BEGIN bnode_proc\n");
694 bufp += strlen(bufp);
695 (void)sprintf(bufp, "comLine: %s\n", tp->comLine);
696 bufp += strlen(bufp);
697 if (!(buf1 = tp->coreName))
699 (void)sprintf(bufp, "coreName: %s\n", buf1);
700 bufp += strlen(bufp);
701 (void)sprintf(bufp, "pid: %ld\n", afs_printable_int32_ld(tp->pid));
702 bufp += strlen(bufp);
703 (void)sprintf(bufp, "lastExit: %ld\n", afs_printable_int32_ld(tp->lastExit));
704 bufp += strlen(bufp);
706 (void)sprintf(bufp, "lastSignal: %ld\n", afs_printable_int32_ld(tp->lastSignal));
707 bufp += strlen(bufp);
709 (void)sprintf(bufp, "flags: %ld\n", afs_printable_int32_ld(tp->flags));
710 bufp += strlen(bufp);
711 (void)sprintf(bufp, "END bnode_proc\n");
712 bufp += strlen(bufp);
713 len = (int)(bufp - buffer);
714 if (write(fd, buffer, len) < 0) {
719 * Now sent out the bnode struct
722 (void)sprintf(bufp, "BEGIN bnode\n");
723 bufp += strlen(bufp);
724 (void)sprintf(bufp, "name: %s\n", tb->name);
725 bufp += strlen(bufp);
726 (void)sprintf(bufp, "rsTime: %ld\n", afs_printable_int32_ld(tb->rsTime));
727 bufp += strlen(bufp);
728 (void)sprintf(bufp, "rsCount: %ld\n", afs_printable_int32_ld(tb->rsCount));
729 bufp += strlen(bufp);
730 (void)sprintf(bufp, "procStartTime: %ld\n", afs_printable_int32_ld(tb->procStartTime));
731 bufp += strlen(bufp);
732 (void)sprintf(bufp, "procStarts: %ld\n", afs_printable_int32_ld(tb->procStarts));
733 bufp += strlen(bufp);
734 (void)sprintf(bufp, "lastAnyExit: %ld\n", afs_printable_int32_ld(tb->lastAnyExit));
735 bufp += strlen(bufp);
736 (void)sprintf(bufp, "lastErrorExit: %ld\n", afs_printable_int32_ld(tb->lastErrorExit));
737 bufp += strlen(bufp);
738 (void)sprintf(bufp, "errorCode: %ld\n", afs_printable_int32_ld(tb->errorCode));
739 bufp += strlen(bufp);
740 (void)sprintf(bufp, "errorSignal: %ld\n", afs_printable_int32_ld(tb->errorSignal));
741 bufp += strlen(bufp);
743 (void) sprintf(bufp, "lastErrorName: %s\n", tb->lastErrorName);
744 bufp += strlen(bufp);
746 (void)sprintf(bufp, "goal: %d\n", tb->goal);
747 bufp += strlen(bufp);
748 (void)sprintf(bufp, "END bnode\n");
749 bufp += strlen(bufp);
750 len = (int)(bufp - buffer);
751 if (write(fd, buffer, len) < 0) {
758 hdl_notifier(struct bnode_proc *tp)
760 #ifndef AFS_NT40_ENV /* NT notifier callout not yet implemented */
764 if (stat(tp->bnode->notifier, &tstat)) {
765 bozo_Log("BNODE: Failed to find notifier '%s'; ignored\n",
766 tp->bnode->notifier);
769 if ((pid = fork()) == 0) {
771 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 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;
980 BOP_PROCSTARTED(abnode, tp);
986 bnode_StopProc(struct bnode_proc *aproc, int asignal)
989 if (!(aproc->flags & BPROC_STARTED) || (aproc->flags & BPROC_EXITED))
992 osi_audit(BOSStopProcEvent, 0, AUD_STR, (aproc ? aproc->comLine : NULL),
995 code = kill(aproc->pid, asignal);
996 bnode_Check(aproc->bnode);
1001 bnode_Deactivate(struct bnode *abnode)
1003 struct bnode **pb, *tb;
1005 if (!(abnode->flags & BNODE_ACTIVE))
1007 for (pb = &allBnodes, tb = *pb; tb; tb = nb) {
1011 tb->flags &= ~BNODE_ACTIVE;