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[] =
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(void);
56 static int initialize(void);
57 static void usage(void);
58 static void do_reset(void);
59 static RETSIGTYPE bye(int);
60 static RETSIGTYPE dbug_on(int);
61 static RETSIGTYPE dbug_off(int);
62 static RETSIGTYPE sig_dump_db(int);
63 static RETSIGTYPE reset(int);
64 static RETSIGTYPE reap(int);
65 static void read_from_dump(char *dumpfile);
66 static void dump_db(void);
67 static void dump_strings(void);
70 static void detach(void);
73 static short doreset = 0; /* if it becomes 1, perform
76 int nfds; /* max file descriptor for select() */
77 int srv_socket; /* dgram socket for clients
79 int bdump_socket = -1; /* brain dump socket fd
80 (closed most of the time) */
81 fd_set interesting; /* the file descrips we are listening
83 struct sockaddr_in srv_addr; /* address of the socket */
85 Unacked *nacklist = NULL; /* list of packets waiting for ack's */
87 unsigned short hm_port; /* host manager receiver port */
88 unsigned short hm_srv_port; /* host manager server sending port */
90 char *programname; /* set to the basename of argv[0] */
91 char myname[MAXHOSTNAMELEN]; /* my host name */
95 char keytab_file[128];
96 static char tkt5_file[256];
99 char srvtab_file[128];
100 char my_realm[REALM_SZ];
101 static char tkt_file[128];
108 int dump_malloc_stats = 0;
109 unsigned long m_size;
115 struct timeval t_local; /* store current time for other uses */
117 static int dump_db_flag = 0;
118 static int dump_strings_flag = 0;
120 u_long npackets; /* number of packets processed */
121 time_t uptime; /* when we started operations */
123 struct in_addr my_addr;
124 char *bdump_version = "1.2";
127 int bdump_auth_proto = 5;
128 #else /* HAVE_KRB5 */
130 int bdump_auth_proto = 4;
131 #else /* HAVE_KRB4 */
132 int bdump_auth_proto = 0;
133 #endif /* HAVE_KRB4 */
134 #endif /* HAVE_KRB5 */
137 krb5_ccache Z_krb5_ccache;
138 krb5_keyblock *__Zephyr_keyblock;
141 C_Block __Zephyr_session;
149 int nfound; /* #fildes ready on select */
152 int init_from_dump = 0;
154 #ifdef _POSIX_VERSION
155 struct sigaction action;
157 int optchar; /* option processing */
161 sprintf(list_file, "%s/zephyr/%s", SYSCONFDIR, SERVER_LIST_FILE);
163 sprintf(srvtab_file, "%s/zephyr/%s", SYSCONFDIR, ZEPHYR_SRVTAB);
164 sprintf(tkt_file, "%s/zephyr/%s", SYSCONFDIR, ZEPHYR_TKFILE);
167 sprintf(keytab_file, "%s/zephyr/%s", SYSCONFDIR, ZEPHYR_KEYTAB);
168 sprintf(tkt5_file, "FILE:%s/zephyr/%s", SYSCONFDIR, ZEPHYR_TK5FILE);
170 sprintf(acl_dir, "%s/zephyr/%s", SYSCONFDIR, ZEPHYR_ACL_DIR);
171 sprintf(subs_file, "%s/zephyr/%s", SYSCONFDIR, DEFAULT_SUBS_FILE);
174 programname = strrchr(argv[0],'/');
175 programname = (programname) ? programname + 1 : argv[0];
177 /* process arguments */
178 while ((optchar = getopt(argc, argv, "dsnv4f:k:")) != EOF) {
193 strncpy(my_realm, optarg, REALM_SZ);
197 bdump_version = optarg;
204 bdump_auth_proto = 4;
214 /* if there is no readable srvtab and we are not standalone, there
215 is no possible way we can succeed, so we exit */
217 if (access(srvtab_file, R_OK)
222 fprintf(stderr, "NO ZEPHYR SRVTAB (%s) available; exiting\n",
226 /* Use local realm if not specified on command line. */
228 if (krb_get_lrealm(my_realm, 1) != KSUCCESS) {
229 fputs("Couldn't get local Kerberos realm; exiting.\n", stderr);
233 #endif /* HAVE_KRB4 */
241 OPENLOG(programname, LOG_PID, LOG_LOCAL6);
243 #if defined (DEBUG) && 0
245 syslog(LOG_DEBUG, "standalone operation");
249 syslog(LOG_DEBUG, "debugging on");
252 /* set up sockets & my_addr and myname,
253 find other servers and set up server table, initialize queues
254 for retransmits, initialize error tables,
255 set up restricted classes */
257 /* Initialize t_local for other uses */
258 gettimeofday(&t_local, NULL);
264 read_from_dump(dumpfile);
266 /* Seed random number set. */
267 srandom(getpid() ^ time(0));
269 /* chdir to somewhere where a core dump will survive */
270 if (chdir(TEMP_DIRECTORY) != 0)
271 syslog(LOG_ERR, "chdir failed (%m) (execution continuing)");
273 FD_ZERO(&interesting);
274 FD_SET(srv_socket, &interesting);
276 nfds = srv_socket + 1;
279 #ifdef _POSIX_VERSION
281 sigemptyset(&action.sa_mask);
283 action.sa_handler = bye;
284 sigaction(SIGINT, &action, NULL);
285 sigaction(SIGTERM, &action, NULL);
287 action.sa_handler = dbug_on;
288 sigaction(SIGUSR1, &action, NULL);
290 action.sa_handler = dbug_off;
291 sigaction(SIGUSR2, &action, NULL);
293 action.sa_handler = reap;
294 sigaction(SIGCHLD, &action, NULL);
296 action.sa_handler = sig_dump_db;
297 sigaction(SIGFPE, &action, NULL);
300 action.sa_handler = sig_dump_strings;
301 sigaction(SIGEMT, &action, NULL);
304 action.sa_handler = reset;
305 sigaction(SIGHUP, &action, NULL);
308 signal(SIGTERM, bye);
309 signal(SIGUSR1, dbug_on);
310 signal(SIGUSR2, dbug_off);
311 signal(SIGCHLD, reap);
312 signal(SIGFPE, sig_dump_db);
314 signal(SIGEMT, sig_dump_strings);
316 signal(SIGHUP, reset);
317 #endif /* _POSIX_VERSION */
319 syslog(LOG_NOTICE, "Ready for action");
321 /* Reinitialize t_local now that initialization is done. */
322 gettimeofday(&t_local, NULL);
327 malloc_inuse(&m_size);
335 if (dump_strings_flag)
340 readable = interesting;
342 /* when there is input in the queue, we
343 artificially set up to pick up the input */
347 nfound = select(nfds, &readable, NULL, NULL, timer_timeout(&tv));
350 /* Initialize t_local for other uses */
351 gettimeofday(&t_local, (struct timezone *)0);
353 /* don't flame about EINTR, since a SIGUSR1 or SIGUSR2
354 can generate it by interrupting the select */
357 syslog(LOG_WARNING, "select error: %m");
359 if (dump_malloc_stats) {
360 unsigned long foo,histid2;
362 dump_malloc_stats = 0;
363 foo = malloc_inuse(&histid2);
364 printf("Total inuse: %d\n",foo);
365 malloc_list(2,m_size,histid2);
372 /* either we timed out or we were just
373 polling for input. Either way we want to continue
374 the loop, and process the next timeout */
377 if (bdump_socket >= 0 && FD_ISSET(bdump_socket,&readable))
379 else if (msgs_queued() || FD_ISSET(srv_socket, &readable))
382 syslog(LOG_ERR, "select weird?!?!");
387 /* Initialize net stuff.
388 Set up the server array.
389 Initialize the packet ack queues to be empty.
390 Initialize the error tables.
391 Restrict certain classes.
403 krb_set_tkt_string(tkt_file);
408 ZInitialize(); /* set up the library */
410 krb5_cc_resolve(Z_krb5_ctx, tkt5_file, &Z_krb5_ccache);
411 #ifdef HAVE_KRB5_CC_SET_DEFAULT_NAME
412 krb5_cc_set_default_name(Z_krb5_ctx, tkt5_file);
415 /* Hack to make krb5_cc_default do something reasonable */
416 char *env=(char *)malloc(strlen(tkt5_file)+12);
418 sprintf(env, "KRB5CCNAME=%s", tkt5_file);
424 /* Override what Zinitialize set for ZGetRealm() */
426 strcpy(__Zephyr_realm, my_realm);
428 init_zsrv_err_tbl(); /* set up err table */
430 ZSetFD(srv_socket); /* set up the socket as the input fildes */
432 /* set up default strings */
434 class_control = make_string(ZEPHYR_CTL_CLASS, 1);
435 class_admin = make_string(ZEPHYR_ADMIN_CLASS, 1);
436 class_hm = make_string(HM_CTL_CLASS, 1);
437 class_ulogin = make_string(LOGIN_CLASS, 1);
438 class_ulocate = make_string(LOCATE_CLASS, 1);
439 wildcard_instance = make_string(WILDCARD_INSTANCE, 1);
440 empty = make_string("", 0);
442 /* restrict certain classes */
448 * Set up the server and client sockets, and initialize my_addr and myname
456 char hostname[MAXHOSTNAMELEN+1];
459 if (gethostname(hostname, MAXHOSTNAMELEN + 1)) {
460 syslog(LOG_ERR, "no hostname: %m");
463 hp = gethostbyname(hostname);
465 syslog(LOG_ERR, "no gethostbyname repsonse");
466 strncpy(myname, hostname, MAXHOSTNAMELEN);
469 strncpy(myname, hp->h_name, MAXHOSTNAMELEN);
470 memcpy(&my_addr, hp->h_addr, sizeof(hp->h_addr));
472 setservent(1); /* keep file/connection open */
474 memset(&srv_addr, 0, sizeof(srv_addr));
475 srv_addr.sin_family = AF_INET;
476 sp = getservbyname(SERVER_SVCNAME, "udp");
477 srv_addr.sin_port = (sp) ? sp->s_port : SERVER_SVC_FALLBACK;
479 sp = getservbyname(HM_SVCNAME, "udp");
480 hm_port = (sp) ? sp->s_port : HM_SVC_FALLBACK;
482 sp = getservbyname(HM_SRV_SVCNAME, "udp");
483 hm_srv_port = (sp) ? sp->s_port : HM_SRV_SVC_FALLBACK;
485 srv_socket = socket(AF_INET, SOCK_DGRAM, 0);
486 if (srv_socket < 0) {
487 syslog(LOG_ERR, "client_sock failed: %m");
493 /* Prevent Linux from giving us socket errors we don't care about. */
494 setsockopt(srv_socket, SOL_SOCKET, SO_BSDCOMPAT, &on, sizeof(on));
497 if (bind(srv_socket, (struct sockaddr *) &srv_addr,
498 sizeof(srv_addr)) < 0) {
499 syslog(LOG_ERR, "client bind failed: %m");
503 /* set not-blocking */
504 #ifdef _POSIX_VERSION
505 flags = fcntl(srv_socket, F_GETFL);
507 fcntl(srv_socket, F_SETFL, flags);
510 ioctl(srv_socket, FIONBIO, &flags);
518 * print out a usage message.
525 fprintf(stderr, "Usage: %s [-d] [-s] [-n] [-k realm] [-f dumpfile]\n",
528 fprintf(stderr, "Usage: %s [-d] [-n] [-k realm] [-f dumpfile]\n",
535 packets_waiting(void)
537 fd_set readable, initial;
543 FD_SET(srv_socket, &initial);
545 tv.tv_sec = tv.tv_usec = 0;
546 return (select(srv_socket + 1, &readable, NULL, NULL, &tv) > 0);
552 server_shutdown(); /* tell other servers */
554 realm_shutdown(); /* tell other realms */
556 hostm_shutdown(); /* tell our hosts */
561 syslog(LOG_NOTICE, "goodbye (sig %d)", sig);
568 syslog(LOG_DEBUG, "debugging turned on");
570 dump_malloc_stats = 1;
578 syslog(LOG_DEBUG, "debugging turned off");
580 malloc_inuse(&m_size);
585 int fork_for_dump = 0;
587 static void dump_strings(void)
594 sprintf(filename, "%szephyr.strings", TEMP_DIRECTORY);
595 fp = fopen (filename, "w");
597 syslog(LOG_ERR, "can't open strings dump file: %m");
599 dump_strings_flag = 0;
602 syslog(LOG_INFO, "dumping strings to disk");
603 print_string_table(fp);
604 if (fclose(fp) == EOF)
605 syslog(LOG_ERR, "error writing strings dump file");
607 syslog(LOG_INFO, "dump done");
609 dump_strings_flag = 0;
622 /* dump the in-core database to human-readable form on disk */
628 pid = (fork_for_dump) ? fork() : -1;
633 sprintf(filename, "%szephyr.db", TEMP_DIRECTORY);
634 fp = fopen(filename, "w");
636 syslog(LOG_ERR, "can't open dump database");
641 syslog(LOG_INFO, "dumping to disk");
642 server_dump_servers(fp);
644 client_dump_clients(fp);
645 triplet_dump_subs(fp);
646 realm_dump_realms(fp);
647 syslog(LOG_INFO, "dump done");
648 if (fclose(fp) == EOF)
649 syslog(LOG_ERR, "can't close dump db");
660 zdbug((LOG_DEBUG,"reset()"));
671 #ifdef _POSIX_VERSION
677 zdbug((LOG_DEBUG,"reap()"));
679 #ifdef _POSIX_VERSION
680 while ((pid = waitpid(-1, &waitb, WNOHANG)) == 0)
681 { i++; if (i > 10) break; }
683 while ((pid = wait3 (&waitb, WNOHANG, (struct rusage*) 0)) == 0)
684 { i++; if (i > 10) break; }
690 if (WIFSIGNALED(waitb) == 0) {
691 if (WIFEXITED(waitb) != 0) {
692 rlm = realm_get_realm_by_pid(pid);
699 rlm = realm_get_realm_by_pid(pid);
711 #ifdef _POSIX_VERSION
712 sigset_t mask, omask;
717 zdbug((LOG_DEBUG,"do_reset()"));
719 #ifdef _POSIX_VERSION
721 sigaddset(&mask, SIGHUP);
722 sigprocmask(SIG_BLOCK, &mask, &omask);
724 omask = sigblock(sigmask(SIGHUP));
727 /* reset various things in the server's state */
731 syslog(LOG_INFO, "restart completed");
734 #ifdef _POSIX_VERSION
735 sigprocmask(SIG_SETMASK, &omask, (sigset_t *)0);
743 * detach from the terminal
749 /* detach from terminal and fork. */
753 #ifdef _POSIX_VERSION
754 size = sysconf(_SC_OPEN_MAX);
756 size = getdtablesize();
758 /* profiling seems to get confused by fork() */
766 for (i = 0; i < size; i++)
769 i = open("/dev/tty", O_RDWR, 666);
770 #ifdef TIOCNOTTY /* Only necessary on old systems. */
771 ioctl(i, TIOCNOTTY, NULL);
774 #ifdef _POSIX_VERSION
778 #endif /* not DEBUG */
781 read_from_dump(char *dumpfile)
783 /* Not yet implemented. */