selftests/timens: Check for right timens offsets after fork and exec
authorAndrei Vagin <avagin@gmail.com>
Tue, 12 Nov 2019 01:27:23 +0000 (01:27 +0000)
committerThomas Gleixner <tglx@linutronix.de>
Tue, 14 Jan 2020 11:21:02 +0000 (12:21 +0100)
Output on success:
 1..1
 ok 1 exec
 # Pass 1 Fail 0 Xfail 0 Xpass 0 Skip 0 Error 0

Output on failure:
 1..1
 not ok 1 36016 16
 Bail out!

Output with lack of permissions:
 1..1
 not ok 1 # SKIP need to run as root

Output without support of time namespaces:
 1..1
 not ok 1 # SKIP Time namespaces are not supported

Co-developed-by: Dmitry Safonov <dima@arista.com>
Signed-off-by: Andrei Vagin <avagin@gmail.com>
Signed-off-by: Dmitry Safonov <dima@arista.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Link: https://lore.kernel.org/r/20191112012724.250792-35-dima@arista.com
tools/testing/selftests/timens/.gitignore
tools/testing/selftests/timens/Makefile
tools/testing/selftests/timens/exec.c [new file with mode: 0644]

index 16292e4..789f21e 100644 (file)
@@ -1,4 +1,5 @@
 clock_nanosleep
+exec
 gettime_perf
 gettime_perf_cold
 procfs
index 6aefcac..e9fb30b 100644 (file)
@@ -1,4 +1,4 @@
-TEST_GEN_PROGS := timens timerfd timer clock_nanosleep procfs
+TEST_GEN_PROGS := timens timerfd timer clock_nanosleep procfs exec
 TEST_GEN_PROGS_EXTENDED := gettime_perf
 
 CFLAGS := -Wall -Werror -pthread
diff --git a/tools/testing/selftests/timens/exec.c b/tools/testing/selftests/timens/exec.c
new file mode 100644 (file)
index 0000000..87b47b5
--- /dev/null
@@ -0,0 +1,94 @@
+// SPDX-License-Identifier: GPL-2.0
+#define _GNU_SOURCE
+#include <errno.h>
+#include <fcntl.h>
+#include <sched.h>
+#include <stdio.h>
+#include <stdbool.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <time.h>
+#include <unistd.h>
+#include <time.h>
+#include <string.h>
+
+#include "log.h"
+#include "timens.h"
+
+#define OFFSET (36000)
+
+int main(int argc, char *argv[])
+{
+       struct timespec now, tst;
+       int status, i;
+       pid_t pid;
+
+       if (argc > 1) {
+               if (sscanf(argv[1], "%ld", &now.tv_sec) != 1)
+                       return pr_perror("sscanf");
+
+               for (i = 0; i < 2; i++) {
+                       _gettime(CLOCK_MONOTONIC, &tst, i);
+                       if (abs(tst.tv_sec - now.tv_sec) > 5)
+                               return pr_fail("%ld %ld\n", now.tv_sec, tst.tv_sec);
+               }
+               return 0;
+       }
+
+       nscheck();
+
+       ksft_set_plan(1);
+
+       clock_gettime(CLOCK_MONOTONIC, &now);
+
+       if (unshare_timens())
+               return 1;
+
+       if (_settime(CLOCK_MONOTONIC, OFFSET))
+               return 1;
+
+       for (i = 0; i < 2; i++) {
+               _gettime(CLOCK_MONOTONIC, &tst, i);
+               if (abs(tst.tv_sec - now.tv_sec) > 5)
+                       return pr_fail("%ld %ld\n",
+                                       now.tv_sec, tst.tv_sec);
+       }
+
+       if (argc > 1)
+               return 0;
+
+       pid = fork();
+       if (pid < 0)
+               return pr_perror("fork");
+
+       if (pid == 0) {
+               char now_str[64];
+               char *cargv[] = {"exec", now_str, NULL};
+               char *cenv[] = {NULL};
+
+               /* Check that a child process is in the new timens. */
+               for (i = 0; i < 2; i++) {
+                       _gettime(CLOCK_MONOTONIC, &tst, i);
+                       if (abs(tst.tv_sec - now.tv_sec - OFFSET) > 5)
+                               return pr_fail("%ld %ld\n",
+                                               now.tv_sec + OFFSET, tst.tv_sec);
+               }
+
+               /* Check for proper vvar offsets after execve. */
+               snprintf(now_str, sizeof(now_str), "%ld", now.tv_sec + OFFSET);
+               execve("/proc/self/exe", cargv, cenv);
+               return pr_perror("execve");
+       }
+
+       if (waitpid(pid, &status, 0) != pid)
+               return pr_perror("waitpid");
+
+       if (status)
+               ksft_exit_fail();
+
+       ksft_test_result_pass("exec\n");
+       ksft_exit_pass();
+       return 0;
+}