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