[LTP] [PATCH] openposix: timers/twoevtimers: Wait for both timer signals
Jan Stancek
jstancek@redhat.com
Fri Oct 9 08:49:19 CEST 2026
The test arms two timers with the same absolute expiration time and
then does a single sleep() before checking that both signals arrived.
sleep() returns as soon as the first signal interrupts it, and the
kernel delivers the two signals one at a time. This sporadically
leads to failure:
Caught abort signal
Test FAILED
even though the second timer fires microseconds later.
Keep sleeping on the remaining time until both signals have been
caught or the LONGTIME budget, which was defined but never used, runs
out. Make the counters volatile sig_atomic_t and print from the handlers
with the async-signal-safe PTS_WRITE_MSG() instead of printf().
Lower the sleep time, and also correct the header comment: the timers
live in the same process, not in different processes, and the test only
checks that both of them expire, not that they expire at the same time.
Signed-off-by: Jan Stancek <jstancek@redhat.com>
---
.../functional/timers/timers/twoevtimers.c | 34 +++++++++++++------
1 file changed, 23 insertions(+), 11 deletions(-)
diff --git a/testcases/open_posix_testsuite/functional/timers/timers/twoevtimers.c b/testcases/open_posix_testsuite/functional/timers/timers/twoevtimers.c
index 0e46229777fe..b581678d2b12 100644
--- a/testcases/open_posix_testsuite/functional/timers/timers/twoevtimers.c
+++ b/testcases/open_posix_testsuite/functional/timers/timers/twoevtimers.c
@@ -5,8 +5,14 @@
* of this license, see the COPYING file at the top level of this
* source tree.
*
- * Test having two timers in different processes set to expire at the
- * same time, and ensure they both expire at the same time.
+ * Test having two timers in the same process, armed with the same
+ * absolute expiration time but delivering different signals, and ensure
+ * that both of them expire.
+ *
+ * Note that the test does not (and cannot) verify that the two signals
+ * are delivered simultaneously. Signals are delivered one at a time, so
+ * the first one interrupts sleep() while the second one may still be
+ * on its way. Keep waiting until both have arrived or LONGTIME elapses.
*/
#include <stdio.h>
#include <time.h>
@@ -17,23 +23,23 @@
#include <unistd.h>
#include "posixtest.h"
-#define EXPIREDELTA 3
-#define LONGTIME 5
+#define EXPIREDELTA 1
+#define LONGTIME 3
#define CHILDPASS 1
-static int caughtabort = 0;
-static int caughtalarm = 0;
+static volatile sig_atomic_t caughtabort = 0;
+static volatile sig_atomic_t caughtalarm = 0;
-static void handler_abrt(int signo)
+static void handler_abrt(int signo PTS_ATTRIBUTE_UNUSED)
{
- printf("Caught abort signal\n");
+ PTS_WRITE_MSG("Caught abort signal\n");
caughtabort++;
}
-static void handler_alrm(int signo)
+static void handler_alrm(int signo PTS_ATTRIBUTE_UNUSED)
{
- printf("Caught alarm signal\n");
+ PTS_WRITE_MSG("Caught alarm signal\n");
caughtalarm++;
}
@@ -45,6 +51,7 @@ int test_main(int argc, char **argv)
struct timespec ts;
struct itimerspec its;
int flags = 0;
+ unsigned int left = LONGTIME;
act1.sa_handler = handler_abrt;
act1.sa_flags = 0;
@@ -93,7 +100,12 @@ int test_main(int argc, char **argv)
return PTS_UNRESOLVED;
}
- sleep(EXPIREDELTA + 1);
+ /*
+ * sleep() returns early when the first signal arrives; keep waiting
+ * until the second one has been delivered as well or we time out.
+ */
+ while (left > 0 && !(caughtalarm && caughtabort))
+ left = sleep(left);
if ((caughtalarm == 1) && (caughtabort == 1)) {
printf("Test PASSED\n");
--
2.56.0
More information about the ltp
mailing list