Importing usr.sbin/rdate

Import the NetBSD rdate command and remove the Minix rdate command.
The default behaviour for both is the same. The NetBSD version adds
options to just display the time, adjust the time using adjtime(),
and set the time without printing the time.

Porting Notes:
 - Compiles cleanly out of the box without any warnings
 - Path changes from /usr/bin/rdate to /usr/sbin/rdate
 - checked pkgsrc for any usages of rdate (none found)
 - checked src for any usages of rdate (none found)

Testing:
 - all command line options work (tested with time.nist.gov server)
 - Native and cross build OK

Change-Id: I613449763891a896527f337999c006a970c3924c
This commit is contained in:
Thomas Cort 2013-04-06 12:47:10 +00:00 committed by Lionel Sambuc
parent 0f487a702b
commit 8357e3a442
11 changed files with 250 additions and 137 deletions

View file

@ -20,7 +20,7 @@ SUBDIR= add_route arp ash at backup basename btrace \
nonamed od paste patch \
ping postinstall poweroff pr prep printroot \
profile progressbar pr_routes ps pwdauth \
ramdisk rarpd rawspeed rcp rdate readclock \
ramdisk rarpd rawspeed rcp readclock \
reboot remsync rev rget rlogin \
rotate rsh rshd service setup shar acksize \
sleep slip spell split sprofalyze sprofdiff srccrc \

View file

@ -1,4 +0,0 @@
PROG= rdate
MAN=
.include <bsd.prog.mk>

View file

@ -1,109 +0,0 @@
/* rdate 1.0 - Set time&date from remote host Author: Kees J. Bot
* 12 Oct 1995
*/
#define nil 0
#include <sys/types.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <net/hton.h>
#include <net/netlib.h>
#include <net/gen/in.h>
#include <net/gen/netdb.h>
#include <net/gen/tcp.h>
#include <net/gen/tcp_io.h>
void report(const char *label)
{
fprintf(stderr, "rdate: %s: %s\n", label, strerror(errno));
}
void fatal(const char *label)
{
report(label);
exit(1);
}
int main(int argc, char **argv)
{
char *tcp_device;
int fd;
int i;
struct servent *servent;
struct hostent *hostent;
u16_t time_port;
nwio_tcpconf_t tcpconf;
nwio_tcpcl_t tcpcl;
u32_t net_time;
time_t unix_time;
if (argc <= 1) {
fprintf(stderr, "Usage: rdate host ...\n");
exit(1);
}
/* Look up the port number of the TCP service "time". */
if ((servent= getservbyname("time", "tcp")) == nil) {
fprintf(stderr, "rdate: \"time\": unknown service\n");
exit(1);
}
time_port= servent->s_port;
if ((tcp_device= getenv("TCP_DEVICE")) == nil) tcp_device= TCP_DEVICE;
if ((fd= open(tcp_device, O_RDWR)) < 0) fatal(tcp_device);
/* Try each host on the command line. */
for (i= 1; i < argc; i++) {
if ((hostent= gethostbyname(argv[i])) == nil) {
fprintf(stderr, "rdate: %s: unknown host\n", argv[i]);
continue;
}
/* Configure a TCP channel and connect to the remote host. */
tcpconf.nwtc_flags= NWTC_LP_SEL | NWTC_SET_RA | NWTC_SET_RP;
memcpy(&tcpconf.nwtc_remaddr, hostent->h_addr, 4);
tcpconf.nwtc_remport= time_port;
if (ioctl(fd, NWIOSTCPCONF, &tcpconf) == -1) fatal(tcp_device);
tcpcl.nwtcl_flags= 0;
if (ioctl(fd, NWIOTCPCONN, &tcpcl) < 0) {
report(argv[i]);
continue;
}
/* Read four bytes to obtain the time. */
switch (read(fd, &net_time, sizeof(net_time))) {
case -1:
report(argv[i]);
continue;
default:
fprintf(stderr, "rdate: %s: short read\n", argv[i]);
continue;
case sizeof(net_time):
break;
}
break;
}
if (i == argc) exit(1);
/* Internet time is in seconds since 1900, UNIX time is in seconds
* since 1970.
*/
unix_time= ntohl(net_time) - 2208988800;
/* Try to set the time and tell us about it. */
if (stime(&unix_time) < 0) {
printf("time on ");
} else {
printf("time set to ");
}
printf("%s: %s", argv[i], ctime(&unix_time));
exit(0);
}

