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