BSD 4_3_Net_2 release
[unix-history] / usr / src / usr.sbin / timed / timed / correct.c
index 420fa8e..285ca38 100644 (file)
@@ -1,20 +1,43 @@
 /*
 /*
- * Copyright (c) 1983 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * Copyright (c) 1985 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)correct.c  1.2 (Berkeley) %G%";
-#endif not lint
+static char sccsid[] = "@(#)correct.c  2.6 (Berkeley) 6/1/90";
+#endif /* not lint */
 
 #include "globals.h"
 #include <protocols/timed.h>
 
 
 #include "globals.h"
 #include <protocols/timed.h>
 
-extern int slvcount;
-extern struct host hp[];
-extern char hostname[];
-extern struct sockaddr_in server;
 #ifdef MEASURE
 extern FILE *fp;
 #endif
 #ifdef MEASURE
 extern FILE *fp;
 #endif
@@ -28,7 +51,6 @@ long avdelta;
 {
        int i;
        int corr;
 {
        int i;
        int corr;
-       char *strcpy();
        struct timeval adjlocal;
        struct tsp msgs;
        struct timeval mstotvround();
        struct timeval adjlocal;
        struct tsp msgs;
        struct timeval mstotvround();
@@ -53,14 +75,12 @@ long avdelta;
 
        for(i=1; i<slvcount; i++) {
                if (hp[i].delta != HOSTDOWN)  {
 
        for(i=1; i<slvcount; i++) {
                if (hp[i].delta != HOSTDOWN)  {
-                       bcopy((char *)&hp[i].addr, 
-                                       (char *)&(server.sin_addr.s_addr),
-                                               hp[i].length);
                        corr = avdelta - hp[i].delta;
                        msgs.tsp_time = mstotvround(&corr);
                        msgs.tsp_type = (u_char)TSP_ADJTIME;
                        (void)strcpy(msgs.tsp_name, hostname);
                        corr = avdelta - hp[i].delta;
                        msgs.tsp_time = mstotvround(&corr);
                        msgs.tsp_type = (u_char)TSP_ADJTIME;
                        (void)strcpy(msgs.tsp_name, hostname);
-                       answer = acksend(&msgs, hp[i].name, TSP_ACK);
+                       answer = acksend(&msgs, &hp[i].addr, hp[i].name,
+                           TSP_ACK, (struct netinfo *)NULL);
                        if (answer == NULL) {
                                hp[i].delta = HOSTDOWN;
 #ifdef MEASURE
                        if (answer == NULL) {
                                hp[i].delta = HOSTDOWN;
 #ifdef MEASURE
@@ -77,7 +97,6 @@ long avdelta;
        }
 #ifdef MEASURE
        fprintf(fp, "\n");
        }
 #ifdef MEASURE
        fprintf(fp, "\n");
-       (void)fflush(fp);
 #endif
 }
 
 #endif
 }
 
@@ -113,18 +132,35 @@ int *x;
 adjclock(corr)
 struct timeval *corr;
 {
 adjclock(corr)
 struct timeval *corr;
 {
+       struct timeval now;
+
        if (timerisset(corr)) {
        if (timerisset(corr)) {
-               if (corr->tv_sec < SAMPLEINTVL/10 && 
-                                       corr->tv_sec > - SAMPLEINTVL/10) {
+               if (corr->tv_sec < MAXADJ && corr->tv_sec > - MAXADJ) {
                        (void)adjtime(corr, (struct timeval *)0);
                } else {
                        (void)adjtime(corr, (struct timeval *)0);
                } else {
-                       corr->tv_usec = 0;
-                       if (corr->tv_sec > 0)
-                               corr->tv_sec = SAMPLEINTVL/10 - 2;
-                       else
-                               corr->tv_sec = - SAMPLEINTVL/10 + 2;
-                       (void)adjtime(corr, (struct timeval *)0);
-                       syslog(LOG_WARNING, "timed: adjclock called with too large a parameter");
+                       syslog(LOG_WARNING,
+                           "clock correction too large to adjust (%d sec)",
+                           corr->tv_sec);
+                       (void) gettimeofday(&now, (struct timezone *)0);
+                       timevaladd(&now, corr);
+                       if (settimeofday(&now, (struct timezone *)0) < 0)
+                               syslog(LOG_ERR, "can't set time");
                }
        }
 }
                }
        }
 }
+
+timevaladd(tv1, tv2)
+       register struct timeval *tv1, *tv2;
+{
+       
+       tv1->tv_sec += tv2->tv_sec;
+       tv1->tv_usec += tv2->tv_usec;
+       if (tv1->tv_usec >= 1000000) {
+               tv1->tv_sec++;
+               tv1->tv_usec -= 1000000;
+       }
+       if (tv1->tv_usec < 0) {
+               tv1->tv_sec--;
+               tv1->tv_usec += 1000000;
+       }
+}