summaryrefslogtreecommitdiff
path: root/usr.sbin/rdate
diff options
context:
space:
mode:
authorJakob Schlyter <jakob@cvs.openbsd.org>2002-09-08 12:33:43 +0000
committerJakob Schlyter <jakob@cvs.openbsd.org>2002-09-08 12:33:43 +0000
commitd6d7c19b6453cc5f0611a112e08627402a9b6aa5 (patch)
treed3b5942adc3fc1977dff9bbee5690fe879f0e8ab /usr.sbin/rdate
parent85e069ce9aafde0883c1f3bd8b7754d8e4c1cd2b (diff)
add leap second support for rfc868, from thorsten glaser
Diffstat (limited to 'usr.sbin/rdate')
-rw-r--r--usr.sbin/rdate/ntp.c10
-rw-r--r--usr.sbin/rdate/rdate.835
-rw-r--r--usr.sbin/rdate/rdate.c25
-rw-r--r--usr.sbin/rdate/rfc868time.c29
4 files changed, 68 insertions, 31 deletions
diff --git a/usr.sbin/rdate/ntp.c b/usr.sbin/rdate/ntp.c
index 4d23523d62a..65a9847e250 100644
--- a/usr.sbin/rdate/ntp.c
+++ b/usr.sbin/rdate/ntp.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ntp.c,v 1.12 2002/08/10 21:37:28 jakob Exp $ */
+/* $OpenBSD: ntp.c,v 1.13 2002/09/08 12:33:42 jakob Exp $ */
/*
* Copyright (c) 1996, 1997 by N.M. Maclaren. All rights reserved.
@@ -101,7 +101,7 @@ struct ntp_data {
double current;
};
-void ntp_client(const char *, struct timeval *, struct timeval *);
+void ntp_client(const char *, struct timeval *, struct timeval *, int);
int sync_ntp(int, const struct sockaddr *, double *, double *);
void make_packet(struct ntp_data *);
int write_packet(int, const struct sockaddr *, struct ntp_data *);
@@ -111,10 +111,11 @@ void unpack_ntp(struct ntp_data *, u_char *, int);
double current_time(double);
void create_timeval(double, struct timeval *, struct timeval *);
-int corrleaps = 0;
+int corrleaps;
void
-ntp_client(const char *hostname, struct timeval *new, struct timeval *adjust)
+ntp_client(const char *hostname, struct timeval *new,
+ struct timeval *adjust, int leapflag)
{
struct addrinfo hints, *res0, *res;
double offset, error;
@@ -129,6 +130,7 @@ ntp_client(const char *hostname, struct timeval *new, struct timeval *adjust)
/*NOTREACHED*/
}
+ corrleaps = leapflag;
if (corrleaps)
ntpleaps_init();
diff --git a/usr.sbin/rdate/rdate.8 b/usr.sbin/rdate/rdate.8
index f03a8851362..eeb5cd64354 100644
--- a/usr.sbin/rdate/rdate.8
+++ b/usr.sbin/rdate/rdate.8
@@ -1,4 +1,4 @@
-.\" $OpenBSD: rdate.8,v 1.18 2002/07/27 20:11:34 jakob Exp $
+.\" $OpenBSD: rdate.8,v 1.19 2002/09/08 12:33:42 jakob Exp $
.\" $NetBSD: rdate.8,v 1.4 1996/04/08 20:55:17 jtc Exp $
.\"
.\" Copyright (c) 1994 Christos Zoulas
@@ -37,7 +37,7 @@
.Nd set the system's date from a remote host
.Sh SYNOPSIS
.Nm rdate
-.Op Fl nNpsav
+.Op Fl ncpsav
.Ar host
.Sh DESCRIPTION
.Nm
@@ -55,8 +55,14 @@ The options are as follows:
.Bl -tag -width Ds
.It Fl n
Use SNTP (RFC1361) instead of the RFC868 time protocol.
-.It Fl N
-Use SNTP (RFC1361) instead of the RFC868 time protocol and correct leap seconds.
+.It Fl c
+Correct leap seconds.
+Sometimes required when synchronizing to an NTP server.
+If using the RFC868 protocol, use only if the server does not correctly
+account for leap seconds.
+You can determine if you need this parameter if you sync against an NTP
+server (with this parameter) or (recommended) check with a local radio
+controlled watch or phone service.
.It Fl p
Do not set, just print the remote time.
.It Fl s
@@ -68,11 +74,32 @@ call to gradually skew the local time to the
remote time rather than just hopping.
.It Fl v
Verbose output.
+Always show the adjustment.
+.El
.Sh FILES
.Bl -tag -width /var/log/wtmp -compact
.It Pa /var/log/wtmp
record of date resets and time changes
.El
+.Sh EXAMPLES
+To get the legal time in Germany, set the
+.Pa /etc/localtime
+symlink to
+.Pa /usr/share/zoneinfo/right/Europe/Berlin
+and issue the following command:
+.Pp
+.D1 Li "rdate -ncv ptbtime1.ptb.de"
+.Pp
+The command of course assumes you have a working internet connection
+and DNS set up to connect to the server at
+.Sy Physikalisch-Technische Bundesanstalt
+in Braunschweig, Germany.
+.Pp
+To gradually adjust time once a hour after the first
+.Qq step
+adjustment, put the following line into root's crontab:
+.Pp
+.D1 Li "58 * * * * rdate -ncav ptbtime1.ptb.de | logger -t NTP"
.Sh SEE ALSO
.Xr adjtime 2 ,
.Xr gettimeofday 2 ,
diff --git a/usr.sbin/rdate/rdate.c b/usr.sbin/rdate/rdate.c
index ce470ba835b..7add012eab4 100644
--- a/usr.sbin/rdate/rdate.c
+++ b/usr.sbin/rdate/rdate.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: rdate.c,v 1.18 2002/08/10 21:37:28 jakob Exp $ */
+/* $OpenBSD: rdate.c,v 1.19 2002/09/08 12:33:42 jakob Exp $ */
/* $NetBSD: rdate.c,v 1.4 1996/03/16 12:37:45 pk Exp $ */
/*
@@ -42,7 +42,7 @@
#if 0
from: static char rcsid[] = "$NetBSD: rdate.c,v 1.3 1996/02/22 06:59:18 thorpej Exp $";
#else
-static const char rcsid[] = "$OpenBSD: rdate.c,v 1.18 2002/08/10 21:37:28 jakob Exp $";
+static const char rcsid[] = "$OpenBSD: rdate.c,v 1.19 2002/09/08 12:33:42 jakob Exp $";
#endif
#endif /* lint */
@@ -63,18 +63,17 @@ static const char rcsid[] = "$OpenBSD: rdate.c,v 1.18 2002/08/10 21:37:28 jakob
#define logwtmp(a,b,c)
#endif
-void rfc868time_client (const char *, struct timeval *, struct timeval *);
-void ntp_client (const char *, struct timeval *, struct timeval *);
+void rfc868time_client (const char *, struct timeval *, struct timeval *, int);
+void ntp_client (const char *, struct timeval *, struct timeval *, int);
extern char *__progname;
-extern int corrleaps;
void
usage()
{
- (void) fprintf(stderr, "Usage: %s [-npsa] host\n", __progname);
+ (void) fprintf(stderr, "Usage: %s [-ncpsa] host\n", __progname);
(void) fprintf(stderr, " -n: use SNTP instead of RFC868 time protocol\n");
- (void) fprintf(stderr, " -N: use SNTP and correct leap seconds\n");
+ (void) fprintf(stderr, " -c: correct leap second count\n");
(void) fprintf(stderr, " -p: just print, don't set\n");
(void) fprintf(stderr, " -s: just set, don't print\n");
(void) fprintf(stderr, " -a: use adjtime instead of instant change\n");
@@ -85,14 +84,14 @@ int
main(int argc, char **argv)
{
int pr = 0, silent = 0, ntp = 0, verbose = 0;
- int slidetime = 0;
+ int slidetime = 0, corrleaps = 0;
char *hname;
extern int optind;
int c;
struct timeval new, adjust;
- while ((c = getopt(argc, argv, "psanNv")) != -1)
+ while ((c = getopt(argc, argv, "psancv")) != -1)
switch (c) {
case 'p':
pr++;
@@ -108,11 +107,9 @@ main(int argc, char **argv)
case 'n':
ntp++;
- corrleaps = 0;
break;
- case 'N':
- ntp++;
+ case 'c':
corrleaps = 1;
break;
@@ -132,9 +129,9 @@ main(int argc, char **argv)
hname = argv[optind];
if (ntp)
- ntp_client(hname, &new, &adjust);
+ ntp_client(hname, &new, &adjust, corrleaps);
else
- rfc868time_client(hname, &new, &adjust);
+ rfc868time_client(hname, &new, &adjust, corrleaps);
if (!pr) {
if (!slidetime) {
diff --git a/usr.sbin/rdate/rfc868time.c b/usr.sbin/rdate/rfc868time.c
index deb472a67f5..9edd1b364aa 100644
--- a/usr.sbin/rdate/rfc868time.c
+++ b/usr.sbin/rdate/rfc868time.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: rfc868time.c,v 1.4 2002/08/10 21:37:28 jakob Exp $ */
+/* $OpenBSD: rfc868time.c,v 1.5 2002/09/08 12:33:42 jakob Exp $ */
/* $NetBSD: rdate.c,v 1.4 1996/03/16 12:37:45 pk Exp $ */
/*
@@ -34,15 +34,16 @@
/*
* rdate.c: Set the date from the specified host
*
- * Uses the rfc868 time protocol at socket 37.
+ * Uses the rfc868 time protocol at socket 37 (tcp).
* Time is returned as the number of seconds since
* midnight January 1st 1900.
*/
+
#ifndef lint
#if 0
from: static char rcsid[] = "$NetBSD: rdate.c,v 1.3 1996/02/22 06:59:18 thorpej Exp $";
#else
-static const char rcsid[] = "$OpenBSD: rfc868time.c,v 1.4 2002/08/10 21:37:28 jakob Exp $";
+static const char rcsid[] = "$OpenBSD: rfc868time.c,v 1.5 2002/09/08 12:33:42 jakob Exp $";
#endif
#endif /* lint */
@@ -59,23 +60,29 @@ static const char rcsid[] = "$OpenBSD: rfc868time.c,v 1.4 2002/08/10 21:37:28 ja
#include <unistd.h>
#include <time.h>
+/* Obviously it is not just for SNTP clients... */
+#include "ntpleaps.h"
+
/* seconds from midnight Jan 1900 - 1970 */
#define DIFFERENCE 2208988800UL
void
-rfc868time_client (const char *hostname,
- struct timeval *new, struct timeval *adjust)
+rfc868time_client (const char *hostname, struct timeval *new,
+ struct timeval *adjust, int leapflag)
{
struct addrinfo hints, *res0, *res;
struct timeval old;
- time_t tim;
+ u_int32_t tim; /* RFC 868 states clearly this is an uint32 */
int s;
int error;
+ u_int64_t td;
memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
+ /* XXX what about rfc868 UDP
+ * probably not due to the Y2038 issue -mirabile */
error = getaddrinfo(hostname, "time", &hints, &res0);
if (error) {
errx(1, "%s: %s", hostname, gai_strerror(error));
@@ -100,7 +107,7 @@ rfc868time_client (const char *hostname,
err(1, "Could not connect socket");
freeaddrinfo(res0);
- if (read(s, &tim, sizeof(time_t)) != sizeof(time_t))
+ if (read(s, &tim, sizeof(tim)) != sizeof(tim))
err(1, "Could not read data");
(void) close(s);
@@ -109,9 +116,13 @@ rfc868time_client (const char *hostname,
if (gettimeofday(&old, NULL) == -1)
err(1, "Could not get local time of day");
- adjust->tv_sec = tim - old.tv_sec;
+ td = SEC_TO_TAI64(old.tv_sec);
+ if (leapflag)
+ ntpleaps_sub(&td);
+
+ adjust->tv_sec = tim - TAI64_TO_SEC(td);
adjust->tv_usec = 0;
- new->tv_sec = tim;
+ new->tv_sec = old.tv_sec + adjust->tv_sec;
new->tv_usec = 0;
}