2 * Copyright (c) 1985, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static char sccsid[] = "@(#)master.c 8.1 (Berkeley) 6/6/93";
34 static const char rcsid[] =
40 #include <sys/types.h>
41 #include <sys/times.h>
44 #include "pathnames.h"
46 extern int measure_delta;
47 extern jmp_buf jmpenv;
52 static int slvcount; /* slaves listening to our clock */
54 static void mchgdate(struct tsp *);
57 * The main function of `master' is to periodically compute the differences
58 * (deltas) between its clock and the clocks of the slaves, to compute the
59 * network average delta, and to send to the slaves the differences between
60 * their individual deltas and the network delta.
61 * While waiting, it receives messages from the slaves (i.e. requests for
62 * master's name, remote requests to set the network time, ...), and
63 * takes the appropriate action.
72 struct timeval wait, ntime;
74 struct tsp *msg, *answer, to;
76 struct sockaddr_in taddr;
77 char tname[MAXHOSTNAMELEN];
81 syslog(LOG_NOTICE, "This machine is master");
83 fprintf(fd, "This machine is master\n");
84 for (ntp = nettab; ntp != NULL; ntp = ntp->next) {
85 if (ntp->status == MASTER)
88 (void)gettimeofday(&ntime, 0);
89 pollingtime = ntime.tv_sec+3;
95 /* Process all outstanding messages before spending the long time necessary
96 * to update all timers.
99 (void)gettimeofday(&ntime, 0);
100 wait.tv_sec = pollingtime - ntime.tv_sec;
104 msg = readmsg(TSP_ANY, ANYADDR, &wait, 0);
106 (void)gettimeofday(&ntime, 0);
107 if (ntime.tv_sec >= pollingtime) {
108 pollingtime = ntime.tv_sec + SAMPLEINTVL;
111 /* If a bogus master told us to quit, we can have decided to ignore a
112 * network. Therefore, periodically try to take over everything.
114 polls = (polls + 1) % POLLRATE;
115 if (0 == polls && nignorednets > 0) {
116 trace_msg("Looking for nets to re-master\n");
117 for (ntp = nettab; ntp; ntp = ntp->next) {
118 if (ntp->status == IGNORE
119 || ntp->status == NOMASTER) {
121 if (ntp->status == MASTER) {
126 if (ntp->status == MASTER
127 && --ntp->quit_count < 0)
136 for (ntp = nettab; ntp != NULL; ntp = ntp->next) {
137 to.tsp_type = TSP_LOOP;
138 to.tsp_vers = TSPVERSION;
139 to.tsp_seq = sequence++;
140 to.tsp_hopcnt = MAX_HOPCNT;
141 (void)strcpy(to.tsp_name, hostname);
143 if (sendto(sock, (char *)&to,
144 sizeof(struct tsp), 0,
145 (struct sockaddr*)&ntp->dest_addr,
146 sizeof(ntp->dest_addr)) < 0) {
147 trace_sendto_err(ntp->dest_addr.sin_addr);
154 switch (msg->tsp_type) {
165 * XXX check to see it is from ourself
167 tsp_time_sec = msg->tsp_time.tv_sec;
168 (void)strcpy(newdate, ctime(&tsp_time_sec));
169 if (!good_host_name(msg->tsp_name)) {
171 "attempted date change by %s to %s",
172 msg->tsp_name, newdate);
178 (void)gettimeofday(&ntime, 0);
179 pollingtime = ntime.tv_sec + SAMPLEINTVL;
183 if (!fromnet || fromnet->status != MASTER)
185 tsp_time_sec = msg->tsp_time.tv_sec;
186 (void)strcpy(newdate, ctime(&tsp_time_sec));
187 htp = findhost(msg->tsp_name);
190 "attempted SET DATEREQ by uncontrolled %s to %s",
191 msg->tsp_name, newdate);
194 if (htp->seq == msg->tsp_seq)
196 htp->seq = msg->tsp_seq;
199 "attempted SET DATEREQ by untrusted %s to %s",
200 msg->tsp_name, newdate);
206 (void)gettimeofday(&ntime, 0);
207 pollingtime = ntime.tv_sec + SAMPLEINTVL;
211 xmit(TSP_ACK, msg->tsp_seq, &from);
222 traceoff("Tracing ended at %s\n");
228 if (fromnet->status == MASTER) {
230 (void)addmach(msg->tsp_name, &from,fromnet);
233 (void)strcpy(tname, msg->tsp_name);
234 to.tsp_type = TSP_QUIT;
235 (void)strcpy(to.tsp_name, hostname);
236 answer = acksend(&to, &taddr, tname,
238 if (answer == NULL) {
239 syslog(LOG_ERR, "election error by %s",
246 * After a network partition, there can be
247 * more than one master: the first slave to
248 * come up will notify here the situation.
250 if (!fromnet || fromnet->status != MASTER)
252 (void)strcpy(to.tsp_name, hostname);
254 /* The other master often gets into the same state,
255 * with boring results if we stay at it forever.
257 ntp = fromnet; /* (acksend() can leave fromnet=0 */
258 for (i = 0; i < 3; i++) {
259 to.tsp_type = TSP_RESOLVE;
260 (void)strcpy(to.tsp_name, hostname);
261 answer = acksend(&to, &ntp->dest_addr,
262 ANYADDR, TSP_MASTERACK,
266 htp = addmach(answer->tsp_name,&from,ntp);
267 to.tsp_type = TSP_QUIT;
268 msg = acksend(&to, &htp->addr, htp->name,
269 TSP_ACK, 0, htp->noanswer);
272 "no response from %s to CONFLICT-QUIT",
281 if (!fromnet || fromnet->status != MASTER)
284 * do not want to call synch() while waiting
287 (void)gettimeofday(&ntime, (struct timezone *)0);
288 pollingtime = ntime.tv_sec + SAMPLEINTVL;
292 doquit(msg); /* become a slave */
296 if (!fromnet || fromnet->status != MASTER
297 || !strcmp(msg->tsp_name, hostname))
300 * We should not have received this from a net
301 * we are master on. There must be two masters.
303 htp = addmach(msg->tsp_name, &from,fromnet);
304 to.tsp_type = TSP_QUIT;
305 (void)strcpy(to.tsp_name, hostname);
306 answer = acksend(&to, &htp->addr, htp->name,
310 "loop breakage: no reply from %s=%s to QUIT",
311 htp->name, inet_ntoa(htp->addr.sin_addr));
318 "\tnets = %d, masters = %d, slaves = %d, ignored = %d\n",
319 nnets, nmasternets, nslavenets, nignorednets);
328 fprintf(fd, "garbage message: ");
339 * change the system date on the master
345 char tname[MAXHOSTNAMELEN];
347 struct timeval otime, ntime, tmptv;
350 (void)strcpy(tname, msg->tsp_name);
352 xmit(TSP_DATEACK, msg->tsp_seq, &from);
354 (void)strcpy(olddate, date());
356 /* adjust time for residence on the queue */
357 (void)gettimeofday(&otime, 0);
358 adj_msg_time(msg,&otime);
360 tmptv.tv_sec = msg->tsp_time.tv_sec;
361 tmptv.tv_usec = msg->tsp_time.tv_usec;
362 timevalsub(&ntime, &tmptv, &otime);
363 if (ntime.tv_sec < MAXADJ && ntime.tv_sec > -MAXADJ) {
365 * do not change the clock if we can adjust it
368 synch(tvtomsround(ntime));
370 utx.ut_type = OLD_TIME;
371 gettimeofday(&utx.ut_tv, NULL);
373 (void)settimeofday(&tmptv, 0);
374 utx.ut_type = NEW_TIME;
375 gettimeofday(&utx.ut_tv, NULL);
380 syslog(LOG_NOTICE, "date changed by %s from %s",
386 * synchronize all of the slaves
394 struct timeval check, stop, wait;
398 fprintf(fd, "measurements starting at %s\n", date());
399 (void)gettimeofday(&check, 0);
400 for (htp = self.l_fwd; htp != &self; htp = htp->l_fwd) {
401 if (htp->noanswer != 0) {
402 measure_status = measure(500, 100,
406 measure_status = measure(3000, 100,
410 if (measure_status != GOOD) {
411 /* The slave did not respond. We have
412 * just wasted lots of time on it.
414 htp->delta = HOSTDOWN;
415 if (++htp->noanswer >= LOSTHOST) {
418 "purging %s for not answering ICMP\n",
425 htp->delta = measure_delta;
427 (void)gettimeofday(&stop, 0);
428 timevalsub(&stop, &stop, &check);
429 if (stop.tv_sec >= 1) {
433 * ack messages periodically
437 if (0 != readmsg(TSP_TRACEON,ANYADDR,
440 (void)gettimeofday(&check, 0);
444 fprintf(fd, "measurements finished at %s\n", date());
446 if (!(status & SLAVE)) {
448 mydelta = networkdelta();
453 if (trace && (mydelta != 0 || (status & SLAVE)))
454 fprintf(fd,"local correction of %ld ms.\n", mydelta);
459 * sends the time to each slave after the master
460 * has received the command to set the network time
468 struct timeval tmptv;
470 /* Do not listen to the consensus after forcing the time. This is because
471 * the consensus takes a while to reach the time we are dictating.
474 for (htp = self.l_fwd; htp != &self; htp = htp->l_fwd) {
475 to.tsp_type = TSP_SETTIME;
476 (void)strcpy(to.tsp_name, hostname);
477 (void)gettimeofday(&tmptv, 0);
478 to.tsp_time.tv_sec = tmptv.tv_sec;
479 to.tsp_time.tv_usec = tmptv.tv_usec;
480 answer = acksend(&to, &htp->addr, htp->name,
481 TSP_ACK, 0, htp->noanswer);
483 /* We client does not respond, then we have
484 * just wasted lots of time on it.
487 "no reply to SETTIME from %s", htp->name);
488 if (++htp->noanswer >= LOSTHOST) {
491 "purging %s for not answering",
505 static time_t next_time;
512 if (!fd) /* quit if tracing already off */
515 this_time = times(&tm);
516 if ((time_t)(this_time + delta) < next_time)
518 next_time = this_time + CLK_TCK;
520 fprintf(fd, "host table: %d entries at %s\n", slvcount, date());
523 for (i = 1; i <= slvcount; i++, htp = htp->l_fwd) {
524 l = strlen(htp->name) + 1;
525 if (length+l >= 80) {
530 fprintf(fd, " %s", htp->name);
536 static struct hosttbl *newhost_hash;
537 static struct hosttbl *lasthfree = &hosttbl[0];
540 struct hosttbl * /* answer or 0 */
549 for (p = name, i = 0; i < 8 && *p != '\0'; i++, p++)
551 newhost_hash = &hosttbl[j % NHOSTS];
554 if (htp->name[0] == '\0')
557 if (!strcmp(name, htp->name))
560 } while (htp != newhost_hash);
565 * add a host to the list of controlled machines if not already there
568 addmach(name, addr, ntp)
570 struct sockaddr_in *addr;
573 struct hosttbl *ret, *p, *b, *f;
575 ret = findhost(name);
577 if (slvcount >= NHOSTS) {
579 fprintf(fd, "no more slots in host table\n");
582 syslog(LOG_ERR, "no more slots in host table");
584 longjmp(jmpenv, 2); /* give up and be a slave */
587 /* if our home hash slot is occupied, find a free entry
590 if (newhost_hash->name[0] != '\0') {
593 if (++lasthfree > &hosttbl[NHOSTS])
594 lasthfree = &hosttbl[1];
595 } while (ret->name[0] != '\0');
597 if (!newhost_hash->head) {
598 /* Move an interloper using our home. Use
599 * scratch pointers in case the new head is
600 * pointing to itself.
602 f = newhost_hash->h_fwd;
603 b = newhost_hash->h_bak;
606 f = newhost_hash->l_fwd;
607 b = newhost_hash->l_bak;
610 bcopy(newhost_hash,ret,sizeof(*ret));
616 /* link to an existing chain in our home
619 p = newhost_hash->h_bak;
620 ret->h_fwd = newhost_hash;
623 newhost_hash->h_bak = ret;
633 (void)strncpy(ret->name, name, sizeof(ret->name));
634 ret->good = good_host_name(name);
636 ret->l_bak = self.l_bak;
637 self.l_bak->l_fwd = ret;
645 ret->noanswer = (ret->noanswer != 0);
648 /* need to clear sequence number anyhow */
654 * remove the machine with the given index in the host table.
660 struct hosttbl *lprv, *hnxt, *f, *b;
663 fprintf(fd, "remove %s\n", htp->name);
665 /* get out of the lists */
666 htp->l_fwd->l_bak = lprv = htp->l_bak;
667 htp->l_bak->l_fwd = htp->l_fwd;
668 htp->h_fwd->h_bak = htp->h_bak;
669 htp->h_bak->h_fwd = hnxt = htp->h_fwd;
671 /* If we are in the home slot, pull up the chain */
672 if (htp->head && hnxt != htp) {
676 /* Use scratch pointers in case the new head is pointing to
688 bcopy(hnxt, htp, sizeof(*htp));
694 lasthfree->name[0] = '\0';
695 lasthfree->h_fwd = 0;
696 lasthfree->l_fwd = 0;
704 * Remove all the machines from the host table that exist on the given
705 * network. This is called when a master transitions to a slave on a
716 for (htp = self.l_fwd; htp != &self; htp = htp->l_fwd) {
728 xmit(TSP_MASTERUP, 0, &net->dest_addr);
731 * Do not tell new slaves our time for a while. This ensures
732 * we do not tell them to start using our time, before we have
733 * found a good master.
735 (void)gettimeofday(&net->slvwait, 0);
743 struct tsp *answer, to;
744 struct timeval now, tmptv;
746 if (!fromnet || fromnet->status != MASTER)
749 htp = addmach(msg->tsp_name, &from,fromnet);
750 htp->seq = msg->tsp_seq;
755 * If we are stable, send our time to the slave.
756 * Do not go crazy if the date has been changed.
758 (void)gettimeofday(&now, 0);
759 if (now.tv_sec >= fromnet->slvwait.tv_sec+3
760 || now.tv_sec < fromnet->slvwait.tv_sec) {
761 to.tsp_type = TSP_SETTIME;
762 (void)strcpy(to.tsp_name, hostname);
763 (void)gettimeofday(&tmptv, 0);
764 to.tsp_time.tv_sec = tmptv.tv_sec;
765 to.tsp_time.tv_usec = tmptv.tv_usec;
766 answer = acksend(&to, &htp->addr,
773 "no reply to initial SETTIME from %s",
775 htp->noanswer = LOSTHOST;
782 * react to a TSP_QUIT:
788 if (fromnet->status == MASTER) {
789 if (!good_host_name(msg->tsp_name)) {
790 if (fromnet->quit_count <= 0) {
791 syslog(LOG_NOTICE,"untrusted %s told us QUIT",
793 suppress(&from, msg->tsp_name, fromnet);
794 fromnet->quit_count = 1;
797 syslog(LOG_NOTICE, "untrusted %s told us QUIT twice",
799 fromnet->quit_count = 2;
800 fromnet->status = NOMASTER;
802 fromnet->status = SLAVE;
805 longjmp(jmpenv, 2); /* give up and be a slave */
808 if (!good_host_name(msg->tsp_name)) {
809 syslog(LOG_NOTICE, "untrusted %s told us QUIT",
811 fromnet->quit_count = 2;
820 fd = fopen(_PATH_TIMEDLOG, "w");
825 fprintf(fd,"Tracing started at %s\n", date());
842 fprintf(fd, msg, date());