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>
38 /* All known versions of AIX lack WCOREDUMP but this works */
39 #define WCOREDUMP(x) ((x) & 0x80)
42 #define BNODE_LWP_STACKSIZE (16 * 1024)
44 int bnode_waiting = 0;
45 static PROCESS bproc_pid; /* pid of waker-upper */
46 static struct bnode *allBnodes=0; /* list of all bnodes */
47 static struct bnode_proc *allProcs=0; /* list of all processes for which we're waiting */
48 static struct bnode_type *allTypes=0; /* list of registered type handlers */
50 static struct bnode_stats {
54 static afs_int32 SendNotifierData();
55 static int DeleteProc();
58 extern char **environ; /* env structure */
61 /* Remember the name of the process, if any, that failed last */
62 static void RememberProcName(ap)
63 register struct bnode_proc *ap; {
64 register struct bnode *tbnodep;
67 if (tbnodep->lastErrorName) {
68 free(tbnodep->lastErrorName);
69 tbnodep->lastErrorName = (char *) 0;
72 tbnodep->lastErrorName = (char *) malloc(strlen(ap->coreName)+1);
73 strcpy(tbnodep->lastErrorName, ap->coreName);
77 /* utility for use by BOP_HASCORE functions to determine where a core file might
80 bnode_CoreName(abnode, acoreName, abuffer)
81 register struct bnode *abnode;
84 strcpy(abuffer, AFSDIR_SERVER_CORELOG_FILEPATH);
86 strcat(abuffer, acoreName);
89 strcat(abuffer, abnode->name);
93 /* save core file, if any */
94 static void SaveCore(abnode, aproc)
95 register struct bnode_proc *aproc;
96 register struct bnode *abnode; {
99 register afs_int32 code;
100 #ifdef BOZO_SAVE_CORES
101 struct timeval Start;
102 struct tm *TimeFields;
106 code = stat(AFSDIR_SERVER_CORELOG_FILEPATH, &tstat);
109 bnode_CoreName(abnode, aproc->coreName, tbuffer);
110 #ifdef BOZO_SAVE_CORES
111 TM_GetTimeOfDay(&Start, 0);
112 TimeFields = localtime(&Start.tv_sec);
113 sprintf(FileName,"%s.%d%02d%02d%02d%02d%02d", tbuffer,
114 TimeFields->tm_year, TimeFields->tm_mon + 1,
115 TimeFields->tm_mday, TimeFields->tm_hour, TimeFields->tm_min,
117 strcpy(tbuffer,FileName);
119 code = renamefile(AFSDIR_SERVER_CORELOG_FILEPATH, tbuffer);
122 bnode_GetString(abnode, abuffer, alen)
123 register struct bnode *abnode;
124 register char *abuffer;
125 register afs_int32 alen;{
126 return BOP_GETSTRING(abnode, abuffer, alen);
129 bnode_GetParm(abnode, aindex, abuffer, alen)
130 register struct bnode *abnode;
131 register afs_int32 aindex;
132 register char *abuffer;
134 return BOP_GETPARM(abnode, aindex, abuffer, alen);
137 bnode_GetStat(abnode, astatus)
138 register struct bnode *abnode;
139 register afs_int32 *astatus; {
140 return BOP_GETSTAT(abnode, astatus);
143 bnode_RestartP(abnode)
144 register struct bnode *abnode; {
145 return BOP_RESTARTP(abnode);
148 static bnode_Check(abnode)
149 register struct bnode *abnode; {
150 if (abnode->flags & BNODE_WAIT) {
151 abnode->flags &= ~BNODE_WAIT;
152 LWP_NoYieldSignal(abnode);
157 /* tell if an instance has a core file */
158 bnode_HasCore(abnode)
159 register struct bnode *abnode; {
160 return BOP_HASCORE(abnode);
163 /* wait for all bnodes to stabilize */
165 register struct bnode *tb;
166 register afs_int32 code;
170 for(tb = allBnodes; tb; tb=tb->next) {
172 code = BOP_GETSTAT(tb, &stat);
177 if (stat != tb->goal) {
178 tb->flags |= BNODE_WAIT;
188 /* wait until bnode status is correct */
189 bnode_WaitStatus(abnode, astatus)
191 register struct bnode *abnode; {
192 register afs_int32 code;
198 code = BOP_GETSTAT(abnode, &stat);
199 if (code) return code;
201 /* otherwise, check if we're done */
202 if (stat == astatus) {
203 bnode_Release(abnode);
206 if (astatus != abnode->goal) {
207 bnode_Release(abnode);
208 return -1; /* no longer our goal, don't keep waiting */
210 /* otherwise, block */
211 abnode->flags |= BNODE_WAIT;
212 LWP_WaitProcess(abnode);
216 bnode_SetStat(abnode, agoal)
217 register struct bnode *abnode;
218 register int agoal; {
219 abnode->goal = agoal;
221 BOP_SETSTAT(abnode, agoal);
222 abnode->flags &= ~BNODE_ERRORSTOP;
226 bnode_SetGoal(abnode, agoal)
227 register struct bnode *abnode;
228 register int agoal; {
229 abnode->goal = agoal;
234 bnode_SetFileGoal(abnode, agoal)
235 register struct bnode *abnode;
236 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(aproc, arock)
247 register struct bnode *tb, *nb;
248 register afs_int32 code;
250 for(tb = allBnodes; tb; tb=nb) {
252 code = (*aproc) (tb, arock);
253 if (code) return code;
258 struct bnode *bnode_FindInstance (aname)
259 register char *aname; {
260 register struct bnode *tb;
262 for(tb=allBnodes;tb;tb=tb->next) {
263 if (!strcmp(tb->name, aname)) return tb;
265 return (struct bnode *) 0;
268 static struct bnode_type *FindType(aname)
269 register char *aname; {
270 register struct bnode_type *tt;
272 for(tt=allTypes;tt;tt=tt->next) {
273 if (!strcmp(tt->name, aname)) return tt;
275 return (struct bnode_type *) 0;
278 bnode_Register(atype, aprocs, anparms)
280 int anparms; /* number of parms to create */
281 struct bnode_ops *aprocs; {
282 register struct bnode_type *tt;
284 for(tt=allTypes;tt;tt=tt->next) {
285 if (!strcmp(tt->name, atype)) break;
288 tt = (struct bnode_type *) malloc(sizeof(struct bnode_type));
289 memset(tt, 0, sizeof(struct bnode_type));
298 afs_int32 bnode_Create(atype, ainstance, abp, ap1, ap2, ap3, ap4, ap5, notifier,fileGoal)
302 char *ap1, *ap2, *ap3, *ap4, *ap5, *notifier;
304 struct bnode_type *type;
306 char *notifierpath = NULL;
309 if (bnode_FindInstance(ainstance)) return BZEXISTS;
310 type = FindType(atype);
311 if (!type) return BZBADTYPE;
313 if (notifier && strcmp(notifier, NONOTIFIER)) {
314 /* construct local path from canonical (wire-format) path */
315 if (ConstructLocalBinPath(notifier, ¬ifierpath)) {
316 bozo_Log("BNODE-Create: Notifier program path invalid '%s'\n", notifier);
320 if (stat(notifierpath, &tstat)) {
321 bozo_Log("BNODE-Create: Notifier program '%s' not found\n", notifierpath);
326 tb = (*type->ops->create)(ainstance, ap1, ap2, ap3, ap4, ap5);
331 tb->notifier = notifierpath;
335 /* The fs_create above calls bnode_InitBnode() which always sets the
336 ** fileGoal to BSTAT_NORMAL .... overwrite it with whatever is passed into
337 ** this function as a parameter... */
338 tb->fileGoal = fileGoal;
340 bnode_SetStat(tb, tb->goal); /* nudge it once */
345 int bnode_DeleteName(ainstance)
347 register struct bnode *tb;
349 tb = bnode_FindInstance(ainstance);
350 if (!tb) return BZNOENT;
352 return bnode_Delete(tb);
356 register struct bnode *abnode; {
361 bnode_Release(abnode)
362 register struct bnode *abnode; {
364 if (abnode->refCount == 0 && abnode->flags & BNODE_DELETE) {
365 abnode->flags &= ~BNODE_DELETE; /* we're going for it */
366 bnode_Delete(abnode);
371 int bnode_Delete(abnode)
372 register struct bnode *abnode; {
373 register afs_int32 code;
374 register struct bnode **lb, *ub;
377 if (abnode->refCount != 0) {
378 abnode->flags |= BNODE_DELETE;
382 /* make sure the bnode is idle before zapping */
384 code = BOP_GETSTAT(abnode, &temp);
385 bnode_Release(abnode);
386 if (code) return code;
387 if (temp != BSTAT_SHUTDOWN) return BZBUSY;
389 /* all clear to zap */
390 for(lb = &allBnodes, ub = *lb; ub; lb= &ub->next, ub = *lb) {
392 /* unthread it from the list */
397 free(abnode->name); /* do this first, since bnode fields may be bad after BOP_DELETE */
398 code = BOP_DELETE(abnode); /* don't play games like holding over this one */
403 /* function to tell if there's a timeout coming up */
404 int bnode_PendingTimeout(abnode)
405 register struct bnode *abnode; {
406 return (abnode->flags & BNODE_NEEDTIMEOUT);
409 /* function called to set / clear periodic bnode wakeup times */
410 int bnode_SetTimeout(abnode, atimeout)
411 register struct bnode *abnode;
412 afs_int32 atimeout; {
414 abnode->nextTimeout = FT_ApproxTime() + atimeout;
415 abnode->flags |= BNODE_NEEDTIMEOUT;
416 abnode->period = atimeout;
417 IOMGR_Cancel(bproc_pid);
420 abnode->flags &= ~BNODE_NEEDTIMEOUT;
425 /* used by new bnode creation code to format bnode header */
426 int bnode_InitBnode (abnode, abnodeops, aname)
427 register struct bnode *abnode;
429 struct bnode_ops *abnodeops; {
430 struct bnode **lb, *nb;
432 /* format the bnode properly */
433 memset(abnode, 0, sizeof(struct bnode));
434 abnode->ops = abnodeops;
435 abnode->name = (char *) malloc(strlen(aname)+1);
436 strcpy(abnode->name, aname);
437 abnode->flags = BNODE_ACTIVE;
438 abnode->fileGoal = BSTAT_NORMAL;
439 abnode->goal = BSTAT_SHUTDOWN;
441 /* put the bnode at the end of the list so we write bnode file in same order */
442 for(lb = &allBnodes, nb = *lb; nb; lb = &nb->next, nb = *lb);
448 /* bnode lwp executes this code repeatedly */
450 register afs_int32 code;
451 register struct bnode *tb;
452 register afs_int32 temp;
453 register struct bnode_proc *tp;
455 int options; /* must not be register */
461 /* first figure out how long to sleep for */
462 temp = 0x7fffffff; /* afs_int32 time; maxint doesn't work in select */
464 for(tb = allBnodes; tb; tb=tb->next) {
465 if (tb->flags & BNODE_NEEDTIMEOUT) {
466 if (tb->nextTimeout < temp) {
468 temp = tb->nextTimeout;
472 /* now temp has the time at which we should wakeup next */
475 if (setAny) temp -= FT_ApproxTime(); /* how many seconds until next event */
480 code = IOMGR_Select(0, 0, 0, 0, &tv);
482 else code = 0; /* fake timeout code */
484 /* figure out why we woke up; child exit or timeouts */
485 FT_GetTimeOfDay(&tv, 0); /* must do the real gettimeofday once and a while */
488 /* check all bnodes to see which ones need timeout events */
489 for(tb = allBnodes; tb; tb=nb) {
490 if ((tb->flags & BNODE_NEEDTIMEOUT) && temp > tb->nextTimeout) {
494 if (tb->flags & BNODE_NEEDTIMEOUT) { /* check again, BOP_TIMEOUT could change */
495 tb->nextTimeout = FT_ApproxTime() + tb->period;
498 bnode_Release(tb); /* delete may occur here */
504 /* signalled, probably by incoming signal */
507 bnode_waiting = options | 0x800000;
508 code = waitpid((pid_t)-1, &status, options);
510 if (code == 0 || code == -1) break; /* all done */
511 /* otherwise code has a process id, which we now search for */
512 for(tp=allProcs; tp; tp=tp->next)
513 if (tp->pid == code) break;
519 /* count restarts in last 10 seconds */
520 if (temp > tb->rsTime + 30) {
521 /* it's been 10 seconds we've been counting */
526 if (WIFSIGNALED(status) == 0) {
527 /* exited, not signalled */
528 tp->lastExit = WEXITSTATUS(status);
531 tb->errorCode = tp->lastExit;
532 tb->lastErrorExit = FT_ApproxTime();
533 RememberProcName(tp);
537 bozo_Log("%s:%s exited with code %d",
538 tb->name, tp->coreName, tp->lastExit);
540 bozo_Log("%s exited with code %d",
541 tb->name, tp->lastExit);
544 /* Signal occurred, perhaps spurious due to shutdown request.
545 * If due to a shutdown request, don't overwrite last error
548 tp->lastSignal = WTERMSIG(status);
550 if (tp->lastSignal != SIGQUIT && tp->lastSignal != SIGTERM
551 && tp->lastSignal != SIGKILL) {
552 tb->errorSignal = tp->lastSignal;
553 tb->lastErrorExit = FT_ApproxTime();
554 RememberProcName(tp);
557 bozo_Log("%s:%s exited on signal %d%s",
558 tb->name, tp->coreName, tp->lastSignal,
559 WCOREDUMP(status) ? " (core dumped)" : "");
561 bozo_Log("%s exited on signal %d%s",
562 tb->name, tp->lastSignal,
563 WCOREDUMP(status) ? " (core dumped)" : "");
566 tb->lastAnyExit = FT_ApproxTime();
569 bozo_Log("BNODE: Notifier %s will be called\n", tb->notifier);
572 BOP_PROCEXIT(tb, tp);
575 if (tb->rsCount++ > 10) {
576 /* 10 in 10 seconds */
577 tb->flags |= BNODE_ERRORSTOP;
578 bnode_SetGoal(tb, BSTAT_SHUTDOWN);
579 bozo_Log("BNODE '%s' repeatedly failed to start, perhaps missing executable.\n",
582 bnode_Release(tb); /* bnode delete can happen here */
585 else bnode_stats.weirdPids++;
595 signal(SIGPIPE, SIG_IGN);
596 bozo_Log("Notifier aborted prematurely");
604 struct bnode_proc *tp;
606 #ifndef AFS_NT40_ENV /* NT notifier callout not yet implemented */
607 int code, pid, status;
610 if (stat(tp->bnode->notifier, &tstat)) {
611 bozo_Log("BNODE: Failed to find notifier '%s'; ignored\n", tp->bnode->notifier);
614 if ((pid = fork()) == 0) {
616 struct bnode *tb = tp->bnode;
619 #if defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_SGI51_ENV)
622 #ifdef AFS_LINUX20_ENV
628 fout = popen(tb->notifier, "w");
630 bozo_Log("BNODE: Failed to find notifier '%s'; ignored\n", tb->notifier);
631 perror(tb->notifier);
634 code = SendNotifierData(fileno(fout), tp);
637 } else if (pid < 0) {
638 bozo_Log("Failed to fork creating process to handle notifier '%s'\n", tp->bnode->notifier);
641 #endif /* AFS_NT40_ENV */
646 static afs_int32 SendNotifierData(fd, tp)
648 register struct bnode_proc *tp;
650 register struct bnode *tb = tp->bnode;
651 char buffer[1000], *bufp = buffer, *buf1;
655 * First sent out the bnode_proc struct
657 (void) sprintf(bufp, "BEGIN bnode_proc\n");
658 bufp += strlen(bufp);
659 (void) sprintf(bufp, "comLine: %s\n", tp->comLine);
660 bufp += strlen(bufp);
661 if (!(buf1 = tp->coreName))
663 (void) sprintf(bufp, "coreName: %s\n", buf1);
664 bufp += strlen(bufp);
665 (void) sprintf(bufp, "pid: %ld\n", tp->pid);
666 bufp += strlen(bufp);
667 (void) sprintf(bufp, "lastExit: %ld\n", tp->lastExit);
668 bufp += strlen(bufp);
670 (void) sprintf(bufp, "lastSignal: %ld\n", tp->lastSignal);
671 bufp += strlen(bufp);
673 (void) sprintf(bufp, "flags: %ld\n", tp->flags);
674 bufp += strlen(bufp);
675 (void) sprintf(bufp, "END bnode_proc\n");
676 bufp += strlen(bufp);
677 len =(int)(bufp-buffer);
678 if (write(fd, buffer, len) < 0) {
683 * Now sent out the bnode struct
686 (void) sprintf(bufp, "BEGIN bnode\n");
687 bufp += strlen(bufp);
688 (void) sprintf(bufp, "name: %s\n", tb->name);
689 bufp += strlen(bufp);
690 (void) sprintf(bufp, "rsTime: %ld\n", tb->rsTime);
691 bufp += strlen(bufp);
692 (void) sprintf(bufp, "rsCount: %ld\n", tb->rsCount);
693 bufp += strlen(bufp);
694 (void) sprintf(bufp, "procStartTime: %ld\n", tb->procStartTime);
695 bufp += strlen(bufp);
696 (void) sprintf(bufp, "procStarts: %ld\n", tb->procStarts);
697 bufp += strlen(bufp);
698 (void) sprintf(bufp, "lastAnyExit: %ld\n", tb->lastAnyExit);
699 bufp += strlen(bufp);
700 (void) sprintf(bufp, "lastErrorExit: %ld\n", tb->lastErrorExit);
701 bufp += strlen(bufp);
702 (void) sprintf(bufp, "errorCode: %ld\n", tb->errorCode);
703 bufp += strlen(bufp);
704 (void) sprintf(bufp, "errorSignal: %ld\n", tb->errorSignal);
705 bufp += strlen(bufp);
707 (void) sprintf(bufp, "lastErrorName: %s\n", tb->lastErrorName);
708 bufp += strlen(bufp);
710 (void) sprintf(bufp, "goal: %d\n", tb->goal);
711 bufp += strlen(bufp);
712 (void) sprintf(bufp, "END bnode\n");
713 bufp += strlen(bufp);
714 len = (int)bufp-(int)buffer;
715 if (write(fd, buffer, len) < 0) {
722 /* Called by IOMGR at low priority on IOMGR's stack shortly after a SIGCHLD
723 * occurs. Wakes up bproc do redo things */
724 bnode_SoftInt(int asignal)
726 IOMGR_Cancel(bproc_pid);
730 /* Called at signal interrupt level; queues function to be called
731 * when IOMGR runs again.
734 bnode_Int(int asignal)
736 extern void bozo_ShutdownAndExit();
738 if (asignal == SIGQUIT) {
739 IOMGR_SoftSig(bozo_ShutdownAndExit, (char *)asignal);
741 IOMGR_SoftSig(bnode_SoftInt, (char *)asignal);
746 /* intialize the whole system */
749 register afs_int32 code;
750 struct sigaction newaction;
753 if (initDone) return 0;
755 memset(&bnode_stats, 0, sizeof(bnode_stats));
756 LWP_InitializeProcessSupport(1, &junk); /* just in case */
758 code = LWP_CreateProcess(bproc, BNODE_LWP_STACKSIZE,
759 /* priority */ 1, /* parm */0, "bnode-manager", &bproc_pid);
760 if (code) return code;
761 memset((char *)&newaction, 0, sizeof(newaction));
762 newaction.sa_handler = bnode_Int;
763 code = sigaction(SIGCHLD, &newaction, NULL);
764 if (code) return errno;
765 code = sigaction(SIGQUIT, &newaction, NULL);
766 if (code) return errno;
770 /* free token list returned by parseLine */
771 bnode_FreeTokens(alist)
772 register struct bnode_token *alist; {
773 register struct bnode_token *nlist;
774 for(; alist; alist = nlist) {
784 if (x == 0 || x == ' ' || x == '\t' || x== '\n') return 1;
788 bnode_ParseLine(aline, alist)
790 struct bnode_token **alist; {
794 struct bnode_token *first, *last;
795 register struct bnode_token *ttok;
798 inToken = 0; /* not copying token chars at start */
799 first = (struct bnode_token *) 0;
800 last = (struct bnode_token *) 0;
803 if (tc == 0 || space(tc)) { /* terminating null gets us in here, too */
805 inToken = 0; /* end of this token */
807 ttok = (struct bnode_token *) malloc(sizeof(struct bnode_token));
808 ttok->next = (struct bnode_token *) 0;
809 ttok->key = (char *) malloc(strlen(tbuffer)+1);
810 strcpy(ttok->key, tbuffer);
816 if (!first) first = ttok;
820 /* an alpha character */
825 if (tptr - tbuffer >= sizeof(tbuffer)) return -1; /* token too long */
829 /* last token flushed 'cause space(0) --> true */
830 if (last) last->next = (struct bnode_token *) 0;
838 int bnode_NewProc(abnode, aexecString, coreName, aproc)
839 struct bnode_proc **aproc;
841 struct bnode *abnode;
843 struct bnode_token *tlist, *tt;
845 struct bnode_proc *tp;
847 char *argv[MAXVARGS];
850 code = bnode_ParseLine(aexecString, &tlist); /* try parsing first */
851 if (code) return code;
852 tp = (struct bnode_proc *) malloc(sizeof(struct bnode_proc));
853 memset(tp, 0, sizeof(struct bnode_proc));
858 tp->comLine = aexecString;
859 tp->coreName = coreName; /* may be null */
860 abnode->procStartTime = FT_ApproxTime();
861 abnode->procStarts++;
863 /* convert linked list of tokens into argv structure */
864 for (tt = tlist, i = 0; i < (MAXVARGS - 1) && tt; tt = tt->next, i++) {
867 argv[i] = (char *) 0; /* null-terminated */
869 cpid = spawnprocve(argv[0], argv, environ, -1);
870 osi_audit(BOSSpawnProcEvent, 0, AUD_STR, aexecString, AUD_END );
872 if (cpid == (pid_t)-1) {
873 bozo_Log("Failed to spawn process for bnode '%s'\n", abnode->name);
874 bnode_FreeTokens(tlist);
879 bnode_FreeTokens(tlist);
881 tp->flags = BPROC_STARTED;
882 tp->flags &= ~BPROC_EXITED;
887 int bnode_StopProc(aproc, asignal)
888 register struct bnode_proc *aproc;
891 if (!(aproc->flags & BPROC_STARTED) || (aproc->flags & BPROC_EXITED))
894 osi_audit( BOSStopProcEvent, 0, AUD_STR, (aproc ? aproc->comLine : (char *)0), AUD_END );
896 code = kill(aproc->pid, asignal);
897 bnode_Check(aproc->bnode);
901 int bnode_Deactivate(abnode)
902 register struct bnode *abnode; {
903 register struct bnode **pb, *tb;
905 if (!(abnode->flags & BNODE_ACTIVE)) return BZNOTACTIVE;
906 for(pb = &allBnodes,tb = *pb; tb; tb=nb) {
910 tb->flags &= ~BNODE_ACTIVE;
917 static int DeleteProc(abproc)
918 register struct bnode_proc *abproc; {
919 register struct bnode_proc **pb, *tb;
920 struct bnode_proc *nb;
922 for(pb = &allProcs,tb = *pb; tb; pb = &tb->next, tb=nb) {