255edb968ad2f0115baac5a9a16e330d33bee1fa
[openafs.git] / src / bozo / bnode.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID("$Header$");
14
15 #include <stddef.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <errno.h>
19 #include <sys/types.h>
20 #ifdef AFS_NT40_ENV
21 #include <io.h>
22 #else
23 #include <sys/file.h>
24 #include <sys/time.h>
25 #endif
26 #ifdef BOZO_SAVE_CORES
27 #include <time.h>
28 #endif
29 #include <sys/stat.h>
30 #include <afs/procmgmt.h>  /* signal(), kill(), wait(), etc. */
31 #include <lwp.h>
32 #include <afs/audit.h>
33 #include <afs/afsutil.h>
34 #include <afs/fileutil.h>
35 #include "bnode.h"
36
37 #ifdef AFS_AIX_ENV
38 /* All known versions of AIX lack WCOREDUMP but this works */
39 #define WCOREDUMP(x) ((x) & 0x80)
40 #endif
41
42 #define BNODE_LWP_STACKSIZE     (16 * 1024)
43
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 */
49
50 static struct bnode_stats {
51     int weirdPids;
52 } bnode_stats;
53
54 static afs_int32 SendNotifierData();
55 static int DeleteProc();
56
57 #ifndef AFS_NT40_ENV
58 extern char **environ;                  /* env structure */
59 #endif
60
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;
65
66     tbnodep = ap->bnode;
67     if (tbnodep->lastErrorName) {
68         free(tbnodep->lastErrorName);
69         tbnodep->lastErrorName = (char *) 0;
70     }
71     if (ap->coreName) {
72         tbnodep->lastErrorName = (char *) malloc(strlen(ap->coreName)+1);
73         strcpy(tbnodep->lastErrorName, ap->coreName);
74     }
75 }
76
77 /* utility for use by BOP_HASCORE functions to determine where a core file might
78  * be stored.
79  */
80 bnode_CoreName(abnode, acoreName, abuffer)
81 register struct bnode *abnode;
82 char *acoreName;
83 char *abuffer; {
84     strcpy(abuffer, AFSDIR_SERVER_CORELOG_FILEPATH);
85     if (acoreName) {
86         strcat(abuffer, acoreName);
87         strcat(abuffer, ".");
88     }
89     strcat(abuffer, abnode->name);
90     return 0;
91 }
92
93 /* save core file, if any */
94 static void SaveCore(abnode, aproc)
95 register struct bnode_proc *aproc;
96 register struct bnode *abnode; {
97     char tbuffer[256];
98     struct stat tstat;
99     register afs_int32 code;
100 #ifdef BOZO_SAVE_CORES
101     struct timeval  Start;
102     struct tm *TimeFields;
103     char FileName[256];
104 #endif
105
106     code = stat(AFSDIR_SERVER_CORELOG_FILEPATH, &tstat);
107     if (code) return;
108     
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,
116             TimeFields->tm_sec);
117     strcpy(tbuffer,FileName);
118 #endif
119     code = renamefile(AFSDIR_SERVER_CORELOG_FILEPATH, tbuffer);
120 }
121
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);
127 }
128
129 bnode_GetParm(abnode, aindex, abuffer, alen)
130 register struct bnode *abnode;
131 register afs_int32 aindex;
132 register char *abuffer;
133 afs_int32 alen; {
134     return BOP_GETPARM(abnode, aindex, abuffer, alen);
135 }
136
137 bnode_GetStat(abnode, astatus)
138 register struct bnode *abnode;
139 register afs_int32 *astatus; {
140     return BOP_GETSTAT(abnode, astatus);
141 }
142
143 bnode_RestartP(abnode)
144 register struct bnode *abnode; {
145     return BOP_RESTARTP(abnode);
146 }
147
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);
153     }
154     return 0;
155 }
156
157 /* tell if an instance has a core file */
158 bnode_HasCore(abnode)
159 register struct bnode *abnode; {
160     return BOP_HASCORE(abnode);
161 }
162
163 /* wait for all bnodes to stabilize */
164 bnode_WaitAll() {
165     register struct bnode *tb;
166     register afs_int32 code;
167     afs_int32 stat;
168
169   retry:
170     for(tb = allBnodes; tb; tb=tb->next) {
171         bnode_Hold(tb);
172         code = BOP_GETSTAT(tb, &stat);
173         if (code) {
174             bnode_Release(tb);
175             return code;
176         }
177         if (stat != tb->goal) {
178             tb->flags |= BNODE_WAIT;
179             LWP_WaitProcess(tb);
180             bnode_Release(tb);
181             goto retry;
182         }
183         bnode_Release(tb);
184     }
185     return 0;
186 }
187
188 /* wait until bnode status is correct */
189 bnode_WaitStatus(abnode, astatus)
190 int astatus;
191 register struct bnode *abnode; {
192     register afs_int32 code;
193     afs_int32 stat;
194
195     bnode_Hold(abnode);
196     while (1) {
197         /* get the status */
198         code = BOP_GETSTAT(abnode, &stat);
199         if (code) return code;
200
201         /* otherwise, check if we're done */
202         if (stat == astatus) {
203             bnode_Release(abnode);
204             return 0;       /* done */
205         }
206         if (astatus != abnode->goal) {
207             bnode_Release(abnode);
208             return -1;  /* no longer our goal, don't keep waiting */
209         }
210         /* otherwise, block */
211         abnode->flags |= BNODE_WAIT;
212         LWP_WaitProcess(abnode);
213     }
214 }
215
216 bnode_SetStat(abnode, agoal)
217 register struct bnode *abnode;
218 register int agoal; {
219     abnode->goal = agoal;
220     bnode_Check(abnode);
221     BOP_SETSTAT(abnode, agoal);
222     abnode->flags &= ~BNODE_ERRORSTOP;
223     return 0;
224 }
225
226 bnode_SetGoal(abnode, agoal)
227 register struct bnode *abnode;
228 register int agoal; {
229     abnode->goal = agoal;
230     bnode_Check(abnode);
231     return 0;
232 }
233
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;
239     WriteBozoFile(0);
240     return 0;
241 }
242
243 /* apply a function to all bnodes in the system */
244 int bnode_ApplyInstance(aproc, arock)
245 int (*aproc)();
246 char *arock; {
247     register struct bnode *tb, *nb;
248     register afs_int32 code;
249
250     for(tb = allBnodes; tb; tb=nb) {
251         nb = tb->next;
252         code = (*aproc) (tb, arock);
253         if (code) return code;
254     }
255     return 0;
256 }
257
258 struct bnode *bnode_FindInstance (aname)
259 register char *aname; {
260     register struct bnode *tb;
261     
262     for(tb=allBnodes;tb;tb=tb->next) {
263         if (!strcmp(tb->name, aname)) return tb;
264     }
265     return (struct bnode *) 0;
266 }
267
268 static struct bnode_type *FindType(aname)
269 register char *aname; {
270     register struct bnode_type *tt;
271     
272     for(tt=allTypes;tt;tt=tt->next) {
273         if (!strcmp(tt->name, aname)) return tt;
274     }
275     return (struct bnode_type *) 0;
276 }
277
278 bnode_Register(atype, aprocs, anparms)
279 char *atype;
280 int anparms;        /* number of parms to create */
281 struct bnode_ops *aprocs; {
282     register struct bnode_type *tt;
283
284     for(tt=allTypes;tt;tt=tt->next) {
285         if (!strcmp(tt->name, atype)) break;
286     }
287     if (!tt) {
288         tt = (struct bnode_type *) malloc(sizeof(struct bnode_type));
289         memset(tt, 0, sizeof(struct bnode_type));
290         tt->next = allTypes;
291         allTypes = tt;
292         tt->name = atype;
293     }
294     tt->ops = aprocs;
295     return 0;
296 }
297
298 afs_int32 bnode_Create(atype, ainstance, abp, ap1, ap2, ap3, ap4, ap5, notifier,fileGoal)
299 char *atype;
300 char *ainstance;
301 struct bnode **abp;
302 char *ap1, *ap2, *ap3, *ap4, *ap5, *notifier; 
303 int fileGoal;{
304     struct bnode_type *type;
305     struct bnode *tb;
306     char *notifierpath = NULL;
307     struct stat tstat;
308
309     if (bnode_FindInstance(ainstance)) return BZEXISTS;
310     type = FindType(atype);
311     if (!type) return BZBADTYPE;
312
313     if (notifier && strcmp(notifier, NONOTIFIER)) {
314         /* construct local path from canonical (wire-format) path */
315         if (ConstructLocalBinPath(notifier, &notifierpath)) {
316             bozo_Log("BNODE-Create: Notifier program path invalid '%s'\n", notifier);
317             return BZNOCREATE;
318         }
319
320         if (stat(notifierpath, &tstat)) {
321             bozo_Log("BNODE-Create: Notifier program '%s' not found\n", notifierpath);
322             free(notifierpath);
323             return BZNOCREATE;
324         }
325     }
326     tb = (*type->ops->create)(ainstance, ap1, ap2, ap3, ap4, ap5);
327     if (!tb) {
328         free(notifierpath);
329         return BZNOCREATE;
330     }
331     tb->notifier = notifierpath;
332     *abp = tb;
333     tb->type = type;
334
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;
339
340     bnode_SetStat(tb, tb->goal);    /* nudge it once */
341     WriteBozoFile(0);
342     return 0;
343 }
344
345 int bnode_DeleteName(ainstance)
346 char *ainstance; {
347     register struct bnode *tb;
348
349     tb = bnode_FindInstance(ainstance);
350     if (!tb) return BZNOENT;
351     
352     return bnode_Delete(tb);
353 }
354
355 bnode_Hold(abnode)
356 register struct bnode *abnode; {
357     abnode->refCount++;
358     return 0;
359 }
360
361 bnode_Release(abnode)
362 register struct bnode *abnode; {
363     abnode->refCount--;
364     if (abnode->refCount == 0 && abnode->flags & BNODE_DELETE) {
365         abnode->flags &= ~BNODE_DELETE; /* we're going for it */
366         bnode_Delete(abnode);
367     }
368     return 0;
369 }
370
371 int bnode_Delete(abnode)
372 register struct bnode *abnode; {
373     register afs_int32 code;
374     register struct bnode **lb, *ub;
375     afs_int32 temp;
376     
377     if (abnode->refCount != 0) {
378         abnode->flags |= BNODE_DELETE;
379         return 0;
380     }
381
382     /* make sure the bnode is idle before zapping */
383     bnode_Hold(abnode);
384     code = BOP_GETSTAT(abnode, &temp);
385     bnode_Release(abnode);
386     if (code) return code;
387     if (temp != BSTAT_SHUTDOWN) return BZBUSY;
388
389     /* all clear to zap */
390     for(lb = &allBnodes, ub = *lb; ub; lb= &ub->next, ub = *lb) {
391         if (ub == abnode) {
392             /* unthread it from the list */
393             *lb = ub->next;
394             break;
395         }
396     }
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 */
399     WriteBozoFile(0);
400     return code;
401 }
402
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);
407 }
408
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; {
413     if (atimeout != 0) {
414         abnode->nextTimeout = FT_ApproxTime() + atimeout;
415         abnode->flags |= BNODE_NEEDTIMEOUT;
416         abnode->period = atimeout;
417         IOMGR_Cancel(bproc_pid);
418     }
419     else {
420         abnode->flags &= ~BNODE_NEEDTIMEOUT;
421     }
422     return 0;
423 }
424
425 /* used by new bnode creation code to format bnode header */
426 int bnode_InitBnode (abnode, abnodeops, aname)
427 register struct bnode *abnode;
428 char *aname;
429 struct bnode_ops *abnodeops; {
430     struct bnode **lb, *nb;
431
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;
440
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);
443     *lb = abnode;
444
445     return 0;
446 }
447
448 /* bnode lwp executes this code repeatedly */
449 static int bproc() {
450     register afs_int32 code;
451     register struct bnode *tb;
452     register afs_int32 temp;
453     register struct bnode_proc *tp;
454     struct bnode *nb;
455     int options;   /* must not be register */
456     struct timeval tv;
457     int setAny;
458     int status;
459
460     while (1) {
461         /* first figure out how long to sleep for */
462         temp = 0x7fffffff;          /* afs_int32 time; maxint doesn't work in select */
463         setAny = 0;
464         for(tb = allBnodes; tb; tb=tb->next) {
465             if (tb->flags & BNODE_NEEDTIMEOUT) {
466                 if (tb->nextTimeout < temp) {
467                     setAny = 1;
468                     temp = tb->nextTimeout;
469                 }
470             }
471         }
472         /* now temp has the time at which we should wakeup next */
473
474         /* sleep */
475         if (setAny) temp -= FT_ApproxTime(); /* how many seconds until next event */
476         else temp = 999999;
477         if (temp > 0) {
478             tv.tv_sec = temp;
479             tv.tv_usec = 0;
480             code = IOMGR_Select(0, 0, 0, 0, &tv);
481         }
482         else code = 0;  /* fake timeout code */
483
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 */
486         temp = tv.tv_sec;
487
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) {
491                 bnode_Hold(tb);
492                 BOP_TIMEOUT(tb);
493                 bnode_Check(tb);
494                 if (tb->flags & BNODE_NEEDTIMEOUT) {    /* check again, BOP_TIMEOUT could change */
495                     tb->nextTimeout = FT_ApproxTime() + tb->period;
496                 }
497                 nb = tb->next;
498                 bnode_Release(tb);  /* delete may occur here */
499             }
500             else nb=tb->next;
501         }
502
503         if (code < 0) {
504             /* signalled, probably by incoming signal */
505             while (1) {
506                 options = WNOHANG;
507                 bnode_waiting = options | 0x800000;
508                 code = waitpid((pid_t)-1, &status, options);
509                 bnode_waiting = 0;
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;
514                 if (tp) {
515                     /* found the pid */
516                     tb = tp->bnode;
517                     bnode_Hold(tb);
518
519                     /* count restarts in last 10 seconds */
520                     if (temp > tb->rsTime + 30) {
521                         /* it's been 10 seconds we've been counting */
522                         tb->rsTime = temp;
523                         tb->rsCount = 0;
524                     }
525
526                     if (WIFSIGNALED(status) == 0) {
527                         /* exited, not signalled */
528                         tp->lastExit = WEXITSTATUS(status);
529                         tp->lastSignal = 0;
530                         if (tp->lastExit) {
531                             tb->errorCode = tp->lastExit;
532                             tb->lastErrorExit = FT_ApproxTime();
533                             RememberProcName(tp);
534                             tb->errorSignal = 0;
535                         }
536                         if (tp->coreName)
537                             bozo_Log("%s:%s exited with code %d",
538                                 tb->name, tp->coreName, tp->lastExit);
539                         else
540                             bozo_Log("%s exited with code %d",
541                                 tb->name, tp->lastExit);
542                     }
543                     else {
544                         /* Signal occurred, perhaps spurious due to shutdown request.
545                          * If due to a shutdown request, don't overwrite last error
546                          * information.
547                          */
548                         tp->lastSignal = WTERMSIG(status);
549                         tp->lastExit = 0;
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);
555                         }
556                         if (tp->coreName)
557                             bozo_Log("%s:%s exited on signal %d%s",
558                                 tb->name, tp->coreName, tp->lastSignal,
559                                 WCOREDUMP(status) ? " (core dumped)" : "");
560                         else
561                             bozo_Log("%s exited on signal %d%s",
562                                 tb->name, tp->lastSignal,
563                                 WCOREDUMP(status) ? " (core dumped)" : "");
564                         SaveCore(tb, tp);
565                     }
566                     tb->lastAnyExit = FT_ApproxTime();
567
568                     if (tb->notifier) {
569                         bozo_Log("BNODE: Notifier %s will be called\n", tb->notifier);
570                         hdl_notifier(tp);
571                     }
572                     BOP_PROCEXIT(tb, tp);
573
574                     bnode_Check(tb);
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",
580                                tb->name);
581                     }
582                     bnode_Release(tb);  /* bnode delete can happen here */
583                     DeleteProc(tp);
584                 }
585                 else bnode_stats.weirdPids++;
586             }
587         }
588     }
589 }
590
591
592 #ifdef  notdef
593 sigcatch()
594 {
595     signal(SIGPIPE, SIG_IGN);
596     bozo_Log("Notifier aborted prematurely");
597     exit(0);
598 }
599 #endif
600
601
602
603 hdl_notifier(tp) 
604     struct bnode_proc *tp;
605 {
606 #ifndef AFS_NT40_ENV  /* NT notifier callout not yet implemented */
607     int code, pid, status;
608     struct stat tstat;
609
610     if (stat(tp->bnode->notifier, &tstat)) {
611         bozo_Log("BNODE: Failed to find notifier '%s'; ignored\n", tp->bnode->notifier);
612         return(1);
613     }
614     if ((pid = fork()) == 0) {
615         FILE *fout;
616         struct bnode *tb = tp->bnode;
617         int ec;
618
619 #if defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_SGI51_ENV)
620         ec = setsid();
621 #else
622 #ifdef AFS_LINUX20_ENV
623         ec = setpgrp();
624 #else
625         ec = setpgrp(0,0);
626 #endif
627 #endif
628         fout = popen(tb->notifier, "w");
629         if (fout == NULL) {
630             bozo_Log("BNODE: Failed to find notifier '%s'; ignored\n", tb->notifier);
631             perror(tb->notifier);       
632             exit(1);
633         }
634         code = SendNotifierData(fileno(fout), tp);
635         pclose(fout);
636         exit(0);
637     } else if (pid < 0) {
638         bozo_Log("Failed to fork creating process to handle notifier '%s'\n", tp->bnode->notifier);
639         return -1;
640     }
641 #endif /* AFS_NT40_ENV */
642     return(0);
643 }
644
645
646 static afs_int32 SendNotifierData(fd, tp)
647     register int fd;
648     register struct bnode_proc *tp;
649 {
650     register struct bnode *tb = tp->bnode;
651     char buffer[1000], *bufp = buffer, *buf1;
652     register int len;
653
654     /*
655      * First sent out the bnode_proc struct
656      */
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))
662         buf1 = "(null)";
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);
669 #ifdef notdef
670     (void) sprintf(bufp, "lastSignal: %ld\n", tp->lastSignal);
671     bufp += strlen(bufp);
672 #endif
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) {
679         return -1;
680     }
681
682     /*
683      * Now sent out the bnode struct
684      */    
685     bufp = buffer;
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);
706 /*
707     (void) sprintf(bufp, "lastErrorName: %s\n", tb->lastErrorName);
708     bufp += strlen(bufp);
709 */
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) {
716         return -1;
717     }
718 }
719
720
721     
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)
725 {
726     IOMGR_Cancel(bproc_pid);
727     return 0;
728 }
729
730 /* Called at signal interrupt level; queues function to be called
731  * when IOMGR runs again.
732  */
733 void
734 bnode_Int(int asignal)
735 {
736     extern void bozo_ShutdownAndExit();
737
738     if (asignal == SIGQUIT) {
739         IOMGR_SoftSig(bozo_ShutdownAndExit, (char *)asignal);
740     } else {
741         IOMGR_SoftSig(bnode_SoftInt, (char *)asignal);
742     }
743 }
744
745
746 /* intialize the whole system */
747 int bnode_Init() {
748     PROCESS junk;
749     register afs_int32 code;
750     struct sigaction newaction;
751     static initDone = 0;
752
753     if (initDone) return 0;
754     initDone = 1;
755     memset(&bnode_stats, 0, sizeof(bnode_stats));
756     LWP_InitializeProcessSupport(1, &junk); /* just in case */
757     IOMGR_Initialize();
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;
767     return code;
768 }
769     
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) {
775         nlist = alist->next;
776         free(alist->key);
777         free(alist);
778     }
779     return 0;
780 }
781
782 static space(x)
783 int x; {
784     if (x == 0 || x == ' ' || x == '\t' || x== '\n') return 1;
785     else return 0;
786 }
787
788 bnode_ParseLine(aline, alist)
789     char *aline;
790     struct bnode_token **alist; {
791     char tbuffer[256];
792     register char *tptr;
793     int inToken;
794     struct bnode_token *first, *last;
795     register struct bnode_token *ttok;
796     register int tc;
797     
798     inToken = 0;        /* not copying token chars at start */
799     first = (struct bnode_token *) 0;
800     last = (struct bnode_token *) 0;
801     while (1) {
802         tc = *aline++;
803         if (tc == 0 || space(tc)) {    /* terminating null gets us in here, too */
804             if (inToken) {
805                 inToken = 0;    /* end of this token */
806                 *tptr++ = 0;
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);
811                 if (last) {
812                     last->next = ttok;
813                     last = ttok;
814                 }
815                 else last = ttok;
816                 if (!first) first = ttok;
817             }
818         }
819         else {
820             /* an alpha character */
821             if (!inToken) {
822                 tptr = tbuffer;
823                 inToken = 1;
824             }
825             if (tptr - tbuffer >= sizeof(tbuffer)) return -1;   /* token too long */
826             *tptr++ = tc;
827         }
828         if (tc == 0) {
829             /* last token flushed 'cause space(0) --> true */
830             if (last) last->next = (struct bnode_token *) 0;
831             *alist = first;
832             return 0;
833         }
834     }
835 }
836
837 #define MAXVARGS            128
838 int bnode_NewProc(abnode, aexecString, coreName, aproc)
839 struct bnode_proc **aproc;
840 char *coreName;
841 struct bnode *abnode;
842 char *aexecString; {
843     struct bnode_token *tlist, *tt;
844     afs_int32 code;
845     struct bnode_proc *tp;
846     pid_t cpid;
847     char *argv[MAXVARGS];
848     int i;
849
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));
854     tp->next = allProcs;
855     allProcs = tp;
856     *aproc = tp;
857     tp->bnode = abnode;
858     tp->comLine = aexecString;
859     tp->coreName = coreName;    /* may be null */
860     abnode->procStartTime = FT_ApproxTime();
861     abnode->procStarts++;
862
863     /* convert linked list of tokens into argv structure */
864     for (tt = tlist, i = 0; i < (MAXVARGS - 1) && tt; tt = tt->next, i++) {
865         argv[i] = tt->key;
866     }
867     argv[i] = (char *) 0;   /* null-terminated */
868
869     cpid = spawnprocve(argv[0], argv, environ, -1);
870     osi_audit(BOSSpawnProcEvent, 0, AUD_STR, aexecString, AUD_END );
871
872     if (cpid == (pid_t)-1) {
873         bozo_Log("Failed to spawn process for bnode '%s'\n", abnode->name);
874         bnode_FreeTokens(tlist);
875         free(tp);
876         return errno;
877     }
878
879     bnode_FreeTokens(tlist);
880     tp->pid = cpid;
881     tp->flags = BPROC_STARTED;
882     tp->flags &= ~BPROC_EXITED;
883     bnode_Check(abnode);
884     return 0;
885 }
886
887 int bnode_StopProc(aproc, asignal)
888 register struct bnode_proc *aproc;
889 int asignal; {
890     register int code;
891     if (!(aproc->flags & BPROC_STARTED) || (aproc->flags & BPROC_EXITED))
892         return BZNOTACTIVE;
893
894     osi_audit( BOSStopProcEvent, 0, AUD_STR, (aproc ? aproc->comLine : (char *)0), AUD_END );
895
896     code = kill(aproc->pid, asignal);
897     bnode_Check(aproc->bnode);
898     return code;
899 }
900
901 int bnode_Deactivate(abnode)
902 register struct bnode *abnode; {
903     register struct bnode **pb, *tb;
904     struct bnode *nb;
905     if (!(abnode->flags & BNODE_ACTIVE)) return BZNOTACTIVE;
906     for(pb = &allBnodes,tb = *pb; tb; tb=nb) {
907         nb = tb->next;
908         if (tb == abnode) {
909             *pb = nb;
910             tb->flags &= ~BNODE_ACTIVE;
911             return 0;
912         }
913     }
914     return BZNOENT;
915 }
916
917 static int DeleteProc(abproc)
918 register struct bnode_proc *abproc; {
919     register struct bnode_proc **pb, *tb;
920     struct bnode_proc *nb;
921
922     for(pb = &allProcs,tb = *pb; tb; pb = &tb->next, tb=nb) {
923         nb = tb->next;
924         if (tb == abproc) {
925             *pb = nb;
926             free(tb);
927             return 0;
928         }
929     }
930     return BZNOENT;
931 }