]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-namespace.c
headers: remove unneeded includes from util.h
[thirdparty/systemd.git] / src / test / test-namespace.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <sys/socket.h>
4 #include <sys/stat.h>
5
6 #include "alloc-util.h"
7 #include "fd-util.h"
8 #include "namespace.h"
9 #include "process-util.h"
10 #include "string-util.h"
11 #include "tests.h"
12 #include "util.h"
13
14 static void test_tmpdir(const char *id, const char *A, const char *B) {
15 _cleanup_free_ char *a, *b;
16 struct stat x, y;
17 char *c, *d;
18
19 assert_se(setup_tmp_dirs(id, &a, &b) == 0);
20 assert_se(startswith(a, A));
21 assert_se(startswith(b, B));
22
23 assert_se(stat(a, &x) >= 0);
24 assert_se(stat(b, &y) >= 0);
25
26 assert_se(S_ISDIR(x.st_mode));
27 assert_se(S_ISDIR(y.st_mode));
28
29 assert_se((x.st_mode & 01777) == 0700);
30 assert_se((y.st_mode & 01777) == 0700);
31
32 c = strjoina(a, "/tmp");
33 d = strjoina(b, "/tmp");
34
35 assert_se(stat(c, &x) >= 0);
36 assert_se(stat(d, &y) >= 0);
37
38 assert_se(S_ISDIR(x.st_mode));
39 assert_se(S_ISDIR(y.st_mode));
40
41 assert_se((x.st_mode & 01777) == 01777);
42 assert_se((y.st_mode & 01777) == 01777);
43
44 assert_se(rmdir(c) >= 0);
45 assert_se(rmdir(d) >= 0);
46
47 assert_se(rmdir(a) >= 0);
48 assert_se(rmdir(b) >= 0);
49 }
50
51 static int test_netns(void) {
52 _cleanup_close_pair_ int s[2] = { -1, -1 };
53 pid_t pid1, pid2, pid3;
54 int r, n = 0;
55 siginfo_t si;
56
57 if (geteuid() > 0)
58 return log_tests_skipped("not root");
59
60 assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, s) >= 0);
61
62 pid1 = fork();
63 assert_se(pid1 >= 0);
64
65 if (pid1 == 0) {
66 r = setup_netns(s);
67 assert_se(r >= 0);
68 _exit(r);
69 }
70
71 pid2 = fork();
72 assert_se(pid2 >= 0);
73
74 if (pid2 == 0) {
75 r = setup_netns(s);
76 assert_se(r >= 0);
77 exit(r);
78 }
79
80 pid3 = fork();
81 assert_se(pid3 >= 0);
82
83 if (pid3 == 0) {
84 r = setup_netns(s);
85 assert_se(r >= 0);
86 exit(r);
87 }
88
89 r = wait_for_terminate(pid1, &si);
90 assert_se(r >= 0);
91 assert_se(si.si_code == CLD_EXITED);
92 n += si.si_status;
93
94 r = wait_for_terminate(pid2, &si);
95 assert_se(r >= 0);
96 assert_se(si.si_code == CLD_EXITED);
97 n += si.si_status;
98
99 r = wait_for_terminate(pid3, &si);
100 assert_se(r >= 0);
101 assert_se(si.si_code == CLD_EXITED);
102 n += si.si_status;
103
104 assert_se(n == 1);
105 return EXIT_SUCCESS;
106 }
107
108 int main(int argc, char *argv[]) {
109 sd_id128_t bid;
110 char boot_id[SD_ID128_STRING_MAX];
111 _cleanup_free_ char *x = NULL, *y = NULL, *z = NULL, *zz = NULL;
112
113 test_setup_logging(LOG_INFO);
114
115 if (!have_namespaces()) {
116 log_tests_skipped("Don't have namespace support");
117 return EXIT_TEST_SKIP;
118 }
119
120 assert_se(sd_id128_get_boot(&bid) >= 0);
121 sd_id128_to_string(bid, boot_id);
122
123 x = strjoin("/tmp/systemd-private-", boot_id, "-abcd.service-");
124 y = strjoin("/var/tmp/systemd-private-", boot_id, "-abcd.service-");
125 assert_se(x && y);
126
127 test_tmpdir("abcd.service", x, y);
128
129 z = strjoin("/tmp/systemd-private-", boot_id, "-sys-devices-pci0000:00-0000:00:1a.0-usb3-3\\x2d1-3\\x2d1:1.0-bluetooth-hci0.device-");
130 zz = strjoin("/var/tmp/systemd-private-", boot_id, "-sys-devices-pci0000:00-0000:00:1a.0-usb3-3\\x2d1-3\\x2d1:1.0-bluetooth-hci0.device-");
131
132 assert_se(z && zz);
133
134 test_tmpdir("sys-devices-pci0000:00-0000:00:1a.0-usb3-3\\x2d1-3\\x2d1:1.0-bluetooth-hci0.device", z, zz);
135
136 return test_netns();
137 }