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 2345 2009-03-24 03:59:25Z kcr $";
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 static char tkt_file[128];
102 #if defined(HAVE_KRB4) || defined(HAVE_KRB5)
103 char my_realm[REALM_SZ];
110 int dump_malloc_stats = 0;
111 unsigned long m_size;
117 struct timeval t_local; /* store current time for other uses */
119 static int dump_db_flag = 0;
120 static int dump_strings_flag = 0;
122 u_long npackets; /* number of packets processed */
123 time_t uptime; /* when we started operations */
125 struct in_addr my_addr;
126 char *bdump_version = "1.2";
129 int bdump_auth_proto = 5;
130 #else /* HAVE_KRB5 */
132 int bdump_auth_proto = 4;
133 #else /* HAVE_KRB4 */
134 int bdump_auth_proto = 0;
135 #endif /* HAVE_KRB4 */
136 #endif /* HAVE_KRB5 */
139 krb5_ccache Z_krb5_ccache;
140 krb5_keyblock *__Zephyr_keyblock;
143 C_Block __Zephyr_session;
151 int nfound; /* #fildes ready on select */
154 int init_from_dump = 0;
156 #ifdef _POSIX_VERSION
157 struct sigaction action;
159 int optchar; /* option processing */
163 sprintf(list_file, "%s/zephyr/%s", SYSCONFDIR, SERVER_LIST_FILE);
165 sprintf(srvtab_file, "%s/zephyr/%s", SYSCONFDIR, ZEPHYR_SRVTAB);
166 sprintf(tkt_file, "%s/zephyr/%s", SYSCONFDIR, ZEPHYR_TKFILE);
169 sprintf(keytab_file, "%s/zephyr/%s", SYSCONFDIR, ZEPHYR_KEYTAB);
170 sprintf(tkt5_file, "FILE:%s/zephyr/%s", SYSCONFDIR, ZEPHYR_TK5FILE);
172 sprintf(acl_dir, "%s/zephyr/%s", SYSCONFDIR, ZEPHYR_ACL_DIR);
173 sprintf(subs_file, "%s/zephyr/%s", SYSCONFDIR, DEFAULT_SUBS_FILE);
176 programname = strrchr(argv[0],'/');
177 programname = (programname) ? programname + 1 : argv[0];
179 /* process arguments */
180 while ((optchar = getopt(argc, argv, "dsnv4f:k:")) != EOF) {
194 #if defined(HAVE_KRB4) || defined(HAVE_KRB5)
195 strncpy(my_realm, optarg, REALM_SZ);
199 bdump_version = optarg;
206 bdump_auth_proto = 4;
216 /* if there is no readable srvtab and we are not standalone, there
217 is no possible way we can succeed, so we exit */
219 if (access(srvtab_file, R_OK)
224 fprintf(stderr, "NO ZEPHYR SRVTAB (%s) available; exiting\n",
228 /* Use local realm if not specified on command line. */
230 if (krb_get_lrealm(my_realm, 1) != KSUCCESS) {
231 fputs("Couldn't get local Kerberos realm; exiting.\n", stderr);
235 #endif /* HAVE_KRB4 */
243 OPENLOG(programname, LOG_PID, LOG_LOCAL6);
245 #if defined (DEBUG) && 0
247 syslog(LOG_DEBUG, "standalone operation");
250 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);
299 action.sa_handler = reset;
300 sigaction(SIGHUP, &action, NULL);
303 signal(SIGTERM, bye);
304 signal(SIGUSR1, dbug_on);
305 signal(SIGUSR2, dbug_off);
306 signal(SIGCHLD, reap);
307 signal(SIGFPE, sig_dump_db);
308 signal(SIGHUP, reset);
309 #endif /* _POSIX_VERSION */
311 syslog(LOG_NOTICE, "Ready for action");
313 /* Reinitialize t_local now that initialization is done. */
314 gettimeofday(&t_local, NULL);
319 malloc_inuse(&m_size);
327 if (dump_strings_flag)
332 readable = interesting;
334 /* when there is input in the queue, we
335 artificially set up to pick up the input */
339 nfound = select(nfds, &readable, NULL, NULL, timer_timeout(&tv));
342 /* Initialize t_local for other uses */
343 gettimeofday(&t_local, (struct timezone *)0);
345 /* don't flame about EINTR, since a SIGUSR1 or SIGUSR2
346 can generate it by interrupting the select */
349 syslog(LOG_WARNING, "select error: %m");
351 if (dump_malloc_stats) {
352 unsigned long foo,histid2;
354 dump_malloc_stats = 0;
355 foo = malloc_inuse(&histid2);
356 printf("Total inuse: %d\n",foo);
357 malloc_list(2,m_size,histid2);
364 /* either we timed out or we were just
365 polling for input. Either way we want to continue
366 the loop, and process the next timeout */
369 if (bdump_socket >= 0 && FD_ISSET(bdump_socket,&readable))
371 else if (msgs_queued() || FD_ISSET(srv_socket, &readable))
374 syslog(LOG_ERR, "select weird?!?!");
379 /* Initialize net stuff.
380 Set up the server array.
381 Initialize the packet ack queues to be empty.
382 Initialize the error tables.
383 Restrict certain classes.
395 krb_set_tkt_string(tkt_file);
400 ZInitialize(); /* set up the library */
402 krb5_cc_resolve(Z_krb5_ctx, tkt5_file, &Z_krb5_ccache);
403 #ifdef HAVE_KRB5_CC_SET_DEFAULT_NAME
404 krb5_cc_set_default_name(Z_krb5_ctx, tkt5_file);
407 /* Hack to make krb5_cc_default do something reasonable */
408 char *env=(char *)malloc(strlen(tkt5_file)+12);
410 sprintf(env, "KRB5CCNAME=%s", tkt5_file);
415 #if defined(HAVE_KRB4) || defined(HAVE_KRB5)
416 /* Override what Zinitialize set for ZGetRealm() */
418 strcpy(__Zephyr_realm, my_realm);
420 init_zsrv_err_tbl(); /* set up err table */
422 ZSetFD(srv_socket); /* set up the socket as the input fildes */
424 /* set up default strings */
426 class_control = make_string(ZEPHYR_CTL_CLASS, 1);
427 class_admin = make_string(ZEPHYR_ADMIN_CLASS, 1);
428 class_hm = make_string(HM_CTL_CLASS, 1);
429 class_ulogin = make_string(LOGIN_CLASS, 1);
430 class_ulocate = make_string(LOCATE_CLASS, 1);
431 wildcard_instance = make_string(WILDCARD_INSTANCE, 1);
432 empty = make_string("", 0);
434 /* restrict certain classes */
440 * Set up the server and client sockets, and initialize my_addr and myname
448 char hostname[MAXHOSTNAMELEN+1];
451 if (gethostname(hostname, MAXHOSTNAMELEN + 1)) {
452 syslog(LOG_ERR, "no hostname: %m");
455 hp = gethostbyname(hostname);
457 syslog(LOG_ERR, "no gethostbyname repsonse");
458 strncpy(myname, hostname, MAXHOSTNAMELEN);
461 strncpy(myname, hp->h_name, MAXHOSTNAMELEN);
462 memcpy(&my_addr, hp->h_addr, sizeof(hp->h_addr));
464 setservent(1); /* keep file/connection open */
466 memset(&srv_addr, 0, sizeof(srv_addr));
467 srv_addr.sin_family = AF_INET;
468 sp = getservbyname(SERVER_SVCNAME, "udp");
469 srv_addr.sin_port = (sp) ? sp->s_port : SERVER_SVC_FALLBACK;
471 sp = getservbyname(HM_SVCNAME, "udp");
472 hm_port = (sp) ? sp->s_port : HM_SVC_FALLBACK;
474 sp = getservbyname(HM_SRV_SVCNAME, "udp");
475 hm_srv_port = (sp) ? sp->s_port : HM_SRV_SVC_FALLBACK;
477 srv_socket = socket(AF_INET, SOCK_DGRAM, 0);
478 if (srv_socket < 0) {
479 syslog(LOG_ERR, "client_sock failed: %m");
482 if (bind(srv_socket, (struct sockaddr *) &srv_addr,
483 sizeof(srv_addr)) < 0) {
484 syslog(LOG_ERR, "client bind failed: %m");
488 /* set not-blocking */
489 #ifdef _POSIX_VERSION
490 flags = fcntl(srv_socket, F_GETFL);
492 fcntl(srv_socket, F_SETFL, flags);
495 ioctl(srv_socket, FIONBIO, &flags);
503 * print out a usage message.
510 fprintf(stderr, "Usage: %s [-d] [-s] [-n] [-k realm] [-f dumpfile]\n",
513 fprintf(stderr, "Usage: %s [-d] [-n] [-k realm] [-f dumpfile]\n",
520 packets_waiting(void)
522 fd_set readable, initial;
528 FD_SET(srv_socket, &initial);
530 tv.tv_sec = tv.tv_usec = 0;
531 return (select(srv_socket + 1, &readable, NULL, NULL, &tv) > 0);
537 server_shutdown(); /* tell other servers */
539 realm_shutdown(); /* tell other realms */
541 hostm_shutdown(); /* tell our hosts */
546 syslog(LOG_NOTICE, "goodbye (sig %d)", sig);
553 syslog(LOG_DEBUG, "debugging turned on");
555 dump_malloc_stats = 1;
563 syslog(LOG_DEBUG, "debugging turned off");
565 malloc_inuse(&m_size);
570 int fork_for_dump = 0;
572 static void dump_strings(void)
579 sprintf(filename, "%szephyr.strings", TEMP_DIRECTORY);
580 fp = fopen (filename, "w");
582 syslog(LOG_ERR, "can't open strings dump file: %m");
584 dump_strings_flag = 0;
587 syslog(LOG_INFO, "dumping strings to disk");
588 print_string_table(fp);
589 if (fclose(fp) == EOF)
590 syslog(LOG_ERR, "error writing strings dump file");
592 syslog(LOG_INFO, "dump done");
594 dump_strings_flag = 0;
607 /* dump the in-core database to human-readable form on disk */
613 pid = (fork_for_dump) ? fork() : -1;
618 sprintf(filename, "%szephyr.db", TEMP_DIRECTORY);
619 fp = fopen(filename, "w");
621 syslog(LOG_ERR, "can't open dump database");
626 syslog(LOG_INFO, "dumping to disk");
627 server_dump_servers(fp);
629 client_dump_clients(fp);
630 triplet_dump_subs(fp);
631 realm_dump_realms(fp);
632 syslog(LOG_INFO, "dump done");
633 if (fclose(fp) == EOF)
634 syslog(LOG_ERR, "can't close dump db");
644 zdbug((LOG_DEBUG,"reset()"));
654 #ifdef _POSIX_VERSION
660 zdbug((LOG_DEBUG,"reap()"));
661 #ifdef _POSIX_VERSION
662 while ((pid = waitpid(-1, &waitb, WNOHANG)) == 0)
663 { i++; if (i > 10) break; }
665 while ((pid = wait3 (&waitb, WNOHANG, (struct rusage*) 0)) == 0)
666 { i++; if (i > 10) break; }
672 if (WIFSIGNALED(waitb) == 0) {
673 if (WIFEXITED(waitb) != 0) {
674 rlm = realm_get_realm_by_pid(pid);
681 rlm = realm_get_realm_by_pid(pid);
693 #ifdef _POSIX_VERSION
694 sigset_t mask, omask;
698 #ifdef _POSIX_VERSION
700 sigaddset(&mask, SIGHUP);
701 sigprocmask(SIG_BLOCK, &mask, &omask);
703 omask = sigblock(sigmask(SIGHUP));
706 /* reset various things in the server's state */
710 syslog(LOG_INFO, "restart completed");
713 #ifdef _POSIX_VERSION
714 sigprocmask(SIG_SETMASK, &omask, (sigset_t *)0);
722 * detach from the terminal
728 /* detach from terminal and fork. */
732 #ifdef _POSIX_VERSION
733 size = sysconf(_SC_OPEN_MAX);
735 size = getdtablesize();
737 /* profiling seems to get confused by fork() */
745 for (i = 0; i < size; i++)
748 i = open("/dev/tty", O_RDWR, 666);
749 #ifdef TIOCNOTTY /* Only necessary on old systems. */
750 ioctl(i, TIOCNOTTY, NULL);
753 #ifdef _POSIX_VERSION
757 #endif /* not DEBUG */
760 read_from_dump(char *dumpfile)
762 /* Not yet implemented. */