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>
19 #include <sys/types.h>
26 #ifdef BOZO_SAVE_CORES
39 #include <afs/procmgmt.h> /* signal(), kill(), wait(), etc. */
41 #include <afs/audit.h>
42 #include <afs/afsutil.h>
43 #include <afs/fileutil.h>
46 #if defined(AFS_AIX_ENV) || defined(AFS_SUN4_ENV)
47 /* All known versions of AIX lack WCOREDUMP but this works */
48 #define WCOREDUMP(x) ((x) & 0x80)
51 #define BNODE_LWP_STACKSIZE (16 * 1024)
53 int bnode_waiting = 0;
54 static PROCESS bproc_pid; /* pid of waker-upper */
55 static struct bnode *allBnodes=0; /* list of all bnodes */
56 static struct bnode_proc *allProcs=0; /* list of all processes for which we're waiting */
57 static struct bnode_type *allTypes=0; /* list of registered type handlers */
59 static struct bnode_stats {
63 static afs_int32 SendNotifierData();
64 static int DeleteProc();
67 extern char **environ; /* env structure */
70 /* Remember the name of the process, if any, that failed last */
71 static void RememberProcName(register struct bnode_proc *ap)
73 register 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
89 int bnode_CoreName(register struct bnode *abnode, char *acoreName,
92 strcpy(abuffer, AFSDIR_SERVER_CORELOG_FILEPATH);
94 strcat(abuffer, acoreName);
97 strcat(abuffer, abnode->name);
101 /* save core file, if any */
102 static void SaveCore(register struct bnode *abnode, register struct bnode_proc
107 register afs_int32 code;
108 #ifdef BOZO_SAVE_CORES
109 struct timeval Start;
110 struct tm *TimeFields;
114 code = stat(AFSDIR_SERVER_CORELOG_FILEPATH, &tstat);
117 bnode_CoreName(abnode, aproc->coreName, tbuffer);
118 #ifdef BOZO_SAVE_CORES
119 TM_GetTimeOfDay(&Start, 0);
120 TimeFields = localtime(&Start.tv_sec);
121 sprintf(FileName,"%s.%d%02d%02d%02d%02d%02d", tbuffer,
122 TimeFields->tm_year, TimeFields->tm_mon + 1,
123 TimeFields->tm_mday, TimeFields->tm_hour, TimeFields->tm_min,
125 strcpy(tbuffer,FileName);
127 code = renamefile(AFSDIR_SERVER_CORELOG_FILEPATH, tbuffer);
130 int bnode_GetString(register struct bnode *abnode, register char *abuffer,
131 register afs_int32 alen)
133 return BOP_GETSTRING(abnode, abuffer, alen);
136 int bnode_GetParm(register struct bnode *abnode, register afs_int32 aindex,
137 register char *abuffer, afs_int32 alen)
139 return BOP_GETPARM(abnode, aindex, abuffer, alen);
142 int bnode_GetStat(register struct bnode *abnode, register afs_int32 *astatus)
144 return BOP_GETSTAT(abnode, astatus);
147 int bnode_RestartP(register struct bnode *abnode)
149 return BOP_RESTARTP(abnode);
152 static int bnode_Check(register struct bnode *abnode)
154 if (abnode->flags & BNODE_WAIT) {
155 abnode->flags &= ~BNODE_WAIT;
156 LWP_NoYieldSignal(abnode);
161 /* tell if an instance has a core file */
162 int bnode_HasCore(register struct bnode *abnode)
164 return BOP_HASCORE(abnode);
167 /* wait for all bnodes to stabilize */
168 int bnode_WaitAll() {
169 register struct bnode *tb;
170 register afs_int32 code;
174 for(tb = allBnodes; tb; tb=tb->next) {
176 code = BOP_GETSTAT(tb, &stat);
181 if (stat != tb->goal) {
182 tb->flags |= BNODE_WAIT;
192 /* wait until bnode status is correct */
193 int bnode_WaitStatus(register struct bnode *abnode, int astatus)
195 register afs_int32 code;
201 code = BOP_GETSTAT(abnode, &stat);
202 if (code) return code;
204 /* otherwise, check if we're done */
205 if (stat == astatus) {
206 bnode_Release(abnode);
209 if (astatus != abnode->goal) {
210 bnode_Release(abnode);
211 return -1; /* no longer our goal, don't keep waiting */
213 /* otherwise, block */
214 abnode->flags |= BNODE_WAIT;
215 LWP_WaitProcess(abnode);
219 int bnode_SetStat(register struct bnode *abnode, register int agoal)
221 abnode->goal = agoal;
223 BOP_SETSTAT(abnode, agoal);
224 abnode->flags &= ~BNODE_ERRORSTOP;
228 int bnode_SetGoal(register struct bnode *abnode, register int agoal)
230 abnode->goal = agoal;
235 int bnode_SetFileGoal(register struct bnode *abnode, register int agoal)
237 if (abnode->fileGoal == agoal) return 0; /* already done */
238 abnode->fileGoal = agoal;
243 /* apply a function to all bnodes in the system */
244 int bnode_ApplyInstance(int (*aproc)(), char *arock)
246 register struct bnode *tb, *nb;
247 register afs_int32 code;
249 for(tb = allBnodes; tb; tb=nb) {
251 code = (*aproc) (tb, arock);
252 if (code) return code;
257 struct bnode *bnode_FindInstance(register char *aname)
259 register struct bnode *tb;
261 for(tb=allBnodes;tb;tb=tb->next) {
262 if (!strcmp(tb->name, aname)) return tb;
267 static struct bnode_type *FindType(register char *aname)
269 register struct bnode_type *tt;
271 for(tt=allTypes;tt;tt=tt->next) {
272 if (!strcmp(tt->name, aname)) return tt;
274 return (struct bnode_type *) 0;
277 int bnode_Register(char *atype, struct bnode_ops *aprocs, int anparms)
279 register struct bnode_type *tt;
281 for(tt=allTypes;tt;tt=tt->next) {
282 if (!strcmp(tt->name, atype)) break;
285 tt = (struct bnode_type *) malloc(sizeof(struct bnode_type));
286 memset(tt, 0, sizeof(struct bnode_type));
295 afs_int32 bnode_Create(char *atype, char *ainstance, struct bnode **abp,
296 char *ap1, char *ap2, char *ap3, char *ap4,
297 char *ap5, char *notifier, int fileGoal)
299 struct bnode_type *type;
301 char *notifierpath = NULL;
304 if (bnode_FindInstance(ainstance)) return BZEXISTS;
305 type = FindType(atype);
306 if (!type) return BZBADTYPE;
308 if (notifier && strcmp(notifier, NONOTIFIER)) {
309 /* construct local path from canonical (wire-format) path */
310 if (ConstructLocalBinPath(notifier, ¬ifierpath)) {
311 bozo_Log("BNODE-Create: Notifier program path invalid '%s'\n", notifier);
315 if (stat(notifierpath, &tstat)) {
316 bozo_Log("BNODE-Create: Notifier program '%s' not found\n", notifierpath);
321 tb = (*type->ops->create)(ainstance, ap1, ap2, ap3, ap4, ap5);
326 tb->notifier = notifierpath;
330 /* The fs_create above calls bnode_InitBnode() which always sets the
331 ** fileGoal to BSTAT_NORMAL .... overwrite it with whatever is passed into
332 ** this function as a parameter... */
333 tb->fileGoal = fileGoal;
335 bnode_SetStat(tb, tb->goal); /* nudge it once */
340 int bnode_DeleteName(char *ainstance)
342 register struct bnode *tb;
344 tb = bnode_FindInstance(ainstance);
345 if (!tb) return BZNOENT;
347 return bnode_Delete(tb);
350 int bnode_Hold(register struct bnode *abnode)
356 int bnode_Release(register struct bnode *abnode)
359 if (abnode->refCount == 0 && abnode->flags & BNODE_DELETE) {
360 abnode->flags &= ~BNODE_DELETE; /* we're going for it */
361 bnode_Delete(abnode);
366 int bnode_Delete(register struct bnode *abnode)
368 register afs_int32 code;
369 register struct bnode **lb, *ub;
372 if (abnode->refCount != 0) {
373 abnode->flags |= BNODE_DELETE;
377 /* make sure the bnode is idle before zapping */
379 code = BOP_GETSTAT(abnode, &temp);
380 bnode_Release(abnode);
381 if (code) return code;
382 if (temp != BSTAT_SHUTDOWN) return BZBUSY;
384 /* all clear to zap */
385 for(lb = &allBnodes, ub = *lb; ub; lb= &ub->next, ub = *lb) {
387 /* unthread it from the list */
392 free(abnode->name); /* do this first, since bnode fields may be bad after BOP_DELETE */
393 code = BOP_DELETE(abnode); /* don't play games like holding over this one */
398 /* function to tell if there's a timeout coming up */
399 int bnode_PendingTimeout(register struct bnode *abnode)
401 return (abnode->flags & BNODE_NEEDTIMEOUT);
404 /* function called to set / clear periodic bnode wakeup times */
405 int bnode_SetTimeout(register struct bnode *abnode, afs_int32 atimeout)
408 abnode->nextTimeout = FT_ApproxTime() + atimeout;
409 abnode->flags |= BNODE_NEEDTIMEOUT;
410 abnode->period = atimeout;
411 IOMGR_Cancel(bproc_pid);
414 abnode->flags &= ~BNODE_NEEDTIMEOUT;
419 /* used by new bnode creation code to format bnode header */
420 int bnode_InitBnode (register struct bnode *abnode,
421 struct bnode_ops *abnodeops, char *aname)
423 struct bnode **lb, *nb;
425 /* format the bnode properly */
426 memset(abnode, 0, sizeof(struct bnode));
427 abnode->ops = abnodeops;
428 abnode->name = (char *) malloc(strlen(aname)+1);
431 strcpy(abnode->name, aname);
432 abnode->flags = BNODE_ACTIVE;
433 abnode->fileGoal = BSTAT_NORMAL;
434 abnode->goal = BSTAT_SHUTDOWN;
436 /* put the bnode at the end of the list so we write bnode file in same order */
437 for(lb = &allBnodes, nb = *lb; nb; lb = &nb->next, nb = *lb);
443 /* bnode lwp executes this code repeatedly */
445 register afs_int32 code;
446 register struct bnode *tb;
447 register afs_int32 temp;
448 register struct bnode_proc *tp;
450 int options; /* must not be register */
456 /* first figure out how long to sleep for */
457 temp = 0x7fffffff; /* afs_int32 time; maxint doesn't work in select */
459 for(tb = allBnodes; tb; tb=tb->next) {
460 if (tb->flags & BNODE_NEEDTIMEOUT) {
461 if (tb->nextTimeout < temp) {
463 temp = tb->nextTimeout;
467 /* now temp has the time at which we should wakeup next */
470 if (setAny) temp -= FT_ApproxTime(); /* how many seconds until next event */
475 code = IOMGR_Select(0, 0, 0, 0, &tv);
477 else code = 0; /* fake timeout code */
479 /* figure out why we woke up; child exit or timeouts */
480 FT_GetTimeOfDay(&tv, 0); /* must do the real gettimeofday once and a while */
483 /* check all bnodes to see which ones need timeout events */
484 for(tb = allBnodes; tb; tb=nb) {
485 if ((tb->flags & BNODE_NEEDTIMEOUT) && temp > tb->nextTimeout) {
489 if (tb->flags & BNODE_NEEDTIMEOUT) { /* check again, BOP_TIMEOUT could change */
490 tb->nextTimeout = FT_ApproxTime() + tb->period;
493 bnode_Release(tb); /* delete may occur here */
499 /* signalled, probably by incoming signal */
502 bnode_waiting = options | 0x800000;
503 code = waitpid((pid_t)-1, &status, options);
505 if (code == 0 || code == -1) break; /* all done */
506 /* otherwise code has a process id, which we now search for */
507 for(tp=allProcs; tp; tp=tp->next)
508 if (tp->pid == code) break;
514 /* count restarts in last 10 seconds */
515 if (temp > tb->rsTime + 30) {
516 /* it's been 10 seconds we've been counting */
521 if (WIFSIGNALED(status) == 0) {
522 /* exited, not signalled */
523 tp->lastExit = WEXITSTATUS(status);
526 tb->errorCode = tp->lastExit;
527 tb->lastErrorExit = FT_ApproxTime();
528 RememberProcName(tp);
532 bozo_Log("%s:%s exited with code %d\n",
533 tb->name, tp->coreName, tp->lastExit);
535 bozo_Log("%s exited with code %d\n",
536 tb->name, tp->lastExit);
539 /* Signal occurred, perhaps spurious due to shutdown request.
540 * If due to a shutdown request, don't overwrite last error
543 tp->lastSignal = WTERMSIG(status);
545 if (tp->lastSignal != SIGQUIT && tp->lastSignal != SIGTERM
546 && tp->lastSignal != SIGKILL) {
547 tb->errorSignal = tp->lastSignal;
548 tb->lastErrorExit = FT_ApproxTime();
549 RememberProcName(tp);
552 bozo_Log("%s:%s exited on signal %d%s\n",
553 tb->name, tp->coreName, tp->lastSignal,
554 WCOREDUMP(status) ? " (core dumped)" : "");
556 bozo_Log("%s exited on signal %d%s\n",
557 tb->name, tp->lastSignal,
558 WCOREDUMP(status) ? " (core dumped)" : "");
561 tb->lastAnyExit = FT_ApproxTime();
564 bozo_Log("BNODE: Notifier %s will be called\n", tb->notifier);
567 BOP_PROCEXIT(tb, tp);
570 if (tb->rsCount++ > 10) {
571 /* 10 in 10 seconds */
572 tb->flags |= BNODE_ERRORSTOP;
573 bnode_SetGoal(tb, BSTAT_SHUTDOWN);
574 bozo_Log("BNODE '%s' repeatedly failed to start, perhaps missing executable.\n",
577 bnode_Release(tb); /* bnode delete can happen here */
580 else bnode_stats.weirdPids++;
587 int hdl_notifier(struct bnode_proc *tp)
589 #ifndef AFS_NT40_ENV /* NT notifier callout not yet implemented */
590 int code, pid, status;
593 if (stat(tp->bnode->notifier, &tstat)) {
594 bozo_Log("BNODE: Failed to find notifier '%s'; ignored\n", tp->bnode->notifier);
597 if ((pid = fork()) == 0) {
599 struct bnode *tb = tp->bnode;
602 #if defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_SGI51_ENV)
605 #ifdef AFS_LINUX20_ENV
611 fout = popen(tb->notifier, "w");
613 bozo_Log("BNODE: Failed to find notifier '%s'; ignored\n", tb->notifier);
614 perror(tb->notifier);
617 code = SendNotifierData(fileno(fout), tp);
620 } else if (pid < 0) {
621 bozo_Log("Failed to fork creating process to handle notifier '%s'\n", tp->bnode->notifier);
624 #endif /* AFS_NT40_ENV */
629 static afs_int32 SendNotifierData(register int fd,
630 register struct bnode_proc *tp)
632 register struct bnode *tb = tp->bnode;
633 char buffer[1000], *bufp = buffer, *buf1;
637 * First sent out the bnode_proc struct
639 (void) sprintf(bufp, "BEGIN bnode_proc\n");
640 bufp += strlen(bufp);
641 (void) sprintf(bufp, "comLine: %s\n", tp->comLine);
642 bufp += strlen(bufp);
643 if (!(buf1 = tp->coreName))
645 (void) sprintf(bufp, "coreName: %s\n", buf1);
646 bufp += strlen(bufp);
647 (void) sprintf(bufp, "pid: %ld\n", tp->pid);
648 bufp += strlen(bufp);
649 (void) sprintf(bufp, "lastExit: %ld\n", tp->lastExit);
650 bufp += strlen(bufp);
652 (void) sprintf(bufp, "lastSignal: %ld\n", tp->lastSignal);
653 bufp += strlen(bufp);
655 (void) sprintf(bufp, "flags: %ld\n", tp->flags);
656 bufp += strlen(bufp);
657 (void) sprintf(bufp, "END bnode_proc\n");
658 bufp += strlen(bufp);
659 len =(int)(bufp-buffer);
660 if (write(fd, buffer, len) < 0) {
665 * Now sent out the bnode struct
668 (void) sprintf(bufp, "BEGIN bnode\n");
669 bufp += strlen(bufp);
670 (void) sprintf(bufp, "name: %s\n", tb->name);
671 bufp += strlen(bufp);
672 (void) sprintf(bufp, "rsTime: %ld\n", tb->rsTime);
673 bufp += strlen(bufp);
674 (void) sprintf(bufp, "rsCount: %ld\n", tb->rsCount);
675 bufp += strlen(bufp);
676 (void) sprintf(bufp, "procStartTime: %ld\n", tb->procStartTime);
677 bufp += strlen(bufp);
678 (void) sprintf(bufp, "procStarts: %ld\n", tb->procStarts);
679 bufp += strlen(bufp);
680 (void) sprintf(bufp, "lastAnyExit: %ld\n", tb->lastAnyExit);
681 bufp += strlen(bufp);
682 (void) sprintf(bufp, "lastErrorExit: %ld\n", tb->lastErrorExit);
683 bufp += strlen(bufp);
684 (void) sprintf(bufp, "errorCode: %ld\n", tb->errorCode);
685 bufp += strlen(bufp);
686 (void) sprintf(bufp, "errorSignal: %ld\n", tb->errorSignal);
687 bufp += strlen(bufp);
689 (void) sprintf(bufp, "lastErrorName: %s\n", tb->lastErrorName);
690 bufp += strlen(bufp);
692 (void) sprintf(bufp, "goal: %d\n", tb->goal);
693 bufp += strlen(bufp);
694 (void) sprintf(bufp, "END bnode\n");
695 bufp += strlen(bufp);
696 len = (int)bufp-(int)buffer;
697 if (write(fd, buffer, len) < 0) {
704 /* Called by IOMGR at low priority on IOMGR's stack shortly after a SIGCHLD
705 * occurs. Wakes up bproc do redo things */
706 int bnode_SoftInt(int asignal)
708 IOMGR_Cancel(bproc_pid);
712 /* Called at signal interrupt level; queues function to be called
713 * when IOMGR runs again.
715 void bnode_Int(int asignal)
717 extern void bozo_ShutdownAndExit();
719 if (asignal == SIGQUIT) {
720 IOMGR_SoftSig(bozo_ShutdownAndExit, (char *)asignal);
722 IOMGR_SoftSig(bnode_SoftInt, (char *)asignal);
727 /* intialize the whole system */
730 register afs_int32 code;
731 struct sigaction newaction;
734 if (initDone) return 0;
736 memset(&bnode_stats, 0, sizeof(bnode_stats));
737 LWP_InitializeProcessSupport(1, &junk); /* just in case */
739 code = LWP_CreateProcess(bproc, BNODE_LWP_STACKSIZE,
740 /* priority */ 1, (void *) /* parm */0,
741 "bnode-manager", &bproc_pid);
742 if (code) return code;
743 memset((char *)&newaction, 0, sizeof(newaction));
744 newaction.sa_handler = bnode_Int;
745 code = sigaction(SIGCHLD, &newaction, NULL);
746 if (code) return errno;
747 code = sigaction(SIGQUIT, &newaction, NULL);
748 if (code) return errno;
752 /* free token list returned by parseLine */
753 int bnode_FreeTokens(register struct bnode_token *alist)
755 register struct bnode_token *nlist;
756 for(; alist; alist = nlist) {
764 static int space(int x)
766 if (x == 0 || x == ' ' || x == '\t' || x== '\n') return 1;
770 int bnode_ParseLine(char *aline, struct bnode_token **alist)
775 struct bnode_token *first, *last;
776 register struct bnode_token *ttok;
779 inToken = 0; /* not copying token chars at start */
780 first = (struct bnode_token *) 0;
781 last = (struct bnode_token *) 0;
784 if (tc == 0 || space(tc)) { /* terminating null gets us in here, too */
786 inToken = 0; /* end of this token */
788 ttok = (struct bnode_token *) malloc(sizeof(struct bnode_token));
789 ttok->next = (struct bnode_token *) 0;
790 ttok->key = (char *) malloc(strlen(tbuffer)+1);
791 strcpy(ttok->key, tbuffer);
797 if (!first) first = ttok;
801 /* an alpha character */
806 if (tptr - tbuffer >= sizeof(tbuffer)) return -1; /* token too long */
810 /* last token flushed 'cause space(0) --> true */
811 if (last) last->next = (struct bnode_token *) 0;
819 int bnode_NewProc(struct bnode *abnode, char *aexecString, char *coreName,
820 struct bnode_proc **aproc)
822 struct bnode_token *tlist, *tt;
824 struct bnode_proc *tp;
826 char *argv[MAXVARGS];
829 code = bnode_ParseLine(aexecString, &tlist); /* try parsing first */
830 if (code) return code;
831 tp = (struct bnode_proc *) malloc(sizeof(struct bnode_proc));
832 memset(tp, 0, sizeof(struct bnode_proc));
837 tp->comLine = aexecString;
838 tp->coreName = coreName; /* may be null */
839 abnode->procStartTime = FT_ApproxTime();
840 abnode->procStarts++;
842 /* convert linked list of tokens into argv structure */
843 for (tt = tlist, i = 0; i < (MAXVARGS - 1) && tt; tt = tt->next, i++) {
846 argv[i] = NULL; /* null-terminated */
848 cpid = spawnprocve(argv[0], argv, environ, -1);
849 osi_audit(BOSSpawnProcEvent, 0, AUD_STR, aexecString, AUD_END );
851 if (cpid == (pid_t)-1) {
852 bozo_Log("Failed to spawn process for bnode '%s'\n", abnode->name);
853 bnode_FreeTokens(tlist);
858 bnode_FreeTokens(tlist);
860 tp->flags = BPROC_STARTED;
861 tp->flags &= ~BPROC_EXITED;
866 int bnode_StopProc(register struct bnode_proc *aproc, int asignal)
869 if (!(aproc->flags & BPROC_STARTED) || (aproc->flags & BPROC_EXITED))
872 osi_audit( BOSStopProcEvent, 0, AUD_STR, (aproc ? aproc->comLine : NULL), AUD_END );
874 code = kill(aproc->pid, asignal);
875 bnode_Check(aproc->bnode);
879 int bnode_Deactivate(register struct bnode *abnode)
881 register struct bnode **pb, *tb;
883 if (!(abnode->flags & BNODE_ACTIVE)) return BZNOTACTIVE;
884 for(pb = &allBnodes,tb = *pb; tb; tb=nb) {
888 tb->flags &= ~BNODE_ACTIVE;
895 static int DeleteProc(register struct bnode_proc *abproc)
897 register struct bnode_proc **pb, *tb;
898 struct bnode_proc *nb;
900 for(pb = &allProcs,tb = *pb; tb; pb = &tb->next, tb=nb) {