]>
git.proxmox.com Git - systemd.git/blob - src/test/test-mountpoint-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
12 #include "mountpoint-util.h"
13 #include "path-util.h"
15 #include "string-util.h"
18 static void test_mount_propagation_flags(const char *name
, int ret
, unsigned long expected
) {
21 log_info("/* %s(%s) */", __func__
, name
);
23 assert_se(mount_propagation_flags_from_string(name
, &flags
) == ret
);
28 assert_se(flags
== expected
);
30 c
= mount_propagation_flags_to_string(flags
);
32 assert_se(isempty(c
));
34 assert_se(streq(c
, name
));
38 static void test_mnt_id(void) {
39 _cleanup_fclose_
FILE *f
= NULL
;
46 log_info("/* %s */", __func__
);
48 assert_se(f
= fopen("/proc/self/mountinfo", "re"));
49 assert_se(h
= hashmap_new(&trivial_hash_ops
));
52 _cleanup_free_
char *line
= NULL
, *path
= NULL
;
55 r
= read_line(f
, LONG_LINE_MAX
, &line
);
60 assert_se(sscanf(line
, "%i %*s %*s %*s %ms", &mnt_id
, &path
) == 2);
62 log_debug("mountinfo: %s → %i", path
, mnt_id
);
64 assert_se(hashmap_put(h
, INT_TO_PTR(mnt_id
), path
) >= 0);
68 HASHMAP_FOREACH_KEY(p
, k
, h
, i
) {
69 int mnt_id
= PTR_TO_INT(k
), mnt_id2
;
71 r
= path_get_mnt_id(p
, &mnt_id2
);
73 log_debug_errno(r
, "Failed to get the mnt id of %s: %m\n", p
);
77 log_debug("mnt ids of %s are %i, %i\n", p
, mnt_id
, mnt_id2
);
79 if (mnt_id
== mnt_id2
)
82 /* The ids don't match? If so, then there are two mounts on the same path, let's check if
83 * that's really the case */
84 char *t
= hashmap_get(h
, INT_TO_PTR(mnt_id2
));
85 log_debug("the other path for mnt id %i is %s\n", mnt_id2
, t
);
86 assert_se(path_equal(p
, t
));
92 static void test_path_is_mount_point(void) {
94 char tmp_dir
[] = "/tmp/test-path-is-mount-point-XXXXXX";
95 _cleanup_free_
char *file1
= NULL
, *file2
= NULL
, *link1
= NULL
, *link2
= NULL
;
96 _cleanup_free_
char *dir1
= NULL
, *dir1file
= NULL
, *dirlink1
= NULL
, *dirlink1file
= NULL
;
97 _cleanup_free_
char *dir2
= NULL
, *dir2file
= NULL
;
99 log_info("/* %s */", __func__
);
101 assert_se(path_is_mount_point("/", NULL
, AT_SYMLINK_FOLLOW
) > 0);
102 assert_se(path_is_mount_point("/", NULL
, 0) > 0);
103 assert_se(path_is_mount_point("//", NULL
, AT_SYMLINK_FOLLOW
) > 0);
104 assert_se(path_is_mount_point("//", NULL
, 0) > 0);
106 assert_se(path_is_mount_point("/proc", NULL
, AT_SYMLINK_FOLLOW
) > 0);
107 assert_se(path_is_mount_point("/proc", NULL
, 0) > 0);
108 assert_se(path_is_mount_point("/proc/", NULL
, AT_SYMLINK_FOLLOW
) > 0);
109 assert_se(path_is_mount_point("/proc/", NULL
, 0) > 0);
111 assert_se(path_is_mount_point("/proc/1", NULL
, AT_SYMLINK_FOLLOW
) == 0);
112 assert_se(path_is_mount_point("/proc/1", NULL
, 0) == 0);
113 assert_se(path_is_mount_point("/proc/1/", NULL
, AT_SYMLINK_FOLLOW
) == 0);
114 assert_se(path_is_mount_point("/proc/1/", NULL
, 0) == 0);
116 assert_se(path_is_mount_point("/sys", NULL
, AT_SYMLINK_FOLLOW
) > 0);
117 assert_se(path_is_mount_point("/sys", NULL
, 0) > 0);
118 assert_se(path_is_mount_point("/sys/", NULL
, AT_SYMLINK_FOLLOW
) > 0);
119 assert_se(path_is_mount_point("/sys/", NULL
, 0) > 0);
121 /* we'll create a hierarchy of different kinds of dir/file/link
124 * <tmp>/file1, <tmp>/file2
125 * <tmp>/link1 -> file1, <tmp>/link2 -> file2
128 * <tmp>/dirlink1 -> dir1
129 * <tmp>/dirlink1file -> dirlink1/file
134 /* file mountpoints */
135 assert_se(mkdtemp(tmp_dir
) != NULL
);
136 file1
= path_join(tmp_dir
, "file1");
138 file2
= path_join(tmp_dir
, "file2");
140 fd
= open(file1
, O_WRONLY
|O_CREAT
|O_EXCL
|O_CLOEXEC
, 0664);
143 fd
= open(file2
, O_WRONLY
|O_CREAT
|O_EXCL
|O_CLOEXEC
, 0664);
146 link1
= path_join(tmp_dir
, "link1");
148 assert_se(symlink("file1", link1
) == 0);
149 link2
= path_join(tmp_dir
, "link2");
151 assert_se(symlink("file2", link2
) == 0);
153 assert_se(path_is_mount_point(file1
, NULL
, AT_SYMLINK_FOLLOW
) == 0);
154 assert_se(path_is_mount_point(file1
, NULL
, 0) == 0);
155 assert_se(path_is_mount_point(link1
, NULL
, AT_SYMLINK_FOLLOW
) == 0);
156 assert_se(path_is_mount_point(link1
, NULL
, 0) == 0);
158 /* directory mountpoints */
159 dir1
= path_join(tmp_dir
, "dir1");
161 assert_se(mkdir(dir1
, 0755) == 0);
162 dirlink1
= path_join(tmp_dir
, "dirlink1");
164 assert_se(symlink("dir1", dirlink1
) == 0);
165 dirlink1file
= path_join(tmp_dir
, "dirlink1file");
166 assert_se(dirlink1file
);
167 assert_se(symlink("dirlink1/file", dirlink1file
) == 0);
168 dir2
= path_join(tmp_dir
, "dir2");
170 assert_se(mkdir(dir2
, 0755) == 0);
172 assert_se(path_is_mount_point(dir1
, NULL
, AT_SYMLINK_FOLLOW
) == 0);
173 assert_se(path_is_mount_point(dir1
, NULL
, 0) == 0);
174 assert_se(path_is_mount_point(dirlink1
, NULL
, AT_SYMLINK_FOLLOW
) == 0);
175 assert_se(path_is_mount_point(dirlink1
, NULL
, 0) == 0);
177 /* file in subdirectory mountpoints */
178 dir1file
= path_join(dir1
, "file");
180 fd
= open(dir1file
, O_WRONLY
|O_CREAT
|O_EXCL
|O_CLOEXEC
, 0664);
184 assert_se(path_is_mount_point(dir1file
, NULL
, AT_SYMLINK_FOLLOW
) == 0);
185 assert_se(path_is_mount_point(dir1file
, NULL
, 0) == 0);
186 assert_se(path_is_mount_point(dirlink1file
, NULL
, AT_SYMLINK_FOLLOW
) == 0);
187 assert_se(path_is_mount_point(dirlink1file
, NULL
, 0) == 0);
189 /* these tests will only work as root */
190 if (mount(file1
, file2
, NULL
, MS_BIND
, NULL
) >= 0) {
191 int rf
, rt
, rdf
, rdt
, rlf
, rlt
, rl1f
, rl1t
;
195 /* capture results in vars, to avoid dangling mounts on failure */
196 log_info("%s: %s", __func__
, file2
);
197 rf
= path_is_mount_point(file2
, NULL
, 0);
198 rt
= path_is_mount_point(file2
, NULL
, AT_SYMLINK_FOLLOW
);
200 file2d
= strjoina(file2
, "/");
201 log_info("%s: %s", __func__
, file2d
);
202 rdf
= path_is_mount_point(file2d
, NULL
, 0);
203 rdt
= path_is_mount_point(file2d
, NULL
, AT_SYMLINK_FOLLOW
);
205 log_info("%s: %s", __func__
, link2
);
206 rlf
= path_is_mount_point(link2
, NULL
, 0);
207 rlt
= path_is_mount_point(link2
, NULL
, AT_SYMLINK_FOLLOW
);
209 assert_se(umount(file2
) == 0);
213 assert_se(rdf
== -ENOTDIR
);
214 assert_se(rdt
== -ENOTDIR
);
219 dir2file
= path_join(dir2
, "file");
221 fd
= open(dir2file
, O_WRONLY
|O_CREAT
|O_EXCL
|O_CLOEXEC
, 0664);
225 assert_se(mount(dir2
, dir1
, NULL
, MS_BIND
, NULL
) >= 0);
227 log_info("%s: %s", __func__
, dir1
);
228 rf
= path_is_mount_point(dir1
, NULL
, 0);
229 rt
= path_is_mount_point(dir1
, NULL
, AT_SYMLINK_FOLLOW
);
230 log_info("%s: %s", __func__
, dirlink1
);
231 rlf
= path_is_mount_point(dirlink1
, NULL
, 0);
232 rlt
= path_is_mount_point(dirlink1
, NULL
, AT_SYMLINK_FOLLOW
);
233 log_info("%s: %s", __func__
, dirlink1file
);
234 /* its parent is a mount point, but not /file itself */
235 rl1f
= path_is_mount_point(dirlink1file
, NULL
, 0);
236 rl1t
= path_is_mount_point(dirlink1file
, NULL
, AT_SYMLINK_FOLLOW
);
238 assert_se(umount(dir1
) == 0);
244 assert_se(rl1f
== 0);
245 assert_se(rl1t
== 0);
248 printf("Skipping bind mount file test: %m\n");
250 assert_se(rm_rf(tmp_dir
, REMOVE_ROOT
|REMOVE_PHYSICAL
) == 0);
253 int main(int argc
, char *argv
[]) {
254 test_setup_logging(LOG_DEBUG
);
256 test_mount_propagation_flags("shared", 0, MS_SHARED
);
257 test_mount_propagation_flags("slave", 0, MS_SLAVE
);
258 test_mount_propagation_flags("private", 0, MS_PRIVATE
);
259 test_mount_propagation_flags(NULL
, 0, 0);
260 test_mount_propagation_flags("", 0, 0);
261 test_mount_propagation_flags("xxxx", -EINVAL
, 0);
262 test_mount_propagation_flags(" ", -EINVAL
, 0);
265 test_path_is_mount_point();