reindent-again-20030808
[openafs.git] / src / bozo / bosserver.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 <afs/stds.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <fcntl.h>
20 #include <errno.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #ifdef AFS_NT40_ENV
25 #include <winsock2.h>
26 #include <direct.h>
27 #include <io.h>
28 #include <WINNT/afsevent.h>
29 #else
30 #include <unistd.h>
31 #include <netinet/in.h>
32 #include <syslog.h>
33 #endif /* AFS_NT40_ENV */
34 #include <afs/cellconfig.h>
35 #include <rx/rx.h>
36 #include <rx/xdr.h>
37 #include <rx/rx_globals.h>
38 #include "bosint.h"
39 #include "bnode.h"
40 #include <afs/auth.h>
41 #include <afs/keys.h>
42 #include <afs/ktime.h>
43 #include <afs/afsutil.h>
44 #include <afs/fileutil.h>
45 #include <afs/procmgmt.h>       /* signal(), kill(), wait(), etc. */
46 #if defined(AFS_SGI_ENV)
47 #include <afs/afs_args.h>
48 #endif
49
50
51 #define BOZO_LWP_STACKSIZE      16000
52 extern int BOZO_ExecuteRequest();
53 extern int RXSTATS_ExecuteRequest();
54 extern int afsconf_GetKey();
55 extern struct bnode_ops fsbnode_ops, ezbnode_ops, cronbnode_ops;
56
57 void bozo_Log();
58
59 struct afsconf_dir *bozo_confdir = 0;   /* bozo configuration dir */
60 static char *bozo_pid;
61 struct rx_securityClass *bozo_rxsc[3];
62 const char *bozo_fileName;
63 FILE *bozo_logFile;
64 extern int rx_stackSize;        /* for rx_SetStackSize macro */
65
66 int DoLogging = 0;
67 int DoSyslog = 0;
68 #ifndef AFS_NT40_ENV
69 int DoSyslogFacility = LOG_DAEMON;
70 #endif
71 static afs_int32 nextRestart;
72 static afs_int32 nextDay;
73
74 struct ktime bozo_nextRestartKT, bozo_nextDayKT;
75 int bozo_newKTs;
76 #ifdef BOS_RESTRICTED_MODE
77 int bozo_isrestricted = 0;
78 int bozo_restdisable = 0;
79
80 void
81 bozo_insecureme(int sig)
82 {
83     signal(SIGFPE, bozo_insecureme);
84     bozo_isrestricted = 0;
85     bozo_restdisable = 1;
86 }
87 #endif
88
89 struct bztemp {
90     FILE *file;
91 };
92
93 /* check whether caller is authorized to manage RX statistics */
94 int
95 bozo_rxstat_userok(struct rx_call *call)
96 {
97     return afsconf_SuperUser(bozo_confdir, call, NULL);
98 }
99
100 /* restart bozo process */
101 int
102 bozo_ReBozo()
103 {
104 #ifdef AFS_NT40_ENV
105     /* exit with restart code; SCM integrator process will restart bosserver */
106     int status = BOSEXIT_RESTART;
107
108     /* if noauth flag is set, pass "-noauth" to new bosserver */
109     if (afsconf_GetNoAuthFlag(bozo_confdir)) {
110         status |= BOSEXIT_NOAUTH_FLAG;
111     }
112     /* if logging is on, pass "-log" to new bosserver */
113     if (DoLogging) {
114         status |= BOSEXIT_LOGGING_FLAG;
115     }
116     exit(status);
117 #else
118     /* exec new bosserver process */
119     char *argv[4];
120     int i = 0;
121
122     argv[i] = (char *)AFSDIR_SERVER_BOSVR_FILEPATH;
123     i++;
124
125     /* if noauth flag is set, pass "-noauth" to new bosserver */
126     if (afsconf_GetNoAuthFlag(bozo_confdir)) {
127         argv[i] = "-noauth";
128         i++;
129     }
130     /* if logging is on, pass "-log" to new bosserver */
131     if (DoLogging) {
132         argv[i] = "-log";
133         i++;
134     }
135 #ifndef AFS_NT40_ENV
136     /* if syslog logging is on, pass "-syslog" to new bosserver */
137     if (DoSyslog) {
138         char *arg = (char *)malloc(40); /* enough for -syslog=# */
139         if (DoSyslogFacility != LOG_DAEMON) {
140             snprintf(arg, 40, "-syslog=%d", DoSyslogFacility);
141         } else {
142             strcpy(arg, "-syslog");
143         }
144         argv[i] = arg;
145         i++;
146     }
147 #endif
148
149     /* null-terminate argument list */
150     argv[i] = NULL;
151
152     /* close random fd's */
153     for (i = 3; i < 64; i++) {
154         close(i);
155     }
156
157     execv(argv[0], argv);       /* should not return */
158     _exit(1);
159 #endif /* AFS_NT40_ENV */
160 }
161
162 /* make sure a dir exists */
163 static int
164 MakeDir(register char *adir)
165 {
166     struct stat tstat;
167     register afs_int32 code;
168     if (stat(adir, &tstat) < 0 || (tstat.st_mode & S_IFMT) != S_IFDIR) {
169         int reqPerm;
170         unlink(adir);
171         reqPerm = GetRequiredDirPerm(adir);
172         if (reqPerm == -1)
173             reqPerm = 0777;
174 #ifdef AFS_NT40_ENV
175         /* underlying filesystem may not support directory protection */
176         code = mkdir(adir);
177 #else
178         code = mkdir(adir, reqPerm);
179 #endif
180         return code;
181     }
182     return 0;
183 }
184
185 /* create all the bozo dirs */
186 static int
187 CreateDirs()
188 {
189     if ((!strncmp
190          (AFSDIR_USR_DIRPATH, AFSDIR_CLIENT_ETC_DIRPATH,
191           strlen(AFSDIR_USR_DIRPATH)))
192         ||
193         (!strncmp
194          (AFSDIR_USR_DIRPATH, AFSDIR_SERVER_BIN_DIRPATH,
195           strlen(AFSDIR_USR_DIRPATH)))) {
196         MakeDir(AFSDIR_USR_DIRPATH);
197     }
198     if (!strncmp
199         (AFSDIR_SERVER_AFS_DIRPATH, AFSDIR_SERVER_BIN_DIRPATH,
200          strlen(AFSDIR_SERVER_AFS_DIRPATH))) {
201         MakeDir(AFSDIR_SERVER_AFS_DIRPATH);
202     }
203     MakeDir(AFSDIR_SERVER_BIN_DIRPATH);
204     MakeDir(AFSDIR_SERVER_ETC_DIRPATH);
205     MakeDir(AFSDIR_SERVER_LOCAL_DIRPATH);
206     MakeDir(AFSDIR_SERVER_DB_DIRPATH);
207     MakeDir(AFSDIR_SERVER_LOGS_DIRPATH);
208 #ifndef AFS_NT40_ENV
209     if (!strncmp
210         (AFSDIR_CLIENT_VICE_DIRPATH, AFSDIR_CLIENT_ETC_DIRPATH,
211          strlen(AFSDIR_CLIENT_VICE_DIRPATH))) {
212         MakeDir(AFSDIR_CLIENT_VICE_DIRPATH);
213     }
214     MakeDir(AFSDIR_CLIENT_ETC_DIRPATH);
215
216     symlink(AFSDIR_SERVER_THISCELL_FILEPATH, AFSDIR_CLIENT_THISCELL_FILEPATH);
217     symlink(AFSDIR_SERVER_CELLSERVDB_FILEPATH,
218             AFSDIR_CLIENT_CELLSERVDB_FILEPATH);
219 #endif /* AFS_NT40_ENV */
220     return 0;
221 }
222
223 /* strip the \\n from the end of the line, if it is present */
224 static int
225 StripLine(register char *abuffer)
226 {
227     register char *tp;
228
229     tp = abuffer + strlen(abuffer);     /* starts off pointing at the null  */
230     if (tp == abuffer)
231         return 0;               /* null string, no last character to check */
232     tp--;                       /* aim at last character */
233     if (*tp == '\n')
234         *tp = 0;
235     return 0;
236 }
237
238 /* write one bnode's worth of entry into the file */
239 static
240 bzwrite(register struct bnode *abnode, register struct bztemp *at)
241 {
242     register int i;
243     char tbuffer[BOZO_BSSIZE];
244     register afs_int32 code;
245
246     if (abnode->notifier)
247         fprintf(at->file, "bnode %s %s %d %s\n", abnode->type->name,
248                 abnode->name, abnode->fileGoal, abnode->notifier);
249     else
250         fprintf(at->file, "bnode %s %s %d\n", abnode->type->name,
251                 abnode->name, abnode->fileGoal);
252     for (i = 0;; i++) {
253         code = bnode_GetParm(abnode, i, tbuffer, BOZO_BSSIZE);
254         if (code) {
255             if (code != BZDOM)
256                 return code;
257             break;
258         }
259         fprintf(at->file, "parm %s\n", tbuffer);
260     }
261     fprintf(at->file, "end\n");
262     return 0;
263 }
264
265 #define MAXPARMS    20
266 int
267 ReadBozoFile(char *aname)
268 {
269     register FILE *tfile;
270     char tbuffer[BOZO_BSSIZE];
271     register char *tp;
272     char *instp, *typep, *notifier, *notp;
273     register afs_int32 code;
274     afs_int32 ktmask, ktday, kthour, ktmin, ktsec;
275     afs_int32 i, goal;
276     struct bnode *tb;
277     char *parms[MAXPARMS];
278 #ifdef BOS_RESTRICTED_MODE
279     int rmode;
280 #endif
281
282     /* rename BozoInit to BosServer for the user */
283     if (!aname) {
284         /* if BozoInit exists and BosConfig doesn't, try a rename */
285         if (access(AFSDIR_SERVER_BOZINIT_FILEPATH, 0) == 0
286             && access(AFSDIR_SERVER_BOZCONF_FILEPATH, 0) != 0) {
287             code =
288                 renamefile(AFSDIR_SERVER_BOZINIT_FILEPATH,
289                            AFSDIR_SERVER_BOZCONF_FILEPATH);
290             if (code < 0)
291                 perror("bosconfig rename");
292         }
293 #ifdef BOS_NEW_CONFIG
294         if (access(AFSDIR_SERVER_BOZCONFNEW_FILEPATH, 0) == 0) {
295             code =
296                 renamefile(AFSDIR_SERVER_BOZCONFNEW_FILEPATH,
297                            AFSDIR_SERVER_BOZCONF_FILEPATH);
298             if (code < 0)
299                 perror("bosconfig rename");
300         }
301 #endif
302     }
303
304     /* setup default times we want to do restarts */
305     bozo_nextRestartKT.mask = KTIME_HOUR | KTIME_MIN | KTIME_DAY;
306     bozo_nextRestartKT.hour = 4;        /* 4 am */
307     bozo_nextRestartKT.min = 0;
308     bozo_nextRestartKT.day = 0; /* Sunday */
309     bozo_nextDayKT.mask = KTIME_HOUR | KTIME_MIN;
310     bozo_nextDayKT.hour = 5;
311     bozo_nextDayKT.min = 0;
312
313     for (code = 0; code < MAXPARMS; code++)
314         parms[code] = NULL;
315     instp = typep = notifier = NULL;
316     tfile = (FILE *) 0;
317     if (!aname)
318         aname = (char *)bozo_fileName;
319     tfile = fopen(aname, "r");
320     if (!tfile)
321         return 0;               /* -1 */
322     instp = (char *)malloc(BOZO_BSSIZE);
323     typep = (char *)malloc(BOZO_BSSIZE);
324     notifier = notp = (char *)malloc(BOZO_BSSIZE);
325     while (1) {
326         /* ok, read lines giving parms and such from the file */
327         tp = fgets(tbuffer, sizeof(tbuffer), tfile);
328         if (tp == (char *)0)
329             break;              /* all done */
330
331         if (strncmp(tbuffer, "restarttime", 11) == 0) {
332             code =
333                 sscanf(tbuffer, "restarttime %d %d %d %d %d", &ktmask, &ktday,
334                        &kthour, &ktmin, &ktsec);
335             if (code != 5) {
336                 code = -1;
337                 goto fail;
338             }
339             /* otherwise we've read in the proper ktime structure; now assign
340              * it and continue processing */
341             bozo_nextRestartKT.mask = ktmask;
342             bozo_nextRestartKT.day = ktday;
343             bozo_nextRestartKT.hour = kthour;
344             bozo_nextRestartKT.min = ktmin;
345             bozo_nextRestartKT.sec = ktsec;
346             continue;
347         }
348
349         if (strncmp(tbuffer, "checkbintime", 12) == 0) {
350             code =
351                 sscanf(tbuffer, "checkbintime %d %d %d %d %d", &ktmask,
352                        &ktday, &kthour, &ktmin, &ktsec);
353             if (code != 5) {
354                 code = -1;
355                 goto fail;
356             }
357             /* otherwise we've read in the proper ktime structure; now assign
358              * it and continue processing */
359             bozo_nextDayKT.mask = ktmask;       /* time to restart the system */
360             bozo_nextDayKT.day = ktday;
361             bozo_nextDayKT.hour = kthour;
362             bozo_nextDayKT.min = ktmin;
363             bozo_nextDayKT.sec = ktsec;
364             continue;
365         }
366 #ifdef BOS_RESTRICTED_MODE
367         if (strncmp(tbuffer, "restrictmode", 12) == 0) {
368             code = sscanf(tbuffer, "restrictmode %d", &rmode);
369             if (code != 1) {
370                 code = -1;
371                 goto fail;
372             }
373             if (rmode != 0 && rmode != 1) {
374                 code = -1;
375                 goto fail;
376             }
377             bozo_isrestricted = rmode;
378             continue;
379         }
380 #endif
381
382         if (strncmp("bnode", tbuffer, 5) != 0) {
383             code = -1;
384             goto fail;
385         }
386         notifier = notp;
387         code =
388             sscanf(tbuffer, "bnode %s %s %d %s", typep, instp, &goal,
389                    notifier);
390         if (code < 3) {
391             code = -1;
392             goto fail;
393         } else if (code == 3)
394             notifier = NULL;
395
396         for (i = 0; i < MAXPARMS; i++) {
397             /* now read the parms, until we see an "end" line */
398             tp = fgets(tbuffer, sizeof(tbuffer), tfile);
399             if (!tp) {
400                 code = -1;
401                 goto fail;
402             }
403             StripLine(tbuffer);
404             if (!strncmp(tbuffer, "end", 3))
405                 break;
406             if (strncmp(tbuffer, "parm ", 5)) {
407                 code = -1;
408                 goto fail;      /* no "parm " either */
409             }
410             if (!parms[i])      /* make sure there's space */
411                 parms[i] = (char *)malloc(BOZO_BSSIZE);
412             strcpy(parms[i], tbuffer + 5);      /* remember the parameter for later */
413         }
414
415         /* ok, we have the type and parms, now create the object */
416         code =
417             bnode_Create(typep, instp, &tb, parms[0], parms[1], parms[2],
418                          parms[3], parms[4], notifier,
419                          goal ? BSTAT_NORMAL : BSTAT_SHUTDOWN);
420         if (code)
421             goto fail;
422
423         /* bnode created in 'temporarily shutdown' state;
424          * check to see if we are supposed to run this guy,
425          * and if so, start the process up */
426         if (goal) {
427             bnode_SetStat(tb, BSTAT_NORMAL);    /* set goal, taking effect immediately */
428         } else {
429             bnode_SetStat(tb, BSTAT_SHUTDOWN);
430         }
431     }
432     /* all done */
433     code = 0;
434
435   fail:
436     if (instp)
437         free(instp);
438     if (typep)
439         free(typep);
440     for (i = 0; i < MAXPARMS; i++)
441         if (parms[i])
442             free(parms[i]);
443     if (tfile)
444         fclose(tfile);
445     return code;
446 }
447
448 /* write a new bozo file */
449 int
450 WriteBozoFile(char *aname)
451 {
452     register FILE *tfile;
453     char tbuffer[AFSDIR_PATH_MAX];
454     register afs_int32 code;
455     struct bztemp btemp;
456
457     if (!aname)
458         aname = (char *)bozo_fileName;
459     strcpy(tbuffer, aname);
460     strcat(tbuffer, ".NBZ");
461     tfile = fopen(tbuffer, "w");
462     if (!tfile)
463         return -1;
464     btemp.file = tfile;
465 #ifdef BOS_RESTRICTED_MODE
466     fprintf(tfile, "restrictmode %d\n", bozo_isrestricted);
467 #endif
468     fprintf(tfile, "restarttime %d %d %d %d %d\n", bozo_nextRestartKT.mask,
469             bozo_nextRestartKT.day, bozo_nextRestartKT.hour,
470             bozo_nextRestartKT.min, bozo_nextRestartKT.sec);
471     fprintf(tfile, "checkbintime %d %d %d %d %d\n", bozo_nextDayKT.mask,
472             bozo_nextDayKT.day, bozo_nextDayKT.hour, bozo_nextDayKT.min,
473             bozo_nextDayKT.sec);
474     code = bnode_ApplyInstance(bzwrite, &btemp);
475     if (code || (code = ferror(tfile))) {       /* something went wrong */
476         fclose(tfile);
477         unlink(tbuffer);
478         return code;
479     }
480     /* close the file, check for errors and snap new file into place */
481     if (fclose(tfile) == EOF) {
482         unlink(tbuffer);
483         return -1;
484     }
485     code = renamefile(tbuffer, aname);
486     if (code) {
487         unlink(tbuffer);
488         return -1;
489     }
490     return 0;
491 }
492
493 static int
494 bdrestart(register struct bnode *abnode, char *arock)
495 {
496     register afs_int32 code;
497
498     if (abnode->fileGoal != BSTAT_NORMAL || abnode->goal != BSTAT_NORMAL)
499         return 0;               /* don't restart stopped bnodes */
500     bnode_Hold(abnode);
501     code = bnode_RestartP(abnode);
502     if (code) {
503         /* restart the dude */
504         bnode_SetStat(abnode, BSTAT_SHUTDOWN);
505         bnode_WaitStatus(abnode, BSTAT_SHUTDOWN);
506         bnode_SetStat(abnode, BSTAT_NORMAL);
507     }
508     bnode_Release(abnode);
509     return 0;                   /* keep trying all bnodes */
510 }
511
512 #define BOZO_MINSKIP 3600       /* minimum to advance clock */
513 /* lwp to handle system restarts */
514 static int
515 BozoDaemon()
516 {
517     register afs_int32 now;
518
519     /* now initialize the values */
520     bozo_newKTs = 1;
521     while (1) {
522         IOMGR_Sleep(60);
523         now = FT_ApproxTime();
524
525 #ifdef BOS_RESTRICTED_MODE
526         if (bozo_restdisable) {
527             bozo_Log("Restricted mode disabled by signal\n");
528             bozo_restdisable = 0;
529         }
530 #endif
531         if (bozo_newKTs) {      /* need to recompute restart times */
532             bozo_newKTs = 0;    /* done for a while */
533             nextRestart = ktime_next(&bozo_nextRestartKT, BOZO_MINSKIP);
534             nextDay = ktime_next(&bozo_nextDayKT, BOZO_MINSKIP);
535         }
536
537         /* see if we should do a restart */
538         if (now > nextRestart) {
539             SBOZO_ReBozo(0);    /* doesn't come back */
540         }
541
542         /* see if we should restart a server */
543         if (now > nextDay) {
544             nextDay = ktime_next(&bozo_nextDayKT, BOZO_MINSKIP);
545
546             /* call the bnode restartp function, and restart all that require it */
547             bnode_ApplyInstance(bdrestart, 0);
548         }
549     }
550 }
551
552 #ifdef AFS_AIX32_ENV
553 static int
554 tweak_config()
555 {
556     FILE *f;
557     char c[80];
558     int s, sb_max, ipfragttl;
559
560     sb_max = 131072;
561     ipfragttl = 20;
562     f = popen("/usr/sbin/no -o sb_max", "r");
563     s = fscanf(f, "sb_max = %d", &sb_max);
564     fclose(f);
565     if (s < 1)
566         return;
567     f = popen("/usr/sbin/no -o ipfragttl", "r");
568     s = fscanf(f, "ipfragttl = %d", &ipfragttl);
569     fclose(f);
570     if (s < 1)
571         ipfragttl = 20;
572
573     if (sb_max < 131072)
574         sb_max = 131072;
575     if (ipfragttl > 20)
576         ipfragttl = 20;
577
578     sprintf(c, "/usr/sbin/no -o sb_max=%d -o ipfragttl=%d", sb_max,
579             ipfragttl);
580     f = popen(c, "r");
581     fclose(f);
582 }
583 #endif
584
585 /*
586  * This routine causes the calling process to go into the background and
587  * to lose its controlling tty.
588  *
589  * It does not close or otherwise alter the standard file descriptors.
590  *
591  * It writes warning messages to the standard error output if certain
592  * fundamental errors occur.
593  *
594  * This routine requires
595  * 
596  * #include <sys/types.h>
597  * #include <sys/stat.h>
598  * #include <fcntl.h>
599  * #include <unistd.h>
600  * #include <stdlib.h>
601  *
602  * and has been tested on:
603  *
604  * AIX 4.2
605  * Digital Unix 4.0D
606  * HP-UX 11.0
607  * IRIX 6.5
608  * Linux 2.1.125
609  * Solaris 2.5
610  * Solaris 2.6
611  */
612
613 #ifndef AFS_NT40_ENV
614 static void
615 background(void)
616 {
617     /* 
618      * A process is a process group leader if its process ID
619      * (getpid()) and its process group ID (getpgrp()) are the same.
620      */
621
622     /*
623      * To create a new session (and thereby lose our controlling
624      * terminal) we cannot be a process group leader.
625      *
626      * To guarantee we are not a process group leader, we fork and
627      * let the parent process exit.
628      */
629
630     if (getpid() == getpgrp()) {
631         pid_t pid;
632         pid = fork();
633         switch (pid) {
634         case -1:
635             abort();            /* leave footprints */
636             break;
637         case 0:         /* child */
638             break;
639         default:                /* parent */
640             exit(0);
641             break;
642         }
643     }
644
645     /*
646      * By here, we are not a process group leader, so we can make a
647      * new session and become the session leader.
648      */
649
650     {
651         pid_t sid = setsid();
652
653         if (sid == -1) {
654             static char err[] = "bosserver: WARNING: setsid() failed\n";
655             write(STDERR_FILENO, err, sizeof err - 1);
656         }
657     }
658
659     /*
660      * Once we create a new session, the current process is a
661      * session leader without a controlling tty.
662      *
663      * On some systems, the first tty device the session leader
664      * opens automatically becomes the controlling tty for the
665      * session.
666      *
667      * So, to guarantee we do not acquire a controlling tty, we fork
668      * and let the parent process exit.  The child process is not a
669      * session leader, and so it will not acquire a controlling tty
670      * even if it should happen to open a tty device.
671      */
672
673     if (getpid() == getpgrp()) {
674         pid_t pid;
675         pid = fork();
676         switch (pid) {
677         case -1:
678             abort();            /* leave footprints */
679             break;
680         case 0:         /* child */
681             break;
682         default:                /* parent */
683             exit(0);
684             break;
685         }
686     }
687
688     /*
689      * check that we no longer have a controlling tty
690      */
691
692     {
693         int fd;
694
695         fd = open("/dev/tty", O_RDONLY);
696
697         if (fd >= 0) {
698             static char err[] =
699                 "bosserver: WARNING: /dev/tty still attached\n";
700             close(fd);
701             write(STDERR_FILENO, err, sizeof err - 1);
702         }
703     }
704 }
705 #endif /* ! AFS_NT40_ENV */
706
707 /* start a process and monitor it */
708
709 #include "AFS_component_version_number.c"
710
711 int
712 main(int argc, char **argv, char **envp)
713 {
714     struct rx_service *tservice;
715     register afs_int32 code;
716     struct afsconf_dir *tdir;
717     int noAuth = 0;
718     struct ktc_encryptionKey tkey;
719     int i;
720     char namebuf[AFSDIR_PATH_MAX];
721 #ifndef AFS_NT40_ENV
722     int nofork = 0;
723 #endif
724 #ifdef  AFS_AIX32_ENV
725     struct sigaction nsa;
726
727     /* for some reason, this permits user-mode RX to run a lot faster.
728      * we do it here in the bosserver, so we don't have to do it 
729      * individually in each server.
730      */
731     tweak_config();
732
733     /*
734      * The following signal action for AIX is necessary so that in case of a 
735      * crash (i.e. core is generated) we can include the user's data section 
736      * in the core dump. Unfortunately, by default, only a partial core is
737      * generated which, in many cases, isn't too useful.
738      */
739     sigemptyset(&nsa.sa_mask);
740     nsa.sa_handler = SIG_DFL;
741     nsa.sa_flags = SA_FULLDUMP;
742     sigaction(SIGSEGV, &nsa, NULL);
743     sigaction(SIGABRT, &nsa, NULL);
744 #endif
745 #ifdef BOS_RESTRICTED_MODE
746     signal(SIGFPE, bozo_insecureme);
747 #endif
748
749 #ifdef AFS_NT40_ENV
750     /* Initialize winsock */
751     if (afs_winsockInit() < 0) {
752         ReportErrorEventAlt(AFSEVT_SVR_WINSOCK_INIT_FAILED, 0, argv[0], 0);
753         fprintf(stderr, "%s: Couldn't initialize winsock.\n", argv[0]);
754         exit(2);
755     }
756 #endif
757
758     /* Initialize dirpaths */
759     if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
760 #ifdef AFS_NT40_ENV
761         ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
762 #endif
763         fprintf(stderr, "%s: Unable to obtain AFS server directory.\n",
764                 argv[0]);
765         exit(2);
766     }
767
768     /* some path inits */
769     bozo_fileName = AFSDIR_SERVER_BOZCONF_FILEPATH;
770
771     /* initialize the list of dirpaths that the bosserver has
772      * an interest in monitoring */
773     initBosEntryStats();
774
775 #if defined(AFS_SGI_ENV)
776     /* offer some protection if AFS isn't loaded */
777     if (syscall(AFS_SYSCALL, AFSOP_ENDLOG) < 0 && errno == ENOPKG) {
778         printf("bosserver: AFS doesn't appear to be configured in O.S..\n");
779         exit(1);
780     }
781 #endif
782
783     /* parse cmd line */
784     for (code = 1; code < argc; code++) {
785         if (strcmp(argv[code], "-noauth") == 0) {
786             /* set noauth flag */
787             noAuth = 1;
788         } else if (strcmp(argv[code], "-log") == 0) {
789             /* set extra logging flag */
790             DoLogging = 1;
791         }
792 #ifndef AFS_NT40_ENV
793         else if (strcmp(argv[code], "-syslog") == 0) {
794             /* set syslog logging flag */
795             DoSyslog = 1;
796         } else if (strncmp(argv[code], "-syslog=", 8) == 0) {
797             DoSyslog = 1;
798             DoSyslogFacility = atoi(argv[code] + 8);
799         } else if (strcmp(argv[code], "-nofork") == 0) {
800             nofork = 1;
801         }
802 #endif
803         else if (strcmp(argv[code], "-enable_peer_stats") == 0) {
804             rx_enablePeerRPCStats();
805         } else if (strcmp(argv[code], "-enable_process_stats") == 0) {
806             rx_enableProcessRPCStats();
807         }
808 #ifdef BOS_RESTRICTED_MODE
809         else if (strcmp(argv[code], "-restricted") == 0) {
810             bozo_isrestricted = 1;
811         }
812 #endif
813         else {
814
815             /* hack to support help flag */
816
817 #ifndef AFS_NT40_ENV
818             printf("Usage: bosserver [-noauth] [-log] "
819                    "[-syslog[=FACILITY]] "
820                    "[-enable_peer_stats] [-enable_process_stats] "
821                    "[-nofork] " "[-help]\n");
822 #else
823             printf("Usage: bosserver [-noauth] [-log] "
824                    "[-enable_peer_stats] [-enable_process_stats] "
825                    "[-help]\n");
826 #endif
827             fflush(stdout);
828
829             exit(0);
830         }
831     }
832
833 #ifndef AFS_NT40_ENV
834     if (geteuid() != 0) {
835         printf("bosserver: must be run as root.\n");
836         exit(1);
837     }
838 #endif
839
840     code = bnode_Init();
841     if (code) {
842         printf("bosserver: could not init bnode package, code %d\n", code);
843         exit(1);
844     }
845
846     bnode_Register("fs", &fsbnode_ops, 3);
847     bnode_Register("simple", &ezbnode_ops, 1);
848     bnode_Register("cron", &cronbnode_ops, 2);
849
850     /* create useful dirs */
851     CreateDirs();
852
853     /* chdir to AFS log directory */
854     chdir(AFSDIR_SERVER_LOGS_DIRPATH);
855
856 #if 0
857     fputs(AFS_GOVERNMENT_MESSAGE, stdout);
858     fflush(stdout);
859 #endif
860
861     /* go into the background and remove our controlling tty */
862
863 #ifndef AFS_NT40_ENV
864     if (!nofork)
865         background();
866 #endif /* ! AFS_NT40_ENV */
867
868     if (!DoSyslog) {
869         strcpy(namebuf, AFSDIR_BOZLOG_FILE);
870         strcat(namebuf, ".old");
871         renamefile(AFSDIR_BOZLOG_FILE, namebuf);        /* try rename first */
872         bozo_logFile = fopen(AFSDIR_BOZLOG_FILE, "a");
873         if (!bozo_logFile) {
874             printf("bosserver: can't initialize log file (%s).\n",
875                    AFSDIR_SERVER_BOZLOG_FILEPATH);
876             exit(1);
877         }
878         /* keep log closed normally, so can be removed */
879         fclose(bozo_logFile);
880     } else {
881 #ifndef AFS_NT40_ENV
882         openlog("bosserver", LOG_PID, DoSyslogFacility);
883 #endif
884     }
885
886     /* Write current state of directory permissions to log file */
887     DirAccessOK();
888
889     for (i = 0; i < 10; i++) {
890         code = rx_Init(htons(AFSCONF_NANNYPORT));
891         if (code) {
892             bozo_Log("can't initialize rx: code=%d\n", code);
893             sleep(3);
894         } else
895             break;
896     }
897     if (i >= 10) {
898         bozo_Log("Bos giving up, can't initialize rx\n");
899         exit(code);
900     }
901
902     code = LWP_CreateProcess(BozoDaemon, BOZO_LWP_STACKSIZE, /* priority */ 1,
903                              (void *) /*parm */ 0, "bozo-the-clown",
904                              &bozo_pid);
905
906     /* try to read the key from the config file */
907     tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
908     if (!tdir) {
909         /* try to create local cell config file */
910         struct afsconf_cell tcell;
911         strcpy(tcell.name, "localcell");
912         tcell.numServers = 1;
913         code = gethostname(tcell.hostName[0], MAXHOSTCHARS);
914         if (code) {
915             bozo_Log("failed to get hostname, code %d\n", errno);
916             exit(1);
917         }
918         if (tcell.hostName[0][0] == 0) {
919             bozo_Log("host name not set, can't start\n");
920             bozo_Log("try the 'hostname' command\n");
921             exit(1);
922         }
923         memset(tcell.hostAddr, 0, sizeof(tcell.hostAddr));      /* not computed */
924         code =
925             afsconf_SetCellInfo(bozo_confdir, AFSDIR_SERVER_ETC_DIRPATH,
926                                 &tcell);
927         if (code) {
928             bozo_Log
929                 ("could not create cell database in '%s' (code %d), quitting\n",
930                  AFSDIR_SERVER_ETC_DIRPATH, code);
931             exit(1);
932         }
933         tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
934         if (!tdir) {
935             bozo_Log
936                 ("failed to open newly-created cell database, quitting\n");
937             exit(1);
938         }
939     }
940
941     /* read init file, starting up programs */
942     if (code = ReadBozoFile(0)) {
943         bozo_Log
944             ("bosserver: Something is wrong (%d) with the bos configuration file %s; aborting\n",
945              code, AFSDIR_SERVER_BOZCONF_FILEPATH);
946         exit(code);
947     }
948
949     /* opened the cell databse */
950     bozo_confdir = tdir;
951     code = afsconf_GetKey(tdir, 999, &tkey);
952
953     /* allow super users to manage RX statistics */
954     rx_SetRxStatUserOk(bozo_rxstat_userok);
955
956     /* have bcrypt key now */
957
958     afsconf_SetNoAuthFlag(tdir, noAuth);
959
960     bozo_rxsc[0] = rxnull_NewServerSecurityObject();
961     bozo_rxsc[1] = (struct rx_securityClass *)0;
962     bozo_rxsc[2] =
963         rxkad_NewServerSecurityObject(0, tdir, afsconf_GetKey, NULL);
964
965     /* Disable jumbograms */
966     rx_SetNoJumbo();
967
968     tservice = rx_NewService( /* port */ 0, /* service id */ 1,
969                              /*service name */ "bozo",
970                              /* security classes */
971                              bozo_rxsc,
972                              /* numb sec classes */ 3, BOZO_ExecuteRequest);
973     rx_SetMinProcs(tservice, 2);
974     rx_SetMaxProcs(tservice, 4);
975     rx_SetStackSize(tservice, BOZO_LWP_STACKSIZE);      /* so gethostbyname works (in cell stuff) */
976
977     tservice =
978         rx_NewService(0, RX_STATS_SERVICE_ID, "rpcstats", bozo_rxsc, 3,
979                       RXSTATS_ExecuteRequest);
980     rx_SetMinProcs(tservice, 2);
981     rx_SetMaxProcs(tservice, 4);
982     rx_StartServer(1);          /* donate this process */
983 }
984
985 void
986 bozo_Log(char *a, char *b, char *c, char *d, char *e, char *f)
987 {
988     char tdate[26];
989     time_t myTime;
990
991     if (DoSyslog) {
992 #ifndef AFS_NT40_ENV
993         syslog(LOG_INFO, a, b, c, d, e, f);
994 #endif
995     } else {
996         myTime = time(0);
997         strcpy(tdate, ctime(&myTime));  /* copy out of static area asap */
998         tdate[24] = ':';
999
1000         /* log normally closed, so can be removed */
1001
1002         bozo_logFile = fopen(AFSDIR_SERVER_BOZLOG_FILEPATH, "a");
1003         if (bozo_logFile == NULL) {
1004             printf("bosserver: WARNING: problem with %s",
1005                    AFSDIR_SERVER_BOZLOG_FILEPATH);
1006             fflush(stdout);
1007         }
1008
1009         if (bozo_logFile) {
1010             fprintf(bozo_logFile, "%s ", tdate);
1011             fprintf(bozo_logFile, a, b, c, d, e, f);
1012             fflush(bozo_logFile);
1013         } else {
1014             printf("%s ", tdate);
1015             printf(a, b, c, d, e, f);
1016         }
1017
1018         /* close so rm BosLog works */
1019         fclose(bozo_logFile);
1020     }
1021 }