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
30 #include <afs/procmgmt.h> /* signal(), kill(), wait(), etc. */
32 #include <afs/audit.h>
33 #include <afs/afsutil.h>
34 #include <afs/fileutil.h>
37 #define BNODE_LWP_STACKSIZE (16 * 1024)
39 int bnode_waiting = 0;
40 static PROCESS bproc_pid; /* pid of waker-upper */
41 static struct bnode *allBnodes=0; /* list of all bnodes */
42 static struct bnode_proc *allProcs=0; /* list of all processes for which we're waiting */
43 static struct bnode_type *allTypes=0; /* list of registered type handlers */
45 static struct bnode_stats {
49 static afs_int32 SendNotifierData();
50 static int DeleteProc();
53 extern char **environ; /* env structure */
56 /* Remember the name of the process, if any, that failed last */
57 static void RememberProcName(ap)
58 register struct bnode_proc *ap; {
59 register struct bnode *tbnodep;
62 if (tbnodep->lastErrorName) {
63 free(tbnodep->lastErrorName);
64 tbnodep->lastErrorName = (char *) 0;
67 tbnodep->lastErrorName = (char *) malloc(strlen(ap->coreName)+1);
68 strcpy(tbnodep->lastErrorName, ap->coreName);
72 /* utility for use by BOP_HASCORE functions to determine where a core file might
75 bnode_CoreName(abnode, acoreName, abuffer)
76 register struct bnode *abnode;
79 strcpy(abuffer, AFSDIR_SERVER_CORELOG_FILEPATH);
81 strcat(abuffer, acoreName);
84 strcat(abuffer, abnode->name);
88 /* save core file, if any */
89 static void SaveCore(abnode, aproc)
90 register struct bnode_proc *aproc;
91 register struct bnode *abnode; {
94 register afs_int32 code;
95 #ifdef BOZO_SAVE_CORES
97 struct tm *TimeFields;
101 code = stat(AFSDIR_SERVER_CORELOG_FILEPATH, &tstat);
104 bnode_CoreName(abnode, aproc->coreName, tbuffer);
105 #ifdef BOZO_SAVE_CORES
106 TM_GetTimeOfDay(&Start, 0);
107 TimeFields = localtime(&Start.tv_sec);
108 sprintf(FileName,"%s.%d%02d%02d%02d%02d%02d", tbuffer,
109 TimeFields->tm_year, TimeFields->tm_mon + 1,
110 TimeFields->tm_mday, TimeFields->tm_hour, TimeFields->tm_min,
112 strcpy(tbuffer,FileName);
114 code = renamefile(AFSDIR_SERVER_CORELOG_FILEPATH, tbuffer);
117 bnode_GetString(abnode, abuffer, alen)
118 register struct bnode *abnode;
119 register char *abuffer;
120 register afs_int32 alen;{
121 return BOP_GETSTRING(abnode, abuffer, alen);
124 bnode_GetParm(abnode, aindex, abuffer, alen)
125 register struct bnode *abnode;
126 register afs_int32 aindex;
127 register char *abuffer;
129 return BOP_GETPARM(abnode, aindex, abuffer, alen);
132 bnode_GetStat(abnode, astatus)
133 register struct bnode *abnode;
134 register afs_int32 *astatus; {
135 return BOP_GETSTAT(abnode, astatus);
138 bnode_RestartP(abnode)
139 register struct bnode *abnode; {
140 return BOP_RESTARTP(abnode);
143 static bnode_Check(abnode)
144 register struct bnode *abnode; {
145 if (abnode->flags & BNODE_WAIT) {
146 abnode->flags &= ~BNODE_WAIT;
147 LWP_NoYieldSignal(abnode);
152 /* tell if an instance has a core file */
153 bnode_HasCore(abnode)
154 register struct bnode *abnode; {
155 return BOP_HASCORE(abnode);
158 /* wait for all bnodes to stabilize */
160 register struct bnode *tb;
161 register afs_int32 code;
165 for(tb = allBnodes; tb; tb=tb->next) {
167 code = BOP_GETSTAT(tb, &stat);
172 if (stat != tb->goal) {
173 tb->flags |= BNODE_WAIT;
183 /* wait until bnode status is correct */
184 bnode_WaitStatus(abnode, astatus)
186 register struct bnode *abnode; {
187 register afs_int32 code;
193 code = BOP_GETSTAT(abnode, &stat);
194 if (code) return code;
196 /* otherwise, check if we're done */
197 if (stat == astatus) {
198 bnode_Release(abnode);
201 if (astatus != abnode->goal) {
202 bnode_Release(abnode);
203 return -1; /* no longer our goal, don't keep waiting */
205 /* otherwise, block */
206 abnode->flags |= BNODE_WAIT;
207 LWP_WaitProcess(abnode);
211 bnode_SetStat(abnode, agoal)
212 register struct bnode *abnode;
213 register int agoal; {
214 abnode->goal = agoal;
216 BOP_SETSTAT(abnode, agoal);
217 abnode->flags &= ~BNODE_ERRORSTOP;
221 bnode_SetGoal(abnode, agoal)
222 register struct bnode *abnode;
223 register int agoal; {
224 abnode->goal = agoal;
229 bnode_SetFileGoal(abnode, agoal)
230 register struct bnode *abnode;
231 register int agoal; {
232 if (abnode->fileGoal == agoal) return 0; /* already done */
233 abnode->fileGoal = agoal;
238 /* apply a function to all bnodes in the system */
239 int bnode_ApplyInstance(aproc, arock)
242 register struct bnode *tb, *nb;
243 register afs_int32 code;
245 for(tb = allBnodes; tb; tb=nb) {
247 code = (*aproc) (tb, arock);
248 if (code) return code;
253 struct bnode *bnode_FindInstance (aname)
254 register char *aname; {
255 register struct bnode *tb;
257 for(tb=allBnodes;tb;tb=tb->next) {
258 if (!strcmp(tb->name, aname)) return tb;
260 return (struct bnode *) 0;
263 static struct bnode_type *FindType(aname)
264 register char *aname; {
265 register struct bnode_type *tt;
267 for(tt=allTypes;tt;tt=tt->next) {
268 if (!strcmp(tt->name, aname)) return tt;
270 return (struct bnode_type *) 0;
273 bnode_Register(atype, aprocs, anparms)
275 int anparms; /* number of parms to create */
276 struct bnode_ops *aprocs; {
277 register struct bnode_type *tt;
279 for(tt=allTypes;tt;tt=tt->next) {
280 if (!strcmp(tt->name, atype)) break;
283 tt = (struct bnode_type *) malloc(sizeof(struct bnode_type));
284 memset(tt, 0, sizeof(struct bnode_type));
293 afs_int32 bnode_Create(atype, ainstance, abp, ap1, ap2, ap3, ap4, ap5, notifier,fileGoal)
297 char *ap1, *ap2, *ap3, *ap4, *ap5, *notifier;
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(ainstance)
342 register struct bnode *tb;
344 tb = bnode_FindInstance(ainstance);
345 if (!tb) return BZNOENT;
347 return bnode_Delete(tb);
351 register struct bnode *abnode; {
356 bnode_Release(abnode)
357 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(abnode)
367 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(abnode)
400 register struct bnode *abnode; {
401 return (abnode->flags & BNODE_NEEDTIMEOUT);
404 /* function called to set / clear periodic bnode wakeup times */
405 int bnode_SetTimeout(abnode, atimeout)
406 register struct bnode *abnode;
407 afs_int32 atimeout; {
409 abnode->nextTimeout = FT_ApproxTime() + atimeout;
410 abnode->flags |= BNODE_NEEDTIMEOUT;
411 abnode->period = atimeout;
412 IOMGR_Cancel(bproc_pid);
415 abnode->flags &= ~BNODE_NEEDTIMEOUT;
420 /* used by new bnode creation code to format bnode header */
421 int bnode_InitBnode (abnode, abnodeops, aname)
422 register struct bnode *abnode;
424 struct bnode_ops *abnodeops; {
425 struct bnode **lb, *nb;
427 /* format the bnode properly */
428 memset(abnode, 0, sizeof(struct bnode));
429 abnode->ops = abnodeops;
430 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);
533 /* Signal occurred, perhaps spurious due to shutdown request.
534 * If due to a shutdown request, don't overwrite last error
537 tp->lastSignal = WTERMSIG(status);
539 if (tp->lastSignal != SIGQUIT && tp->lastSignal != SIGTERM
540 && tp->lastSignal != SIGKILL) {
541 tb->errorSignal = tp->lastSignal;
542 tb->lastErrorExit = FT_ApproxTime();
543 RememberProcName(tp);
547 tb->lastAnyExit = FT_ApproxTime();
550 bozo_Log("BNODE: Notifier %s will be called\n", tb->notifier);
553 BOP_PROCEXIT(tb, tp);
556 if (tb->rsCount++ > 10) {
557 /* 10 in 10 seconds */
558 tb->flags |= BNODE_ERRORSTOP;
559 bnode_SetGoal(tb, BSTAT_SHUTDOWN);
560 bozo_Log("BNODE '%s' repeatedly failed to start, perhaps missing executable.\n",
563 bnode_Release(tb); /* bnode delete can happen here */
566 else bnode_stats.weirdPids++;
576 signal(SIGPIPE, SIG_IGN);
577 bozo_Log("Notifier aborted prematurely");
585 struct bnode_proc *tp;
587 #ifndef AFS_NT40_ENV /* NT notifier callout not yet implemented */
588 int code, pid, status;
591 if (stat(tp->bnode->notifier, &tstat)) {
592 bozo_Log("BNODE: Failed to find notifier '%s'; ignored\n", tp->bnode->notifier);
595 if ((pid = fork()) == 0) {
597 struct bnode *tb = tp->bnode;
600 #if defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_SGI51_ENV)
603 #ifdef AFS_LINUX20_ENV
609 fout = popen(tb->notifier, "w");
611 bozo_Log("BNODE: Failed to find notifier '%s'; ignored\n", tb->notifier);
612 perror(tb->notifier);
615 code = SendNotifierData(fileno(fout), tp);
618 } else if (pid < 0) {
619 bozo_Log("Failed to fork creating process to handle notifier '%s'\n", tp->bnode->notifier);
622 #endif /* AFS_NT40_ENV */
627 static afs_int32 SendNotifierData(fd, tp)
629 register struct bnode_proc *tp;
631 register struct bnode *tb = tp->bnode;
632 char buffer[1000], *bufp = buffer, *buf1;
636 * First sent out the bnode_proc struct
638 (void) sprintf(bufp, "BEGIN bnode_proc\n");
639 bufp += strlen(bufp);
640 (void) sprintf(bufp, "comLine: %s\n", tp->comLine);
641 bufp += strlen(bufp);
642 if (!(buf1 = tp->coreName))
644 (void) sprintf(bufp, "coreName: %s\n", buf1);
645 bufp += strlen(bufp);
646 (void) sprintf(bufp, "pid: %ld\n", tp->pid);
647 bufp += strlen(bufp);
648 (void) sprintf(bufp, "lastExit: %ld\n", tp->lastExit);
649 bufp += strlen(bufp);
651 (void) sprintf(bufp, "lastSignal: %ld\n", tp->lastSignal);
652 bufp += strlen(bufp);
654 (void) sprintf(bufp, "flags: %ld\n", tp->flags);
655 bufp += strlen(bufp);
656 (void) sprintf(bufp, "END bnode_proc\n");
657 bufp += strlen(bufp);
658 len =(int)(bufp-buffer);
659 if (write(fd, buffer, len) < 0) {
664 * Now sent out the bnode struct
667 (void) sprintf(bufp, "BEGIN bnode\n");
668 bufp += strlen(bufp);
669 (void) sprintf(bufp, "name: %s\n", tb->name);
670 bufp += strlen(bufp);
671 (void) sprintf(bufp, "rsTime: %ld\n", tb->rsTime);
672 bufp += strlen(bufp);
673 (void) sprintf(bufp, "rsCount: %ld\n", tb->rsCount);
674 bufp += strlen(bufp);
675 (void) sprintf(bufp, "procStartTime: %ld\n", tb->procStartTime);
676 bufp += strlen(bufp);
677 (void) sprintf(bufp, "procStarts: %ld\n", tb->procStarts);
678 bufp += strlen(bufp);
679 (void) sprintf(bufp, "lastAnyExit: %ld\n", tb->lastAnyExit);
680 bufp += strlen(bufp);
681 (void) sprintf(bufp, "lastErrorExit: %ld\n", tb->lastErrorExit);
682 bufp += strlen(bufp);
683 (void) sprintf(bufp, "errorCode: %ld\n", tb->errorCode);
684 bufp += strlen(bufp);
685 (void) sprintf(bufp, "errorSignal: %ld\n", tb->errorSignal);
686 bufp += strlen(bufp);
688 (void) sprintf(bufp, "lastErrorName: %s\n", tb->lastErrorName);
689 bufp += strlen(bufp);
691 (void) sprintf(bufp, "goal: %d\n", tb->goal);
692 bufp += strlen(bufp);
693 (void) sprintf(bufp, "END bnode\n");
694 bufp += strlen(bufp);
695 len = (int)bufp-(int)buffer;
696 if (write(fd, buffer, len) < 0) {
703 /* Called by IOMGR at low priority on IOMGR's stack shortly after a SIGCHLD
704 * occurs. Wakes up bproc do redo things */
705 bnode_SoftInt(int asignal)
707 IOMGR_Cancel(bproc_pid);
711 /* Called at signal interrupt level; queues function to be called
712 * when IOMGR runs again.
715 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, /* parm */0, "bnode-manager", &bproc_pid);
741 if (code) return code;
742 memset((char *)&newaction, 0, sizeof(newaction));
743 newaction.sa_handler = bnode_Int;
744 code = sigaction(SIGCHLD, &newaction, NULL);
745 if (code) return errno;
746 code = sigaction(SIGQUIT, &newaction, NULL);
747 if (code) return errno;
751 /* free token list returned by parseLine */
752 bnode_FreeTokens(alist)
753 register struct bnode_token *alist; {
754 register struct bnode_token *nlist;
755 for(; alist; alist = nlist) {
765 if (x == 0 || x == ' ' || x == '\t' || x== '\n') return 1;
769 bnode_ParseLine(aline, alist)
771 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(abnode, aexecString, coreName, aproc)
820 struct bnode_proc **aproc;
822 struct bnode *abnode;
824 struct bnode_token *tlist, *tt;
826 struct bnode_proc *tp;
828 char *argv[MAXVARGS];
831 code = bnode_ParseLine(aexecString, &tlist); /* try parsing first */
832 if (code) return code;
833 tp = (struct bnode_proc *) malloc(sizeof(struct bnode_proc));
834 memset(tp, 0, sizeof(struct bnode_proc));
839 tp->comLine = aexecString;
840 tp->coreName = coreName; /* may be null */
841 abnode->procStartTime = FT_ApproxTime();
842 abnode->procStarts++;
844 /* convert linked list of tokens into argv structure */
845 for (tt = tlist, i = 0; i < (MAXVARGS - 1) && tt; tt = tt->next, i++) {
848 argv[i] = (char *) 0; /* null-terminated */
850 cpid = spawnprocve(argv[0], argv, environ, -1);
851 osi_audit(BOSSpawnProcEvent, 0, AUD_STR, aexecString, AUD_END );
853 if (cpid == (pid_t)-1) {
854 bozo_Log("Failed to spawn process for bnode '%s'\n", abnode->name);
855 bnode_FreeTokens(tlist);
860 bnode_FreeTokens(tlist);
862 tp->flags = BPROC_STARTED;
863 tp->flags &= ~BPROC_EXITED;
868 int bnode_StopProc(aproc, asignal)
869 register struct bnode_proc *aproc;
872 if (!(aproc->flags & BPROC_STARTED) || (aproc->flags & BPROC_EXITED))
875 osi_audit( BOSStopProcEvent, 0, AUD_STR, (aproc ? aproc->comLine : (char *)0), AUD_END );
877 code = kill(aproc->pid, asignal);
878 bnode_Check(aproc->bnode);
882 int bnode_Deactivate(abnode)
883 register struct bnode *abnode; {
884 register struct bnode **pb, *tb;
886 if (!(abnode->flags & BNODE_ACTIVE)) return BZNOTACTIVE;
887 for(pb = &allBnodes,tb = *pb; tb; tb=nb) {
891 tb->flags &= ~BNODE_ACTIVE;
898 static int DeleteProc(abproc)
899 register struct bnode_proc *abproc; {
900 register struct bnode_proc **pb, *tb;
901 struct bnode_proc *nb;
903 for(pb = &allProcs,tb = *pb; tb; pb = &tb->next, tb=nb) {