diff options
author | 1987-07-07 13:21:14 +0000 | |
---|---|---|
committer | 1987-07-07 13:21:14 +0000 | |
commit | a9e44103be9292a0da391af14cbc90c6049dca40 (patch) | |
tree | 067191624cc009eb2321935387a752233473a01c | |
parent | 13cb4ba0a1f63d09b277cee30c4db8d78eb72ec2 (diff) |
add comments, clean up null pointer references
-rw-r--r-- | server/main.c | 113 |
1 files changed, 78 insertions, 35 deletions
diff --git a/server/main.c b/server/main.c index c5cea2f..eccbc3d 100644 --- a/server/main.c +++ b/server/main.c @@ -19,12 +19,31 @@ static char rcsid_main_c[] = "$Header$"; #endif SABER #endif lint static char copyright[] = "Copyright (c) 1987 Massachusetts Institute of Technology.\nPortions Copyright (c) 1986 Student Information Processing Board, Massachusetts Institute of Technology\n"; -static char version[] = "Zephyr Server (Prerelease) 0.1"; +static char version[] = "Zephyr Server (Prerelease) 0.2"; /* * Server loop for Zephyr. */ +/* + The Zephyr server maintains several linked lists of information. + + There is an array of servers initialized in main.c and maintained + by server_s.c. + Each server descriptor contains a pointer to a linked list of hosts + which are ``owned'' by that server. + Each of these host list entries has an IP address and a pointer to a + linked list of clients on that host. + Each client has a sockaddr_in, a list of subscriptions, and possibly + a session key. + + In addition, the class manager has copies of the pointers to the + clients which are registered with a particular class, the + not-yet-acknowledged list has copies of pointers to some clients, + and the hostm manager may have copies of pointers to some clients + (if the client has not acknowledged a packet after a given timeout). +*/ + #include "zserver.h" /* which includes zephyr/zephyr.h <errno.h> @@ -56,11 +75,9 @@ static void detach(); int srv_socket; /* dgram socket for clients and other servers */ -struct sockaddr_in sock_sin; -struct in_addr my_addr; -struct timeval nexthost_tv = {0, 0}; /* time till next host keepalive - initialize to zero so select doesn't - timeout */ +struct sockaddr_in sock_sin; /* address of the socket */ +struct in_addr my_addr; /* for convenience, my IP address */ +struct timeval nexthost_tv; /* time till next timeout for select */ int nservers; /* number of other servers */ ZServerDesc_t *otherservers; /* points to an array of the known @@ -68,11 +85,10 @@ ZServerDesc_t *otherservers; /* points to an array of the known int me_server_idx; /* # of my entry in the array */ ZNotAcked_t *nacklist; /* list of packets waiting for ack's */ -char *programname; /* set to the last element of argv[0] */ +char *programname; /* set to the basename of argv[0] */ char myname[MAXHOSTNAMELEN]; /* my host name */ ZAcl_t zctlacl = { ZEPHYR_CTL_ACL }; -/* These are commented out until the ACL package does wildcarding. */ ZAcl_t loginacl = { LOGIN_ACL }; ZAcl_t locateacl = { LOCATE_ACL }; ZAcl_t matchallacl = { MATCH_ALL_ACL }; @@ -98,15 +114,17 @@ char **argv; extern char *optarg; extern int optind; + /* print out various copyright bullshit */ puts(version); puts(copyright); + /* set name */ if (programname = rindex(argv[0],'/')) programname++; else programname = argv[0]; #ifdef DEBUG - /* open log */ + /* open log here, before we might send a message */ /* XXX eventually make this LOG_DAEMON */ openlog(programname, LOG_PID, LOG_LOCAL6); #endif DEBUG @@ -133,10 +151,12 @@ char **argv; /* XXX eventually make this LOG_DAEMON */ openlog(programname, LOG_PID, LOG_LOCAL6); #endif !DEBUG + /* set up sockets & my_addr and myname, find other servers and set up server table, initialize queues for retransmits, initialize error tables, set up restricted classes */ + if (initialize()) exit(1); @@ -147,22 +167,26 @@ char **argv; #ifdef DEBUG - (void) signal(SIGALRM, bye); + /* DBX catches sigterm and does the wrong thing with sigint, + so we provide another hook */ + (void) signal(SIGALRM, bye); + (void) signal(SIGTERM, bye); (void) signal(SIGINT, SIG_IGN); + syslog(LOG_INFO, "Ready for action"); #else (void) signal(SIGINT, bye); (void) signal(SIGTERM, bye); #endif DEBUG - /* GO! */ - syslog(LOG_INFO, "Ready for action"); + /* GO! */ for EVER { tvp = &nexthost_tv; if (nexttimo != 0L) { nexthost_tv.tv_sec = nexttimo - NOW; nexthost_tv.tv_usec = 0; - if (nexthost_tv.tv_sec < 0) { /* timeout has passed! */ + if (nexthost_tv.tv_sec < 0) { + /* timeout has passed! */ /* so we process one timeout, then pop to select, polling for input. This way we get work done even if swamped with many @@ -182,7 +206,7 @@ char **argv; continue; } if (nfound == 0) - /* either we timed out for keepalive or we were just + /* either we timed out or we were just polling for input. Either way we want to continue the loop, and process the next timeout */ continue; @@ -220,14 +244,12 @@ initialize() { register int i; struct in_addr *serv_addr, *hes_addrs; - /* XXX temporary hack */ if (do_net_setup()) return(1); /* talk to hesiod here, set nservers */ - if ((hes_addrs = get_server_addrs(&nservers)) == - (struct in_addr *) NULL) { + if (!(hes_addrs = get_server_addrs(&nservers))) { syslog(LOG_ERR, "No servers?!?"); exit(1); } @@ -238,20 +260,24 @@ initialize() for (serv_addr = hes_addrs, i = 0; i < nservers; serv_addr++, i++) { setup_server(&otherservers[i], serv_addr); + /* is this me? */ if (serv_addr->s_addr == my_addr.s_addr) { me_server_idx = i; otherservers[i].zs_state = SERV_UP; timer_reset(otherservers[i].zs_timer); otherservers[i].zs_timer = (timer) NULL; - zdbug1("found myself"); + zdbug((LOG_DEBUG,"found myself")); } } + + /* free up the addresses */ xfree(hes_addrs); + if (me_server_idx == -1) { ZServerDesc_t *temp; syslog(LOG_WARNING, "I'm a renegade server!"); - temp = (ZServerDesc_t *)realloc((caddr_t) otherservers,(unsigned) (++nservers * sizeof(ZServerDesc_t))); - if (temp == NULLZSDT) { + temp = (ZServerDesc_t *)realloc((caddr_t) otherservers, (unsigned) (++nservers * sizeof(ZServerDesc_t))); + if (!temp) { syslog(LOG_CRIT, "renegade realloc"); abort(); } @@ -264,6 +290,7 @@ initialize() adjusting */ for (i = 0; i < nservers - 1; i++) { timer_reset(otherservers[i].zs_timer); + /* all the HELLO's are due now */ otherservers[i].zs_timer = timer_set_rel(0L, server_timo, (caddr_t) &otherservers[i]); } /* I don't send hello's to myself--cancel the timer */ @@ -272,9 +299,7 @@ initialize() me_server_idx = nservers - 1; } - if ((nacklist = (ZNotAcked_t *) xmalloc(sizeof(ZNotAcked_t))) == - (ZNotAcked_t *) NULL) - { + if (!(nacklist = (ZNotAcked_t *) xmalloc(sizeof(ZNotAcked_t)))) { /* unrecoverable */ syslog(LOG_CRIT, "nacklist malloc"); abort(); @@ -317,7 +342,7 @@ do_net_setup() syslog(LOG_ERR, "no hostname: %m"); return(1); } - if ((hp = gethostbyname(hostname)) == (struct hostent *) NULL) { + if (!(hp = gethostbyname(hostname))) { syslog(LOG_ERR, "no gethostbyname repsonse"); (void) strncpy(myname, hostname, MAXHOSTNAMELEN); return(1); @@ -325,12 +350,9 @@ do_net_setup() (void) strncpy(myname, hp->h_name, MAXHOSTNAMELEN); bcopy((caddr_t) hp->h_addr, (caddr_t) &my_addr, sizeof(hp->h_addr)); - /* note that getservbyname may actually ask hesiod and not - /etc/services */ (void) setservent(1); /* keep file/connection open */ - if ((sp = getservbyname("zephyr-clt", "udp")) == - (struct servent *) NULL) { + if (!(sp = getservbyname("zephyr-clt", "udp"))) { syslog(LOG_ERR, "zephyr-clt/udp unknown"); return(1); } @@ -356,9 +378,11 @@ do_net_setup() } -/* get a list of server addresses, from Hesiod. Return a pointer to an - array of allocated storage. This storage is freed by the caller. - */ +/* + * get a list of server addresses, from Hesiod. Return a pointer to an + * array of allocated storage. This storage is freed by the caller. + */ + static struct in_addr * get_server_addrs(number) int *number; /* RETURN */ @@ -372,7 +396,7 @@ int *number; /* RETURN */ register struct hostent *hp; /* get the names from Hesiod */ - if ((server_hosts = hes_resolve("zephyr","sloc")) == (char **)NULL) + if (!(server_hosts = hes_resolve("zephyr","sloc"))) return((struct in_addr *)NULL); /* count up */ @@ -384,7 +408,9 @@ int *number; /* RETURN */ for (cpp = server_hosts, addr = addrs, i = 0; *cpp; cpp++) { hp = gethostbyname(*cpp); if (hp) { - bcopy((caddr_t)hp->h_addr, (caddr_t) addr, sizeof(struct in_addr)); + bcopy((caddr_t)hp->h_addr, + (caddr_t) addr, + sizeof(struct in_addr)); addr++, i++; } else syslog(LOG_WARNING, "hostname failed, %s",*cpp); @@ -393,6 +419,11 @@ int *number; /* RETURN */ return(addrs); } +/* + * initialize the server structure for address addr, and set a timer + * to go off immediately to send hello's to other servers. + */ + static void setup_server(server, addr) register ZServerDesc_t *server; @@ -411,8 +442,7 @@ struct in_addr *addr; /* set up a timer for this server */ server->zs_timer = timer_set_rel(0L, server_timo, (caddr_t) server); - if ((host = (ZHostList_t *) xmalloc(sizeof(ZHostList_t))) == NULLZHLT) - { + if (!(host = (ZHostList_t *) xmalloc(sizeof(ZHostList_t)))) { /* unrecoverable */ syslog(LOG_CRIT, "zs_host malloc"); abort(); @@ -422,6 +452,10 @@ struct in_addr *addr; return; } +/* + * print out a usage message. + */ + static void usage() { @@ -433,6 +467,10 @@ usage() exit(2); } +/* + * interrupt routine + */ + static int bye() { @@ -441,7 +479,12 @@ bye() exit(0); /*NOTREACHED*/ } + #ifndef DEBUG +/* + * detach from the terminal + */ + static void detach() { |