* Copyright (c) 1983 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
static char sccsid
[] = "@(#)master.c 2.2 (Berkeley) %G%";
#include <protocols/timed.h>
extern int measure_delta
;
* The main function of `master' is to periodically compute the differences
* (deltas) between its clock and the clocks of the slaves, to compute the
* network average delta, and to send to the slaves the differences between
* their individual deltas and the network delta.
* While waiting, it receives messages from the slaves (i.e. requests for
* master's name, remote requests to set the network time, ...), and
* takes the appropriate action.
struct sockaddr_in saveaddr
;
struct tsp
*answer
, *acksend();
struct sockaddr_in server
;
register struct netinfo
*ntp
;
fi
= "/usr/adm/timed.masterlog";
syslog(LOG_INFO
, "THIS MACHINE IS MASTER");
fprintf(fd
, "THIS MACHINE IS MASTER\n");
for (ntp
= nettab
; ntp
!= NULL
; ntp
= ntp
->next
)
if (ntp
->status
== MASTER
)
(void)gettimeofday(&time
, (struct timezone
*)0);
if (time
.tv_sec
>= pollingtime
) {
pollingtime
= time
.tv_sec
+ SAMPLEINTVL
;
wait
.tv_sec
= pollingtime
- time
.tv_sec
;
msg
= readmsg(TSP_ANY
, (char *)ANYADDR
, &wait
, (struct netinfo
*)NULL
);
ind
= addmach(msg
->tsp_name
, &from
);
if (hp
[ind
].seq
!= msg
->tsp_seq
) {
hp
[ind
].seq
= msg
->tsp_seq
;
to
.tsp_type
= TSP_SETTIME
;
(void)strcpy(to
.tsp_name
, hostname
);
* give the upcoming slave the time
* to check its input queue before
(void) gettimeofday(&mytime
,
to
.tsp_time
.tv_sec
= mytime
.tv_sec
;
answer
= acksend(&to
, &hp
[ind
].addr
,
"ERROR ON SETTIME machine: %s",
(void) addmach(msg
->tsp_name
, &from
);
msg
->tsp_time
.tv_usec
= 0;
* the following line is necessary due to syslog
* calling ctime() which clobbers the static buffer
(void)strcpy(olddate
, date());
(void)gettimeofday(&time
, &tzone
);
time
.tv_sec
+= msg
->tsp_time
.tv_sec
;
(void)settimeofday(&time
, &tzone
);
syslog(LOG_NOTICE
, "date changed from: %s", olddate
);
msg
->tsp_type
= TSP_DATEACK
;
msg
->tsp_vers
= TSPVERSION
;
(void)strcpy(msg
->tsp_name
, hostname
);
if (sendto(sock
, (char *)msg
, sizeof(struct tsp
), 0,
&saveaddr
, sizeof(struct sockaddr_in
)) < 0) {
syslog(LOG_ERR
, "sendto: %m");
ind
= findhost(msg
->tsp_name
);
"DATEREQ from uncontrolled machine");
if (hp
[ind
].seq
!= msg
->tsp_seq
) {
hp
[ind
].seq
= msg
->tsp_seq
;
msg
->tsp_time
.tv_usec
= 0;
* the following line is necessary due to syslog
* calling ctime() which clobbers the static buffer
(void)strcpy(olddate
, date());
(void)gettimeofday(&time
, &tzone
);
time
.tv_sec
+= msg
->tsp_time
.tv_sec
;
(void)settimeofday(&time
, &tzone
);
"date changed by %s from: %s",
fd
= fopen(tracefile
, "w");
fprintf(fd
, "Tracing started on: %s\n\n",
fprintf(fd
, "Tracing ended on: %s\n", date());
(void)strcpy(to
.tsp_name
, hostname
);
answer
= acksend(&to
, &server
, msg
->tsp_name
, TSP_ACK
,
syslog(LOG_ERR
, "election error");
(void) addmach(msg
->tsp_name
, &from
);
* After a network partition, there can be
* more than one master: the first slave to
* come up will notify here the situation.
(void)strcpy(to
.tsp_name
, hostname
);
for (ntp
= nettab
; ntp
!= NULL
; ntp
= ntp
->next
) {
if ((ntp
->mask
& from
.sin_addr
.s_addr
) ==
to
.tsp_type
= TSP_RESOLVE
;
answer
= acksend(&to
, &ntp
->dest_addr
,
(char *)ANYADDR
, TSP_MASTERACK
, ntp
);
msg
= acksend(&to
, &server
, answer
->tsp_name
,
TSP_MASTERACK
, (struct netinfo
*)NULL
);
syslog(LOG_ERR
, "error on sending QUIT");
(void) addmach(answer
->tsp_name
, &from
);
* do not want to call synch() while waiting
(void)gettimeofday(&time
, (struct timezone
*)0);
pollingtime
= time
.tv_sec
+ SAMPLEINTVL
;
fprintf(fd
, "garbage: ");
* `synch' synchronizes all the slaves by calling measure,
* networkdelta and correct
struct timeval start
, end
;
(void)gettimeofday(&start
, (struct timezone
*)0);
if (header
== ON
|| --lines
== 0) {
fprintf(fp
, "%s\n", date());
for (i
=0; i
<slvcount
; i
++)
fprintf(fp
, "%.7s\t", hp
[i
].name
);
for(i
=1; i
<slvcount
; i
++) {
if ((measure_status
= measure(&tack
, &hp
[i
].addr
,
syslog(LOG_ERR
, "measure: %m");
hp
[i
].delta
= measure_delta
;
if (measure_status
== GOOD
)
/* called by a submaster */
fprintf(fd
, "submaster correct: %d ms.\n",
netdelta
= networkdelta();
"master correct: %d ms.\n",
end
.tv_sec
-= start
.tv_sec
;
end
.tv_usec
-= start
.tv_usec
;
fprintf(fp
, "%d ms.\n", (end
.tv_sec
*1000+end
.tv_usec
/1000));
for(i
=1; i
<slvcount
; i
++) {
if (hp
[i
].delta
== HOSTDOWN
) {
free((char *)hp
[i
].name
);
* 'spreadtime' sends the time to each slave after the master
* has received the command to set the network time
struct tsp
*answer
, *acksend();
for(i
=1; i
<slvcount
; i
++) {
to
.tsp_type
= TSP_SETTIME
;
(void)strcpy(to
.tsp_name
, hostname
);
(void)gettimeofday(&to
.tsp_time
, (struct timezone
*)0);
answer
= acksend(&to
, &hp
[i
].addr
, hp
[i
].name
, TSP_ACK
,
syslog(LOG_ERR
, "ERROR ON SETTIME machine: %s", hp
[i
].name
);
for (i
=1; i
<slvcount
; i
++) {
if (strcmp(name
, hp
[i
].name
) == 0) {
* 'addmach' adds a host to the list of controlled machines
struct sockaddr_in
*addr
;
hp
[slvcount
].addr
= *addr
;
hp
[slvcount
].name
= (char *)malloc(MAXHOSTNAMELEN
);
(void)strcpy(hp
[slvcount
].name
, name
);
syslog(LOG_ALERT
, "no more slots in host table");
/* need to clear sequence number anyhow */
* Remove all the machines from the host table that exist on the given
* network. This is called when a master transitions to a slave on a
register struct netinfo
*ntp
;
for (i
= 1; i
< slvcount
; i
++)
if ((hp
[i
].addr
.sin_addr
.s_addr
& ntp
->mask
) == ntp
->net
)
* remove the machine with the given index in the host table.
fprintf(fd
, "rmmach: %s\n", hp
[ind
].name
);
bcopy(&hp
[ind
+1], &hp
[ind
], (slvcount
-ind
-1)*sizeof(hp
[ind
]));
fprintf(fd
, "host table:");
for (i
=1; i
<slvcount
; i
++)
fprintf(fd
, " %s", hp
[i
].name
);
struct tsp to
, *msg
, *readmsg();
to
.tsp_type
= TSP_MASTERUP
;
to
.tsp_vers
= TSPVERSION
;
(void)strcpy(to
.tsp_name
, hostname
);
if (sendto(sock
, (char *)&to
, sizeof(struct tsp
), 0, &net
->dest_addr
,
sizeof(struct sockaddr_in
)) < 0) {
syslog(LOG_ERR
, "sendto: %m");
msg
= readmsg(TSP_SLAVEUP
, (char *)ANYADDR
, &wait
, net
);
(void) addmach(msg
->tsp_name
, &from
);