Skip to contentSkip to conversation

clock_settime: use POSIX runtime detection for CLOCK_MONOTONIC

PATCH Applied Andrea Cervesato 3 participants 5 messages May 4

lore
Share & tools
lore
#0 lore raw

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.

Fixes 9ecb4a004b18 ("clock_settime: Detect external clock adjustments via CLOCK_MONOTONIC")

---
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
index 631aad7debb242df88bd3493319a82add7e9f0e9..ebd609963a3651212447ff2ab0885e20b19ba302 100644
@@ -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
index ff8660f18e73df472463ebd7316a436402b1b6ba..06023036cf4c704c43ec64cfaed75157973f3524 100644
@@ -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
index de1a8e422f86e2cd71e8237fe77d5083f541b599..bb4e86c2fef77e75f9216c7fe59930d71e5715a0 100644
@@ -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
index 7009b38b42a730a3526724c14843f1656a16d30a..0ed40fe612506d5a571003d4d9b592a3faab608c 100644
@@ -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
index 6713369be4c31c4076a493a224a85f24b5120a83..6162152e574e51cfce095b518314132d331ee68f 100644
@@ -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
#1 lore raw

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-7670708ed94f

Best 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
#2 lore raw
Quoted 3 lines

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).

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
Reviewed-by
#3 lore raw

Hi!

Quoted 4 lines
Quoted 3 lines

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).

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