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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)master.c 8.1 (Berkeley) 6/6/93
34 * $FreeBSD: src/usr.sbin/timed/timed/master.c,v 1.6 1999/08/28 01:20:17 peter Exp $
35 * $DragonFly: src/usr.sbin/timed/timed/master.c,v 1.8 2004/09/05 02:20:15 dillon Exp $
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 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 gettimeofday(&ntime, 0);
100 wait.tv_sec = pollingtime - ntime.tv_sec;
104 msg = readmsg(TSP_ANY, ANYADDR, &wait, 0);
106 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 strlcpy(to.tsp_name, hostname,
142 sizeof(to.tsp_name));
144 if (sendto(sock, (char *)&to,
145 sizeof(struct tsp), 0,
146 (struct sockaddr*)&ntp->dest_addr,
147 sizeof(ntp->dest_addr)) < 0) {
148 trace_sendto_err(ntp->dest_addr.sin_addr);
155 switch (msg->tsp_type) {
166 * XXX check to see it is from ourself
168 tsp_time_sec = msg->tsp_time.tv_sec;
169 strlcpy(newdate, ctime(&tsp_time_sec), sizeof(newdate));
170 if (!good_host_name(msg->tsp_name)) {
172 "attempted date change by %s to %s",
173 msg->tsp_name, newdate);
179 gettimeofday(&ntime, 0);
180 pollingtime = ntime.tv_sec + SAMPLEINTVL;
184 if (!fromnet || fromnet->status != MASTER)
186 tsp_time_sec = msg->tsp_time.tv_sec;
187 strlcpy(newdate, ctime(&tsp_time_sec), sizeof(newdate));
188 htp = findhost(msg->tsp_name);
191 "attempted SET DATEREQ by uncontrolled %s to %s",
192 msg->tsp_name, newdate);
195 if (htp->seq == msg->tsp_seq)
197 htp->seq = msg->tsp_seq;
200 "attempted SET DATEREQ by untrusted %s to %s",
201 msg->tsp_name, newdate);
207 gettimeofday(&ntime, 0);
208 pollingtime = ntime.tv_sec + SAMPLEINTVL;
212 xmit(TSP_ACK, msg->tsp_seq, &from);
223 traceoff("Tracing ended at %s\n");
229 if (fromnet->status == MASTER) {
231 addmach(msg->tsp_name, &from,fromnet);
234 strlcpy(tname, msg->tsp_name, sizeof(tname));
235 to.tsp_type = TSP_QUIT;
236 strlcpy(to.tsp_name, hostname, sizeof(to.tsp_name));
237 answer = acksend(&to, &taddr, tname,
239 if (answer == NULL) {
240 syslog(LOG_ERR, "election error by %s",
247 * After a network partition, there can be
248 * more than one master: the first slave to
249 * come up will notify here the situation.
251 if (!fromnet || fromnet->status != MASTER)
253 strlcpy(to.tsp_name, hostname, sizeof(to.tsp_name));
255 /* The other master often gets into the same state,
256 * with boring results if we stay at it forever.
258 ntp = fromnet; /* (acksend() can leave fromnet=0 */
259 for (i = 0; i < 3; i++) {
260 to.tsp_type = TSP_RESOLVE;
261 strlcpy(to.tsp_name, hostname,
262 sizeof(to.tsp_name));
263 answer = acksend(&to, &ntp->dest_addr,
264 ANYADDR, TSP_MASTERACK,
268 htp = addmach(answer->tsp_name,&from,ntp);
269 to.tsp_type = TSP_QUIT;
270 msg = acksend(&to, &htp->addr, htp->name,
271 TSP_ACK, 0, htp->noanswer);
274 "no response from %s to CONFLICT-QUIT",
283 if (!fromnet || fromnet->status != MASTER)
286 * do not want to call synch() while waiting
289 gettimeofday(&ntime, (struct timezone *)0);
290 pollingtime = ntime.tv_sec + SAMPLEINTVL;
294 doquit(msg); /* become a slave */
298 if (!fromnet || fromnet->status != MASTER
299 || !strcmp(msg->tsp_name, hostname))
302 * We should not have received this from a net
303 * we are master on. There must be two masters.
305 htp = addmach(msg->tsp_name, &from,fromnet);
306 to.tsp_type = TSP_QUIT;
307 strlcpy(to.tsp_name, hostname, sizeof(to.tsp_name));
308 answer = acksend(&to, &htp->addr, htp->name,
312 "loop breakage: no reply from %s=%s to QUIT",
313 htp->name, inet_ntoa(htp->addr.sin_addr));
320 "\tnets = %d, masters = %d, slaves = %d, ignored = %d\n",
321 nnets, nmasternets, nslavenets, nignorednets);
330 fprintf(fd, "garbage message: ");
341 * change the system date on the master
344 mchgdate(struct tsp *msg)
346 char tname[MAXHOSTNAMELEN];
348 struct timeval otime, ntime;
350 strlcpy(tname, msg->tsp_name, sizeof(tname));
352 xmit(TSP_DATEACK, msg->tsp_seq, &from);
354 strlcpy(olddate, date(), sizeof(olddate));
356 /* adjust time for residence on the queue */
357 gettimeofday(&otime, 0);
358 adj_msg_time(msg,&otime);
360 timevalsub(&ntime, &msg->tsp_time, &otime);
361 if (ntime.tv_sec < MAXADJ && ntime.tv_sec > -MAXADJ) {
363 * do not change the clock if we can adjust it
366 synch(tvtomsround(ntime));
368 logwtmp("|", "date", "");
369 settimeofday(&msg->tsp_time, 0);
370 logwtmp("{", "date", "");
374 syslog(LOG_NOTICE, "date changed by %s from %s",
380 * synchronize all of the slaves
387 struct timeval check, stop, wait;
391 fprintf(fd, "measurements starting at %s\n", date());
392 gettimeofday(&check, 0);
393 for (htp = self.l_fwd; htp != &self; htp = htp->l_fwd) {
394 if (htp->noanswer != 0) {
395 measure_status = measure(500, 100,
399 measure_status = measure(3000, 100,
403 if (measure_status != GOOD) {
404 /* The slave did not respond. We have
405 * just wasted lots of time on it.
407 htp->delta = HOSTDOWN;
408 if (++htp->noanswer >= LOSTHOST) {
411 "purging %s for not answering ICMP\n",
418 htp->delta = measure_delta;
420 gettimeofday(&stop, 0);
421 timevalsub(&stop, &stop, &check);
422 if (stop.tv_sec >= 1) {
426 * ack messages periodically
430 if (0 != readmsg(TSP_TRACEON,ANYADDR,
433 gettimeofday(&check, 0);
437 fprintf(fd, "measurements finished at %s\n", date());
439 if (!(status & SLAVE)) {
441 mydelta = networkdelta();
446 if (trace && (mydelta != 0 || (status & SLAVE)))
447 fprintf(fd,"local correction of %ld ms.\n", mydelta);
452 * sends the time to each slave after the master
453 * has received the command to set the network time
462 /* Do not listen to the consensus after forcing the time. This is because
463 * the consensus takes a while to reach the time we are dictating.
466 for (htp = self.l_fwd; htp != &self; htp = htp->l_fwd) {
467 to.tsp_type = TSP_SETTIME;
468 strlcpy(to.tsp_name, hostname, sizeof(to.tsp_name));
469 gettimeofday(&to.tsp_time, 0);
470 answer = acksend(&to, &htp->addr, htp->name,
471 TSP_ACK, 0, htp->noanswer);
473 /* We client does not respond, then we have
474 * just wasted lots of time on it.
477 "no reply to SETTIME from %s", htp->name);
478 if (++htp->noanswer >= LOSTHOST) {
481 "purging %s for not answering",
494 static time_t next_time;
501 if (!fd) /* quit if tracing already off */
504 this_time = times(&tm);
505 if (this_time + (time_t)delta < next_time)
507 next_time = this_time + CLK_TCK;
509 fprintf(fd, "host table: %d entries at %s\n", slvcount, date());
512 for (i = 1; i <= slvcount; i++, htp = htp->l_fwd) {
513 l = strlen(htp->name) + 1;
514 if (length+l >= 80) {
519 fprintf(fd, " %s", htp->name);
525 static struct hosttbl *newhost_hash;
526 static struct hosttbl *lasthfree = &hosttbl[0];
529 struct hosttbl * /* answer or 0 */
537 for (p = name, i = 0; i < 8 && *p != '\0'; i++, p++)
539 newhost_hash = &hosttbl[j % NHOSTS];
542 if (htp->name[0] == '\0')
545 if (!strcmp(name, htp->name))
548 } while (htp != newhost_hash);
553 * add a host to the list of controlled machines if not already there
556 addmach(char *name, struct sockaddr_in *addr, struct netinfo *ntp)
558 struct hosttbl *ret, *p, *b, *f;
560 ret = findhost(name);
562 if (slvcount >= NHOSTS) {
564 fprintf(fd, "no more slots in host table\n");
567 syslog(LOG_ERR, "no more slots in host table");
569 longjmp(jmpenv, 2); /* give up and be a slave */
572 /* if our home hash slot is occupied, find a free entry
575 if (newhost_hash->name[0] != '\0') {
578 if (++lasthfree > &hosttbl[NHOSTS])
579 lasthfree = &hosttbl[1];
580 } while (ret->name[0] != '\0');
582 if (!newhost_hash->head) {
583 /* Move an interloper using our home. Use
584 * scratch pointers in case the new head is
585 * pointing to itself.
587 f = newhost_hash->h_fwd;
588 b = newhost_hash->h_bak;
591 f = newhost_hash->l_fwd;
592 b = newhost_hash->l_bak;
595 bcopy(newhost_hash,ret,sizeof(*ret));
601 /* link to an existing chain in our home
604 p = newhost_hash->h_bak;
605 ret->h_fwd = newhost_hash;
608 newhost_hash->h_bak = ret;
618 strlcpy(ret->name, name, sizeof(ret->name));
619 ret->good = good_host_name(name);
621 ret->l_bak = self.l_bak;
622 self.l_bak->l_fwd = ret;
630 ret->noanswer = (ret->noanswer != 0);
633 /* need to clear sequence number anyhow */
639 * remove the machine with the given index in the host table.
642 remmach(struct hosttbl *htp)
644 struct hosttbl *lprv, *hnxt, *f, *b;
647 fprintf(fd, "remove %s\n", htp->name);
649 /* get out of the lists */
650 htp->l_fwd->l_bak = lprv = htp->l_bak;
651 htp->l_bak->l_fwd = htp->l_fwd;
652 htp->h_fwd->h_bak = htp->h_bak;
653 htp->h_bak->h_fwd = hnxt = htp->h_fwd;
655 /* If we are in the home slot, pull up the chain */
656 if (htp->head && hnxt != htp) {
660 /* Use scratch pointers in case the new head is pointing to
672 bcopy(hnxt, htp, sizeof(*htp));
678 lasthfree->name[0] = '\0';
679 lasthfree->h_fwd = 0;
680 lasthfree->l_fwd = 0;
688 * Remove all the machines from the host table that exist on the given
689 * network. This is called when a master transitions to a slave on a
693 rmnetmachs(struct netinfo *ntp)
699 for (htp = self.l_fwd; htp != &self; htp = htp->l_fwd) {
708 masterup(struct netinfo *net)
711 xmit(TSP_MASTERUP, 0, &net->dest_addr);
714 * Do not tell new slaves our time for a while. This ensures
715 * we do not tell them to start using our time, before we have
716 * found a good master.
718 gettimeofday(&net->slvwait, 0);
722 newslave(struct tsp *msg)
725 struct tsp *answer, to;
728 if (!fromnet || fromnet->status != MASTER)
731 htp = addmach(msg->tsp_name, &from,fromnet);
732 htp->seq = msg->tsp_seq;
737 * If we are stable, send our time to the slave.
738 * Do not go crazy if the date has been changed.
740 gettimeofday(&now, 0);
741 if (now.tv_sec >= fromnet->slvwait.tv_sec+3
742 || now.tv_sec < fromnet->slvwait.tv_sec) {
743 to.tsp_type = TSP_SETTIME;
744 strlcpy(to.tsp_name, hostname, sizeof(to.tsp_name));
745 gettimeofday(&to.tsp_time, 0);
746 answer = acksend(&to, &htp->addr,
753 "no reply to initial SETTIME from %s",
755 htp->noanswer = LOSTHOST;
762 * react to a TSP_QUIT:
765 doquit(struct tsp *msg)
768 if (fromnet->status == MASTER) {
769 if (!good_host_name(msg->tsp_name)) {
770 if (fromnet->quit_count <= 0) {
771 syslog(LOG_NOTICE,"untrusted %s told us QUIT",
773 suppress(&from, msg->tsp_name, fromnet);
774 fromnet->quit_count = 1;
777 syslog(LOG_NOTICE, "untrusted %s told us QUIT twice",
779 fromnet->quit_count = 2;
780 fromnet->status = NOMASTER;
782 fromnet->status = SLAVE;
785 longjmp(jmpenv, 2); /* give up and be a slave */
788 if (!good_host_name(msg->tsp_name)) {
789 syslog(LOG_NOTICE, "untrusted %s told us QUIT",
791 fromnet->quit_count = 2;
801 fd = fopen(_PATH_TIMEDLOG, "w");
806 fprintf(fd,"Tracing started at %s\n", date());
823 fprintf(fd, msg, date());