View file

@ -380,7 +380,7 @@
./usr/bin/rarpd minix-sys
./usr/bin/rawspeed minix-sys
./usr/bin/rcp minix-sys
./usr/bin/rdate minix-sys
./usr/bin/rdate minix-sys obsolete
./usr/bin/readall minix-sys obsolete
./usr/bin/readlink minix-sys
./usr/bin/reboot minix-sys
@ -3818,6 +3818,7 @@
./usr/sbin/pm minix-sys
./usr/sbin/postinstall minix-sys
./usr/sbin/pwd_mkdb minix-sys
./usr/sbin/rdate minix-sys
./usr/sbin/rs minix-sys
./usr/sbin/sched minix-sys
./usr/sbin/tty minix-sys

View file

@ -6,7 +6,7 @@ MAN= add_route.8 backup.8 boot.8 btrace.8 \
netconf.8 newroot.8 nonamed.8 \
ossdevlinks.8 part.8 partition.8 \
poweroff.8 printroot.8 pr_routes.8 pwdauth.8 rarpd.8 \
rdate.8 readclock.8 reboot.8 repartition.8 \
readclock.8 reboot.8 repartition.8 \
rshd.8 screendump.8 serial-ip.8 \
setup.8 shutdown.8 slip.8 srccrc.8 syslogd.8 tcpd.8 \
unix.8 update.8 usage.8 vbfs.8

View file

@ -1,20 +0,0 @@
.TH RDATE 8
.SH NAME
rdate \- set time and date from a remote host
.SH SYNOPSIS
.B rdate
.IR host " ..."
.SH DESCRIPTION
.B Rdate
obtains the current time from a remote time server. If run by the
super-user then local time is set to that time. The time of the remote host
is printed on standard output.
.PP
More than one host name may be specified. They are tried one by one until
one responds.
.SH "SEE ALSO"
.BR date (1),
.BR readclock (8),
.BR inet (8).
.SH AUTHOR
Kees J. Bot (kjb@cs.vu.nl)

View file

@ -174,6 +174,7 @@
2012/10/10 16:16:12,usr.sbin/mtree
2012/10/17 12:00:00,usr.sbin/postinstall
2011/01/04 10:01:51,usr.sbin/pwd_mkdb
2013/04/05 12:00:00,usr.sbin/rdate
2011/01/04 10:30:21,usr.sbin/user
2009/04/19 00:44:49,usr.sbin/vipw
2009/04/22 15:23:10,usr.sbin/zic

View file

@ -8,7 +8,7 @@ SUBDIR= \
installboot \
mtree \
pwd_mkdb postinstall \
user \
rdate user \
vipw \
zic

9
usr.sbin/rdate/Makefile Normal file
View file

@ -0,0 +1,9 @@
# $NetBSD: Makefile,v 1.7 2009/04/22 15:23:07 lukem Exp $
PROG= rdate
DPADD+= ${LIBUTIL}
LDADD+= -lutil
MAN= rdate.8
.include <bsd.prog.mk>

65
usr.sbin/rdate/rdate.8 Normal file
View file

