roll back to 20101210 -- the current version just hung if one tried

to set the date
This commit is contained in:
drochner 2011-01-28 20:23:38 +00:00
parent dac4423feb
commit da4141bdd5
2 changed files with 34 additions and 48 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: date.c,v 1.57 2010/12/12 17:30:23 christos Exp $ */ /* $NetBSD: date.c,v 1.58 2011/01/28 20:23:38 drochner Exp $ */
/* /*
* Copyright (c) 1985, 1987, 1988, 1993 * Copyright (c) 1985, 1987, 1988, 1993
@ -40,7 +40,7 @@ __COPYRIGHT(
#if 0 #if 0
static char sccsid[] = "@(#)date.c 8.2 (Berkeley) 4/28/95"; static char sccsid[] = "@(#)date.c 8.2 (Berkeley) 4/28/95";
#else #else
__RCSID("$NetBSD: date.c,v 1.57 2010/12/12 17:30:23 christos Exp $"); __RCSID("$NetBSD: date.c,v 1.58 2011/01/28 20:23:38 drochner Exp $");
#endif #endif
#endif /* not lint */ #endif /* not lint */
@ -50,7 +50,6 @@ __RCSID("$NetBSD: date.c,v 1.57 2010/12/12 17:30:23 christos Exp $");
#include <ctype.h> #include <ctype.h>
#include <err.h> #include <err.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h>
#include <locale.h> #include <locale.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -65,12 +64,13 @@ __RCSID("$NetBSD: date.c,v 1.57 2010/12/12 17:30:23 christos Exp $");
static time_t tval; static time_t tval;
static int aflag, jflag, rflag, nflag; static int aflag, jflag, rflag, nflag;
int retval;
static void badformat(void); static void badformat(void);
static void badtime(void); static void badtime(void);
static void badvalue(const char *); static void badvalue(const char *);
static void setthetime(const char *); static void setthetime(const char *);
static void usage(void) __attribute__((__noreturn__)); static void usage(void);
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
@ -79,8 +79,6 @@ main(int argc, char *argv[])
size_t bufsiz; size_t bufsiz;
const char *format; const char *format;
int ch; int ch;
long long val;
struct tm *tm;
setprogname(argv[0]); setprogname(argv[0]);
(void)setlocale(LC_ALL, ""); (void)setlocale(LC_ALL, "");
@ -94,9 +92,8 @@ main(int argc, char *argv[])
case 'd': case 'd':
rflag = 1; rflag = 1;
tval = parsedate(optarg, NULL, NULL); tval = parsedate(optarg, NULL, NULL);
if (tval == -1) if (tval == -1)
badarg: errx(EXIT_FAILURE, errx(1, "Cannot parse `%s'", optarg);
"Cannot parse `%s'", optarg);
break; break;
case 'j': /* don't set time */ case 'j': /* don't set time */
jflag = 1; jflag = 1;
@ -105,15 +102,8 @@ badarg: errx(EXIT_FAILURE,
nflag = 1; nflag = 1;
break; break;
case 'r': /* user specified seconds */ case 'r': /* user specified seconds */
errno = 0;
val = strtoll(optarg, &buf, 0);
if (optarg[0] == '\0' || *buf != '\0')
goto badarg;
if (errno == ERANGE && (val == LLONG_MAX ||
val == LLONG_MIN))
err(EXIT_FAILURE, "Bad number `%s'", optarg);
rflag = 1; rflag = 1;
tval = (time_t)val; tval = strtoll(optarg, NULL, 0);
break; break;
case 'u': /* do everything in UTC */ case 'u': /* do everything in UTC */
(void)setenv("TZ", "UTC0", 1); (void)setenv("TZ", "UTC0", 1);
@ -128,13 +118,13 @@ badarg: errx(EXIT_FAILURE,
if (!rflag && time(&tval) == -1) if (!rflag && time(&tval) == -1)
err(EXIT_FAILURE, "time"); err(EXIT_FAILURE, "time");
format = "+%a %b %e %H:%M:%S %Z %Y";
/* allow the operands in any order */ /* allow the operands in any order */
if (*argv && **argv == '+') { if (*argv && **argv == '+') {
format = *argv; format = *argv;
++argv; ++argv;
} else }
format = "+%a %b %e %H:%M:%S %Z %Y";
if (*argv) { if (*argv) {
setthetime(*argv); setthetime(*argv);
@ -146,19 +136,14 @@ badarg: errx(EXIT_FAILURE,
if ((buf = malloc(bufsiz = 1024)) == NULL) if ((buf = malloc(bufsiz = 1024)) == NULL)
goto bad; goto bad;
while (strftime(buf, bufsiz, format, localtime(&tval)) == 0)
if ((tm = localtime(&tval)) == NULL)
err(EXIT_FAILURE, "localtime %lld failed", (long long)tval);
while (strftime(buf, bufsiz, format, tm) == 0)
if ((buf = realloc(buf, bufsiz <<= 1)) == NULL) if ((buf = realloc(buf, bufsiz <<= 1)) == NULL)
goto bad; goto bad;
(void)printf("%s\n", buf+1);
(void)printf("%s\n", buf + 1);
free(buf); free(buf);
return 0; return 0;
bad: bad:
err(EXIT_FAILURE, "Cannot allocate format buffer"); err(1, "Cannot allocate format buffer");
} }
static void static void
@ -204,8 +189,7 @@ setthetime(const char *p)
badformat(); badformat();
} }
if ((lt = localtime(&tval)) == NULL) lt = localtime(&tval);
err(EXIT_FAILURE, "localtime %lld failed", (long long)tval);
lt->tm_isdst = -1; /* Divine correct DST */ lt->tm_isdst = -1; /* Divine correct DST */
@ -333,8 +317,7 @@ static void
usage(void) usage(void)
{ {
(void)fprintf(stderr, (void)fprintf(stderr,
"Usage: %s [-ajnu] [-d date] [-r seconds] [+format]", "usage: %s [-ajnu] [-d date] [-r seconds] [+format]", getprogname());
getprogname());
(void)fprintf(stderr, " [[[[[[CC]yy]mm]dd]HH]MM[.SS]]\n"); (void)fprintf(stderr, " [[[[[[CC]yy]mm]dd]HH]MM[.SS]]\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
/* NOTREACHED */ /* NOTREACHED */

View File

@ -1,4 +1,4 @@
/* $NetBSD: netdate.c,v 1.28 2010/12/11 16:57:51 christos Exp $ */ /* $NetBSD: netdate.c,v 1.29 2011/01/28 20:23:38 drochner Exp $ */
/*- /*-
* Copyright (c) 1990, 1993 * Copyright (c) 1990, 1993
@ -34,7 +34,7 @@
#if 0 #if 0
static char sccsid[] = "@(#)netdate.c 8.2 (Berkeley) 4/28/95"; static char sccsid[] = "@(#)netdate.c 8.2 (Berkeley) 4/28/95";
#else #else
__RCSID("$NetBSD: netdate.c,v 1.28 2010/12/11 16:57:51 christos Exp $"); __RCSID("$NetBSD: netdate.c,v 1.29 2011/01/28 20:23:38 drochner Exp $");
#endif #endif
#endif /* not lint */ #endif /* not lint */
@ -59,6 +59,8 @@ __RCSID("$NetBSD: netdate.c,v 1.28 2010/12/11 16:57:51 christos Exp $");
#define WAITACK 2000 /* milliseconds */ #define WAITACK 2000 /* milliseconds */
#define WAITDATEACK 5000 /* milliseconds */ #define WAITDATEACK 5000 /* milliseconds */
extern int retval;
static const char * static const char *
tsp_type_to_string(const struct tsp *msg) tsp_type_to_string(const struct tsp *msg)
{ {
@ -73,7 +75,7 @@ tsp_type_to_string(const struct tsp *msg)
* new date to the local timedaemon. If the timedaemon is in the master state, * new date to the local timedaemon. If the timedaemon is in the master state,
* it performs the correction on all slaves. If it is in the slave state, it * it performs the correction on all slaves. If it is in the slave state, it
* notifies the master that a correction is needed. * notifies the master that a correction is needed.
* Returns 0 on success. Returns > 0 on failure. * Returns 0 on success. Returns > 0 on failure, setting retval to 2;
*/ */
int int
netsettime(time_t tval) netsettime(time_t tval)
@ -87,7 +89,7 @@ netsettime(time_t tval)
if ((sp = getservbyname("timed", "udp")) == NULL) { if ((sp = getservbyname("timed", "udp")) == NULL) {
warnx("udp/timed: unknown service"); warnx("udp/timed: unknown service");
return 2; return (retval = 2);
} }
(void)memset(&dest, 0, sizeof(dest)); (void)memset(&dest, 0, sizeof(dest));
@ -98,18 +100,18 @@ netsettime(time_t tval)
dest.sin_port = sp->s_port; dest.sin_port = sp->s_port;
dest.sin_addr.s_addr = htonl(INADDR_ANY); dest.sin_addr.s_addr = htonl(INADDR_ANY);
s = socket(AF_INET, SOCK_DGRAM, 0); s = socket(AF_INET, SOCK_DGRAM, 0);
if (s == -1) { if (s < 0) {
if (errno != EAFNOSUPPORT) if (errno != EAFNOSUPPORT)
warn("timed"); warn("timed");
return 2; return (retval = 2);
} }
#ifdef IP_PORTRANGE #ifdef IP_PORTRANGE
{ {
static const int on = IP_PORTRANGE_LOW; static const int on = IP_PORTRANGE_LOW;
if (setsockopt(s, IPPROTO_IP, IP_PORTRANGE, &on, if (setsockopt(s, IPPROTO_IP, IP_PORTRANGE,
sizeof(on)) == -1) { &on, sizeof(on)) < 0) {
warn("setsockopt"); warn("setsockopt");
goto bad; goto bad;
} }
@ -118,19 +120,20 @@ netsettime(time_t tval)
msg.tsp_type = TSP_SETDATE; msg.tsp_type = TSP_SETDATE;
msg.tsp_vers = TSPVERSION; msg.tsp_vers = TSPVERSION;
if (gethostname(hostname, sizeof(hostname)) == -1) { if (gethostname(hostname, sizeof(hostname))) {
warn("gethostname"); warn("gethostname");
goto bad; goto bad;
} }
(void)strlcpy(msg.tsp_name, hostname, sizeof(msg.tsp_name)); strncpy(msg.tsp_name, hostname, sizeof(msg.tsp_name));
msg.tsp_name[sizeof(msg.tsp_name) - 1] = '\0';
msg.tsp_seq = htons((uint16_t)0); msg.tsp_seq = htons((uint16_t)0);
msg.tsp_time.tv_sec = htonl((uint32_t)tval); /* XXX: y2038 */ msg.tsp_time.tv_sec = htonl((uint32_t)tval); /* XXX: y2038 */
msg.tsp_time.tv_usec = htonl((uint32_t)0); msg.tsp_time.tv_usec = htonl((uint32_t)0);
if (connect(s, (const void *)&dest, sizeof(dest)) == -1) { if (connect(s, (const struct sockaddr *)&dest, sizeof(dest)) < 0) {
warn("connect"); warn("connect");
goto bad; goto bad;
} }
if (send(s, &msg, sizeof(msg), 0) == -1) { if (send(s, &msg, sizeof(msg), 0) < 0) {
if (errno != ECONNREFUSED) if (errno != ECONNREFUSED)
warn("send"); warn("send");
goto bad; goto bad;
@ -148,7 +151,7 @@ loop:
int error; int error;
length = sizeof(error); length = sizeof(error);
if (getsockopt(s, SOL_SOCKET, SO_ERROR, &error, &length) == -1 if (!getsockopt(s, SOL_SOCKET, SO_ERROR, &error, &length)
&& error) { && error) {
if (error != ECONNREFUSED) if (error != ECONNREFUSED)
warn("send (delayed error)"); warn("send (delayed error)");
@ -159,8 +162,8 @@ loop:
if (found > 0 && ready.revents & POLLIN) { if (found > 0 && ready.revents & POLLIN) {
ssize_t ret; ssize_t ret;
if ((ret = recv(s, &msg, sizeof(msg), 0)) == -1) { ret = recv(s, &msg, sizeof(msg), 0);
if (ret < 0) if (ret < 0) {
if (errno != ECONNREFUSED) if (errno != ECONNREFUSED)
warn("recv"); warn("recv");
goto bad; goto bad;
@ -179,7 +182,7 @@ loop:
goto loop; goto loop;
case TSP_DATEACK: case TSP_DATEACK:
(void)close(s); (void)close(s);
return 0; return (0);
default: default:
warnx("wrong ack received from timed: %s", warnx("wrong ack received from timed: %s",
tsp_type_to_string(&msg)); tsp_type_to_string(&msg));
@ -192,5 +195,5 @@ loop:
bad: bad:
(void)close(s); (void)close(s);
return 2; return (retval = 2);
} }