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>
21 #include <sys/types.h>
31 #ifdef BOZO_SAVE_CORES
37 #include <afs/procmgmt.h> /* signal(), kill(), wait(), etc. */
40 #include <afs/audit.h>
41 #include <afs/afsutil.h>
42 #include <afs/fileutil.h>
44 #include "bosprototypes.h"
47 #define WCOREDUMP(x) ((x) & 0200)
50 #define BNODE_LWP_STACKSIZE (16 * 1024)
52 int bnode_waiting = 0;
53 static PROCESS bproc_pid; /* pid of waker-upper */
54 static struct bnode *allBnodes = 0; /* list of all bnodes */
55 static struct bnode_proc *allProcs = 0; /* list of all processes for which we're waiting */
56 static struct bnode_type *allTypes = 0; /* list of registered type handlers */
58 static struct bnode_stats {
62 extern const char *DoCore;
64 extern char **environ; /* env structure */
67 int hdl_notifier(struct bnode_proc *tp);
69 /* Remember the name of the process, if any, that failed last */
71 RememberProcName(struct bnode_proc *ap)
73 struct bnode *tbnodep;
76 if (tbnodep->lastErrorName) {
77 free(tbnodep->lastErrorName);
78 tbnodep->lastErrorName = NULL;
81 tbnodep->lastErrorName = (char *)malloc(strlen(ap->coreName) + 1);
82 strcpy(tbnodep->lastErrorName, ap->coreName);
86 /* utility for use by BOP_HASCORE functions to determine where a core file might
90 bnode_CoreName(struct bnode *abnode, char *acoreName, char *abuffer)
93 strcpy(abuffer, DoCore);
95 strcat(abuffer, AFSDIR_CORE_FILE);
97 strcpy(abuffer, AFSDIR_SERVER_CORELOG_FILEPATH);
99 strcat(abuffer, acoreName);
100 strcat(abuffer, ".");
102 strcat(abuffer, abnode->name);
106 /* save core file, if any */
108 SaveCore(struct bnode *abnode, struct bnode_proc
114 char *corefile = NULL;
115 #ifdef BOZO_SAVE_CORES
116 struct timeval Start;
117 struct tm *TimeFields;
121 /* Linux always appends the PID to core dumps from threaded processes, so
122 * we have to scan the directory to find core files under another name. */
124 strcpy(tbuffer, DoCore);
125 strcat(tbuffer, "/");
126 strcat(tbuffer, AFSDIR_CORE_FILE);
128 code = stat(AFSDIR_SERVER_CORELOG_FILEPATH, &tstat);
134 const char *coredir = AFSDIR_LOGS_DIR;
139 logdir = opendir(coredir);
142 while ((file = readdir(logdir)) != NULL) {
143 if (strncmp(file->d_name, "core.", 5) != 0)
145 pid = atol(file->d_name + 5);
146 if (pid == aproc->pid) {
147 length = strlen(coredir) + strlen(file->d_name) + 2;
148 corefile = malloc(length);
149 if (corefile == NULL) {
153 snprintf(corefile, length, "%s/%s", coredir, file->d_name);
160 corefile = strdup(tbuffer);
165 bnode_CoreName(abnode, aproc->coreName, tbuffer);
166 #ifdef BOZO_SAVE_CORES
167 FT_GetTimeOfDay(&Start, 0);
168 TimeFields = localtime(&Start.tv_sec);
169 sprintf(FileName, "%s.%d%02d%02d%02d%02d%02d", tbuffer,
170 TimeFields->tm_year + 1900, TimeFields->tm_mon + 1, TimeFields->tm_mday,
171 TimeFields->tm_hour, TimeFields->tm_min, TimeFields->tm_sec);
172 strcpy(tbuffer, FileName);
174 code = renamefile(corefile, tbuffer);
179 bnode_GetString(struct bnode *abnode, char *abuffer,
182 return BOP_GETSTRING(abnode, abuffer, alen);
186 bnode_GetParm(struct bnode *abnode, afs_int32 aindex,
187 char *abuffer, afs_int32 alen)
189 return BOP_GETPARM(abnode, aindex, abuffer, alen);
193 bnode_GetStat(struct bnode *abnode, afs_int32 * astatus)
195 return BOP_GETSTAT(abnode, astatus);
199 bnode_RestartP(struct bnode *abnode)
201 return BOP_RESTARTP(abnode);
205 bnode_Check(struct bnode *abnode)
207 if (abnode->flags & BNODE_WAIT) {
208 abnode->flags &= ~BNODE_WAIT;
209 LWP_NoYieldSignal(abnode);
214 /* tell if an instance has a core file */
216 bnode_HasCore(struct bnode *abnode)
218 return BOP_HASCORE(abnode);
221 /* wait for all bnodes to stabilize */
230 for (tb = allBnodes; tb; tb = tb->next) {
232 code = BOP_GETSTAT(tb, &stat);
237 if (stat != tb->goal) {
238 tb->flags |= BNODE_WAIT;
248 /* wait until bnode status is correct */
250 bnode_WaitStatus(struct bnode *abnode, int astatus)
258 code = BOP_GETSTAT(abnode, &stat);
262 /* otherwise, check if we're done */
263 if (stat == astatus) {
264 bnode_Release(abnode);
267 if (astatus != abnode->goal) {
268 bnode_Release(abnode);
269 return -1; /* no longer our goal, don't keep waiting */
271 /* otherwise, block */
272 abnode->flags |= BNODE_WAIT;
273 LWP_WaitProcess(abnode);
278 bnode_SetStat(struct bnode *abnode, int agoal)
280 abnode->goal = agoal;
282 BOP_SETSTAT(abnode, agoal);
283 abnode->flags &= ~BNODE_ERRORSTOP;
288 bnode_SetGoal(struct bnode *abnode, int agoal)
290 abnode->goal = agoal;
296 bnode_SetFileGoal(struct bnode *abnode, int agoal)
298 if (abnode->fileGoal == agoal)
299 return 0; /* already done */
300 abnode->fileGoal = agoal;
305 /* apply a function to all bnodes in the system */
307 bnode_ApplyInstance(int (*aproc) (struct bnode *tb, void *), void *arock)
309 struct bnode *tb, *nb;
312 for (tb = allBnodes; tb; tb = nb) {
314 code = (*aproc) (tb, arock);
322 bnode_FindInstance(char *aname)
326 for (tb = allBnodes; tb; tb = tb->next) {
327 if (!strcmp(tb->name, aname))
333 static struct bnode_type *
334 FindType(char *aname)
336 struct bnode_type *tt;
338 for (tt = allTypes; tt; tt = tt->next) {
339 if (!strcmp(tt->name, aname))
342 return (struct bnode_type *)0;
346 bnode_Register(char *atype, struct bnode_ops *aprocs, int anparms)
348 struct bnode_type *tt;
350 for (tt = allTypes; tt; tt = tt->next) {
351 if (!strcmp(tt->name, atype))
355 tt = (struct bnode_type *)malloc(sizeof(struct bnode_type));
356 memset(tt, 0, sizeof(struct bnode_type));
366 bnode_Create(char *atype, char *ainstance, struct bnode ** abp, char *ap1,
367 char *ap2, char *ap3, char *ap4, char *ap5, char *notifier,
368 int fileGoal, int rewritefile)
370 struct bnode_type *type;
372 char *notifierpath = NULL;
375 if (bnode_FindInstance(ainstance))
377 type = FindType(atype);
381 if (notifier && strcmp(notifier, NONOTIFIER)) {
382 /* construct local path from canonical (wire-format) path */
383 if (ConstructLocalBinPath(notifier, ¬ifierpath)) {
384 bozo_Log("BNODE-Create: Notifier program path invalid '%s'\n",
389 if (stat(notifierpath, &tstat)) {
390 bozo_Log("BNODE-Create: Notifier program '%s' not found\n",
396 tb = (*type->ops->create) (ainstance, ap1, ap2, ap3, ap4, ap5);
401 tb->notifier = notifierpath;
405 /* The fs_create above calls bnode_InitBnode() which always sets the
406 ** fileGoal to BSTAT_NORMAL .... overwrite it with whatever is passed into
407 ** this function as a parameter... */
408 tb->fileGoal = fileGoal;
410 bnode_SetStat(tb, tb->goal); /* nudge it once */
412 if (rewritefile != 0)
419 bnode_DeleteName(char *ainstance)
423 tb = bnode_FindInstance(ainstance);
427 return bnode_Delete(tb);
431 bnode_Hold(struct bnode *abnode)
438 bnode_Release(struct bnode *abnode)
441 if (abnode->refCount == 0 && abnode->flags & BNODE_DELETE) {
442 abnode->flags &= ~BNODE_DELETE; /* we're going for it */
443 bnode_Delete(abnode);
449 bnode_Delete(struct bnode *abnode)
452 struct bnode **lb, *ub;
455 if (abnode->refCount != 0) {
456 abnode->flags |= BNODE_DELETE;
460 /* make sure the bnode is idle before zapping */
462 code = BOP_GETSTAT(abnode, &temp);
463 bnode_Release(abnode);
466 if (temp != BSTAT_SHUTDOWN)
469 /* all clear to zap */
470 for (lb = &allBnodes, ub = *lb; ub; lb = &ub->next, ub = *lb) {
472 /* unthread it from the list */
477 free(abnode->name); /* do this first, since bnode fields may be bad after BOP_DELETE */
478 code = BOP_DELETE(abnode); /* don't play games like holding over this one */
483 /* function to tell if there's a timeout coming up */
485 bnode_PendingTimeout(struct bnode *abnode)
487 return (abnode->flags & BNODE_NEEDTIMEOUT);
490 /* function called to set / clear periodic bnode wakeup times */
492 bnode_SetTimeout(struct bnode *abnode, afs_int32 atimeout)
495 abnode->nextTimeout = FT_ApproxTime() + atimeout;
496 abnode->flags |= BNODE_NEEDTIMEOUT;
497 abnode->period = atimeout;
498 IOMGR_Cancel(bproc_pid);
500 abnode->flags &= ~BNODE_NEEDTIMEOUT;
505 /* used by new bnode creation code to format bnode header */
507 bnode_InitBnode(struct bnode *abnode, struct bnode_ops *abnodeops,
510 struct bnode **lb, *nb;
512 /* format the bnode properly */
513 memset(abnode, 0, sizeof(struct bnode));
514 abnode->ops = abnodeops;
515 abnode->name = (char *)malloc(strlen(aname) + 1);
518 strcpy(abnode->name, aname);
519 abnode->flags = BNODE_ACTIVE;
520 abnode->fileGoal = BSTAT_NORMAL;
521 abnode->goal = BSTAT_SHUTDOWN;
523 /* put the bnode at the end of the list so we write bnode file in same order */
524 for (lb = &allBnodes, nb = *lb; nb; lb = &nb->next, nb = *lb);
531 DeleteProc(struct bnode_proc *abproc)
533 struct bnode_proc **pb, *tb;
534 struct bnode_proc *nb;
536 for (pb = &allProcs, tb = *pb; tb; pb = &tb->next, tb = nb) {
547 /* bnode lwp executes this code repeatedly */
554 struct bnode_proc *tp;
556 int options; /* must not be register */
562 /* first figure out how long to sleep for */
563 temp = 0x7fffffff; /* afs_int32 time; maxint doesn't work in select */
565 for (tb = allBnodes; tb; tb = tb->next) {
566 if (tb->flags & BNODE_NEEDTIMEOUT) {
567 if (tb->nextTimeout < temp) {
569 temp = tb->nextTimeout;
573 /* now temp has the time at which we should wakeup next */
577 temp -= FT_ApproxTime(); /* how many seconds until next event */
583 code = IOMGR_Select(0, 0, 0, 0, &tv);
585 code = 0; /* fake timeout code */
587 /* figure out why we woke up; child exit or timeouts */
588 FT_GetTimeOfDay(&tv, 0); /* must do the real gettimeofday once and a while */
591 /* check all bnodes to see which ones need timeout events */
592 for (tb = allBnodes; tb; tb = nb) {
593 if ((tb->flags & BNODE_NEEDTIMEOUT) && temp > tb->nextTimeout) {
597 if (tb->flags & BNODE_NEEDTIMEOUT) { /* check again, BOP_TIMEOUT could change */
598 tb->nextTimeout = FT_ApproxTime() + tb->period;
601 bnode_Release(tb); /* delete may occur here */
607 /* signalled, probably by incoming signal */
610 bnode_waiting = options | 0x800000;
611 code = waitpid((pid_t) - 1, &status, options);
613 if (code == 0 || code == -1)
614 break; /* all done */
615 /* otherwise code has a process id, which we now search for */
616 for (tp = allProcs; tp; tp = tp->next)
624 /* count restarts in last 10 seconds */
625 if (temp > tb->rsTime + 30) {
626 /* it's been 10 seconds we've been counting */
631 if (WIFSIGNALED(status) == 0) {
632 /* exited, not signalled */
633 tp->lastExit = WEXITSTATUS(status);
636 tb->errorCode = tp->lastExit;
637 tb->lastErrorExit = FT_ApproxTime();
638 RememberProcName(tp);
642 bozo_Log("%s:%s exited with code %d\n", tb->name,
643 tp->coreName, tp->lastExit);
645 bozo_Log("%s exited with code %d\n", tb->name,
648 /* Signal occurred, perhaps spurious due to shutdown request.
649 * If due to a shutdown request, don't overwrite last error
652 tp->lastSignal = WTERMSIG(status);
654 if (tp->lastSignal != SIGQUIT
655 && tp->lastSignal != SIGTERM
656 && tp->lastSignal != SIGKILL) {
657 tb->errorSignal = tp->lastSignal;
658 tb->lastErrorExit = FT_ApproxTime();
659 RememberProcName(tp);
662 bozo_Log("%s:%s exited on signal %d%s\n",
663 tb->name, tp->coreName, tp->lastSignal,
664 WCOREDUMP(status) ? " (core dumped)" :
667 bozo_Log("%s exited on signal %d%s\n", tb->name,
669 WCOREDUMP(status) ? " (core dumped)" :
673 tb->lastAnyExit = FT_ApproxTime();
676 bozo_Log("BNODE: Notifier %s will be called\n",
680 BOP_PROCEXIT(tb, tp);
683 if (tb->rsCount++ > 10) {
684 /* 10 in 10 seconds */
685 tb->flags |= BNODE_ERRORSTOP;
686 bnode_SetGoal(tb, BSTAT_SHUTDOWN);
688 ("BNODE '%s' repeatedly failed to start, perhaps missing executable.\n",
691 bnode_Release(tb); /* bnode delete can happen here */
694 bnode_stats.weirdPids++;
702 SendNotifierData(int fd, struct bnode_proc *tp)
704 struct bnode *tb = tp->bnode;
705 char buffer[1000], *bufp = buffer, *buf1;
709 * First sent out the bnode_proc struct
711 (void)sprintf(bufp, "BEGIN bnode_proc\n");
712 bufp += strlen(bufp);
713 (void)sprintf(bufp, "comLine: %s\n", tp->comLine);
714 bufp += strlen(bufp);
715 if (!(buf1 = tp->coreName))
717 (void)sprintf(bufp, "coreName: %s\n", buf1);
718 bufp += strlen(bufp);
719 (void)sprintf(bufp, "pid: %ld\n", afs_printable_int32_ld(tp->pid));
720 bufp += strlen(bufp);
721 (void)sprintf(bufp, "lastExit: %ld\n", afs_printable_int32_ld(tp->lastExit));
722 bufp += strlen(bufp);
724 (void)sprintf(bufp, "lastSignal: %ld\n", afs_printable_int32_ld(tp->lastSignal));
725 bufp += strlen(bufp);
727 (void)sprintf(bufp, "flags: %ld\n", afs_printable_int32_ld(tp->flags));
728 bufp += strlen(bufp);
729 (void)sprintf(bufp, "END bnode_proc\n");
730 bufp += strlen(bufp);
731 len = (int)(bufp - buffer);
732 if (write(fd, buffer, len) < 0) {
737 * Now sent out the bnode struct
740 (void)sprintf(bufp, "BEGIN bnode\n");
741 bufp += strlen(bufp);
742 (void)sprintf(bufp, "name: %s\n", tb->name);
743 bufp += strlen(bufp);
744 (void)sprintf(bufp, "rsTime: %ld\n", afs_printable_int32_ld(tb->rsTime));
745 bufp += strlen(bufp);
746 (void)sprintf(bufp, "rsCount: %ld\n", afs_printable_int32_ld(tb->rsCount));
747 bufp += strlen(bufp);
748 (void)sprintf(bufp, "procStartTime: %ld\n", afs_printable_int32_ld(tb->procStartTime));
749 bufp += strlen(bufp);
750 (void)sprintf(bufp, "procStarts: %ld\n", afs_printable_int32_ld(tb->procStarts));
751 bufp += strlen(bufp);
752 (void)sprintf(bufp, "lastAnyExit: %ld\n", afs_printable_int32_ld(tb->lastAnyExit));
753 bufp += strlen(bufp);
754 (void)sprintf(bufp, "lastErrorExit: %ld\n", afs_printable_int32_ld(tb->lastErrorExit));
755 bufp += strlen(bufp);
756 (void)sprintf(bufp, "errorCode: %ld\n", afs_printable_int32_ld(tb->errorCode));
757 bufp += strlen(bufp);
758 (void)sprintf(bufp, "errorSignal: %ld\n", afs_printable_int32_ld(tb->errorSignal));
759 bufp += strlen(bufp);
761 (void) sprintf(bufp, "lastErrorName: %s\n", tb->lastErrorName);
762 bufp += strlen(bufp);
764 (void)sprintf(bufp, "goal: %d\n", tb->goal);
765 bufp += strlen(bufp);
766 (void)sprintf(bufp, "END bnode\n");
767 bufp += strlen(bufp);
768 len = (int)(bufp - buffer);
769 if (write(fd, buffer, len) < 0) {
776 hdl_notifier(struct bnode_proc *tp)
778 #ifndef AFS_NT40_ENV /* NT notifier callout not yet implemented */
782 if (stat(tp->bnode->notifier, &tstat)) {
783 bozo_Log("BNODE: Failed to find notifier '%s'; ignored\n",
784 tp->bnode->notifier);
787 if ((pid = fork()) == 0) {
789 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 code = 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 memset(&bnode_stats, 0, sizeof(bnode_stats));
858 LWP_InitializeProcessSupport(1, &junk); /* just in case */
860 code = LWP_CreateProcess(bproc, BNODE_LWP_STACKSIZE,
861 /* priority */ 1, (void *) /* parm */ 0,
862 "bnode-manager", &bproc_pid);
865 memset(&newaction, 0, sizeof(newaction));
866 newaction.sa_handler = bnode_Int;
867 code = sigaction(SIGCHLD, &newaction, NULL);
870 code = sigaction(SIGQUIT, &newaction, NULL);
873 code = sigaction(SIGTERM, &newaction, NULL);
879 /* free token list returned by parseLine */
881 bnode_FreeTokens(struct bnode_token *alist)
883 struct bnode_token *nlist;
884 for (; alist; alist = nlist) {
895 if (x == 0 || x == ' ' || x == '\t' || x == '\n')
902 bnode_ParseLine(char *aline, struct bnode_token **alist)
907 struct bnode_token *first, *last;
908 struct bnode_token *ttok;
911 inToken = 0; /* not copying token chars at start */
912 first = (struct bnode_token *)0;
913 last = (struct bnode_token *)0;
916 if (tc == 0 || space(tc)) { /* terminating null gets us in here, too */
918 inToken = 0; /* end of this token */
921 (struct bnode_token *)malloc(sizeof(struct bnode_token));
922 ttok->next = (struct bnode_token *)0;
923 ttok->key = (char *)malloc(strlen(tbuffer) + 1);
924 strcpy(ttok->key, tbuffer);
934 /* an alpha character */
939 if (tptr - tbuffer >= sizeof(tbuffer))
940 return -1; /* token too long */
944 /* last token flushed 'cause space(0) --> true */
946 last->next = (struct bnode_token *)0;
956 bnode_NewProc(struct bnode *abnode, char *aexecString, char *coreName,
957 struct bnode_proc **aproc)
959 struct bnode_token *tlist, *tt;
961 struct bnode_proc *tp;
963 char *argv[MAXVARGS];
966 code = bnode_ParseLine(aexecString, &tlist); /* try parsing first */
969 tp = (struct bnode_proc *)malloc(sizeof(struct bnode_proc));
970 memset(tp, 0, sizeof(struct bnode_proc));
973 tp->comLine = aexecString;
974 tp->coreName = coreName; /* may be null */
975 abnode->procStartTime = FT_ApproxTime();
976 abnode->procStarts++;
978 /* convert linked list of tokens into argv structure */
979 for (tt = tlist, i = 0; i < (MAXVARGS - 1) && tt; tt = tt->next, i++) {
982 argv[i] = NULL; /* null-terminated */
984 cpid = spawnprocve(argv[0], argv, environ, -1);
985 osi_audit(BOSSpawnProcEvent, 0, AUD_STR, aexecString, AUD_END);
987 if (cpid == (pid_t) - 1) {
988 bozo_Log("Failed to spawn process for bnode '%s'\n", abnode->name);
989 bnode_FreeTokens(tlist);
994 bnode_FreeTokens(tlist);
998 tp->flags = BPROC_STARTED;
999 tp->flags &= ~BPROC_EXITED;
1000 bnode_Check(abnode);
1005 bnode_StopProc(struct bnode_proc *aproc, int asignal)
1008 if (!(aproc->flags & BPROC_STARTED) || (aproc->flags & BPROC_EXITED))
1011 osi_audit(BOSStopProcEvent, 0, AUD_STR, (aproc ? aproc->comLine : NULL),
1014 code = kill(aproc->pid, asignal);
1015 bnode_Check(aproc->bnode);
1020 bnode_Deactivate(struct bnode *abnode)
1022 struct bnode **pb, *tb;
1024 if (!(abnode->flags & BNODE_ACTIVE))
1026 for (pb = &allBnodes, tb = *pb; tb; tb = nb) {
1030 tb->flags &= ~BNODE_ACTIVE;