|
19 | 19 | #include "parse-util.h" |
20 | 20 | #include "path-util.h" |
21 | 21 | #include "set.h" |
| 22 | +#include "stat-util.h" |
22 | 23 | #include "stdio-util.h" |
23 | 24 | #include "string-util.h" |
24 | 25 | #include "strv.h" |
25 | 26 |
|
| 27 | +int mount_fd(const char *source, |
| 28 | + int target_fd, |
| 29 | + const char *filesystemtype, |
| 30 | + unsigned long mountflags, |
| 31 | + const void *data) { |
| 32 | + |
| 33 | + char path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)]; |
| 34 | + |
| 35 | + xsprintf(path, "/proc/self/fd/%i", target_fd); |
| 36 | + if (mount(source, path, filesystemtype, mountflags, data) < 0) { |
| 37 | + if (errno != ENOENT) |
| 38 | + return -errno; |
| 39 | + |
| 40 | + /* ENOENT can mean two things: either that the source is missing, or that /proc/ isn't |
| 41 | + * mounted. Check for the latter to generate better error messages. */ |
| 42 | + if (proc_mounted() == 0) |
| 43 | + return -ENOSYS; |
| 44 | + |
| 45 | + return -ENOENT; |
| 46 | + } |
| 47 | + |
| 48 | + return 0; |
| 49 | +} |
| 50 | + |
| 51 | +int mount_nofollow( |
| 52 | + const char *source, |
| 53 | + const char *target, |
| 54 | + const char *filesystemtype, |
| 55 | + unsigned long mountflags, |
| 56 | + const void *data) { |
| 57 | + |
| 58 | + _cleanup_close_ int fd = -1; |
| 59 | + |
| 60 | + /* In almost all cases we want to manipulate the mount table without following symlinks, hence |
| 61 | + * mount_nofollow() is usually the way to go. The only exceptions are environments where /proc/ is |
| 62 | + * not available yet, since we need /proc/self/fd/ for this logic to work. i.e. during the early |
| 63 | + * initialization of namespacing/container stuff where /proc is not yet mounted (and maybe even the |
| 64 | + * fs to mount) we can only use traditional mount() directly. |
| 65 | + * |
| 66 | + * Note that this disables following only for the final component of the target, i.e symlinks within |
| 67 | + * the path of the target are honoured, as are symlinks in the source path everywhere. */ |
| 68 | + |
| 69 | + fd = open(target, O_PATH|O_CLOEXEC|O_NOFOLLOW); |
| 70 | + if (fd < 0) |
| 71 | + return -errno; |
| 72 | + |
| 73 | + return mount_fd(source, fd, filesystemtype, mountflags, data); |
| 74 | +} |
| 75 | + |
26 | 76 | int umount_recursive(const char *prefix, int flags) { |
27 | 77 | int n = 0, r; |
28 | 78 | bool again; |
|
0 commit comments