aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2020-12-30 23:48:01 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2020-12-30 23:48:01 +0100
commit3c13da3dab539eac948de48640d8862857d0c8d0 (patch)
tree9cf3ae59b79ca89cbcfe08ce6b2b121f37d54a66
parent89a55972fd5d7f0ab815c2a62be69a8f34718353 (diff)
downloadbusybox-w32-3c13da3dab539eac948de48640d8862857d0c8d0.tar.gz
busybox-w32-3c13da3dab539eac948de48640d8862857d0c8d0.tar.bz2
busybox-w32-3c13da3dab539eac948de48640d8862857d0c8d0.zip
libbb: introduce and use xgettimeofday(), do not truncate 64-bit time_t in shells
function old new delta xgettimeofday - 11 +11 get_local_var_value 280 281 +1 svlogd_main 1323 1322 -1 change_epoch 67 66 -1 timestamp_and_log 461 458 -3 hwclock_main 301 298 -3 fmt_time_bernstein_25 135 132 -3 step_time 331 326 -5 script_main 1207 1202 -5 machtime 34 28 -6 curtime 61 54 -7 ts_main 423 415 -8 nmeter_main 761 751 -10 gettime1900d 67 46 -21 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 1/12 up/down: 12/-73) Total: -61 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--include/libbb.h1
-rw-r--r--libbb/time.c6
-rw-r--r--libbb/xfuncs_printf.c11
-rw-r--r--miscutils/ts.c4
-rw-r--r--networking/inetd.c2
-rw-r--r--networking/ntpd.c4
-rw-r--r--networking/traceroute.c2
-rw-r--r--procps/nmeter.c8
-rw-r--r--runit/runsv.c4
-rw-r--r--runit/svlogd.c4
-rw-r--r--shell/ash.c10
-rw-r--r--shell/hush.c10
-rw-r--r--sysklogd/syslogd.c2
-rw-r--r--util-linux/hwclock.c8
-rw-r--r--util-linux/mdev.c2
-rw-r--r--util-linux/script.c2
16 files changed, 46 insertions, 34 deletions
diff --git a/include/libbb.h b/include/libbb.h
index cae54658b..dad6fc687 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -657,6 +657,7 @@ void parse_datestr(const char *date_str, struct tm *ptm) FAST_FUNC;
657time_t validate_tm_time(const char *date_str, struct tm *ptm) FAST_FUNC; 657time_t validate_tm_time(const char *date_str, struct tm *ptm) FAST_FUNC;
658char *strftime_HHMMSS(char *buf, unsigned len, time_t *tp) FAST_FUNC; 658char *strftime_HHMMSS(char *buf, unsigned len, time_t *tp) FAST_FUNC;
659char *strftime_YYYYMMDDHHMMSS(char *buf, unsigned len, time_t *tp) FAST_FUNC; 659char *strftime_YYYYMMDDHHMMSS(char *buf, unsigned len, time_t *tp) FAST_FUNC;
660void xgettimeofday(struct timeval *tv) FAST_FUNC;
660void xsettimeofday(const struct timeval *tv) FAST_FUNC; 661void xsettimeofday(const struct timeval *tv) FAST_FUNC;
661 662
662 663
diff --git a/libbb/time.c b/libbb/time.c
index 74a69eefb..cf5f2e5c8 100644
--- a/libbb/time.c
+++ b/libbb/time.c
@@ -291,19 +291,19 @@ unsigned FAST_FUNC monotonic_sec(void)
291unsigned long long FAST_FUNC monotonic_ns(void) 291unsigned long long FAST_FUNC monotonic_ns(void)
292{ 292{
293 struct timeval tv; 293 struct timeval tv;
294 gettimeofday(&tv, NULL); 294 xgettimeofday(&tv);
295 return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000; 295 return tv.tv_sec * 1000000000ULL + tv.tv_usec * 1000;
296} 296}
297unsigned long long FAST_FUNC monotonic_us(void) 297unsigned long long FAST_FUNC monotonic_us(void)
298{ 298{
299 struct timeval tv; 299 struct timeval tv;
300 gettimeofday(&tv, NULL); 300 xgettimeofday(&tv);
301 return tv.tv_sec * 1000000ULL + tv.tv_usec; 301 return tv.tv_sec * 1000000ULL + tv.tv_usec;
302} 302}
303unsigned long long FAST_FUNC monotonic_ms(void) 303unsigned long long FAST_FUNC monotonic_ms(void)
304{ 304{
305 struct timeval tv; 305 struct timeval tv;
306 gettimeofday(&tv, NULL); 306 xgettimeofday(&tv);
307 return tv.tv_sec * 1000ULL + tv.tv_usec / 1000; 307 return tv.tv_sec * 1000ULL + tv.tv_usec / 1000;
308} 308}
309unsigned FAST_FUNC monotonic_sec(void) 309unsigned FAST_FUNC monotonic_sec(void)
diff --git a/libbb/xfuncs_printf.c b/libbb/xfuncs_printf.c
index aea995a5c..99596b9d0 100644
--- a/libbb/xfuncs_printf.c
+++ b/libbb/xfuncs_printf.c
@@ -720,3 +720,14 @@ void FAST_FUNC xsettimeofday(const struct timeval *tv)
720 if (settimeofday(tv, NULL)) 720 if (settimeofday(tv, NULL))
721 bb_simple_perror_msg_and_die("settimeofday"); 721 bb_simple_perror_msg_and_die("settimeofday");
722} 722}
723
724void FAST_FUNC xgettimeofday(struct timeval *tv)
725{
726#if 0
727 if (gettimeofday(tv, NULL))
728 bb_simple_perror_msg_and_die("gettimeofday");
729#else
730 /* Never fails on Linux */
731 gettimeofday(tv, NULL);
732#endif
733}
diff --git a/miscutils/ts.c b/miscutils/ts.c
index f2d367654..6e5d77bda 100644
--- a/miscutils/ts.c
+++ b/miscutils/ts.c
@@ -47,13 +47,13 @@ int ts_main(int argc UNUSED_PARAM, char **argv)
47 47
48#define date_buf bb_common_bufsiz1 48#define date_buf bb_common_bufsiz1
49 setup_common_bufsiz(); 49 setup_common_bufsiz();
50 gettimeofday(&base, NULL); 50 xgettimeofday(&base);
51 51
52 while ((line = xmalloc_fgets(stdin)) != NULL) { 52 while ((line = xmalloc_fgets(stdin)) != NULL) {
53 struct timeval ts; 53 struct timeval ts;
54 struct tm tm_time; 54 struct tm tm_time;
55 55
56 gettimeofday(&ts, NULL); 56 xgettimeofday(&ts);
57 if (opt) { 57 if (opt) {
58 /* -i and/or -s */ 58 /* -i and/or -s */
59 struct timeval ts1 = ts1; 59 struct timeval ts1 = ts1;
diff --git a/networking/inetd.c b/networking/inetd.c
index 9f5a436d6..febfb7b73 100644
--- a/networking/inetd.c
+++ b/networking/inetd.c
@@ -1701,7 +1701,7 @@ static uint32_t machtime(void)
1701{ 1701{
1702 struct timeval tv; 1702 struct timeval tv;
1703 1703
1704 gettimeofday(&tv, NULL); 1704 xgettimeofday(&tv);
1705 return htonl((uint32_t)(tv.tv_sec + 2208988800U)); 1705 return htonl((uint32_t)(tv.tv_sec + 2208988800U));
1706} 1706}
1707/* ARGSUSED */ 1707/* ARGSUSED */
diff --git a/networking/ntpd.c b/networking/ntpd.c
index 5a540a391..46d8f3495 100644
--- a/networking/ntpd.c
+++ b/networking/ntpd.c
@@ -560,7 +560,7 @@ static double
560gettime1900d(void) 560gettime1900d(void)
561{ 561{
562 struct timeval tv; 562 struct timeval tv;
563 gettimeofday(&tv, NULL); /* never fails */ 563 xgettimeofday(&tv);
564 G.cur_time = tv.tv_sec + (1.0e-6 * tv.tv_usec) + OFFSET_1900_1970; 564 G.cur_time = tv.tv_sec + (1.0e-6 * tv.tv_usec) + OFFSET_1900_1970;
565 return G.cur_time; 565 return G.cur_time;
566} 566}
@@ -1144,7 +1144,7 @@ step_time(double offset)
1144 char buf[sizeof("yyyy-mm-dd hh:mm:ss") + /*paranoia:*/ 4]; 1144 char buf[sizeof("yyyy-mm-dd hh:mm:ss") + /*paranoia:*/ 4];
1145 time_t tval; 1145 time_t tval;
1146 1146
1147 gettimeofday(&tvc, NULL); /* never fails */ 1147 xgettimeofday(&tvc);
1148 dtime = tvc.tv_sec + (1.0e-6 * tvc.tv_usec) + offset; 1148 dtime = tvc.tv_sec + (1.0e-6 * tvc.tv_usec) + offset;
1149 d_to_tv(dtime, &tvn); 1149 d_to_tv(dtime, &tvn);
1150 xsettimeofday(&tvn); 1150 xsettimeofday(&tvn);
diff --git a/networking/traceroute.c b/networking/traceroute.c
index 3f1a9ab46..bd63e4449 100644
--- a/networking/traceroute.c
+++ b/networking/traceroute.c
@@ -505,7 +505,7 @@ send_probe(int seq, int ttl)
505 struct outdata6_t *pkt = (void *) outdata; 505 struct outdata6_t *pkt = (void *) outdata;
506 pkt->ident6 = ident; 506 pkt->ident6 = ident;
507 pkt->seq6 = htonl(seq); 507 pkt->seq6 = htonl(seq);
508 /*gettimeofday(&pkt->tv, &tz);*/ 508 /*xgettimeofday(&pkt->tv);*/
509 icp = outicmp6; 509 icp = outicmp6;
510 } else 510 } else
511#endif 511#endif
diff --git a/procps/nmeter.c b/procps/nmeter.c
index 07b7abe2f..f08938654 100644
--- a/procps/nmeter.c
+++ b/procps/nmeter.c
@@ -952,11 +952,11 @@ int nmeter_main(int argc UNUSED_PARAM, char **argv)
952 reset_outbuf(); 952 reset_outbuf();
953 953
954 if (G.delta >= 0) { 954 if (G.delta >= 0) {
955 gettimeofday(&G.tv, NULL); 955 xgettimeofday(&G.tv);
956 usleep(G.delta > 1000000 ? 1000000 : G.delta - G.tv.tv_usec % G.deltanz); 956 usleep(G.delta > 1000000 ? 1000000 : G.delta - G.tv.tv_usec % G.deltanz);
957 } 957 }
958 958
959 gettimeofday(&G.start, NULL); 959 xgettimeofday(&G.start);
960 G.tv = G.start; 960 G.tv = G.start;
961 while (1) { 961 while (1) {
962 collect_info(first); 962 collect_info(first);
@@ -971,7 +971,7 @@ int nmeter_main(int argc UNUSED_PARAM, char **argv)
971 if (G.delta >= 0) { 971 if (G.delta >= 0) {
972 int rem; 972 int rem;
973 // can be commented out, will sacrifice sleep time precision a bit 973 // can be commented out, will sacrifice sleep time precision a bit
974 gettimeofday(&G.tv, NULL); 974 xgettimeofday(&G.tv);
975 if (need_seconds) 975 if (need_seconds)
976 rem = G.delta - ((ullong)G.tv.tv_sec*1000000 + G.tv.tv_usec) % G.deltanz; 976 rem = G.delta - ((ullong)G.tv.tv_sec*1000000 + G.tv.tv_usec) % G.deltanz;
977 else 977 else
@@ -983,7 +983,7 @@ int nmeter_main(int argc UNUSED_PARAM, char **argv)
983 } 983 }
984 usleep(rem); 984 usleep(rem);
985 } 985 }
986 gettimeofday(&G.tv, NULL); 986 xgettimeofday(&G.tv);
987 } 987 }
988 988
989 /*return 0;*/ 989 /*return 0;*/
diff --git a/runit/runsv.c b/runit/runsv.c
index d395d4528..ecab8cdf5 100644
--- a/runit/runsv.c
+++ b/runit/runsv.c
@@ -62,12 +62,12 @@ static void gettimeofday_ns(struct timespec *ts)
62 && sizeof(((struct timeval*)ts)->tv_usec) == sizeof(ts->tv_nsec) 62 && sizeof(((struct timeval*)ts)->tv_usec) == sizeof(ts->tv_nsec)
63 ) { 63 ) {
64 /* Cheat */ 64 /* Cheat */
65 gettimeofday((void*)ts, NULL); 65 xgettimeofday((void*)ts);
66 ts->tv_nsec *= 1000; 66 ts->tv_nsec *= 1000;
67 } else { 67 } else {
68 /* For example, musl has "incompatible" layouts */ 68 /* For example, musl has "incompatible" layouts */
69 struct timeval tv; 69 struct timeval tv;
70 gettimeofday(&tv, NULL); 70 xgettimeofday(&tv);
71 ts->tv_sec = tv.tv_sec; 71 ts->tv_sec = tv.tv_sec;
72 ts->tv_nsec = tv.tv_usec * 1000; 72 ts->tv_nsec = tv.tv_usec * 1000;
73 } 73 }
diff --git a/runit/svlogd.c b/runit/svlogd.c
index 040e71104..294e31aca 100644
--- a/runit/svlogd.c
+++ b/runit/svlogd.c
@@ -351,7 +351,7 @@ static void fmt_time_human_30nul(char *s, char dt_delim)
351 struct tm *ptm; 351 struct tm *ptm;
352 struct timeval tv; 352 struct timeval tv;
353 353
354 gettimeofday(&tv, NULL); 354 xgettimeofday(&tv);
355 ptm = gmtime_r(&tv.tv_sec, &tm); 355 ptm = gmtime_r(&tv.tv_sec, &tm);
356 /* ^^^ using gmtime_r() instead of gmtime() to not use static data */ 356 /* ^^^ using gmtime_r() instead of gmtime() to not use static data */
357 sprintf(s, "%04u-%02u-%02u%c%02u:%02u:%02u.%06u000", 357 sprintf(s, "%04u-%02u-%02u%c%02u:%02u:%02u.%06u000",
@@ -376,7 +376,7 @@ static void fmt_time_bernstein_25(char *s)
376 struct timeval tv; 376 struct timeval tv;
377 unsigned sec_hi; 377 unsigned sec_hi;
378 378
379 gettimeofday(&tv, NULL); 379 xgettimeofday(&tv);
380 sec_hi = (0x400000000000000aULL + tv.tv_sec) >> 32; 380 sec_hi = (0x400000000000000aULL + tv.tv_sec) >> 32;
381 tv.tv_sec = (time_t)(0x400000000000000aULL) + tv.tv_sec; 381 tv.tv_sec = (time_t)(0x400000000000000aULL) + tv.tv_sec;
382 tv.tv_usec *= 1000; 382 tv.tv_usec *= 1000;
diff --git a/shell/ash.c b/shell/ash.c
index f16d7fb6a..54f004375 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -11371,10 +11371,10 @@ static void FAST_FUNC
11371change_epoch(struct var *vepoch, const char *fmt) 11371change_epoch(struct var *vepoch, const char *fmt)
11372{ 11372{
11373 struct timeval tv; 11373 struct timeval tv;
11374 char buffer[sizeof("%lu.nnnnnn") + sizeof(long)*3]; 11374 char buffer[sizeof("%llu.nnnnnn") + sizeof(long long)*3];
11375 11375
11376 gettimeofday(&tv, NULL); 11376 xgettimeofday(&tv);
11377 sprintf(buffer, fmt, (unsigned long)tv.tv_sec, (unsigned)tv.tv_usec); 11377 sprintf(buffer, fmt, (unsigned long long)tv.tv_sec, (unsigned)tv.tv_usec);
11378 setvar(vepoch->var_text, buffer, VNOFUNC); 11378 setvar(vepoch->var_text, buffer, VNOFUNC);
11379 vepoch->flags &= ~VNOFUNC; 11379 vepoch->flags &= ~VNOFUNC;
11380} 11380}
@@ -11382,13 +11382,13 @@ change_epoch(struct var *vepoch, const char *fmt)
11382static void FAST_FUNC 11382static void FAST_FUNC
11383change_seconds(const char *value UNUSED_PARAM) 11383change_seconds(const char *value UNUSED_PARAM)
11384{ 11384{
11385 change_epoch(&vepochs, "%lu"); 11385 change_epoch(&vepochs, "%llu");
11386} 11386}
11387 11387
11388static void FAST_FUNC 11388static void FAST_FUNC
11389change_realtime(const char *value UNUSED_PARAM) 11389change_realtime(const char *value UNUSED_PARAM)
11390{ 11390{
11391 change_epoch(&vepochr, "%lu.%06u"); 11391 change_epoch(&vepochr, "%llu.%06u");
11392} 11392}
11393#endif 11393#endif
11394 11394
diff --git a/shell/hush.c b/shell/hush.c
index 9fead37da..65f08659f 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -1027,7 +1027,7 @@ struct globals {
1027 struct sigaction sa; 1027 struct sigaction sa;
1028 char optstring_buf[sizeof("eixcs")]; 1028 char optstring_buf[sizeof("eixcs")];
1029#if BASH_EPOCH_VARS 1029#if BASH_EPOCH_VARS
1030 char epoch_buf[sizeof("%lu.nnnnnn") + sizeof(long)*3]; 1030 char epoch_buf[sizeof("%llu.nnnnnn") + sizeof(long long)*3];
1031#endif 1031#endif
1032#if ENABLE_FEATURE_EDITING 1032#if ENABLE_FEATURE_EDITING
1033 char user_input_buf[CONFIG_FEATURE_EDITING_MAX_LEN]; 1033 char user_input_buf[CONFIG_FEATURE_EDITING_MAX_LEN];
@@ -2277,13 +2277,13 @@ static const char* FAST_FUNC get_local_var_value(const char *name)
2277 { 2277 {
2278 const char *fmt = NULL; 2278 const char *fmt = NULL;
2279 if (strcmp(name, "EPOCHSECONDS") == 0) 2279 if (strcmp(name, "EPOCHSECONDS") == 0)
2280 fmt = "%lu"; 2280 fmt = "%llu";
2281 else if (strcmp(name, "EPOCHREALTIME") == 0) 2281 else if (strcmp(name, "EPOCHREALTIME") == 0)
2282 fmt = "%lu.%06u"; 2282 fmt = "%llu.%06u";
2283 if (fmt) { 2283 if (fmt) {
2284 struct timeval tv; 2284 struct timeval tv;
2285 gettimeofday(&tv, NULL); 2285 xgettimeofday(&tv);
2286 sprintf(G.epoch_buf, fmt, (unsigned long)tv.tv_sec, 2286 sprintf(G.epoch_buf, fmt, (unsigned long long)tv.tv_sec,
2287 (unsigned)tv.tv_usec); 2287 (unsigned)tv.tv_usec);
2288 return G.epoch_buf; 2288 return G.epoch_buf;
2289 } 2289 }
diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c
index 598f28d0c..6ddfd771a 100644
--- a/sysklogd/syslogd.c
+++ b/sysklogd/syslogd.c
@@ -842,7 +842,7 @@ static void timestamp_and_log(int pri, char *msg, int len)
842#if ENABLE_FEATURE_SYSLOGD_PRECISE_TIMESTAMPS 842#if ENABLE_FEATURE_SYSLOGD_PRECISE_TIMESTAMPS
843 if (!timestamp) { 843 if (!timestamp) {
844 struct timeval tv; 844 struct timeval tv;
845 gettimeofday(&tv, NULL); 845 xgettimeofday(&tv);
846 now = tv.tv_sec; 846 now = tv.tv_sec;
847 timestamp = ctime(&now) + 4; /* skip day of week */ 847 timestamp = ctime(&now) + 4; /* skip day of week */
848 /* overwrite year by milliseconds, zero terminate */ 848 /* overwrite year by milliseconds, zero terminate */
diff --git a/util-linux/hwclock.c b/util-linux/hwclock.c
index 25db7cdad..723b09589 100644
--- a/util-linux/hwclock.c
+++ b/util-linux/hwclock.c
@@ -79,7 +79,7 @@ static time_t read_rtc(const char **pp_rtcname, struct timeval *sys_tv, int utc)
79 int before = tm_time.tm_sec; 79 int before = tm_time.tm_sec;
80 while (1) { 80 while (1) {
81 rtc_read_tm(&tm_time, fd); 81 rtc_read_tm(&tm_time, fd);
82 gettimeofday(sys_tv, NULL); 82 xgettimeofday(sys_tv);
83 if (before != (int)tm_time.tm_sec) 83 if (before != (int)tm_time.tm_sec)
84 break; 84 break;
85 } 85 }
@@ -205,7 +205,7 @@ static void from_sys_clock(const char **pp_rtcname, int utc)
205 int rtc; 205 int rtc;
206 206
207 rtc = rtc_xopen(pp_rtcname, O_WRONLY); 207 rtc = rtc_xopen(pp_rtcname, O_WRONLY);
208 gettimeofday(&tv, NULL); 208 xgettimeofday(&tv);
209 /* Prepare tm_time */ 209 /* Prepare tm_time */
210 if (sizeof(time_t) == sizeof(tv.tv_sec)) { 210 if (sizeof(time_t) == sizeof(tv.tv_sec)) {
211 if (utc) 211 if (utc)
@@ -253,7 +253,7 @@ static void from_sys_clock(const char **pp_rtcname, int utc)
253 unsigned rem_usec; 253 unsigned rem_usec;
254 time_t t; 254 time_t t;
255 255
256 gettimeofday(&tv, NULL); 256 xgettimeofday(&tv);
257 257
258 t = tv.tv_sec; 258 t = tv.tv_sec;
259 rem_usec = 1000000 - tv.tv_usec; 259 rem_usec = 1000000 - tv.tv_usec;
@@ -274,7 +274,7 @@ static void from_sys_clock(const char **pp_rtcname, int utc)
274 } 274 }
275 275
276 /* gmtime/localtime took some time, re-get cur time */ 276 /* gmtime/localtime took some time, re-get cur time */
277 gettimeofday(&tv, NULL); 277 xgettimeofday(&tv);
278 278
279 if (tv.tv_sec < t /* we are still in old second */ 279 if (tv.tv_sec < t /* we are still in old second */
280 || (tv.tv_sec == t && tv.tv_usec < adj) /* not too far into next second */ 280 || (tv.tv_sec == t && tv.tv_usec < adj) /* not too far into next second */
diff --git a/util-linux/mdev.c b/util-linux/mdev.c
index ebf83d1a3..dbbcbc655 100644
--- a/util-linux/mdev.c
+++ b/util-linux/mdev.c
@@ -963,7 +963,7 @@ static void load_firmware(const char *firmware, const char *sysfs_path)
963static char *curtime(void) 963static char *curtime(void)
964{ 964{
965 struct timeval tv; 965 struct timeval tv;
966 gettimeofday(&tv, NULL); 966 xgettimeofday(&tv);
967 sprintf( 967 sprintf(
968 strftime_HHMMSS(G.timestr, sizeof(G.timestr), &tv.tv_sec), 968 strftime_HHMMSS(G.timestr, sizeof(G.timestr), &tv.tv_sec),
969 ".%06u", 969 ".%06u",
diff --git a/util-linux/script.c b/util-linux/script.c
index 4eac5e94f..963435335 100644
--- a/util-linux/script.c
+++ b/util-linux/script.c
@@ -172,7 +172,7 @@ int script_main(int argc UNUSED_PARAM, char **argv)
172 struct timeval tv; 172 struct timeval tv;
173 double newtime; 173 double newtime;
174 174
175 gettimeofday(&tv, NULL); 175 xgettimeofday(&tv);
176 newtime = tv.tv_sec + (double) tv.tv_usec / 1000000; 176 newtime = tv.tv_sec + (double) tv.tv_usec / 1000000;
177 fprintf(timing_fp, "%f %u\n", newtime - oldtime, count); 177 fprintf(timing_fp, "%f %u\n", newtime - oldtime, count);
178 oldtime = newtime; 178 oldtime = newtime;