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