<div dir="ltr"><div dir="ltr"><div class="gmail_default" style="font-size:small"><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Mar 18, 2020 at 11:35 PM Cyril Hrubis <<a href="mailto:chrubis@suse.cz" target="_blank">chrubis@suse.cz</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">This tests that the uptime in sysinfo() is adjusted correctly by the<br>
namespace offset.<br>
<br>
Also check that /proc/uptime is consistent with the uptime from the<br>
sysinfo() syscall.<br>
<br>
Signed-off-by: Cyril Hrubis <<a href="mailto:chrubis@suse.cz" target="_blank">chrubis@suse.cz</a>><br>
---<br>
 runtest/containers                            |  3 +<br>
 runtest/syscalls                              |  1 +<br>
 testcases/kernel/syscalls/sysinfo/.gitignore  |  1 +<br>
 testcases/kernel/syscalls/sysinfo/sysinfo03.c | 81 +++++++++++++++++++<br>
 4 files changed, 86 insertions(+)<br>
 create mode 100644 testcases/kernel/syscalls/sysinfo/sysinfo03.c<br>
<br>
diff --git a/runtest/containers b/runtest/containers<br>
index 871cd2a42..4dc05af93 100644<br>
--- a/runtest/containers<br>
+++ b/runtest/containers<br>
@@ -85,3 +85,6 @@ userns04 userns04<br>
 userns05 userns05<br>
 userns06 userns06<br>
 userns07 userns07<br>
+<br>
+# time namespaces<br>
+sysinfo03 sysinfo03<br>
diff --git a/runtest/syscalls b/runtest/syscalls<br>
index 6f2dcd82a..fb0b9e539 100644<br>
--- a/runtest/syscalls<br>
+++ b/runtest/syscalls<br>
@@ -1465,6 +1465,7 @@ sysfs06 sysfs06<br>
<br>
 sysinfo01 sysinfo01<br>
 sysinfo02 sysinfo02<br>
+sysinfo03 sysinfo03<br>
<br>
 syslog01 syslog01<br>
 syslog02 syslog02<br>
diff --git a/testcases/kernel/syscalls/sysinfo/.gitignore b/testcases/kernel/syscalls/sysinfo/.gitignore<br>
index aa7c26946..8ad2279a4 100644<br>
--- a/testcases/kernel/syscalls/sysinfo/.gitignore<br>
+++ b/testcases/kernel/syscalls/sysinfo/.gitignore<br>
@@ -1,2 +1,3 @@<br>
 /sysinfo01<br>
 /sysinfo02<br>
+/sysinfo03<br>
diff --git a/testcases/kernel/syscalls/sysinfo/sysinfo03.c b/testcases/kernel/syscalls/sysinfo/sysinfo03.c<br>
new file mode 100644<br>
index 000000000..af1024915<br>
--- /dev/null<br>
+++ b/testcases/kernel/syscalls/sysinfo/sysinfo03.c<br>
@@ -0,0 +1,81 @@<br>
+// SPDX-License-Identifier: GPL-2.0-or-later<br>
+/*<br>
+  Copyright (c) 2020 Cyril Hrubis <<a href="mailto:chrubis@suse.cz" target="_blank">chrubis@suse.cz</a>><br>
+ */<br>
+/*<br>
+<br>
+  Test if CLOCK_BOOTTIME namespace offset is applied to sysinfo uptime and that<br>
+  it's consistent with /proc/uptime as well.<br>
+<br>
+  After a call to unshare(CLONE_NEWTIME) a new timer namespace is created, the<br>
+  process that has called the unshare() can adjust offsets for CLOCK_MONOTONIC<br>
+  and CLOCK_BOOTTIME for its children by writing to the '/proc/self/timens_offsets'.<br>
+<br>
+ */<br>
+<br>
+#include <sys/sysinfo.h><br>
+#include "lapi/namespaces_constants.h"<br>
+#include "tst_test.h"<br>
+<br>
+static int offsets[] = {<br>
+       10,<br>
+       -10,<br>
+       3600,<br>
+};<br>
+<br>
+static long read_proc_uptime(void)<br>
+{<br>
+       long sec, sec_rem;<br>
+<br>
+       SAFE_FILE_SCANF("/proc/uptime", "%li.%li", &sec, &sec_rem);<br>
+<br>
+       return sec + (sec_rem ? 1 : 0);<br>
+}<br>
+<br>
+static void verify_sysinfo(unsigned int n)<br>
+{<br>
+       struct sysinfo si;<br>
+       long uptime;<br>
+       int off = offsets[n];<br>
+<br>
+       SAFE_UNSHARE(CLONE_NEWTIME);<br>
+<br>
+        SAFE_FILE_PRINTF("/proc/self/timens_offsets", "%d %d 0",<br>
+                        CLOCK_BOOTTIME, off);<br>
+<br>
+       sysinfo(&si);<br>
+<br>
+       uptime = si.uptime;<br>
+<br>
+       if (!SAFE_FORK()) {<br>
+               sysinfo(&si);<br>
+               long proc_uptime = read_proc_uptime();<br>
+<br>
+               long diff = si.uptime - uptime;<br>
+<br>
+               if (diff < off || diff > off + 1)<br>
+                       tst_res(TFAIL, "Wrong sysinfo uptime offset %li", diff);<br>
+               else<br>
+                       tst_res(TPASS, "Correct sysinfo uptime offset %i", off);<br>
+<br>
+               if (si.uptime < proc_uptime || si.uptime > proc_uptime + 1) {<br>
+                       tst_res(TFAIL, "/proc/uptime %li differs from sysinfo %li",<br>
+                               proc_uptime, si.uptime);<br>
+               } else {<br>
+                       tst_res(TPASS, "/proc/uptime is consistent with sysinfo");<br>
+               }<br>
+       }<br>
+}<br>
+<br>
+static struct tst_test test = {<br>
+       .tcnt = ARRAY_SIZE(offsets),<br>
+       .test = verify_sysinfo,<br>
+       .needs_root = 1,<br>
+       .forks_child = 1,<br>
+       .needs_kconfigs = (const char *[]) {<br>
+               "CONFIG_TIME_NS=y"<br></blockquote><div><br></div><div><div class="gmail_default" style="font-size:small">Shouldn't end with 'NULL' in kconfig struct?</div><div class="gmail_default" style="font-size:small">If not that will mislead arrary_len to recognise wrong number of arrry(cnt) and caused segmentation fault in test.</div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+       }<br></blockquote><div><br></div><div class="gmail_default" style="font-size:small">A comma is required here ^, otherwise it'd be failing in the build phase.</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small"></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+       .tags = (const struct tst_tag[]) {<br>
+               {"linux-git", "ecc421e05bab"},<br></blockquote><div><br></div><div><div class="gmail_default" style="font-size:small">Ending with '{}' in tags struct? </div></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
+       }<br>
+};<br>
-- <br>
2.24.1<br>
<br>
<br>
-- <br>
Mailing list info: <a href="https://lists.linux.it/listinfo/ltp" rel="noreferrer" target="_blank">https://lists.linux.it/listinfo/ltp</a><br>
<br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr"><div dir="ltr"><div>Regards,<br></div><div>Li Wang<br></div></div></div></div>