1 /* This file is part of the Project Athena Zephyr Notification System.
2 * It contains the main loop of the Zephyr server
4 * Created by: John T. Kohl
6 * $Source: /afs/dev.mit.edu/source/repository/athena/lib/zephyr/server/main.c,v $
9 * Copyright (c) 1987,1988,1991 by the Massachusetts Institute of Technology.
10 * For copying and distribution information, see the file
14 #include <zephyr/mit-copyright.h>
16 #include <sys/socket.h>
17 #include <sys/resource.h>
21 static const char rcsid_main_c[] =
22 "$Id: main.c,v 1.69 2001/02/27 04:50:08 zacheiss Exp $";
27 * Server loop for Zephyr.
31 The Zephyr server maintains several linked lists of information.
33 There is an array of servers (otherservers) initialized and maintained
36 Each server descriptor contains a pointer to a linked list of hosts
37 which are ``owned'' by that server. The first server is the ``limbo''
38 server which owns any host which was formerly owned by a dead server.
40 Each of these host list entries has an IP address and a pointer to a
41 linked list of clients on that host.
43 Each client has a sockaddr_in, a list of subscriptions, and possibly
46 In addition, the class manager has copies of the pointers to the
47 clients which are registered with a particular class, the
48 not-yet-acknowledged list has copies of pointers to some clients,
49 and the hostm manager may have copies of pointers to some clients
50 (if the client has not acknowledged a packet after a given timeout).
53 #define EVER (;;) /* don't stop looping */
55 static int do_net_setup __P((void));
56 static int initialize __P((void));
57 static void usage __P((void));
58 static void do_reset __P((void));
59 static RETSIGTYPE bye __P((int));
60 static RETSIGTYPE dbug_on __P((int));
61 static RETSIGTYPE dbug_off __P((int));
62 static RETSIGTYPE sig_dump_db __P((int));
63 static RETSIGTYPE sig_dump_strings __P((int));
64 static RETSIGTYPE reset __P((int));
65 static RETSIGTYPE reap __P((int));
66 static void read_from_dump __P((char *dumpfile));
67 static void dump_db __P((void));
68 static void dump_strings __P((void));
71 static void detach __P((void));
74 static short doreset = 0; /* if it becomes 1, perform
77 int nfds; /* max file descriptor for select() */
78 int srv_socket; /* dgram socket for clients
80 int bdump_socket = -1; /* brain dump socket fd
81 (closed most of the time) */
82 fd_set interesting; /* the file descrips we are listening
84 struct sockaddr_in srv_addr; /* address of the socket */
86 Unacked *nacklist = NULL; /* list of packets waiting for ack's */
88 unsigned short hm_port; /* host manager receiver port */
89 unsigned short hm_srv_port; /* host manager server sending port */
91 char *programname; /* set to the basename of argv[0] */
92 char myname[MAXHOSTNAMELEN]; /* my host name */
96 char srvtab_file[128];
97 char my_realm[REALM_SZ];
98 static char tkt_file[128];
105 int dump_malloc_stats = 0;
106 unsigned long m_size;
112 struct timeval t_local; /* store current time for other uses */
114 static int dump_db_flag = 0;
115 static int dump_strings_flag = 0;
117 u_long npackets; /* number of packets processed */
118 time_t uptime; /* when we started operations */
120 struct in_addr my_addr;
121 char *bdump_version = "1.2";
128 int nfound; /* #fildes ready on select */
131 int init_from_dump = 0;
133 #ifdef _POSIX_VERSION
134 struct sigaction action;
136 int optchar; /* option processing */
140 sprintf(list_file, "%s/zephyr/%s", SYSCONFDIR, SERVER_LIST_FILE);
142 sprintf(srvtab_file, "%s/zephyr/%s", SYSCONFDIR, ZEPHYR_SRVTAB);
143 sprintf(tkt_file, "%s/zephyr/%s", SYSCONFDIR, ZEPHYR_TKFILE);
145 sprintf(acl_dir, "%s/zephyr/%s", SYSCONFDIR, ZEPHYR_ACL_DIR);
146 sprintf(subs_file, "%s/zephyr/%s", SYSCONFDIR, DEFAULT_SUBS_FILE);
149 programname = strrchr(argv[0],'/');
150 programname = (programname) ? programname + 1 : argv[0];
152 /* process arguments */
153 while ((optchar = getopt(argc, argv, "dsnv:f:k:")) != EOF) {
168 strncpy(my_realm, optarg, REALM_SZ);
172 bdump_version = optarg;
186 /* if there is no readable srvtab and we are not standalone, there
187 is no possible way we can succeed, so we exit */
189 if (access(srvtab_file, R_OK)
194 fprintf(stderr, "NO ZEPHYR SRVTAB (%s) available; exiting\n",
198 /* Use local realm if not specified on command line. */
200 if (krb_get_lrealm(my_realm, 1) != KSUCCESS) {
201 fputs("Couldn't get local Kerberos realm; exiting.\n", stderr);
205 #endif /* HAVE_KRB4 */
213 OPENLOG(programname, LOG_PID, LOG_LOCAL6);
215 #if defined (DEBUG) && 0
217 syslog(LOG_DEBUG, "standalone operation");
221 syslog(LOG_DEBUG, "debugging on");
224 /* set up sockets & my_addr and myname,
225 find other servers and set up server table, initialize queues
226 for retransmits, initialize error tables,
227 set up restricted classes */
229 /* Initialize t_local for other uses */
230 gettimeofday(&t_local, NULL);
236 read_from_dump(dumpfile);
238 /* Seed random number set. */
239 srandom(getpid() ^ time(0));
241 /* chdir to somewhere where a core dump will survive */
242 if (chdir(TEMP_DIRECTORY) != 0)
243 syslog(LOG_ERR, "chdir failed (%m) (execution continuing)");
245 FD_ZERO(&interesting);
246 FD_SET(srv_socket, &interesting);
248 nfds = srv_socket + 1;
251 #ifdef _POSIX_VERSION
253 sigemptyset(&action.sa_mask);
255 action.sa_handler = bye;
256 sigaction(SIGINT, &action, NULL);
257 sigaction(SIGTERM, &action, NULL);
259 action.sa_handler = dbug_on;
260 sigaction(SIGUSR1, &action, NULL);
262 action.sa_handler = dbug_off;
263 sigaction(SIGUSR2, &action, NULL);
265 action.sa_handler = reap;
266 sigaction(SIGCHLD, &action, NULL);
268 action.sa_handler = sig_dump_db;
269 sigaction(SIGFPE, &action, NULL);
272 action.sa_handler = sig_dump_strings;
273 sigaction(SIGEMT, &action, NULL);
276 action.sa_handler = reset;
277 sigaction(SIGHUP, &action, NULL);
280 signal(SIGTERM, bye);
281 signal(SIGUSR1, dbug_on);
282 signal(SIGUSR2, dbug_off);
283 signal(SIGCHLD, reap);
284 signal(SIGFPE, sig_dump_db);
286 signal(SIGEMT, sig_dump_strings);
288 signal(SIGHUP, reset);
289 #endif /* _POSIX_VERSION */
291 syslog(LOG_NOTICE, "Ready for action");
293 /* Reinitialize t_local now that initialization is done. */
294 gettimeofday(&t_local, NULL);
297 timer_set_rel(SWEEP_INTERVAL, sweep_ticket_hash_table, NULL);
302 malloc_inuse(&m_size);
310 if (dump_strings_flag)
315 readable = interesting;
317 /* when there is input in the queue, we
318 artificially set up to pick up the input */
322 nfound = select(nfds, &readable, NULL, NULL, timer_timeout(&tv));
325 /* Initialize t_local for other uses */
326 gettimeofday(&t_local, (struct timezone *)0);
328 /* don't flame about EINTR, since a SIGUSR1 or SIGUSR2
329 can generate it by interrupting the select */
332 syslog(LOG_WARNING, "select error: %m");
334 if (dump_malloc_stats) {
335 unsigned long foo,histid2;
337 dump_malloc_stats = 0;
338 foo = malloc_inuse(&histid2);
339 printf("Total inuse: %d\n",foo);
340 malloc_list(2,m_size,histid2);
347 /* either we timed out or we were just
348 polling for input. Either way we want to continue
349 the loop, and process the next timeout */
352 if (bdump_socket >= 0 && FD_ISSET(bdump_socket,&readable))
354 else if (msgs_queued() || FD_ISSET(srv_socket, &readable))
357 syslog(LOG_ERR, "select weird?!?!");
362 /* Initialize net stuff.
363 Set up the server array.
364 Initialize the packet ack queues to be empty.
365 Initialize the error tables.
366 Restrict certain classes.
378 krb_set_tkt_string(tkt_file);
383 ZInitialize(); /* set up the library */
385 /* Override what Zinitialize set for ZGetRealm() */
387 strcpy(__Zephyr_realm, my_realm);
389 init_zsrv_err_tbl(); /* set up err table */
391 ZSetFD(srv_socket); /* set up the socket as the input fildes */
393 /* set up default strings */
395 class_control = make_string(ZEPHYR_CTL_CLASS, 1);
396 class_admin = make_string(ZEPHYR_ADMIN_CLASS, 1);
397 class_hm = make_string(HM_CTL_CLASS, 1);
398 class_ulogin = make_string(LOGIN_CLASS, 1);
399 class_ulocate = make_string(LOCATE_CLASS, 1);
400 wildcard_instance = make_string(WILDCARD_INSTANCE, 1);
401 empty = make_string("", 0);
403 /* restrict certain classes */
409 * Set up the server and client sockets, and initialize my_addr and myname
417 char hostname[MAXHOSTNAMELEN+1];
420 if (gethostname(hostname, MAXHOSTNAMELEN + 1)) {
421 syslog(LOG_ERR, "no hostname: %m");
424 hp = gethostbyname(hostname);
426 syslog(LOG_ERR, "no gethostbyname repsonse");
427 strncpy(myname, hostname, MAXHOSTNAMELEN);
430 strncpy(myname, hp->h_name, MAXHOSTNAMELEN);
431 memcpy(&my_addr, hp->h_addr, sizeof(hp->h_addr));
433 setservent(1); /* keep file/connection open */
435 memset(&srv_addr, 0, sizeof(srv_addr));
436 srv_addr.sin_family = AF_INET;
437 sp = getservbyname(SERVER_SVCNAME, "udp");
438 srv_addr.sin_port = (sp) ? sp->s_port : SERVER_SVC_FALLBACK;
440 sp = getservbyname(HM_SVCNAME, "udp");
441 hm_port = (sp) ? sp->s_port : HM_SVC_FALLBACK;
443 sp = getservbyname(HM_SRV_SVCNAME, "udp");
444 hm_srv_port = (sp) ? sp->s_port : HM_SRV_SVC_FALLBACK;
446 srv_socket = socket(AF_INET, SOCK_DGRAM, 0);
447 if (srv_socket < 0) {
448 syslog(LOG_ERR, "client_sock failed: %m");
451 if (bind(srv_socket, (struct sockaddr *) &srv_addr,
452 sizeof(srv_addr)) < 0) {
453 syslog(LOG_ERR, "client bind failed: %m");
457 /* set not-blocking */
458 #ifdef _POSIX_VERSION
459 flags = fcntl(srv_socket, F_GETFL);
461 fcntl(srv_socket, F_SETFL, flags);
464 ioctl(srv_socket, FIONBIO, &flags);
472 * print out a usage message.
479 fprintf(stderr, "Usage: %s [-d] [-s] [-n] [-k realm] [-f dumpfile]\n",
482 fprintf(stderr, "Usage: %s [-d] [-n] [-k realm] [-f dumpfile]\n",
491 fd_set readable, initial;
497 FD_SET(srv_socket, &initial);
499 tv.tv_sec = tv.tv_usec = 0;
500 return (select(srv_socket + 1, &readable, NULL, NULL, &tv) > 0);
507 server_shutdown(); /* tell other servers */
509 realm_shutdown(); /* tell other realms */
511 hostm_shutdown(); /* tell our hosts */
516 syslog(LOG_NOTICE, "goodbye (sig %d)", sig);
524 syslog(LOG_DEBUG, "debugging turned on");
526 dump_malloc_stats = 1;
535 syslog(LOG_DEBUG, "debugging turned off");
537 malloc_inuse(&m_size);
542 int fork_for_dump = 0;
545 sig_dump_strings(sig)
548 dump_strings_flag = 1;
551 static void dump_strings()
558 sprintf(filename, "%szephyr.strings", TEMP_DIRECTORY);
559 fp = fopen (filename, "w");
561 syslog(LOG_ERR, "can't open strings dump file: %m");
563 dump_strings_flag = 0;
566 syslog(LOG_INFO, "dumping strings to disk");
567 print_string_table(fp);
568 if (fclose(fp) == EOF)
569 syslog(LOG_ERR, "error writing strings dump file");
571 syslog(LOG_INFO, "dump done");
573 dump_strings_flag = 0;
584 static void dump_db()
586 /* dump the in-core database to human-readable form on disk */
592 pid = (fork_for_dump) ? fork() : -1;
597 sprintf(filename, "%szephyr.db", TEMP_DIRECTORY);
598 fp = fopen(filename, "w");
600 syslog(LOG_ERR, "can't open dump database");
605 syslog(LOG_INFO, "dumping to disk");
606 server_dump_servers(fp);
608 client_dump_clients(fp);
609 triplet_dump_subs(fp);
610 realm_dump_realms(fp);
611 syslog(LOG_INFO, "dump done");
612 if (fclose(fp) == EOF)
613 syslog(LOG_ERR, "can't close dump db");
625 zdbug((LOG_DEBUG,"reset()"));
637 #ifdef _POSIX_VERSION
643 zdbug((LOG_DEBUG,"reap()"));
645 #ifdef _POSIX_VERSION
646 while ((pid = waitpid(-1, &waitb, WNOHANG)) == 0)
647 { i++; if (i > 10) break; }
649 while ((pid = wait3 (&waitb, WNOHANG, (struct rusage*) 0)) == 0)
650 { i++; if (i > 10) break; }
656 if (WIFSIGNALED(waitb) == 0) {
657 if (WIFEXITED(waitb) != 0) {
658 rlm = realm_get_realm_by_pid(pid);
665 rlm = realm_get_realm_by_pid(pid);
677 #ifdef _POSIX_VERSION
678 sigset_t mask, omask;
683 zdbug((LOG_DEBUG,"do_reset()"));
685 #ifdef _POSIX_VERSION
687 sigaddset(&mask, SIGHUP);
688 sigprocmask(SIG_BLOCK, &mask, &omask);
690 omask = sigblock(sigmask(SIGHUP));
693 /* reset various things in the server's state */
697 syslog(LOG_INFO, "restart completed");
700 #ifdef _POSIX_VERSION
701 sigprocmask(SIG_SETMASK, &omask, (sigset_t *)0);
709 * detach from the terminal
715 /* detach from terminal and fork. */
719 #ifdef _POSIX_VERSION
720 size = sysconf(_SC_OPEN_MAX);
722 size = getdtablesize();
724 /* profiling seems to get confused by fork() */
732 for (i = 0; i < size; i++)
735 i = open("/dev/tty", O_RDWR, 666);
736 #ifdef TIOCNOTTY /* Only necessary on old systems. */
737 ioctl(i, TIOCNOTTY, NULL);
740 #ifdef _POSIX_VERSION
744 #endif /* not DEBUG */
747 read_from_dump(dumpfile)
750 /* Not yet implemented. */