clock_settime: use POSIX runtime detection for CLOCK_MONOTONIC
PATCH v4 1/1 clock_settime: use POSIX runtime detection for CLOCK_MONOTONIC
From: Andrea Cervesato <[email protected]>
The clock_settime helpers.h used a compile-time #ifdef on _POSIX_MONOTONIC_CLOCK to select between real and stub implementations of pts_mono_time_start()/pts_mono_time_check().
On Linux/glibc, _POSIX_MONOTONIC_CLOCK is defined as 0, meaning support is optional and must be verified at runtime via sysconf(_SC_MONOTONIC_CLOCK). The #ifdef treated 0 the same as
Quoted 1 line
0 (always available), which is not POSIX-correct.
Replace the compile-time #ifdef/#else split with a runtime pts_mono_available() helper that performs proper POSIX detection via sysconf(). Extract pts_mono_available() into the shared include/clock.h so it can be reused by nanosleep helpers as well. Also add a missing #ifndef include guard and #include <unistd.h> so _POSIX_MONOTONIC_CLOCK is always defined.
---
Changes in v4:
- verify that CLOCK_MONOTONIC exists vs _POSIX_MONOTONIC_CLOCK
- Link to v3: https://lore.kernel.org/r/[email protected]
--- .../conformance/interfaces/clock_settime/4-1.c | 1 + .../conformance/interfaces/clock_settime/5-1.c | 1 + .../conformance/interfaces/clock_settime/5-2.c | 1 + .../conformance/interfaces/clock_settime/7-1.c | 1 + .../conformance/interfaces/clock_settime/7-2.c | 1 + .../conformance/interfaces/clock_settime/helpers.h | 56 +--------------- testcases/open_posix_testsuite/include/clock.h | 77 ++++++++++++++++++++++ 7 files changed, 85 insertions(+), 53 deletions(-)
testcases/open_posix_testsuite/conformance/interfaces/clock_settime/4-1.c +1 -0
@@ -28,6 +28,7 @@#include <signal.h> #include <unistd.h> #include "posixtest.h" +#include "clock.h" #include "helpers.h" // SLEEPTIME < TIMEROFFSET
testcases/open_posix_testsuite/conformance/interfaces/clock_settime/5-1.c +1 -0
@@ -23,6 +23,7 @@#include <unistd.h> #include <stdlib.h> #include "posixtest.h" +#include "clock.h" #include "helpers.h" #define TIMERSEC 5
testcases/open_posix_testsuite/conformance/interfaces/clock_settime/5-2.c +1 -0
@@ -23,6 +23,7 @@#include <unistd.h> #include <stdlib.h> #include "posixtest.h" +#include "clock.h" #include "helpers.h" #define TIMERSEC 5
testcases/open_posix_testsuite/conformance/interfaces/clock_settime/7-1.c +1 -0
@@ -24,6 +24,7 @@#include <unistd.h> #include <sys/wait.h> #include "posixtest.h" +#include "clock.h" #include "helpers.h" #define SLEEPOFFSET 5
testcases/open_posix_testsuite/conformance/interfaces/clock_settime/7-2.c +1 -0
@@ -23,6 +23,7 @@#include <unistd.h> #include <sys/wait.h> #include "posixtest.h" +#include "clock.h" #include "helpers.h" #define SLEEPOFFSET 5
testcases/open_posix_testsuite/conformance/interfaces/clock_settime/helpers.h +3 -53
65 lines not on this page · raw message
testcases/open_posix_testsuite/include/clock.h new file +77 -0
77 lines not on this page · raw message
---
base-commit: 73cfad509db014e5a631bd2298f4e9d8ea3ee1c1
change-id: 20260429-fix_clock_settime_helper-7670708ed94f
Best regards,
Signature · 5 lines
Andrea Cervesato <[email protected]> -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
testcases/open_posix_testsuite/include/clock.h line 16
Quoted 8 lines
testcases/open_posix_testsuite/include/clock.h new file +8
-0+#include <stdio.h> +#include <stdlib.h> +#include <time.h> +#include <unistd.h> + +static inline int pts_mono_available (void ) +{ +#ifdef CLOCK_MONOTONIC
This one should have stayed _POSIX_MONOTONIC_CLOCK since we are doing a presence checks and the -1 is correctly handled here (we get to return 0 case in this case).
Quoted 21 lines
testcases/open_posix_testsuite/include/clock.h new file +21
-0+if (_POSIX_MONOTONIC_CLOCK >0 ) +return 1 ; + +if (!_POSIX_MONOTONIC_CLOCK &&sysconf (_SC_MONOTONIC_CLOCK) >0 ) +return 1 ; +#endif +return 0 ; +} + +#define PTS_MONO_MAX_RETRIES 3 + +static struct timespec pts_mono_start; + +static inline int pts_mono_time_start (void ) +{ +if (!pts_mono_available ()) { +static int warned; + +if (!warned) { +printf ("CLOCK_MONOTONIC unavailable, test may fail due to clock adjustment\n" ); + warned =1 ;
testcases/open_posix_testsuite/include/clock.h line 47
Quoted 10 lines
testcases/open_posix_testsuite/include/clock.h new file +10
-0+ } +return 0 ; + } + +#ifdef CLOCK_MONOTONIC +if (clock_gettime (CLOCK_MONOTONIC , &pts_mono_start) !=0 ) { +perror ("clock_gettime(CLOCK_MONOTONIC) failed" ); +return -1 ; + } +#endif
These are guarding againts compilation failures and looks fine to me now.
Quoted 42 lines
testcases/open_posix_testsuite/include/clock.h new file +30
-1+ return 0; +} + +static inline int pts_mono_time_check (unsigned int expected_secs) +{ +#ifdef CLOCK_MONOTONIC +if (pts_mono_available ()) { +struct timespec now; +long elapsed; + +if (clock_gettime (CLOCK_MONOTONIC , &now) !=0 ) { +perror ("clock_gettime(CLOCK_MONOTONIC) failed" ); +return -1 ; + } + + elapsed = now.tv_sec - pts_mono_start.tv_sec; + +if (labs (elapsed - (long )expected_secs) >1 ) { +printf ("Clock adjustment detected (elapsed %lds, expected ~%us)\n" , + elapsed, expected_secs); +return 1 ; + } +return 0 ; + } +#endif + (void )expected_secs; +return 0 ; +} + +#endif /* PTS_CLOCK_H */ ---base-commit:
73cfad509db014e5a631bd2298f4e9d8ea3ee1c1
change-id: 20260429-fix_clock_settime_helper-7670708ed94fBest regards,
Signature · 5 lines
Andrea Cervesato <[email protected]> -- Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 3 lines
This one should have stayed
_POSIX_MONOTONIC_CLOCKsince we are doing a presence checks and the -1 is correctly handled here (we get to return 0 case in this case).
If it's ok, i can merge with this change
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Cyril Hrubis
Reviewed-byHi!
Quoted 4 lines
Quoted 3 lines
This one should have stayed
_POSIX_MONOTONIC_CLOCKsince we are doing a presence checks and the -1 is correctly handled here (we get to return 0 case in this case).If it's ok, i can merge with this change
Yes. With this change:
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Andrea Cervesato via ltp
AppliedMerged, Thanks!
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp