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