]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-mount-util.c
e2765d0c912805b0bd4aa518e0a463b8b68dbd3b
[thirdparty/systemd.git] / src / test / test-mount-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright 2016 Lennart Poettering
4 ***/
5
6 #include <sys/mount.h>
7
8 #include "alloc-util.h"
9 #include "def.h"
10 #include "fd-util.h"
11 #include "fileio.h"
12 #include "hashmap.h"
13 #include "log.h"
14 #include "log.h"
15 #include "mount-util.h"
16 #include "path-util.h"
17 #include "rm-rf.h"
18 #include "string-util.h"
19
20 static void test_mount_propagation_flags(const char *name, int ret, unsigned long expected) {
21 long unsigned flags;
22
23 assert_se(mount_propagation_flags_from_string(name, &flags) == ret);
24
25 if (ret >= 0) {
26 const char *c;
27
28 assert_se(flags == expected);
29
30 c = mount_propagation_flags_to_string(flags);
31 if (isempty(name))
32 assert_se(isempty(c));
33 else
34 assert_se(streq(c, name));
35 }
36 }
37
38 static void test_mnt_id(void) {
39 _cleanup_fclose_ FILE *f = NULL;
40 Hashmap *h;
41 Iterator i;
42 char *p;
43 void *k;
44 int r;
45
46 assert_se(f = fopen("/proc/self/mountinfo", "re"));
47 assert_se(h = hashmap_new(&trivial_hash_ops));
48
49 for (;;) {
50 _cleanup_free_ char *line = NULL, *path = NULL;
51 int mnt_id;
52
53 r = read_line(f, LONG_LINE_MAX, &line);
54 if (r == 0)
55 break;
56 assert_se(r > 0);
57
58 assert_se(sscanf(line, "%i %*s %*s %*s %ms", &mnt_id, &path) == 2);
59
60 assert_se(hashmap_put(h, INT_TO_PTR(mnt_id), path) >= 0);
61 path = NULL;
62 }
63
64 HASHMAP_FOREACH_KEY(p, k, h, i) {
65 int mnt_id = PTR_TO_INT(k), mnt_id2;
66
67 r = path_get_mnt_id(p, &mnt_id2);
68 if (r < 0) {
69 log_debug_errno(r, "Failed to get the mnt id of %s: %m\n", p);
70 continue;
71 }
72
73 log_debug("mnt id of %s is %i\n", p, mnt_id2);
74
75 if (mnt_id == mnt_id2)
76 continue;
77
78 /* The ids don't match? If so, then there are two mounts on the same path, let's check if that's really
79 * the case */
80 assert_se(path_equal_ptr(hashmap_get(h, INT_TO_PTR(mnt_id2)), p));
81 }
82
83 hashmap_free_free(h);
84 }
85
86 static void test_path_is_mount_point(void) {
87 int fd;
88 char tmp_dir[] = "/tmp/test-path-is-mount-point-XXXXXX";
89 _cleanup_free_ char *file1 = NULL, *file2 = NULL, *link1 = NULL, *link2 = NULL;
90 _cleanup_free_ char *dir1 = NULL, *dir1file = NULL, *dirlink1 = NULL, *dirlink1file = NULL;
91 _cleanup_free_ char *dir2 = NULL, *dir2file = NULL;
92
93 assert_se(path_is_mount_point("/", NULL, AT_SYMLINK_FOLLOW) > 0);
94 assert_se(path_is_mount_point("/", NULL, 0) > 0);
95 assert_se(path_is_mount_point("//", NULL, AT_SYMLINK_FOLLOW) > 0);
96 assert_se(path_is_mount_point("//", NULL, 0) > 0);
97
98 assert_se(path_is_mount_point("/proc", NULL, AT_SYMLINK_FOLLOW) > 0);
99 assert_se(path_is_mount_point("/proc", NULL, 0) > 0);
100 assert_se(path_is_mount_point("/proc/", NULL, AT_SYMLINK_FOLLOW) > 0);
101 assert_se(path_is_mount_point("/proc/", NULL, 0) > 0);
102
103 assert_se(path_is_mount_point("/proc/1", NULL, AT_SYMLINK_FOLLOW) == 0);
104 assert_se(path_is_mount_point("/proc/1", NULL, 0) == 0);
105 assert_se(path_is_mount_point("/proc/1/", NULL, AT_SYMLINK_FOLLOW) == 0);
106 assert_se(path_is_mount_point("/proc/1/", NULL, 0) == 0);
107
108 assert_se(path_is_mount_point("/sys", NULL, AT_SYMLINK_FOLLOW) > 0);
109 assert_se(path_is_mount_point("/sys", NULL, 0) > 0);
110 assert_se(path_is_mount_point("/sys/", NULL, AT_SYMLINK_FOLLOW) > 0);
111 assert_se(path_is_mount_point("/sys/", NULL, 0) > 0);
112
113 /* we'll create a hierarchy of different kinds of dir/file/link
114 * layouts:
115 *
116 * <tmp>/file1, <tmp>/file2
117 * <tmp>/link1 -> file1, <tmp>/link2 -> file2
118 * <tmp>/dir1/
119 * <tmp>/dir1/file
120 * <tmp>/dirlink1 -> dir1
121 * <tmp>/dirlink1file -> dirlink1/file
122 * <tmp>/dir2/
123 * <tmp>/dir2/file
124 */
125
126 /* file mountpoints */
127 assert_se(mkdtemp(tmp_dir) != NULL);
128 file1 = path_join(NULL, tmp_dir, "file1");
129 assert_se(file1);
130 file2 = path_join(NULL, tmp_dir, "file2");
131 assert_se(file2);
132 fd = open(file1, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
133 assert_se(fd > 0);
134 close(fd);
135 fd = open(file2, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
136 assert_se(fd > 0);
137 close(fd);
138 link1 = path_join(NULL, tmp_dir, "link1");
139 assert_se(link1);
140 assert_se(symlink("file1", link1) == 0);
141 link2 = path_join(NULL, tmp_dir, "link2");
142 assert_se(link1);
143 assert_se(symlink("file2", link2) == 0);
144
145 assert_se(path_is_mount_point(file1, NULL, AT_SYMLINK_FOLLOW) == 0);
146 assert_se(path_is_mount_point(file1, NULL, 0) == 0);
147 assert_se(path_is_mount_point(link1, NULL, AT_SYMLINK_FOLLOW) == 0);
148 assert_se(path_is_mount_point(link1, NULL, 0) == 0);
149
150 /* directory mountpoints */
151 dir1 = path_join(NULL, tmp_dir, "dir1");
152 assert_se(dir1);
153 assert_se(mkdir(dir1, 0755) == 0);
154 dirlink1 = path_join(NULL, tmp_dir, "dirlink1");
155 assert_se(dirlink1);
156 assert_se(symlink("dir1", dirlink1) == 0);
157 dirlink1file = path_join(NULL, tmp_dir, "dirlink1file");
158 assert_se(dirlink1file);
159 assert_se(symlink("dirlink1/file", dirlink1file) == 0);
160 dir2 = path_join(NULL, tmp_dir, "dir2");
161 assert_se(dir2);
162 assert_se(mkdir(dir2, 0755) == 0);
163
164 assert_se(path_is_mount_point(dir1, NULL, AT_SYMLINK_FOLLOW) == 0);
165 assert_se(path_is_mount_point(dir1, NULL, 0) == 0);
166 assert_se(path_is_mount_point(dirlink1, NULL, AT_SYMLINK_FOLLOW) == 0);
167 assert_se(path_is_mount_point(dirlink1, NULL, 0) == 0);
168
169 /* file in subdirectory mountpoints */
170 dir1file = path_join(NULL, dir1, "file");
171 assert_se(dir1file);
172 fd = open(dir1file, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
173 assert_se(fd > 0);
174 close(fd);
175
176 assert_se(path_is_mount_point(dir1file, NULL, AT_SYMLINK_FOLLOW) == 0);
177 assert_se(path_is_mount_point(dir1file, NULL, 0) == 0);
178 assert_se(path_is_mount_point(dirlink1file, NULL, AT_SYMLINK_FOLLOW) == 0);
179 assert_se(path_is_mount_point(dirlink1file, NULL, 0) == 0);
180
181 /* these tests will only work as root */
182 if (mount(file1, file2, NULL, MS_BIND, NULL) >= 0) {
183 int rf, rt, rdf, rdt, rlf, rlt, rl1f, rl1t;
184 const char *file2d;
185
186 /* files */
187 /* capture results in vars, to avoid dangling mounts on failure */
188 log_info("%s: %s", __func__, file2);
189 rf = path_is_mount_point(file2, NULL, 0);
190 rt = path_is_mount_point(file2, NULL, AT_SYMLINK_FOLLOW);
191
192 file2d = strjoina(file2, "/");
193 log_info("%s: %s", __func__, file2d);
194 rdf = path_is_mount_point(file2d, NULL, 0);
195 rdt = path_is_mount_point(file2d, NULL, AT_SYMLINK_FOLLOW);
196
197 log_info("%s: %s", __func__, link2);
198 rlf = path_is_mount_point(link2, NULL, 0);
199 rlt = path_is_mount_point(link2, NULL, AT_SYMLINK_FOLLOW);
200
201 assert_se(umount(file2) == 0);
202
203 assert_se(rf == 1);
204 assert_se(rt == 1);
205 assert_se(rdf == -ENOTDIR);
206 assert_se(rdt == -ENOTDIR);
207 assert_se(rlf == 0);
208 assert_se(rlt == 1);
209
210 /* dirs */
211 dir2file = path_join(NULL, dir2, "file");
212 assert_se(dir2file);
213 fd = open(dir2file, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664);
214 assert_se(fd > 0);
215 close(fd);
216
217 assert_se(mount(dir2, dir1, NULL, MS_BIND, NULL) >= 0);
218
219 log_info("%s: %s", __func__, dir1);
220 rf = path_is_mount_point(dir1, NULL, 0);
221 rt = path_is_mount_point(dir1, NULL, AT_SYMLINK_FOLLOW);
222 log_info("%s: %s", __func__, dirlink1);
223 rlf = path_is_mount_point(dirlink1, NULL, 0);
224 rlt = path_is_mount_point(dirlink1, NULL, AT_SYMLINK_FOLLOW);
225 log_info("%s: %s", __func__, dirlink1file);
226 /* its parent is a mount point, but not /file itself */
227 rl1f = path_is_mount_point(dirlink1file, NULL, 0);
228 rl1t = path_is_mount_point(dirlink1file, NULL, AT_SYMLINK_FOLLOW);
229
230 assert_se(umount(dir1) == 0);
231
232 assert_se(rf == 1);
233 assert_se(rt == 1);
234 assert_se(rlf == 0);
235 assert_se(rlt == 1);
236 assert_se(rl1f == 0);
237 assert_se(rl1t == 0);
238
239 } else
240 printf("Skipping bind mount file test: %m\n");
241
242 assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
243 }
244
245 static void test_mount_option_mangle(void) {
246 char *opts = NULL;
247 unsigned long f;
248
249 assert_se(mount_option_mangle(NULL, MS_RDONLY|MS_NOSUID, &f, &opts) == 0);
250 assert_se(f == (MS_RDONLY|MS_NOSUID));
251 assert_se(opts == NULL);
252
253 assert_se(mount_option_mangle("", MS_RDONLY|MS_NOSUID, &f, &opts) == 0);
254 assert_se(f == (MS_RDONLY|MS_NOSUID));
255 assert_se(opts == NULL);
256
257 assert_se(mount_option_mangle("ro,nosuid,nodev,noexec", 0, &f, &opts) == 0);
258 assert_se(f == (MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC));
259 assert_se(opts == NULL);
260
261 assert_se(mount_option_mangle("ro,nosuid,nodev,noexec,mode=755", 0, &f, &opts) == 0);
262 assert_se(f == (MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC));
263 assert_se(streq(opts, "mode=755"));
264 opts = mfree(opts);
265
266 assert_se(mount_option_mangle("rw,nosuid,foo,hogehoge,nodev,mode=755", 0, &f, &opts) == 0);
267 assert_se(f == (MS_NOSUID|MS_NODEV));
268 assert_se(streq(opts, "foo,hogehoge,mode=755"));
269 opts = mfree(opts);
270
271 assert_se(mount_option_mangle("rw,nosuid,nodev,noexec,relatime,net_cls,net_prio", MS_RDONLY, &f, &opts) == 0);
272 assert_se(f == (MS_NOSUID|MS_NODEV|MS_NOEXEC|MS_RELATIME));
273 assert_se(streq(opts, "net_cls,net_prio"));
274 opts = mfree(opts);
275
276 assert_se(mount_option_mangle("rw,nosuid,nodev,relatime,size=1630748k,mode=700,uid=1000,gid=1000", MS_RDONLY, &f, &opts) == 0);
277 assert_se(f == (MS_NOSUID|MS_NODEV|MS_RELATIME));
278 assert_se(streq(opts, "size=1630748k,mode=700,uid=1000,gid=1000"));
279 opts = mfree(opts);
280
281 assert_se(mount_option_mangle("size=1630748k,rw,gid=1000,,,nodev,relatime,,mode=700,nosuid,uid=1000", MS_RDONLY, &f, &opts) == 0);
282 assert_se(f == (MS_NOSUID|MS_NODEV|MS_RELATIME));
283 assert_se(streq(opts, "size=1630748k,gid=1000,mode=700,uid=1000"));
284 opts = mfree(opts);
285
286 assert_se(mount_option_mangle("rw,exec,size=8143984k,nr_inodes=2035996,mode=755", MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV, &f, &opts) == 0);
287 assert_se(f == (MS_NOSUID|MS_NODEV));
288 assert_se(streq(opts, "size=8143984k,nr_inodes=2035996,mode=755"));
289 opts = mfree(opts);
290
291 assert_se(mount_option_mangle("rw,relatime,fmask=0022,,,dmask=0022", MS_RDONLY, &f, &opts) == 0);
292 assert_se(f == MS_RELATIME);
293 assert_se(streq(opts, "fmask=0022,dmask=0022"));
294 opts = mfree(opts);
295
296 assert_se(mount_option_mangle("rw,relatime,fmask=0022,dmask=0022,\"hogehoge", MS_RDONLY, &f, &opts) < 0);
297 }
298
299 int main(int argc, char *argv[]) {
300
301 log_set_max_level(LOG_DEBUG);
302
303 test_mount_propagation_flags("shared", 0, MS_SHARED);
304 test_mount_propagation_flags("slave", 0, MS_SLAVE);
305 test_mount_propagation_flags("private", 0, MS_PRIVATE);
306 test_mount_propagation_flags(NULL, 0, 0);
307 test_mount_propagation_flags("", 0, 0);
308 test_mount_propagation_flags("xxxx", -EINVAL, 0);
309 test_mount_propagation_flags(" ", -EINVAL, 0);
310
311 test_mnt_id();
312 test_path_is_mount_point();
313 test_mount_option_mangle();
314
315 return 0;
316 }