[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [PATCH] timeout: allow fractional seconds in timeout values
From:       Ron Yorston <rmy () pobox ! com>
Date:       2024-04-08 12:12:25
Message-ID: 6613df29.ZXgyiGsvwOS5oiJ0%rmy () pobox ! com
[Download RAW message or body]

The 'timeout' applet uses parse_duration_str() to obtain its
timeout values.  The default configuration enables float durations.

However, the applet silently ignores fractional seconds.  This
results in unexpected behaviour:

   $ timeout 5.99 sleep 5.1; echo $?
   Terminated
   143

When float durations are enabled ensure that any fractional seconds
are taken into account.

function                                             old     new   delta
timeout_wait                                          44      92     +48
timeout_main                                         383     365     -18
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 48/-18)             Total: 30 bytes

Signed-off-by: Ron Yorston <rmy@pobox.com>
---
 coreutils/timeout.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/coreutils/timeout.c b/coreutils/timeout.c
index 18aa9ebeb..84299a0a5 100644
--- a/coreutils/timeout.c
+++ b/coreutils/timeout.c
@@ -47,11 +47,16 @@
 
 #include "libbb.h"
 
-static NOINLINE int timeout_wait(int timeout, pid_t pid)
+static NOINLINE int timeout_wait(duration_t timeout, pid_t pid)
 {
 	/* Just sleep(HUGE_NUM); kill(parent) may kill wrong process! */
 	while (1) {
-		sleep1();
+#if ENABLE_FLOAT_DURATION
+		if (timeout < 1)
+			sleep_for_duration(timeout);
+		else
+#endif
+			sleep1();
 		if (--timeout <= 0)
 			break;
 		if (kill(pid, 0)) {
@@ -68,8 +73,8 @@ int timeout_main(int argc UNUSED_PARAM, char **argv)
 	int signo;
 	int status;
 	int parent = 0;
-	int timeout;
-	int kill_timeout;
+	duration_t timeout;
+	duration_t kill_timeout;
 	pid_t pid;
 #if !BB_MMU
 	char *sv1, *sv2;
-- 
2.44.0

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic