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