From: Thomas Cort Date: Sat, 6 Apr 2013 12:47:10 +0000 (+0000) Subject: Importing usr.sbin/rdate X-Git-Tag: v3.3.0~1049 X-Git-Url: http://zhaoyanbai.com/repos/%22http:/www.isc.org/icons/zlib_tech.html?a=commitdiff_plain;h=refs%2Fchanges%2F76%2F476%2F2;p=minix.git 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 --- diff --git a/commands/Makefile b/commands/Makefile index 3cd141501..ae8b4bcca 100644 --- a/commands/Makefile +++ b/commands/Makefile @@ -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 \ diff --git a/commands/rdate/Makefile b/commands/rdate/Makefile deleted file mode 100644 index 4483ce7c1..000000000 --- a/commands/rdate/Makefile +++ /dev/null @@ -1,4 +0,0 @@ -PROG= rdate -MAN= - -.include diff --git a/commands/rdate/rdate.c b/commands/rdate/rdate.c deleted file mode 100644 index edb4b39f0..000000000 --- a/commands/rdate/rdate.c +++ /dev/null @@ -1,109 +0,0 @@ -/* rdate 1.0 - Set time&date from remote host Author: Kees J. Bot - * 12 Oct 1995 - */ -#define nil 0 -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -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); -} diff --git a/distrib/sets/lists/minix/mi b/distrib/sets/lists/minix/mi index 6414f674c..3930c1945 100644 --- a/distrib/sets/lists/minix/mi +++ b/distrib/sets/lists/minix/mi @@ -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 diff --git a/man/man8/Makefile b/man/man8/Makefile index 170bfe0ca..440c44076 100644 --- a/man/man8/Makefile +++ b/man/man8/Makefile @@ -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 diff --git a/man/man8/rdate.8 b/man/man8/rdate.8 deleted file mode 100644 index c81752631..000000000 --- a/man/man8/rdate.8 +++ /dev/null @@ -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) diff --git a/releasetools/nbsd_ports b/releasetools/nbsd_ports index 4afa747e0..ee27e29e4 100644 --- a/releasetools/nbsd_ports +++ b/releasetools/nbsd_ports @@ -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 diff --git a/usr.sbin/Makefile b/usr.sbin/Makefile index ac09d0027..ff33db3df 100644 --- a/usr.sbin/Makefile +++ b/usr.sbin/Makefile @@ -8,7 +8,7 @@ SUBDIR= \ installboot \ mtree \ pwd_mkdb postinstall \ - user \ + rdate user \ vipw \ zic diff --git a/usr.sbin/rdate/Makefile b/usr.sbin/rdate/Makefile new file mode 100644 index 000000000..34206eef8 --- /dev/null +++ b/usr.sbin/rdate/Makefile @@ -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 diff --git a/usr.sbin/rdate/rdate.8 b/usr.sbin/rdate/rdate.8 new file mode 100644 index 000000000..1f93fdbf9 --- /dev/null +++ b/usr.sbin/rdate/rdate.8 @@ -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 diff --git a/usr.sbin/rdate/rdate.c b/usr.sbin/rdate/rdate.c new file mode 100644 index 000000000..62e3734d1 --- /dev/null +++ b/usr.sbin/rdate/rdate.c @@ -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 +#ifndef lint +__RCSID("$NetBSD: rdate.c,v 1.19 2009/10/21 01:07:47 snj Exp $"); +#endif /* lint */ + +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include + +/* 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; +}