]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tests: add helper call have_namespaces() to test whether Linux namespaces are available
authorLennart Poettering <lennart@poettering.net>
Wed, 24 Oct 2018 15:07:04 +0000 (17:07 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 24 Oct 2018 17:40:24 +0000 (19:40 +0200)
A slighly sloppy test call for conditionalizing several tests.

src/shared/tests.c
src/shared/tests.h

index ec8039575e8e0833809284d85f554f1b33302e3e..d21bd22b4092281c38ff4e6ff97a2dd55a687d54 100644 (file)
@@ -1,6 +1,10 @@
 /* SPDX-License-Identifier: LGPL-2.1+ */
 
+#include <sched.h>
+#include <signal.h>
 #include <stdlib.h>
+#include <sys/mount.h>
+#include <sys/wait.h>
 #include <util.h>
 
 /* When we include libgen.h because we need dirname() we immediately
@@ -112,3 +116,36 @@ int log_tests_skipped_errno(int r, const char *message) {
                          program_invocation_short_name, message);
         return EXIT_TEST_SKIP;
 }
+
+bool have_namespaces(void) {
+        siginfo_t si = {};
+        pid_t pid;
+
+        /* Checks whether namespaces are available. In some cases they aren't. We do this by calling unshare(), and we
+         * do so in a child process in order not to affect our own process. */
+
+        pid = fork();
+        assert_se(pid >= 0);
+
+        if (pid == 0) {
+                /* child */
+                if (unshare(CLONE_NEWNS) < 0)
+                        _exit(EXIT_FAILURE);
+
+                if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL) < 0)
+                        _exit(EXIT_FAILURE);
+
+                _exit(EXIT_SUCCESS);
+        }
+
+        assert_se(waitid(P_PID, pid, &si, WEXITED) >= 0);
+        assert_se(si.si_code == CLD_EXITED);
+
+        if (si.si_status == EXIT_SUCCESS)
+                return true;
+
+        if (si.si_status == EXIT_FAILURE)
+                return false;
+
+        assert_not_reached("unexpected exit code");
+}
index 58a3e1c32df7a5c61e2bf6e0ebf1fd022f44a1bb..718196f134bed74aa504cf76e96295cad198011f 100644 (file)
@@ -10,3 +10,5 @@ bool slow_tests_enabled(void);
 void test_setup_logging(int level);
 int log_tests_skipped(const char *message);
 int log_tests_skipped_errno(int r, const char *message);
+
+bool have_namespaces(void);