]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
d8c9d3a4 ZJS |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2013 Zbigniew Jędrzejewski-Szmek | |
d8c9d3a4 ZJS |
6 | ***/ |
7 | ||
613b411c | 8 | #include <sys/socket.h> |
d8c9d3a4 | 9 | |
b5efdb8a | 10 | #include "alloc-util.h" |
3ffd4af2 | 11 | #include "fd-util.h" |
d8c9d3a4 | 12 | #include "namespace.h" |
0b452006 | 13 | #include "process-util.h" |
07630cea LP |
14 | #include "string-util.h" |
15 | #include "util.h" | |
d8c9d3a4 ZJS |
16 | |
17 | static void test_tmpdir(const char *id, const char *A, const char *B) { | |
18 | _cleanup_free_ char *a, *b; | |
613b411c LP |
19 | struct stat x, y; |
20 | char *c, *d; | |
d8c9d3a4 | 21 | |
613b411c LP |
22 | assert_se(setup_tmp_dirs(id, &a, &b) == 0); |
23 | assert_se(startswith(a, A)); | |
24 | assert_se(startswith(b, B)); | |
d8c9d3a4 | 25 | |
613b411c LP |
26 | assert_se(stat(a, &x) >= 0); |
27 | assert_se(stat(b, &y) >= 0); | |
d8c9d3a4 | 28 | |
613b411c LP |
29 | assert_se(S_ISDIR(x.st_mode)); |
30 | assert_se(S_ISDIR(y.st_mode)); | |
d8c9d3a4 | 31 | |
613b411c LP |
32 | assert_se((x.st_mode & 01777) == 0700); |
33 | assert_se((y.st_mode & 01777) == 0700); | |
34 | ||
63c372cb LP |
35 | c = strjoina(a, "/tmp"); |
36 | d = strjoina(b, "/tmp"); | |
613b411c LP |
37 | |
38 | assert_se(stat(c, &x) >= 0); | |
39 | assert_se(stat(d, &y) >= 0); | |
40 | ||
41 | assert_se(S_ISDIR(x.st_mode)); | |
42 | assert_se(S_ISDIR(y.st_mode)); | |
43 | ||
44 | assert_se((x.st_mode & 01777) == 01777); | |
45 | assert_se((y.st_mode & 01777) == 01777); | |
46 | ||
47 | assert_se(rmdir(c) >= 0); | |
48 | assert_se(rmdir(d) >= 0); | |
49 | ||
50 | assert_se(rmdir(a) >= 0); | |
51 | assert_se(rmdir(b) >= 0); | |
52 | } | |
53 | ||
54 | static void test_netns(void) { | |
3d94f76c | 55 | _cleanup_close_pair_ int s[2] = { -1, -1 }; |
613b411c LP |
56 | pid_t pid1, pid2, pid3; |
57 | int r, n = 0; | |
58 | siginfo_t si; | |
59 | ||
d2528deb EV |
60 | if (geteuid() > 0) { |
61 | log_info("Skipping test: not root"); | |
62 | exit(EXIT_TEST_SKIP); | |
63 | } | |
613b411c LP |
64 | |
65 | assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, s) >= 0); | |
66 | ||
67 | pid1 = fork(); | |
68 | assert_se(pid1 >= 0); | |
69 | ||
70 | if (pid1 == 0) { | |
71 | r = setup_netns(s); | |
72 | assert_se(r >= 0); | |
73 | _exit(r); | |
74 | } | |
75 | ||
76 | pid2 = fork(); | |
77 | assert_se(pid2 >= 0); | |
78 | ||
79 | if (pid2 == 0) { | |
80 | r = setup_netns(s); | |
81 | assert_se(r >= 0); | |
82 | exit(r); | |
83 | } | |
84 | ||
85 | pid3 = fork(); | |
86 | assert_se(pid3 >= 0); | |
87 | ||
88 | if (pid3 == 0) { | |
89 | r = setup_netns(s); | |
90 | assert_se(r >= 0); | |
91 | exit(r); | |
92 | } | |
93 | ||
94 | r = wait_for_terminate(pid1, &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(pid2, &si); | |
100 | assert_se(r >= 0); | |
101 | assert_se(si.si_code == CLD_EXITED); | |
102 | n += si.si_status; | |
103 | ||
104 | r = wait_for_terminate(pid3, &si); | |
105 | assert_se(r >= 0); | |
106 | assert_se(si.si_code == CLD_EXITED); | |
107 | n += si.si_status; | |
108 | ||
109 | assert_se(n == 1); | |
d8c9d3a4 ZJS |
110 | } |
111 | ||
112 | int main(int argc, char *argv[]) { | |
6b46ea73 LP |
113 | sd_id128_t bid; |
114 | char boot_id[SD_ID128_STRING_MAX]; | |
115 | _cleanup_free_ char *x = NULL, *y = NULL, *z = NULL, *zz = NULL; | |
d8c9d3a4 | 116 | |
d2528deb EV |
117 | log_parse_environment(); |
118 | log_open(); | |
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 | |
613b411c LP |
136 | test_netns(); |
137 | ||
d8c9d3a4 ZJS |
138 | return 0; |
139 | } |