roll back to 20101210 -- the current version just hung if one tried
to set the date
This commit is contained in:
parent
dac4423feb
commit
da4141bdd5
|
@ -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
|
||||
|
@ -40,7 +40,7 @@ __COPYRIGHT(
|
|||
#if 0
|
||||
static char sccsid[] = "@(#)date.c 8.2 (Berkeley) 4/28/95";
|
||||
#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 /* 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 <err.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <locale.h>
|
||||
#include <stdio.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 int aflag, jflag, rflag, nflag;
|
||||
int retval;
|
||||
|
||||
static void badformat(void);
|
||||
static void badtime(void);
|
||||
static void badvalue(const char *);
|
||||
static void setthetime(const char *);
|
||||
static void usage(void) __attribute__((__noreturn__));
|
||||
static void usage(void);
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
|
@ -79,8 +79,6 @@ main(int argc, char *argv[])
|
|||
size_t bufsiz;
|
||||
const char *format;
|
||||
int ch;
|
||||
long long val;
|
||||
struct tm *tm;
|
||||
|
||||
setprogname(argv[0]);
|
||||
(void)setlocale(LC_ALL, "");
|
||||
|
@ -94,9 +92,8 @@ main(int argc, char *argv[])
|
|||
case 'd':
|
||||
rflag = 1;
|
||||
tval = parsedate(optarg, NULL, NULL);
|
||||
if (tval == -1)
|
||||
badarg: errx(EXIT_FAILURE,
|
||||
"Cannot parse `%s'", optarg);
|
||||
if (tval == -1)
|
||||
errx(1, "Cannot parse `%s'", optarg);
|
||||
break;
|
||||
case 'j': /* don't set time */
|
||||
jflag = 1;
|
||||
|
@ -105,15 +102,8 @@ badarg: errx(EXIT_FAILURE,
|
|||
nflag = 1;
|
||||
break;
|
||||
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;
|
||||
tval = (time_t)val;
|
||||
tval = strtoll(optarg, NULL, 0);
|
||||
break;
|
||||
case 'u': /* do everything in UTC */
|
||||
(void)setenv("TZ", "UTC0", 1);
|
||||
|
@ -128,13 +118,13 @@ badarg: errx(EXIT_FAILURE,
|
|||
if (!rflag && time(&tval) == -1)
|
||||
err(EXIT_FAILURE, "time");
|
||||
|
||||
format = "+%a %b %e %H:%M:%S %Z %Y";
|
||||
|
||||
/* allow the operands in any order */
|
||||
if (*argv && **argv == '+') {
|
||||
format = *argv;
|
||||
++argv;
|
||||
} else
|
||||
format = "+%a %b %e %H:%M:%S %Z %Y";
|
||||
}
|
||||
|
||||
if (*argv) {
|
||||
setthetime(*argv);
|
||||
|
@ -146,19 +136,14 @@ badarg: errx(EXIT_FAILURE,
|
|||
|
||||
if ((buf = malloc(bufsiz = 1024)) == NULL)
|
||||
goto bad;
|
||||
|
||||
if ((tm = localtime(&tval)) == NULL)
|
||||
err(EXIT_FAILURE, "localtime %lld failed", (long long)tval);
|
||||
|
||||
while (strftime(buf, bufsiz, format, tm) == 0)
|
||||
while (strftime(buf, bufsiz, format, localtime(&tval)) == 0)
|
||||
if ((buf = realloc(buf, bufsiz <<= 1)) == NULL)
|
||||
goto bad;
|
||||
|
||||
(void)printf("%s\n", buf + 1);
|
||||
(void)printf("%s\n", buf+1);
|
||||
free(buf);
|
||||
return 0;
|
||||
bad:
|
||||
err(EXIT_FAILURE, "Cannot allocate format buffer");
|
||||
err(1, "Cannot allocate format buffer");
|
||||
}
|
||||
|
||||
static void
|
||||
|
@ -204,8 +189,7 @@ setthetime(const char *p)
|
|||
badformat();
|
||||
}
|
||||
|
||||
if ((lt = localtime(&tval)) == NULL)
|
||||
err(EXIT_FAILURE, "localtime %lld failed", (long long)tval);
|
||||
lt = localtime(&tval);
|
||||
|
||||
lt->tm_isdst = -1; /* Divine correct DST */
|
||||
|
||||
|
@ -333,8 +317,7 @@ static void
|
|||
usage(void)
|
||||
{
|
||||
(void)fprintf(stderr,
|
||||
"Usage: %s [-ajnu] [-d date] [-r seconds] [+format]",
|
||||
getprogname());
|
||||
"usage: %s [-ajnu] [-d date] [-r seconds] [+format]", getprogname());
|
||||
(void)fprintf(stderr, " [[[[[[CC]yy]mm]dd]HH]MM[.SS]]\n");
|
||||
exit(EXIT_FAILURE);
|
||||
/* NOTREACHED */
|
||||
|
|
|
@ -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
|
||||
|
@ -34,7 +34,7 @@
|
|||
#if 0
|
||||
static char sccsid[] = "@(#)netdate.c 8.2 (Berkeley) 4/28/95";
|
||||
#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 /* 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 WAITDATEACK 5000 /* milliseconds */
|
||||
|
||||
extern int retval;
|
||||
|
||||
static const char *
|
||||
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,
|
||||
* it performs the correction on all slaves. If it is in the slave state, it
|
||||
* 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
|
||||
netsettime(time_t tval)
|
||||
|
@ -87,7 +89,7 @@ netsettime(time_t tval)
|
|||
|
||||
if ((sp = getservbyname("timed", "udp")) == NULL) {
|
||||
warnx("udp/timed: unknown service");
|
||||
return 2;
|
||||
return (retval = 2);
|
||||
}
|
||||
|
||||
(void)memset(&dest, 0, sizeof(dest));
|
||||
|
@ -98,18 +100,18 @@ netsettime(time_t tval)
|
|||
dest.sin_port = sp->s_port;
|
||||
dest.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
s = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (s == -1) {
|
||||
if (s < 0) {
|
||||
if (errno != EAFNOSUPPORT)
|
||||
warn("timed");
|
||||
return 2;
|
||||
return (retval = 2);
|
||||
}
|
||||
|
||||
#ifdef IP_PORTRANGE
|
||||
{
|
||||
static const int on = IP_PORTRANGE_LOW;
|
||||
|
||||
if (setsockopt(s, IPPROTO_IP, IP_PORTRANGE, &on,
|
||||
sizeof(on)) == -1) {
|
||||
if (setsockopt(s, IPPROTO_IP, IP_PORTRANGE,
|
||||
&on, sizeof(on)) < 0) {
|
||||
warn("setsockopt");
|
||||
goto bad;
|
||||
}
|
||||
|
@ -118,19 +120,20 @@ netsettime(time_t tval)
|
|||
|
||||
msg.tsp_type = TSP_SETDATE;
|
||||
msg.tsp_vers = TSPVERSION;
|
||||
if (gethostname(hostname, sizeof(hostname)) == -1) {
|
||||
if (gethostname(hostname, sizeof(hostname))) {
|
||||
warn("gethostname");
|
||||
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_time.tv_sec = htonl((uint32_t)tval); /* XXX: y2038 */
|
||||
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");
|
||||
goto bad;
|
||||
}
|
||||
if (send(s, &msg, sizeof(msg), 0) == -1) {
|
||||
if (send(s, &msg, sizeof(msg), 0) < 0) {
|
||||
if (errno != ECONNREFUSED)
|
||||
warn("send");
|
||||
goto bad;
|
||||
|
@ -148,7 +151,7 @@ loop:
|
|||
int error;
|
||||
|
||||
length = sizeof(error);
|
||||
if (getsockopt(s, SOL_SOCKET, SO_ERROR, &error, &length) == -1
|
||||
if (!getsockopt(s, SOL_SOCKET, SO_ERROR, &error, &length)
|
||||
&& error) {
|
||||
if (error != ECONNREFUSED)
|
||||
warn("send (delayed error)");
|
||||
|
@ -159,8 +162,8 @@ loop:
|
|||
if (found > 0 && ready.revents & POLLIN) {
|
||||
ssize_t ret;
|
||||
|
||||
if ((ret = recv(s, &msg, sizeof(msg), 0)) == -1) {
|
||||
if (ret < 0)
|
||||
ret = recv(s, &msg, sizeof(msg), 0);
|
||||
if (ret < 0) {
|
||||
if (errno != ECONNREFUSED)
|
||||
warn("recv");
|
||||
goto bad;
|
||||
|
@ -179,7 +182,7 @@ loop:
|
|||
goto loop;
|
||||
case TSP_DATEACK:
|
||||
(void)close(s);
|
||||
return 0;
|
||||
return (0);
|
||||
default:
|
||||
warnx("wrong ack received from timed: %s",
|
||||
tsp_type_to_string(&msg));
|
||||
|
@ -192,5 +195,5 @@ loop:
|
|||
|
||||
bad:
|
||||
(void)close(s);
|
||||
return 2;
|
||||
return (retval = 2);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue