diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2017-01-20 16:03:48 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2017-01-20 16:03:48 +0100 |
commit | 179e88bec91cfe58096900dc5509a080ff37b083 (patch) | |
tree | 069b8a997ea753191155630a2c4f8568e14df915 | |
parent | 19e695ebadda206d1e0fbefa59ed8fabee0d0f64 (diff) | |
download | busybox-w32-179e88bec91cfe58096900dc5509a080ff37b083.tar.gz busybox-w32-179e88bec91cfe58096900dc5509a080ff37b083.tar.bz2 busybox-w32-179e88bec91cfe58096900dc5509a080ff37b083.zip |
rdate: make it do something remotely sane, facing 32-bit time overflow
function old new delta
rdate_main 251 254 +3
packed_usage 31029 31023 -6
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | networking/inetd.c | 2 | ||||
-rw-r--r-- | util-linux/rdate.c | 33 |
2 files changed, 28 insertions, 7 deletions
diff --git a/networking/inetd.c b/networking/inetd.c index 4d0ab2e0d..612284e76 100644 --- a/networking/inetd.c +++ b/networking/inetd.c | |||
@@ -1677,7 +1677,7 @@ static uint32_t machtime(void) | |||
1677 | struct timeval tv; | 1677 | struct timeval tv; |
1678 | 1678 | ||
1679 | gettimeofday(&tv, NULL); | 1679 | gettimeofday(&tv, NULL); |
1680 | return htonl((uint32_t)(tv.tv_sec + 2208988800)); | 1680 | return htonl((uint32_t)(tv.tv_sec + 2208988800U)); |
1681 | } | 1681 | } |
1682 | /* ARGSUSED */ | 1682 | /* ARGSUSED */ |
1683 | static void FAST_FUNC machtime_stream(int s, servtab_t *sep UNUSED_PARAM) | 1683 | static void FAST_FUNC machtime_stream(int s, servtab_t *sep UNUSED_PARAM) |
diff --git a/util-linux/rdate.c b/util-linux/rdate.c index 8dd784d3d..a62591914 100644 --- a/util-linux/rdate.c +++ b/util-linux/rdate.c | |||
@@ -21,11 +21,11 @@ | |||
21 | //kbuild:lib-$(CONFIG_RDATE) += rdate.o | 21 | //kbuild:lib-$(CONFIG_RDATE) += rdate.o |
22 | 22 | ||
23 | //usage:#define rdate_trivial_usage | 23 | //usage:#define rdate_trivial_usage |
24 | //usage: "[-sp] HOST" | 24 | //usage: "[-s/-p] HOST" |
25 | //usage:#define rdate_full_usage "\n\n" | 25 | //usage:#define rdate_full_usage "\n\n" |
26 | //usage: "Get and possibly set system time from a remote HOST\n" | 26 | //usage: "Set and print time from HOST using RFC 868\n" |
27 | //usage: "\n -s Set system time (default)" | 27 | //usage: "\n -s Only set system time" |
28 | //usage: "\n -p Print time" | 28 | //usage: "\n -p Only print time" |
29 | 29 | ||
30 | #include "libbb.h" | 30 | #include "libbb.h" |
31 | 31 | ||
@@ -58,8 +58,22 @@ static time_t askremotedate(const char *host) | |||
58 | * the RFC 868 time 2,208,988,800 corresponds to 00:00 1 Jan 1970 GMT | 58 | * the RFC 868 time 2,208,988,800 corresponds to 00:00 1 Jan 1970 GMT |
59 | * Subtract the RFC 868 time to get Linux epoch. | 59 | * Subtract the RFC 868 time to get Linux epoch. |
60 | */ | 60 | */ |
61 | 61 | nett = ntohl(nett) - RFC_868_BIAS; | |
62 | return ntohl(nett) - RFC_868_BIAS; | 62 | |
63 | if (sizeof(time_t) > 4) { | ||
64 | /* Now we have 32-bit lsb of a wider time_t | ||
65 | * Imagine that nett = 0x00000001, | ||
66 | * current time cur = 0x123ffffffff. | ||
67 | * Assuming our time is not some 40 years off, | ||
68 | * remote time must be 0x12400000001. | ||
69 | * Need to adjust out time by (int32_t)(nett - cur). | ||
70 | */ | ||
71 | time_t cur = time(NULL); | ||
72 | int32_t adjust = (int32_t)(nett - (uint32_t)cur); | ||
73 | return cur + adjust; | ||
74 | } | ||
75 | /* This is not going to work, but what can we do */ | ||
76 | return (time_t)nett; | ||
63 | } | 77 | } |
64 | 78 | ||
65 | int rdate_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 79 | int rdate_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
@@ -73,6 +87,13 @@ int rdate_main(int argc UNUSED_PARAM, char **argv) | |||
73 | 87 | ||
74 | remote_time = askremotedate(argv[optind]); | 88 | remote_time = askremotedate(argv[optind]); |
75 | 89 | ||
90 | /* Manpages of various Unixes are confusing. What happens is: | ||
91 | * (no opts) set and print time | ||
92 | * -s: set time ("do not print the time") | ||
93 | * -p: print time ("do not set, just print the remote time") | ||
94 | * -sp: print time (that's what we do, not sure this is right) | ||
95 | */ | ||
96 | |||
76 | if (!(flags & 2)) { /* no -p (-s may be present) */ | 97 | if (!(flags & 2)) { /* no -p (-s may be present) */ |
77 | time_t current_time; | 98 | time_t current_time; |
78 | 99 | ||