]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-mountpoint-util.c
ci: restrict x86-only packages to x86 configs (#38056)
[thirdparty/systemd.git] / src / test / test-mountpoint-util.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
049af8ad 2
af918c48 3#include <sched.h>
fa34123c 4#include <stdlib.h>
049af8ad 5#include <sys/mount.h>
ca78ad1d 6#include <unistd.h>
049af8ad
ZJS
7
8#include "alloc-util.h"
049af8ad
ZJS
9#include "fd-util.h"
10#include "fileio.h"
fa34123c 11#include "fs-util.h"
049af8ad
ZJS
12#include "hashmap.h"
13#include "log.h"
049af8ad
ZJS
14#include "mountpoint-util.h"
15#include "path-util.h"
16#include "rm-rf.h"
d9ccf6b3 17#include "stat-util.h"
049af8ad
ZJS
18#include "string-util.h"
19#include "tests.h"
8bab8029 20#include "tmpfile-util.h"
049af8ad 21
b205e59a 22static void test_mount_propagation_flag_one(const char *name, int ret, unsigned long expected) {
da185cd0 23 unsigned long flags;
049af8ad 24
1b3502b0 25 log_info("/* %s(%s) */", __func__, strnull(name));
568ef987 26
b205e59a 27 assert_se(mount_propagation_flag_from_string(name, &flags) == ret);
049af8ad
ZJS
28
29 if (ret >= 0) {
30 const char *c;
31
32 assert_se(flags == expected);
33
b205e59a 34 c = mount_propagation_flag_to_string(flags);
049af8ad
ZJS
35 if (isempty(name))
36 assert_se(isempty(c));
37 else
c79e88b3 38 ASSERT_STREQ(c, name);
049af8ad
ZJS
39 }
40}
41
b205e59a
YW
42TEST(mount_propagation_flag) {
43 test_mount_propagation_flag_one("shared", 0, MS_SHARED);
44 test_mount_propagation_flag_one("slave", 0, MS_SLAVE);
45 test_mount_propagation_flag_one("private", 0, MS_PRIVATE);
46 test_mount_propagation_flag_one(NULL, 0, 0);
47 test_mount_propagation_flag_one("", 0, 0);
48 test_mount_propagation_flag_one("xxxx", -EINVAL, 0);
49 test_mount_propagation_flag_one(" ", -EINVAL, 0);
c462e63e
JJ
50}
51
52TEST(mnt_id) {
049af8ad 53 _cleanup_fclose_ FILE *f = NULL;
58f0cd14 54 _cleanup_hashmap_free_ Hashmap *h = NULL;
049af8ad
ZJS
55 char *p;
56 void *k;
57 int r;
58
59 assert_se(f = fopen("/proc/self/mountinfo", "re"));
58f0cd14 60 assert_se(h = hashmap_new(&trivial_hash_ops_value_free));
049af8ad
ZJS
61
62 for (;;) {
63 _cleanup_free_ char *line = NULL, *path = NULL;
64 int mnt_id;
65
66 r = read_line(f, LONG_LINE_MAX, &line);
67 if (r == 0)
68 break;
69 assert_se(r > 0);
70
71 assert_se(sscanf(line, "%i %*s %*s %*s %ms", &mnt_id, &path) == 2);
9003da29
ZJS
72#if HAS_FEATURE_MEMORY_SANITIZER
73 /* We don't know the length of the string, so we need to unpoison it one char at a time */
1d03d970 74 for (const char *c = path; ; c++) {
9003da29
ZJS
75 msan_unpoison(c, 1);
76 if (!*c)
77 break;
78 }
79#endif
568ef987
ZJS
80 log_debug("mountinfo: %s → %i", path, mnt_id);
81
049af8ad
ZJS
82 assert_se(hashmap_put(h, INT_TO_PTR(mnt_id), path) >= 0);
83 path = NULL;
84 }
85
90e74a66 86 HASHMAP_FOREACH_KEY(p, k, h) {
049af8ad 87 int mnt_id = PTR_TO_INT(k), mnt_id2;
b13268dc 88 const char *q;
049af8ad
ZJS
89
90 r = path_get_mnt_id(p, &mnt_id2);
91 if (r < 0) {
b13268dc 92 log_debug_errno(r, "Failed to get the mnt id of %s: %m", p);
049af8ad
ZJS
93 continue;
94 }
95
c569d529 96 if (mnt_id == mnt_id2) {
b13268dc 97 log_debug("mnt ids of %s is %i.", p, mnt_id);
049af8ad 98 continue;
c569d529 99 } else
b13268dc
YW
100 log_debug("mnt ids of %s are %i (from /proc/self/mountinfo), %i (from path_get_mnt_id()).", p, mnt_id, mnt_id2);
101
102 /* The ids don't match? This can easily happen e.g. running with "unshare --mount-proc".
103 * See #11505. */
104 assert_se(q = hashmap_get(h, INT_TO_PTR(mnt_id2)));
105
b409aacb 106 assert_se((r = path_is_mount_point_full(p, NULL, 0)) >= 0);
b13268dc
YW
107 if (r == 0) {
108 /* If the path is not a mount point anymore, then it must be a sub directory of
109 * the path corresponds to mnt_id2. */
110 log_debug("The path %s for mnt id %i is not a mount point.", p, mnt_id2);
111 assert_se(!isempty(path_startswith(p, q)));
112 } else {
113 /* If the path is still a mount point, then it must be equivalent to the path
114 * corresponds to mnt_id2 */
115 log_debug("There are multiple mounts on the same path %s.", p);
116 assert_se(path_equal(p, q));
117 }
049af8ad 118 }
049af8ad
ZJS
119}
120
c462e63e 121TEST(path_is_mount_point) {
049af8ad
ZJS
122 int fd;
123 char tmp_dir[] = "/tmp/test-path-is-mount-point-XXXXXX";
124 _cleanup_free_ char *file1 = NULL, *file2 = NULL, *link1 = NULL, *link2 = NULL;
125 _cleanup_free_ char *dir1 = NULL, *dir1file = NULL, *dirlink1 = NULL, *dirlink1file = NULL;
126 _cleanup_free_ char *dir2 = NULL, *dir2file = NULL;
127
b409aacb
MY
128 assert_se(path_is_mount_point_full("/", NULL, AT_SYMLINK_FOLLOW) > 0);
129 assert_se(path_is_mount_point_full("/", NULL, 0) > 0);
130 assert_se(path_is_mount_point_full("//", NULL, AT_SYMLINK_FOLLOW) > 0);
131 assert_se(path_is_mount_point_full("//", NULL, 0) > 0);
049af8ad 132
b409aacb
MY
133 assert_se(path_is_mount_point_full("/proc", NULL, AT_SYMLINK_FOLLOW) > 0);
134 assert_se(path_is_mount_point_full("/proc", NULL, 0) > 0);
135 assert_se(path_is_mount_point_full("/proc/", NULL, AT_SYMLINK_FOLLOW) > 0);
136 assert_se(path_is_mount_point_full("/proc/", NULL, 0) > 0);
049af8ad 137
b409aacb
MY
138 assert_se(path_is_mount_point_full("/proc/1", NULL, AT_SYMLINK_FOLLOW) == 0);
139 assert_se(path_is_mount_point_full("/proc/1", NULL, 0) == 0);
140 assert_se(path_is_mount_point_full("/proc/1/", NULL, AT_SYMLINK_FOLLOW) == 0);
141 assert_se(path_is_mount_point_full("/proc/1/", NULL, 0) == 0);
049af8ad 142
049af8ad
ZJS
143 /* we'll create a hierarchy of different kinds of dir/file/link
144 * layouts:
145 *
146 * <tmp>/file1, <tmp>/file2
147 * <tmp>/link1 -> file1, <tmp>/link2 -> file2
148 * <tmp>/dir1/
149 * <tmp>/dir1/file
150 * <tmp>/dirlink1 -> dir1
151 * <tmp>/dirlink1file -> dirlink1/file
152 * <tmp>/dir2/
153 * <tmp>/dir2/file
154 */
155
156 /* file mountpoints */
5f0e4d2f 157 ASSERT_NOT_NULL(mkdtemp(tmp_dir));
62a85ee0 158 file1 = path_join(tmp_dir, "file1");
049af8ad 159 assert_se(file1);
62a85ee0 160 file2 = path_join(tmp_dir, "file2");
049af8ad
ZJS
161 assert_se(file2);
162 fd = open(file1, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
163 assert_se(fd > 0);
164 close(fd);
165 fd = open(file2, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
166 assert_se(fd > 0);
167 close(fd);
62a85ee0 168 link1 = path_join(tmp_dir, "link1");
049af8ad
ZJS
169 assert_se(link1);
170 assert_se(symlink("file1", link1) == 0);
62a85ee0 171 link2 = path_join(tmp_dir, "link2");
049af8ad
ZJS
172 assert_se(link1);
173 assert_se(symlink("file2", link2) == 0);
174
b409aacb
MY
175 assert_se(path_is_mount_point_full(file1, NULL, AT_SYMLINK_FOLLOW) == 0);
176 assert_se(path_is_mount_point_full(file1, NULL, 0) == 0);
177 assert_se(path_is_mount_point_full(link1, NULL, AT_SYMLINK_FOLLOW) == 0);
178 assert_se(path_is_mount_point_full(link1, NULL, 0) == 0);
049af8ad
ZJS
179
180 /* directory mountpoints */
62a85ee0 181 dir1 = path_join(tmp_dir, "dir1");
049af8ad
ZJS
182 assert_se(dir1);
183 assert_se(mkdir(dir1, 0755) == 0);
62a85ee0 184 dirlink1 = path_join(tmp_dir, "dirlink1");
049af8ad
ZJS
185 assert_se(dirlink1);
186 assert_se(symlink("dir1", dirlink1) == 0);
62a85ee0 187 dirlink1file = path_join(tmp_dir, "dirlink1file");
049af8ad
ZJS
188 assert_se(dirlink1file);
189 assert_se(symlink("dirlink1/file", dirlink1file) == 0);
62a85ee0 190 dir2 = path_join(tmp_dir, "dir2");
049af8ad
ZJS
191 assert_se(dir2);
192 assert_se(mkdir(dir2, 0755) == 0);
193
b409aacb
MY
194 assert_se(path_is_mount_point_full(dir1, NULL, AT_SYMLINK_FOLLOW) == 0);
195 assert_se(path_is_mount_point_full(dir1, NULL, 0) == 0);
196 assert_se(path_is_mount_point_full(dirlink1, NULL, AT_SYMLINK_FOLLOW) == 0);
197 assert_se(path_is_mount_point_full(dirlink1, NULL, 0) == 0);
049af8ad
ZJS
198
199 /* file in subdirectory mountpoints */
62a85ee0 200 dir1file = path_join(dir1, "file");
049af8ad
ZJS
201 assert_se(dir1file);
202 fd = open(dir1file, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
203 assert_se(fd > 0);
204 close(fd);
205
b409aacb
MY
206 assert_se(path_is_mount_point_full(dir1file, NULL, AT_SYMLINK_FOLLOW) == 0);
207 assert_se(path_is_mount_point_full(dir1file, NULL, 0) == 0);
208 assert_se(path_is_mount_point_full(dirlink1file, NULL, AT_SYMLINK_FOLLOW) == 0);
209 assert_se(path_is_mount_point_full(dirlink1file, NULL, 0) == 0);
049af8ad
ZJS
210
211 /* these tests will only work as root */
212 if (mount(file1, file2, NULL, MS_BIND, NULL) >= 0) {
213 int rf, rt, rdf, rdt, rlf, rlt, rl1f, rl1t;
214 const char *file2d;
215
216 /* files */
217 /* capture results in vars, to avoid dangling mounts on failure */
218 log_info("%s: %s", __func__, file2);
b409aacb
MY
219 rf = path_is_mount_point_full(file2, NULL, 0);
220 rt = path_is_mount_point_full(file2, NULL, AT_SYMLINK_FOLLOW);
049af8ad
ZJS
221
222 file2d = strjoina(file2, "/");
223 log_info("%s: %s", __func__, file2d);
b409aacb
MY
224 rdf = path_is_mount_point_full(file2d, NULL, 0);
225 rdt = path_is_mount_point_full(file2d, NULL, AT_SYMLINK_FOLLOW);
049af8ad
ZJS
226
227 log_info("%s: %s", __func__, link2);
b409aacb
MY
228 rlf = path_is_mount_point_full(link2, NULL, 0);
229 rlt = path_is_mount_point_full(link2, NULL, AT_SYMLINK_FOLLOW);
049af8ad
ZJS
230
231 assert_se(umount(file2) == 0);
232
233 assert_se(rf == 1);
234 assert_se(rt == 1);
235 assert_se(rdf == -ENOTDIR);
236 assert_se(rdt == -ENOTDIR);
237 assert_se(rlf == 0);
238 assert_se(rlt == 1);
239
240 /* dirs */
62a85ee0 241 dir2file = path_join(dir2, "file");
049af8ad
ZJS
242 assert_se(dir2file);
243 fd = open(dir2file, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
244 assert_se(fd > 0);
245 close(fd);
246
247 assert_se(mount(dir2, dir1, NULL, MS_BIND, NULL) >= 0);
248
249 log_info("%s: %s", __func__, dir1);
b409aacb
MY
250 rf = path_is_mount_point_full(dir1, NULL, 0);
251 rt = path_is_mount_point_full(dir1, NULL, AT_SYMLINK_FOLLOW);
049af8ad 252 log_info("%s: %s", __func__, dirlink1);
b409aacb
MY
253 rlf = path_is_mount_point_full(dirlink1, NULL, 0);
254 rlt = path_is_mount_point_full(dirlink1, NULL, AT_SYMLINK_FOLLOW);
049af8ad
ZJS
255 log_info("%s: %s", __func__, dirlink1file);
256 /* its parent is a mount point, but not /file itself */
b409aacb
MY
257 rl1f = path_is_mount_point_full(dirlink1file, NULL, 0);
258 rl1t = path_is_mount_point_full(dirlink1file, NULL, AT_SYMLINK_FOLLOW);
049af8ad
ZJS
259
260 assert_se(umount(dir1) == 0);
261
262 assert_se(rf == 1);
263 assert_se(rt == 1);
264 assert_se(rlf == 0);
265 assert_se(rlt == 1);
266 assert_se(rl1f == 0);
267 assert_se(rl1t == 0);
268
269 } else
6c882f4f 270 log_info("Skipping bind mount file test");
049af8ad
ZJS
271
272 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
273}
274
7ce2c1bb 275TEST(is_mount_point_at) {
487ae08b 276 _cleanup_(rm_rf_physical_and_freep) char *tmpdir = NULL;
6b783209 277 _cleanup_free_ char *pwd = NULL, *tmpdir_basename = NULL;
254d1313 278 _cleanup_close_ int fd = -EBADF;
71c943dc 279 int r;
95231c72 280
95231c72
LP
281 fd = open("/", O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY);
282 assert_se(fd >= 0);
283
284 /* Not allowed, since "/" is a path, not a plain filename */
7ce2c1bb
MY
285 assert_se(is_mount_point_at(fd, "/", 0) == -EINVAL);
286 assert_se(is_mount_point_at(fd, "..", 0) == -EINVAL);
287 assert_se(is_mount_point_at(fd, "../", 0) == -EINVAL);
288 assert_se(is_mount_point_at(fd, "/proc", 0) == -EINVAL);
289 assert_se(is_mount_point_at(fd, "/proc/", 0) == -EINVAL);
290 assert_se(is_mount_point_at(fd, "proc/sys", 0) == -EINVAL);
291 assert_se(is_mount_point_at(fd, "proc/sys/", 0) == -EINVAL);
95231c72
LP
292
293 /* This one definitely is a mount point */
7ce2c1bb
MY
294 assert_se(is_mount_point_at(fd, "proc", 0) > 0);
295 assert_se(is_mount_point_at(fd, "proc/", 0) > 0);
95231c72 296
487ae08b
FB
297 safe_close(fd);
298 fd = open("/tmp", O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY);
299 assert_se(fd >= 0);
300
301 assert_se(mkdtemp_malloc("/tmp/not-mounted-XXXXXX", &tmpdir) >= 0);
6b783209
W
302 ASSERT_OK(path_extract_filename(tmpdir, &tmpdir_basename));
303 ASSERT_OK_ZERO(is_mount_point_at(fd, tmpdir_basename, 0));
304 ASSERT_OK_ZERO(is_mount_point_at(fd, strjoina(tmpdir_basename, "/"), 0));
71c943dc
LP
305
306 safe_close(fd);
307 fd = open("/proc", O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY);
308 assert_se(fd >= 0);
309
7ce2c1bb
MY
310 ASSERT_OK_POSITIVE(is_mount_point_at(fd, NULL, 0));
311 ASSERT_OK_POSITIVE(is_mount_point_at(fd, "", 0));
312 ASSERT_OK_POSITIVE(is_mount_point_at(fd, ".", 0));
313 ASSERT_OK_POSITIVE(is_mount_point_at(fd, "./", 0));
314 ASSERT_OK_ZERO(is_mount_point_at(fd, "version", 0));
e2f97c79
MY
315
316 ASSERT_OK(safe_getcwd(&pwd));
317 ASSERT_OK_ERRNO(fchdir(fd));
318
7ce2c1bb
MY
319 ASSERT_OK_POSITIVE(is_mount_point_at(AT_FDCWD, NULL, 0));
320 ASSERT_OK_POSITIVE(is_mount_point_at(AT_FDCWD, "", 0));
321 ASSERT_OK_POSITIVE(is_mount_point_at(AT_FDCWD, "./", 0));
e2f97c79
MY
322
323 ASSERT_OK_ERRNO(chdir(pwd));
71c943dc
LP
324
325 safe_close(fd);
326 fd = open("/proc/version", O_RDONLY|O_CLOEXEC|O_NOCTTY);
327 assert_se(fd >= 0);
328
7ce2c1bb 329 r = is_mount_point_at(fd, NULL, 0);
71c943dc 330 assert_se(IN_SET(r, 0, -ENOTDIR)); /* on old kernels we can't determine if regular files are mount points if we have no directory fd */
b917e5b0
MY
331
332 if (!mount_new_api_supported())
333 return;
334
335 /* Symlinks can be mount points with new mount API */
336 _cleanup_close_ int mfd = -EBADF, rfd = -EBADF;
337 _cleanup_free_ char *t = NULL;
338 struct stat st;
339
340 safe_close(fd);
341 ASSERT_OK_ERRNO(fd = open(tmpdir, O_DIRECTORY|O_PATH|O_CLOEXEC));
342
343 ASSERT_OK_ERRNO(symlinkat("/usr", fd, "symlink"));
344
345 mfd = open_tree(fd, "symlink", AT_SYMLINK_NOFOLLOW|OPEN_TREE_CLONE|OPEN_TREE_CLOEXEC);
346 if (mfd < 0 && ERRNO_IS_PRIVILEGE(errno))
347 return;
348 ASSERT_OK_ERRNO(mfd);
349
350 ASSERT_OK_ERRNO(rfd = openat(fd, "regular", O_CLOEXEC|O_CREAT|O_EXCL, 0644));
351
352 ASSERT_OK_ERRNO(move_mount(mfd, "", rfd, "", MOVE_MOUNT_F_EMPTY_PATH|MOVE_MOUNT_T_EMPTY_PATH));
353
354 ASSERT_OK_ERRNO(fstatat(fd, "regular", &st, AT_SYMLINK_NOFOLLOW));
355 ASSERT_OK(stat_verify_symlink(&st));
356 ASSERT_OK(readlinkat_malloc(fd, "regular", &t));
357 ASSERT_STREQ(t, "/usr");
358
7ce2c1bb 359 ASSERT_OK(is_mount_point_at(fd, "regular", 0));
95231c72
LP
360}
361
7d3b157b
LP
362TEST(ms_nosymfollow_supported) {
363 log_info("MS_NOSYMFOLLOW supported: %s", yes_no(ms_nosymfollow_supported()));
364}
365
117e7034
LP
366TEST(mount_option_supported) {
367 int r;
368
369 r = mount_option_supported("tmpfs", "size", "64M");
caf8495e 370 log_info("tmpfs supports size=64M: %s (%i)", r < 0 ? "don't know" : yes_no(r), r);
6f8b3838 371 assert_se(r > 0 || r == -EAGAIN || ERRNO_IS_NEG_PRIVILEGE(r));
117e7034
LP
372
373 r = mount_option_supported("ext4", "discard", NULL);
caf8495e 374 log_info("ext4 supports discard: %s (%i)", r < 0 ? "don't know" : yes_no(r), r);
6f8b3838 375 assert_se(r > 0 || r == -EAGAIN || ERRNO_IS_NEG_PRIVILEGE(r));
117e7034
LP
376
377 r = mount_option_supported("tmpfs", "idontexist", "64M");
caf8495e 378 log_info("tmpfs supports idontexist: %s (%i)", r < 0 ? "don't know" : yes_no(r), r);
6f8b3838 379 assert_se(IN_SET(r, 0, -EAGAIN) || ERRNO_IS_NEG_PRIVILEGE(r));
117e7034
LP
380
381 r = mount_option_supported("tmpfs", "ialsodontexist", NULL);
caf8495e 382 log_info("tmpfs supports ialsodontexist: %s (%i)", r < 0 ? "don't know" : yes_no(r), r);
6f8b3838 383 assert_se(IN_SET(r, 0, -EAGAIN) || ERRNO_IS_NEG_PRIVILEGE(r));
117e7034
LP
384
385 r = mount_option_supported("proc", "hidepid", "1");
caf8495e 386 log_info("proc supports hidepid=1: %s (%i)", r < 0 ? "don't know" : yes_no(r), r);
6f8b3838 387 assert_se(r >= 0 || r == -EAGAIN || ERRNO_IS_NEG_PRIVILEGE(r));
117e7034
LP
388}
389
4e6d305a
LP
390TEST(fstype_can_discard) {
391 assert_se(fstype_can_discard("ext4"));
392 assert_se(!fstype_can_discard("squashfs"));
393 assert_se(!fstype_can_discard("iso9660"));
394}
395
034ebc47 396TEST(fstype_can_norecovery) {
e3828d71
DDM
397 ASSERT_STREQ(fstype_norecovery_option("ext4"), "norecovery");
398 ASSERT_NULL(fstype_norecovery_option("vfat"));
399 ASSERT_NULL(fstype_norecovery_option("tmpfs"));
034ebc47
LP
400}
401
96963e56 402TEST(fstype_can_fmask_dmask) {
403 assert_se(fstype_can_fmask_dmask("vfat"));
404 assert_se(!fstype_can_fmask_dmask("tmpfs"));
6eda6f7e
LP
405}
406
c8ab89e5
LP
407TEST(path_get_mnt_id_at_null) {
408 _cleanup_close_ int root_fd = -EBADF, run_fd = -EBADF;
409 int id1, id2;
410
411 assert_se(path_get_mnt_id_at(AT_FDCWD, "/run/", &id1) >= 0);
412 assert_se(id1 > 0);
413
414 assert_se(path_get_mnt_id_at(AT_FDCWD, "/run", &id2) >= 0);
415 assert_se(id1 == id2);
416 id2 = -1;
417
418 root_fd = open("/", O_DIRECTORY|O_CLOEXEC);
419 assert_se(root_fd >= 0);
420
421 assert_se(path_get_mnt_id_at(root_fd, "/run/", &id2) >= 0);
422 assert_se(id1 = id2);
423 id2 = -1;
424
425 assert_se(path_get_mnt_id_at(root_fd, "/run", &id2) >= 0);
426 assert_se(id1 = id2);
427 id2 = -1;
428
429 assert_se(path_get_mnt_id_at(root_fd, "run", &id2) >= 0);
430 assert_se(id1 = id2);
431 id2 = -1;
432
433 assert_se(path_get_mnt_id_at(root_fd, "run/", &id2) >= 0);
434 assert_se(id1 = id2);
435 id2 = -1;
436
437 run_fd = openat(root_fd, "run", O_DIRECTORY|O_CLOEXEC);
438 assert_se(run_fd >= 0);
439
440 id2 = -1;
441 assert_se(path_get_mnt_id_at(run_fd, "", &id2) >= 0);
442 assert_se(id1 = id2);
443 id2 = -1;
444
445 assert_se(path_get_mnt_id_at(run_fd, NULL, &id2) >= 0);
446 assert_se(id1 = id2);
447 id2 = -1;
448
449 assert_se(path_get_mnt_id_at(run_fd, ".", &id2) >= 0);
450 assert_se(id1 = id2);
451 id2 = -1;
452}
453
99839c7e
LP
454static int intro(void) {
455 /* let's move into our own mount namespace with all propagation from the host turned off, so
456 * that /proc/self/mountinfo is static and constant for the whole time our test runs. */
457
458 if (unshare(CLONE_NEWNS) < 0) {
459 if (!ERRNO_IS_PRIVILEGE(errno))
460 return log_error_errno(errno, "Failed to detach mount namespace: %m");
461
462 log_notice("Lacking privilege to create separate mount namespace, proceeding in originating mount namespace.");
463 } else
464 assert_se(mount(NULL, "/", NULL, MS_PRIVATE | MS_REC, NULL) >= 0);
465
466 return EXIT_SUCCESS;
467}
468
e85fdacc 469DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro);