[git commit] libbb: introduce and use msleep()

Denys Vlasenko vda.linux at googlemail.com
Sun Nov 29 11:40:25 UTC 2020


commit: https://git.busybox.net/busybox/commit/?id=6a55b4e403979ba299261816a7ec1bb55bbf3f2b
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

function                                             old     new   delta
msleep                                                 -      45     +45
watchdog_main                                        271     266      -5
common_traceroute_main                              3546    3530     -16
beep_main                                            277     248     -29
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 0/3 up/down: 45/-50)             Total: -5 bytes

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 include/libbb.h         |  3 ++-
 libbb/bb_do_delay.c     | 15 ++++++++++++++-
 miscutils/beep.c        |  4 ++--
 miscutils/watchdog.c    |  2 +-
 networking/traceroute.c |  2 +-
 5 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/include/libbb.h b/include/libbb.h
index 9fa0ce90d..18dc9f935 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1602,7 +1602,8 @@ char *bb_simplify_path(const char *path) FAST_FUNC;
 char *bb_simplify_abs_path_inplace(char *path) FAST_FUNC;
 
 void pause_after_failed_login(void) FAST_FUNC;
-void bb_do_delay(int seconds) FAST_FUNC;
+void bb_do_delay(unsigned seconds) FAST_FUNC;
+void msleep(unsigned ms) FAST_FUNC;
 void sleep1(void) FAST_FUNC;
 void change_identity(const struct passwd *pw) FAST_FUNC;
 void run_shell(const char *shell, int loginshell, const char **args) NORETURN FAST_FUNC;
diff --git a/libbb/bb_do_delay.c b/libbb/bb_do_delay.c
index 3a86dc2ae..3dbf032db 100644
--- a/libbb/bb_do_delay.c
+++ b/libbb/bb_do_delay.c
@@ -8,7 +8,7 @@
  */
 #include "libbb.h"
 
-/* void FAST_FUNC bb_do_delay(int seconds) { ... } - no users yet */
+/* void FAST_FUNC bb_do_delay(unsigned seconds) { ... } - no users yet */
 
 #ifndef LOGIN_FAIL_DELAY
 #define LOGIN_FAIL_DELAY 3
@@ -34,3 +34,16 @@ void FAST_FUNC sleep1(void)
 	sleep(1);
 }
 
+void FAST_FUNC msleep(unsigned ms)
+{
+	/* 1. usleep(n) is not guaranteed by standards to accept n >= 1000000
+	 * 2. multiplication in usleep(ms * 1000) can overflow if ms > 4294967
+	 *    (sleep of ~71.5 minutes)
+	 * Let's play safe and loop:
+	 */
+	while (ms > 500) {
+		usleep(500000);
+		ms -= 500;
+	}
+	usleep(ms * 1000);
+}
diff --git a/miscutils/beep.c b/miscutils/beep.c
index 1669332fd..7c60aed08 100644
--- a/miscutils/beep.c
+++ b/miscutils/beep.c
@@ -114,10 +114,10 @@ int beep_main(int argc, char **argv)
 		while (rep) {
 //bb_error_msg("rep[%d] freq=%d, length=%d, delay=%d", rep, freq, length, delay);
 			xioctl(speaker, KIOCSOUND, (void*)(uintptr_t)tickrate_div_freq);
-			usleep(1000 * length);
+			msleep(length);
 			ioctl(speaker, KIOCSOUND, (void*)0);
 			if (--rep)
-				usleep(1000 * delay);
+				msleep(delay);
 		}
 	}
 
diff --git a/miscutils/watchdog.c b/miscutils/watchdog.c
index 8c8d7217f..971b777a3 100644
--- a/miscutils/watchdog.c
+++ b/miscutils/watchdog.c
@@ -143,7 +143,7 @@ int watchdog_main(int argc UNUSED_PARAM, char **argv)
 		 * as the counter value is undefined at this point -- PFM
 		 */
 		write(3, "", 1); /* write zero byte */
-		usleep(stimer_duration * 1000L);
+		msleep(stimer_duration);
 	}
 	return EXIT_SUCCESS; /* - not reached, but gcc 4.2.1 is too dumb! */
 }
diff --git a/networking/traceroute.c b/networking/traceroute.c
index 1c4dc3e4a..34d2a09dd 100644
--- a/networking/traceroute.c
+++ b/networking/traceroute.c
@@ -1071,7 +1071,7 @@ common_traceroute_main(int op, char **argv)
 
 			fflush_all();
 			if (probe != 0 && pausemsecs > 0)
-				usleep(pausemsecs * 1000);
+				msleep(pausemsecs);
 
 			send_probe(++seq, ttl);
 			t2 = t1 = monotonic_us();


More information about the busybox-cvs mailing list