xref: /original-bsd/usr.sbin/timed/timed/master.c (revision 24002341)
1251af36eSbostic /*-
2*24002341Sbostic  * Copyright (c) 1985, 1993
3*24002341Sbostic  *	The Regents of the University of California.  All rights reserved.
4b0eb8cefSbostic  *
5e1a0b129Sbostic  * %sccs.include.redist.c%
69abb11fcSgusella  */
79abb11fcSgusella 
89abb11fcSgusella #ifndef lint
9*24002341Sbostic static char sccsid[] = "@(#)master.c	8.1 (Berkeley) 06/06/93";
10b0eb8cefSbostic #endif /* not lint */
119abb11fcSgusella 
12251af36eSbostic #ifdef sgi
13251af36eSbostic #ident "$Revision: 1.21 $"
14251af36eSbostic #endif
15251af36eSbostic 
169abb11fcSgusella #include "globals.h"
17afdbedcaSlepreau #include <sys/file.h>
18251af36eSbostic #include <sys/types.h>
19251af36eSbostic #include <sys/times.h>
209abb11fcSgusella #include <setjmp.h>
21251af36eSbostic #ifdef sgi
22251af36eSbostic #include <sys/schedctl.h>
23251af36eSbostic #endif /* sgi */
24afdbedcaSlepreau #include <utmp.h>
259b16b345Sbostic #include "pathnames.h"
269abb11fcSgusella 
279abb11fcSgusella extern int measure_delta;
28bc6a4b57Sbloom extern jmp_buf jmpenv;
29251af36eSbostic extern int Mflag;
30251af36eSbostic extern int justquit;
319abb11fcSgusella 
32251af36eSbostic static int dictate;
33251af36eSbostic static int slvcount;			/* slaves listening to our clock */
34612ddc41Sbloom 
35251af36eSbostic static void mchgdate __P((struct tsp *));
36251af36eSbostic 
37251af36eSbostic #ifdef sgi
38251af36eSbostic extern void logwtmp __P((struct timeval *, struct timeval *));
39251af36eSbostic #else
40251af36eSbostic extern void logwtmp __P((char *, char *, char *));
41251af36eSbostic #endif /* sgi */
429abb11fcSgusella 
439abb11fcSgusella /*
449abb11fcSgusella  * The main function of `master' is to periodically compute the differences
459abb11fcSgusella  * (deltas) between its clock and the clocks of the slaves, to compute the
469abb11fcSgusella  * network average delta, and to send to the slaves the differences between
479abb11fcSgusella  * their individual deltas and the network delta.
489abb11fcSgusella  * While waiting, it receives messages from the slaves (i.e. requests for
499abb11fcSgusella  * master's name, remote requests to set the network time, ...), and
509abb11fcSgusella  * takes the appropriate action.
519abb11fcSgusella  */
52251af36eSbostic int
master()539abb11fcSgusella master()
549abb11fcSgusella {
55251af36eSbostic 	struct hosttbl *htp;
569abb11fcSgusella 	long pollingtime;
57251af36eSbostic #define POLLRATE 4
58251af36eSbostic 	int polls;
59251af36eSbostic 	struct timeval wait, ntime;
60251af36eSbostic 	struct tsp *msg, *answer, to;
61251af36eSbostic 	char newdate[32];
62251af36eSbostic 	struct sockaddr_in taddr;
63251af36eSbostic 	char tname[MAXHOSTNAMELEN];
64251af36eSbostic 	struct netinfo *ntp;
65251af36eSbostic 	int i;
669abb11fcSgusella 
67251af36eSbostic 	syslog(LOG_NOTICE, "This machine is master");
689abb11fcSgusella 	if (trace)
69251af36eSbostic 		fprintf(fd, "This machine is master\n");
70251af36eSbostic 	for (ntp = nettab; ntp != NULL; ntp = ntp->next) {
71bc6a4b57Sbloom 		if (ntp->status == MASTER)
72bc6a4b57Sbloom 			masterup(ntp);
73251af36eSbostic 	}
74251af36eSbostic 	(void)gettimeofday(&ntime, 0);
75251af36eSbostic 	pollingtime = ntime.tv_sec+3;
76251af36eSbostic 	if (justquit)
77251af36eSbostic 		polls = 0;
78251af36eSbostic 	else
79251af36eSbostic 		polls = POLLRATE-1;
809abb11fcSgusella 
81251af36eSbostic /* Process all outstanding messages before spending the long time necessary
82251af36eSbostic  *	to update all timers.
83251af36eSbostic  */
849abb11fcSgusella loop:
85251af36eSbostic 	(void)gettimeofday(&ntime, 0);
86251af36eSbostic 	wait.tv_sec = pollingtime - ntime.tv_sec;
87251af36eSbostic 	if (wait.tv_sec < 0)
88251af36eSbostic 		wait.tv_sec = 0;
89251af36eSbostic 	wait.tv_usec = 0;
90251af36eSbostic 	msg = readmsg(TSP_ANY, ANYADDR, &wait, 0);
91251af36eSbostic 	if (!msg) {
92251af36eSbostic 		(void)gettimeofday(&ntime, 0);
93251af36eSbostic 		if (ntime.tv_sec >= pollingtime) {
94251af36eSbostic 			pollingtime = ntime.tv_sec + SAMPLEINTVL;
95251af36eSbostic 			get_goodgroup(0);
96251af36eSbostic 
97251af36eSbostic /* If a bogus master told us to quit, we can have decided to ignore a
98251af36eSbostic  * network.  Therefore, periodically try to take over everything.
99251af36eSbostic  */
100251af36eSbostic 			polls = (polls + 1) % POLLRATE;
101251af36eSbostic 			if (0 == polls && nignorednets > 0) {
102251af36eSbostic 				trace_msg("Looking for nets to re-master\n");
103251af36eSbostic 				for (ntp = nettab; ntp; ntp = ntp->next) {
104251af36eSbostic 					if (ntp->status == IGNORE
105251af36eSbostic 					    || ntp->status == NOMASTER) {
106251af36eSbostic 						lookformaster(ntp);
107251af36eSbostic 						if (ntp->status == MASTER) {
108251af36eSbostic 							masterup(ntp);
109251af36eSbostic 							polls = POLLRATE-1;
110251af36eSbostic 						}
111251af36eSbostic 					}
112251af36eSbostic 					if (ntp->status == MASTER
113251af36eSbostic 					    && --ntp->quit_count < 0)
114251af36eSbostic 						ntp->quit_count = 0;
115251af36eSbostic 				}
116251af36eSbostic 				if (polls != 0)
117251af36eSbostic 					setstatus();
118251af36eSbostic 			}
119251af36eSbostic 
120bc6a4b57Sbloom 			synch(0L);
121612ddc41Sbloom 
122612ddc41Sbloom 			for (ntp = nettab; ntp != NULL; ntp = ntp->next) {
123612ddc41Sbloom 				to.tsp_type = TSP_LOOP;
124612ddc41Sbloom 				to.tsp_vers = TSPVERSION;
125f3a5a916Skarels 				to.tsp_seq = sequence++;
126251af36eSbostic 				to.tsp_hopcnt = MAX_HOPCNT;
127612ddc41Sbloom 				(void)strcpy(to.tsp_name, hostname);
128612ddc41Sbloom 				bytenetorder(&to);
129251af36eSbostic 				if (sendto(sock, (char *)&to,
130251af36eSbostic 					   sizeof(struct tsp), 0,
1318ad07651Sbostic 					   (struct sockaddr*)&ntp->dest_addr,
132251af36eSbostic 					   sizeof(ntp->dest_addr)) < 0) {
133251af36eSbostic 				   trace_sendto_err(ntp->dest_addr.sin_addr);
134612ddc41Sbloom 				}
135612ddc41Sbloom 			}
1369abb11fcSgusella 		}
1379abb11fcSgusella 
138251af36eSbostic 
139251af36eSbostic 	} else {
1409abb11fcSgusella 		switch (msg->tsp_type) {
1419abb11fcSgusella 
1429abb11fcSgusella 		case TSP_MASTERREQ:
1439abb11fcSgusella 			break;
144251af36eSbostic 
1459abb11fcSgusella 		case TSP_SLAVEUP:
146251af36eSbostic 			newslave(msg);
1479abb11fcSgusella 			break;
148251af36eSbostic 
1491c20bcebSbloom 		case TSP_SETDATE:
1505a8c20e3Sbloom 			/*
151251af36eSbostic 			 * XXX check to see it is from ourself
1525a8c20e3Sbloom 			 */
153251af36eSbostic #ifdef sgi
154251af36eSbostic 			(void)cftime(newdate, "%D %T", &msg->tsp_time.tv_sec);
155251af36eSbostic #else
156251af36eSbostic 			(void)strcpy(newdate, ctime(&msg->tsp_time.tv_sec));
157251af36eSbostic #endif /* sgi */
158251af36eSbostic 			if (!good_host_name(msg->tsp_name)) {
1597ad1a809Sbloom 				syslog(LOG_NOTICE,
160251af36eSbostic 				       "attempted date change by %s to %s",
161251af36eSbostic 				       msg->tsp_name, newdate);
1629abb11fcSgusella 				spreadtime();
1639abb11fcSgusella 				break;
164251af36eSbostic 			}
165251af36eSbostic 
166251af36eSbostic 			mchgdate(msg);
167251af36eSbostic 			(void)gettimeofday(&ntime, 0);
168251af36eSbostic 			pollingtime = ntime.tv_sec + SAMPLEINTVL;
169251af36eSbostic 			break;
170251af36eSbostic 
171251af36eSbostic 		case TSP_SETDATEREQ:
172251af36eSbostic 			if (!fromnet || fromnet->status != MASTER)
173251af36eSbostic 				break;
174251af36eSbostic #ifdef sgi
175251af36eSbostic 			(void)cftime(newdate, "%D %T", &msg->tsp_time.tv_sec);
176251af36eSbostic #else
177251af36eSbostic 			(void)strcpy(newdate, ctime(&msg->tsp_time.tv_sec));
178251af36eSbostic #endif /* sgi */
179251af36eSbostic 			htp = findhost(msg->tsp_name);
180251af36eSbostic 			if (htp == 0) {
181251af36eSbostic 				syslog(LOG_ERR,
182251af36eSbostic 				       "attempted SET DATEREQ by uncontrolled %s to %s",
183251af36eSbostic 				       msg->tsp_name, newdate);
184251af36eSbostic 				break;
185251af36eSbostic 			}
186251af36eSbostic 			if (htp->seq == msg->tsp_seq)
187251af36eSbostic 				break;
188251af36eSbostic 			htp->seq = msg->tsp_seq;
189251af36eSbostic 			if (!htp->good) {
190251af36eSbostic 				syslog(LOG_NOTICE,
191251af36eSbostic 				"attempted SET DATEREQ by untrusted %s to %s",
192251af36eSbostic 				       msg->tsp_name, newdate);
193251af36eSbostic 				spreadtime();
194251af36eSbostic 				break;
195251af36eSbostic 			}
196251af36eSbostic 
197251af36eSbostic 			mchgdate(msg);
198251af36eSbostic 			(void)gettimeofday(&ntime, 0);
199251af36eSbostic 			pollingtime = ntime.tv_sec + SAMPLEINTVL;
200251af36eSbostic 			break;
201251af36eSbostic 
2029abb11fcSgusella 		case TSP_MSITE:
203251af36eSbostic 			xmit(TSP_ACK, msg->tsp_seq, &from);
204251af36eSbostic 			break;
205251af36eSbostic 
2069abb11fcSgusella 		case TSP_MSITEREQ:
2079abb11fcSgusella 			break;
208251af36eSbostic 
2099abb11fcSgusella 		case TSP_TRACEON:
210251af36eSbostic 			traceon();
2119abb11fcSgusella 			break;
212251af36eSbostic 
2139abb11fcSgusella 		case TSP_TRACEOFF:
214251af36eSbostic 			traceoff("Tracing ended at %s\n");
2159abb11fcSgusella 			break;
216251af36eSbostic 
2179abb11fcSgusella 		case TSP_ELECTION:
218251af36eSbostic 			if (!fromnet)
219251af36eSbostic 				break;
220251af36eSbostic 			if (fromnet->status == MASTER) {
221251af36eSbostic 				pollingtime = 0;
222251af36eSbostic 				(void)addmach(msg->tsp_name, &from,fromnet);
223251af36eSbostic 			}
224251af36eSbostic 			taddr = from;
225251af36eSbostic 			(void)strcpy(tname, msg->tsp_name);
2269abb11fcSgusella 			to.tsp_type = TSP_QUIT;
2279abb11fcSgusella 			(void)strcpy(to.tsp_name, hostname);
228251af36eSbostic 			answer = acksend(&to, &taddr, tname,
229251af36eSbostic 					 TSP_ACK, 0, 1);
2309abb11fcSgusella 			if (answer == NULL) {
231251af36eSbostic 				syslog(LOG_ERR, "election error by %s",
232251af36eSbostic 				       tname);
2339abb11fcSgusella 			}
2349abb11fcSgusella 			break;
235251af36eSbostic 
2369abb11fcSgusella 		case TSP_CONFLICT:
2379abb11fcSgusella 			/*
2389abb11fcSgusella 			 * After a network partition, there can be
2399abb11fcSgusella 			 * more than one master: the first slave to
2409abb11fcSgusella 			 * come up will notify here the situation.
2419abb11fcSgusella 			 */
242251af36eSbostic 			if (!fromnet || fromnet->status != MASTER)
243251af36eSbostic 				break;
2449abb11fcSgusella 			(void)strcpy(to.tsp_name, hostname);
2459abb11fcSgusella 
246251af36eSbostic 			/* The other master often gets into the same state,
247251af36eSbostic 			 * with boring results if we stay at it forever.
248251af36eSbostic 			 */
249251af36eSbostic 			ntp = fromnet;	/* (acksend() can leave fromnet=0 */
250251af36eSbostic 			for (i = 0; i < 3; i++) {
2519abb11fcSgusella 				to.tsp_type = TSP_RESOLVE;
252251af36eSbostic 				(void)strcpy(to.tsp_name, hostname);
253251af36eSbostic 				answer = acksend(&to, &ntp->dest_addr,
254251af36eSbostic 						 ANYADDR, TSP_MASTERACK,
255251af36eSbostic 						 ntp, 0);
256251af36eSbostic 				if (!answer)
2579abb11fcSgusella 					break;
258251af36eSbostic 				htp = addmach(answer->tsp_name,&from,ntp);
2599abb11fcSgusella 				to.tsp_type = TSP_QUIT;
260251af36eSbostic 				msg = acksend(&to, &htp->addr, htp->name,
261251af36eSbostic 					      TSP_ACK, 0, htp->noanswer);
2629abb11fcSgusella 				if (msg == NULL) {
263251af36eSbostic 					syslog(LOG_ERR,
264251af36eSbostic 				    "no response from %s to CONFLICT-QUIT",
265251af36eSbostic 					       htp->name);
2669abb11fcSgusella 				}
2679abb11fcSgusella 			}
268251af36eSbostic 			masterup(ntp);
2699abb11fcSgusella 			pollingtime = 0;
2709abb11fcSgusella 			break;
271251af36eSbostic 
2729abb11fcSgusella 		case TSP_RESOLVE:
273251af36eSbostic 			if (!fromnet || fromnet->status != MASTER)
274251af36eSbostic 				break;
2759abb11fcSgusella 			/*
2769abb11fcSgusella 			 * do not want to call synch() while waiting
2779abb11fcSgusella 			 * to be killed!
2789abb11fcSgusella 			 */
279251af36eSbostic 			(void)gettimeofday(&ntime, (struct timezone *)0);
280251af36eSbostic 			pollingtime = ntime.tv_sec + SAMPLEINTVL;
2819abb11fcSgusella 			break;
282251af36eSbostic 
2839abb11fcSgusella 		case TSP_QUIT:
284251af36eSbostic 			doquit(msg);		/* become a slave */
2859abb11fcSgusella 			break;
286251af36eSbostic 
287612ddc41Sbloom 		case TSP_LOOP:
288251af36eSbostic 			if (!fromnet || fromnet->status != MASTER
289251af36eSbostic 			    || !strcmp(msg->tsp_name, hostname))
290251af36eSbostic 				break;
291612ddc41Sbloom 			/*
292612ddc41Sbloom 			 * We should not have received this from a net
293251af36eSbostic 			 * we are master on.  There must be two masters.
294612ddc41Sbloom 			 */
295251af36eSbostic 			htp = addmach(msg->tsp_name, &from,fromnet);
296612ddc41Sbloom 			to.tsp_type = TSP_QUIT;
297612ddc41Sbloom 			(void)strcpy(to.tsp_name, hostname);
298251af36eSbostic 			answer = acksend(&to, &htp->addr, htp->name,
299251af36eSbostic 					 TSP_ACK, 0, 1);
300251af36eSbostic 			if (!answer) {
301b58f5466Skarels 				syslog(LOG_WARNING,
302251af36eSbostic 				"loop breakage: no reply from %s=%s to QUIT",
303251af36eSbostic 				    htp->name, inet_ntoa(htp->addr.sin_addr));
304251af36eSbostic 				(void)remmach(htp);
305612ddc41Sbloom 			}
306251af36eSbostic 
307251af36eSbostic 		case TSP_TEST:
308251af36eSbostic 			if (trace) {
309251af36eSbostic 				fprintf(fd,
310251af36eSbostic 		"\tnets = %d, masters = %d, slaves = %d, ignored = %d\n",
311251af36eSbostic 		nnets, nmasternets, nslavenets, nignorednets);
312251af36eSbostic 				setstatus();
313251af36eSbostic 			}
314251af36eSbostic 			pollingtime = 0;
315251af36eSbostic 			polls = POLLRATE-1;
316251af36eSbostic 			break;
317251af36eSbostic 
3189abb11fcSgusella 		default:
3199abb11fcSgusella 			if (trace) {
320251af36eSbostic 				fprintf(fd, "garbage message: ");
3218f3fa542Sbloom 				print(msg, &from);
3229abb11fcSgusella 			}
3239abb11fcSgusella 			break;
3249abb11fcSgusella 		}
3259abb11fcSgusella 	}
3269abb11fcSgusella 	goto loop;
3279abb11fcSgusella }
3289abb11fcSgusella 
3299abb11fcSgusella 
330251af36eSbostic /*
331251af36eSbostic  * change the system date on the master
332251af36eSbostic  */
333251af36eSbostic static void
mchgdate(msg)334251af36eSbostic mchgdate(msg)
335251af36eSbostic 	struct tsp *msg;
336251af36eSbostic {
337251af36eSbostic 	char tname[MAXHOSTNAMELEN];
338251af36eSbostic 	char olddate[32];
339251af36eSbostic 	struct timeval otime, ntime;
340251af36eSbostic 
341251af36eSbostic 	(void)strcpy(tname, msg->tsp_name);
342251af36eSbostic 
343251af36eSbostic 	xmit(TSP_DATEACK, msg->tsp_seq, &from);
344251af36eSbostic 
345251af36eSbostic 	(void)strcpy(olddate, date());
346251af36eSbostic 
347251af36eSbostic 	/* adjust time for residence on the queue */
348251af36eSbostic 	(void)gettimeofday(&otime, 0);
349251af36eSbostic 	adj_msg_time(msg,&otime);
350251af36eSbostic 
351251af36eSbostic 	timevalsub(&ntime, &msg->tsp_time, &otime);
352251af36eSbostic 	if (ntime.tv_sec < MAXADJ && ntime.tv_sec > -MAXADJ) {
353251af36eSbostic 		/*
354251af36eSbostic 		 * do not change the clock if we can adjust it
355251af36eSbostic 		 */
356251af36eSbostic 		dictate = 3;
357251af36eSbostic 		synch(tvtomsround(ntime));
358251af36eSbostic 	} else {
359251af36eSbostic #ifdef sgi
360251af36eSbostic 		if (0 > settimeofday(&msg->tsp_time, 0)) {
361251af36eSbostic 			syslog(LOG_ERR, "settimeofday(): %m");
362251af36eSbostic 		}
363251af36eSbostic 		logwtmp(&otime, &msg->tsp_time);
364251af36eSbostic #else
365251af36eSbostic 		logwtmp("|", "date", "");
366251af36eSbostic 		(void)settimeofday(&msg->tsp_time, 0);
367251af36eSbostic 		logwtmp("}", "date", "");
368251af36eSbostic #endif /* sgi */
369251af36eSbostic 		spreadtime();
370251af36eSbostic 	}
371251af36eSbostic 
372251af36eSbostic 	syslog(LOG_NOTICE, "date changed by %s from %s",
373251af36eSbostic 	       tname, olddate);
374251af36eSbostic }
375251af36eSbostic 
376251af36eSbostic 
377251af36eSbostic /*
378251af36eSbostic  * synchronize all of the slaves
379251af36eSbostic  */
380251af36eSbostic void
synch(mydelta)381bc6a4b57Sbloom synch(mydelta)
382bc6a4b57Sbloom 	long mydelta;
3839abb11fcSgusella {
384251af36eSbostic 	struct hosttbl *htp;
3859abb11fcSgusella 	int measure_status;
386251af36eSbostic 	struct timeval check, stop, wait;
387251af36eSbostic #ifdef sgi
388251af36eSbostic 	int pri;
389251af36eSbostic #endif /* sgi */
3909abb11fcSgusella 
391251af36eSbostic 	if (slvcount > 0) {
392bc6a4b57Sbloom 		if (trace)
393251af36eSbostic 			fprintf(fd, "measurements starting at %s\n", date());
394251af36eSbostic 		(void)gettimeofday(&check, 0);
395251af36eSbostic #ifdef sgi
396251af36eSbostic 		/* run fast to get good time */
397251af36eSbostic 		pri = schedctl(NDPRI,0,NDPHIMIN);
398251af36eSbostic 		if (pri < 0)
399251af36eSbostic 			syslog(LOG_ERR, "schedctl(): %m");
400251af36eSbostic #endif /* sgi */
401251af36eSbostic 		for (htp = self.l_fwd; htp != &self; htp = htp->l_fwd) {
402251af36eSbostic 			if (htp->noanswer != 0) {
403251af36eSbostic 				measure_status = measure(500, 100,
404251af36eSbostic 							 htp->name,
405251af36eSbostic 							 &htp->addr,0);
406bc6a4b57Sbloom 			} else {
407251af36eSbostic 				measure_status = measure(3000, 100,
408251af36eSbostic 							 htp->name,
409251af36eSbostic 							 &htp->addr,0);
410251af36eSbostic 			}
411251af36eSbostic 			if (measure_status != GOOD) {
412251af36eSbostic 				/* The slave did not respond.  We have
413251af36eSbostic 				 * just wasted lots of time on it.
414251af36eSbostic 				 */
415251af36eSbostic 				htp->delta = HOSTDOWN;
416251af36eSbostic 				if (++htp->noanswer >= LOSTHOST) {
417251af36eSbostic 					if (trace) {
418bc6a4b57Sbloom 						fprintf(fd,
419251af36eSbostic 					"purging %s for not answering ICMP\n",
420251af36eSbostic 							htp->name);
421251af36eSbostic 						(void)fflush(fd);
4229abb11fcSgusella 					}
423251af36eSbostic 					htp = remmach(htp);
4249abb11fcSgusella 				}
425bc6a4b57Sbloom 			} else {
426251af36eSbostic 				htp->delta = measure_delta;
427251af36eSbostic 			}
428251af36eSbostic 			(void)gettimeofday(&stop, 0);
429251af36eSbostic 			timevalsub(&stop, &stop, &check);
430251af36eSbostic 			if (stop.tv_sec >= 1) {
431251af36eSbostic 				if (trace)
432251af36eSbostic 					(void)fflush(fd);
433251af36eSbostic 				/*
434251af36eSbostic 				 * ack messages periodically
435251af36eSbostic 				 */
436251af36eSbostic 				wait.tv_sec = 0;
437251af36eSbostic 				wait.tv_usec = 0;
438251af36eSbostic 				if (0 != readmsg(TSP_TRACEON,ANYADDR,
439251af36eSbostic 						 &wait,0))
440251af36eSbostic 					traceon();
441251af36eSbostic 				(void)gettimeofday(&check, 0);
442251af36eSbostic 			}
443251af36eSbostic 		}
444251af36eSbostic #ifdef sgi
445251af36eSbostic 		if (pri >= 0)
446251af36eSbostic 			(void)schedctl(NDPRI,0,pri);
447251af36eSbostic #endif /* sgi */
448251af36eSbostic 		if (trace)
449251af36eSbostic 			fprintf(fd, "measurements finished at %s\n", date());
450251af36eSbostic 	}
451251af36eSbostic 	if (!(status & SLAVE)) {
452251af36eSbostic 		if (!dictate) {
453251af36eSbostic 			mydelta = networkdelta();
454251af36eSbostic 		} else {
455251af36eSbostic 			dictate--;
456251af36eSbostic 		}
457251af36eSbostic 	}
458251af36eSbostic 	if (trace && (mydelta != 0 || (status & SLAVE)))
459251af36eSbostic 		fprintf(fd,"local correction of %ld ms.\n", mydelta);
460bc6a4b57Sbloom 	correct(mydelta);
461bc6a4b57Sbloom }
4629abb11fcSgusella 
4639abb11fcSgusella /*
464251af36eSbostic  * sends the time to each slave after the master
4659abb11fcSgusella  * has received the command to set the network time
4669abb11fcSgusella  */
467251af36eSbostic void
spreadtime()4689abb11fcSgusella spreadtime()
4699abb11fcSgusella {
470251af36eSbostic 	struct hosttbl *htp;
4719abb11fcSgusella 	struct tsp to;
472251af36eSbostic 	struct tsp *answer;
4739abb11fcSgusella 
474251af36eSbostic /* Do not listen to the consensus after forcing the time.  This is because
475251af36eSbostic  *	the consensus takes a while to reach the time we are dictating.
476251af36eSbostic  */
477251af36eSbostic 	dictate = 2;
478251af36eSbostic 	for (htp = self.l_fwd; htp != &self; htp = htp->l_fwd) {
4799abb11fcSgusella 		to.tsp_type = TSP_SETTIME;
4809abb11fcSgusella 		(void)strcpy(to.tsp_name, hostname);
481251af36eSbostic 		(void)gettimeofday(&to.tsp_time, 0);
482251af36eSbostic 		answer = acksend(&to, &htp->addr, htp->name,
483251af36eSbostic 				 TSP_ACK, 0, htp->noanswer);
484251af36eSbostic 		if (answer == 0) {
485251af36eSbostic 			/* We client does not respond, then we have
486251af36eSbostic 			 * just wasted lots of time on it.
487251af36eSbostic 			 */
488b58f5466Skarels 			syslog(LOG_WARNING,
489251af36eSbostic 			       "no reply to SETTIME from %s", htp->name);
490251af36eSbostic 			if (++htp->noanswer >= LOSTHOST) {
491251af36eSbostic 				if (trace) {
492251af36eSbostic 					fprintf(fd,
493251af36eSbostic 					     "purging %s for not answering",
494251af36eSbostic 						htp->name);
495251af36eSbostic 					(void)fflush(fd);
496251af36eSbostic 				}
497251af36eSbostic 				htp = remmach(htp);
498251af36eSbostic 			}
4999abb11fcSgusella 		}
5009abb11fcSgusella 	}
5019abb11fcSgusella }
5029abb11fcSgusella 
503251af36eSbostic void
prthp(delta)504251af36eSbostic prthp(delta)
505251af36eSbostic 	clock_t delta;
506251af36eSbostic {
507251af36eSbostic 	static time_t next_time;
508251af36eSbostic 	time_t this_time;
509251af36eSbostic 	struct tms tm;
510251af36eSbostic 	struct hosttbl *htp;
511251af36eSbostic 	int length, l;
512251af36eSbostic 	int i;
513251af36eSbostic 
514251af36eSbostic 	if (!fd)			/* quit if tracing already off */
515251af36eSbostic 		return;
516251af36eSbostic 
517251af36eSbostic 	this_time = times(&tm);
518251af36eSbostic 	if (this_time + delta < next_time)
519251af36eSbostic 		return;
520251af36eSbostic 	next_time = this_time + CLK_TCK;
521251af36eSbostic 
522251af36eSbostic 	fprintf(fd, "host table: %d entries at %s\n", slvcount, date());
523251af36eSbostic 	htp = self.l_fwd;
524251af36eSbostic 	length = 1;
525251af36eSbostic 	for (i = 1; i <= slvcount; i++, htp = htp->l_fwd) {
526251af36eSbostic 		l = strlen(htp->name) + 1;
527251af36eSbostic 		if (length+l >= 80) {
528251af36eSbostic 			fprintf(fd, "\n");
529251af36eSbostic 			length = 0;
530251af36eSbostic 		}
531251af36eSbostic 		length += l;
532251af36eSbostic 		fprintf(fd, " %s", htp->name);
533251af36eSbostic 	}
534251af36eSbostic 	fprintf(fd, "\n");
535251af36eSbostic }
536251af36eSbostic 
537251af36eSbostic 
538251af36eSbostic static struct hosttbl *newhost_hash;
539251af36eSbostic static struct hosttbl *lasthfree = &hosttbl[0];
540251af36eSbostic 
541251af36eSbostic 
542251af36eSbostic struct hosttbl *			/* answer or 0 */
findhost(name)5439abb11fcSgusella findhost(name)
5449abb11fcSgusella 	char *name;
5459abb11fcSgusella {
546251af36eSbostic 	int i, j;
547251af36eSbostic 	struct hosttbl *htp;
548251af36eSbostic 	char *p;
5499abb11fcSgusella 
550251af36eSbostic 	j= 0;
551251af36eSbostic 	for (p = name, i = 0; i < 8 && *p != '\0'; i++, p++)
552251af36eSbostic 		j = (j << 2) ^ *p;
553251af36eSbostic 	newhost_hash = &hosttbl[j % NHOSTS];
554251af36eSbostic 
555251af36eSbostic 	htp = newhost_hash;
556251af36eSbostic 	if (htp->name[0] == '\0')
557251af36eSbostic 		return(0);
558251af36eSbostic 	do {
559251af36eSbostic 		if (!strcmp(name, htp->name))
560251af36eSbostic 			return(htp);
561251af36eSbostic 		htp = htp->h_fwd;
562251af36eSbostic 	} while (htp != newhost_hash);
563251af36eSbostic 	return(0);
5649abb11fcSgusella }
5659abb11fcSgusella 
5669abb11fcSgusella /*
567251af36eSbostic  * add a host to the list of controlled machines if not already there
5689abb11fcSgusella  */
569251af36eSbostic struct hosttbl *
addmach(name,addr,ntp)570251af36eSbostic addmach(name, addr, ntp)
5719abb11fcSgusella 	char *name;
572bc6a4b57Sbloom 	struct sockaddr_in *addr;
573251af36eSbostic 	struct netinfo *ntp;
5749abb11fcSgusella {
575251af36eSbostic 	struct hosttbl *ret, *p, *b, *f;
5769abb11fcSgusella 
5779abb11fcSgusella 	ret = findhost(name);
578251af36eSbostic 	if (ret == 0) {
579251af36eSbostic 		if (slvcount >= NHOSTS) {
580251af36eSbostic 			if (trace) {
581251af36eSbostic 				fprintf(fd, "no more slots in host table\n");
582251af36eSbostic 				prthp(CLK_TCK);
583251af36eSbostic 			}
584b58f5466Skarels 			syslog(LOG_ERR, "no more slots in host table");
585251af36eSbostic 			Mflag = 0;
586251af36eSbostic 			longjmp(jmpenv, 2); /* give up and be a slave */
587251af36eSbostic 		}
588251af36eSbostic 
589251af36eSbostic 		/* if our home hash slot is occupied, find a free entry
590251af36eSbostic 		 * in the hash table
591251af36eSbostic 		 */
592251af36eSbostic 		if (newhost_hash->name[0] != '\0') {
593251af36eSbostic 			do {
594251af36eSbostic 				ret = lasthfree;
595251af36eSbostic 				if (++lasthfree > &hosttbl[NHOSTS])
596251af36eSbostic 					lasthfree = &hosttbl[1];
597251af36eSbostic 			} while (ret->name[0] != '\0');
598251af36eSbostic 
599251af36eSbostic 			if (!newhost_hash->head) {
600251af36eSbostic 				/* Move an interloper using our home.  Use
601251af36eSbostic 				 * scratch pointers in case the new head is
602251af36eSbostic 				 * pointing to itself.
603251af36eSbostic 				 */
604251af36eSbostic 				f = newhost_hash->h_fwd;
605251af36eSbostic 				b = newhost_hash->h_bak;
606251af36eSbostic 				f->h_bak = ret;
607251af36eSbostic 				b->h_fwd = ret;
608251af36eSbostic 				f = newhost_hash->l_fwd;
609251af36eSbostic 				b = newhost_hash->l_bak;
610251af36eSbostic 				f->l_bak = ret;
611251af36eSbostic 				b->l_fwd = ret;
612251af36eSbostic 				bcopy(newhost_hash,ret,sizeof(*ret));
613251af36eSbostic 				ret = newhost_hash;
614251af36eSbostic 				ret->head = 1;
615251af36eSbostic 				ret->h_fwd = ret;
616251af36eSbostic 				ret->h_bak = ret;
617251af36eSbostic 			} else {
618251af36eSbostic 				/* link to an existing chain in our home
619251af36eSbostic 				 */
620251af36eSbostic 				ret->head = 0;
621251af36eSbostic 				p = newhost_hash->h_bak;
622251af36eSbostic 				ret->h_fwd = newhost_hash;
623251af36eSbostic 				ret->h_bak = p;
624251af36eSbostic 				p->h_fwd = ret;
625251af36eSbostic 				newhost_hash->h_bak = ret;
6269abb11fcSgusella 			}
6279abb11fcSgusella 		} else {
628251af36eSbostic 			ret = newhost_hash;
629251af36eSbostic 			ret->head = 1;
630251af36eSbostic 			ret->h_fwd = ret;
631251af36eSbostic 			ret->h_bak = ret;
6329abb11fcSgusella 		}
633251af36eSbostic 		ret->addr = *addr;
634251af36eSbostic 		ret->ntp = ntp;
635251af36eSbostic 		(void)strncpy(ret->name, name, sizeof(ret->name));
636251af36eSbostic 		ret->good = good_host_name(name);
637251af36eSbostic 		ret->l_fwd = &self;
638251af36eSbostic 		ret->l_bak = self.l_bak;
639251af36eSbostic 		self.l_bak->l_fwd = ret;
640251af36eSbostic 		self.l_bak = ret;
641251af36eSbostic 		slvcount++;
642251af36eSbostic 
643251af36eSbostic 		ret->noanswer = 0;
644251af36eSbostic 		ret->need_set = 1;
645251af36eSbostic 
646251af36eSbostic 	} else {
647251af36eSbostic 		ret->noanswer = (ret->noanswer != 0);
648251af36eSbostic 	}
649251af36eSbostic 
650251af36eSbostic 	/* need to clear sequence number anyhow */
651251af36eSbostic 	ret->seq = 0;
6529abb11fcSgusella 	return(ret);
6539abb11fcSgusella }
6549abb11fcSgusella 
655052451aeSbloom /*
656251af36eSbostic  * remove the machine with the given index in the host table.
657251af36eSbostic  */
658251af36eSbostic struct hosttbl *
remmach(htp)659251af36eSbostic remmach(htp)
660251af36eSbostic 	struct hosttbl *htp;
661251af36eSbostic {
662251af36eSbostic 	struct hosttbl *lprv, *hnxt, *f, *b;
663251af36eSbostic 
664251af36eSbostic 	if (trace)
665251af36eSbostic 		fprintf(fd, "remove %s\n", htp->name);
666251af36eSbostic 
667251af36eSbostic 	/* get out of the lists */
668251af36eSbostic 	htp->l_fwd->l_bak = lprv = htp->l_bak;
669251af36eSbostic 	htp->l_bak->l_fwd = htp->l_fwd;
670251af36eSbostic 	htp->h_fwd->h_bak = htp->h_bak;
671251af36eSbostic 	htp->h_bak->h_fwd = hnxt = htp->h_fwd;
672251af36eSbostic 
673251af36eSbostic 	/* If we are in the home slot, pull up the chain */
674251af36eSbostic 	if (htp->head && hnxt != htp) {
675251af36eSbostic 		if (lprv == hnxt)
676251af36eSbostic 			lprv = htp;
677251af36eSbostic 
678251af36eSbostic 		/* Use scratch pointers in case the new head is pointing to
679251af36eSbostic 		 * itself.
680251af36eSbostic 		 */
681251af36eSbostic 		f = hnxt->h_fwd;
682251af36eSbostic 		b = hnxt->h_bak;
683251af36eSbostic 		f->h_bak = htp;
684251af36eSbostic 		b->h_fwd = htp;
685251af36eSbostic 		f = hnxt->l_fwd;
686251af36eSbostic 		b = hnxt->l_bak;
687251af36eSbostic 		f->l_bak = htp;
688251af36eSbostic 		b->l_fwd = htp;
689251af36eSbostic 		hnxt->head = 1;
690251af36eSbostic 		bcopy(hnxt, htp, sizeof(*htp));
691251af36eSbostic 		lasthfree = hnxt;
692251af36eSbostic 	} else {
693251af36eSbostic 		lasthfree = htp;
694251af36eSbostic 	}
695251af36eSbostic 
696251af36eSbostic 	lasthfree->name[0] = '\0';
697251af36eSbostic 	lasthfree->h_fwd = 0;
698251af36eSbostic 	lasthfree->l_fwd = 0;
699251af36eSbostic 	slvcount--;
700251af36eSbostic 
701251af36eSbostic 	return lprv;
702251af36eSbostic }
703251af36eSbostic 
704251af36eSbostic 
705251af36eSbostic /*
706052451aeSbloom  * Remove all the machines from the host table that exist on the given
707052451aeSbloom  * network.  This is called when a master transitions to a slave on a
708052451aeSbloom  * given network.
709052451aeSbloom  */
710251af36eSbostic void
rmnetmachs(ntp)711052451aeSbloom rmnetmachs(ntp)
712251af36eSbostic 	struct netinfo *ntp;
713052451aeSbloom {
714251af36eSbostic 	struct hosttbl *htp;
715052451aeSbloom 
716052451aeSbloom 	if (trace)
717251af36eSbostic 		prthp(CLK_TCK);
718251af36eSbostic 	for (htp = self.l_fwd; htp != &self; htp = htp->l_fwd) {
719251af36eSbostic 		if (ntp == htp->ntp)
720251af36eSbostic 			htp = remmach(htp);
721251af36eSbostic 	}
722052451aeSbloom 	if (trace)
723251af36eSbostic 		prthp(CLK_TCK);
724052451aeSbloom }
725052451aeSbloom 
726251af36eSbostic void
masterup(net)727bc6a4b57Sbloom masterup(net)
728bc6a4b57Sbloom 	struct netinfo *net;
7299abb11fcSgusella {
730251af36eSbostic 	xmit(TSP_MASTERUP, 0, &net->dest_addr);
7319abb11fcSgusella 
732251af36eSbostic 	/*
733251af36eSbostic 	 * Do not tell new slaves our time for a while.  This ensures
734251af36eSbostic 	 * we do not tell them to start using our time, before we have
735251af36eSbostic 	 * found a good master.
736251af36eSbostic 	 */
737251af36eSbostic 	(void)gettimeofday(&net->slvwait, 0);
738bc6a4b57Sbloom }
7399abb11fcSgusella 
740251af36eSbostic void
newslave(msg)741251af36eSbostic newslave(msg)
742251af36eSbostic 	struct tsp *msg;
7438f3fa542Sbloom {
744251af36eSbostic 	struct hosttbl *htp;
745251af36eSbostic 	struct tsp *answer, to;
746251af36eSbostic 	struct timeval now;
7478f3fa542Sbloom 
748251af36eSbostic 	if (!fromnet || fromnet->status != MASTER)
749251af36eSbostic 		return;
750251af36eSbostic 
751251af36eSbostic 	htp = addmach(msg->tsp_name, &from,fromnet);
752251af36eSbostic 	htp->seq = msg->tsp_seq;
7538f3fa542Sbloom 	if (trace)
754251af36eSbostic 		prthp(0);
755251af36eSbostic 
756251af36eSbostic 	/*
757251af36eSbostic 	 * If we are stable, send our time to the slave.
758251af36eSbostic 	 * Do not go crazy if the date has been changed.
759251af36eSbostic 	 */
760251af36eSbostic 	(void)gettimeofday(&now, 0);
761251af36eSbostic 	if (now.tv_sec >= fromnet->slvwait.tv_sec+3
762251af36eSbostic 	    || now.tv_sec < fromnet->slvwait.tv_sec) {
7638f3fa542Sbloom 		to.tsp_type = TSP_SETTIME;
7648f3fa542Sbloom 		(void)strcpy(to.tsp_name, hostname);
765251af36eSbostic 		(void)gettimeofday(&to.tsp_time, 0);
766251af36eSbostic 		answer = acksend(&to, &htp->addr,
767251af36eSbostic 				 htp->name, TSP_ACK,
768251af36eSbostic 				 0, htp->noanswer);
769251af36eSbostic 		if (answer) {
770251af36eSbostic 			htp->need_set = 0;
771251af36eSbostic 		} else {
772b58f5466Skarels 			syslog(LOG_WARNING,
773251af36eSbostic 			       "no reply to initial SETTIME from %s",
774251af36eSbostic 			       htp->name);
775251af36eSbostic 			htp->noanswer = LOSTHOST;
7768f3fa542Sbloom 		}
7778f3fa542Sbloom 	}
7788f3fa542Sbloom }
779251af36eSbostic 
780251af36eSbostic 
781251af36eSbostic /*
782251af36eSbostic  * react to a TSP_QUIT:
783251af36eSbostic  */
784251af36eSbostic void
doquit(msg)785251af36eSbostic doquit(msg)
786251af36eSbostic 	struct tsp *msg;
787251af36eSbostic {
788251af36eSbostic 	if (fromnet->status == MASTER) {
789251af36eSbostic 		if (!good_host_name(msg->tsp_name)) {
790251af36eSbostic 			if (fromnet->quit_count <= 0) {
791251af36eSbostic 				syslog(LOG_NOTICE,"untrusted %s told us QUIT",
792251af36eSbostic 				       msg->tsp_name);
793251af36eSbostic 				suppress(&from, msg->tsp_name, fromnet);
794251af36eSbostic 				fromnet->quit_count = 1;
795251af36eSbostic 				return;
796251af36eSbostic 			}
797251af36eSbostic 			syslog(LOG_NOTICE, "untrusted %s told us QUIT twice",
798251af36eSbostic 			       msg->tsp_name);
799251af36eSbostic 			fromnet->quit_count = 2;
800251af36eSbostic 			fromnet->status = NOMASTER;
801251af36eSbostic 		} else {
802251af36eSbostic 			fromnet->status = SLAVE;
803251af36eSbostic 		}
804251af36eSbostic 		rmnetmachs(fromnet);
805251af36eSbostic 		longjmp(jmpenv, 2);		/* give up and be a slave */
806251af36eSbostic 
807251af36eSbostic 	} else {
808251af36eSbostic 		if (!good_host_name(msg->tsp_name)) {
809251af36eSbostic 			syslog(LOG_NOTICE, "untrusted %s told us QUIT",
810251af36eSbostic 			       msg->tsp_name);
811251af36eSbostic 			fromnet->quit_count = 2;
812251af36eSbostic 		}
813251af36eSbostic 	}
814251af36eSbostic }
815251af36eSbostic 
816251af36eSbostic void
traceon()817251af36eSbostic traceon()
818251af36eSbostic {
819251af36eSbostic 	if (!fd) {
820251af36eSbostic 		fd = fopen(_PATH_TIMEDLOG, "w");
821251af36eSbostic 		if (!fd) {
822251af36eSbostic 			trace = 0;
823251af36eSbostic 			return;
824251af36eSbostic 		}
825251af36eSbostic 		fprintf(fd,"Tracing started at %s\n", date());
826251af36eSbostic 	}
827251af36eSbostic 	trace = 1;
828251af36eSbostic 	get_goodgroup(1);
829251af36eSbostic 	setstatus();
830251af36eSbostic 	prthp(CLK_TCK);
831251af36eSbostic }
832251af36eSbostic 
833251af36eSbostic 
834251af36eSbostic void
traceoff(msg)835251af36eSbostic traceoff(msg)
836251af36eSbostic 	char *msg;
837251af36eSbostic {
838251af36eSbostic 	get_goodgroup(1);
839251af36eSbostic 	setstatus();
840251af36eSbostic 	prthp(CLK_TCK);
841251af36eSbostic 	if (trace) {
842251af36eSbostic 		fprintf(fd, msg, date());
843251af36eSbostic 		(void)fclose(fd);
844251af36eSbostic 		fd = 0;
845251af36eSbostic 	}
846251af36eSbostic #ifdef GPROF
847251af36eSbostic 	moncontrol(0);
848251af36eSbostic 	_mcleanup();
849251af36eSbostic 	moncontrol(1);
850251af36eSbostic #endif
851251af36eSbostic 	trace = OFF;
852251af36eSbostic }
853251af36eSbostic 
854251af36eSbostic 
855251af36eSbostic #ifdef sgi
856251af36eSbostic void
logwtmp(otime,ntime)857251af36eSbostic logwtmp(otime, ntime)
858251af36eSbostic 	struct timeval *otime, *ntime;
859251af36eSbostic {
860251af36eSbostic 	static struct utmp wtmp[2] = {
861251af36eSbostic 		{"","",OTIME_MSG,0,OLD_TIME,0,0,0},
862251af36eSbostic 		{"","",NTIME_MSG,0,NEW_TIME,0,0,0}
863251af36eSbostic 	};
864251af36eSbostic 	static char *wtmpfile = WTMP_FILE;
865251af36eSbostic 	int f;
866251af36eSbostic 
867251af36eSbostic 	wtmp[0].ut_time = otime->tv_sec + (otime->tv_usec + 500000) / 1000000;
868251af36eSbostic 	wtmp[1].ut_time = ntime->tv_sec + (ntime->tv_usec + 500000) / 1000000;
869251af36eSbostic 	if (wtmp[0].ut_time == wtmp[1].ut_time)
870251af36eSbostic 		return;
871251af36eSbostic 
872251af36eSbostic 	setutent();
873251af36eSbostic 	(void)pututline(&wtmp[0]);
874251af36eSbostic 	(void)pututline(&wtmp[1]);
875251af36eSbostic 	endutent();
876251af36eSbostic 	if ((f = open(wtmpfile, O_WRONLY|O_APPEND)) >= 0) {
877251af36eSbostic 		(void) write(f, (char *)wtmp, sizeof(wtmp));
878251af36eSbostic 		(void) close(f);
879251af36eSbostic 	}
880251af36eSbostic }
881251af36eSbostic #endif /* sgi */
882