]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
vsock/test: add timeout_usleep() to allow sleeping in timeout sections
authorStefano Garzarella <sgarzare@redhat.com>
Wed, 14 May 2025 14:19:25 +0000 (16:19 +0200)
committerJakub Kicinski <kuba@kernel.org>
Sat, 17 May 2025 01:01:32 +0000 (18:01 -0700)
The timeout API uses signals, so we have documented not to use sleep(),
but we can use nanosleep(2) since POSIX.1 explicitly specifies that it
does not interact with signals.

Let's provide timeout_usleep() for that.

Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
Link: https://patch.msgid.link/20250514141927.159456-2-sgarzare@redhat.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
tools/testing/vsock/timeout.c
tools/testing/vsock/timeout.h

index 44aee49b6cee07deb9443e3c2595ef680b726053..1453d38e08bbb0d3698fcf209933a5e7bdecc7e7 100644 (file)
@@ -21,6 +21,7 @@
 #include <stdbool.h>
 #include <unistd.h>
 #include <stdio.h>
+#include <time.h>
 #include "timeout.h"
 
 static volatile bool timeout;
@@ -28,6 +29,8 @@ static volatile bool timeout;
 /* SIGALRM handler function.  Do not use sleep(2), alarm(2), or
  * setitimer(2) while using this API - they may interfere with each
  * other.
+ *
+ * If you need to sleep, please use timeout_sleep() provided by this API.
  */
 void sigalrm(int signo)
 {
@@ -58,3 +61,18 @@ void timeout_end(void)
        alarm(0);
        timeout = false;
 }
+
+/* Sleep in a timeout section.
+ *
+ * nanosleep(2) can be used with this API since POSIX.1 explicitly
+ * specifies that it does not interact with signals.
+ */
+int timeout_usleep(useconds_t usec)
+{
+       struct timespec ts = {
+               .tv_sec = usec / 1000000,
+               .tv_nsec = (usec % 1000000) * 1000,
+       };
+
+       return nanosleep(&ts, NULL);
+}
index ecb7c840e65ae5d40419bf5f9ca57fdf4051aa41..1c3fcad87a49875a6fa1c6092f2e52b17d867a57 100644 (file)
@@ -11,5 +11,6 @@ void sigalrm(int signo);
 void timeout_begin(unsigned int seconds);
 void timeout_check(const char *operation);
 void timeout_end(void);
+int timeout_usleep(useconds_t usec);
 
 #endif /* TIMEOUT_H */