]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
83555251 LP |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2016 Lennart Poettering | |
6 | ||
7 | systemd is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU Lesser General Public License as published by | |
9 | the Free Software Foundation; either version 2.1 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | systemd is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public License | |
18 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
19 | ***/ | |
20 | ||
21 | #include <sys/mount.h> | |
22 | ||
c2a986d5 LP |
23 | #include "alloc-util.h" |
24 | #include "def.h" | |
25 | #include "fd-util.h" | |
26 | #include "fileio.h" | |
27 | #include "hashmap.h" | |
83555251 | 28 | #include "log.h" |
ee3467c6 | 29 | #include "log.h" |
83555251 | 30 | #include "mount-util.h" |
9b1573ef | 31 | #include "path-util.h" |
ee3467c6 | 32 | #include "rm-rf.h" |
83555251 LP |
33 | #include "string-util.h" |
34 | ||
c7383828 ZJS |
35 | static void test_mount_propagation_flags(const char *name, int ret, unsigned long expected) { |
36 | long unsigned flags; | |
83555251 | 37 | |
85e55d14 | 38 | assert_se(mount_propagation_flags_from_string(name, &flags) == ret); |
c7383828 ZJS |
39 | |
40 | if (ret >= 0) { | |
41 | const char *c; | |
42 | ||
43 | assert_se(flags == expected); | |
44 | ||
45 | c = mount_propagation_flags_to_string(flags); | |
46 | if (isempty(name)) | |
47 | assert_se(isempty(c)); | |
48 | else | |
49 | assert_se(streq(c, name)); | |
50 | } | |
83555251 LP |
51 | } |
52 | ||
c2a986d5 LP |
53 | static void test_mnt_id(void) { |
54 | _cleanup_fclose_ FILE *f = NULL; | |
55 | Hashmap *h; | |
56 | Iterator i; | |
9b1573ef LP |
57 | char *p; |
58 | void *k; | |
c2a986d5 LP |
59 | int r; |
60 | ||
61 | assert_se(f = fopen("/proc/self/mountinfo", "re")); | |
9b1573ef | 62 | assert_se(h = hashmap_new(&trivial_hash_ops)); |
c2a986d5 LP |
63 | |
64 | for (;;) { | |
65 | _cleanup_free_ char *line = NULL, *path = NULL; | |
c2a986d5 LP |
66 | int mnt_id; |
67 | ||
68 | r = read_line(f, LONG_LINE_MAX, &line); | |
69 | if (r == 0) | |
70 | break; | |
71 | assert_se(r > 0); | |
72 | ||
73 | assert_se(sscanf(line, "%i %*s %*s %*s %ms", &mnt_id, &path) == 2); | |
74 | ||
9b1573ef | 75 | assert_se(hashmap_put(h, INT_TO_PTR(mnt_id), path) >= 0); |
c2a986d5 LP |
76 | path = NULL; |
77 | } | |
78 | ||
79 | HASHMAP_FOREACH_KEY(p, k, h, i) { | |
9b1573ef | 80 | int mnt_id = PTR_TO_INT(k), mnt_id2; |
c2a986d5 | 81 | |
9b1573ef | 82 | r = path_get_mnt_id(p, &mnt_id2); |
c2a986d5 | 83 | if (r == -EOPNOTSUPP) { /* kernel or file system too old? */ |
9b1573ef | 84 | log_debug("%s doesn't support mount IDs\n", p); |
c2a986d5 LP |
85 | continue; |
86 | } | |
87 | if (IN_SET(r, -EACCES, -EPERM)) { | |
9b1573ef | 88 | log_debug("Can't access %s\n", p); |
c2a986d5 LP |
89 | continue; |
90 | } | |
91 | ||
9b1573ef LP |
92 | log_debug("mnt id of %s is %i\n", p, mnt_id2); |
93 | ||
94 | if (mnt_id == mnt_id2) | |
95 | continue; | |
c2a986d5 | 96 | |
9b1573ef LP |
97 | /* The ids don't match? If so, then there are two mounts on the same path, let's check if that's really |
98 | * the case */ | |
99 | assert_se(path_equal_ptr(hashmap_get(h, INT_TO_PTR(mnt_id2)), p)); | |
c2a986d5 LP |
100 | } |
101 | ||
ec1d2909 | 102 | hashmap_free_free(h); |
c2a986d5 LP |
103 | } |
104 | ||
ee3467c6 ZJS |
105 | static void test_path_is_mount_point(void) { |
106 | int fd; | |
107 | char tmp_dir[] = "/tmp/test-path-is-mount-point-XXXXXX"; | |
108 | _cleanup_free_ char *file1 = NULL, *file2 = NULL, *link1 = NULL, *link2 = NULL; | |
109 | _cleanup_free_ char *dir1 = NULL, *dir1file = NULL, *dirlink1 = NULL, *dirlink1file = NULL; | |
110 | _cleanup_free_ char *dir2 = NULL, *dir2file = NULL; | |
111 | ||
112 | assert_se(path_is_mount_point("/", NULL, AT_SYMLINK_FOLLOW) > 0); | |
113 | assert_se(path_is_mount_point("/", NULL, 0) > 0); | |
114 | ||
115 | assert_se(path_is_mount_point("/proc", NULL, AT_SYMLINK_FOLLOW) > 0); | |
116 | assert_se(path_is_mount_point("/proc", NULL, 0) > 0); | |
117 | ||
118 | assert_se(path_is_mount_point("/proc/1", NULL, AT_SYMLINK_FOLLOW) == 0); | |
119 | assert_se(path_is_mount_point("/proc/1", NULL, 0) == 0); | |
120 | ||
121 | assert_se(path_is_mount_point("/sys", NULL, AT_SYMLINK_FOLLOW) > 0); | |
122 | assert_se(path_is_mount_point("/sys", NULL, 0) > 0); | |
123 | ||
124 | /* we'll create a hierarchy of different kinds of dir/file/link | |
125 | * layouts: | |
126 | * | |
127 | * <tmp>/file1, <tmp>/file2 | |
128 | * <tmp>/link1 -> file1, <tmp>/link2 -> file2 | |
129 | * <tmp>/dir1/ | |
130 | * <tmp>/dir1/file | |
131 | * <tmp>/dirlink1 -> dir1 | |
132 | * <tmp>/dirlink1file -> dirlink1/file | |
133 | * <tmp>/dir2/ | |
134 | * <tmp>/dir2/file | |
135 | */ | |
136 | ||
137 | /* file mountpoints */ | |
138 | assert_se(mkdtemp(tmp_dir) != NULL); | |
139 | file1 = path_join(NULL, tmp_dir, "file1"); | |
140 | assert_se(file1); | |
141 | file2 = path_join(NULL, tmp_dir, "file2"); | |
142 | assert_se(file2); | |
143 | fd = open(file1, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664); | |
144 | assert_se(fd > 0); | |
145 | close(fd); | |
146 | fd = open(file2, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664); | |
147 | assert_se(fd > 0); | |
148 | close(fd); | |
149 | link1 = path_join(NULL, tmp_dir, "link1"); | |
150 | assert_se(link1); | |
151 | assert_se(symlink("file1", link1) == 0); | |
152 | link2 = path_join(NULL, tmp_dir, "link2"); | |
153 | assert_se(link1); | |
154 | assert_se(symlink("file2", link2) == 0); | |
155 | ||
156 | assert_se(path_is_mount_point(file1, NULL, AT_SYMLINK_FOLLOW) == 0); | |
157 | assert_se(path_is_mount_point(file1, NULL, 0) == 0); | |
158 | assert_se(path_is_mount_point(link1, NULL, AT_SYMLINK_FOLLOW) == 0); | |
159 | assert_se(path_is_mount_point(link1, NULL, 0) == 0); | |
160 | ||
161 | /* directory mountpoints */ | |
162 | dir1 = path_join(NULL, tmp_dir, "dir1"); | |
163 | assert_se(dir1); | |
164 | assert_se(mkdir(dir1, 0755) == 0); | |
165 | dirlink1 = path_join(NULL, tmp_dir, "dirlink1"); | |
166 | assert_se(dirlink1); | |
167 | assert_se(symlink("dir1", dirlink1) == 0); | |
168 | dirlink1file = path_join(NULL, tmp_dir, "dirlink1file"); | |
169 | assert_se(dirlink1file); | |
170 | assert_se(symlink("dirlink1/file", dirlink1file) == 0); | |
171 | dir2 = path_join(NULL, tmp_dir, "dir2"); | |
172 | assert_se(dir2); | |
173 | assert_se(mkdir(dir2, 0755) == 0); | |
174 | ||
175 | assert_se(path_is_mount_point(dir1, NULL, AT_SYMLINK_FOLLOW) == 0); | |
176 | assert_se(path_is_mount_point(dir1, NULL, 0) == 0); | |
177 | assert_se(path_is_mount_point(dirlink1, NULL, AT_SYMLINK_FOLLOW) == 0); | |
178 | assert_se(path_is_mount_point(dirlink1, NULL, 0) == 0); | |
179 | ||
180 | /* file in subdirectory mountpoints */ | |
181 | dir1file = path_join(NULL, dir1, "file"); | |
182 | assert_se(dir1file); | |
183 | fd = open(dir1file, O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC, 0664); | |
184 | assert_se(fd > 0); | |
185 | close(fd); | |
186 | ||
187 | assert_se(path_is_mount_point(dir1file, NULL, AT_SYMLINK_FOLLOW) == 0); | |
188 | assert_se(path_is_mount_point(dir1file, NULL, 0) == 0); | |
189 | assert_se(path_is_mount_point(dirlink1file, NULL, AT_SYMLINK_FOLLOW) == 0); | |
190 | assert_se(path_is_mount_point(dirlink1file, NULL, 0) == 0); | |
191 | ||
192 | /* these tests will only work as root */ | |
193 | if (mount(file1, file2, NULL, MS_BIND, NULL) >= 0) { | |
194 | int rt, rf, rlt, rlf, rl1t, rl1f; | |
195 | ||
196 | /* files */ | |
197 | /* capture results in vars, to avoid dangling mounts on failure */ | |
198 | rf = path_is_mount_point(file2, NULL, 0); | |
199 | rt = path_is_mount_point(file2, NULL, AT_SYMLINK_FOLLOW); | |
200 | rlf = path_is_mount_point(link2, NULL, 0); | |
201 | rlt = path_is_mount_point(link2, NULL, AT_SYMLINK_FOLLOW); | |
202 | ||
203 | assert_se(umount(file2) == 0); | |
204 | ||
205 | assert_se(rf == 1); | |
206 | assert_se(rt == 1); | |
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 | rf = path_is_mount_point(dir1, NULL, 0); | |
220 | rt = path_is_mount_point(dir1, NULL, AT_SYMLINK_FOLLOW); | |
221 | rlf = path_is_mount_point(dirlink1, NULL, 0); | |
222 | rlt = path_is_mount_point(dirlink1, NULL, AT_SYMLINK_FOLLOW); | |
223 | /* its parent is a mount point, but not /file itself */ | |
224 | rl1f = path_is_mount_point(dirlink1file, NULL, 0); | |
225 | rl1t = path_is_mount_point(dirlink1file, NULL, AT_SYMLINK_FOLLOW); | |
226 | ||
227 | assert_se(umount(dir1) == 0); | |
228 | ||
229 | assert_se(rf == 1); | |
230 | assert_se(rt == 1); | |
231 | assert_se(rlf == 0); | |
232 | assert_se(rlt == 1); | |
233 | assert_se(rl1f == 0); | |
234 | assert_se(rl1t == 0); | |
235 | ||
236 | } else | |
237 | printf("Skipping bind mount file test: %m\n"); | |
238 | ||
239 | assert_se(rm_rf(tmp_dir, REMOVE_ROOT|REMOVE_PHYSICAL) == 0); | |
240 | } | |
241 | ||
83555251 LP |
242 | int main(int argc, char *argv[]) { |
243 | ||
244 | log_set_max_level(LOG_DEBUG); | |
245 | ||
c7383828 ZJS |
246 | test_mount_propagation_flags("shared", 0, MS_SHARED); |
247 | test_mount_propagation_flags("slave", 0, MS_SLAVE); | |
248 | test_mount_propagation_flags("private", 0, MS_PRIVATE); | |
249 | test_mount_propagation_flags(NULL, 0, 0); | |
250 | test_mount_propagation_flags("", 0, 0); | |
251 | test_mount_propagation_flags("xxxx", -EINVAL, 0); | |
252 | test_mount_propagation_flags(" ", -EINVAL, 0); | |
83555251 | 253 | |
c2a986d5 | 254 | test_mnt_id(); |
ee3467c6 | 255 | test_path_is_mount_point(); |
c2a986d5 | 256 | |
83555251 LP |
257 | return 0; |
258 | } |