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