]>
Commit | Line | Data |
---|---|---|
4349cd7c LP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2010 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
11c3a366 TA |
20 | #include <errno.h> |
21 | #include <stdlib.h> | |
4349cd7c LP |
22 | #include <string.h> |
23 | #include <sys/mount.h> | |
11c3a366 | 24 | #include <sys/stat.h> |
4349cd7c | 25 | #include <sys/statvfs.h> |
11c3a366 | 26 | #include <unistd.h> |
4349cd7c | 27 | |
b5efdb8a | 28 | #include "alloc-util.h" |
4349cd7c LP |
29 | #include "escape.h" |
30 | #include "fd-util.h" | |
31 | #include "fileio.h" | |
e1873695 | 32 | #include "fs-util.h" |
93cc7779 | 33 | #include "hashmap.h" |
4349cd7c LP |
34 | #include "mount-util.h" |
35 | #include "parse-util.h" | |
36 | #include "path-util.h" | |
37 | #include "set.h" | |
15a5e950 | 38 | #include "stdio-util.h" |
4349cd7c | 39 | #include "string-util.h" |
6b7c9f8b | 40 | #include "strv.h" |
4349cd7c LP |
41 | |
42 | static int fd_fdinfo_mnt_id(int fd, const char *filename, int flags, int *mnt_id) { | |
43 | char path[strlen("/proc/self/fdinfo/") + DECIMAL_STR_MAX(int)]; | |
44 | _cleanup_free_ char *fdinfo = NULL; | |
45 | _cleanup_close_ int subfd = -1; | |
46 | char *p; | |
47 | int r; | |
48 | ||
49 | if ((flags & AT_EMPTY_PATH) && isempty(filename)) | |
50 | xsprintf(path, "/proc/self/fdinfo/%i", fd); | |
51 | else { | |
c4b69156 | 52 | subfd = openat(fd, filename, O_CLOEXEC|O_PATH); |
4349cd7c LP |
53 | if (subfd < 0) |
54 | return -errno; | |
55 | ||
56 | xsprintf(path, "/proc/self/fdinfo/%i", subfd); | |
57 | } | |
58 | ||
59 | r = read_full_file(path, &fdinfo, NULL); | |
60 | if (r == -ENOENT) /* The fdinfo directory is a relatively new addition */ | |
61 | return -EOPNOTSUPP; | |
62 | if (r < 0) | |
63 | return -errno; | |
64 | ||
65 | p = startswith(fdinfo, "mnt_id:"); | |
66 | if (!p) { | |
67 | p = strstr(fdinfo, "\nmnt_id:"); | |
68 | if (!p) /* The mnt_id field is a relatively new addition */ | |
69 | return -EOPNOTSUPP; | |
70 | ||
71 | p += 8; | |
72 | } | |
73 | ||
74 | p += strspn(p, WHITESPACE); | |
75 | p[strcspn(p, WHITESPACE)] = 0; | |
76 | ||
77 | return safe_atoi(p, mnt_id); | |
78 | } | |
79 | ||
4349cd7c LP |
80 | int fd_is_mount_point(int fd, const char *filename, int flags) { |
81 | union file_handle_union h = FILE_HANDLE_INIT, h_parent = FILE_HANDLE_INIT; | |
82 | int mount_id = -1, mount_id_parent = -1; | |
83 | bool nosupp = false, check_st_dev = true; | |
84 | struct stat a, b; | |
85 | int r; | |
86 | ||
87 | assert(fd >= 0); | |
88 | assert(filename); | |
89 | ||
90 | /* First we will try the name_to_handle_at() syscall, which | |
91 | * tells us the mount id and an opaque file "handle". It is | |
92 | * not supported everywhere though (kernel compile-time | |
93 | * option, not all file systems are hooked up). If it works | |
94 | * the mount id is usually good enough to tell us whether | |
95 | * something is a mount point. | |
96 | * | |
97 | * If that didn't work we will try to read the mount id from | |
98 | * /proc/self/fdinfo/<fd>. This is almost as good as | |
99 | * name_to_handle_at(), however, does not return the | |
100 | * opaque file handle. The opaque file handle is pretty useful | |
101 | * to detect the root directory, which we should always | |
102 | * consider a mount point. Hence we use this only as | |
103 | * fallback. Exporting the mnt_id in fdinfo is a pretty recent | |
104 | * kernel addition. | |
105 | * | |
106 | * As last fallback we do traditional fstat() based st_dev | |
107 | * comparisons. This is how things were traditionally done, | |
61233823 | 108 | * but unionfs breaks this since it exposes file |
4349cd7c LP |
109 | * systems with a variety of st_dev reported. Also, btrfs |
110 | * subvolumes have different st_dev, even though they aren't | |
111 | * real mounts of their own. */ | |
112 | ||
113 | r = name_to_handle_at(fd, filename, &h.handle, &mount_id, flags); | |
114 | if (r < 0) { | |
059c35f5 LP |
115 | if (IN_SET(errno, ENOSYS, EACCES, EPERM)) |
116 | /* This kernel does not support name_to_handle_at() at all, or the syscall was blocked (maybe | |
117 | * through seccomp, because we are running inside of a container?): fall back to simpler | |
118 | * logic. */ | |
4349cd7c LP |
119 | goto fallback_fdinfo; |
120 | else if (errno == EOPNOTSUPP) | |
121 | /* This kernel or file system does not support | |
122 | * name_to_handle_at(), hence let's see if the | |
123 | * upper fs supports it (in which case it is a | |
124 | * mount point), otherwise fallback to the | |
125 | * traditional stat() logic */ | |
126 | nosupp = true; | |
127 | else | |
128 | return -errno; | |
129 | } | |
130 | ||
131 | r = name_to_handle_at(fd, "", &h_parent.handle, &mount_id_parent, AT_EMPTY_PATH); | |
132 | if (r < 0) { | |
133 | if (errno == EOPNOTSUPP) { | |
134 | if (nosupp) | |
135 | /* Neither parent nor child do name_to_handle_at()? | |
136 | We have no choice but to fall back. */ | |
137 | goto fallback_fdinfo; | |
138 | else | |
139 | /* The parent can't do name_to_handle_at() but the | |
140 | * directory we are interested in can? | |
141 | * If so, it must be a mount point. */ | |
142 | return 1; | |
143 | } else | |
144 | return -errno; | |
145 | } | |
146 | ||
147 | /* The parent can do name_to_handle_at() but the | |
148 | * directory we are interested in can't? If so, it | |
149 | * must be a mount point. */ | |
150 | if (nosupp) | |
151 | return 1; | |
152 | ||
153 | /* If the file handle for the directory we are | |
154 | * interested in and its parent are identical, we | |
155 | * assume this is the root directory, which is a mount | |
156 | * point. */ | |
157 | ||
158 | if (h.handle.handle_bytes == h_parent.handle.handle_bytes && | |
159 | h.handle.handle_type == h_parent.handle.handle_type && | |
160 | memcmp(h.handle.f_handle, h_parent.handle.f_handle, h.handle.handle_bytes) == 0) | |
161 | return 1; | |
162 | ||
163 | return mount_id != mount_id_parent; | |
164 | ||
165 | fallback_fdinfo: | |
166 | r = fd_fdinfo_mnt_id(fd, filename, flags, &mount_id); | |
059c35f5 | 167 | if (IN_SET(r, -EOPNOTSUPP, -EACCES, -EPERM)) |
4349cd7c LP |
168 | goto fallback_fstat; |
169 | if (r < 0) | |
170 | return r; | |
171 | ||
172 | r = fd_fdinfo_mnt_id(fd, "", AT_EMPTY_PATH, &mount_id_parent); | |
173 | if (r < 0) | |
174 | return r; | |
175 | ||
176 | if (mount_id != mount_id_parent) | |
177 | return 1; | |
178 | ||
179 | /* Hmm, so, the mount ids are the same. This leaves one | |
180 | * special case though for the root file system. For that, | |
181 | * let's see if the parent directory has the same inode as we | |
182 | * are interested in. Hence, let's also do fstat() checks now, | |
183 | * too, but avoid the st_dev comparisons, since they aren't | |
184 | * that useful on unionfs mounts. */ | |
185 | check_st_dev = false; | |
186 | ||
187 | fallback_fstat: | |
188 | /* yay for fstatat() taking a different set of flags than the other | |
189 | * _at() above */ | |
190 | if (flags & AT_SYMLINK_FOLLOW) | |
191 | flags &= ~AT_SYMLINK_FOLLOW; | |
192 | else | |
193 | flags |= AT_SYMLINK_NOFOLLOW; | |
194 | if (fstatat(fd, filename, &a, flags) < 0) | |
195 | return -errno; | |
196 | ||
197 | if (fstatat(fd, "", &b, AT_EMPTY_PATH) < 0) | |
198 | return -errno; | |
199 | ||
200 | /* A directory with same device and inode as its parent? Must | |
201 | * be the root directory */ | |
202 | if (a.st_dev == b.st_dev && | |
203 | a.st_ino == b.st_ino) | |
204 | return 1; | |
205 | ||
206 | return check_st_dev && (a.st_dev != b.st_dev); | |
207 | } | |
208 | ||
209 | /* flags can be AT_SYMLINK_FOLLOW or 0 */ | |
e1873695 | 210 | int path_is_mount_point(const char *t, const char *root, int flags) { |
4349cd7c | 211 | _cleanup_free_ char *canonical = NULL, *parent = NULL; |
e1873695 LP |
212 | _cleanup_close_ int fd = -1; |
213 | int r; | |
4349cd7c LP |
214 | |
215 | assert(t); | |
216 | ||
217 | if (path_equal(t, "/")) | |
218 | return 1; | |
219 | ||
220 | /* we need to resolve symlinks manually, we can't just rely on | |
221 | * fd_is_mount_point() to do that for us; if we have a structure like | |
222 | * /bin -> /usr/bin/ and /usr is a mount point, then the parent that we | |
223 | * look at needs to be /usr, not /. */ | |
224 | if (flags & AT_SYMLINK_FOLLOW) { | |
c4f4fce7 | 225 | r = chase_symlinks(t, root, 0, &canonical); |
e1873695 LP |
226 | if (r < 0) |
227 | return r; | |
4349cd7c LP |
228 | |
229 | t = canonical; | |
230 | } | |
231 | ||
232 | parent = dirname_malloc(t); | |
233 | if (!parent) | |
234 | return -ENOMEM; | |
235 | ||
c4b69156 | 236 | fd = openat(AT_FDCWD, parent, O_DIRECTORY|O_CLOEXEC|O_PATH); |
4349cd7c LP |
237 | if (fd < 0) |
238 | return -errno; | |
239 | ||
240 | return fd_is_mount_point(fd, basename(t), flags); | |
241 | } | |
242 | ||
243 | int umount_recursive(const char *prefix, int flags) { | |
244 | bool again; | |
245 | int n = 0, r; | |
246 | ||
247 | /* Try to umount everything recursively below a | |
248 | * directory. Also, take care of stacked mounts, and keep | |
249 | * unmounting them until they are gone. */ | |
250 | ||
251 | do { | |
252 | _cleanup_fclose_ FILE *proc_self_mountinfo = NULL; | |
253 | ||
254 | again = false; | |
255 | r = 0; | |
256 | ||
257 | proc_self_mountinfo = fopen("/proc/self/mountinfo", "re"); | |
258 | if (!proc_self_mountinfo) | |
259 | return -errno; | |
260 | ||
261 | for (;;) { | |
262 | _cleanup_free_ char *path = NULL, *p = NULL; | |
263 | int k; | |
264 | ||
265 | k = fscanf(proc_self_mountinfo, | |
266 | "%*s " /* (1) mount id */ | |
267 | "%*s " /* (2) parent id */ | |
268 | "%*s " /* (3) major:minor */ | |
269 | "%*s " /* (4) root */ | |
270 | "%ms " /* (5) mount point */ | |
271 | "%*s" /* (6) mount options */ | |
272 | "%*[^-]" /* (7) optional fields */ | |
273 | "- " /* (8) separator */ | |
274 | "%*s " /* (9) file system type */ | |
275 | "%*s" /* (10) mount source */ | |
276 | "%*s" /* (11) mount options 2 */ | |
277 | "%*[^\n]", /* some rubbish at the end */ | |
278 | &path); | |
279 | if (k != 1) { | |
280 | if (k == EOF) | |
281 | break; | |
282 | ||
283 | continue; | |
284 | } | |
285 | ||
286 | r = cunescape(path, UNESCAPE_RELAX, &p); | |
287 | if (r < 0) | |
288 | return r; | |
289 | ||
290 | if (!path_startswith(p, prefix)) | |
291 | continue; | |
292 | ||
293 | if (umount2(p, flags) < 0) { | |
6b7c9f8b | 294 | r = log_debug_errno(errno, "Failed to umount %s: %m", p); |
4349cd7c LP |
295 | continue; |
296 | } | |
297 | ||
6b7c9f8b LP |
298 | log_debug("Successfully unmounted %s", p); |
299 | ||
4349cd7c LP |
300 | again = true; |
301 | n++; | |
302 | ||
303 | break; | |
304 | } | |
305 | ||
306 | } while (again); | |
307 | ||
308 | return r ? r : n; | |
309 | } | |
310 | ||
311 | static int get_mount_flags(const char *path, unsigned long *flags) { | |
312 | struct statvfs buf; | |
313 | ||
314 | if (statvfs(path, &buf) < 0) | |
315 | return -errno; | |
316 | *flags = buf.f_flag; | |
317 | return 0; | |
318 | } | |
319 | ||
6b7c9f8b | 320 | int bind_remount_recursive(const char *prefix, bool ro, char **blacklist) { |
4349cd7c LP |
321 | _cleanup_set_free_free_ Set *done = NULL; |
322 | _cleanup_free_ char *cleaned = NULL; | |
323 | int r; | |
324 | ||
6b7c9f8b LP |
325 | /* Recursively remount a directory (and all its submounts) read-only or read-write. If the directory is already |
326 | * mounted, we reuse the mount and simply mark it MS_BIND|MS_RDONLY (or remove the MS_RDONLY for read-write | |
327 | * operation). If it isn't we first make it one. Afterwards we apply MS_BIND|MS_RDONLY (or remove MS_RDONLY) to | |
328 | * all submounts we can access, too. When mounts are stacked on the same mount point we only care for each | |
329 | * individual "top-level" mount on each point, as we cannot influence/access the underlying mounts anyway. We | |
330 | * do not have any effect on future submounts that might get propagated, they migt be writable. This includes | |
331 | * future submounts that have been triggered via autofs. | |
332 | * | |
333 | * If the "blacklist" parameter is specified it may contain a list of subtrees to exclude from the | |
334 | * remount operation. Note that we'll ignore the blacklist for the top-level path. */ | |
4349cd7c LP |
335 | |
336 | cleaned = strdup(prefix); | |
337 | if (!cleaned) | |
338 | return -ENOMEM; | |
339 | ||
340 | path_kill_slashes(cleaned); | |
341 | ||
342 | done = set_new(&string_hash_ops); | |
343 | if (!done) | |
344 | return -ENOMEM; | |
345 | ||
346 | for (;;) { | |
347 | _cleanup_fclose_ FILE *proc_self_mountinfo = NULL; | |
348 | _cleanup_set_free_free_ Set *todo = NULL; | |
349 | bool top_autofs = false; | |
350 | char *x; | |
351 | unsigned long orig_flags; | |
352 | ||
353 | todo = set_new(&string_hash_ops); | |
354 | if (!todo) | |
355 | return -ENOMEM; | |
356 | ||
357 | proc_self_mountinfo = fopen("/proc/self/mountinfo", "re"); | |
358 | if (!proc_self_mountinfo) | |
359 | return -errno; | |
360 | ||
361 | for (;;) { | |
362 | _cleanup_free_ char *path = NULL, *p = NULL, *type = NULL; | |
363 | int k; | |
364 | ||
365 | k = fscanf(proc_self_mountinfo, | |
366 | "%*s " /* (1) mount id */ | |
367 | "%*s " /* (2) parent id */ | |
368 | "%*s " /* (3) major:minor */ | |
369 | "%*s " /* (4) root */ | |
370 | "%ms " /* (5) mount point */ | |
371 | "%*s" /* (6) mount options (superblock) */ | |
372 | "%*[^-]" /* (7) optional fields */ | |
373 | "- " /* (8) separator */ | |
374 | "%ms " /* (9) file system type */ | |
375 | "%*s" /* (10) mount source */ | |
376 | "%*s" /* (11) mount options (bind mount) */ | |
377 | "%*[^\n]", /* some rubbish at the end */ | |
378 | &path, | |
379 | &type); | |
380 | if (k != 2) { | |
381 | if (k == EOF) | |
382 | break; | |
383 | ||
384 | continue; | |
385 | } | |
386 | ||
387 | r = cunescape(path, UNESCAPE_RELAX, &p); | |
388 | if (r < 0) | |
389 | return r; | |
390 | ||
6b7c9f8b LP |
391 | if (!path_startswith(p, cleaned)) |
392 | continue; | |
393 | ||
394 | /* Ignore this mount if it is blacklisted, but only if it isn't the top-level mount we shall | |
395 | * operate on. */ | |
396 | if (!path_equal(cleaned, p)) { | |
397 | bool blacklisted = false; | |
398 | char **i; | |
399 | ||
400 | STRV_FOREACH(i, blacklist) { | |
401 | ||
402 | if (path_equal(*i, cleaned)) | |
403 | continue; | |
404 | ||
405 | if (!path_startswith(*i, cleaned)) | |
406 | continue; | |
407 | ||
408 | if (path_startswith(p, *i)) { | |
409 | blacklisted = true; | |
410 | log_debug("Not remounting %s, because blacklisted by %s, called for %s", p, *i, cleaned); | |
411 | break; | |
412 | } | |
413 | } | |
414 | if (blacklisted) | |
415 | continue; | |
416 | } | |
417 | ||
4349cd7c LP |
418 | /* Let's ignore autofs mounts. If they aren't |
419 | * triggered yet, we want to avoid triggering | |
420 | * them, as we don't make any guarantees for | |
421 | * future submounts anyway. If they are | |
422 | * already triggered, then we will find | |
423 | * another entry for this. */ | |
424 | if (streq(type, "autofs")) { | |
425 | top_autofs = top_autofs || path_equal(cleaned, p); | |
426 | continue; | |
427 | } | |
428 | ||
6b7c9f8b | 429 | if (!set_contains(done, p)) { |
4349cd7c LP |
430 | r = set_consume(todo, p); |
431 | p = NULL; | |
4349cd7c LP |
432 | if (r == -EEXIST) |
433 | continue; | |
434 | if (r < 0) | |
435 | return r; | |
436 | } | |
437 | } | |
438 | ||
439 | /* If we have no submounts to process anymore and if | |
440 | * the root is either already done, or an autofs, we | |
441 | * are done */ | |
442 | if (set_isempty(todo) && | |
443 | (top_autofs || set_contains(done, cleaned))) | |
444 | return 0; | |
445 | ||
446 | if (!set_contains(done, cleaned) && | |
447 | !set_contains(todo, cleaned)) { | |
6b7c9f8b | 448 | /* The prefix directory itself is not yet a mount, make it one. */ |
4349cd7c LP |
449 | if (mount(cleaned, cleaned, NULL, MS_BIND|MS_REC, NULL) < 0) |
450 | return -errno; | |
451 | ||
452 | orig_flags = 0; | |
453 | (void) get_mount_flags(cleaned, &orig_flags); | |
454 | orig_flags &= ~MS_RDONLY; | |
455 | ||
456 | if (mount(NULL, prefix, NULL, orig_flags|MS_BIND|MS_REMOUNT|(ro ? MS_RDONLY : 0), NULL) < 0) | |
457 | return -errno; | |
458 | ||
6b7c9f8b LP |
459 | log_debug("Made top-level directory %s a mount point.", prefix); |
460 | ||
4349cd7c LP |
461 | x = strdup(cleaned); |
462 | if (!x) | |
463 | return -ENOMEM; | |
464 | ||
465 | r = set_consume(done, x); | |
466 | if (r < 0) | |
467 | return r; | |
468 | } | |
469 | ||
470 | while ((x = set_steal_first(todo))) { | |
471 | ||
472 | r = set_consume(done, x); | |
473 | if (r == -EEXIST || r == 0) | |
474 | continue; | |
475 | if (r < 0) | |
476 | return r; | |
477 | ||
6b7c9f8b | 478 | /* Deal with mount points that are obstructed by a later mount */ |
e1873695 | 479 | r = path_is_mount_point(x, NULL, 0); |
98df8089 AC |
480 | if (r == -ENOENT || r == 0) |
481 | continue; | |
482 | if (r < 0) | |
483 | return r; | |
484 | ||
485 | /* Try to reuse the original flag set */ | |
4349cd7c LP |
486 | orig_flags = 0; |
487 | (void) get_mount_flags(x, &orig_flags); | |
488 | orig_flags &= ~MS_RDONLY; | |
489 | ||
98df8089 AC |
490 | if (mount(NULL, x, NULL, orig_flags|MS_BIND|MS_REMOUNT|(ro ? MS_RDONLY : 0), NULL) < 0) |
491 | return -errno; | |
4349cd7c | 492 | |
6b7c9f8b | 493 | log_debug("Remounted %s read-only.", x); |
4349cd7c LP |
494 | } |
495 | } | |
496 | } | |
497 | ||
498 | int mount_move_root(const char *path) { | |
499 | assert(path); | |
500 | ||
501 | if (chdir(path) < 0) | |
502 | return -errno; | |
503 | ||
504 | if (mount(path, "/", NULL, MS_MOVE, NULL) < 0) | |
505 | return -errno; | |
506 | ||
507 | if (chroot(".") < 0) | |
508 | return -errno; | |
509 | ||
510 | if (chdir("/") < 0) | |
511 | return -errno; | |
512 | ||
513 | return 0; | |
514 | } | |
4e036b7a LP |
515 | |
516 | bool fstype_is_network(const char *fstype) { | |
517 | static const char table[] = | |
518 | "afs\0" | |
519 | "cifs\0" | |
520 | "smbfs\0" | |
521 | "sshfs\0" | |
522 | "ncpfs\0" | |
523 | "ncp\0" | |
524 | "nfs\0" | |
525 | "nfs4\0" | |
526 | "gfs\0" | |
527 | "gfs2\0" | |
a44cb5cb ZJS |
528 | "glusterfs\0" |
529 | "pvfs2\0" /* OrangeFS */ | |
0a86e681 | 530 | "ocfs2\0" |
67ae4366 | 531 | "lustre\0" |
a44cb5cb | 532 | ; |
4e036b7a LP |
533 | |
534 | const char *x; | |
535 | ||
536 | x = startswith(fstype, "fuse."); | |
537 | if (x) | |
538 | fstype = x; | |
539 | ||
540 | return nulstr_contains(table, fstype); | |
541 | } | |
3f2c0bec LP |
542 | |
543 | int repeat_unmount(const char *path, int flags) { | |
544 | bool done = false; | |
545 | ||
546 | assert(path); | |
547 | ||
548 | /* If there are multiple mounts on a mount point, this | |
549 | * removes them all */ | |
550 | ||
551 | for (;;) { | |
552 | if (umount2(path, flags) < 0) { | |
553 | ||
554 | if (errno == EINVAL) | |
555 | return done; | |
556 | ||
557 | return -errno; | |
558 | } | |
559 | ||
560 | done = true; | |
561 | } | |
562 | } | |
c4b41707 AP |
563 | |
564 | const char* mode_to_inaccessible_node(mode_t mode) { | |
b3d1d516 AP |
565 | /* This function maps a node type to the correspondent inaccessible node type. |
566 | * Character and block inaccessible devices may not be created (because major=0 and minor=0), | |
567 | * in such case we map character and block devices to the inaccessible node type socket. */ | |
c4b41707 AP |
568 | switch(mode & S_IFMT) { |
569 | case S_IFREG: | |
570 | return "/run/systemd/inaccessible/reg"; | |
571 | case S_IFDIR: | |
572 | return "/run/systemd/inaccessible/dir"; | |
573 | case S_IFCHR: | |
b3d1d516 AP |
574 | if (access("/run/systemd/inaccessible/chr", F_OK) == 0) |
575 | return "/run/systemd/inaccessible/chr"; | |
576 | return "/run/systemd/inaccessible/sock"; | |
c4b41707 | 577 | case S_IFBLK: |
b3d1d516 AP |
578 | if (access("/run/systemd/inaccessible/blk", F_OK) == 0) |
579 | return "/run/systemd/inaccessible/blk"; | |
580 | return "/run/systemd/inaccessible/sock"; | |
c4b41707 AP |
581 | case S_IFIFO: |
582 | return "/run/systemd/inaccessible/fifo"; | |
583 | case S_IFSOCK: | |
584 | return "/run/systemd/inaccessible/sock"; | |
585 | } | |
586 | return NULL; | |
587 | } | |
60e76d48 ZJS |
588 | |
589 | #define FLAG(name) (flags & name ? STRINGIFY(name) "|" : "") | |
590 | static char* mount_flags_to_string(long unsigned flags) { | |
591 | char *x; | |
592 | _cleanup_free_ char *y = NULL; | |
593 | long unsigned overflow; | |
594 | ||
595 | overflow = flags & ~(MS_RDONLY | | |
596 | MS_NOSUID | | |
597 | MS_NODEV | | |
598 | MS_NOEXEC | | |
599 | MS_SYNCHRONOUS | | |
600 | MS_REMOUNT | | |
601 | MS_MANDLOCK | | |
602 | MS_DIRSYNC | | |
603 | MS_NOATIME | | |
604 | MS_NODIRATIME | | |
605 | MS_BIND | | |
606 | MS_MOVE | | |
607 | MS_REC | | |
608 | MS_SILENT | | |
609 | MS_POSIXACL | | |
610 | MS_UNBINDABLE | | |
611 | MS_PRIVATE | | |
612 | MS_SLAVE | | |
613 | MS_SHARED | | |
614 | MS_RELATIME | | |
615 | MS_KERNMOUNT | | |
616 | MS_I_VERSION | | |
617 | MS_STRICTATIME | | |
618 | MS_LAZYTIME); | |
619 | ||
620 | if (flags == 0 || overflow != 0) | |
621 | if (asprintf(&y, "%lx", overflow) < 0) | |
622 | return NULL; | |
623 | ||
624 | x = strjoin(FLAG(MS_RDONLY), | |
625 | FLAG(MS_NOSUID), | |
626 | FLAG(MS_NODEV), | |
627 | FLAG(MS_NOEXEC), | |
628 | FLAG(MS_SYNCHRONOUS), | |
629 | FLAG(MS_REMOUNT), | |
630 | FLAG(MS_MANDLOCK), | |
631 | FLAG(MS_DIRSYNC), | |
632 | FLAG(MS_NOATIME), | |
633 | FLAG(MS_NODIRATIME), | |
634 | FLAG(MS_BIND), | |
635 | FLAG(MS_MOVE), | |
636 | FLAG(MS_REC), | |
637 | FLAG(MS_SILENT), | |
638 | FLAG(MS_POSIXACL), | |
639 | FLAG(MS_UNBINDABLE), | |
640 | FLAG(MS_PRIVATE), | |
641 | FLAG(MS_SLAVE), | |
642 | FLAG(MS_SHARED), | |
643 | FLAG(MS_RELATIME), | |
644 | FLAG(MS_KERNMOUNT), | |
645 | FLAG(MS_I_VERSION), | |
646 | FLAG(MS_STRICTATIME), | |
647 | FLAG(MS_LAZYTIME), | |
605405c6 | 648 | y); |
60e76d48 ZJS |
649 | if (!x) |
650 | return NULL; | |
651 | if (!y) | |
652 | x[strlen(x) - 1] = '\0'; /* truncate the last | */ | |
653 | return x; | |
654 | } | |
655 | ||
656 | int mount_verbose( | |
657 | int error_log_level, | |
658 | const char *what, | |
659 | const char *where, | |
660 | const char *type, | |
661 | unsigned long flags, | |
662 | const char *options) { | |
663 | ||
664 | _cleanup_free_ char *fl = NULL; | |
665 | ||
666 | fl = mount_flags_to_string(flags); | |
667 | ||
668 | if ((flags & MS_REMOUNT) && !what && !type) | |
669 | log_debug("Remounting %s (%s \"%s\")...", | |
670 | where, strnull(fl), strempty(options)); | |
671 | else if (!what && !type) | |
672 | log_debug("Mounting %s (%s \"%s\")...", | |
673 | where, strnull(fl), strempty(options)); | |
674 | else if ((flags & MS_BIND) && !type) | |
675 | log_debug("Bind-mounting %s on %s (%s \"%s\")...", | |
676 | what, where, strnull(fl), strempty(options)); | |
afe682bc LP |
677 | else if (flags & MS_MOVE) |
678 | log_debug("Moving mount %s → %s (%s \"%s\")...", | |
679 | what, where, strnull(fl), strempty(options)); | |
60e76d48 ZJS |
680 | else |
681 | log_debug("Mounting %s on %s (%s \"%s\")...", | |
682 | strna(type), where, strnull(fl), strempty(options)); | |
683 | if (mount(what, where, type, flags, options) < 0) | |
684 | return log_full_errno(error_log_level, errno, | |
685 | "Failed to mount %s on %s (%s \"%s\"): %m", | |
686 | strna(type), where, strnull(fl), strempty(options)); | |
687 | return 0; | |
688 | } | |
689 | ||
690 | int umount_verbose(const char *what) { | |
691 | log_debug("Umounting %s...", what); | |
692 | if (umount(what) < 0) | |
693 | return log_error_errno(errno, "Failed to unmount %s: %m", what); | |
694 | return 0; | |
695 | } | |
83555251 LP |
696 | |
697 | const char *mount_propagation_flags_to_string(unsigned long flags) { | |
698 | ||
699 | switch (flags & (MS_SHARED|MS_SLAVE|MS_PRIVATE)) { | |
c7383828 ZJS |
700 | case 0: |
701 | return ""; | |
83555251 LP |
702 | case MS_SHARED: |
703 | return "shared"; | |
83555251 LP |
704 | case MS_SLAVE: |
705 | return "slave"; | |
83555251 LP |
706 | case MS_PRIVATE: |
707 | return "private"; | |
708 | } | |
709 | ||
710 | return NULL; | |
711 | } | |
712 | ||
83555251 | 713 | |
c7383828 | 714 | int mount_propagation_flags_from_string(const char *name, unsigned long *ret) { |
83555251 | 715 | |
c7383828 ZJS |
716 | if (isempty(name)) |
717 | *ret = 0; | |
718 | else if (streq(name, "shared")) | |
719 | *ret = MS_SHARED; | |
720 | else if (streq(name, "slave")) | |
721 | *ret = MS_SLAVE; | |
722 | else if (streq(name, "private")) | |
723 | *ret = MS_PRIVATE; | |
724 | else | |
725 | return -EINVAL; | |
83555251 LP |
726 | return 0; |
727 | } |