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