1/*- 2 * Copyright (c) 1985, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 20 * 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 31 * SUCH DAMAGE. 32 */ 33 34#ifndef lint 35#if 0 36static char sccsid[] = "@(#)correct.c 8.1 (Berkeley) 6/6/93"; 37#endif 38static const char rcsid[] = 39 "$FreeBSD: src/usr.sbin/timed/timed/correct.c,v 1.8 2007/11/07 10:53:41 kevlo Exp $"; 40#endif /* not lint */ 41#include <sys/cdefs.h> 42 43#include "globals.h" 44#include <math.h> 45#include <sys/types.h> 46#include <sys/times.h> 47 48static void adjclock(struct timeval *); 49 50/* 51 * sends to the slaves the corrections for their clocks after fixing our 52 * own 53 */ 54void 55correct(avdelta) 56 long avdelta; 57{ 58 struct hosttbl *htp; 59 int corr; 60 struct timeval adjlocal, tmptv; 61 struct tsp to; 62 struct tsp *answer; 63 64 mstotvround(&adjlocal, avdelta); 65 66 for (htp = self.l_fwd; htp != &self; htp = htp->l_fwd) { 67 if (htp->delta != HOSTDOWN) { 68 corr = avdelta - htp->delta; 69/* If the other machine is off in the weeds, set its time directly. 70 * If a slave gets the wrong day, the original code would simply 71 * fix the minutes. If you fix a network partition, you can get 72 * into such situations. 73 */ 74 if (htp->need_set 75 || corr >= MAXADJ*1000 76 || corr <= -MAXADJ*1000) { 77 htp->need_set = 0; 78 (void)gettimeofday(&tmptv,0); 79 timevaladd(&tmptv, &adjlocal); 80 to.tsp_time.tv_sec = tmptv.tv_sec; 81 to.tsp_time.tv_usec = tmptv.tv_usec; 82 to.tsp_type = TSP_SETTIME; 83 } else { 84 tmptv.tv_sec = to.tsp_time.tv_sec; 85 tmptv.tv_usec = to.tsp_time.tv_usec; 86 mstotvround(&tmptv, corr); 87 to.tsp_time.tv_sec = tmptv.tv_sec; 88 to.tsp_time.tv_usec = tmptv.tv_usec; 89 to.tsp_type = TSP_ADJTIME; 90 } 91 (void)strcpy(to.tsp_name, hostname); 92 answer = acksend(&to, &htp->addr, htp->name, 93 TSP_ACK, 0, 0); 94 if (!answer) { 95 htp->delta = HOSTDOWN; 96 syslog(LOG_WARNING, 97 "no reply to time correction from %s", 98 htp->name); 99 if (++htp->noanswer >= LOSTHOST) { 100 if (trace) { 101 fprintf(fd, 102 "purging %s for not answering\n", 103 htp->name); 104 (void)fflush(fd); 105 } 106 htp = remmach(htp); 107 } 108 } 109 } 110 } 111 112 /* 113 * adjust our own clock now that we are not sending it out 114 */ 115 adjclock(&adjlocal); 116} 117 118 119static void 120adjclock(corr) 121 struct timeval *corr; 122{ 123 static int passes = 0; 124 static int smoother = 0; 125 long delta; /* adjustment in usec */ 126 long ndelta; 127 struct timeval now; 128 struct timeval adj; 129 130 if (!timerisset(corr)) 131 return; 132 133 adj = *corr; 134 if (adj.tv_sec < MAXADJ && adj.tv_sec > - MAXADJ) { 135 delta = adj.tv_sec*1000000 + adj.tv_usec; 136 /* If the correction is less than the minimum round 137 * trip time for an ICMP packet, and thus 138 * less than the likely error in the measurement, 139 * do not do the entire correction. Do half 140 * or a quarter of it. 141 */ 142 143 if (delta > -MIN_ROUND*1000 144 && delta < MIN_ROUND*1000) { 145 if (smoother <= 4) 146 smoother++; 147 ndelta = delta >> smoother; 148 if (trace) 149 fprintf(fd, 150 "trimming delta %ld usec to %ld\n", 151 delta, ndelta); 152 adj.tv_usec = ndelta; 153 adj.tv_sec = 0; 154 } else if (smoother > 0) { 155 smoother--; 156 } 157 if (0 > adjtime(corr, 0)) { 158 syslog(LOG_ERR, "adjtime: %m"); 159 } 160 if (passes > 1 161 && (delta < -BIG_ADJ || delta > BIG_ADJ)) { 162 smoother = 0; 163 passes = 0; 164 syslog(LOG_WARNING, 165 "large time adjustment of %+.3f sec", 166 delta/1000000.0); 167 } 168 } else { 169 syslog(LOG_WARNING, 170 "clock correction %ld sec too large to adjust", 171 adj.tv_sec); 172 (void) gettimeofday(&now, 0); 173 timevaladd(&now, corr); 174 if (settimeofday(&now, 0) < 0) 175 syslog(LOG_ERR, "settimeofday: %m"); 176 } 177} 178 179 180/* adjust the time in a message by the time it 181 * spent in the queue 182 */ 183void 184adj_msg_time(msg, now) 185 struct tsp *msg; 186 struct timeval *now; 187{ 188 msg->tsp_time.tv_sec += (now->tv_sec - from_when.tv_sec); 189 msg->tsp_time.tv_usec += (now->tv_usec - from_when.tv_usec); 190 191 while (msg->tsp_time.tv_usec < 0) { 192 msg->tsp_time.tv_sec--; 193 msg->tsp_time.tv_usec += 1000000; 194 } 195 while (msg->tsp_time.tv_usec >= 1000000) { 196 msg->tsp_time.tv_sec++; 197 msg->tsp_time.tv_usec -= 1000000; 198 } 199} 200