2 * Copyright 2000, International Business Machines Corporation and others.
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
10 #include <afs/param.h>
12 #include <sys/types.h>
23 #include <WINNT/afsevent.h>
26 #include <netinet/in.h>
27 #endif /* AFS_NT40_ENV */
28 #include <afs/cellconfig.h>
31 #include <rx/rx_globals.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>
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;
55 extern int rx_stackSize; /* for rx_SetStackSize macro */
58 static afs_int32 nextRestart;
59 static afs_int32 nextDay;
61 struct ktime bozo_nextRestartKT, bozo_nextDayKT;
68 /* check whether caller is authorized to manage RX statistics */
69 int bozo_rxstat_userok(call)
72 return afsconf_SuperUser(bozo_confdir, call, (char *)0);
75 /* restart bozo process */
78 /* exit with restart code; SCM integrator process will restart bosserver */
79 int status = BOSEXIT_RESTART;
81 /* if noauth flag is set, pass "-noauth" to new bosserver */
82 if (afsconf_GetNoAuthFlag(bozo_confdir)) {
83 status |= BOSEXIT_NOAUTH_FLAG;
85 /* if logging is on, pass "-log" to new bosserver */
87 status |= BOSEXIT_LOGGING_FLAG;
91 /* exec new bosserver process */
95 argv[i] = (char *)AFSDIR_SERVER_BOSVR_FILEPATH;
98 /* if noauth flag is set, pass "-noauth" to new bosserver */
99 if (afsconf_GetNoAuthFlag(bozo_confdir)) {
103 /* if logging is on, pass "-log" to new bosserver */
109 /* null-terminate argument list */
112 /* close random fd's */
113 for (i = 3; i < 64; i++) {
117 execv(argv[0], argv); /* should not return */
119 #endif /* AFS_NT40_ENV */
122 /* make sure a dir exists */
124 register char *adir; {
126 register afs_int32 code;
127 if (stat(adir, &tstat) < 0 || (tstat.st_mode & S_IFMT) != S_IFDIR) {
130 reqPerm = GetRequiredDirPerm (adir);
131 if (reqPerm == -1) reqPerm = 0777;
133 /* underlying filesystem may not support directory protection */
136 code = mkdir(adir, reqPerm);
143 /* create all the bozo dirs */
144 static CreateDirs() {
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);
154 MakeDir(AFSDIR_CLIENT_VICE_DIRPATH);
155 MakeDir(AFSDIR_CLIENT_ETC_DIRPATH);
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 */
163 /* strip the \\n from the end of the line, if it is present */
164 static StripLine(abuffer)
165 register char *abuffer; {
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;
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; {
180 char tbuffer[BOZO_BSSIZE];
181 register afs_int32 code;
183 if (abnode->notifier)
184 fprintf(at->file, "bnode %s %s %d %s\n",
185 abnode->type->name, abnode->name, abnode->fileGoal, abnode->notifier);
187 fprintf(at->file, "bnode %s %s %d\n", abnode->type->name, abnode->name, abnode->fileGoal);
189 code = bnode_GetParm(abnode, i, tbuffer, BOZO_BSSIZE);
191 if (code != BZDOM) return code;
194 fprintf(at->file, "parm %s\n", tbuffer);
196 fprintf(at->file, "end\n");
203 register FILE *tfile;
204 char tbuffer[BOZO_BSSIZE];
206 char *instp, *typep, *notifier, *notp;
207 register afs_int32 code;
208 afs_int32 ktmask, ktday, kthour, ktmin, ktsec;
211 char *parms[MAXPARMS];
213 /* rename BozoInit to BosServer for the user */
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);
220 perror("bosconfig rename");
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;
233 for(code=0;code<MAXPARMS;code++)
234 parms[code] = (char *) 0;
235 instp = typep = notifier = (char *) 0;
237 if (!aname) aname = (char *)bozo_fileName;
238 tfile = fopen(aname, "r");
241 instp = (char *) malloc(BOZO_BSSIZE);
242 typep = (char *) malloc(BOZO_BSSIZE);
243 notifier = notp = (char *) malloc(BOZO_BSSIZE);
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 */
249 if (strncmp(tbuffer, "restarttime", 11) == 0) {
250 code = sscanf(tbuffer, "restarttime %d %d %d %d %d",
251 &ktmask, &ktday, &kthour, &ktmin, &ktsec);
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;
266 if (strncmp(tbuffer, "checkbintime", 12) == 0) {
267 code = sscanf(tbuffer, "checkbintime %d %d %d %d %d",
268 &ktmask, &ktday, &kthour, &ktmin, &ktsec);
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;
283 if (strncmp("bnode", tbuffer, 5) != 0) {
288 code = sscanf(tbuffer, "bnode %s %s %d %s", typep, instp, &goal, notifier);
292 } else if (code == 3)
293 notifier = (char *)0;
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);
303 if (!strncmp(tbuffer, "end", 3)) break;
304 if (strncmp(tbuffer, "parm ", 5)) {
306 goto fail; /* no "parm " either */
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 */
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);
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 */
323 bnode_SetStat(tb, BSTAT_NORMAL); /* set goal, taking effect immediately */
326 bnode_SetStat(tb, BSTAT_SHUTDOWN);
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);
340 /* write a new bozo file */
343 register FILE *tfile;
344 char tbuffer[AFSDIR_PATH_MAX];
345 register afs_int32 code;
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;
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,
360 code = bnode_ApplyInstance(bzwrite, &btemp);
361 if (code || (code = ferror(tfile))) { /* something went wrong */
366 /* close the file, check for errors and snap new file into place */
367 if (fclose(tfile) == EOF) {
371 code = renamefile(tbuffer, aname);
379 static bdrestart(abnode, arock)
380 register struct bnode *abnode;
382 register afs_int32 code;
384 if (abnode->fileGoal != BSTAT_NORMAL || abnode->goal != BSTAT_NORMAL)
385 return 0; /* don't restart stopped bnodes */
387 code = bnode_RestartP(abnode);
389 /* restart the dude */
390 bnode_SetStat(abnode, BSTAT_SHUTDOWN);
391 bnode_WaitStatus(abnode, BSTAT_SHUTDOWN);
392 bnode_SetStat(abnode, BSTAT_NORMAL);
394 bnode_Release(abnode);
395 return 0; /* keep trying all bnodes */
398 #define BOZO_MINSKIP 3600 /* minimum to advance clock */
399 /* lwp to handle system restarts */
400 static BozoDaemon() {
401 register afs_int32 now;
403 /* now initialize the values */
407 now = FT_ApproxTime();
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);
415 /* see if we should do a restart */
416 if (now > nextRestart) {
417 BOZO_ReBozo(0); /* doesn't come back */
420 /* see if we should restart a server */
422 nextDay = ktime_next(&bozo_nextDayKT, BOZO_MINSKIP);
424 /* call the bnode restartp function, and restart all that require it */
425 bnode_ApplyInstance(bdrestart, 0);
431 static tweak_config()
435 int s, sb_max, ipfragttl;
439 f = popen("/usr/sbin/no -o sb_max", "r");
440 s = fscanf(f, "sb_max = %d", &sb_max);
444 f = popen("/usr/sbin/no -o ipfragttl", "r");
445 s = fscanf(f, "ipfragttl = %d", &ipfragttl);
455 sprintf(c, "/usr/sbin/no -o sb_max=%d -o ipfragttl=%d", sb_max, ipfragttl);
462 * This routine causes the calling process to go into the background and
463 * to lose its controlling tty.
465 * It does not close or otherwise alter the standard file descriptors.
467 * It writes warning messages to the standard error output if certain
468 * fundamental errors occur.
470 * This routine requires
472 * #include <sys/types.h>
473 * #include <sys/stat.h>
475 * #include <unistd.h>
476 * #include <stdlib.h>
478 * and has been tested on:
494 * A process is a process group leader if its process ID
495 * (getpid()) and its process group ID (getpgrp()) are the same.
499 * To create a new session (and thereby lose our controlling
500 * terminal) we cannot be a process group leader.
502 * To guarantee we are not a process group leader, we fork and
503 * let the parent process exit.
506 if (getpid() == getpgrp()) {
511 abort(); /* leave footprints */
515 default: /* parent */
522 * By here, we are not a process group leader, so we can make a
523 * new session and become the session leader.
527 pid_t sid = setsid();
530 static char err[] = "bosserver: WARNING: setsid() failed\n";
531 write(STDERR_FILENO, err, sizeof err - 1);
536 * Once we create a new session, the current process is a
537 * session leader without a controlling tty.
539 * On some systems, the first tty device the session leader
540 * opens automatically becomes the controlling tty for the
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.
549 if (getpid() == getpgrp()) {
554 abort(); /* leave footprints */
558 default: /* parent */
565 * check that we no longer have a controlling tty
571 fd = open("/dev/tty", O_RDONLY);
574 static char err[] = "bosserver: WARNING: /dev/tty still attached\n";
576 write(STDERR_FILENO, err, sizeof err - 1);
580 #endif /* ! AFS_NT40_ENV */
582 /* start a process and monitor it */
584 #include "AFS_component_version_number.c"
587 main (argc, argv,envp)
592 struct rx_service *tservice;
593 register afs_int32 code;
594 struct afsconf_dir *tdir;
596 struct ktc_encryptionKey tkey;
599 char namebuf[AFSDIR_PATH_MAX];
602 struct sigaction nsa;
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.
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.
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);
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]);
632 /* Initialize dirpaths */
633 if (!(initAFSDirPath() & AFSDIR_SERVER_PATHS_OK)) {
635 ReportErrorEventAlt(AFSEVT_SVR_NO_INSTALL_DIR, 0, argv[0], 0);
637 fprintf(stderr, "%s: Unable to obtain AFS server directory.\n", argv[0]);
641 /* some path inits */
642 bozo_fileName = AFSDIR_SERVER_BOZCONF_FILEPATH;
644 /* initialize the list of dirpaths that the bosserver has
645 * an interest in monitoring */
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");
657 for(code=1;code<argc;code++) {
658 if (strcmp(argv[code], "-noauth")==0) {
659 /* set noauth flag */
662 else if (strcmp(argv[code], "-log")==0) {
663 /* set extra logging flag */
666 else if (strcmp(argv[code], "-enable_peer_stats")==0) {
667 rx_enablePeerRPCStats();
669 else if (strcmp(argv[code], "-enable_process_stats")==0) {
670 rx_enableProcessRPCStats();
674 /* hack to support help flag */
676 printf("Usage: bosserver [-noauth] [-log] "
677 /* "[-enable_peer_stats] [-enable_process_stats] " */
686 if (geteuid() != 0) {
687 printf("bosserver: must be run as root.\n");
694 printf("bosserver: could not init bnode package, code %d\n", code);
698 bnode_Register("fs", &fsbnode_ops, 3);
699 bnode_Register("simple", &ezbnode_ops, 1);
700 bnode_Register("cron", &cronbnode_ops, 2);
702 /* create useful dirs */
705 /* chdir to AFS log directory */
706 chdir(AFSDIR_SERVER_LOGS_DIRPATH);
708 fputs(AFS_GOVERNMENT_MESSAGE, stdout);
711 /* go into the background and remove our controlling tty */
715 #endif /* ! AFS_NT40_ENV */
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");
723 printf("bosserver: can't initialize log file (%s).\n",
724 AFSDIR_SERVER_BOZLOG_FILEPATH);
728 /* keep log closed normally, so can be removed */
730 fclose(bozo_logFile);
732 /* Write current state of directory permissions to log file */
736 code = rx_Init(htons(AFSCONF_NANNYPORT));
738 bozo_Log("can't initialize rx: code=%d\n",code);
746 code = LWP_CreateProcess(BozoDaemon, BOZO_LWP_STACKSIZE, /* priority */ 1,
747 /* parm */0, "bozo-the-clown", &bozo_pid);
749 /* try to read the key from the config file */
750 tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
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);
758 bozo_Log("failed to get hostname, code %d\n", errno);
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");
766 bzero(tcell.hostAddr, sizeof(tcell.hostAddr)); /* not computed */
767 code = afsconf_SetCellInfo(bozo_confdir, AFSDIR_SERVER_ETC_DIRPATH, &tcell);
769 bozo_Log("could not create cell database in '%s' (code %d), quitting\n", AFSDIR_SERVER_ETC_DIRPATH, code);
772 tdir = afsconf_Open(AFSDIR_SERVER_ETC_DIRPATH);
774 bozo_Log("failed to open newly-created cell database, quitting\n");
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);
785 /* opened the cell databse */
787 code = afsconf_GetKey(tdir, 999, &tkey);
789 /* allow super users to manage RX statistics */
790 rx_SetRxStatUserOk(bozo_rxstat_userok);
792 /* have bcrypt key now */
794 afsconf_SetNoAuthFlag(tdir, noAuth);
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);
801 /* These two lines disallow jumbograms */
802 rx_maxReceiveSize = OLD_MAX_PACKET_SIZE;
803 rxi_nSendFrags = rxi_nRecvFrags = 1;
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) */
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 */
819 bozo_Log(a,b,c,d,e,f)
820 char *a, *b, *c, *d, *e, *f; {
825 strcpy(tdate, ctime(&myTime)); /* copy out of static area asap */
828 /* log normally closed, so can be removed */
830 bozo_logFile=fopen(AFSDIR_SERVER_BOZLOG_FILEPATH, "a");
831 if(bozo_logFile == NULL)
833 printf("bosserver: WARNING: problem with %s", AFSDIR_SERVER_BOZLOG_FILEPATH);
838 fprintf(bozo_logFile, "%s ", tdate);
839 fprintf(bozo_logFile, a, b, c, d, e, f);
840 fflush(bozo_logFile);
843 printf("%s ", tdate);
844 printf(a, b, c, d, e, f);
847 /* close so rm BosLog works */
849 fclose(bozo_logFile);