]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
049af8ad | 2 | |
af918c48 | 3 | #include <sched.h> |
049af8ad | 4 | #include <sys/mount.h> |
ca78ad1d | 5 | #include <unistd.h> |
049af8ad ZJS |
6 | |
7 | #include "alloc-util.h" | |
8 | #include "def.h" | |
9 | #include "fd-util.h" | |
10 | #include "fileio.h" | |
11 | #include "hashmap.h" | |
12 | #include "log.h" | |
049af8ad ZJS |
13 | #include "mountpoint-util.h" |
14 | #include "path-util.h" | |
15 | #include "rm-rf.h" | |
16 | #include "string-util.h" | |
17 | #include "tests.h" | |
18 | ||
19 | static void test_mount_propagation_flags(const char *name, int ret, unsigned long expected) { | |
20 | long unsigned flags; | |
21 | ||
568ef987 ZJS |
22 | log_info("/* %s(%s) */", __func__, name); |
23 | ||
049af8ad ZJS |
24 | assert_se(mount_propagation_flags_from_string(name, &flags) == ret); |
25 | ||
26 | if (ret >= 0) { | |
27 | const char *c; | |
28 | ||
29 | assert_se(flags == expected); | |
30 | ||
31 | c = mount_propagation_flags_to_string(flags); | |
32 | if (isempty(name)) | |
33 | assert_se(isempty(c)); | |
34 | else | |
35 | assert_se(streq(c, name)); | |
36 | } | |
37 | } | |
38 | ||
39 | static void test_mnt_id(void) { | |
40 | _cleanup_fclose_ FILE *f = NULL; | |
9003da29 | 41 | _cleanup_hashmap_free_free_ Hashmap *h = NULL; |
049af8ad ZJS |
42 | char *p; |
43 | void *k; | |
44 | int r; | |
45 | ||
568ef987 ZJS |
46 | log_info("/* %s */", __func__); |
47 | ||
049af8ad ZJS |
48 | assert_se(f = fopen("/proc/self/mountinfo", "re")); |
49 | assert_se(h = hashmap_new(&trivial_hash_ops)); | |
50 | ||
51 | for (;;) { | |
52 | _cleanup_free_ char *line = NULL, *path = NULL; | |
53 | int mnt_id; | |
54 | ||
55 | r = read_line(f, LONG_LINE_MAX, &line); | |
56 | if (r == 0) | |
57 | break; | |
58 | assert_se(r > 0); | |
59 | ||
60 | assert_se(sscanf(line, "%i %*s %*s %*s %ms", &mnt_id, &path) == 2); | |
9003da29 ZJS |
61 | #if HAS_FEATURE_MEMORY_SANITIZER |
62 | /* We don't know the length of the string, so we need to unpoison it one char at a time */ | |
63 | for (const char *c = path; ;c++) { | |
64 | msan_unpoison(c, 1); | |
65 | if (!*c) | |
66 | break; | |
67 | } | |
68 | #endif | |
568ef987 ZJS |
69 | log_debug("mountinfo: %s → %i", path, mnt_id); |
70 | ||
049af8ad ZJS |
71 | assert_se(hashmap_put(h, INT_TO_PTR(mnt_id), path) >= 0); |
72 | path = NULL; | |
73 | } | |
74 | ||
90e74a66 | 75 | HASHMAP_FOREACH_KEY(p, k, h) { |
049af8ad ZJS |
76 | int mnt_id = PTR_TO_INT(k), mnt_id2; |
77 | ||
78 | r = path_get_mnt_id(p, &mnt_id2); | |
79 | if (r < 0) { | |
80 | log_debug_errno(r, "Failed to get the mnt id of %s: %m\n", p); | |
81 | continue; | |
82 | } | |
83 | ||
c569d529 ZJS |
84 | if (mnt_id == mnt_id2) { |
85 | log_debug("mnt ids of %s is %i\n", p, mnt_id); | |
049af8ad | 86 | continue; |
c569d529 ZJS |
87 | } else |
88 | log_debug("mnt ids of %s are %i, %i\n", p, mnt_id, mnt_id2); | |
049af8ad | 89 | |
568ef987 ZJS |
90 | /* The ids don't match? If so, then there are two mounts on the same path, let's check if |
91 | * that's really the case */ | |
92 | char *t = hashmap_get(h, INT_TO_PTR(mnt_id2)); | |
93 | log_debug("the other path for mnt id %i is %s\n", mnt_id2, t); | |
94 | assert_se(path_equal(p, t)); | |
049af8ad | 95 | } |
049af8ad ZJS |
96 | } |
97 | ||
98 | static void test_path_is_mount_point(void) { | |
99 | int fd; | |
100 | char tmp_dir[] = "/tmp/test-path-is-mount-point-XXXXXX"; | |
101 | _cleanup_free_ char *file1 = NULL, *file2 = NULL, *link1 = NULL, *link2 = NULL; | |
102 | _cleanup_free_ char *dir1 = NULL, *dir1file = NULL, *dirlink1 = NULL, *dirlink1file = NULL; | |
103 | _cleanup_free_ char *dir2 = NULL, *dir2file = NULL; | |
104 | ||
568ef987 ZJS |
105 | log_info("/* %s */", __func__); |
106 | ||
049af8ad ZJS |
107 | assert_se(path_is_mount_point("/", NULL, AT_SYMLINK_FOLLOW) > 0); |
108 | assert_se(path_is_mount_point("/", NULL, 0) > 0); | |
109 | assert_se(path_is_mount_point("//", NULL, AT_SYMLINK_FOLLOW) > 0); | |
110 | assert_se(path_is_mount_point("//", NULL, 0) > 0); | |
111 | ||
112 | assert_se(path_is_mount_point("/proc", NULL, AT_SYMLINK_FOLLOW) > 0); | |
113 | assert_se(path_is_mount_point("/proc", NULL, 0) > 0); | |
114 | assert_se(path_is_mount_point("/proc/", NULL, AT_SYMLINK_FOLLOW) > 0); | |
115 | assert_se(path_is_mount_point("/proc/", NULL, 0) > 0); | |
116 | ||
117 | assert_se(path_is_mount_point("/proc/1", NULL, AT_SYMLINK_FOLLOW) == 0); | |
118 | assert_se(path_is_mount_point("/proc/1", NULL, 0) == 0); | |
119 | assert_se(path_is_mount_point("/proc/1/", NULL, AT_SYMLINK_FOLLOW) == 0); | |
120 | assert_se(path_is_mount_point("/proc/1/", NULL, 0) == 0); | |
121 | ||
122 | assert_se(path_is_mount_point("/sys", NULL, AT_SYMLINK_FOLLOW) > 0); | |
123 | assert_se(path_is_mount_point("/sys", NULL, 0) > 0); | |
124 | assert_se(path_is_mount_point("/sys/", NULL, AT_SYMLINK_FOLLOW) > 0); | |
125 | assert_se(path_is_mount_point("/sys/", NULL, 0) > 0); | |
126 | ||
127 | /* we'll create a hierarchy of different kinds of dir/file/link | |
128 | * layouts: | |
129 | * | |
130 | * <tmp>/file1, <tmp>/file2 | |
131 | * <tmp>/link1 -> file1, <tmp>/link2 -> file2 | |
132 | * <tmp>/dir1/ | |
133 | * <tmp>/dir1/file | |
134 | * <tmp>/dirlink1 -> dir1 | |
135 | * <tmp>/dirlink1file -> dirlink1/file | |
136 | * <tmp>/dir2/ | |
137 | * <tmp>/dir2/file | |
138 | */ | |
139 | ||
140 | /* file mountpoints */ | |
141 | assert_se(mkdtemp(tmp_dir) != NULL); | |
62a85ee0 | 142 | file1 = path_join(tmp_dir, "file1"); |
049af8ad | 143 | assert_se(file1); |
62a85ee0 | 144 | file2 = path_join(tmp_dir, "file2"); |
049af8ad ZJS |
145 | assert_se(file2); |
146 | fd = open(file1, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664); | |
147 | assert_se(fd > 0); | |
148 | close(fd); | |
149 | fd = open(file2, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664); | |
150 | assert_se(fd > 0); | |
151 | close(fd); | |
62a85ee0 | 152 | link1 = path_join(tmp_dir, "link1"); |
049af8ad ZJS |
153 | assert_se(link1); |
154 | assert_se(symlink("file1", link1) == 0); | |
62a85ee0 | 155 | link2 = path_join(tmp_dir, "link2"); |
049af8ad ZJS |
156 | assert_se(link1); |
157 | assert_se(symlink("file2", link2) == 0); | |
158 | ||
159 | assert_se(path_is_mount_point(file1, NULL, AT_SYMLINK_FOLLOW) == 0); | |
160 | assert_se(path_is_mount_point(file1, NULL, 0) == 0); | |
161 | assert_se(path_is_mount_point(link1, NULL, AT_SYMLINK_FOLLOW) == 0); | |
162 | assert_se(path_is_mount_point(link1, NULL, 0) == 0); | |
163 | ||
164 | /* directory mountpoints */ | |
62a85ee0 | 165 | dir1 = path_join(tmp_dir, "dir1"); |
049af8ad ZJS |
166 | assert_se(dir1); |
167 | assert_se(mkdir(dir1, 0755) == 0); | |
62a85ee0 | 168 | dirlink1 = path_join(tmp_dir, "dirlink1"); |
049af8ad ZJS |
169 | assert_se(dirlink1); |
170 | assert_se(symlink("dir1", dirlink1) == 0); | |
62a85ee0 | 171 | dirlink1file = path_join(tmp_dir, "dirlink1file"); |
049af8ad ZJS |
172 | assert_se(dirlink1file); |
173 | assert_se(symlink("dirlink1/file", dirlink1file) == 0); | |
62a85ee0 | 174 | dir2 = path_join(tmp_dir, "dir2"); |
049af8ad ZJS |
175 | assert_se(dir2); |
176 | assert_se(mkdir(dir2, 0755) == 0); | |
177 | ||
178 | assert_se(path_is_mount_point(dir1, NULL, AT_SYMLINK_FOLLOW) == 0); | |
179 | assert_se(path_is_mount_point(dir1, NULL, 0) == 0); | |
180 | assert_se(path_is_mount_point(dirlink1, NULL, AT_SYMLINK_FOLLOW) == 0); | |
181 | assert_se(path_is_mount_point(dirlink1, NULL, 0) == 0); | |
182 | ||
183 | /* file in subdirectory mountpoints */ | |
62a85ee0 | 184 | dir1file = path_join(dir1, "file"); |
049af8ad ZJS |
185 | assert_se(dir1file); |
186 | fd = open(dir1file, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664); | |
187 | assert_se(fd > 0); | |
188 | close(fd); | |
189 | ||
190 | assert_se(path_is_mount_point(dir1file, NULL, AT_SYMLINK_FOLLOW) == 0); | |
191 | assert_se(path_is_mount_point(dir1file, NULL, 0) == 0); | |
192 | assert_se(path_is_mount_point(dirlink1file, NULL, AT_SYMLINK_FOLLOW) == 0); | |
193 | assert_se(path_is_mount_point(dirlink1file, NULL, 0) == 0); | |
194 | ||
195 | /* these tests will only work as root */ | |
196 | if (mount(file1, file2, NULL, MS_BIND, NULL) >= 0) { | |
197 | int rf, rt, rdf, rdt, rlf, rlt, rl1f, rl1t; | |
198 | const char *file2d; | |
199 | ||
200 | /* files */ | |
201 | /* capture results in vars, to avoid dangling mounts on failure */ | |
202 | log_info("%s: %s", __func__, file2); | |
203 | rf = path_is_mount_point(file2, NULL, 0); | |
204 | rt = path_is_mount_point(file2, NULL, AT_SYMLINK_FOLLOW); | |
205 | ||
206 | file2d = strjoina(file2, "/"); | |
207 | log_info("%s: %s", __func__, file2d); | |
208 | rdf = path_is_mount_point(file2d, NULL, 0); | |
209 | rdt = path_is_mount_point(file2d, NULL, AT_SYMLINK_FOLLOW); | |
210 | ||
211 | log_info("%s: %s", __func__, link2); | |
212 | rlf = path_is_mount_point(link2, NULL, 0); | |
213 | rlt = path_is_mount_point(link2, NULL, AT_SYMLINK_FOLLOW); | |
214 | ||
215 | assert_se(umount(file2) == 0); | |
216 | ||
217 | assert_se(rf == 1); | |
218 | assert_se(rt == 1); | |
219 | assert_se(rdf == -ENOTDIR); | |
220 | assert_se(rdt == -ENOTDIR); | |
221 | assert_se(rlf == 0); | |
222 | assert_se(rlt == 1); | |
223 | ||
224 | /* dirs */ | |
62a85ee0 | 225 | dir2file = path_join(dir2, "file"); |
049af8ad ZJS |
226 | assert_se(dir2file); |
227 | fd = open(dir2file, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664); | |
228 | assert_se(fd > 0); | |
229 | close(fd); | |
230 | ||
231 | assert_se(mount(dir2, dir1, NULL, MS_BIND, NULL) >= 0); | |
232 | ||
233 | log_info("%s: %s", __func__, dir1); | |
234 | rf = path_is_mount_point(dir1, NULL, 0); | |
235 | rt = path_is_mount_point(dir1, NULL, AT_SYMLINK_FOLLOW); | |
236 | log_info("%s: %s", __func__, dirlink1); | |
237 | rlf = path_is_mount_point(dirlink1, NULL, 0); | |
238 | rlt = path_is_mount_point(dirlink1, NULL, AT_SYMLINK_FOLLOW); | |
239 | log_info("%s: %s", __func__, dirlink1file); | |
240 | /* its parent is a mount point, but not /file itself */ | |
241 | rl1f = path_is_mount_point(dirlink1file, NULL, 0); | |
242 | rl1t = path_is_mount_point(dirlink1file, NULL, AT_SYMLINK_FOLLOW); | |
243 | ||
244 | assert_se(umount(dir1) == 0); | |
245 | ||
246 | assert_se(rf == 1); | |
247 | assert_se(rt == 1); | |
248 | assert_se(rlf == 0); | |
249 | assert_se(rlt == 1); | |
250 | assert_se(rl1f == 0); | |
251 | assert_se(rl1t == 0); | |
252 | ||
253 | } else | |
254 | printf("Skipping bind mount file test: %m\n"); | |
255 | ||
256 | assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0); | |
257 | } | |
258 | ||
259 | int main(int argc, char *argv[]) { | |
260 | test_setup_logging(LOG_DEBUG); | |
261 | ||
af918c48 LP |
262 | /* let's move into our own mount namespace with all propagation from the host turned off, so that |
263 | * /proc/self/mountinfo is static and constant for the whole time our test runs. */ | |
264 | if (unshare(CLONE_NEWNS) < 0) { | |
265 | if (!ERRNO_IS_PRIVILEGE(errno)) | |
266 | return log_error_errno(errno, "Failed to detach mount namespace: %m"); | |
267 | ||
268 | log_notice("Lacking privilege to create separate mount namespace, proceeding in originating mount namespace."); | |
269 | } else | |
270 | assert_se(mount(NULL, "/", NULL, MS_PRIVATE | MS_REC, NULL) >= 0); | |
271 | ||
049af8ad ZJS |
272 | test_mount_propagation_flags("shared", 0, MS_SHARED); |
273 | test_mount_propagation_flags("slave", 0, MS_SLAVE); | |
274 | test_mount_propagation_flags("private", 0, MS_PRIVATE); | |
275 | test_mount_propagation_flags(NULL, 0, 0); | |
276 | test_mount_propagation_flags("", 0, 0); | |
277 | test_mount_propagation_flags("xxxx", -EINVAL, 0); | |
278 | test_mount_propagation_flags(" ", -EINVAL, 0); | |
279 | ||
280 | test_mnt_id(); | |
281 | test_path_is_mount_point(); | |
282 | ||
283 | return 0; | |
284 | } |