]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/timed/timed/correct.c
Merge OpenSSL 1.0.2n.
[FreeBSD/FreeBSD.git] / usr.sbin / timed / timed / correct.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1985, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 #ifndef lint
33 #if 0
34 static char sccsid[] = "@(#)correct.c   8.1 (Berkeley) 6/6/93";
35 #endif
36 static const char rcsid[] =
37   "$FreeBSD$";
38 #endif /* not lint */
39
40 #include "globals.h"
41 #include <math.h>
42 #include <sys/types.h>
43 #include <sys/times.h>
44
45 static void adjclock(struct timeval *);
46
47 /*
48  * sends to the slaves the corrections for their clocks after fixing our
49  * own
50  */
51 void
52 correct(long avdelta)
53 {
54         struct hosttbl *htp;
55         int corr;
56         struct timeval adjlocal, tmptv;
57         struct tsp to;
58         struct tsp *answer;
59
60         mstotvround(&adjlocal, avdelta);
61
62         for (htp = self.l_fwd; htp != &self; htp = htp->l_fwd) {
63                 if (htp->delta != HOSTDOWN)  {
64                         corr = avdelta - htp->delta;
65 /* If the other machine is off in the weeds, set its time directly.
66  *      If a slave gets the wrong day, the original code would simply
67  *      fix the minutes.  If you fix a network partition, you can get
68  *      into such situations.
69  */
70                         if (htp->need_set
71                             || corr >= MAXADJ*1000
72                             || corr <= -MAXADJ*1000) {
73                                 htp->need_set = 0;
74                                 (void)gettimeofday(&tmptv,0);
75                                 timevaladd(&tmptv, &adjlocal);
76                                 to.tsp_time.tv_sec = tmptv.tv_sec;
77                                 to.tsp_time.tv_usec = tmptv.tv_usec;
78                                 to.tsp_type = TSP_SETTIME;
79                         } else {
80                                 tmptv.tv_sec = to.tsp_time.tv_sec;
81                                 tmptv.tv_usec = to.tsp_time.tv_usec;
82                                 mstotvround(&tmptv, corr);
83                                 to.tsp_time.tv_sec = tmptv.tv_sec;
84                                 to.tsp_time.tv_usec = tmptv.tv_usec;
85                                 to.tsp_type = TSP_ADJTIME;
86                         }
87                         (void)strcpy(to.tsp_name, hostname);
88                         answer = acksend(&to, &htp->addr, htp->name,
89                                          TSP_ACK, 0, 0);
90                         if (!answer) {
91                                 htp->delta = HOSTDOWN;
92                                 syslog(LOG_WARNING,
93                                        "no reply to time correction from %s",
94                                        htp->name);
95                                 if (++htp->noanswer >= LOSTHOST) {
96                                         if (trace) {
97                                                 fprintf(fd,
98                                              "purging %s for not answering\n",
99                                                         htp->name);
100                                                 (void)fflush(fd);
101                                         }
102                                         htp = remmach(htp);
103                                 }
104                         }
105                 }
106         }
107
108         /*
109          * adjust our own clock now that we are not sending it out
110          */
111         adjclock(&adjlocal);
112 }
113
114
115 static void
116 adjclock(struct timeval *corr)
117 {
118         static int passes = 0;
119         static int smoother = 0;
120         long delta;                     /* adjustment in usec */
121         long ndelta;
122         struct timeval now;
123         struct timeval adj;
124
125         if (!timerisset(corr))
126                 return;
127
128         adj = *corr;
129         if (adj.tv_sec < MAXADJ && adj.tv_sec > - MAXADJ) {
130                 delta = adj.tv_sec*1000000 + adj.tv_usec;
131                 /* If the correction is less than the minimum round
132                  *      trip time for an ICMP packet, and thus
133                  *      less than the likely error in the measurement,
134                  *      do not do the entire correction.  Do half
135                  *      or a quarter of it.
136                  */
137
138                 if (delta > -MIN_ROUND*1000
139                     && delta < MIN_ROUND*1000) {
140                         if (smoother <= 4)
141                                 smoother++;
142                         ndelta = delta >> smoother;
143                         if (trace)
144                                 fprintf(fd,
145                                         "trimming delta %ld usec to %ld\n",
146                                         delta, ndelta);
147                         adj.tv_usec = ndelta;
148                         adj.tv_sec = 0;
149                 } else if (smoother > 0) {
150                         smoother--;
151                 }
152                 if (0 > adjtime(corr, 0)) {
153                         syslog(LOG_ERR, "adjtime: %m");
154                 }
155                 if (passes > 1
156                     && (delta < -BIG_ADJ || delta > BIG_ADJ)) {
157                         smoother = 0;
158                         passes = 0;
159                         syslog(LOG_WARNING,
160                                "large time adjustment of %+.3f sec",
161                                delta/1000000.0);
162                 }
163         } else {
164                 syslog(LOG_WARNING,
165                        "clock correction %jd sec too large to adjust",
166                        (intmax_t)adj.tv_sec);
167                 (void) gettimeofday(&now, 0);
168                 timevaladd(&now, corr);
169                 if (settimeofday(&now, 0) < 0)
170                         syslog(LOG_ERR, "settimeofday: %m");
171         }
172 }
173
174
175 /* adjust the time in a message by the time it
176  *      spent in the queue
177  */
178 void
179 adj_msg_time(struct tsp *msg, struct timeval *now)
180 {
181         msg->tsp_time.tv_sec += (now->tv_sec - from_when.tv_sec);
182         msg->tsp_time.tv_usec += (now->tv_usec - from_when.tv_usec);
183
184         while (msg->tsp_time.tv_usec < 0) {
185                 msg->tsp_time.tv_sec--;
186                 msg->tsp_time.tv_usec += 1000000;
187         }
188         while (msg->tsp_time.tv_usec >= 1000000) {
189                 msg->tsp_time.tv_sec++;
190                 msg->tsp_time.tv_usec -= 1000000;
191         }
192 }