Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

vsock/test: add timeout_usleep() to allow sleeping in timeout sections

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>

authored by

Stefano Garzarella and committed by
Jakub Kicinski
a7262ed4 9e1f7a31

+19
+18
tools/testing/vsock/timeout.c
··· 21 21 #include <stdbool.h> 22 22 #include <unistd.h> 23 23 #include <stdio.h> 24 + #include <time.h> 24 25 #include "timeout.h" 25 26 26 27 static volatile bool timeout; ··· 29 28 /* SIGALRM handler function. Do not use sleep(2), alarm(2), or 30 29 * setitimer(2) while using this API - they may interfere with each 31 30 * other. 31 + * 32 + * If you need to sleep, please use timeout_sleep() provided by this API. 32 33 */ 33 34 void sigalrm(int signo) 34 35 { ··· 60 57 { 61 58 alarm(0); 62 59 timeout = false; 60 + } 61 + 62 + /* Sleep in a timeout section. 63 + * 64 + * nanosleep(2) can be used with this API since POSIX.1 explicitly 65 + * specifies that it does not interact with signals. 66 + */ 67 + int timeout_usleep(useconds_t usec) 68 + { 69 + struct timespec ts = { 70 + .tv_sec = usec / 1000000, 71 + .tv_nsec = (usec % 1000000) * 1000, 72 + }; 73 + 74 + return nanosleep(&ts, NULL); 63 75 }
+1
tools/testing/vsock/timeout.h
··· 11 11 void timeout_begin(unsigned int seconds); 12 12 void timeout_check(const char *operation); 13 13 void timeout_end(void); 14 + int timeout_usleep(useconds_t usec); 14 15 15 16 #endif /* TIMEOUT_H */