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 <afs/param.h>
15 #include <sys/types.h>
22 #ifdef BOZO_SAVE_CORES
26 #include <afs/procmgmt.h> /* signal(), kill(), wait(), etc. */
28 #include <afs/audit.h>
29 #include <afs/afsutil.h>
30 #include <afs/fileutil.h>
33 #define BNODE_LWP_STACKSIZE (16 * 1024)
35 int bnode_waiting = 0;
36 static PROCESS bproc_pid; /* pid of waker-upper */
37 static struct bnode *allBnodes=0; /* list of all bnodes */
38 static struct bnode_proc *allProcs=0; /* list of all processes for which we're waiting */
39 static struct bnode_type *allTypes=0; /* list of registered type handlers */
41 static struct bnode_stats {
45 static afs_int32 SendNotifierData();
46 static int DeleteProc();
49 extern char **environ; /* env structure */
52 /* Remember the name of the process, if any, that failed last */
53 static void RememberProcName(ap)
54 register struct bnode_proc *ap; {
55 register struct bnode *tbnodep;
58 if (tbnodep->lastErrorName) {
59 free(tbnodep->lastErrorName);
60 tbnodep->lastErrorName = (char *) 0;
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
71 bnode_CoreName(abnode, acoreName, abuffer)
72 register struct bnode *abnode;
75 strcpy(abuffer, AFSDIR_SERVER_CORELOG_FILEPATH);
77 strcat(abuffer, acoreName);
80 strcat(abuffer, abnode->name);
84 /* save core file, if any */
85 static void SaveCore(abnode, aproc)
86 register struct bnode_proc *aproc;
87 register struct bnode *abnode; {
90 register afs_int32 code;
91 #ifdef BOZO_SAVE_CORES
93 struct tm *TimeFields;
97 code = stat(AFSDIR_SERVER_CORELOG_FILEPATH, &tstat);
100 bnode_CoreName(abnode, aproc->coreName, tbuffer);
101 #ifdef BOZO_SAVE_CORES
102 TM_GetTimeOfDay(&Start, 0);
103 TimeFields = localtime(&Start.tv_sec);
104 sprintf(FileName,"%s.%d%02d%02d%02d%02d%02d", tbuffer,
105 TimeFields->tm_year, TimeFields->tm_mon + 1,
106 TimeFields->tm_mday, TimeFields->tm_hour, TimeFields->tm_min,
108 strcpy(tbuffer,FileName);
110 code = renamefile(AFSDIR_SERVER_CORELOG_FILEPATH, tbuffer);
113 bnode_GetString(abnode, abuffer, alen)
114 register struct bnode *abnode;
115 register char *abuffer;
116 register afs_int32 alen;{
117 return BOP_GETSTRING(abnode, abuffer, alen);
120 bnode_GetParm(abnode, aindex, abuffer, alen)
121 register struct bnode *abnode;
122 register afs_int32 aindex;
123 register char *abuffer;
125 return BOP_GETPARM(abnode, aindex, abuffer, alen);
128 bnode_GetStat(abnode, astatus)
129 register struct bnode *abnode;
130 register afs_int32 *astatus; {
131 return BOP_GETSTAT(abnode, astatus);
134 bnode_RestartP(abnode)
135 register struct bnode *abnode; {
136 return BOP_RESTARTP(abnode);
139 static bnode_Check(abnode)
140 register struct bnode *abnode; {
141 if (abnode->flags & BNODE_WAIT) {
142 abnode->flags &= ~BNODE_WAIT;
143 LWP_NoYieldSignal(abnode);
148 /* tell if an instance has a core file */
149 bnode_HasCore(abnode)
150 register struct bnode *abnode; {
151 return BOP_HASCORE(abnode);
154 /* wait for all bnodes to stabilize */
156 register struct bnode *tb;
157 register afs_int32 code;
161 for(tb = allBnodes; tb; tb=tb->next) {
163 code = BOP_GETSTAT(tb, &stat);
168 if (stat != tb->goal) {
169 tb->flags |= BNODE_WAIT;
179 /* wait until bnode status is correct */
180 bnode_WaitStatus(abnode, astatus)
182 register struct bnode *abnode; {
183 register afs_int32 code;
189 code = BOP_GETSTAT(abnode, &stat);
190 if (code) return code;
192 /* otherwise, check if we're done */
193 if (stat == astatus) {
194 bnode_Release(abnode);
197 if (astatus != abnode->goal) {
198 bnode_Release(abnode);
199 return -1; /* no longer our goal, don't keep waiting */
201 /* otherwise, block */
202 abnode->flags |= BNODE_WAIT;
203 LWP_WaitProcess(abnode);
207 bnode_SetStat(abnode, agoal)
208 register struct bnode *abnode;
209 register int agoal; {
210 abnode->goal = agoal;
212 BOP_SETSTAT(abnode, agoal);
213 abnode->flags &= ~BNODE_ERRORSTOP;
217 bnode_SetGoal(abnode, agoal)
218 register struct bnode *abnode;
219 register int agoal; {
220 abnode->goal = agoal;
225 bnode_SetFileGoal(abnode, agoal)
226 register struct bnode *abnode;
227 register int agoal; {
228 if (abnode->fileGoal == agoal) return 0; /* already done */
229 abnode->fileGoal = agoal;
234 /* apply a function to all bnodes in the system */
235 int bnode_ApplyInstance(aproc, arock)
238 register struct bnode *tb, *nb;
239 register afs_int32 code;
241 for(tb = allBnodes; tb; tb=nb) {
243 code = (*aproc) (tb, arock);
244 if (code) return code;
249 struct bnode *bnode_FindInstance (aname)
250 register char *aname; {
251 register struct bnode *tb;
253 for(tb=allBnodes;tb;tb=tb->next) {
254 if (!strcmp(tb->name, aname)) return tb;
256 return (struct bnode *) 0;
259 static struct bnode_type *FindType(aname)
260 register char *aname; {
261 register struct bnode_type *tt;
263 for(tt=allTypes;tt;tt=tt->next) {
264 if (!strcmp(tt->name, aname)) return tt;
266 return (struct bnode_type *) 0;
269 bnode_Register(atype, aprocs, anparms)
271 int anparms; /* number of parms to create */
272 struct bnode_ops *aprocs; {
273 register struct bnode_type *tt;
275 for(tt=allTypes;tt;tt=tt->next) {
276 if (!strcmp(tt->name, atype)) break;
279 tt = (struct bnode_type *) malloc(sizeof(struct bnode_type));
280 bzero(tt, sizeof(struct bnode_type));
289 afs_int32 bnode_Create(atype, ainstance, abp, ap1, ap2, ap3, ap4, ap5, notifier,fileGoal)
293 char *ap1, *ap2, *ap3, *ap4, *ap5, *notifier;
295 struct bnode_type *type;
297 char *notifierpath = NULL;
300 if (bnode_FindInstance(ainstance)) return BZEXISTS;
301 type = FindType(atype);
302 if (!type) return BZBADTYPE;
304 if (notifier && strcmp(notifier, NONOTIFIER)) {
305 /* construct local path from canonical (wire-format) path */
306 if (ConstructLocalBinPath(notifier, ¬ifierpath)) {
307 bozo_Log("BNODE-Create: Notifier program path invalid '%s'\n", notifier);
311 if (stat(notifierpath, &tstat)) {
312 bozo_Log("BNODE-Create: Notifier program '%s' not found\n", notifierpath);
317 tb = (*type->ops->create)(ainstance, ap1, ap2, ap3, ap4, ap5);
322 tb->notifier = notifierpath;
326 /* The fs_create above calls bnode_InitBnode() which always sets the
327 ** fileGoal to BSTAT_NORMAL .... overwrite it with whatever is passed into
328 ** this function as a parameter... */
329 tb->fileGoal = fileGoal;
331 bnode_SetStat(tb, tb->goal); /* nudge it once */
336 int bnode_DeleteName(ainstance)
338 register struct bnode *tb;
340 tb = bnode_FindInstance(ainstance);
341 if (!tb) return BZNOENT;
343 return bnode_Delete(tb);
347 register struct bnode *abnode; {
352 bnode_Release(abnode)
353 register struct bnode *abnode; {
355 if (abnode->refCount == 0 && abnode->flags & BNODE_DELETE) {
356 abnode->flags &= ~BNODE_DELETE; /* we're going for it */
357 bnode_Delete(abnode);
362 int bnode_Delete(abnode)
363 register struct bnode *abnode; {
364 register afs_int32 code;
365 register struct bnode **lb, *ub;
368 if (abnode->refCount != 0) {
369 abnode->flags |= BNODE_DELETE;
373 /* make sure the bnode is idle before zapping */
375 code = BOP_GETSTAT(abnode, &temp);
376 bnode_Release(abnode);
377 if (code) return code;
378 if (temp != BSTAT_SHUTDOWN) return BZBUSY;
380 /* all clear to zap */
381 for(lb = &allBnodes, ub = *lb; ub; lb= &ub->next, ub = *lb) {
383 /* unthread it from the list */
388 free(abnode->name); /* do this first, since bnode fields may be bad after BOP_DELETE */
389 code = BOP_DELETE(abnode); /* don't play games like holding over this one */
394 /* function to tell if there's a timeout coming up */
395 int bnode_PendingTimeout(abnode)
396 register struct bnode *abnode; {
397 return (abnode->flags & BNODE_NEEDTIMEOUT);
400 /* function called to set / clear periodic bnode wakeup times */
401 int bnode_SetTimeout(abnode, atimeout)
402 register struct bnode *abnode;
403 afs_int32 atimeout; {
405 abnode->nextTimeout = FT_ApproxTime() + atimeout;
406 abnode->flags |= BNODE_NEEDTIMEOUT;
407 abnode->period = atimeout;
408 IOMGR_Cancel(bproc_pid);
411 abnode->flags &= ~BNODE_NEEDTIMEOUT;
416 /* used by new bnode creation code to format bnode header */
417 int bnode_InitBnode (abnode, abnodeops, aname)
418 register struct bnode *abnode;
420 struct bnode_ops *abnodeops; {
421 struct bnode **lb, *nb;
423 /* format the bnode properly */
424 bzero(abnode, sizeof(struct bnode));
425 abnode->ops = abnodeops;
426 abnode->name = (char *) malloc(strlen(aname)+1);
427 strcpy(abnode->name, aname);
428 abnode->flags = BNODE_ACTIVE;
429 abnode->fileGoal = BSTAT_NORMAL;
430 abnode->goal = BSTAT_SHUTDOWN;
432 /* put the bnode at the end of the list so we write bnode file in same order */
433 for(lb = &allBnodes, nb = *lb; nb; lb = &nb->next, nb = *lb);
439 /* bnode lwp executes this code repeatedly */
441 register afs_int32 code;
442 register struct bnode *tb;
443 register afs_int32 temp;
444 register struct bnode_proc *tp;
446 int options; /* must not be register */
452 /* first figure out how long to sleep for */
453 temp = 0x7fffffff; /* afs_int32 time; maxint doesn't work in select */
455 for(tb = allBnodes; tb; tb=tb->next) {
456 if (tb->flags & BNODE_NEEDTIMEOUT) {
457 if (tb->nextTimeout < temp) {
459 temp = tb->nextTimeout;
463 /* now temp has the time at which we should wakeup next */
466 if (setAny) temp -= FT_ApproxTime(); /* how many seconds until next event */
471 code = IOMGR_Select(0, 0, 0, 0, &tv);
473 else code = 0; /* fake timeout code */
475 /* figure out why we woke up; child exit or timeouts */
476 FT_GetTimeOfDay(&tv, 0); /* must do the real gettimeofday once and a while */
479 /* check all bnodes to see which ones need timeout events */
480 for(tb = allBnodes; tb; tb=nb) {
481 if ((tb->flags & BNODE_NEEDTIMEOUT) && temp > tb->nextTimeout) {
485 if (tb->flags & BNODE_NEEDTIMEOUT) { /* check again, BOP_TIMEOUT could change */
486 tb->nextTimeout = FT_ApproxTime() + tb->period;
489 bnode_Release(tb); /* delete may occur here */
495 /* signalled, probably by incoming signal */
498 bnode_waiting = options | 0x800000;
499 code = waitpid((pid_t)-1, &status, options);
501 if (code == 0 || code == -1) break; /* all done */
502 /* otherwise code has a process id, which we now search for */
503 for(tp=allProcs; tp; tp=tp->next)
504 if (tp->pid == code) break;
510 /* count restarts in last 10 seconds */
511 if (temp > tb->rsTime + 30) {
512 /* it's been 10 seconds we've been counting */
517 if (WIFSIGNALED(status) == 0) {
518 /* exited, not signalled */
519 tp->lastExit = WEXITSTATUS(status);
522 tb->errorCode = tp->lastExit;
523 tb->lastErrorExit = FT_ApproxTime();
524 RememberProcName(tp);
529 /* Signal occurred, perhaps spurious due to shutdown request.
530 * If due to a shutdown request, don't overwrite last error
533 tp->lastSignal = WTERMSIG(status);
535 if (tp->lastSignal != SIGQUIT && tp->lastSignal != SIGTERM
536 && tp->lastSignal != SIGKILL) {
537 tb->errorSignal = tp->lastSignal;
538 tb->lastErrorExit = FT_ApproxTime();
539 RememberProcName(tp);
543 tb->lastAnyExit = FT_ApproxTime();
546 bozo_Log("BNODE: Notifier %s will be called\n", tb->notifier);
549 BOP_PROCEXIT(tb, tp);
552 if (tb->rsCount++ > 10) {
553 /* 10 in 10 seconds */
554 tb->flags |= BNODE_ERRORSTOP;
555 bnode_SetGoal(tb, BSTAT_SHUTDOWN);
556 bozo_Log("BNODE '%s' repeatedly failed to start, perhaps missing executable.\n",
559 bnode_Release(tb); /* bnode delete can happen here */
562 else bnode_stats.weirdPids++;
572 signal(SIGPIPE, SIG_IGN);
573 bozo_Log("Notifier aborted prematurely");
581 struct bnode_proc *tp;
583 #ifndef AFS_NT40_ENV /* NT notifier callout not yet implemented */
584 int code, pid, status;
587 if (stat(tp->bnode->notifier, &tstat)) {
588 bozo_Log("BNODE: Failed to find notifier '%s'; ignored\n", tp->bnode->notifier);
591 if ((pid = fork()) == 0) {
593 struct bnode *tb = tp->bnode;
596 #if defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_SGI51_ENV)
599 #ifdef AFS_LINUX20_ENV
605 fout = popen(tb->notifier, "w");
607 bozo_Log("BNODE: Failed to find notifier '%s'; ignored\n", tb->notifier);
608 perror(tb->notifier);
611 code = SendNotifierData(fileno(fout), tp);
614 } else if (pid < 0) {
615 bozo_Log("Failed to fork creating process to handle notifier '%s'\n", tp->bnode->notifier);
618 #endif /* AFS_NT40_ENV */
623 static afs_int32 SendNotifierData(fd, tp)
625 register struct bnode_proc *tp;
627 register struct bnode *tb = tp->bnode;
628 char buffer[1000], *bufp = buffer, *buf1;
632 * First sent out the bnode_proc struct
634 (void) sprintf(bufp, "BEGIN bnode_proc\n");
635 bufp += strlen(bufp);
636 (void) sprintf(bufp, "comLine: %s\n", tp->comLine);
637 bufp += strlen(bufp);
638 if (!(buf1 = tp->coreName))
640 (void) sprintf(bufp, "coreName: %s\n", buf1);
641 bufp += strlen(bufp);
642 (void) sprintf(bufp, "pid: %ld\n", tp->pid);
643 bufp += strlen(bufp);
644 (void) sprintf(bufp, "lastExit: %ld\n", tp->lastExit);
645 bufp += strlen(bufp);
647 (void) sprintf(bufp, "lastSignal: %ld\n", tp->lastSignal);
648 bufp += strlen(bufp);
650 (void) sprintf(bufp, "flags: %ld\n", tp->flags);
651 bufp += strlen(bufp);
652 (void) sprintf(bufp, "END bnode_proc\n");
653 bufp += strlen(bufp);
654 len =(int)(bufp-buffer);
655 if (write(fd, buffer, len) < 0) {
660 * Now sent out the bnode struct
663 (void) sprintf(bufp, "BEGIN bnode\n");
664 bufp += strlen(bufp);
665 (void) sprintf(bufp, "name: %s\n", tb->name);
666 bufp += strlen(bufp);
667 (void) sprintf(bufp, "rsTime: %ld\n", tb->rsTime);
668 bufp += strlen(bufp);
669 (void) sprintf(bufp, "rsCount: %ld\n", tb->rsCount);
670 bufp += strlen(bufp);
671 (void) sprintf(bufp, "procStartTime: %ld\n", tb->procStartTime);
672 bufp += strlen(bufp);
673 (void) sprintf(bufp, "procStarts: %ld\n", tb->procStarts);
674 bufp += strlen(bufp);
675 (void) sprintf(bufp, "lastAnyExit: %ld\n", tb->lastAnyExit);
676 bufp += strlen(bufp);
677 (void) sprintf(bufp, "lastErrorExit: %ld\n", tb->lastErrorExit);
678 bufp += strlen(bufp);
679 (void) sprintf(bufp, "errorCode: %ld\n", tb->errorCode);
680 bufp += strlen(bufp);
681 (void) sprintf(bufp, "errorSignal: %ld\n", tb->errorSignal);
682 bufp += strlen(bufp);
684 (void) sprintf(bufp, "lastErrorName: %s\n", tb->lastErrorName);
685 bufp += strlen(bufp);
687 (void) sprintf(bufp, "goal: %d\n", tb->goal);
688 bufp += strlen(bufp);
689 (void) sprintf(bufp, "END bnode\n");
690 bufp += strlen(bufp);
691 len = (int)bufp-(int)buffer;
692 if (write(fd, buffer, len) < 0) {
699 /* Called by IOMGR at low priority on IOMGR's stack shortly after a SIGCHLD
700 * occurs. Wakes up bproc do redo things */
701 bnode_SoftInt(int asignal)
703 IOMGR_Cancel(bproc_pid);
707 /* Called at signal interrupt level; queues function to be called
708 * when IOMGR runs again.
711 bnode_Int(int asignal)
713 extern void bozo_ShutdownAndExit();
715 if (asignal == SIGQUIT) {
716 IOMGR_SoftSig(bozo_ShutdownAndExit, asignal);
718 IOMGR_SoftSig(bnode_SoftInt, asignal);
723 /* intialize the whole system */
726 register afs_int32 code;
727 struct sigaction newaction;
730 if (initDone) return 0;
732 bzero(&bnode_stats, sizeof(bnode_stats));
733 LWP_InitializeProcessSupport(1, &junk); /* just in case */
735 code = LWP_CreateProcess(bproc, BNODE_LWP_STACKSIZE,
736 /* priority */ 1, /* parm */0, "bnode-manager", &bproc_pid);
737 if (code) return code;
738 bzero((char *)&newaction, sizeof(newaction));
739 newaction.sa_handler = bnode_Int;
740 code = sigaction(SIGCHLD, &newaction, NULL);
741 if (code) return errno;
742 code = sigaction(SIGQUIT, &newaction, NULL);
743 if (code) return errno;
747 /* free token list returned by parseLine */
748 bnode_FreeTokens(alist)
749 register struct bnode_token *alist; {
750 register struct bnode_token *nlist;
751 for(; alist; alist = nlist) {
761 if (x == 0 || x == ' ' || x == '\t' || x== '\n') return 1;
765 bnode_ParseLine(aline, alist)
767 struct bnode_token **alist; {
771 struct bnode_token *first, *last;
772 register struct bnode_token *ttok;
775 inToken = 0; /* not copying token chars at start */
776 first = (struct bnode_token *) 0;
777 last = (struct bnode_token *) 0;
780 if (tc == 0 || space(tc)) { /* terminating null gets us in here, too */
782 inToken = 0; /* end of this token */
784 ttok = (struct bnode_token *) malloc(sizeof(struct bnode_token));
785 ttok->next = (struct bnode_token *) 0;
786 ttok->key = (char *) malloc(strlen(tbuffer)+1);
787 strcpy(ttok->key, tbuffer);
793 if (!first) first = ttok;
797 /* an alpha character */
802 if (tptr - tbuffer >= sizeof(tbuffer)) return -1; /* token too long */
806 /* last token flushed 'cause space(0) --> true */
807 if (last) last->next = (struct bnode_token *) 0;
815 int bnode_NewProc(abnode, aexecString, coreName, aproc)
816 struct bnode_proc **aproc;
818 struct bnode *abnode;
820 struct bnode_token *tlist, *tt;
822 struct bnode_proc *tp;
824 char *argv[MAXVARGS];
827 code = bnode_ParseLine(aexecString, &tlist); /* try parsing first */
828 if (code) return code;
829 tp = (struct bnode_proc *) malloc(sizeof(struct bnode_proc));
830 bzero(tp, sizeof(struct bnode_proc));
835 tp->comLine = aexecString;
836 tp->coreName = coreName; /* may be null */
837 abnode->procStartTime = FT_ApproxTime();
838 abnode->procStarts++;
840 /* convert linked list of tokens into argv structure */
841 for (tt = tlist, i = 0; i < (MAXVARGS - 1) && tt; tt = tt->next, i++) {
844 argv[i] = (char *) 0; /* null-terminated */
846 cpid = spawnprocve(argv[0], argv, environ, -1);
847 osi_audit(BOSSpawnProcEvent, 0, AUD_STR, aexecString, AUD_END );
849 if (cpid == (pid_t)-1) {
850 bozo_Log("Failed to spawn process for bnode '%s'\n", abnode->name);
851 bnode_FreeTokens(tlist);
856 bnode_FreeTokens(tlist);
858 tp->flags = BPROC_STARTED;
859 tp->flags &= ~BPROC_EXITED;
864 int bnode_StopProc(aproc, asignal)
865 register struct bnode_proc *aproc;
868 if (!(aproc->flags & BPROC_STARTED) || (aproc->flags & BPROC_EXITED))
871 osi_audit( BOSStopProcEvent, 0, AUD_STR, (aproc ? aproc->comLine : (char *)0), AUD_END );
873 code = kill(aproc->pid, asignal);
874 bnode_Check(aproc->bnode);
878 int bnode_Deactivate(abnode)
879 register struct bnode *abnode; {
880 register struct bnode **pb, *tb;
882 if (!(abnode->flags & BNODE_ACTIVE)) return BZNOTACTIVE;
883 for(pb = &allBnodes,tb = *pb; tb; tb=nb) {
887 tb->flags &= ~BNODE_ACTIVE;
894 static int DeleteProc(abproc)
895 register struct bnode_proc *abproc; {
896 register struct bnode_proc **pb, *tb;
897 struct bnode_proc *nb;
899 for(pb = &allProcs,tb = *pb; tb; pb = &tb->next, tb=nb) {