]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
d8c9d3a4 | 2 | |
613b411c | 3 | #include <sys/socket.h> |
ca78ad1d | 4 | #include <sys/stat.h> |
d8c9d3a4 | 5 | |
b5efdb8a | 6 | #include "alloc-util.h" |
3ffd4af2 | 7 | #include "fd-util.h" |
d8c9d3a4 | 8 | #include "namespace.h" |
0b452006 | 9 | #include "process-util.h" |
07630cea | 10 | #include "string-util.h" |
317bb217 | 11 | #include "tests.h" |
07630cea | 12 | #include "util.h" |
d8c9d3a4 ZJS |
13 | |
14 | static void test_tmpdir(const char *id, const char *A, const char *B) { | |
15 | _cleanup_free_ char *a, *b; | |
613b411c LP |
16 | struct stat x, y; |
17 | char *c, *d; | |
d8c9d3a4 | 18 | |
613b411c LP |
19 | assert_se(setup_tmp_dirs(id, &a, &b) == 0); |
20 | assert_se(startswith(a, A)); | |
21 | assert_se(startswith(b, B)); | |
d8c9d3a4 | 22 | |
613b411c LP |
23 | assert_se(stat(a, &x) >= 0); |
24 | assert_se(stat(b, &y) >= 0); | |
d8c9d3a4 | 25 | |
613b411c LP |
26 | assert_se(S_ISDIR(x.st_mode)); |
27 | assert_se(S_ISDIR(y.st_mode)); | |
d8c9d3a4 | 28 | |
613b411c LP |
29 | assert_se((x.st_mode & 01777) == 0700); |
30 | assert_se((y.st_mode & 01777) == 0700); | |
31 | ||
63c372cb LP |
32 | c = strjoina(a, "/tmp"); |
33 | d = strjoina(b, "/tmp"); | |
613b411c LP |
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 | ||
317bb217 | 51 | static int test_netns(void) { |
3d94f76c | 52 | _cleanup_close_pair_ int s[2] = { -1, -1 }; |
613b411c LP |
53 | pid_t pid1, pid2, pid3; |
54 | int r, n = 0; | |
55 | siginfo_t si; | |
56 | ||
317bb217 ZJS |
57 | if (geteuid() > 0) |
58 | return log_tests_skipped("not root"); | |
613b411c LP |
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); | |
317bb217 | 105 | return EXIT_SUCCESS; |
d8c9d3a4 ZJS |
106 | } |
107 | ||
108 | int main(int argc, char *argv[]) { | |
6b46ea73 LP |
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; | |
d8c9d3a4 | 112 | |
6d7c4033 | 113 | test_setup_logging(LOG_INFO); |
d2528deb | 114 | |
5f00dc4d LP |
115 | if (!have_namespaces()) { |
116 | log_tests_skipped("Don't have namespace support"); | |
117 | return EXIT_TEST_SKIP; | |
118 | } | |
119 | ||
6b46ea73 LP |
120 | assert_se(sd_id128_get_boot(&bid) >= 0); |
121 | sd_id128_to_string(bid, boot_id); | |
122 | ||
605405c6 ZJS |
123 | x = strjoin("/tmp/systemd-private-", boot_id, "-abcd.service-"); |
124 | y = strjoin("/var/tmp/systemd-private-", boot_id, "-abcd.service-"); | |
6b46ea73 LP |
125 | assert_se(x && y); |
126 | ||
127 | test_tmpdir("abcd.service", x, y); | |
128 | ||
605405c6 ZJS |
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-"); | |
6b46ea73 LP |
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); | |
d8c9d3a4 | 135 | |
317bb217 | 136 | return test_netns(); |
d8c9d3a4 | 137 | } |