@ -0,0 +1,65 @@
.\" $NetBSD: rdate.8,v 1.11 2009/10/21 01:07:47 snj Exp $
.\"
.\" Copyright (c) 1994 Christos Zoulas
.\" 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.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
.\"
.Dd April 30, 1994
.Dt RDATE 8
.Os
.Sh NAME
.Nm rdate
.Nd set the system's date from a remote host
.Sh SYNOPSIS
.Nm
.Op Fl psa
.Ar host
.Sh DESCRIPTION
.Nm
displays and sets the local date and time from the
host name or address given as the argument. It uses the RFC 868
protocol which is usually implemented as a built-in service of
.Xr inetd 8 .
.Pp
Available options:
.Pp
.Bl -tag -width indent
.It Fl p
Do not set, just print the remote time
.It Fl s
Do not print the time.
.It Fl a
Use the
.Xr adjtime 2
call to gradually skew the local time to the
remote time rather than just hopping.
.El
.Sh FILES
.Bl -tag -width /var/log/wtmp -compact
.It Pa /var/log/wtmp
A record of date resets and time changes.
.El
.Sh SEE ALSO
.Xr adjtime 2 ,
.Xr gettimeofday 2 ,
.Xr utmp 5 ,
.Xr inetd 8

170
usr.sbin/rdate/rdate.c Normal file
View file

@ -0,0 +1,170 @@
/* $NetBSD: rdate.c,v 1.19 2009/10/21 01:07:47 snj Exp $ */
/*
* Copyright (c) 1994 Christos Zoulas
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
*/
/*
* rdate.c: Set the date from the specified host
*
* Uses the rfc868 time protocol at socket 37.
* Time is returned as the number of seconds since
* midnight January 1st 1900.
*/
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: rdate.c,v 1.19 2009/10/21 01:07:47 snj Exp $");
#endif /* lint */
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <err.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <util.h>
/* seconds from midnight Jan 1900 - 1970 */
#define DIFFERENCE 2208988800ULL
int main(int, char **);
static void usage(void);
static void
usage(void)
{
(void) fprintf(stderr, "usage: %s [-psa] host\n", getprogname());
(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");
}
int
main(int argc, char *argv[])
{
int pr = 0, silent = 0, s;
int slidetime = 0;
int adjustment;
uint32_t data;
time_t tim;
char *hname;
const char *emsg = NULL;
struct addrinfo hints, *res, *res0;
int c;
int error;
adjustment = 0;
while ((c = getopt(argc, argv, "psa")) != -1)
switch (c) {
case 'p':
pr++;
break;
case 's':
silent++;
break;
case 'a':
slidetime++;
break;
default:
usage();
return 1;
}
if (argc - 1 != optind) {
usage();
return 1;
}
hname = argv[optind];
memset(&hints, 0, sizeof (hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_CANONNAME;
error = getaddrinfo(hname, "time", &hints, &res0);
if (error)
errx(1, "%s: %s", gai_strerror(error), hname);
for (res = res0, s = -1; res != NULL; res = res->ai_next) {
s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (s < 0) {
emsg = "socket";
continue;
}
if (connect(s, res->ai_addr, res->ai_addrlen)) {
close(s);
s = -1;
emsg = "connect";
continue;
}
break;
}
if (s < 0)
err(1, "%s", emsg);
if (read(s, &data, sizeof(uint32_t)) != sizeof(uint32_t))
err(1, "Could not read data");
(void) close(s);
tim = ntohl(data) - DIFFERENCE;
if (!pr) {
struct timeval tv;
if (!slidetime) {
logwtmp("|", "date", "");
tv.tv_sec = tim;
tv.tv_usec = 0;
if (settimeofday(&tv, NULL) == -1)
err(1, "Could not set time of day");
logwtmp("{", "date", "");
} else {
struct timeval tv_current;
if (gettimeofday(&tv_current, NULL) == -1)
err(1, "Could not get local time of day");
adjustment = tv.tv_sec = tim - tv_current.tv_sec;
tv.tv_usec = 0;
if (adjtime(&tv, NULL) == -1)
err(1, "Could not adjust time of day");
}
}
if (!silent) {
(void) fputs(ctime(&tim), stdout);
if (slidetime)
(void) fprintf(stdout,
"%s: adjust local clock by %d seconds\n",
getprogname(), adjustment);
}
return 0;
}