date and time created 85/06/22 22:07:51 by gusella
[unix-history] / usr / src / usr.sbin / timed / timed / correct.c
/*
* Copyright (c) 1983 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
*/
#ifndef lint
static char sccsid[] = "@(#)correct.c 1.1 (Berkeley) %G%";
#endif not lint
#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
/*
* `correct' sends to the slaves the corrections for their clocks
*/
correct(avdelta)
long avdelta;
{
int i;
int corr;
char *strcpy();
struct timeval adjlocal;
struct tsp msgs;
struct timeval mstotvround();
struct tsp *answer, *acksend();
#ifdef MEASURE
for(i=0; i<slvcount; i++) {
if (hp[i].delta == HOSTDOWN)
fprintf(fp, "%s\t", "down");
else {
fprintf(fp, "%d\t", hp[i].delta);
}
}
fprintf(fp, "\n");
#endif
corr = avdelta - hp[0].delta;
adjlocal = mstotvround(&corr);
adjclock(&adjlocal);
#ifdef MEASURE
fprintf(fp, "%d\t", corr);
#endif
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);
answer = acksend(&msgs, hp[i].name, TSP_ACK);
if (answer == NULL) {
hp[i].delta = HOSTDOWN;
#ifdef MEASURE
fprintf(fp, "%s\t", "down");
} else {
fprintf(fp, "%d\t", corr);
#endif
}
} else {
#ifdef MEASURE
fprintf(fp, "%s\t", "down");
#endif
}
}
#ifdef MEASURE
fprintf(fp, "\n");
(void)fflush(fp);
#endif
}
/*
* `mstotvround' rounds up the value of the argument to the
* nearest multiple of five, and converts it into a timeval
*/
struct timeval mstotvround(x)
int *x;
{
int temp;
struct timeval adj;
temp = *x % 5;
if (temp >= 3)
*x = *x-temp+5;
else {
if (temp <= -3)
*x = *x - temp -5;
else
*x = *x-temp;
}
adj.tv_sec = *x/1000;
adj.tv_usec = (*x-adj.tv_sec*1000)*1000;
if (adj.tv_usec < 0) {
adj.tv_usec += 1000000;
adj.tv_sec--;
}
return(adj);
}
adjclock(corr)
struct timeval *corr;
{
if (timerisset(corr)) {
if (corr->tv_sec < SAMPLEINTVL/10 &&
corr->tv_sec > - SAMPLEINTVL/10) {
(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\n");
}
}
}