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