1: /*
   2:  * Copyright (c) 1985 Regents of the University of California.
   3:  * All rights reserved.  The Berkeley software License Agreement
   4:  * specifies the terms and conditions for redistribution.
   5:  */
   6: 
   7: #ifndef lint
   8: static char sccsid[] = "@(#)correct.c	2.3 (Berkeley) 5/28/86";
   9: #endif not lint
  10: 
  11: #include "globals.h"
  12: #include <protocols/timed.h>
  13: 
  14: #ifdef MEASURE
  15: extern FILE *fp;
  16: #endif
  17: 
  18: /*
  19:  * `correct' sends to the slaves the corrections for their clocks
  20:  */
  21: 
  22: correct(avdelta)
  23: long avdelta;
  24: {
  25:     int i;
  26:     long corr;
  27:     struct timeval adjlocal;
  28:     struct tsp msgs;
  29:     struct timeval mstotvround();
  30:     struct tsp *answer, *acksend();
  31: 
  32: #ifdef MEASURE
  33:     for(i=0; i<slvcount; i++) {
  34:         if (hp[i].delta == HOSTDOWN)
  35:             fprintf(fp, "%s\t", "down");
  36:         else {
  37:             fprintf(fp, "%ld\t", hp[i].delta);
  38:         }
  39:     }
  40:     fprintf(fp, "\n");
  41: #endif
  42:     corr = avdelta - hp[0].delta;
  43:     adjlocal = mstotvround(&corr);
  44:     adjclock(&adjlocal);
  45: #ifdef MEASURE
  46:     fprintf(fp, "%ld\t", corr);
  47: #endif
  48: 
  49:     for(i=1; i<slvcount; i++) {
  50:         if (hp[i].delta != HOSTDOWN)  {
  51:             corr = avdelta - hp[i].delta;
  52:             msgs.tsp_time = mstotvround(&corr);
  53:             msgs.tsp_type = (u_char)TSP_ADJTIME;
  54:             (void)strcpy(msgs.tsp_name, hostname);
  55:             answer = acksend(&msgs, &hp[i].addr, hp[i].name,
  56:                 TSP_ACK, (struct netinfo *)NULL);
  57:             if (answer == NULL) {
  58:                 hp[i].delta = HOSTDOWN;
  59: #ifdef MEASURE
  60:                 fprintf(fp, "%s\t", "down");
  61:             } else {
  62:                 fprintf(fp, "%ld\t", corr);
  63: #endif
  64:             }
  65:         } else {
  66: #ifdef MEASURE
  67:             fprintf(fp, "%s\t", "down");
  68: #endif
  69:         }
  70:     }
  71: #ifdef MEASURE
  72:     fprintf(fp, "\n");
  73: #endif
  74: }
  75: 
  76: /*
  77:  * `mstotvround' rounds up the value of the argument to the
  78:  * nearest multiple of five, and converts it into a timeval
  79:  */
  80: 
  81: struct timeval mstotvround(x)
  82: long *x;
  83: {
  84:     int temp;
  85:     struct timeval adj;
  86: 
  87:     temp = *x % 5;
  88:     if (temp >= 3)
  89:         *x = *x-temp+5;
  90:     else {
  91:         if (temp <= -3)
  92:             *x = *x - temp -5;
  93:         else
  94:             *x = *x-temp;
  95:     }
  96:     adj.tv_sec = *x/1000;
  97:     adj.tv_usec = (*x-adj.tv_sec*1000)*1000;
  98:     if (adj.tv_usec < 0) {
  99:         adj.tv_usec += 1000000;
 100:         adj.tv_sec--;
 101:     }
 102:     return(adj);
 103: }
 104: 
 105: adjclock(corr)
 106: struct timeval *corr;
 107: {
 108:     struct timeval now;
 109: 
 110:     if (timerisset(corr)) {
 111:         if (corr->tv_sec < MAXADJ && corr->tv_sec > - MAXADJ) {
 112:             (void)adjtime(corr, (struct timeval *)0);
 113:         } else {
 114:             syslog(LOG_WARNING,
 115:                 "clock correction too large to adjust (%ld sec)",
 116:                 corr->tv_sec);
 117:             (void) gettimeofday(&now, (struct timezone *)0);
 118:             timevaladd(&now, corr);
 119:             if (settimeofday(&now, (struct timezone *)0) < 0)
 120:                 syslog(LOG_ERR, "can't set time");
 121:         }
 122:     }
 123: }
 124: 
 125: timevaladd(tv1, tv2)
 126:     register struct timeval *tv1, *tv2;
 127: {
 128: 
 129:     tv1->tv_sec += tv2->tv_sec;
 130:     tv1->tv_usec += tv2->tv_usec;
 131:     if (tv1->tv_usec >= 1000000) {
 132:         tv1->tv_sec++;
 133:         tv1->tv_usec -= 1000000;
 134:     }
 135:     if (tv1->tv_usec < 0) {
 136:         tv1->tv_sec--;
 137:         tv1->tv_usec += 1000000;
 138:     }
 139: }

Defined functions

adjclock defined in line 105; used 2 times
correct defined in line 22; used 4 times
mstotvround defined in line 81; used 3 times
timevaladd defined in line 125; used 1 times

Defined variables

sccsid defined in line 8; never used
Last modified: 1988-12-25
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 3519
Valid CSS Valid XHTML 1.0 Strict