]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tests: create test-raw-clone.c for raw-clone.h
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sun, 6 Nov 2022 16:13:57 +0000 (17:13 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 8 Nov 2022 12:41:13 +0000 (13:41 +0100)
The include for process-util.h is added for reset_cached_pid(). This
essentially fixes a pre-existing missing include.

src/basic/raw-clone.h
src/test/meson.build
src/test/test-raw-clone.c [new file with mode: 0644]
src/test/test-util.c

index becf42e70bf84f0c6b8a2e88ca68aa24df839dcb..a3b768f826181023887c23d58311bf2f785b1786 100644 (file)
@@ -11,6 +11,7 @@
 
 #include "log.h"
 #include "macro.h"
+#include "process-util.h"
 
 /**
  * raw_clone() - uses clone to create a new process with clone flags
index d59ea44aa10d393b3d722d916db85d392d452719..bb095989661c2405041c00235ac96da707c3dd51 100644 (file)
@@ -205,6 +205,8 @@ tests += [
 
         [files('test-ratelimit.c')],
 
+        [files('test-raw-clone.c')],
+
         [files('test-limits-util.c')],
 
         [files('test-util.c')],
diff --git a/src/test/test-raw-clone.c b/src/test/test-raw-clone.c
new file mode 100644 (file)
index 0000000..23ec7d1
--- /dev/null
@@ -0,0 +1,41 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include <sys/wait.h>
+#include <unistd.h>
+
+#include "errno-util.h"
+#include "format-util.h"
+#include "missing_syscall.h"
+#include "raw-clone.h"
+#include "tests.h"
+
+TEST(raw_clone) {
+        pid_t parent, pid, pid2;
+
+        parent = getpid();
+        log_info("before clone: getpid()→"PID_FMT, parent);
+        assert_se(raw_getpid() == parent);
+
+        pid = raw_clone(0);
+        assert_se(pid >= 0);
+
+        pid2 = raw_getpid();
+        log_info("raw_clone: "PID_FMT" getpid()→"PID_FMT" raw_getpid()→"PID_FMT,
+                 pid, getpid(), pid2);
+        if (pid == 0) {
+                assert_se(pid2 != parent);
+                _exit(EXIT_SUCCESS);
+        } else {
+                int status;
+
+                assert_se(pid2 == parent);
+                waitpid(pid, &status, __WCLONE);
+                assert_se(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
+        }
+
+        errno = 0;
+        assert_se(raw_clone(CLONE_FS|CLONE_NEWNS) == -1);
+        assert_se(errno == EINVAL || ERRNO_IS_PRIVILEGE(errno)); /* Certain container environments prohibit namespaces to us, don't fail in that case */
+}
+
+DEFINE_TEST_MAIN(LOG_INFO);
index a526387528442a47dcd8525f98f457a194945ae0..ecbe08687a5edeb2067db880ebcacc3ea875db1c 100644 (file)
@@ -1,8 +1,6 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 
 #include <errno.h>
-#include <sys/wait.h>
-#include <unistd.h>
 
 #include "fileio.h"
 #include "fs-util.h"
@@ -128,33 +126,4 @@ TEST(eqzero) {
         assert_se(!eqzero(longer));
 }
 
-TEST(raw_clone) {
-        pid_t parent, pid, pid2;
-
-        parent = getpid();
-        log_info("before clone: getpid()→"PID_FMT, parent);
-        assert_se(raw_getpid() == parent);
-
-        pid = raw_clone(0);
-        assert_se(pid >= 0);
-
-        pid2 = raw_getpid();
-        log_info("raw_clone: "PID_FMT" getpid()→"PID_FMT" raw_getpid()→"PID_FMT,
-                 pid, getpid(), pid2);
-        if (pid == 0) {
-                assert_se(pid2 != parent);
-                _exit(EXIT_SUCCESS);
-        } else {
-                int status;
-
-                assert_se(pid2 == parent);
-                waitpid(pid, &status, __WCLONE);
-                assert_se(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
-        }
-
-        errno = 0;
-        assert_se(raw_clone(CLONE_FS|CLONE_NEWNS) == -1);
-        assert_se(errno == EINVAL || ERRNO_IS_PRIVILEGE(errno)); /* Certain container environments prohibit namespaces to us, don't fail in that case */
-}
-
 DEFINE_TEST_MAIN(LOG_INFO);