bnode-reorganize-source-20030309
[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
31 #ifdef HAVE_STRING_H
32 #include <string.h>
33 #else
34 #ifdef HAVE_STRINGS_H
35 #include <strings.h>
36 #endif
37 #endif
38
39 #include <afs/procmgmt.h>  /* signal(), kill(), wait(), etc. */
40 #include <lwp.h>
41 #include <afs/audit.h>
42 #include <afs/afsutil.h>
43 #include <afs/fileutil.h>
44 #include "bnode.h"
45
46 #if defined(AFS_AIX_ENV) || defined(AFS_SUN4_ENV)
47 /* All known versions of AIX lack WCOREDUMP but this works */
48 #define WCOREDUMP(x) ((x) & 0x80)
49 #endif
50
51 #define BNODE_LWP_STACKSIZE     (16 * 1024)
52
53 int bnode_waiting = 0;
54 static PROCESS bproc_pid;                       /* pid of waker-upper */
55 static struct bnode *allBnodes=0;       /* list of all bnodes */
56 static struct bnode_proc *allProcs=0;   /* list of all processes for which we're waiting */
57 static struct bnode_type *allTypes=0;   /* list of registered type handlers */
58
59 static struct bnode_stats {
60     int weirdPids;
61 } bnode_stats;
62
63 #ifndef AFS_NT40_ENV
64 extern char **environ;                  /* env structure */
65 #endif
66
67 /* Remember the name of the process, if any, that failed last */
68 static void RememberProcName(register struct bnode_proc *ap)
69 {
70     register struct bnode *tbnodep;
71
72     tbnodep = ap->bnode;
73     if (tbnodep->lastErrorName) {
74         free(tbnodep->lastErrorName);
75         tbnodep->lastErrorName = NULL;
76     }
77     if (ap->coreName) {
78         tbnodep->lastErrorName = (char *) malloc(strlen(ap->coreName)+1);
79         strcpy(tbnodep->lastErrorName, ap->coreName);
80     }
81 }
82
83 /* utility for use by BOP_HASCORE functions to determine where a core file might
84  * be stored.
85  */
86 int bnode_CoreName(register struct bnode *abnode, char *acoreName, 
87                    char *abuffer)
88 {
89     strcpy(abuffer, AFSDIR_SERVER_CORELOG_FILEPATH);
90     if (acoreName) {
91         strcat(abuffer, acoreName);
92         strcat(abuffer, ".");
93     }
94     strcat(abuffer, abnode->name);
95     return 0;
96 }
97
98 /* save core file, if any */
99 static void SaveCore(register struct bnode *abnode, register struct bnode_proc
100                      *aproc)
101 {
102     char tbuffer[256];
103     struct stat tstat;
104     register afs_int32 code;
105 #ifdef BOZO_SAVE_CORES
106     struct timeval  Start;
107     struct tm *TimeFields;
108     char FileName[256];
109 #endif
110
111     code = stat(AFSDIR_SERVER_CORELOG_FILEPATH, &tstat);
112     if (code) return;
113     
114     bnode_CoreName(abnode, aproc->coreName, tbuffer);
115 #ifdef BOZO_SAVE_CORES
116     TM_GetTimeOfDay(&Start, 0);
117     TimeFields = localtime(&Start.tv_sec);
118     sprintf(FileName,"%s.%d%02d%02d%02d%02d%02d", tbuffer,
119             TimeFields->tm_year, TimeFields->tm_mon + 1,
120             TimeFields->tm_mday, TimeFields->tm_hour, TimeFields->tm_min,
121             TimeFields->tm_sec);
122     strcpy(tbuffer,FileName);
123 #endif
124     code = renamefile(AFSDIR_SERVER_CORELOG_FILEPATH, tbuffer);
125 }
126
127 int bnode_GetString(register struct bnode *abnode, register char *abuffer,
128 register afs_int32 alen)
129 {
130     return BOP_GETSTRING(abnode, abuffer, alen);
131 }
132
133 int bnode_GetParm(register struct bnode *abnode, register afs_int32 aindex, 
134                   register char *abuffer, afs_int32 alen)
135 {
136     return BOP_GETPARM(abnode, aindex, abuffer, alen);
137 }
138
139 int bnode_GetStat(register struct bnode *abnode, register afs_int32 *astatus)
140 {
141     return BOP_GETSTAT(abnode, astatus);
142 }
143
144 int bnode_RestartP(register struct bnode *abnode)
145 {
146     return BOP_RESTARTP(abnode);
147 }
148
149 static int bnode_Check(register struct bnode *abnode)
150 {
151     if (abnode->flags & BNODE_WAIT) {
152         abnode->flags &= ~BNODE_WAIT;
153         LWP_NoYieldSignal(abnode);
154     }
155     return 0;
156 }
157
158 /* tell if an instance has a core file */
159 int bnode_HasCore(register struct bnode *abnode)
160 {
161     return BOP_HASCORE(abnode);
162 }
163
164 /* wait for all bnodes to stabilize */
165 int bnode_WaitAll() {
166     register struct bnode *tb;
167     register afs_int32 code;
168     afs_int32 stat;
169
170   retry:
171     for(tb = allBnodes; tb; tb=tb->next) {
172         bnode_Hold(tb);
173         code = BOP_GETSTAT(tb, &stat);
174         if (code) {
175             bnode_Release(tb);
176             return code;
177         }
178         if (stat != tb->goal) {
179             tb->flags |= BNODE_WAIT;
180             LWP_WaitProcess(tb);
181             bnode_Release(tb);
182             goto retry;
183         }
184         bnode_Release(tb);
185     }
186     return 0;
187 }
188
189 /* wait until bnode status is correct */
190 int bnode_WaitStatus(register struct bnode *abnode, int astatus)
191 {
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 int bnode_SetStat(register struct bnode *abnode, register int agoal)
217 {
218     abnode->goal = agoal;
219     bnode_Check(abnode);
220     BOP_SETSTAT(abnode, agoal);
221     abnode->flags &= ~BNODE_ERRORSTOP;
222     return 0;
223 }
224
225 int bnode_SetGoal(register struct bnode *abnode, register int agoal)
226 {
227     abnode->goal = agoal;
228     bnode_Check(abnode);
229     return 0;
230 }
231
232 int bnode_SetFileGoal(register struct bnode *abnode, register int agoal)
233 {
234     if (abnode->fileGoal == agoal) return 0;    /* already done */
235     abnode->fileGoal = agoal;
236     WriteBozoFile(0);
237     return 0;
238 }
239
240 /* apply a function to all bnodes in the system */
241 int bnode_ApplyInstance(int (*aproc)(), char *arock)
242 {
243     register struct bnode *tb, *nb;
244     register afs_int32 code;
245
246     for(tb = allBnodes; tb; tb=nb) {
247         nb = tb->next;
248         code = (*aproc) (tb, arock);
249         if (code) return code;
250     }
251     return 0;
252 }
253
254 struct bnode *bnode_FindInstance(register char *aname)
255 {
256     register struct bnode *tb;
257     
258     for(tb=allBnodes;tb;tb=tb->next) {
259         if (!strcmp(tb->name, aname)) return tb;
260     }
261     return NULL;
262 }
263
264 static struct bnode_type *FindType(register char *aname)
265 {
266     register struct bnode_type *tt;
267     
268     for(tt=allTypes;tt;tt=tt->next) {
269         if (!strcmp(tt->name, aname)) return tt;
270     }
271     return (struct bnode_type *) 0;
272 }
273
274 int bnode_Register(char *atype, struct bnode_ops *aprocs, int anparms)
275 {
276     register struct bnode_type *tt;
277
278     for(tt=allTypes;tt;tt=tt->next) {
279         if (!strcmp(tt->name, atype)) break;
280     }
281     if (!tt) {
282         tt = (struct bnode_type *) malloc(sizeof(struct bnode_type));
283         memset(tt, 0, sizeof(struct bnode_type));
284         tt->next = allTypes;
285         allTypes = tt;
286         tt->name = atype;
287     }
288     tt->ops = aprocs;
289     return 0;
290 }
291
292 afs_int32 bnode_Create(char *atype, char *ainstance, struct bnode **abp, 
293                        char *ap1, char *ap2, char *ap3, char *ap4, 
294                        char *ap5, char *notifier, int fileGoal)
295 {
296     struct bnode_type *type;
297     struct bnode *tb;
298     char *notifierpath = NULL;
299     struct stat tstat;
300
301     if (bnode_FindInstance(ainstance)) return BZEXISTS;
302     type = FindType(atype);
303     if (!type) return BZBADTYPE;
304
305     if (notifier && strcmp(notifier, NONOTIFIER)) {
306         /* construct local path from canonical (wire-format) path */
307         if (ConstructLocalBinPath(notifier, &notifierpath)) {
308             bozo_Log("BNODE-Create: Notifier program path invalid '%s'\n", notifier);
309             return BZNOCREATE;
310         }
311
312         if (stat(notifierpath, &tstat)) {
313             bozo_Log("BNODE-Create: Notifier program '%s' not found\n", notifierpath);
314             free(notifierpath);
315             return BZNOCREATE;
316         }
317     }
318     tb = (*type->ops->create)(ainstance, ap1, ap2, ap3, ap4, ap5);
319     if (!tb) {
320         free(notifierpath);
321         return BZNOCREATE;
322     }
323     tb->notifier = notifierpath;
324     *abp = tb;
325     tb->type = type;
326
327     /* The fs_create above calls bnode_InitBnode() which always sets the 
328     ** fileGoal to BSTAT_NORMAL .... overwrite it with whatever is passed into
329     ** this function as a parameter... */
330     tb->fileGoal = fileGoal;
331
332     bnode_SetStat(tb, tb->goal);    /* nudge it once */
333     WriteBozoFile(0);
334     return 0;
335 }
336
337 int bnode_DeleteName(char *ainstance)
338 {
339     register struct bnode *tb;
340
341     tb = bnode_FindInstance(ainstance);
342     if (!tb) return BZNOENT;
343     
344     return bnode_Delete(tb);
345 }
346
347 int bnode_Hold(register struct bnode *abnode)
348 {
349     abnode->refCount++;
350     return 0;
351 }
352
353 int bnode_Release(register struct bnode *abnode)
354 {
355     abnode->refCount--;
356     if (abnode->refCount == 0 && abnode->flags & BNODE_DELETE) {
357         abnode->flags &= ~BNODE_DELETE; /* we're going for it */
358         bnode_Delete(abnode);
359     }
360     return 0;
361 }
362
363 int bnode_Delete(register struct bnode *abnode)
364 {
365     register afs_int32 code;
366     register struct bnode **lb, *ub;
367     afs_int32 temp;
368     
369     if (abnode->refCount != 0) {
370         abnode->flags |= BNODE_DELETE;
371         return 0;
372     }
373
374     /* make sure the bnode is idle before zapping */
375     bnode_Hold(abnode);
376     code = BOP_GETSTAT(abnode, &temp);
377     bnode_Release(abnode);
378     if (code) return code;
379     if (temp != BSTAT_SHUTDOWN) return BZBUSY;
380
381     /* all clear to zap */
382     for(lb = &allBnodes, ub = *lb; ub; lb= &ub->next, ub = *lb) {
383         if (ub == abnode) {
384             /* unthread it from the list */
385             *lb = ub->next;
386             break;
387         }
388     }
389     free(abnode->name); /* do this first, since bnode fields may be bad after BOP_DELETE */
390     code = BOP_DELETE(abnode);  /* don't play games like holding over this one */
391     WriteBozoFile(0);
392     return code;
393 }
394
395 /* function to tell if there's a timeout coming up */
396 int bnode_PendingTimeout(register struct bnode *abnode)
397 {
398     return (abnode->flags & BNODE_NEEDTIMEOUT);
399 }
400
401 /* function called to set / clear periodic bnode wakeup times */
402 int bnode_SetTimeout(register struct bnode *abnode, afs_int32 atimeout)
403 {
404     if (atimeout != 0) {
405         abnode->nextTimeout = FT_ApproxTime() + atimeout;
406         abnode->flags |= BNODE_NEEDTIMEOUT;
407         abnode->period = atimeout;
408         IOMGR_Cancel(bproc_pid);
409     }
410     else {
411         abnode->flags &= ~BNODE_NEEDTIMEOUT;
412     }
413     return 0;
414 }
415
416 /* used by new bnode creation code to format bnode header */
417 int bnode_InitBnode (register struct bnode *abnode, 
418                      struct bnode_ops *abnodeops, char *aname) 
419 {
420     struct bnode **lb, *nb;
421
422     /* format the bnode properly */
423     memset(abnode, 0, sizeof(struct bnode));
424     abnode->ops = abnodeops;
425     abnode->name = (char *) malloc(strlen(aname)+1);
426     if (!abnode->name)
427         return ENOMEM;
428     strcpy(abnode->name, aname);
429     abnode->flags = BNODE_ACTIVE;
430     abnode->fileGoal = BSTAT_NORMAL;
431     abnode->goal = BSTAT_SHUTDOWN;
432
433     /* put the bnode at the end of the list so we write bnode file in same order */
434     for(lb = &allBnodes, nb = *lb; nb; lb = &nb->next, nb = *lb);
435     *lb = abnode;
436
437     return 0;
438 }
439
440 static int DeleteProc(register struct bnode_proc *abproc)
441 {
442     register struct bnode_proc **pb, *tb;
443     struct bnode_proc *nb;
444
445     for(pb = &allProcs,tb = *pb; tb; pb = &tb->next, tb=nb) {
446         nb = tb->next;
447         if (tb == abproc) {
448             *pb = nb;
449             free(tb);
450             return 0;
451         }
452     }
453     return BZNOENT;
454 }
455
456 /* bnode lwp executes this code repeatedly */
457 static int bproc() {
458     register afs_int32 code;
459     register struct bnode *tb;
460     register afs_int32 temp;
461     register struct bnode_proc *tp;
462     struct bnode *nb;
463     int options;   /* must not be register */
464     struct timeval tv;
465     int setAny;
466     int status;
467
468     while (1) {
469         /* first figure out how long to sleep for */
470         temp = 0x7fffffff;          /* afs_int32 time; maxint doesn't work in select */
471         setAny = 0;
472         for(tb = allBnodes; tb; tb=tb->next) {
473             if (tb->flags & BNODE_NEEDTIMEOUT) {
474                 if (tb->nextTimeout < temp) {
475                     setAny = 1;
476                     temp = tb->nextTimeout;
477                 }
478             }
479         }
480         /* now temp has the time at which we should wakeup next */
481
482         /* sleep */
483         if (setAny) temp -= FT_ApproxTime(); /* how many seconds until next event */
484         else temp = 999999;
485         if (temp > 0) {
486             tv.tv_sec = temp;
487             tv.tv_usec = 0;
488             code = IOMGR_Select(0, 0, 0, 0, &tv);
489         }
490         else code = 0;  /* fake timeout code */
491
492         /* figure out why we woke up; child exit or timeouts */
493         FT_GetTimeOfDay(&tv, 0);    /* must do the real gettimeofday once and a while */
494         temp = tv.tv_sec;
495
496         /* check all bnodes to see which ones need timeout events */
497         for(tb = allBnodes; tb; tb=nb) {
498             if ((tb->flags & BNODE_NEEDTIMEOUT) && temp > tb->nextTimeout) {
499                 bnode_Hold(tb);
500                 BOP_TIMEOUT(tb);
501                 bnode_Check(tb);
502                 if (tb->flags & BNODE_NEEDTIMEOUT) {    /* check again, BOP_TIMEOUT could change */
503                     tb->nextTimeout = FT_ApproxTime() + tb->period;
504                 }
505                 nb = tb->next;
506                 bnode_Release(tb);  /* delete may occur here */
507             }
508             else nb=tb->next;
509         }
510
511         if (code < 0) {
512             /* signalled, probably by incoming signal */
513             while (1) {
514                 options = WNOHANG;
515                 bnode_waiting = options | 0x800000;
516                 code = waitpid((pid_t)-1, &status, options);
517                 bnode_waiting = 0;
518                 if (code == 0 || code == -1) break;     /* all done */
519                 /* otherwise code has a process id, which we now search for */
520                 for(tp=allProcs; tp; tp=tp->next)
521                     if (tp->pid == code) break;
522                 if (tp) {
523                     /* found the pid */
524                     tb = tp->bnode;
525                     bnode_Hold(tb);
526
527                     /* count restarts in last 10 seconds */
528                     if (temp > tb->rsTime + 30) {
529                         /* it's been 10 seconds we've been counting */
530                         tb->rsTime = temp;
531                         tb->rsCount = 0;
532                     }
533
534                     if (WIFSIGNALED(status) == 0) {
535                         /* exited, not signalled */
536                         tp->lastExit = WEXITSTATUS(status);
537                         tp->lastSignal = 0;
538                         if (tp->lastExit) {
539                             tb->errorCode = tp->lastExit;
540                             tb->lastErrorExit = FT_ApproxTime();
541                             RememberProcName(tp);
542                             tb->errorSignal = 0;
543                         }
544                         if (tp->coreName)
545                             bozo_Log("%s:%s exited with code %d\n",
546                                 tb->name, tp->coreName, tp->lastExit);
547                         else
548                             bozo_Log("%s exited with code %d\n",
549                                 tb->name, tp->lastExit);
550                     }
551                     else {
552                         /* Signal occurred, perhaps spurious due to shutdown request.
553                          * If due to a shutdown request, don't overwrite last error
554                          * information.
555                          */
556                         tp->lastSignal = WTERMSIG(status);
557                         tp->lastExit = 0;
558                         if (tp->lastSignal != SIGQUIT && tp->lastSignal != SIGTERM
559                             && tp->lastSignal != SIGKILL) {
560                             tb->errorSignal = tp->lastSignal;
561                             tb->lastErrorExit = FT_ApproxTime();
562                             RememberProcName(tp);
563                         }
564                         if (tp->coreName)
565                             bozo_Log("%s:%s exited on signal %d%s\n",
566                                 tb->name, tp->coreName, tp->lastSignal,
567                                 WCOREDUMP(status) ? " (core dumped)" : "");
568                         else
569                             bozo_Log("%s exited on signal %d%s\n",
570                                 tb->name, tp->lastSignal,
571                                 WCOREDUMP(status) ? " (core dumped)" : "");
572                         SaveCore(tb, tp);
573                     }
574                     tb->lastAnyExit = FT_ApproxTime();
575
576                     if (tb->notifier) {
577                         bozo_Log("BNODE: Notifier %s will be called\n", tb->notifier);
578                         hdl_notifier(tp);
579                     }
580                     BOP_PROCEXIT(tb, tp);
581
582                     bnode_Check(tb);
583                     if (tb->rsCount++ > 10) {
584                         /* 10 in 10 seconds */
585                         tb->flags |= BNODE_ERRORSTOP;
586                         bnode_SetGoal(tb, BSTAT_SHUTDOWN);
587                         bozo_Log("BNODE '%s' repeatedly failed to start, perhaps missing executable.\n",
588                                tb->name);
589                     }
590                     bnode_Release(tb);  /* bnode delete can happen here */
591                     DeleteProc(tp);
592                 }
593                 else bnode_stats.weirdPids++;
594             }
595         }
596     }
597 }
598
599 static afs_int32 SendNotifierData(register int fd, 
600                                   register struct bnode_proc *tp)
601 {
602     register struct bnode *tb = tp->bnode;
603     char buffer[1000], *bufp = buffer, *buf1;
604     register int len;
605
606     /*
607      * First sent out the bnode_proc struct
608      */
609     (void) sprintf(bufp, "BEGIN bnode_proc\n");
610     bufp += strlen(bufp);
611     (void) sprintf(bufp, "comLine: %s\n", tp->comLine);
612     bufp += strlen(bufp);
613     if (!(buf1 = tp->coreName))
614         buf1 = "(null)";
615     (void) sprintf(bufp, "coreName: %s\n", buf1);
616     bufp += strlen(bufp);
617     (void) sprintf(bufp, "pid: %ld\n", tp->pid);
618     bufp += strlen(bufp);
619     (void) sprintf(bufp, "lastExit: %ld\n", tp->lastExit);
620     bufp += strlen(bufp);
621 #ifdef notdef
622     (void) sprintf(bufp, "lastSignal: %ld\n", tp->lastSignal);
623     bufp += strlen(bufp);
624 #endif
625     (void) sprintf(bufp, "flags: %ld\n", tp->flags);
626     bufp += strlen(bufp);
627     (void) sprintf(bufp, "END bnode_proc\n");
628     bufp += strlen(bufp);
629     len =(int)(bufp-buffer);
630     if (write(fd, buffer, len) < 0) {
631         return -1;
632     }
633
634     /*
635      * Now sent out the bnode struct
636      */    
637     bufp = buffer;
638     (void) sprintf(bufp, "BEGIN bnode\n");
639     bufp += strlen(bufp);
640     (void) sprintf(bufp, "name: %s\n", tb->name);
641     bufp += strlen(bufp);
642     (void) sprintf(bufp, "rsTime: %ld\n", tb->rsTime);
643     bufp += strlen(bufp);
644     (void) sprintf(bufp, "rsCount: %ld\n", tb->rsCount);
645     bufp += strlen(bufp);
646     (void) sprintf(bufp, "procStartTime: %ld\n", tb->procStartTime);
647     bufp += strlen(bufp);
648     (void) sprintf(bufp, "procStarts: %ld\n", tb->procStarts);
649     bufp += strlen(bufp);
650     (void) sprintf(bufp, "lastAnyExit: %ld\n", tb->lastAnyExit);
651     bufp += strlen(bufp);
652     (void) sprintf(bufp, "lastErrorExit: %ld\n", tb->lastErrorExit);
653     bufp += strlen(bufp);
654     (void) sprintf(bufp, "errorCode: %ld\n", tb->errorCode);
655     bufp += strlen(bufp);
656     (void) sprintf(bufp, "errorSignal: %ld\n", tb->errorSignal);
657     bufp += strlen(bufp);
658 /*
659     (void) sprintf(bufp, "lastErrorName: %s\n", tb->lastErrorName);
660     bufp += strlen(bufp);
661 */
662     (void) sprintf(bufp, "goal: %d\n", tb->goal);
663     bufp += strlen(bufp);
664     (void) sprintf(bufp, "END bnode\n");
665     bufp += strlen(bufp);
666     len = (int)bufp-(int)buffer;
667     if (write(fd, buffer, len) < 0) {
668         return -1;
669     }
670 }
671
672 int hdl_notifier(struct bnode_proc *tp)
673 {
674 #ifndef AFS_NT40_ENV  /* NT notifier callout not yet implemented */
675     int code, pid, status;
676     struct stat tstat;
677
678     if (stat(tp->bnode->notifier, &tstat)) {
679         bozo_Log("BNODE: Failed to find notifier '%s'; ignored\n", tp->bnode->notifier);
680         return(1);
681     }
682     if ((pid = fork()) == 0) {
683         FILE *fout;
684         struct bnode *tb = tp->bnode;
685         int ec;
686
687 #if defined(AFS_HPUX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_SGI51_ENV)
688         ec = setsid();
689 #else
690 #ifdef AFS_LINUX20_ENV
691         ec = setpgrp();
692 #else
693         ec = setpgrp(0,0);
694 #endif
695 #endif
696         fout = popen(tb->notifier, "w");
697         if (fout == NULL) {
698             bozo_Log("BNODE: Failed to find notifier '%s'; ignored\n", tb->notifier);
699             perror(tb->notifier);       
700             exit(1);
701         }
702         code = SendNotifierData(fileno(fout), tp);
703         pclose(fout);
704         exit(0);
705     } else if (pid < 0) {
706         bozo_Log("Failed to fork creating process to handle notifier '%s'\n", tp->bnode->notifier);
707         return -1;
708     }
709 #endif /* AFS_NT40_ENV */
710     return(0);
711 }
712
713 /* Called by IOMGR at low priority on IOMGR's stack shortly after a SIGCHLD
714  * occurs.  Wakes up bproc do redo things */
715 int bnode_SoftInt(int asignal)
716 {
717     IOMGR_Cancel(bproc_pid);
718     return 0;
719 }
720
721 /* Called at signal interrupt level; queues function to be called
722  * when IOMGR runs again.
723  */
724 void bnode_Int(int asignal)
725 {
726     extern void bozo_ShutdownAndExit();
727
728     if (asignal == SIGQUIT) {
729         IOMGR_SoftSig(bozo_ShutdownAndExit, (char *)asignal);
730     } else {
731         IOMGR_SoftSig(bnode_SoftInt, (char *)asignal);
732     }
733 }
734
735
736 /* intialize the whole system */
737 int bnode_Init() {
738     PROCESS junk;
739     register afs_int32 code;
740     struct sigaction newaction;
741     static initDone = 0;
742
743     if (initDone) return 0;
744     initDone = 1;
745     memset(&bnode_stats, 0, sizeof(bnode_stats));
746     LWP_InitializeProcessSupport(1, &junk); /* just in case */
747     IOMGR_Initialize();
748     code = LWP_CreateProcess(bproc, BNODE_LWP_STACKSIZE,
749                              /* priority */ 1, (void *) /* parm */0, 
750                              "bnode-manager", &bproc_pid);
751     if (code) return code;
752     memset((char *)&newaction, 0, sizeof(newaction));
753     newaction.sa_handler = bnode_Int;
754     code = sigaction(SIGCHLD, &newaction, NULL);
755     if (code) return errno;
756     code = sigaction(SIGQUIT, &newaction, NULL);
757     if (code) return errno;
758     return code;
759 }
760     
761 /* free token list returned by parseLine */
762 int bnode_FreeTokens(register struct bnode_token *alist)
763 {
764     register struct bnode_token *nlist;
765     for(; alist; alist = nlist) {
766         nlist = alist->next;
767         free(alist->key);
768         free(alist);
769     }
770     return 0;
771 }
772
773 static int space(int x)
774 {
775     if (x == 0 || x == ' ' || x == '\t' || x== '\n') return 1;
776     else return 0;
777 }
778
779 int bnode_ParseLine(char *aline, struct bnode_token **alist)
780 {
781     char tbuffer[256];
782     register char *tptr;
783     int inToken;
784     struct bnode_token *first, *last;
785     register struct bnode_token *ttok;
786     register int tc;
787     
788     inToken = 0;        /* not copying token chars at start */
789     first = (struct bnode_token *) 0;
790     last = (struct bnode_token *) 0;
791     while (1) {
792         tc = *aline++;
793         if (tc == 0 || space(tc)) {    /* terminating null gets us in here, too */
794             if (inToken) {
795                 inToken = 0;    /* end of this token */
796                 *tptr++ = 0;
797                 ttok = (struct bnode_token *) malloc(sizeof(struct bnode_token));
798                 ttok->next = (struct bnode_token *) 0;
799                 ttok->key = (char *) malloc(strlen(tbuffer)+1);
800                 strcpy(ttok->key, tbuffer);
801                 if (last) {
802                     last->next = ttok;
803                     last = ttok;
804                 }
805                 else last = ttok;
806                 if (!first) first = ttok;
807             }
808         }
809         else {
810             /* an alpha character */
811             if (!inToken) {
812                 tptr = tbuffer;
813                 inToken = 1;
814             }
815             if (tptr - tbuffer >= sizeof(tbuffer)) return -1;   /* token too long */
816             *tptr++ = tc;
817         }
818         if (tc == 0) {
819             /* last token flushed 'cause space(0) --> true */
820             if (last) last->next = (struct bnode_token *) 0;
821             *alist = first;
822             return 0;
823         }
824     }
825 }
826
827 #define MAXVARGS            128
828 int bnode_NewProc(struct bnode *abnode, char *aexecString, char *coreName, 
829                   struct bnode_proc **aproc)
830 {
831     struct bnode_token *tlist, *tt;
832     afs_int32 code;
833     struct bnode_proc *tp;
834     pid_t cpid;
835     char *argv[MAXVARGS];
836     int i;
837
838     code = bnode_ParseLine(aexecString, &tlist);  /* try parsing first */
839     if (code) return code;
840     tp = (struct bnode_proc *) malloc(sizeof(struct bnode_proc));
841     memset(tp, 0, sizeof(struct bnode_proc));
842     tp->next = allProcs;
843     allProcs = tp;
844     *aproc = tp;
845     tp->bnode = abnode;
846     tp->comLine = aexecString;
847     tp->coreName = coreName;    /* may be null */
848     abnode->procStartTime = FT_ApproxTime();
849     abnode->procStarts++;
850
851     /* convert linked list of tokens into argv structure */
852     for (tt = tlist, i = 0; i < (MAXVARGS - 1) && tt; tt = tt->next, i++) {
853         argv[i] = tt->key;
854     }
855     argv[i] = NULL;   /* null-terminated */
856
857     cpid = spawnprocve(argv[0], argv, environ, -1);
858     osi_audit(BOSSpawnProcEvent, 0, AUD_STR, aexecString, AUD_END );
859
860     if (cpid == (pid_t)-1) {
861         bozo_Log("Failed to spawn process for bnode '%s'\n", abnode->name);
862         bnode_FreeTokens(tlist);
863         free(tp);
864         return errno;
865     }
866
867     bnode_FreeTokens(tlist);
868     tp->pid = cpid;
869     tp->flags = BPROC_STARTED;
870     tp->flags &= ~BPROC_EXITED;
871     bnode_Check(abnode);
872     return 0;
873 }
874
875 int bnode_StopProc(register struct bnode_proc *aproc, int asignal)
876 {
877     register int code;
878     if (!(aproc->flags & BPROC_STARTED) || (aproc->flags & BPROC_EXITED))
879         return BZNOTACTIVE;
880
881     osi_audit( BOSStopProcEvent, 0, AUD_STR, (aproc ? aproc->comLine : NULL), AUD_END );
882
883     code = kill(aproc->pid, asignal);
884     bnode_Check(aproc->bnode);
885     return code;
886 }
887
888 int bnode_Deactivate(register struct bnode *abnode)
889 {
890     register struct bnode **pb, *tb;
891     struct bnode *nb;
892     if (!(abnode->flags & BNODE_ACTIVE)) return BZNOTACTIVE;
893     for(pb = &allBnodes,tb = *pb; tb; tb=nb) {
894         nb = tb->next;
895         if (tb == abnode) {
896             *pb = nb;
897             tb->flags &= ~BNODE_ACTIVE;
898             return 0;
899         }
900     }
901     return BZNOENT;
902 }
903