* [LTP] [PATCH v3] clock_settime: use POSIX runtime detection for CLOCK_MONOTONIC
@ 2026-04-29 8:28 Andrea Cervesato
2026-04-29 10:12 ` [LTP] " linuxtestproject.agent
2026-05-04 9:07 ` [LTP] [PATCH v3] " Cyril Hrubis
0 siblings, 2 replies; 4+ messages in thread
From: Andrea Cervesato @ 2026-04-29 8:28 UTC (permalink / raw)
To: Linux Test Project
From: Andrea Cervesato <andrea.cervesato@suse.com>
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
>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")
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
.../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(-)
diff --git a/testcases/open_posix_testsuite/conformance/interfaces/clock_settime/4-1.c b/testcases/open_posix_testsuite/conformance/interfaces/clock_settime/4-1.c
index 631aad7debb242df88bd3493319a82add7e9f0e9..ebd609963a3651212447ff2ab0885e20b19ba302 100644
--- a/testcases/open_posix_testsuite/conformance/interfaces/clock_settime/4-1.c
+++ b/testcases/open_posix_testsuite/conformance/interfaces/clock_settime/4-1.c
@@ -28,6 +28,7 @@
#include <signal.h>
#include <unistd.h>
#include "posixtest.h"
+#include "clock.h"
#include "helpers.h"
// SLEEPTIME < TIMEROFFSET
diff --git a/testcases/open_posix_testsuite/conformance/interfaces/clock_settime/5-1.c b/testcases/open_posix_testsuite/conformance/interfaces/clock_settime/5-1.c
index ff8660f18e73df472463ebd7316a436402b1b6ba..06023036cf4c704c43ec64cfaed75157973f3524 100644
--- a/testcases/open_posix_testsuite/conformance/interfaces/clock_settime/5-1.c
+++ b/testcases/open_posix_testsuite/conformance/interfaces/clock_settime/5-1.c
@@ -23,6 +23,7 @@
#include <unistd.h>
#include <stdlib.h>
#include "posixtest.h"
+#include "clock.h"
#include "helpers.h"
#define TIMERSEC 5
diff --git a/testcases/open_posix_testsuite/conformance/interfaces/clock_settime/5-2.c b/testcases/open_posix_testsuite/conformance/interfaces/clock_settime/5-2.c
index de1a8e422f86e2cd71e8237fe77d5083f541b599..bb4e86c2fef77e75f9216c7fe59930d71e5715a0 100644
--- a/testcases/open_posix_testsuite/conformance/interfaces/clock_settime/5-2.c
+++ b/testcases/open_posix_testsuite/conformance/interfaces/clock_settime/5-2.c
@@ -23,6 +23,7 @@
#include <unistd.h>
#include <stdlib.h>
#include "posixtest.h"
+#include "clock.h"
#include "helpers.h"
#define TIMERSEC 5
diff --git a/testcases/open_posix_testsuite/conformance/interfaces/clock_settime/7-1.c b/testcases/open_posix_testsuite/conformance/interfaces/clock_settime/7-1.c
index 7009b38b42a730a3526724c14843f1656a16d30a..0ed40fe612506d5a571003d4d9b592a3faab608c 100644
--- a/testcases/open_posix_testsuite/conformance/interfaces/clock_settime/7-1.c
+++ b/testcases/open_posix_testsuite/conformance/interfaces/clock_settime/7-1.c
@@ -24,6 +24,7 @@
#include <unistd.h>
#include <sys/wait.h>
#include "posixtest.h"
+#include "clock.h"
#include "helpers.h"
#define SLEEPOFFSET 5
diff --git a/testcases/open_posix_testsuite/conformance/interfaces/clock_settime/7-2.c b/testcases/open_posix_testsuite/conformance/interfaces/clock_settime/7-2.c
index 6713369be4c31c4076a493a224a85f24b5120a83..6162152e574e51cfce095b518314132d331ee68f 100644
--- a/testcases/open_posix_testsuite/conformance/interfaces/clock_settime/7-2.c
+++ b/testcases/open_posix_testsuite/conformance/interfaces/clock_settime/7-2.c
@@ -23,6 +23,7 @@
#include <unistd.h>
#include <sys/wait.h>
#include "posixtest.h"
+#include "clock.h"
#include "helpers.h"
#define SLEEPOFFSET 5
diff --git a/testcases/open_posix_testsuite/conformance/interfaces/clock_settime/helpers.h b/testcases/open_posix_testsuite/conformance/interfaces/clock_settime/helpers.h
index 37bf30926f039553e9e370751b7938ca5ea1d00a..c559c189c163d7f7e1fb995b0806933504b278ab 100644
--- a/testcases/open_posix_testsuite/conformance/interfaces/clock_settime/helpers.h
+++ b/testcases/open_posix_testsuite/conformance/interfaces/clock_settime/helpers.h
@@ -13,7 +13,8 @@
* by those tests.
*/
-#include <stdlib.h>
+#ifndef CLOCK_SETTIME_HELPERS_H
+#define CLOCK_SETTIME_HELPERS_H
static int getBeforeTime(struct timespec *tpget)
{
@@ -35,55 +36,4 @@ static int setBackTime(struct timespec tpset)
return PTS_PASS;
}
-#define PTS_MONO_MAX_RETRIES 3
-
-#ifdef _POSIX_MONOTONIC_CLOCK
-static struct timespec _pts_mono_start;
-
-static inline int pts_mono_time_start(void)
-{
- if (clock_gettime(CLOCK_MONOTONIC, &_pts_mono_start) != 0) {
- perror("clock_gettime(CLOCK_MONOTONIC) failed");
- return -1;
- }
- return 0;
-}
-
-static inline int pts_mono_time_check(unsigned int expected_secs)
-{
- 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;
-}
-#else
-static inline int pts_mono_time_start(void)
-{
- static int warned;
-
- if (!warned) {
- printf("CLOCK_MONOTONIC unavailable, test may fail due to clock adjustment\n");
- warned = 1;
- }
- return 0;
-}
-
-static inline int pts_mono_time_check(unsigned int expected_secs)
-{
- (void)expected_secs;
- return 0;
-}
-#endif
-
+#endif /* CLOCK_SETTIME_HELPERS_H */
diff --git a/testcases/open_posix_testsuite/include/clock.h b/testcases/open_posix_testsuite/include/clock.h
new file mode 100644
index 0000000000000000000000000000000000000000..221dd38ff34b627aa5f6c07825cd7439cf559c74
--- /dev/null
+++ b/testcases/open_posix_testsuite/include/clock.h
@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) 2026, Linux Test Project
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef PTS_CLOCK_H
+#define PTS_CLOCK_H
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+
+static inline int pts_mono_available(void)
+{
+#ifdef _POSIX_MONOTONIC_CLOCK
+ 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;
+ }
+ return 0;
+ }
+
+#ifdef _POSIX_MONOTONIC_CLOCK
+ if (clock_gettime(CLOCK_MONOTONIC, &pts_mono_start) != 0) {
+ perror("clock_gettime(CLOCK_MONOTONIC) failed");
+ return -1;
+ }
+#endif
+ return 0;
+}
+
+static inline int pts_mono_time_check(unsigned int expected_secs)
+{
+#ifdef _POSIX_MONOTONIC_CLOCK
+ 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: 1d2ea5b042a6c6c169689efebd856583200c564f
change-id: 20260429-fix_clock_settime_helper-7670708ed94f
Best regards,
--
Andrea Cervesato <andrea.cervesato@suse.com>
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [LTP] clock_settime: use POSIX runtime detection for CLOCK_MONOTONIC
2026-04-29 8:28 [LTP] [PATCH v3] clock_settime: use POSIX runtime detection for CLOCK_MONOTONIC Andrea Cervesato
@ 2026-04-29 10:12 ` linuxtestproject.agent
2026-05-04 9:07 ` [LTP] [PATCH v3] " Cyril Hrubis
1 sibling, 0 replies; 4+ messages in thread
From: linuxtestproject.agent @ 2026-04-29 10:12 UTC (permalink / raw)
To: Andrea Cervesato; +Cc: ltp
Hi Andrea,
On Wed, 29 Apr 2026 10:28:35 +0200, Andrea Cervesato wrote:
> clock_settime: use POSIX runtime detection for CLOCK_MONOTONIC
> +/*
> + * Copyright (c) 2026, Linux Test Project
> + * SPDX-License-Identifier: GPL-2.0-or-later
> + */
Missing `Created by:` attribution line. Other headers in this directory
(posixtest.h, mq_send.h) carry it. Add:
* Created by: Andrea Cervesato <andrea.cervesato@suse.com>
[...]
Pre-existing issues noticed in the surrounding code (not introduced
by this patch):
- clock_settime/4-1.c (and siblings) — uses `test_main` instead of `int main`
- clock_settime/helpers.h — `getBeforeTime()`/`setBackTime()` call perror()
without including <stdio.h> in the header itself
---
Note:
Our agent completed the review of the patch. The full review can be
found at: https://github.com/linux-test-project/ltp-agent/actions/runs/25102778503
The agent can sometimes produce false positives although often its
findings are genuine. If you find issues with the review, please
comment this email or ignore the suggestions.
Regards,
LTP AI Reviewer
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [LTP] [PATCH v3] clock_settime: use POSIX runtime detection for CLOCK_MONOTONIC
2026-04-29 8:28 [LTP] [PATCH v3] clock_settime: use POSIX runtime detection for CLOCK_MONOTONIC Andrea Cervesato
2026-04-29 10:12 ` [LTP] " linuxtestproject.agent
@ 2026-05-04 9:07 ` Cyril Hrubis
2026-05-04 9:26 ` Andrea Cervesato via ltp
1 sibling, 1 reply; 4+ messages in thread
From: Cyril Hrubis @ 2026-05-04 9:07 UTC (permalink / raw)
To: Andrea Cervesato; +Cc: Linux Test Project
Hi!
> diff --git a/testcases/open_posix_testsuite/include/clock.h b/testcases/open_posix_testsuite/include/clock.h
> new file mode 100644
> index 0000000000000000000000000000000000000000..221dd38ff34b627aa5f6c07825cd7439cf559c74
> --- /dev/null
> +++ b/testcases/open_posix_testsuite/include/clock.h
> @@ -0,0 +1,77 @@
> +/*
> + * Copyright (c) 2026, Linux Test Project
> + * SPDX-License-Identifier: GPL-2.0-or-later
> + */
> +
> +#ifndef PTS_CLOCK_H
> +#define PTS_CLOCK_H
> +
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <time.h>
> +#include <unistd.h>
> +
> +static inline int pts_mono_available(void)
> +{
> +#ifdef _POSIX_MONOTONIC_CLOCK
> + 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;
> + }
> + return 0;
> + }
> +
> +#ifdef _POSIX_MONOTONIC_CLOCK
I suppose that this not correct either. If _POSIX_MONOTONIC_CLOCK is set
to -1 the CLOCK_MONOTONIC constant may not be defined.
So we either have to to do
#if _POSIX_MONOTONIC_CLOCK >= 0
or
#ifdef CLOCK_MONOTONIC
> + if (clock_gettime(CLOCK_MONOTONIC, &pts_mono_start) != 0) {
> + perror("clock_gettime(CLOCK_MONOTONIC) failed");
> + return -1;
> + }
> +#endif
> + return 0;
> +}
> +
> +static inline int pts_mono_time_check(unsigned int expected_secs)
> +{
> +#ifdef _POSIX_MONOTONIC_CLOCK
> + 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: 1d2ea5b042a6c6c169689efebd856583200c564f
> change-id: 20260429-fix_clock_settime_helper-7670708ed94f
>
> Best regards,
> --
> Andrea Cervesato <andrea.cervesato@suse.com>
>
>
> --
> Mailing list info: https://lists.linux.it/listinfo/ltp
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [LTP] [PATCH v3] clock_settime: use POSIX runtime detection for CLOCK_MONOTONIC
2026-05-04 9:07 ` [LTP] [PATCH v3] " Cyril Hrubis
@ 2026-05-04 9:26 ` Andrea Cervesato via ltp
0 siblings, 0 replies; 4+ messages in thread
From: Andrea Cervesato via ltp @ 2026-05-04 9:26 UTC (permalink / raw)
To: Cyril Hrubis; +Cc: Linux Test Project
Hi Cyril,
> > +#ifdef _POSIX_MONOTONIC_CLOCK
>
> I suppose that this not correct either. If _POSIX_MONOTONIC_CLOCK is set
> to -1 the CLOCK_MONOTONIC constant may not be defined.
>
> So we either have to to do
>
> #if _POSIX_MONOTONIC_CLOCK >= 0
>
> or
>
> #ifdef CLOCK_MONOTONIC
>
You are right, that's indeed a valid value. I double checked the
definition and some systems might set it to -1.
I will send a fix for it with the new version.
Regards,
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
andrea.cervesato@suse.com
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-05-04 9:26 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-29 8:28 [LTP] [PATCH v3] clock_settime: use POSIX runtime detection for CLOCK_MONOTONIC Andrea Cervesato
2026-04-29 10:12 ` [LTP] " linuxtestproject.agent
2026-05-04 9:07 ` [LTP] [PATCH v3] " Cyril Hrubis
2026-05-04 9:26 ` Andrea Cervesato via ltp
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox