Debian Codegraph @

src/test/test-raw-clone.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/wait.h>
4 #include <unistd.h>
5
6 #include "errno-util.h"
7 #include "format-util.h"
8 #include "raw-clone.h"
9 #include "tests.h"
10
11 TEST(raw_clone) {
12 pid_t parent, pid, pid2;
13
14 parent = getpid();
15 log_info("before clone: getpid()→"PID_FMT, parent);
16 assert_se(getpid() == parent);
17
18 pid = raw_clone(0);
19 assert_se(pid >= 0);
20
21 pid2 = getpid();
22 log_info("raw_clone: "PID_FMT" getpid()→"PID_FMT" getpid()→"PID_FMT,
23 pid, getpid(), pid2);
24 if (pid == 0) {
25 assert_se(pid2 != parent);
26 _exit(EXIT_SUCCESS);
27 } else {
28 int status;
29
30 assert_se(pid2 == parent);
31 waitpid(pid, &status, __WCLONE);
32 assert_se(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
33 }
34
35 errno = 0;
36 assert_se(raw_clone(CLONE_FS|CLONE_NEWNS) == -1);
37 assert_se(errno == EINVAL || ERRNO_IS_PRIVILEGE(errno)); /* Certain container environments prohibit namespaces to us, don't fail in that case */
38 }
39
40 DEFINE_TEST_MAIN(LOG_INFO);