From: Cyril Hrubis <chrubis@suse.cz>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH v2 6/9] syscalls/clock_nanosleep03: Add test for time namespace
Date: Wed, 18 Mar 2020 16:37:58 +0100 [thread overview]
Message-ID: <20200318153801.3529-7-chrubis@suse.cz> (raw)
In-Reply-To: <20200318153801.3529-1-chrubis@suse.cz>
Test that absolute timeout for clock nanosleep is adjusted by the offset
correctly inside of a time namespace.
Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
---
runtest/containers | 1 +
runtest/syscalls | 1 +
.../syscalls/clock_nanosleep/.gitignore | 1 +
.../clock_nanosleep/clock_nanosleep03.c | 71 +++++++++++++++++++
4 files changed, 74 insertions(+)
create mode 100644 testcases/kernel/syscalls/clock_nanosleep/clock_nanosleep03.c
diff --git a/runtest/containers b/runtest/containers
index 4dc05af93..8100cd2bc 100644
--- a/runtest/containers
+++ b/runtest/containers
@@ -88,3 +88,4 @@ userns07 userns07
# time namespaces
sysinfo03 sysinfo03
+clock_nanosleep03 clock_nanosleep03
diff --git a/runtest/syscalls b/runtest/syscalls
index fb0b9e539..eac4eeffc 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -87,6 +87,7 @@ clock_getres01 clock_getres01
clock_nanosleep01 clock_nanosleep01
clock_nanosleep02 clock_nanosleep02
clock_nanosleep2_01 clock_nanosleep2_01
+clock_nanosleep03 clock_nanosleep03
clock_gettime01 clock_gettime01
clock_gettime02 clock_gettime02
diff --git a/testcases/kernel/syscalls/clock_nanosleep/.gitignore b/testcases/kernel/syscalls/clock_nanosleep/.gitignore
index 6714ff468..406897cde 100644
--- a/testcases/kernel/syscalls/clock_nanosleep/.gitignore
+++ b/testcases/kernel/syscalls/clock_nanosleep/.gitignore
@@ -1,2 +1,3 @@
/clock_nanosleep01
/clock_nanosleep02
+/clock_nanosleep03
diff --git a/testcases/kernel/syscalls/clock_nanosleep/clock_nanosleep03.c b/testcases/kernel/syscalls/clock_nanosleep/clock_nanosleep03.c
new file mode 100644
index 000000000..99ff99736
--- /dev/null
+++ b/testcases/kernel/syscalls/clock_nanosleep/clock_nanosleep03.c
@@ -0,0 +1,71 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+
+ Copyright (c) 2020 Cyril Hrubis <chrubis@suse.cz>
+
+ */
+/*
+
+ Test that clock_nanosleep() adds correctly an offset with absolute timeout
+ and CLOCK_MONOTONIC inside of a timer namespace.
+
+ After a call to unshare(CLONE_NEWTIME) a new timer namespace is created, the
+ process that has called the unshare() can adjust offsets for CLOCK_MONOTONIC
+ and CLOCK_BOOTTIME for its children by writing to the '/proc/self/timens_offsets'.
+
+ */
+
+#include <stdlib.h>
+#include "tst_safe_clocks.h"
+#include "tst_timer.h"
+#include "lapi/namespaces_constants.h"
+#include "tst_test.h"
+
+#define OFFSET_S 10
+#define SLEEP_US 100000
+
+static void verify_clock_nanosleep(void)
+{
+ struct timespec start, end, sleep_abs;
+
+ SAFE_UNSHARE(CLONE_NEWTIME);
+
+ SAFE_FILE_PRINTF("/proc/self/timens_offsets", "%d %d 0", CLOCK_MONOTONIC, OFFSET_S);
+
+ SAFE_CLOCK_GETTIME(CLOCK_MONOTONIC, &start);
+
+ sleep_abs = tst_timespec_add_us(start, 1000000 * OFFSET_S + SLEEP_US);
+
+ if (!SAFE_FORK()) {
+ clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &sleep_abs, NULL);
+ exit(0);
+ }
+
+ SAFE_WAIT(NULL);
+
+ SAFE_CLOCK_GETTIME(CLOCK_MONOTONIC, &end);
+
+ long long diff = tst_timespec_diff_us(end, start);
+
+ if (diff > 5 * SLEEP_US) {
+ tst_res(TFAIL, "clock_nanosleep() slept too long %lli", diff);
+ return;
+ }
+
+ if (diff < SLEEP_US) {
+ tst_res(TFAIL, "clock_nanosleep() slept too short %lli", diff);
+ return;
+ }
+
+ tst_res(TPASS, "clock_nanosleep() slept correctly %lli", diff);
+}
+
+static struct tst_test test = {
+ .test_all = verify_clock_nanosleep,
+ .needs_root = 1,
+ .forks_child = 1,
+ .needs_kconfigs = (const char *[]) {
+ "CONFIG_TIME_NS=y"
+ }
+
+};
--
2.24.1
next prev parent reply other threads:[~2020-03-18 15:37 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-03-18 15:37 [LTP] [PATCH v2 0/9] Add basic time namespace testcases Cyril Hrubis
2020-03-18 15:37 ` [LTP] [PATCH v2 1/9] lapi/namespace_constants.h: Add CLONE_NEWTIME Cyril Hrubis
2020-03-18 15:37 ` [LTP] [PATCH v2 2/9] lapi: Add a configure check and fallback for setns Cyril Hrubis
2020-03-18 15:37 ` [LTP] [PATCH v2 3/9] safe_macros: Add SAFE_SETNS() Cyril Hrubis
2020-03-23 16:11 ` Petr Vorel
2020-03-30 14:09 ` Cyril Hrubis
2020-03-18 15:37 ` [LTP] [PATCH v2 4/9] include/tst_timer: Fix normalization Cyril Hrubis
2020-03-18 15:37 ` [LTP] [PATCH v2 5/9] syscalls/sysinfo03: Add time namespace test Cyril Hrubis
2020-03-19 7:55 ` Li Wang
2020-03-19 22:00 ` Cyril Hrubis
2020-03-20 7:32 ` Li Wang
2020-03-23 18:47 ` Petr Vorel
2020-03-18 15:37 ` Cyril Hrubis [this message]
2020-03-18 15:37 ` [LTP] [PATCH v2 7/9] syscalls/clock_gettime03: Add basic " Cyril Hrubis
2020-03-18 15:38 ` [LTP] [PATCH v2 8/9] containers/timens: Add basic error test Cyril Hrubis
2020-03-18 15:38 ` [LTP] [PATCH v2 9/9] syscalls/timerfd04: Add time namespace test Cyril Hrubis
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200318153801.3529-7-chrubis@suse.cz \
--to=chrubis@suse.cz \
--cc=ltp@lists.linux.it \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox