2 * (C) COPYRIGHT IBM CORPORATION 1987, 1988
3 * LICENSED MATERIALS - PROPERTY OF IBM
11 #include <sys/types.h>
19 #include <afs/procmgmt.h> /* signal(), kill(), wait(), etc. */
21 #include <afs/audit.h>
22 #include <afs/afsutil.h>
23 #include <afs/fileutil.h>
26 #define BNODE_LWP_STACKSIZE (16 * 1024)
28 int bnode_waiting = 0;
29 static PROCESS bproc_pid; /* pid of waker-upper */
30 static struct bnode *allBnodes=0; /* list of all bnodes */
31 static struct bnode_proc *allProcs=0; /* list of all processes for which we're waiting */
32 static struct bnode_type *allTypes=0; /* list of registered type handlers */
34 static struct bnode_stats {
38 static afs_int32 SendNotifierData();
39 static int DeleteProc();
42 extern char **environ; /* env structure */
45 /* Remember the name of the process, if any, that failed last */
46 static void RememberProcName(ap)
47 register struct bnode_proc *ap; {
48 register struct bnode *tbnodep;
51 if (tbnodep->lastErrorName) {
52 free(tbnodep->lastErrorName);
53 tbnodep->lastErrorName = (char *) 0;
56 tbnodep->lastErrorName = (char *) malloc(strlen(ap->coreName)+1);
57 strcpy(tbnodep->lastErrorName, ap->coreName);
61 /* utility for use by BOP_HASCORE functions to determine where a core file might
64 bnode_CoreName(abnode, acoreName, abuffer)
65 register struct bnode *abnode;
68 strcpy(abuffer, AFSDIR_SERVER_CORELOG_FILEPATH);
70 strcat(abuffer, acoreName);
73 strcat(abuffer, abnode->name);
77 /* save core file, if any */
78 static void SaveCore(abnode, aproc)
79 register struct bnode_proc *aproc;
80 register struct bnode *abnode; {
83 register afs_int32 code;
85 code = stat(AFSDIR_SERVER_CORELOG_FILEPATH, &tstat);
88 bnode_CoreName(abnode, aproc->coreName, tbuffer);
89 code = renamefile(AFSDIR_SERVER_CORELOG_FILEPATH, tbuffer);
92 bnode_GetString(abnode, abuffer, alen)
93 register struct bnode *abnode;
94 register char *abuffer;
95 register afs_int32 alen;{
96 return BOP_GETSTRING(abnode, abuffer, alen);
99 bnode_GetParm(abnode, aindex, abuffer, alen)
100 register struct bnode *abnode;
101 register afs_int32 aindex;
102 register char *abuffer;
104 return BOP_GETPARM(abnode, aindex, abuffer, alen);
107 bnode_GetStat(abnode, astatus)
108 register struct bnode *abnode;
109 register afs_int32 *astatus; {
110 return BOP_GETSTAT(abnode, astatus);
113 bnode_RestartP(abnode)
114 register struct bnode *abnode; {
115 return BOP_RESTARTP(abnode);
118 static bnode_Check(abnode)
119 register struct bnode *abnode; {
120 if (abnode->flags & BNODE_WAIT) {
121 abnode->flags &= ~BNODE_WAIT;
122 LWP_NoYieldSignal(abnode);
127 /* tell if an instance has a core file */
128 bnode_HasCore(abnode)
129 register struct bnode *abnode; {
130 return BOP_HASCORE(abnode);
133 /* wait for all bnodes to stabilize */
135 register struct bnode *tb;
136 register afs_int32 code;
140 for(tb = allBnodes; tb; tb=tb->next) {
142 code = BOP_GETSTAT(tb, &stat);
147 if (stat != tb->goal) {
148 tb->flags |= BNODE_WAIT;
158 /* wait until bnode status is correct */
159 bnode_WaitStatus(abnode, astatus)
161 register struct bnode *abnode; {
162 register afs_int32 code;
168 code = BOP_GETSTAT(abnode, &stat);
169 if (code) return code;
171 /* otherwise, check if we're done */
172 if (stat == astatus) {
173 bnode_Release(abnode);
176 if (astatus != abnode->goal) {
177 bnode_Release(abnode);
178 return -1; /* no longer our goal, don't keep waiting */
180 /* otherwise, block */
181 abnode->flags |= BNODE_WAIT;
182 LWP_WaitProcess(abnode);
186 bnode_SetStat(abnode, agoal)
187 register struct bnode *abnode;
188 register int agoal; {
189 abnode->goal = agoal;
191 BOP_SETSTAT(abnode, agoal);
192 abnode->flags &= ~BNODE_ERRORSTOP;
196 bnode_SetGoal(abnode, agoal)
197 register struct bnode *abnode;
198 register int agoal; {
199 abnode->goal = agoal;
204 bnode_SetFileGoal(abnode, agoal)
205 register struct bnode *abnode;
206 register int agoal; {
207 if (abnode->fileGoal == agoal) return 0; /* already done */
208 abnode->fileGoal = agoal;
213 /* apply a function to all bnodes in the system */
214 int bnode_ApplyInstance(aproc, arock)
217 register struct bnode *tb, *nb;
218 register afs_int32 code;
220 for(tb = allBnodes; tb; tb=nb) {
222 code = (*aproc) (tb, arock);
223 if (code) return code;
228 struct bnode *bnode_FindInstance (aname)
229 register char *aname; {
230 register struct bnode *tb;
232 for(tb=allBnodes;tb;tb=tb->next) {
233 if (!strcmp(tb->name, aname)) return tb;
235 return (struct bnode *) 0;
238 static struct bnode_type *FindType(aname)
239 register char *aname; {
240 register struct bnode_type *tt;
242 for(tt=allTypes;tt;tt=tt->next) {
243 if (!strcmp(tt->name, aname)) return tt;
245 return (struct bnode_type *) 0;
248 bnode_Register(atype, aprocs, anparms)
250 int anparms; /* number of parms to create */
251 struct bnode_ops *aprocs; {
252 register struct bnode_type *tt;
254 for(tt=allTypes;tt;tt=tt->next) {
255 if (!strcmp(tt->name, atype)) break;
258 tt = (struct bnode_type *) malloc(sizeof(struct bnode_type));
259 bzero(tt, sizeof(struct bnode_type));
268 afs_int32 bnode_Create(atype, ainstance, abp, ap1, ap2, ap3, ap4, ap5, notifier,fileGoal)
272 char *ap1, *ap2, *ap3, *ap4, *ap5, *notifier;
274 struct bnode_type *type;
276 char *notifierpath = NULL;
279 if (bnode_FindInstance(ainstance)) return BZEXISTS;
280 type = FindType(atype);
281 if (!type) return BZBADTYPE;
283 if (notifier && strcmp(notifier, NONOTIFIER)) {
284 /* construct local path from canonical (wire-format) path */
285 if (ConstructLocalBinPath(notifier, ¬ifierpath)) {
286 bozo_Log("BNODE-Create: Notifier program path invalid '%s'\n", notifier);
290 if (stat(notifierpath, &tstat)) {
291 bozo_Log("BNODE-Create: Notifier program '%s' not found\n", notifierpath);
296 tb = (*type->ops->create)(ainstance, ap1, ap2, ap3, ap4, ap5);
301 tb->notifier = notifierpath;
305 /* The fs_create above calls bnode_InitBnode() which always sets the
306 ** fileGoal to BSTAT_NORMAL .... overwrite it with whatever is passed into
307 ** this function as a parameter... */
308 tb->fileGoal = fileGoal;
310 bnode_SetStat(tb, tb->goal); /* nudge it once */
315 int bnode_DeleteName(ainstance)
317 register struct bnode *tb;
319 tb = bnode_FindInstance(ainstance);
320 if (!tb) return BZNOENT;
322 return bnode_Delete(tb);
326 register struct bnode *abnode; {
331 bnode_Release(abnode)
332 register struct bnode *abnode; {
334 if (abnode->refCount == 0 && abnode->flags & BNODE_DELETE) {
335 abnode->flags &= ~BNODE_DELETE; /* we're going for it */
336 bnode_Delete(abnode);
341 int bnode_Delete(abnode)
342 register struct bnode *abnode; {
343 register afs_int32 code;
344 register struct bnode **lb, *ub;
347 if (abnode->refCount != 0) {
348 abnode->flags |= BNODE_DELETE;
352 /* make sure the bnode is idle before zapping */
354 code = BOP_GETSTAT(abnode, &temp);
355 bnode_Release(abnode);
356 if (code) return code;
357 if (temp != BSTAT_SHUTDOWN) return BZBUSY;
359 /* all clear to zap */
360 for(lb = &allBnodes, ub = *lb; ub; lb= &ub->next, ub = *lb) {
362 /* unthread it from the list */
367 free(abnode->name); /* do this first, since bnode fields may be bad after BOP_DELETE */
368 code = BOP_DELETE(abnode); /* don't play games like holding over this one */
373 /* function to tell if there's a timeout coming up */
374 int bnode_PendingTimeout(abnode)
375 register struct bnode *abnode; {
376 return (abnode->flags & BNODE_NEEDTIMEOUT);
379 /* function called to set / clear periodic bnode wakeup times */
380 int bnode_SetTimeout(abnode, atimeout)
381 register struct bnode *abnode;
382 afs_int32 atimeout; {
384 abnode->nextTimeout = FT_ApproxTime() + atimeout;
385 abnode->flags |= BNODE_NEEDTIMEOUT;
386 abnode->period = atimeout;
387 IOMGR_Cancel(bproc_pid);
390 abnode->flags &= ~BNODE_NEEDTIMEOUT;
395 /* used by new bnode creation code to format bnode header */
396 int bnode_InitBnode (abnode, abnodeops, aname)
397 register struct bnode *abnode;
399 struct bnode_ops *abnodeops; {
400 struct bnode **lb, *nb;
402 /* format the bnode properly */
403 bzero(abnode, sizeof(struct bnode));
404 abnode->ops = abnodeops;
405 abnode->name = (char *) malloc(strlen(aname)+1);
406 strcpy(abnode->name, aname);
407 abnode->flags = BNODE_ACTIVE;
408 abnode->fileGoal = BSTAT_NORMAL;
409 abnode->goal = BSTAT_SHUTDOWN;
411 /* put the bnode at the end of the list so we write bnode file in same order */
412 for(lb = &allBnodes, nb = *lb; nb; lb = &nb->next, nb = *lb);
418 /* bnode lwp executes this code repeatedly */
420 register afs_int32 code;
421 register struct bnode *tb;
422 register afs_int32 temp;
423 register struct bnode_proc *tp;
425 int options; /* must not be register */
431 /* first figure out how long to sleep for */
432 temp = 0x7fffffff; /* afs_int32 time; maxint doesn't work in select */
434 for(tb = allBnodes; tb; tb=tb->next) {
435 if (tb->flags & BNODE_NEEDTIMEOUT) {
436 if (tb->nextTimeout < temp) {
438 temp = tb->nextTimeout;
442 /* now temp has the time at which we should wakeup next */
445 if (setAny) temp -= FT_ApproxTime(); /* how many seconds until next event */
450 code = IOMGR_Select(0, 0, 0, 0, &tv);
452 else code = 0; /* fake timeout code */
454 /* figure out why we woke up; child exit or timeouts */
455 FT_GetTimeOfDay(&tv, 0); /* must do the real gettimeofday once and a while */
458 /* check all bnodes to see which ones need timeout events */
459 for(tb = allBnodes; tb; tb=nb) {
460 if ((tb->flags & BNODE_NEEDTIMEOUT) && temp > tb->nextTimeout) {
464 if (tb->flags & BNODE_NEEDTIMEOUT) { /* check again, BOP_TIMEOUT could change */
465 tb->nextTimeout = FT_ApproxTime() + tb->period;
468 bnode_Release(tb); /* delete may occur here */
474 /* signalled, probably by incoming signal */
477 bnode_waiting = options | 0x800000;
478 code = waitpid((pid_t)-1, &status, options);
480 if (code == 0 || code == -1) break; /* all done */
481 /* otherwise code has a process id, which we now search for */
482 for(tp=allProcs; tp; tp=tp->next)
483 if (tp->pid == code) break;
489 /* count restarts in last 10 seconds */
490 if (temp > tb->rsTime + 30) {
491 /* it's been 10 seconds we've been counting */
496 if (WIFSIGNALED(status) == 0) {
497 /* exited, not signalled */
498 tp->lastExit = WEXITSTATUS(status);
501 tb->errorCode = tp->lastExit;
502 tb->lastErrorExit = FT_ApproxTime();
503 RememberProcName(tp);
508 /* Signal occurred, perhaps spurious due to shutdown request.
509 * If due to a shutdown request, don't overwrite last error
512 tp->lastSignal = WTERMSIG(status);
514 if (tp->lastSignal != SIGQUIT && tp->lastSignal != SIGTERM
515 && tp->lastSignal != SIGKILL) {
516 tb->errorSignal = tp->lastSignal;
517 tb->lastErrorExit = FT_ApproxTime();
518 RememberProcName(tp);
522 tb->lastAnyExit = FT_ApproxTime();
525 bozo_Log("BNODE: Notifier %s will be called\n", tb->notifier);
528 BOP_PROCEXIT(tb, tp);
531 if (tb->rsCount++ > 10) {
532 /* 10 in 10 seconds */
533 tb->flags |= BNODE_ERRORSTOP;
534 bnode_SetGoal(tb, BSTAT_SHUTDOWN);
535 bozo_Log("BNODE '%s' repeatedly failed to start, perhaps missing executable.\n",
538 bnode_Release(tb); /* bnode delete can happen here */
541 else bnode_stats.weirdPids++;
551 signal(SIGPIPE, SIG_IGN);
552 bozo_Log("Notifier aborted prematurely");
560 struct bnode_proc *tp;
562 #ifndef AFS_NT40_ENV /* NT notifier callout not yet implemented */
563 int code, pid, status;
566 if (stat(tp->bnode->notifier, &tstat)) {
567 bozo_Log("BNODE: Failed to find notifier '%s'; ignored\n", tp->bnode->notifier);
570 if ((pid = fork()) == 0) {
572 struct bnode *tb = tp->bnode;
575 #if defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_SGI51_ENV)
578 #ifdef AFS_LINUX20_ENV
584 fout = popen(tb->notifier, "w");
586 bozo_Log("BNODE: Failed to find notifier '%s'; ignored\n", tb->notifier);
587 perror(tb->notifier);
590 code = SendNotifierData(fileno(fout), tp);
593 } else if (pid < 0) {
594 bozo_Log("Failed to fork creating process to handle notifier '%s'\n", tp->bnode->notifier);
597 #endif /* AFS_NT40_ENV */
602 static afs_int32 SendNotifierData(fd, tp)
604 register struct bnode_proc *tp;
606 register struct bnode *tb = tp->bnode;
607 char buffer[1000], *bufp = buffer, *buf1;
611 * First sent out the bnode_proc struct
613 (void) sprintf(bufp, "BEGIN bnode_proc\n");
614 bufp += strlen(bufp);
615 (void) sprintf(bufp, "comLine: %s\n", tp->comLine);
616 bufp += strlen(bufp);
617 if (!(buf1 = tp->coreName))
619 (void) sprintf(bufp, "coreName: %s\n", buf1);
620 bufp += strlen(bufp);
621 (void) sprintf(bufp, "pid: %ld\n", tp->pid);
622 bufp += strlen(bufp);
623 (void) sprintf(bufp, "lastExit: %ld\n", tp->lastExit);
624 bufp += strlen(bufp);
626 (void) sprintf(bufp, "lastSignal: %ld\n", tp->lastSignal);
627 bufp += strlen(bufp);
629 (void) sprintf(bufp, "flags: %ld\n", tp->flags);
630 bufp += strlen(bufp);
631 (void) sprintf(bufp, "END bnode_proc\n");
632 bufp += strlen(bufp);
633 len =(int)(bufp-buffer);
634 if (write(fd, buffer, len) < 0) {
639 * Now sent out the bnode struct
642 (void) sprintf(bufp, "BEGIN bnode\n");
643 bufp += strlen(bufp);
644 (void) sprintf(bufp, "name: %s\n", tb->name);
645 bufp += strlen(bufp);
646 (void) sprintf(bufp, "rsTime: %ld\n", tb->rsTime);
647 bufp += strlen(bufp);
648 (void) sprintf(bufp, "rsCount: %ld\n", tb->rsCount);
649 bufp += strlen(bufp);
650 (void) sprintf(bufp, "procStartTime: %ld\n", tb->procStartTime);
651 bufp += strlen(bufp);
652 (void) sprintf(bufp, "procStarts: %ld\n", tb->procStarts);
653 bufp += strlen(bufp);
654 (void) sprintf(bufp, "lastAnyExit: %ld\n", tb->lastAnyExit);
655 bufp += strlen(bufp);
656 (void) sprintf(bufp, "lastErrorExit: %ld\n", tb->lastErrorExit);
657 bufp += strlen(bufp);
658 (void) sprintf(bufp, "errorCode: %ld\n", tb->errorCode);
659 bufp += strlen(bufp);
660 (void) sprintf(bufp, "errorSignal: %ld\n", tb->errorSignal);
661 bufp += strlen(bufp);
663 (void) sprintf(bufp, "lastErrorName: %s\n", tb->lastErrorName);
664 bufp += strlen(bufp);
666 (void) sprintf(bufp, "goal: %d\n", tb->goal);
667 bufp += strlen(bufp);
668 (void) sprintf(bufp, "END bnode\n");
669 bufp += strlen(bufp);
670 len = (int)bufp-(int)buffer;
671 if (write(fd, buffer, len) < 0) {
678 /* Called by IOMGR at low priority on IOMGR's stack shortly after a SIGCHLD
679 * occurs. Wakes up bproc do redo things */
680 bnode_SoftInt(int asignal)
682 IOMGR_Cancel(bproc_pid);
686 /* Called at signal interrupt level; queues function to be called
687 * when IOMGR runs again.
690 bnode_Int(int asignal)
692 extern void bozo_ShutdownAndExit();
694 if (asignal == SIGQUIT) {
695 IOMGR_SoftSig(bozo_ShutdownAndExit, asignal);
697 IOMGR_SoftSig(bnode_SoftInt, asignal);
702 /* intialize the whole system */
705 register afs_int32 code;
706 struct sigaction newaction;
709 if (initDone) return 0;
711 bzero(&bnode_stats, sizeof(bnode_stats));
712 LWP_InitializeProcessSupport(1, &junk); /* just in case */
714 code = LWP_CreateProcess(bproc, BNODE_LWP_STACKSIZE,
715 /* priority */ 1, /* parm */0, "bnode-manager", &bproc_pid);
716 if (code) return code;
717 bzero((char *)&newaction, sizeof(newaction));
718 newaction.sa_handler = bnode_Int;
719 code = sigaction(SIGCHLD, &newaction, NULL);
720 if (code) return errno;
721 code = sigaction(SIGQUIT, &newaction, NULL);
722 if (code) return errno;
726 /* free token list returned by parseLine */
727 bnode_FreeTokens(alist)
728 register struct bnode_token *alist; {
729 register struct bnode_token *nlist;
730 for(; alist; alist = nlist) {
740 if (x == 0 || x == ' ' || x == '\t' || x== '\n') return 1;
744 bnode_ParseLine(aline, alist)
746 struct bnode_token **alist; {
750 struct bnode_token *first, *last;
751 register struct bnode_token *ttok;
754 inToken = 0; /* not copying token chars at start */
755 first = (struct bnode_token *) 0;
756 last = (struct bnode_token *) 0;
759 if (tc == 0 || space(tc)) { /* terminating null gets us in here, too */
761 inToken = 0; /* end of this token */
763 ttok = (struct bnode_token *) malloc(sizeof(struct bnode_token));
764 ttok->next = (struct bnode_token *) 0;
765 ttok->key = (char *) malloc(strlen(tbuffer)+1);
766 strcpy(ttok->key, tbuffer);
772 if (!first) first = ttok;
776 /* an alpha character */
781 if (tptr - tbuffer >= sizeof(tbuffer)) return -1; /* token too long */
785 /* last token flushed 'cause space(0) --> true */
786 if (last) last->next = (struct bnode_token *) 0;
794 int bnode_NewProc(abnode, aexecString, coreName, aproc)
795 struct bnode_proc **aproc;
797 struct bnode *abnode;
799 struct bnode_token *tlist, *tt;
801 struct bnode_proc *tp;
803 char *argv[MAXVARGS];
806 code = bnode_ParseLine(aexecString, &tlist); /* try parsing first */
807 if (code) return code;
808 tp = (struct bnode_proc *) malloc(sizeof(struct bnode_proc));
809 bzero(tp, sizeof(struct bnode_proc));
814 tp->comLine = aexecString;
815 tp->coreName = coreName; /* may be null */
816 abnode->procStartTime = FT_ApproxTime();
817 abnode->procStarts++;
819 /* convert linked list of tokens into argv structure */
820 for (tt = tlist, i = 0; i < (MAXVARGS - 1) && tt; tt = tt->next, i++) {
823 argv[i] = (char *) 0; /* null-terminated */
825 cpid = spawnprocve(argv[0], argv, environ, -1);
826 osi_audit(BOSSpawnProcEvent, 0, AUD_STR, aexecString, AUD_END );
828 if (cpid == (pid_t)-1) {
829 bozo_Log("Failed to spawn process for bnode '%s'\n", abnode->name);
830 bnode_FreeTokens(tlist);
835 bnode_FreeTokens(tlist);
837 tp->flags = BPROC_STARTED;
838 tp->flags &= ~BPROC_EXITED;
843 int bnode_StopProc(aproc, asignal)
844 register struct bnode_proc *aproc;
847 if (!(aproc->flags & BPROC_STARTED) || (aproc->flags & BPROC_EXITED))
850 osi_audit( BOSStopProcEvent, 0, AUD_STR, (aproc ? aproc->comLine : (char *)0), AUD_END );
852 code = kill(aproc->pid, asignal);
853 bnode_Check(aproc->bnode);
857 int bnode_Deactivate(abnode)
858 register struct bnode *abnode; {
859 register struct bnode **pb, *tb;
861 if (!(abnode->flags & BNODE_ACTIVE)) return BZNOTACTIVE;
862 for(pb = &allBnodes,tb = *pb; tb; tb=nb) {
866 tb->flags &= ~BNODE_ACTIVE;
873 static int DeleteProc(abproc)
874 register struct bnode_proc *abproc; {
875 register struct bnode_proc **pb, *tb;
876 struct bnode_proc *nb;
878 for(pb = &allProcs,tb = *pb; tb; pb = &tb->next, tb=nb) {