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