[LTP] [PATCH] nanosleep: use POSIX runtime detection for CLOCK_MONOTONIC
Andrea Cervesato
andrea.cervesato@suse.de
Mon May 4 15:20:18 CEST 2026
From: Andrea Cervesato <andrea.cervesato@suse.com>
The compile-time #ifdef _POSIX_MONOTONIC_CLOCK check was incorrect:
three tests did not include <unistd.h> so the macro was never defined,
and on Linux/glibc it is set to 0 which requires a runtime sysconf()
check. This caused tests to silently fall back to CLOCK_REALTIME,
leading to sporadic failures when NTP or VM time sync adjusted the
wall clock during sleep.
Add pts_get_clock() to clock.h and use it in all nanosleep tests
to perform proper POSIX runtime detection via sysconf().
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
.../conformance/interfaces/nanosleep/1-1.c | 16 ++++------------
.../conformance/interfaces/nanosleep/1-2.c | 16 ++++------------
.../conformance/interfaces/nanosleep/1-3.c | 16 ++++------------
.../conformance/interfaces/nanosleep/10000-1.c | 16 ++++------------
.../conformance/interfaces/nanosleep/2-1.c | 16 ++++------------
.../conformance/interfaces/nanosleep/3-2.c | 16 ++++------------
.../conformance/interfaces/nanosleep/7-2.c | 16 ++++------------
testcases/open_posix_testsuite/include/clock.h | 9 +++++++++
8 files changed, 37 insertions(+), 84 deletions(-)
diff --git a/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/1-1.c b/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/1-1.c
index 0f417715bf0a85e926eb9f1e0cf5cf5e9f172547..9d390e3206867b5555eade5a832d832e73c2a11d 100644
--- a/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/1-1.c
+++ b/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/1-1.c
@@ -11,24 +11,16 @@
#include <stdio.h>
#include <time.h>
#include "posixtest.h"
-
-#ifdef _POSIX_MONOTONIC_CLOCK
-#define TEST_CLOCK CLOCK_MONOTONIC
-#else
-#define TEST_CLOCK CLOCK_REALTIME
-#endif
+#include "clock.h"
int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED)
{
struct timespec tssleepfor, tsstorage, tsbefore, tsafter;
int sleepnsec = 3;
int slepts = 0, sleptns = 0;
+ clockid_t test_clock = pts_get_clock();
-#ifndef _POSIX_MONOTONIC_CLOCK
- printf("CLOCK_MONOTONIC unavailable, test may fail due to external clock adjustments\n");
-#endif
-
- if (clock_gettime(TEST_CLOCK, &tsbefore) == -1) {
+ if (clock_gettime(test_clock, &tsbefore) == -1) {
perror("Error in clock_gettime()\n");
return PTS_UNRESOLVED;
}
@@ -40,7 +32,7 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED)
return PTS_UNRESOLVED;
}
- if (clock_gettime(TEST_CLOCK, &tsafter) == -1) {
+ if (clock_gettime(test_clock, &tsafter) == -1) {
perror("Error in clock_gettime()\n");
return PTS_UNRESOLVED;
}
diff --git a/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/1-2.c b/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/1-2.c
index 44697324aae218a0b67c32c69e01e01151478ff2..0d608d5127ea0dea5e64ab2b801bc1a5f05965f1 100644
--- a/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/1-2.c
+++ b/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/1-2.c
@@ -14,24 +14,16 @@
#include <unistd.h>
#include <sys/wait.h>
#include "posixtest.h"
-
-#ifdef _POSIX_MONOTONIC_CLOCK
-#define TEST_CLOCK CLOCK_MONOTONIC
-#else
-#define TEST_CLOCK CLOCK_REALTIME
-#endif
+#include "clock.h"
int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED)
{
struct timespec tssleepfor, tsstorage, tsbefore, tsafter;
int sleepsec = 30;
int pid;
+ clockid_t test_clock = pts_get_clock();
-#ifndef _POSIX_MONOTONIC_CLOCK
- printf("CLOCK_MONOTONIC unavailable, test may fail due to external clock adjustments\n");
-#endif
-
- if (clock_gettime(TEST_CLOCK, &tsbefore) == -1) {
+ if (clock_gettime(test_clock, &tsbefore) == -1) {
perror("Error in clock_gettime()\n");
return PTS_UNRESOLVED;
}
@@ -56,7 +48,7 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED)
perror("Error waiting for child to exit\n");
return PTS_UNRESOLVED;
}
- if (clock_gettime(TEST_CLOCK, &tsafter) == -1) {
+ if (clock_gettime(test_clock, &tsafter) == -1) {
perror("Error in clock_gettime()\n");
return PTS_UNRESOLVED;
}
diff --git a/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/1-3.c b/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/1-3.c
index 71a289552227086d5869c4dd8fbe2a5e0697fb61..a7ee7e02616bb412837700c60148fa89756ec48a 100644
--- a/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/1-3.c
+++ b/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/1-3.c
@@ -15,12 +15,7 @@
#include <unistd.h>
#include <sys/wait.h>
#include "posixtest.h"
-
-#ifdef _POSIX_MONOTONIC_CLOCK
-#define TEST_CLOCK CLOCK_MONOTONIC
-#else
-#define TEST_CLOCK CLOCK_REALTIME
-#endif
+#include "clock.h"
static void handler(int signo PTS_ATTRIBUTE_UNUSED)
{
@@ -33,12 +28,9 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED)
int sleepsec = 30;
int pid;
struct sigaction act;
+ clockid_t test_clock = pts_get_clock();
-#ifndef _POSIX_MONOTONIC_CLOCK
- printf("CLOCK_MONOTONIC unavailable, test may fail due to external clock adjustments\n");
-#endif
-
- if (clock_gettime(TEST_CLOCK, &tsbefore) == -1) {
+ if (clock_gettime(test_clock, &tsbefore) == -1) {
perror("Error in clock_gettime()\n");
return PTS_UNRESOLVED;
}
@@ -74,7 +66,7 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED)
perror("Error waiting for child to exit\n");
return PTS_UNRESOLVED;
}
- if (clock_gettime(TEST_CLOCK, &tsafter) == -1) {
+ if (clock_gettime(test_clock, &tsafter) == -1) {
perror("Error in clock_gettime()\n");
return PTS_UNRESOLVED;
}
diff --git a/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/10000-1.c b/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/10000-1.c
index e0575029fadd1828fd083ec43b0b96577cd85342..36cf5b90948fb59111f457d2a048f7b47d5df320 100644
--- a/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/10000-1.c
+++ b/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/10000-1.c
@@ -18,12 +18,7 @@
#include <time.h>
#include <errno.h>
#include "posixtest.h"
-
-#ifdef _POSIX_MONOTONIC_CLOCK
-#define TEST_CLOCK CLOCK_MONOTONIC
-#else
-#define TEST_CLOCK CLOCK_REALTIME
-#endif
+#include "clock.h"
#define NUMVALID 6
#define NUMINVALID 7
@@ -56,23 +51,20 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED)
int i;
int failure = 0;
int slepts = 0, sleptns = 0;
-
-#ifndef _POSIX_MONOTONIC_CLOCK
- printf("CLOCK_MONOTONIC unavailable, test may fail due to external clock adjustments\n");
-#endif
+ clockid_t test_clock = pts_get_clock();
for (i = 0; i < NUMVALID; i++) {
tssleepfor.tv_sec = sleepvalid[i][0];
tssleepfor.tv_nsec = sleepvalid[i][1];
printf("sleep %d sec %d nsec\n",
(int)tssleepfor.tv_sec, (int)tssleepfor.tv_nsec);
- if (clock_gettime(TEST_CLOCK, &tsbefore) == -1) {
+ if (clock_gettime(test_clock, &tsbefore) == -1) {
perror("Error in clock_gettime()\n");
return PTS_UNRESOLVED;
}
if (nanosleep(&tssleepfor, &tsstorage) == 0) {
- if (clock_gettime(TEST_CLOCK, &tsafter) == -1) {
+ if (clock_gettime(test_clock, &tsafter) == -1) {
perror("Error in clock_gettime()\n");
return PTS_UNRESOLVED;
}
diff --git a/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/2-1.c b/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/2-1.c
index d3eb514b5e7ef2382b1a93909f884cee0acb6b0b..82cf275066aa7b0c354b5633b41c8983873250e9 100644
--- a/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/2-1.c
+++ b/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/2-1.c
@@ -12,12 +12,7 @@
#include <stdio.h>
#include <time.h>
#include "posixtest.h"
-
-#ifdef _POSIX_MONOTONIC_CLOCK
-#define TEST_CLOCK CLOCK_MONOTONIC
-#else
-#define TEST_CLOCK CLOCK_REALTIME
-#endif
+#include "clock.h"
#define NUMINTERVALS 13
int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED)
@@ -30,12 +25,9 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED)
int i;
int failure = 0;
int slepts, sleptns;
+ clockid_t test_clock = pts_get_clock();
-#ifndef _POSIX_MONOTONIC_CLOCK
- printf("CLOCK_MONOTONIC unavailable, test may fail due to external clock adjustments\n");
-#endif
-
- if (clock_gettime(TEST_CLOCK, &tsbefore) == -1) {
+ if (clock_gettime(test_clock, &tsbefore) == -1) {
perror("Error in clock_gettime()\n");
return PTS_UNRESOLVED;
}
@@ -48,7 +40,7 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED)
return PTS_UNRESOLVED;
}
- if (clock_gettime(TEST_CLOCK, &tsafter) == -1) {
+ if (clock_gettime(test_clock, &tsafter) == -1) {
perror("Error in clock_gettime()\n");
return PTS_UNRESOLVED;
}
diff --git a/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/3-2.c b/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/3-2.c
index 15fb7cd7cbece7ed352a74e56fde2161bbde060d..8f366d9dd16998d899291cf220296dfb6cdfb9a4 100644
--- a/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/3-2.c
+++ b/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/3-2.c
@@ -15,12 +15,7 @@
#include <sys/wait.h>
#include <stdlib.h>
#include "posixtest.h"
-
-#ifdef _POSIX_MONOTONIC_CLOCK
-#define TEST_CLOCK CLOCK_MONOTONIC
-#else
-#define TEST_CLOCK CLOCK_REALTIME
-#endif
+#include "clock.h"
#define SLEEPSEC 5
@@ -31,12 +26,9 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED)
{
int pid, slepts;
struct timespec tsbefore, tsafter;
+ clockid_t test_clock = pts_get_clock();
-#ifndef _POSIX_MONOTONIC_CLOCK
- printf("CLOCK_MONOTONIC unavailable, test may fail due to external clock adjustments\n");
-#endif
-
- if (clock_gettime(TEST_CLOCK, &tsbefore) != 0) {
+ if (clock_gettime(test_clock, &tsbefore) != 0) {
perror("clock_gettime() did not return success\n");
return PTS_UNRESOLVED;
}
@@ -84,7 +76,7 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED)
return PTS_FAIL;
}
- if (clock_gettime(TEST_CLOCK, &tsafter) == -1) {
+ if (clock_gettime(test_clock, &tsafter) == -1) {
perror("Error in clock_gettime()\n");
return PTS_UNRESOLVED;
}
diff --git a/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/7-2.c b/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/7-2.c
index 58698b459defc9746b5acbdd6f9ca366a3668ccb..59ca2d2583f6ff4aca16451e7895b431a5d0d664 100644
--- a/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/7-2.c
+++ b/testcases/open_posix_testsuite/conformance/interfaces/nanosleep/7-2.c
@@ -16,12 +16,7 @@
#include <sys/wait.h>
#include <stdlib.h>
#include "posixtest.h"
-
-#ifdef _POSIX_MONOTONIC_CLOCK
-#define TEST_CLOCK CLOCK_MONOTONIC
-#else
-#define TEST_CLOCK CLOCK_REALTIME
-#endif
+#include "clock.h"
#define CHILDSUCCESS 1
#define CHILDFAILURE 0
@@ -39,12 +34,9 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED)
int sleepsec = 30;
int pid;
struct sigaction act;
+ clockid_t test_clock = pts_get_clock();
-#ifndef _POSIX_MONOTONIC_CLOCK
- printf("CLOCK_MONOTONIC unavailable, test may fail due to external clock adjustments\n");
-#endif
-
- if (clock_gettime(TEST_CLOCK, &tsbefore) == -1) {
+ if (clock_gettime(test_clock, &tsbefore) == -1) {
perror("Error in clock_gettime()\n");
return PTS_UNRESOLVED;
}
@@ -70,7 +62,7 @@ int test_main(int argc PTS_ATTRIBUTE_UNUSED, char **argv PTS_ATTRIBUTE_UNUSED)
return CHILDFAILURE;
}
- if (clock_gettime(TEST_CLOCK, &tsafter) == -1) {
+ if (clock_gettime(test_clock, &tsafter) == -1) {
perror("Error in clock_gettime()\n");
return CHILDFAILURE;
}
diff --git a/testcases/open_posix_testsuite/include/clock.h b/testcases/open_posix_testsuite/include/clock.h
index 11c7e949fa4b9b9ba1473b6bd4b54ba70c5ff63c..0073db2c73e575ee537375e7cb571f5a98e14448 100644
--- a/testcases/open_posix_testsuite/include/clock.h
+++ b/testcases/open_posix_testsuite/include/clock.h
@@ -23,6 +23,15 @@ static inline int pts_mono_available(void)
return 0;
}
+static inline clockid_t pts_get_clock(void)
+{
+ if (pts_mono_available())
+ return CLOCK_MONOTONIC;
+
+ printf("CLOCK_MONOTONIC unavailable, test may fail due to clock adjustment\n");
+ return CLOCK_REALTIME;
+}
+
#define PTS_MONO_MAX_RETRIES 3
static struct timespec pts_mono_start;
---
base-commit: 1ad07fa36483b27c781cad63f346e91986b3ac15
change-id: 20260504-fix_nanosleep_pts_get_clock-e6bd186b5f40
Best regards,
--
Andrea Cervesato <andrea.cervesato@suse.com>
More information about the ltp
mailing list