timed.c revision 30872
11553Srgrimes/*-
21553Srgrimes * Copyright (c) 1985, 1993
31553Srgrimes *	The Regents of the University of California.  All rights reserved.
41553Srgrimes *
51553Srgrimes * Redistribution and use in source and binary forms, with or without
61553Srgrimes * modification, are permitted provided that the following conditions
71553Srgrimes * are met:
81553Srgrimes * 1. Redistributions of source code must retain the above copyright
91553Srgrimes *    notice, this list of conditions and the following disclaimer.
101553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111553Srgrimes *    notice, this list of conditions and the following disclaimer in the
121553Srgrimes *    documentation and/or other materials provided with the distribution.
131553Srgrimes * 3. All advertising materials mentioning features or use of this software
141553Srgrimes *    must display the following acknowledgement:
151553Srgrimes *	This product includes software developed by the University of
161553Srgrimes *	California, Berkeley and its contributors.
171553Srgrimes * 4. Neither the name of the University nor the names of its contributors
181553Srgrimes *    may be used to endorse or promote products derived from this software
191553Srgrimes *    without specific prior written permission.
201553Srgrimes *
211553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241553Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311553Srgrimes * SUCH DAMAGE.
321553Srgrimes */
331553Srgrimes
341553Srgrimes#ifndef lint
3530642Scharnierstatic const char copyright[] =
361553Srgrimes"@(#) Copyright (c) 1985, 1993\n\
371553Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381553Srgrimes#endif /* not lint */
391553Srgrimes
401553Srgrimes#ifndef lint
4130642Scharnier#if 0
421553Srgrimesstatic char sccsid[] = "@(#)timed.c	8.1 (Berkeley) 6/6/93";
4330642Scharnier#endif
4430642Scharnierstatic const char rcsid[] =
4530872Scharnier	"$Id: timed.c,v 1.6 1997/10/29 07:32:30 charnier Exp $";
461553Srgrimes#endif /* not lint */
471553Srgrimes
481553Srgrimes#define TSPTYPES
491553Srgrimes#include "globals.h"
501553Srgrimes#include <net/if.h>
511553Srgrimes#include <sys/file.h>
521553Srgrimes#include <sys/ioctl.h>
531553Srgrimes#include <setjmp.h>
541553Srgrimes#include "pathnames.h"
551553Srgrimes#include <math.h>
561553Srgrimes#include <sys/types.h>
571553Srgrimes#include <sys/times.h>
581553Srgrimes#ifdef sgi
591553Srgrimes#include <unistd.h>
601553Srgrimes#include <sys/syssgi.h>
611553Srgrimes#include <sys/schedctl.h>
621553Srgrimes#endif /* sgi */
631553Srgrimes
641553Srgrimesint trace = 0;
651553Srgrimesint sock, sock_raw = -1;
661553Srgrimesint status = 0;
671553Srgrimesu_short sequence;			/* sequence number */
681553Srgrimeslong delay1;
691553Srgrimeslong delay2;
701553Srgrimes
711553Srgrimesint nslavenets;				/* nets were I could be a slave */
721553Srgrimesint nmasternets;			/* nets were I could be a master */
731553Srgrimesint nignorednets;			/* ignored nets */
741553Srgrimesint nnets;				/* nets I am connected to */
751553Srgrimes
761553SrgrimesFILE *fd;				/* trace file FD */
771553Srgrimes
781553Srgrimesjmp_buf jmpenv;
791553Srgrimes
801553Srgrimesstruct netinfo *nettab = 0;
811553Srgrimesstruct netinfo *slavenet;
821553Srgrimesint Mflag;
831553Srgrimesint justquit = 0;
841553Srgrimesint debug;
851553Srgrimes
861553Srgrimesstatic struct nets {
871553Srgrimes	char	*name;
881553Srgrimes	long	net;
891553Srgrimes	struct nets *next;
901553Srgrimes} *nets = 0;
911553Srgrimes
921553Srgrimesstruct hosttbl hosttbl[NHOSTS+1];	/* known hosts */
931553Srgrimes
941553Srgrimesstatic struct goodhost {		/* hosts that we trust */
9530872Scharnier	char	name[MAXHOSTNAMELEN];
961553Srgrimes	struct goodhost *next;
971553Srgrimes	char	perm;
981553Srgrimes} *goodhosts;
991553Srgrimes
1001553Srgrimesstatic char *goodgroup;			/* net group of trusted hosts */
1011553Srgrimesstatic void checkignorednets __P((void));
1021553Srgrimesstatic void pickslavenet __P((struct netinfo *));
1031553Srgrimesstatic void add_good_host __P((char *, int));
1041553Srgrimes
1051553Srgrimes#ifdef sgi
1061553Srgrimeschar *timetrim_fn;
1071553Srgrimeschar *timetrim_wpat = "long timetrim = %ld;\ndouble tot_adj = %.0f;\ndouble tot_ticks = %.0f;\n/* timed version 2 */\n";
1081553Srgrimeschar *timetrim_rpat = "long timetrim = %ld;\ndouble tot_adj = %lf;\ndouble tot_ticks = %lf;";
1091553Srgrimeslong timetrim;
1101553Srgrimesdouble tot_adj, hr_adj;			/* totals in nsec */
1111553Srgrimesdouble tot_ticks, hr_ticks;
1121553Srgrimes
1131553Srgrimesint bufspace = 60*1024;
1141553Srgrimes#endif
1151553Srgrimes
11630642Scharnierstatic void usage __P((void));
1171553Srgrimes
1181553Srgrimes/*
1191553Srgrimes * The timedaemons synchronize the clocks of hosts in a local area network.
1201553Srgrimes * One daemon runs as master, all the others as slaves. The master
1211553Srgrimes * performs the task of computing clock differences and sends correction
1221553Srgrimes * values to the slaves.
1231553Srgrimes * Slaves start an election to choose a new master when the latter disappears
1241553Srgrimes * because of a machine crash, network partition, or when killed.
1251553Srgrimes * A resolution protocol is used to kill all but one of the masters
1261553Srgrimes * that happen to exist in segments of a partitioned network when the
1271553Srgrimes * network partition is fixed.
1281553Srgrimes *
1291553Srgrimes * Authors: Riccardo Gusella & Stefano Zatti
1301553Srgrimes *
1311553Srgrimes * overhauled at Silicon Graphics
1321553Srgrimes */
1331553Srgrimesint
1341553Srgrimesmain(argc, argv)
1351553Srgrimes	int argc;
1361553Srgrimes	char *argv[];
1371553Srgrimes{
1381553Srgrimes	int on;
1391553Srgrimes	int ret;
1401553Srgrimes	int nflag, iflag;
1411553Srgrimes	struct timeval ntime;
1421553Srgrimes	struct servent *srvp;
1431553Srgrimes	char buf[BUFSIZ], *cp, *cplim;
1441553Srgrimes	struct ifconf ifc;
1451553Srgrimes	struct ifreq ifreq, ifreqf, *ifr;
1461553Srgrimes	register struct netinfo *ntp;
1471553Srgrimes	struct netinfo *ntip;
1481553Srgrimes	struct netinfo *savefromnet;
1491553Srgrimes	struct netent *nentp;
1501553Srgrimes	struct nets *nt;
1511553Srgrimes	struct sockaddr_in server;
1521553Srgrimes	u_short port;
1531553Srgrimes	char c;
1541553Srgrimes#ifdef sgi
1551553Srgrimes	FILE *timetrim_st;
1561553Srgrimes#endif
1571553Srgrimes
1581553Srgrimes#ifdef sgi
1591553Srgrimes	struct tms tms;
1601553Srgrimes#endif /* sgi */
1611553Srgrimes
1621553Srgrimes#ifdef lint
1631553Srgrimes	ntip = NULL;
1641553Srgrimes#endif
1651553Srgrimes
1661553Srgrimes	on = 1;
1671553Srgrimes	nflag = OFF;
1681553Srgrimes	iflag = OFF;
1691553Srgrimes
1701553Srgrimes#ifdef sgi
1711553Srgrimes	if (0 > syssgi(SGI_GETTIMETRIM, &timetrim)) {
17230642Scharnier		warn("syssgi(GETTIMETRIM)");
1731553Srgrimes		timetrim = 0;
1741553Srgrimes	}
1751553Srgrimes	tot_ticks = hr_ticks = times(&tms);
1761553Srgrimes#endif /* sgi */
1771553Srgrimes
1781553Srgrimes	opterr = 0;
17924428Simp	while ((c = getopt(argc, argv, "Mtdn:i:F:G:P:")) != -1) {
1801553Srgrimes		switch (c) {
1811553Srgrimes		case 'M':
1821553Srgrimes			Mflag = 1;
1831553Srgrimes			break;
1841553Srgrimes
1851553Srgrimes		case 't':
1861553Srgrimes			trace = 1;
1871553Srgrimes			break;
1881553Srgrimes
1891553Srgrimes		case 'n':
1901553Srgrimes			if (iflag) {
19130642Scharnier				errx(1, "-i and -n make no sense together");
1921553Srgrimes			} else {
1931553Srgrimes				nflag = ON;
1941553Srgrimes				addnetname(optarg);
1951553Srgrimes			}
1961553Srgrimes			break;
1971553Srgrimes
1981553Srgrimes		case 'i':
1991553Srgrimes			if (nflag) {
20030642Scharnier				errx(1, "-i and -n make no sense together");
2011553Srgrimes			} else {
2021553Srgrimes				iflag = ON;
2031553Srgrimes				addnetname(optarg);
2041553Srgrimes			}
2051553Srgrimes			break;
2061553Srgrimes
2071553Srgrimes		case 'F':
2081553Srgrimes			add_good_host(optarg,1);
2091553Srgrimes			while (optind < argc && argv[optind][0] != '-')
2101553Srgrimes				add_good_host(argv[optind++], 1);
2111553Srgrimes			break;
2121553Srgrimes
2131553Srgrimes		case 'd':
2141553Srgrimes			debug = 1;
2151553Srgrimes			break;
2161553Srgrimes		case 'G':
21730642Scharnier			if (goodgroup != 0)
21830642Scharnier				errx(1, "only one net group");
2191553Srgrimes			goodgroup = optarg;
2201553Srgrimes			break;
2211553Srgrimes#ifdef sgi
2221553Srgrimes		case 'P':
2231553Srgrimes			timetrim_fn = optarg;
2241553Srgrimes			timetrim_st = fopen(timetrim_fn, "r+");
2251553Srgrimes			if (0 == timetrim_st) {
2261553Srgrimes				if (errno != ENOENT) {
22730642Scharnier					warn("%s", timetrim_fn);
2281553Srgrimes					timetrim_fn = 0;
2291553Srgrimes				}
2301553Srgrimes			} else {
2311553Srgrimes				int i;
2321553Srgrimes				long trim;
2331553Srgrimes				double adj, ticks;
2341553Srgrimes
2351553Srgrimes				i = fscanf(timetrim_st, timetrim_rpat,
2361553Srgrimes					   &trim, &adj, &ticks);
2371553Srgrimes				if (i < 1
2381553Srgrimes				    || trim > MAX_TRIM
2391553Srgrimes				    || trim < -MAX_TRIM
2401553Srgrimes				    || i == 2
2411553Srgrimes				    || (i == 3
2421553Srgrimes					&& trim != rint(adj*CLK_TCK/ticks))) {
2431553Srgrimes					if (trace && i != EOF)
24430642Scharnier						warn(
24530642Scharnier						"unrecognized contents in %s",
2461553Srgrimes							      timetrim_fn);
2471553Srgrimes				} else {
2481553Srgrimes					if (0 > syssgi(SGI_SETTIMETRIM,
2491553Srgrimes						       trim)) {
25030642Scharnier					 warn("syssgi(SETTIMETRIM)");
2511553Srgrimes					} else {
2521553Srgrimes						timetrim = trim;
2531553Srgrimes					}
2541553Srgrimes					if (i == 3) {
2551553Srgrimes						tot_adj = adj;
2561553Srgrimes						tot_ticks -= ticks;
2571553Srgrimes					}
2581553Srgrimes				}
2591553Srgrimes				(void)fclose(timetrim_st);
2601553Srgrimes			}
2611553Srgrimes			break;
2621553Srgrimes#endif /* sgi */
2631553Srgrimes
2641553Srgrimes		default:
26530642Scharnier			usage();
2661553Srgrimes			break;
2671553Srgrimes		}
2681553Srgrimes	}
26930642Scharnier	if (optind < argc)
27030642Scharnier		usage();
2711553Srgrimes
2721553Srgrimes	/* If we care about which machine is the master, then we must
2731553Srgrimes	 *	be willing to be a master
2741553Srgrimes	 */
2751553Srgrimes	if (0 != goodgroup || 0 != goodhosts)
2761553Srgrimes		Mflag = 1;
2771553Srgrimes
27830642Scharnier	if (gethostname(hostname, sizeof(hostname) - 1) < 0)
27930642Scharnier		err(1, "gethostname");
2801553Srgrimes	self.l_bak = &self;
2811553Srgrimes	self.l_fwd = &self;
2821553Srgrimes	self.h_bak = &self;
2831553Srgrimes	self.h_fwd = &self;
2841553Srgrimes	self.head = 1;
2851553Srgrimes	self.good = 1;
2861553Srgrimes
2871553Srgrimes	if (goodhosts != 0)		/* trust ourself */
2881553Srgrimes		add_good_host(hostname,1);
2891553Srgrimes
2901553Srgrimes	srvp = getservbyname("timed", "udp");
29130642Scharnier	if (srvp == 0)
29230642Scharnier		errx(1, "unknown service 'timed/udp'");
2931553Srgrimes	port = srvp->s_port;
2948532Sdg	bzero(&server, sizeof(struct sockaddr_in));
2951553Srgrimes	server.sin_port = srvp->s_port;
2961553Srgrimes	server.sin_family = AF_INET;
2971553Srgrimes	sock = socket(AF_INET, SOCK_DGRAM, 0);
29830642Scharnier	if (sock < 0)
29930642Scharnier		err(1, "socket");
3001553Srgrimes	if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&on,
30130642Scharnier							sizeof(on)) < 0)
30230642Scharnier		err(1, "setsockopt");
3031553Srgrimes	if (bind(sock, (struct sockaddr*)&server, sizeof(server))) {
3041553Srgrimes		if (errno == EADDRINUSE)
30530642Scharnier			warnx("time daemon already running");
3061553Srgrimes		else
30730642Scharnier			warn("bind");
3081553Srgrimes		exit(1);
3091553Srgrimes	}
3101553Srgrimes#ifdef sgi
3111553Srgrimes	/*
3121553Srgrimes	 * handle many slaves with our buffer
3131553Srgrimes	 */
3141553Srgrimes	if (0 > setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char*)&bufspace,
31530642Scharnier			 sizeof(bufspace)))
31630642Scharnier		err(1, "setsockopt");
3171553Srgrimes#endif /* sgi */
3181553Srgrimes
3191553Srgrimes	/* choose a unique seed for random number generation */
3201553Srgrimes	(void)gettimeofday(&ntime, 0);
3211553Srgrimes	srandom(ntime.tv_sec + ntime.tv_usec);
3221553Srgrimes
3231553Srgrimes	sequence = random();     /* initial seq number */
3241553Srgrimes
3251553Srgrimes#ifndef sgi
3261553Srgrimes	/* rounds kernel variable time to multiple of 5 ms. */
3271553Srgrimes	ntime.tv_sec = 0;
3281553Srgrimes	ntime.tv_usec = -((ntime.tv_usec/1000) % 5) * 1000;
3291553Srgrimes	(void)adjtime(&ntime, (struct timeval *)0);
3301553Srgrimes#endif /* sgi */
3311553Srgrimes
3321553Srgrimes	for (nt = nets; nt; nt = nt->next) {
3331553Srgrimes		nentp = getnetbyname(nt->name);
3341553Srgrimes		if (nentp == 0) {
3351553Srgrimes			nt->net = inet_network(nt->name);
3361553Srgrimes			if (nt->net != INADDR_NONE)
3371553Srgrimes				nentp = getnetbyaddr(nt->net, AF_INET);
3381553Srgrimes		}
3391553Srgrimes		if (nentp != 0) {
3401553Srgrimes			nt->net = nentp->n_net;
3411553Srgrimes		} else if (nt->net == INADDR_NONE) {
34230642Scharnier			errx(1, "unknown net %s", nt->name);
3431553Srgrimes		} else if (nt->net == INADDR_ANY) {
34430642Scharnier			errx(1, "bad net %s", nt->name);
3451553Srgrimes		} else {
34630642Scharnier			warnx("warning: %s unknown in /etc/networks",
3471553Srgrimes				nt->name);
3481553Srgrimes		}
3491553Srgrimes
3501553Srgrimes		if (0 == (nt->net & 0xff000000))
3511553Srgrimes		    nt->net <<= 8;
3521553Srgrimes		if (0 == (nt->net & 0xff000000))
3531553Srgrimes		    nt->net <<= 8;
3541553Srgrimes		if (0 == (nt->net & 0xff000000))
3551553Srgrimes		    nt->net <<= 8;
3561553Srgrimes	}
3571553Srgrimes	ifc.ifc_len = sizeof(buf);
3581553Srgrimes	ifc.ifc_buf = buf;
35930642Scharnier	if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0)
36030642Scharnier		err(1, "get interface configuration");
3611553Srgrimes	ntp = NULL;
3621553Srgrimes#ifdef sgi
3631553Srgrimes#define size(p)	(sizeof(*ifr) - sizeof(ifr->ifr_name))  /* XXX hack. kludge */
3641553Srgrimes#else
3651553Srgrimes#define size(p)	max((p).sa_len, sizeof(p))
3661553Srgrimes#endif
3671553Srgrimes	cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */
3681553Srgrimes	for (cp = buf; cp < cplim;
3691553Srgrimes			cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) {
3701553Srgrimes		ifr = (struct ifreq *)cp;
3711553Srgrimes		if (ifr->ifr_addr.sa_family != AF_INET)
3721553Srgrimes			continue;
3731553Srgrimes		if (!ntp)
3741553Srgrimes			ntp = (struct netinfo*)malloc(sizeof(struct netinfo));
3751553Srgrimes		bzero(ntp,sizeof(*ntp));
3761553Srgrimes		ntp->my_addr=((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
3771553Srgrimes		ntp->status = NOMASTER;
3781553Srgrimes		ifreq = *ifr;
3791553Srgrimes		ifreqf = *ifr;
3801553Srgrimes
3811553Srgrimes		if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifreqf) < 0) {
38230642Scharnier			warn("get interface flags");
3831553Srgrimes			continue;
3841553Srgrimes		}
3851553Srgrimes		if ((ifreqf.ifr_flags & IFF_UP) == 0)
3861553Srgrimes			continue;
3871553Srgrimes		if ((ifreqf.ifr_flags & IFF_BROADCAST) == 0 &&
3881553Srgrimes		    (ifreqf.ifr_flags & IFF_POINTOPOINT) == 0) {
3891553Srgrimes			continue;
3901553Srgrimes		}
3911553Srgrimes
3921553Srgrimes
3931553Srgrimes		if (ioctl(sock, SIOCGIFNETMASK, (char *)&ifreq) < 0) {
39430642Scharnier			warn("get netmask");
3951553Srgrimes			continue;
3961553Srgrimes		}
3971553Srgrimes		ntp->mask = ((struct sockaddr_in *)
3981553Srgrimes			&ifreq.ifr_addr)->sin_addr.s_addr;
3991553Srgrimes
4001553Srgrimes		if (ifreqf.ifr_flags & IFF_BROADCAST) {
4011553Srgrimes			if (ioctl(sock, SIOCGIFBRDADDR, (char *)&ifreq) < 0) {
40230642Scharnier				warn("get broadaddr");
4031553Srgrimes				continue;
4041553Srgrimes			}
4051553Srgrimes			ntp->dest_addr = *(struct sockaddr_in *)&ifreq.ifr_broadaddr;
4061553Srgrimes			/* What if the broadcast address is all ones?
4071553Srgrimes			 * So we cannot just mask ntp->dest_addr.  */
4081553Srgrimes			ntp->net = ntp->my_addr;
4091553Srgrimes			ntp->net.s_addr &= ntp->mask;
4101553Srgrimes		} else {
4111553Srgrimes			if (ioctl(sock, SIOCGIFDSTADDR,
4121553Srgrimes						(char *)&ifreq) < 0) {
41330642Scharnier				warn("get destaddr");
4141553Srgrimes				continue;
4151553Srgrimes			}
4161553Srgrimes			ntp->dest_addr = *(struct sockaddr_in *)&ifreq.ifr_dstaddr;
4171553Srgrimes			ntp->net = ntp->dest_addr.sin_addr;
4181553Srgrimes		}
4191553Srgrimes
4201553Srgrimes		ntp->dest_addr.sin_port = port;
4211553Srgrimes
4221553Srgrimes		for (nt = nets; nt; nt = nt->next) {
42328547Sjlemon			if (ntp->net.s_addr == htonl(nt->net))
4241553Srgrimes				break;
4251553Srgrimes		}
42630642Scharnier		if ((nflag && !nt) || (iflag && nt))
4271553Srgrimes			continue;
4281553Srgrimes
4291553Srgrimes		ntp->next = NULL;
4301553Srgrimes		if (nettab == NULL) {
4311553Srgrimes			nettab = ntp;
4321553Srgrimes		} else {
4331553Srgrimes			ntip->next = ntp;
4341553Srgrimes		}
4351553Srgrimes		ntip = ntp;
4361553Srgrimes		ntp = NULL;
4371553Srgrimes	}
4381553Srgrimes	if (ntp)
4391553Srgrimes		(void) free((char *)ntp);
44030642Scharnier	if (nettab == NULL)
44130642Scharnier		errx(1, "no network usable");
4421553Srgrimes
4431553Srgrimes
4441553Srgrimes#ifdef sgi
4451553Srgrimes	(void)schedctl(RENICE,0,10);	   /* run fast to get good time */
4461553Srgrimes
4471553Srgrimes	/* ticks to delay before responding to a broadcast */
4481553Srgrimes	delay1 = casual(0, CLK_TCK/10);
4491553Srgrimes#else
4501553Srgrimes
4511553Srgrimes	/* microseconds to delay before responding to a broadcast */
4521553Srgrimes	delay1 = casual(1, 100*1000);
4531553Srgrimes#endif /* sgi */
4541553Srgrimes
4551553Srgrimes	/* election timer delay in secs. */
4561553Srgrimes	delay2 = casual(MINTOUT, MAXTOUT);
4571553Srgrimes
4581553Srgrimes
4591553Srgrimes#ifdef sgi
4601553Srgrimes	(void)_daemonize(debug ? _DF_NOFORK|_DF_NOCHDIR : 0, sock, -1, -1);
4611553Srgrimes#else
4621553Srgrimes	if (!debug)
4631553Srgrimes		daemon(debug, 0);
4641553Srgrimes#endif /* sgi */
4651553Srgrimes
4661553Srgrimes	if (trace)
4671553Srgrimes		traceon();
4681553Srgrimes	openlog("timed", LOG_CONS|LOG_PID, LOG_DAEMON);
4691553Srgrimes
4701553Srgrimes	/*
4711553Srgrimes	 * keep returning here
4721553Srgrimes	 */
4731553Srgrimes	ret = setjmp(jmpenv);
4741553Srgrimes	savefromnet = fromnet;
4751553Srgrimes	setstatus();
4761553Srgrimes
4771553Srgrimes	if (Mflag) {
4781553Srgrimes		switch (ret) {
4791553Srgrimes
4801553Srgrimes		case 0:
4811553Srgrimes			checkignorednets();
4821553Srgrimes			pickslavenet(0);
4831553Srgrimes			break;
4841553Srgrimes		case 1:
4851553Srgrimes			/* Just lost our master */
4861553Srgrimes			if (slavenet != 0)
4871553Srgrimes				slavenet->status = election(slavenet);
4881553Srgrimes			if (!slavenet || slavenet->status == MASTER) {
4891553Srgrimes				checkignorednets();
4901553Srgrimes				pickslavenet(0);
4911553Srgrimes			} else {
4921553Srgrimes				makeslave(slavenet);	/* prune extras */
4931553Srgrimes			}
4941553Srgrimes			break;
4951553Srgrimes
4961553Srgrimes		case 2:
4971553Srgrimes			/* Just been told to quit */
4981553Srgrimes			justquit = 1;
4991553Srgrimes			pickslavenet(savefromnet);
5001553Srgrimes			break;
5011553Srgrimes		}
5021553Srgrimes
5031553Srgrimes		setstatus();
5041553Srgrimes		if (!(status & MASTER) && sock_raw != -1) {
5051553Srgrimes			/* sock_raw is not being used now */
5061553Srgrimes			(void)close(sock_raw);
5071553Srgrimes			sock_raw = -1;
5081553Srgrimes		}
5091553Srgrimes
5101553Srgrimes		if (status == MASTER)
5111553Srgrimes			master();
5121553Srgrimes		else
5131553Srgrimes			slave();
5141553Srgrimes
5151553Srgrimes	} else {
5161553Srgrimes		if (sock_raw != -1) {
5171553Srgrimes			(void)close(sock_raw);
5181553Srgrimes			sock_raw = -1;
5191553Srgrimes		}
5201553Srgrimes
5211553Srgrimes		if (ret) {
5221553Srgrimes			/* we just lost our master or were told to quit */
5231553Srgrimes			justquit = 1;
5241553Srgrimes		}
5251553Srgrimes		for (ntp = nettab; ntp != NULL; ntp = ntp->next) {
5261553Srgrimes			if (ntp->status == MASTER)
5271553Srgrimes				rmnetmachs(ntp);
5281553Srgrimes				ntp->status = NOMASTER;
5291553Srgrimes		}
5301553Srgrimes		checkignorednets();
5311553Srgrimes		pickslavenet(0);
5321553Srgrimes		setstatus();
5331553Srgrimes
5341553Srgrimes		slave();
5351553Srgrimes	}
5361553Srgrimes	/* NOTREACHED */
5371553Srgrimes	return(0);
5381553Srgrimes}
5391553Srgrimes
54030642Scharnierstatic void
54130642Scharnierusage()
54230642Scharnier{
54330642Scharnier#ifdef sgi
54430642Scharnier	fprintf(stderr, "%s\n%s\n",
54530642Scharnier"usage: timed [-dtM] [-i net|-n net] [-F host1 host2 ...]",
54630642Scharnier"             [-G netgp] [-P trimfile]");
54730642Scharnier#else
54830642Scharnier#ifdef HAVENIS
54930642Scharnier	fprintf(stderr,
55030642Scharnier"usage: timed [-dtM] [-i net|-n net] [-F host1 host2 ...] [-G netgp]\n");
55130642Scharnier#else
55230642Scharnier	fprintf(stderr,
55330642Scharnier"usage: timed [-dtM] [-i net|-n net] [-F host1 host2 ...]\n");
55430642Scharnier#endif /* HAVENIS */
55530642Scharnier#endif /* sgi */
55630642Scharnier	exit(1);
55730642Scharnier}
55830642Scharnier
5591553Srgrimes/*
5601553Srgrimes * suppress an upstart, untrustworthy, self-appointed master
5611553Srgrimes */
5621553Srgrimesvoid
5631553Srgrimessuppress(addr, name,net)
5641553Srgrimes	struct sockaddr_in *addr;
5651553Srgrimes	char *name;
5661553Srgrimes	struct netinfo *net;
5671553Srgrimes{
5681553Srgrimes	struct sockaddr_in tgt;
5691553Srgrimes	char tname[MAXHOSTNAMELEN];
5701553Srgrimes	struct tsp msg;
5711553Srgrimes	static struct timeval wait;
5721553Srgrimes
5731553Srgrimes	if (trace)
5741553Srgrimes		fprintf(fd, "suppress: %s\n", name);
5751553Srgrimes	tgt = *addr;
57630830Scharnier	(void)strcpy(tname, name);
5771553Srgrimes
5781553Srgrimes	while (0 != readmsg(TSP_ANY, ANYADDR, &wait, net)) {
5791553Srgrimes		if (trace)
5801553Srgrimes			fprintf(fd, "suppress:\tdiscarded packet from %s\n",
5811553Srgrimes				    name);
5821553Srgrimes	}
5831553Srgrimes
5841553Srgrimes	syslog(LOG_NOTICE, "suppressing false master %s", tname);
5851553Srgrimes	msg.tsp_type = TSP_QUIT;
58630830Scharnier	(void)strcpy(msg.tsp_name, hostname);
5871553Srgrimes	(void)acksend(&msg, &tgt, tname, TSP_ACK, 0, 1);
5881553Srgrimes}
5891553Srgrimes
5901553Srgrimesvoid
5911553Srgrimeslookformaster(ntp)
5921553Srgrimes	struct netinfo *ntp;
5931553Srgrimes{
5941553Srgrimes	struct tsp resp, conflict, *answer;
5951553Srgrimes	struct timeval ntime;
5961553Srgrimes	char mastername[MAXHOSTNAMELEN];
5971553Srgrimes	struct sockaddr_in masteraddr;
5981553Srgrimes
5991553Srgrimes	get_goodgroup(0);
6001553Srgrimes	ntp->status = SLAVE;
6011553Srgrimes
6021553Srgrimes	/* look for master */
6031553Srgrimes	resp.tsp_type = TSP_MASTERREQ;
60430830Scharnier	(void)strcpy(resp.tsp_name, hostname);
6051553Srgrimes	answer = acksend(&resp, &ntp->dest_addr, ANYADDR,
6061553Srgrimes			 TSP_MASTERACK, ntp, 0);
6071553Srgrimes	if (answer != 0 && !good_host_name(answer->tsp_name)) {
6081553Srgrimes		suppress(&from, answer->tsp_name, ntp);
6091553Srgrimes		ntp->status = NOMASTER;
6101553Srgrimes		answer = 0;
6111553Srgrimes	}
6121553Srgrimes	if (answer == 0) {
6131553Srgrimes		/*
6141553Srgrimes		 * Various conditions can cause conflict: races between
6151553Srgrimes		 * two just started timedaemons when no master is
6161553Srgrimes		 * present, or timedaemons started during an election.
6171553Srgrimes		 * A conservative approach is taken.  Give up and became a
6181553Srgrimes		 * slave, postponing election of a master until first
6191553Srgrimes		 * timer expires.
6201553Srgrimes		 */
6211553Srgrimes		ntime.tv_sec = ntime.tv_usec = 0;
6221553Srgrimes		answer = readmsg(TSP_MASTERREQ, ANYADDR, &ntime, ntp);
6231553Srgrimes		if (answer != 0) {
6241553Srgrimes			if (!good_host_name(answer->tsp_name)) {
6251553Srgrimes				suppress(&from, answer->tsp_name, ntp);
6261553Srgrimes				ntp->status = NOMASTER;
6271553Srgrimes			}
6281553Srgrimes			return;
6291553Srgrimes		}
6301553Srgrimes
6311553Srgrimes		ntime.tv_sec = ntime.tv_usec = 0;
6321553Srgrimes		answer = readmsg(TSP_MASTERUP, ANYADDR, &ntime, ntp);
6331553Srgrimes		if (answer != 0) {
6341553Srgrimes			if (!good_host_name(answer->tsp_name)) {
6351553Srgrimes				suppress(&from, answer->tsp_name, ntp);
6361553Srgrimes				ntp->status = NOMASTER;
6371553Srgrimes			}
6381553Srgrimes			return;
6391553Srgrimes		}
6401553Srgrimes
6411553Srgrimes		ntime.tv_sec = ntime.tv_usec = 0;
6421553Srgrimes		answer = readmsg(TSP_ELECTION, ANYADDR, &ntime, ntp);
6431553Srgrimes		if (answer != 0) {
6441553Srgrimes			if (!good_host_name(answer->tsp_name)) {
6451553Srgrimes				suppress(&from, answer->tsp_name, ntp);
6461553Srgrimes				ntp->status = NOMASTER;
6471553Srgrimes			}
6481553Srgrimes			return;
6491553Srgrimes		}
6501553Srgrimes
6511553Srgrimes		if (Mflag)
6521553Srgrimes			ntp->status = MASTER;
6531553Srgrimes		else
6541553Srgrimes			ntp->status = NOMASTER;
6551553Srgrimes		return;
6561553Srgrimes	}
6571553Srgrimes
6581553Srgrimes	ntp->status = SLAVE;
65930830Scharnier	(void)strcpy(mastername, answer->tsp_name);
6601553Srgrimes	masteraddr = from;
6611553Srgrimes
6621553Srgrimes	/*
6631553Srgrimes	 * If network has been partitioned, there might be other
6641553Srgrimes	 * masters; tell the one we have just acknowledged that
6651553Srgrimes	 * it has to gain control over the others.
6661553Srgrimes	 */
6671553Srgrimes	ntime.tv_sec = 0;
6681553Srgrimes	ntime.tv_usec = 300000;
6691553Srgrimes	answer = readmsg(TSP_MASTERACK, ANYADDR, &ntime, ntp);
6701553Srgrimes	/*
6711553Srgrimes	 * checking also not to send CONFLICT to ack'ed master
6721553Srgrimes	 * due to duplicated MASTERACKs
6731553Srgrimes	 */
6741553Srgrimes	if (answer != NULL &&
6751553Srgrimes	    strcmp(answer->tsp_name, mastername) != 0) {
6761553Srgrimes		conflict.tsp_type = TSP_CONFLICT;
67730830Scharnier		(void)strcpy(conflict.tsp_name, hostname);
6781553Srgrimes		if (!acksend(&conflict, &masteraddr, mastername,
6791553Srgrimes			     TSP_ACK, 0, 0)) {
6801553Srgrimes			syslog(LOG_ERR,
6811553Srgrimes			       "error on sending TSP_CONFLICT");
6821553Srgrimes		}
6831553Srgrimes	}
6841553Srgrimes}
6851553Srgrimes
6861553Srgrimes/*
6871553Srgrimes * based on the current network configuration, set the status, and count
6881553Srgrimes * networks;
6891553Srgrimes */
6901553Srgrimesvoid
6911553Srgrimessetstatus()
6921553Srgrimes{
6931553Srgrimes	struct netinfo *ntp;
6941553Srgrimes
6951553Srgrimes	status = 0;
6961553Srgrimes	nmasternets = nslavenets = nnets = nignorednets = 0;
6971553Srgrimes	if (trace)
6981553Srgrimes		fprintf(fd, "Net status:\n");
6991553Srgrimes	for (ntp = nettab; ntp != NULL; ntp = ntp->next) {
7001553Srgrimes		switch ((int)ntp->status) {
7011553Srgrimes		case MASTER:
7021553Srgrimes			nmasternets++;
7031553Srgrimes			break;
7041553Srgrimes		case SLAVE:
7051553Srgrimes			nslavenets++;
7061553Srgrimes			break;
7071553Srgrimes		case NOMASTER:
7081553Srgrimes		case IGNORE:
7091553Srgrimes			nignorednets++;
7101553Srgrimes			break;
7111553Srgrimes		}
7121553Srgrimes		if (trace) {
7131553Srgrimes			fprintf(fd, "\t%-16s", inet_ntoa(ntp->net));
7141553Srgrimes			switch ((int)ntp->status) {
7151553Srgrimes			case NOMASTER:
7161553Srgrimes				fprintf(fd, "NOMASTER\n");
7171553Srgrimes				break;
7181553Srgrimes			case MASTER:
7191553Srgrimes				fprintf(fd, "MASTER\n");
7201553Srgrimes				break;
7211553Srgrimes			case SLAVE:
7221553Srgrimes				fprintf(fd, "SLAVE\n");
7231553Srgrimes				break;
7241553Srgrimes			case IGNORE:
7251553Srgrimes				fprintf(fd, "IGNORE\n");
7261553Srgrimes				break;
7271553Srgrimes			default:
7281553Srgrimes				fprintf(fd, "invalid state %d\n",
7291553Srgrimes					(int)ntp->status);
7301553Srgrimes				break;
7311553Srgrimes			}
7321553Srgrimes		}
7331553Srgrimes		nnets++;
7341553Srgrimes		status |= ntp->status;
7351553Srgrimes	}
7361553Srgrimes	status &= ~IGNORE;
7371553Srgrimes	if (trace)
7381553Srgrimes		fprintf(fd,
7391553Srgrimes			"\tnets=%d masters=%d slaves=%d ignored=%d delay2=%d\n",
7401553Srgrimes			nnets, nmasternets, nslavenets, nignorednets, delay2);
7411553Srgrimes}
7421553Srgrimes
7431553Srgrimesvoid
7441553Srgrimesmakeslave(net)
7451553Srgrimes	struct netinfo *net;
7461553Srgrimes{
7471553Srgrimes	register struct netinfo *ntp;
7481553Srgrimes
7491553Srgrimes	for (ntp = nettab; ntp != NULL; ntp = ntp->next) {
7501553Srgrimes		if (ntp->status == SLAVE && ntp != net)
7511553Srgrimes			ntp->status = IGNORE;
7521553Srgrimes	}
7531553Srgrimes	slavenet = net;
7541553Srgrimes}
7551553Srgrimes
7561553Srgrimes/*
7571553Srgrimes * Try to become master over ignored nets..
7581553Srgrimes */
7591553Srgrimesstatic void
7601553Srgrimescheckignorednets()
7611553Srgrimes{
7621553Srgrimes	register struct netinfo *ntp;
7631553Srgrimes
7641553Srgrimes	for (ntp = nettab; ntp != NULL; ntp = ntp->next) {
7651553Srgrimes		if (!Mflag && ntp->status == SLAVE)
7661553Srgrimes			break;
7671553Srgrimes
7681553Srgrimes		if (ntp->status == IGNORE || ntp->status == NOMASTER) {
7691553Srgrimes			lookformaster(ntp);
7701553Srgrimes			if (!Mflag && ntp->status == SLAVE)
7711553Srgrimes				break;
7721553Srgrimes		}
7731553Srgrimes	}
7741553Srgrimes}
7751553Srgrimes
7761553Srgrimes/*
7771553Srgrimes * choose a good network on which to be a slave
7781553Srgrimes *	The ignored networks must have already been checked.
7791553Srgrimes *	Take a hint about for a good network.
7801553Srgrimes */
7811553Srgrimesstatic void
7821553Srgrimespickslavenet(ntp)
7831553Srgrimes	struct netinfo *ntp;
7841553Srgrimes{
7851553Srgrimes	if (slavenet != 0 && slavenet->status == SLAVE) {
7861553Srgrimes		makeslave(slavenet);		/* prune extras */
7871553Srgrimes		return;
7881553Srgrimes	}
7891553Srgrimes
7901553Srgrimes	if (ntp == 0 || ntp->status != SLAVE) {
7911553Srgrimes		for (ntp = nettab; ntp != 0; ntp = ntp->next) {
7921553Srgrimes			if (ntp->status == SLAVE)
7931553Srgrimes				break;
7941553Srgrimes		}
7951553Srgrimes	}
7961553Srgrimes	makeslave(ntp);
7971553Srgrimes}
7981553Srgrimes
7991553Srgrimes/*
8001553Srgrimes * returns a random number in the range [inf, sup]
8011553Srgrimes */
8021553Srgrimeslong
8031553Srgrimescasual(inf, sup)
8041553Srgrimes	long inf, sup;
8051553Srgrimes{
8061553Srgrimes	double value;
8071553Srgrimes
8081553Srgrimes	value = ((double)(random() & 0x7fffffff)) / (0x7fffffff*1.0);
8091553Srgrimes	return(inf + (sup - inf)*value);
8101553Srgrimes}
8111553Srgrimes
8121553Srgrimeschar *
8131553Srgrimesdate()
8141553Srgrimes{
8151553Srgrimes#ifdef sgi
8161553Srgrimes	struct	timeval tv;
8171553Srgrimes	static char tm[32];
8181553Srgrimes
8191553Srgrimes	(void)gettimeofday(&tv, (struct timezone *)0);
8201553Srgrimes	(void)cftime(tm, "%D %T", &tv.tv_sec);
8211553Srgrimes	return (tm);
8221553Srgrimes#else
8231553Srgrimes	struct	timeval tv;
8241553Srgrimes
8251553Srgrimes	(void)gettimeofday(&tv, (struct timezone *)0);
8261553Srgrimes	return (ctime(&tv.tv_sec));
8271553Srgrimes#endif /* sgi */
8281553Srgrimes}
8291553Srgrimes
8301553Srgrimesvoid
8311553Srgrimesaddnetname(name)
8321553Srgrimes	char *name;
8331553Srgrimes{
8341553Srgrimes	register struct nets **netlist = &nets;
8351553Srgrimes
8361553Srgrimes	while (*netlist)
8371553Srgrimes		netlist = &((*netlist)->next);
8381553Srgrimes	*netlist = (struct nets *)malloc(sizeof **netlist);
83930642Scharnier	if (*netlist == 0)
84030642Scharnier		errx(1, "malloc failed");
8411553Srgrimes	bzero((char *)*netlist, sizeof(**netlist));
8421553Srgrimes	(*netlist)->name = name;
8431553Srgrimes}
8441553Srgrimes
8451553Srgrimes/* note a host as trustworthy */
8461553Srgrimesstatic void
8471553Srgrimesadd_good_host(name, perm)
8481553Srgrimes	char *name;
8491553Srgrimes	int perm;			/* 1=not part of the netgroup */
8501553Srgrimes{
8511553Srgrimes	register struct goodhost *ghp;
8521553Srgrimes	register struct hostent *hentp;
8531553Srgrimes
8541553Srgrimes	ghp = (struct goodhost*)malloc(sizeof(*ghp));
8551553Srgrimes	if (!ghp) {
8561553Srgrimes		syslog(LOG_ERR, "malloc failed");
8571553Srgrimes		exit(1);
8581553Srgrimes	}
8591553Srgrimes
8601553Srgrimes	bzero((char*)ghp, sizeof(*ghp));
8611553Srgrimes	(void)strncpy(&ghp->name[0], name, sizeof(ghp->name));
8621553Srgrimes	ghp->next = goodhosts;
8631553Srgrimes	ghp->perm = perm;
8641553Srgrimes	goodhosts = ghp;
8651553Srgrimes
8661553Srgrimes	hentp = gethostbyname(name);
8671553Srgrimes	if (0 == hentp && perm)
86830642Scharnier		warnx("unknown host %s", name);
8691553Srgrimes}
8701553Srgrimes
8711553Srgrimes
8721553Srgrimes/* update our image of the net-group of trustworthy hosts
8731553Srgrimes */
8741553Srgrimesvoid
8751553Srgrimesget_goodgroup(force)
8761553Srgrimes	int force;
8771553Srgrimes{
8781553Srgrimes# define NG_DELAY (30*60*CLK_TCK)	/* 30 minutes */
8791553Srgrimes	static unsigned long last_update = -NG_DELAY;
8801553Srgrimes	unsigned long new_update;
88130642Scharnier	struct goodhost *ghp, **ghpp;
88230642Scharnier#ifdef HAVENIS
8831553Srgrimes	struct hosttbl *htp;
8841553Srgrimes	char *mach, *usr, *dom;
88530642Scharnier#endif /* HAVENIS */
8861553Srgrimes	struct tms tm;
8871553Srgrimes
8881553Srgrimes
8891553Srgrimes	/* if no netgroup, then we are finished */
8901553Srgrimes	if (goodgroup == 0 || !Mflag)
8911553Srgrimes		return;
8921553Srgrimes
8931553Srgrimes	/* Do not chatter with the netgroup master too often.
8941553Srgrimes	 */
8951553Srgrimes	new_update = times(&tm);
8961553Srgrimes	if (new_update < last_update + NG_DELAY
8971553Srgrimes	    && !force)
8981553Srgrimes		return;
8991553Srgrimes	last_update = new_update;
9001553Srgrimes
9011553Srgrimes	/* forget the old temporary entries */
9021553Srgrimes	ghpp = &goodhosts;
9031553Srgrimes	while (0 != (ghp = *ghpp)) {
9041553Srgrimes		if (!ghp->perm) {
9051553Srgrimes			*ghpp = ghp->next;
9061553Srgrimes			free((char*)ghp);
9071553Srgrimes		} else {
9081553Srgrimes			ghpp = &ghp->next;
9091553Srgrimes		}
9101553Srgrimes	}
9111553Srgrimes
9121553Srgrimes#ifdef HAVENIS
9131553Srgrimes	/* quit now if we are not one of the trusted masters
9141553Srgrimes	 */
9151553Srgrimes	if (!innetgr(goodgroup, &hostname[0], 0,0)) {
9161553Srgrimes		if (trace)
9171553Srgrimes			(void)fprintf(fd, "get_goodgroup: %s not in %s\n",
9181553Srgrimes				      &hostname[0], goodgroup);
9191553Srgrimes		return;
9201553Srgrimes	}
9211553Srgrimes	if (trace)
9221553Srgrimes		(void)fprintf(fd, "get_goodgroup: %s in %s\n",
9231553Srgrimes				  &hostname[0], goodgroup);
9241553Srgrimes
9251553Srgrimes	/* mark the entire netgroup as trusted */
9261553Srgrimes	(void)setnetgrent(goodgroup);
9271553Srgrimes	while (getnetgrent(&mach,&usr,&dom)) {
9281553Srgrimes		if (0 != mach)
9291553Srgrimes			add_good_host(mach,0);
9301553Srgrimes	}
9311553Srgrimes	(void)endnetgrent();
9321553Srgrimes
9331553Srgrimes	/* update list of slaves */
9341553Srgrimes	for (htp = self.l_fwd; htp != &self; htp = htp->l_fwd) {
9351553Srgrimes		htp->good = good_host_name(&htp->name[0]);
9361553Srgrimes	}
9371553Srgrimes#endif /* HAVENIS */
9381553Srgrimes}
9391553Srgrimes
9401553Srgrimes
9411553Srgrimes/* see if a machine is trustworthy
9421553Srgrimes */
9431553Srgrimesint					/* 1=trust hp to change our date */
9441553Srgrimesgood_host_name(name)
9451553Srgrimes	char *name;
9461553Srgrimes{
9471553Srgrimes	register struct goodhost *ghp = goodhosts;
9481553Srgrimes	register char c;
9491553Srgrimes
9501553Srgrimes	if (!ghp || !Mflag)		/* trust everyone if no one named */
9511553Srgrimes		return 1;
9521553Srgrimes
9531553Srgrimes	c = *name;
9541553Srgrimes	do {
9551553Srgrimes		if (c == ghp->name[0]
9561553Srgrimes		    && !strcasecmp(name, ghp->name))
9571553Srgrimes			return 1;	/* found him, so say so */
9581553Srgrimes	} while (0 != (ghp = ghp->next));
9591553Srgrimes
9601553Srgrimes	if (!strcasecmp(name,hostname))	/* trust ourself */
9611553Srgrimes		return 1;
9621553Srgrimes
9631553Srgrimes	return 0;			/* did not find him */
9641553Srgrimes}
965