]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/mount-util.c
base-filesystem: use uid_is_valid() at one more place
[thirdparty/systemd.git] / src / shared / mount-util.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
4349cd7c 2
11c3a366
TA
3#include <errno.h>
4#include <stdlib.h>
4349cd7c 5#include <sys/mount.h>
11c3a366 6#include <sys/stat.h>
4349cd7c 7#include <sys/statvfs.h>
11c3a366 8#include <unistd.h>
35fd3558
LP
9#include <linux/loop.h>
10#include <linux/fs.h>
4349cd7c 11
b5efdb8a 12#include "alloc-util.h"
f4351959 13#include "chase-symlinks.h"
70599967 14#include "dissect-image.h"
45a68ed3 15#include "exec-util.h"
9e7f941a 16#include "extract-word.h"
4349cd7c
LP
17#include "fd-util.h"
18#include "fileio.h"
e1873695 19#include "fs-util.h"
93cc7779 20#include "hashmap.h"
9c653536 21#include "label.h"
13dcfe46 22#include "libmount-util.h"
1c092b62 23#include "missing_mount.h"
35fd3558 24#include "missing_syscall.h"
35cd0ba5 25#include "mkdir-label.h"
4349cd7c 26#include "mount-util.h"
049af8ad 27#include "mountpoint-util.h"
2338a175 28#include "namespace-util.h"
4349cd7c
LP
29#include "parse-util.h"
30#include "path-util.h"
6af52c3a 31#include "process-util.h"
4349cd7c 32#include "set.h"
28126409 33#include "stat-util.h"
15a5e950 34#include "stdio-util.h"
4349cd7c 35#include "string-util.h"
6b7c9f8b 36#include "strv.h"
6af52c3a 37#include "tmpfile-util.h"
70599967 38#include "user-util.h"
4349cd7c 39
28126409
LP
40int mount_fd(const char *source,
41 int target_fd,
42 const char *filesystemtype,
43 unsigned long mountflags,
44 const void *data) {
45
ddb6eeaf 46 if (mount(source, FORMAT_PROC_FD_PATH(target_fd), filesystemtype, mountflags, data) < 0) {
28126409
LP
47 if (errno != ENOENT)
48 return -errno;
49
50 /* ENOENT can mean two things: either that the source is missing, or that /proc/ isn't
51 * mounted. Check for the latter to generate better error messages. */
52 if (proc_mounted() == 0)
53 return -ENOSYS;
54
55 return -ENOENT;
56 }
57
58 return 0;
59}
60
61int mount_nofollow(
62 const char *source,
63 const char *target,
64 const char *filesystemtype,
65 unsigned long mountflags,
66 const void *data) {
67
68 _cleanup_close_ int fd = -1;
69
70 /* In almost all cases we want to manipulate the mount table without following symlinks, hence
71 * mount_nofollow() is usually the way to go. The only exceptions are environments where /proc/ is
72 * not available yet, since we need /proc/self/fd/ for this logic to work. i.e. during the early
73 * initialization of namespacing/container stuff where /proc is not yet mounted (and maybe even the
74 * fs to mount) we can only use traditional mount() directly.
75 *
76 * Note that this disables following only for the final component of the target, i.e symlinks within
77 * the path of the target are honoured, as are symlinks in the source path everywhere. */
78
79 fd = open(target, O_PATH|O_CLOEXEC|O_NOFOLLOW);
80 if (fd < 0)
81 return -errno;
82
83 return mount_fd(source, fd, filesystemtype, mountflags, data);
84}
85
4349cd7c 86int umount_recursive(const char *prefix, int flags) {
4349cd7c 87 int n = 0, r;
f8b1904f 88 bool again;
4349cd7c 89
9d0619de
LP
90 /* Try to umount everything recursively below a directory. Also, take care of stacked mounts, and
91 * keep unmounting them until they are gone. */
4349cd7c
LP
92
93 do {
13dcfe46
ZJS
94 _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
95 _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
4349cd7c
LP
96
97 again = false;
4349cd7c 98
2f2d81d9 99 r = libmount_parse("/proc/self/mountinfo", NULL, &table, &iter);
fdeea3f4 100 if (r < 0)
13dcfe46 101 return log_debug_errno(r, "Failed to parse /proc/self/mountinfo: %m");
35bbbf85 102
4349cd7c 103 for (;;) {
13dcfe46
ZJS
104 struct libmnt_fs *fs;
105 const char *path;
4349cd7c 106
13dcfe46
ZJS
107 r = mnt_table_next_fs(table, iter, &fs);
108 if (r == 1)
109 break;
110 if (r < 0)
111 return log_debug_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
4349cd7c 112
13dcfe46
ZJS
113 path = mnt_fs_get_target(fs);
114 if (!path)
115 continue;
4349cd7c 116
13dcfe46 117 if (!path_startswith(path, prefix))
4349cd7c
LP
118 continue;
119
827ea521
LP
120 if (umount2(path, flags | UMOUNT_NOFOLLOW) < 0) {
121 log_debug_errno(errno, "Failed to umount %s, ignoring: %m", path);
4349cd7c
LP
122 continue;
123 }
124
13dcfe46 125 log_debug("Successfully unmounted %s", path);
6b7c9f8b 126
4349cd7c
LP
127 again = true;
128 n++;
129
130 break;
131 }
4349cd7c
LP
132 } while (again);
133
13dcfe46 134 return n;
4349cd7c
LP
135}
136
4f5644db
LP
137#define MS_CONVERTIBLE_FLAGS (MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC|MS_NOSYMFOLLOW)
138
139static uint64_t ms_flags_to_mount_attr(unsigned long a) {
140 uint64_t f = 0;
141
142 if (FLAGS_SET(a, MS_RDONLY))
143 f |= MOUNT_ATTR_RDONLY;
144
145 if (FLAGS_SET(a, MS_NOSUID))
146 f |= MOUNT_ATTR_NOSUID;
147
148 if (FLAGS_SET(a, MS_NODEV))
149 f |= MOUNT_ATTR_NODEV;
150
151 if (FLAGS_SET(a, MS_NOEXEC))
152 f |= MOUNT_ATTR_NOEXEC;
153
154 if (FLAGS_SET(a, MS_NOSYMFOLLOW))
155 f |= MOUNT_ATTR_NOSYMFOLLOW;
156
157 return f;
158}
159
160static bool skip_mount_set_attr = false;
161
be3f3752 162/* Use this function only if you do not have direct access to /proc/self/mountinfo but the caller can open it
64e82c19
LP
163 * for you. This is the case when /proc is masked or not mounted. Otherwise, use bind_remount_recursive. */
164int bind_remount_recursive_with_mountinfo(
165 const char *prefix,
166 unsigned long new_flags,
167 unsigned long flags_mask,
6b000af4 168 char **deny_list,
64e82c19
LP
169 FILE *proc_self_mountinfo) {
170
0289948e 171 _cleanup_fclose_ FILE *proc_self_mountinfo_opened = NULL;
ba8dced2 172 _cleanup_set_free_ Set *done = NULL;
670e8efd 173 unsigned n_tries = 0;
4349cd7c
LP
174 int r;
175
8403219f 176 assert(prefix);
ac9de0b3 177
874052c5
LP
178 if ((flags_mask & ~MS_CONVERTIBLE_FLAGS) == 0 && strv_isempty(deny_list) && !skip_mount_set_attr) {
179 /* Let's take a shortcut for all the flags we know how to convert into mount_setattr() flags */
180
181 if (mount_setattr(AT_FDCWD, prefix, AT_SYMLINK_NOFOLLOW|AT_RECURSIVE,
182 &(struct mount_attr) {
183 .attr_set = ms_flags_to_mount_attr(new_flags & flags_mask),
184 .attr_clr = ms_flags_to_mount_attr(~new_flags & flags_mask),
185 }, MOUNT_ATTR_SIZE_VER0) < 0) {
186
187 log_debug_errno(errno, "mount_setattr() failed, falling back to classic remounting: %m");
188
189 /* We fall through to classic behaviour if not supported (i.e. kernel < 5.12). We
190 * also do this for all other kinds of errors since they are so many different, and
191 * mount_setattr() has no graceful mode where it continues despite seeing errors one
192 * some mounts, but we want that. Moreover mount_setattr() only works on the mount
193 * point inode itself, not a non-mount point inode, and we want to support arbitrary
194 * prefixes here. */
195
196 if (ERRNO_IS_NOT_SUPPORTED(errno)) /* if not supported, then don't bother at all anymore */
197 skip_mount_set_attr = true;
198 } else
199 return 0; /* Nice, this worked! */
200 }
201
0289948e
LP
202 if (!proc_self_mountinfo) {
203 r = fopen_unlocked("/proc/self/mountinfo", "re", &proc_self_mountinfo_opened);
204 if (r < 0)
205 return r;
206
207 proc_self_mountinfo = proc_self_mountinfo_opened;
208 }
209
ddc155b2
TM
210 /* Recursively remount a directory (and all its submounts) with desired flags (MS_READONLY,
211 * MS_NOSUID, MS_NOEXEC). If the directory is already mounted, we reuse the mount and simply mark it
212 * MS_BIND|MS_RDONLY (or remove the MS_RDONLY for read-write operation), ditto for other flags. If it
213 * isn't we first make it one. Afterwards we apply (or remove) the flags to all submounts we can
214 * access, too. When mounts are stacked on the same mount point we only care for each individual
215 * "top-level" mount on each point, as we cannot influence/access the underlying mounts anyway. We do
216 * not have any effect on future submounts that might get propagated, they might be writable
4b6ef527
LP
217 * etc. This includes future submounts that have been triggered via autofs. Also note that we can't
218 * operate atomically here. Mounts established while we process the tree might or might not get
219 * noticed and thus might or might not be covered.
6b7c9f8b 220 *
6b000af4
LP
221 * If the "deny_list" parameter is specified it may contain a list of subtrees to exclude from the
222 * remount operation. Note that we'll ignore the deny list for the top-level path. */
4349cd7c 223
4349cd7c 224 for (;;) {
13dcfe46
ZJS
225 _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
226 _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
ba8dced2 227 _cleanup_hashmap_free_ Hashmap *todo = NULL;
4349cd7c 228 bool top_autofs = false;
4349cd7c 229
670e8efd
LP
230 if (n_tries++ >= 32) /* Let's not retry this loop forever */
231 return -EBUSY;
232
ac9de0b3 233 rewind(proc_self_mountinfo);
4349cd7c 234
e2857b3d 235 r = libmount_parse("/proc/self/mountinfo", proc_self_mountinfo, &table, &iter);
13dcfe46
ZJS
236 if (r < 0)
237 return log_debug_errno(r, "Failed to parse /proc/self/mountinfo: %m");
4349cd7c 238
13dcfe46 239 for (;;) {
ba8dced2
LP
240 _cleanup_free_ char *d = NULL;
241 const char *path, *type, *opts;
242 unsigned long flags = 0;
13dcfe46 243 struct libmnt_fs *fs;
4349cd7c 244
13dcfe46 245 r = mnt_table_next_fs(table, iter, &fs);
d6bfab11 246 if (r == 1) /* EOF */
13dcfe46 247 break;
4349cd7c 248 if (r < 0)
13dcfe46 249 return log_debug_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
4349cd7c 250
13dcfe46 251 path = mnt_fs_get_target(fs);
d6bfab11 252 if (!path)
6b7c9f8b
LP
253 continue;
254
c6111b85 255 if (!path_startswith(path, prefix))
13dcfe46
ZJS
256 continue;
257
d6bfab11
LP
258 type = mnt_fs_get_fstype(fs);
259 if (!type)
260 continue;
261
262 /* Let's ignore autofs mounts. If they aren't triggered yet, we want to avoid
263 * triggering them, as we don't make any guarantees for future submounts anyway. If
264 * they are already triggered, then we will find another entry for this. */
265 if (streq(type, "autofs")) {
266 top_autofs = top_autofs || path_equal(path, prefix);
267 continue;
268 }
269
270 if (set_contains(done, path))
271 continue;
272
6b000af4 273 /* Ignore this mount if it is deny-listed, but only if it isn't the top-level mount
13dcfe46 274 * we shall operate on. */
c6111b85 275 if (!path_equal(path, prefix)) {
6b000af4 276 bool deny_listed = false;
6b7c9f8b
LP
277 char **i;
278
6b000af4 279 STRV_FOREACH(i, deny_list) {
c6111b85 280 if (path_equal(*i, prefix))
6b7c9f8b
LP
281 continue;
282
c6111b85 283 if (!path_startswith(*i, prefix))
6b7c9f8b
LP
284 continue;
285
13dcfe46 286 if (path_startswith(path, *i)) {
6b000af4 287 deny_listed = true;
d6bfab11 288 log_debug("Not remounting %s deny-listed by %s, called for %s", path, *i, prefix);
6b7c9f8b
LP
289 break;
290 }
291 }
d6bfab11 292
6b000af4 293 if (deny_listed)
6b7c9f8b
LP
294 continue;
295 }
296
ba8dced2
LP
297 opts = mnt_fs_get_vfs_options(fs);
298 if (opts) {
299 r = mnt_optstr_get_flags(opts, &flags, mnt_get_builtin_optmap(MNT_LINUX_MAP));
300 if (r < 0)
301 log_debug_errno(r, "Could not get flags for '%s', ignoring: %m", path);
302 }
303
304 d = strdup(path);
305 if (!d)
306 return -ENOMEM;
307
308 r = hashmap_ensure_put(&todo, &path_hash_ops_free, d, ULONG_TO_PTR(flags));
309 if (r == -EEXIST)
e0103063
LB
310 /* If the same path was recorded, but with different mount flags, update it:
311 * it means a mount point is overmounted, and libmount returns the "bottom" (or
312 * older one) first, but we want to reapply the flags from the "top" (or newer
313 * one). See: https://github.com/systemd/systemd/issues/20032
314 * Note that this shouldn't really fail, as we were just told that the key
315 * exists, and it's an update so we want 'd' to be freed immediately. */
316 r = hashmap_update(todo, d, ULONG_TO_PTR(flags));
d6bfab11
LP
317 if (r < 0)
318 return r;
ba8dced2
LP
319 if (r > 0)
320 TAKE_PTR(d);
4349cd7c
LP
321 }
322
5c5753b9
LP
323 /* Check if the top-level directory was among what we have seen so far. For that check both
324 * 'done' and 'todo'. Also check 'top_autofs' because if the top-level dir is an autofs we'll
325 * not include it in either set but will set this bool. */
c6111b85 326 if (!set_contains(done, prefix) &&
ba8dced2 327 !(top_autofs || hashmap_contains(todo, prefix))) {
5c5753b9 328
6b7c9f8b 329 /* The prefix directory itself is not yet a mount, make it one. */
c6111b85 330 r = mount_nofollow(prefix, prefix, NULL, MS_BIND|MS_REC, NULL);
511a8cfe
LP
331 if (r < 0)
332 return r;
4349cd7c 333
5c5753b9
LP
334 /* Immediately rescan, so that we pick up the new mount's flags */
335 continue;
4349cd7c
LP
336 }
337
5c5753b9 338 /* If we have no submounts to process anymore, we are done */
ba8dced2 339 if (hashmap_isempty(todo))
5c5753b9
LP
340 return 0;
341
ba8dced2
LP
342 for (;;) {
343 unsigned long flags;
344 char *x = NULL;
345
346 /* Take the first mount from our list of mounts to still process */
347 flags = PTR_TO_ULONG(hashmap_steal_first_key_and_value(todo, (void**) &x));
348 if (!x)
349 break;
4349cd7c 350
ba8dced2 351 r = set_ensure_consume(&done, &path_hash_ops_free, x);
4c701096 352 if (IN_SET(r, 0, -EEXIST))
ba8dced2 353 continue; /* Already done */
4349cd7c
LP
354 if (r < 0)
355 return r;
356
ba8dced2
LP
357 /* Now, remount this with the new flags set, but exclude MS_RELATIME from it. (It's
358 * the default anyway, thus redundant, and in userns we'll get an error if we try to
359 * explicitly enable it) */
360 r = mount_nofollow(NULL, x, NULL, ((flags & ~flags_mask)|MS_BIND|MS_REMOUNT|new_flags) & ~MS_RELATIME, NULL);
065b4774 361 if (r < 0) {
ba8dced2
LP
362 int q;
363
364 /* OK, so the remount of this entry failed. We'll ultimately ignore this in
365 * almost all cases (there are simply so many reasons why this can fail,
366 * think autofs, NFS, FUSE, …), but let's generate useful debug messages at
367 * the very least. */
368
369 q = path_is_mount_point(x, NULL, 0);
370 if (IN_SET(q, 0, -ENOENT)) {
371 /* Hmm, whaaaa? The mount point is not actually a mount point? Then
372 * it is either obstructed by a later mount or somebody has been
373 * racing against us and removed it. Either way the mount point
374 * doesn't matter to us, let's ignore it hence. */
375 log_debug_errno(r, "Mount point '%s' to remount is not a mount point anymore, ignoring remount failure: %m", x);
376 continue;
377 }
378 if (q < 0) /* Any other error on this? Just log and continue */
379 log_debug_errno(q, "Failed to determine whether '%s' is a mount point or not, ignoring: %m", x);
380
381 if (((flags ^ new_flags) & flags_mask & ~MS_RELATIME) == 0) { /* ignore MS_RELATIME while comparing */
382 log_debug_errno(r, "Couldn't remount '%s', but the flags already match what we want, hence ignoring: %m", x);
383 continue;
384 }
385
386 /* Make this fatal if this is the top-level mount */
387 if (path_equal(x, prefix))
065b4774
LP
388 return r;
389
ba8dced2
LP
390 /* If this is not the top-level mount, then handle this gracefully: log but
391 * otherwise ignore. With NFS, FUSE, autofs there are just too many reasons
392 * this might fail without a chance for us to do anything about it, let's
393 * hence be strict on the top-level mount and lenient on the inner ones. */
394 log_debug_errno(r, "Couldn't remount submount '%s' for unexpected reason, ignoring: %m", x);
ef454fd1
YW
395 continue;
396 }
98df8089 397
ba8dced2 398 log_debug("Remounted %s.", x);
4349cd7c
LP
399 }
400 }
401}
402
7cce68e1
LP
403int bind_remount_one_with_mountinfo(
404 const char *path,
405 unsigned long new_flags,
406 unsigned long flags_mask,
407 FILE *proc_self_mountinfo) {
408
409 _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
2c5ff8ea
LP
410 unsigned long flags = 0;
411 struct libmnt_fs *fs;
412 const char *opts;
7cce68e1
LP
413 int r;
414
415 assert(path);
416 assert(proc_self_mountinfo);
417
4f5644db
LP
418 if ((flags_mask & ~MS_CONVERTIBLE_FLAGS) == 0 && !skip_mount_set_attr) {
419 /* Let's take a shortcut for all the flags we know how to convert into mount_setattr() flags */
420
421 if (mount_setattr(AT_FDCWD, path, AT_SYMLINK_NOFOLLOW,
422 &(struct mount_attr) {
423 .attr_set = ms_flags_to_mount_attr(new_flags & flags_mask),
424 .attr_clr = ms_flags_to_mount_attr(~new_flags & flags_mask),
425 }, MOUNT_ATTR_SIZE_VER0) < 0) {
426
427 log_debug_errno(errno, "mount_setattr() didn't work, falling back to classic remounting: %m");
428
429 if (ERRNO_IS_NOT_SUPPORTED(errno)) /* if not supported, then don't bother at all anymore */
430 skip_mount_set_attr = true;
431 } else
432 return 0; /* Nice, this worked! */
433 }
434
7cce68e1
LP
435 rewind(proc_self_mountinfo);
436
437 table = mnt_new_table();
438 if (!table)
439 return -ENOMEM;
440
441 r = mnt_table_parse_stream(table, proc_self_mountinfo, "/proc/self/mountinfo");
442 if (r < 0)
443 return r;
444
2c5ff8ea 445 fs = mnt_table_find_target(table, path, MNT_ITER_FORWARD);
0338df47
LP
446 if (!fs) {
447 if (laccess(path, F_OK) < 0) /* Hmm, it's not in the mount table, but does it exist at all? */
448 return -errno;
449
2c5ff8ea 450 return -EINVAL; /* Not a mount point we recognize */
0338df47 451 }
2c5ff8ea
LP
452
453 opts = mnt_fs_get_vfs_options(fs);
454 if (opts) {
455 r = mnt_optstr_get_flags(opts, &flags, mnt_get_builtin_optmap(MNT_LINUX_MAP));
456 if (r < 0)
457 log_debug_errno(r, "Could not get flags for '%s', ignoring: %m", path);
458 }
7cce68e1 459
2c5ff8ea 460 r = mount_nofollow(NULL, path, NULL, ((flags & ~flags_mask)|MS_BIND|MS_REMOUNT|new_flags) & ~MS_RELATIME, NULL);
b23c6a64
LP
461 if (r < 0) {
462 if (((flags ^ new_flags) & flags_mask & ~MS_RELATIME) != 0) /* Ignore MS_RELATIME again,
463 * since kernel adds it in
464 * everywhere, because it's the
465 * default. */
466 return r;
467
468 /* Let's handle redundant remounts gracefully */
469 log_debug_errno(r, "Failed to remount '%s' but flags already match what we want, ignoring: %m", path);
470 }
7cce68e1
LP
471
472 return 0;
473}
474
4349cd7c
LP
475int mount_move_root(const char *path) {
476 assert(path);
477
478 if (chdir(path) < 0)
479 return -errno;
480
481 if (mount(path, "/", NULL, MS_MOVE, NULL) < 0)
482 return -errno;
483
484 if (chroot(".") < 0)
485 return -errno;
486
7c248223 487 return RET_NERRNO(chdir("/"));
4349cd7c 488}
4e036b7a 489
3f2c0bec
LP
490int repeat_unmount(const char *path, int flags) {
491 bool done = false;
492
493 assert(path);
494
495 /* If there are multiple mounts on a mount point, this
496 * removes them all */
497
498 for (;;) {
499 if (umount2(path, flags) < 0) {
500
501 if (errno == EINVAL)
502 return done;
503
504 return -errno;
505 }
506
507 done = true;
508 }
509}
c4b41707 510
48b747fa
LP
511int mode_to_inaccessible_node(
512 const char *runtime_dir,
513 mode_t mode,
514 char **ret) {
515
516 /* This function maps a node type to a corresponding inaccessible file node. These nodes are created
517 * during early boot by PID 1. In some cases we lacked the privs to create the character and block
518 * devices (maybe because we run in an userns environment, or miss CAP_SYS_MKNOD, or run with a
519 * devices policy that excludes device nodes with major and minor of 0), but that's fine, in that
520 * case we use an AF_UNIX file node instead, which is not the same, but close enough for most
521 * uses. And most importantly, the kernel allows bind mounts from socket nodes to any non-directory
522 * file nodes, and that's the most important thing that matters.
523 *
524 * Note that the runtime directory argument shall be the top-level runtime directory, i.e. /run/ if
525 * we operate in system context and $XDG_RUNTIME_DIR if we operate in user context. */
526
e5f10caf
AZ
527 _cleanup_free_ char *d = NULL;
528 const char *node = NULL;
e5f10caf 529
48b747fa
LP
530 assert(ret);
531
532 if (!runtime_dir)
533 runtime_dir = "/run";
fe80fcc7 534
c4b41707
AP
535 switch(mode & S_IFMT) {
536 case S_IFREG:
48b747fa 537 node = "/systemd/inaccessible/reg";
e5f10caf 538 break;
fe80fcc7 539
c4b41707 540 case S_IFDIR:
48b747fa 541 node = "/systemd/inaccessible/dir";
e5f10caf 542 break;
fe80fcc7 543
c4b41707 544 case S_IFCHR:
48b747fa 545 node = "/systemd/inaccessible/chr";
e5f10caf 546 break;
fe80fcc7 547
c4b41707 548 case S_IFBLK:
48b747fa 549 node = "/systemd/inaccessible/blk";
e5f10caf 550 break;
fe80fcc7 551
c4b41707 552 case S_IFIFO:
48b747fa 553 node = "/systemd/inaccessible/fifo";
e5f10caf 554 break;
fe80fcc7 555
c4b41707 556 case S_IFSOCK:
48b747fa 557 node = "/systemd/inaccessible/sock";
e5f10caf 558 break;
c4b41707 559 }
e5f10caf
AZ
560 if (!node)
561 return -EINVAL;
562
48b747fa
LP
563 d = path_join(runtime_dir, node);
564 if (!d)
565 return -ENOMEM;
566
cbed1dc8
LP
567 /* On new kernels unprivileged users are permitted to create 0:0 char device nodes (because they also
568 * act as whiteout inode for overlayfs), but no other char or block device nodes. On old kernels no
569 * device node whatsoever may be created by unprivileged processes. Hence, if the caller asks for the
570 * inaccessible block device node let's see if the block device node actually exists, and if not,
571 * fall back to the character device node. From there fall back to the socket device node. This means
572 * in the best case we'll get the right device node type — but if not we'll hopefully at least get a
573 * device node at all. */
574
575 if (S_ISBLK(mode) &&
576 access(d, F_OK) < 0 && errno == ENOENT) {
577 free(d);
578 d = path_join(runtime_dir, "/systemd/inaccessible/chr");
579 if (!d)
580 return -ENOMEM;
581 }
582
583 if (IN_SET(mode & S_IFMT, S_IFBLK, S_IFCHR) &&
584 access(d, F_OK) < 0 && errno == ENOENT) {
48b747fa
LP
585 free(d);
586 d = path_join(runtime_dir, "/systemd/inaccessible/sock");
587 if (!d)
588 return -ENOMEM;
589 }
e5f10caf 590
48b747fa 591 *ret = TAKE_PTR(d);
e5f10caf 592 return 0;
c4b41707 593}
60e76d48 594
da185cd0 595int mount_flags_to_string(unsigned long flags, char **ret) {
1c092b62 596 static const struct {
da185cd0 597 unsigned long flag;
1c092b62
YW
598 const char *name;
599 } map[] = {
600 { .flag = MS_RDONLY, .name = "MS_RDONLY", },
601 { .flag = MS_NOSUID, .name = "MS_NOSUID", },
602 { .flag = MS_NODEV, .name = "MS_NODEV", },
603 { .flag = MS_NOEXEC, .name = "MS_NOEXEC", },
604 { .flag = MS_SYNCHRONOUS, .name = "MS_SYNCHRONOUS", },
605 { .flag = MS_REMOUNT, .name = "MS_REMOUNT", },
606 { .flag = MS_MANDLOCK, .name = "MS_MANDLOCK", },
607 { .flag = MS_DIRSYNC, .name = "MS_DIRSYNC", },
608 { .flag = MS_NOSYMFOLLOW, .name = "MS_NOSYMFOLLOW", },
609 { .flag = MS_NOATIME, .name = "MS_NOATIME", },
610 { .flag = MS_NODIRATIME, .name = "MS_NODIRATIME", },
611 { .flag = MS_BIND, .name = "MS_BIND", },
612 { .flag = MS_MOVE, .name = "MS_MOVE", },
613 { .flag = MS_REC, .name = "MS_REC", },
614 { .flag = MS_SILENT, .name = "MS_SILENT", },
615 { .flag = MS_POSIXACL, .name = "MS_POSIXACL", },
616 { .flag = MS_UNBINDABLE, .name = "MS_UNBINDABLE", },
617 { .flag = MS_PRIVATE, .name = "MS_PRIVATE", },
618 { .flag = MS_SLAVE, .name = "MS_SLAVE", },
619 { .flag = MS_SHARED, .name = "MS_SHARED", },
620 { .flag = MS_RELATIME, .name = "MS_RELATIME", },
621 { .flag = MS_KERNMOUNT, .name = "MS_KERNMOUNT", },
622 { .flag = MS_I_VERSION, .name = "MS_I_VERSION", },
623 { .flag = MS_STRICTATIME, .name = "MS_STRICTATIME", },
624 { .flag = MS_LAZYTIME, .name = "MS_LAZYTIME", },
625 };
626 _cleanup_free_ char *str = NULL;
627
4bee2333
YW
628 assert(ret);
629
1c092b62
YW
630 for (size_t i = 0; i < ELEMENTSOF(map); i++)
631 if (flags & map[i].flag) {
632 if (!strextend_with_separator(&str, "|", map[i].name))
633 return -ENOMEM;
634 flags &= ~map[i].flag;
635 }
636
637 if (!str || flags != 0)
638 if (strextendf_with_separator(&str, "|", "%lx", flags) < 0)
639 return -ENOMEM;
640
641 *ret = TAKE_PTR(str);
642 return 0;
60e76d48
ZJS
643}
644
511a8cfe 645int mount_verbose_full(
60e76d48
ZJS
646 int error_log_level,
647 const char *what,
648 const char *where,
649 const char *type,
650 unsigned long flags,
511a8cfe
LP
651 const char *options,
652 bool follow_symlink) {
60e76d48 653
6ef8df2b
YW
654 _cleanup_free_ char *fl = NULL, *o = NULL;
655 unsigned long f;
656 int r;
657
658 r = mount_option_mangle(options, flags, &f, &o);
659 if (r < 0)
660 return log_full_errno(error_log_level, r,
661 "Failed to mangle mount options %s: %m",
662 strempty(options));
60e76d48 663
1c092b62 664 (void) mount_flags_to_string(f, &fl);
60e76d48 665
6ef8df2b 666 if ((f & MS_REMOUNT) && !what && !type)
60e76d48 667 log_debug("Remounting %s (%s \"%s\")...",
6ef8df2b 668 where, strnull(fl), strempty(o));
60e76d48
ZJS
669 else if (!what && !type)
670 log_debug("Mounting %s (%s \"%s\")...",
6ef8df2b
YW
671 where, strnull(fl), strempty(o));
672 else if ((f & MS_BIND) && !type)
60e76d48 673 log_debug("Bind-mounting %s on %s (%s \"%s\")...",
6ef8df2b
YW
674 what, where, strnull(fl), strempty(o));
675 else if (f & MS_MOVE)
afe682bc 676 log_debug("Moving mount %s → %s (%s \"%s\")...",
6ef8df2b 677 what, where, strnull(fl), strempty(o));
60e76d48 678 else
3b493d94
LP
679 log_debug("Mounting %s (%s) on %s (%s \"%s\")...",
680 strna(what), strna(type), where, strnull(fl), strempty(o));
511a8cfe
LP
681
682 if (follow_symlink)
7c248223 683 r = RET_NERRNO(mount(what, where, type, f, o));
511a8cfe
LP
684 else
685 r = mount_nofollow(what, where, type, f, o);
686 if (r < 0)
687 return log_full_errno(error_log_level, r,
3ccf6126
LP
688 "Failed to mount %s (type %s) on %s (%s \"%s\"): %m",
689 strna(what), strna(type), where, strnull(fl), strempty(o));
60e76d48
ZJS
690 return 0;
691}
692
30f5d104
LP
693int umount_verbose(
694 int error_log_level,
695 const char *what,
696 int flags) {
697
698 assert(what);
699
60e76d48 700 log_debug("Umounting %s...", what);
30f5d104
LP
701
702 if (umount2(what, flags) < 0)
703 return log_full_errno(error_log_level, errno,
704 "Failed to unmount %s: %m", what);
705
60e76d48
ZJS
706 return 0;
707}
83555251 708
9e7f941a
YW
709int mount_option_mangle(
710 const char *options,
711 unsigned long mount_flags,
712 unsigned long *ret_mount_flags,
713 char **ret_remaining_options) {
714
715 const struct libmnt_optmap *map;
716 _cleanup_free_ char *ret = NULL;
9e7f941a
YW
717 int r;
718
719 /* This extracts mount flags from the mount options, and store
720 * non-mount-flag options to '*ret_remaining_options'.
721 * E.g.,
722 * "rw,nosuid,nodev,relatime,size=1630748k,mode=700,uid=1000,gid=1000"
723 * is split to MS_NOSUID|MS_NODEV|MS_RELATIME and
724 * "size=1630748k,mode=700,uid=1000,gid=1000".
725 * See more examples in test-mount-utils.c.
726 *
727 * Note that if 'options' does not contain any non-mount-flag options,
5238e957 728 * then '*ret_remaining_options' is set to NULL instead of empty string.
9e7f941a
YW
729 * Note that this does not check validity of options stored in
730 * '*ret_remaining_options'.
731 * Note that if 'options' is NULL, then this just copies 'mount_flags'
732 * to '*ret_mount_flags'. */
733
734 assert(ret_mount_flags);
735 assert(ret_remaining_options);
736
737 map = mnt_get_builtin_optmap(MNT_LINUX_MAP);
738 if (!map)
739 return -EINVAL;
740
25086b4c 741 for (const char *p = options;;) {
9e7f941a
YW
742 _cleanup_free_ char *word = NULL;
743 const struct libmnt_optmap *ent;
744
9b23679e 745 r = extract_first_word(&p, &word, ",", EXTRACT_KEEP_QUOTE);
9e7f941a
YW
746 if (r < 0)
747 return r;
748 if (r == 0)
749 break;
750
751 for (ent = map; ent->name; ent++) {
752 /* All entries in MNT_LINUX_MAP do not take any argument.
753 * Thus, ent->name does not contain "=" or "[=]". */
754 if (!streq(word, ent->name))
755 continue;
756
757 if (!(ent->mask & MNT_INVERT))
758 mount_flags |= ent->id;
759 else if (mount_flags & ent->id)
760 mount_flags ^= ent->id;
761
762 break;
763 }
764
765 /* If 'word' is not a mount flag, then store it in '*ret_remaining_options'. */
c2bc710b 766 if (!ent->name && !strextend_with_separator(&ret, ",", word))
9e7f941a
YW
767 return -ENOMEM;
768 }
769
770 *ret_mount_flags = mount_flags;
ae2a15bc 771 *ret_remaining_options = TAKE_PTR(ret);
9e7f941a
YW
772
773 return 0;
774}
6af52c3a 775
70599967 776static int mount_in_namespace(
6af52c3a
LB
777 pid_t target,
778 const char *propagate_path,
779 const char *incoming_path,
780 const char *src,
781 const char *dest,
782 bool read_only,
70599967
LB
783 bool make_file_or_directory,
784 const MountOptions *options,
785 bool is_image) {
6af52c3a
LB
786
787 _cleanup_close_pair_ int errno_pipe_fd[2] = { -1, -1 };
f7c18d3d 788 _cleanup_close_ int self_mntns_fd = -1, mntns_fd = -1, root_fd = -1, pidns_fd = -1, chased_src_fd = -1;
ddb6eeaf 789 char mount_slave[] = "/tmp/propagate.XXXXXX", *mount_tmp, *mount_outside, *p;
6af52c3a
LB
790 bool mount_slave_created = false, mount_slave_mounted = false,
791 mount_tmp_created = false, mount_tmp_mounted = false,
792 mount_outside_created = false, mount_outside_mounted = false;
2338a175 793 struct stat st, self_mntns_st;
6af52c3a
LB
794 pid_t child;
795 int r;
796
797 assert(target > 0);
798 assert(propagate_path);
799 assert(incoming_path);
800 assert(src);
801 assert(dest);
70599967 802 assert(!options || is_image);
6af52c3a 803
98f654fd 804 r = namespace_open(target, &pidns_fd, &mntns_fd, NULL, NULL, &root_fd);
2338a175
LB
805 if (r < 0)
806 return log_debug_errno(r, "Failed to retrieve FDs of the target process' namespace: %m");
807
808 if (fstat(mntns_fd, &st) < 0)
809 return log_debug_errno(errno, "Failed to fstat mount namespace FD of target process: %m");
810
811 r = namespace_open(0, NULL, &self_mntns_fd, NULL, NULL, NULL);
812 if (r < 0)
813 return log_debug_errno(r, "Failed to retrieve FDs of systemd's namespace: %m");
814
815 if (fstat(self_mntns_fd, &self_mntns_st) < 0)
816 return log_debug_errno(errno, "Failed to fstat mount namespace FD of systemd: %m");
817
818 /* We can't add new mounts at runtime if the process wasn't started in a namespace */
a9dac7a6 819 if (stat_inode_same(&st, &self_mntns_st))
2338a175
LB
820 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to activate bind mount in target, not running in a mount namespace");
821
ddb6eeaf
LP
822 /* One day, when bind mounting /proc/self/fd/n works across namespace boundaries we should rework
823 * this logic to make use of it... */
6af52c3a
LB
824
825 p = strjoina(propagate_path, "/");
826 r = laccess(p, F_OK);
827 if (r < 0)
828 return log_debug_errno(r == -ENOENT ? SYNTHETIC_ERRNO(EOPNOTSUPP) : r, "Target does not allow propagation of mount points");
829
f7c18d3d 830 r = chase_symlinks(src, NULL, CHASE_TRAIL_SLASH, NULL, &chased_src_fd);
6af52c3a
LB
831 if (r < 0)
832 return log_debug_errno(r, "Failed to resolve source path of %s: %m", src);
833
f7c18d3d
LB
834 if (fstat(chased_src_fd, &st) < 0)
835 return log_debug_errno(errno, "Failed to stat() resolved source path %s: %m", src);
6af52c3a 836 if (S_ISLNK(st.st_mode)) /* This shouldn't really happen, given that we just chased the symlinks above, but let's better be safe… */
f7c18d3d 837 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Source directory %s can't be a symbolic link", src);
6af52c3a
LB
838
839 /* Our goal is to install a new bind mount into the container,
840 possibly read-only. This is irritatingly complex
841 unfortunately, currently.
842
843 First, we start by creating a private playground in /tmp,
844 that we can mount MS_SLAVE. (Which is necessary, since
845 MS_MOVE cannot be applied to mounts with MS_SHARED parent
846 mounts.) */
847
848 if (!mkdtemp(mount_slave))
849 return log_debug_errno(errno, "Failed to create playground %s: %m", mount_slave);
850
851 mount_slave_created = true;
852
853 r = mount_nofollow_verbose(LOG_DEBUG, mount_slave, mount_slave, NULL, MS_BIND, NULL);
854 if (r < 0)
855 goto finish;
856
857 mount_slave_mounted = true;
858
859 r = mount_nofollow_verbose(LOG_DEBUG, NULL, mount_slave, NULL, MS_SLAVE, NULL);
860 if (r < 0)
861 goto finish;
862
863 /* Second, we mount the source file or directory to a directory inside of our MS_SLAVE playground. */
864 mount_tmp = strjoina(mount_slave, "/mount");
70599967
LB
865 if (is_image)
866 r = mkdir_p(mount_tmp, 0700);
867 else
868 r = make_mount_point_inode_from_stat(&st, mount_tmp, 0700);
6af52c3a
LB
869 if (r < 0) {
870 log_debug_errno(r, "Failed to create temporary mount point %s: %m", mount_tmp);
871 goto finish;
872 }
873
874 mount_tmp_created = true;
875
70599967 876 if (is_image)
60c5f700 877 r = verity_dissect_and_mount(FORMAT_PROC_FD_PATH(chased_src_fd), mount_tmp, options, NULL, NULL, NULL, NULL);
70599967 878 else
ddb6eeaf 879 r = mount_follow_verbose(LOG_DEBUG, FORMAT_PROC_FD_PATH(chased_src_fd), mount_tmp, NULL, MS_BIND, NULL);
6af52c3a
LB
880 if (r < 0)
881 goto finish;
882
883 mount_tmp_mounted = true;
884
885 /* Third, we remount the new bind mount read-only if requested. */
886 if (read_only) {
887 r = mount_nofollow_verbose(LOG_DEBUG, NULL, mount_tmp, NULL, MS_BIND|MS_REMOUNT|MS_RDONLY, NULL);
888 if (r < 0)
889 goto finish;
890 }
891
892 /* Fourth, we move the new bind mount into the propagation directory. This way it will appear there read-only
893 * right-away. */
894
895 mount_outside = strjoina(propagate_path, "/XXXXXX");
70599967 896 if (is_image || S_ISDIR(st.st_mode))
6af52c3a
LB
897 r = mkdtemp(mount_outside) ? 0 : -errno;
898 else {
899 r = mkostemp_safe(mount_outside);
900 safe_close(r);
901 }
902 if (r < 0) {
903 log_debug_errno(r, "Cannot create propagation file or directory %s: %m", mount_outside);
904 goto finish;
905 }
906
907 mount_outside_created = true;
908
909 r = mount_nofollow_verbose(LOG_DEBUG, mount_tmp, mount_outside, NULL, MS_MOVE, NULL);
910 if (r < 0)
911 goto finish;
912
913 mount_outside_mounted = true;
914 mount_tmp_mounted = false;
915
70599967 916 if (is_image || S_ISDIR(st.st_mode))
6af52c3a
LB
917 (void) rmdir(mount_tmp);
918 else
919 (void) unlink(mount_tmp);
920 mount_tmp_created = false;
921
922 (void) umount_verbose(LOG_DEBUG, mount_slave, UMOUNT_NOFOLLOW);
923 mount_slave_mounted = false;
924
925 (void) rmdir(mount_slave);
926 mount_slave_created = false;
927
928 if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0) {
929 log_debug_errno(errno, "Failed to create pipe: %m");
930 goto finish;
931 }
932
2338a175 933 r = namespace_fork("(sd-bindmnt)", "(sd-bindmnt-inner)", NULL, 0, FORK_RESET_SIGNALS|FORK_DEATHSIG,
98f654fd 934 pidns_fd, mntns_fd, -1, -1, root_fd, &child);
6af52c3a
LB
935 if (r < 0)
936 goto finish;
937 if (r == 0) {
2338a175 938 const char *mount_inside;
6af52c3a
LB
939
940 errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]);
941
6af52c3a 942 if (make_file_or_directory) {
70599967
LB
943 if (!is_image) {
944 (void) mkdir_parents(dest, 0755);
945 (void) make_mount_point_inode_from_stat(&st, dest, 0700);
946 } else
947 (void) mkdir_p(dest, 0755);
6af52c3a
LB
948 }
949
950 /* Fifth, move the mount to the right place inside */
951 mount_inside = strjoina(incoming_path, basename(mount_outside));
952 r = mount_nofollow_verbose(LOG_ERR, mount_inside, dest, NULL, MS_MOVE, NULL);
953 if (r < 0)
954 goto child_fail;
955
956 _exit(EXIT_SUCCESS);
957
958 child_fail:
959 (void) write(errno_pipe_fd[1], &r, sizeof(r));
960 errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]);
961
962 _exit(EXIT_FAILURE);
963 }
964
965 errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]);
966
967 r = wait_for_terminate_and_check("(sd-bindmnt)", child, 0);
968 if (r < 0) {
969 log_debug_errno(r, "Failed to wait for child: %m");
970 goto finish;
971 }
972 if (r != EXIT_SUCCESS) {
973 if (read(errno_pipe_fd[0], &r, sizeof(r)) == sizeof(r))
974 log_debug_errno(r, "Failed to mount: %m");
975 else
976 log_debug("Child failed.");
977 goto finish;
978 }
979
980finish:
981 if (mount_outside_mounted)
982 (void) umount_verbose(LOG_DEBUG, mount_outside, UMOUNT_NOFOLLOW);
983 if (mount_outside_created) {
70599967 984 if (is_image || S_ISDIR(st.st_mode))
6af52c3a
LB
985 (void) rmdir(mount_outside);
986 else
987 (void) unlink(mount_outside);
988 }
989
990 if (mount_tmp_mounted)
991 (void) umount_verbose(LOG_DEBUG, mount_tmp, UMOUNT_NOFOLLOW);
992 if (mount_tmp_created) {
70599967 993 if (is_image || S_ISDIR(st.st_mode))
6af52c3a
LB
994 (void) rmdir(mount_tmp);
995 else
996 (void) unlink(mount_tmp);
997 }
998
999 if (mount_slave_mounted)
1000 (void) umount_verbose(LOG_DEBUG, mount_slave, UMOUNT_NOFOLLOW);
1001 if (mount_slave_created)
1002 (void) rmdir(mount_slave);
1003
1004 return r;
1005}
70599967
LB
1006
1007int bind_mount_in_namespace(
1008 pid_t target,
1009 const char *propagate_path,
1010 const char *incoming_path,
1011 const char *src,
1012 const char *dest,
1013 bool read_only,
1014 bool make_file_or_directory) {
1015
1016 return mount_in_namespace(target, propagate_path, incoming_path, src, dest, read_only, make_file_or_directory, NULL, false);
1017}
1018
1019int mount_image_in_namespace(
1020 pid_t target,
1021 const char *propagate_path,
1022 const char *incoming_path,
1023 const char *src,
1024 const char *dest,
1025 bool read_only,
1026 bool make_file_or_directory,
1027 const MountOptions *options) {
1028
1029 return mount_in_namespace(target, propagate_path, incoming_path, src, dest, read_only, make_file_or_directory, options, true);
1030}
14a25e1f
LP
1031
1032int make_mount_point(const char *path) {
1033 int r;
1034
1035 assert(path);
1036
1037 /* If 'path' is already a mount point, does nothing and returns 0. If it is not it makes it one, and returns 1. */
1038
1039 r = path_is_mount_point(path, NULL, 0);
1040 if (r < 0)
1041 return log_debug_errno(r, "Failed to determine whether '%s' is a mount point: %m", path);
1042 if (r > 0)
1043 return 0;
1044
1045 r = mount_nofollow_verbose(LOG_DEBUG, path, path, NULL, MS_BIND|MS_REC, NULL);
1046 if (r < 0)
1047 return r;
1048
1049 return 1;
1050}
35fd3558
LP
1051
1052static int make_userns(uid_t uid_shift, uid_t uid_range) {
979b0ff2 1053 char line[DECIMAL_STR_MAX(uid_t)*3+3+1];
35fd3558 1054 _cleanup_close_ int userns_fd = -1;
35fd3558
LP
1055
1056 /* Allocates a userns file descriptor with the mapping we need. For this we'll fork off a child
1057 * process whose only purpose is to give us a new user namespace. It's killed when we got it. */
1058
35fd3558
LP
1059 xsprintf(line, UID_FMT " " UID_FMT " " UID_FMT "\n", 0, uid_shift, uid_range);
1060
35fd3558 1061 /* We always assign the same UID and GID ranges */
979b0ff2
LP
1062 userns_fd = userns_acquire(line, line);
1063 if (userns_fd < 0)
1064 return log_debug_errno(userns_fd, "Failed to acquire new userns: %m");
35fd3558
LP
1065
1066 return TAKE_FD(userns_fd);
1067}
1068
1069int remount_idmap(
1070 const char *p,
1071 uid_t uid_shift,
1072 uid_t uid_range) {
1073
1074 _cleanup_close_ int mount_fd = -1, userns_fd = -1;
1075 int r;
1076
1077 assert(p);
1078
1079 if (!userns_shift_range_valid(uid_shift, uid_range))
1080 return -EINVAL;
1081
1082 /* Clone the mount point */
1083 mount_fd = open_tree(-1, p, OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC);
1084 if (mount_fd < 0)
1085 return log_debug_errno(errno, "Failed to open tree of mounted filesystem '%s': %m", p);
1086
1087 /* Create a user namespace mapping */
1088 userns_fd = make_userns(uid_shift, uid_range);
1089 if (userns_fd < 0)
1090 return userns_fd;
1091
1092 /* Set the user namespace mapping attribute on the cloned mount point */
1093 if (mount_setattr(mount_fd, "", AT_EMPTY_PATH | AT_RECURSIVE,
1094 &(struct mount_attr) {
1095 .attr_set = MOUNT_ATTR_IDMAP,
1096 .userns_fd = userns_fd,
1097 }, sizeof(struct mount_attr)) < 0)
1098 return log_debug_errno(errno, "Failed to change bind mount attributes for '%s': %m", p);
1099
1100 /* Remove the old mount point */
1101 r = umount_verbose(LOG_DEBUG, p, UMOUNT_NOFOLLOW);
1102 if (r < 0)
1103 return r;
1104
1105 /* And place the cloned version in its place */
1106 if (move_mount(mount_fd, "", -1, p, MOVE_MOUNT_F_EMPTY_PATH) < 0)
1107 return log_debug_errno(errno, "Failed to attach UID mapped mount to '%s': %m", p);
1108
1109 return 0;
1110}
9c653536
ZJS
1111
1112int make_mount_point_inode_from_stat(const struct stat *st, const char *dest, mode_t mode) {
1113 assert(st);
1114 assert(dest);
1115
1116 if (S_ISDIR(st->st_mode))
1117 return mkdir_label(dest, mode);
1118 else
1119 return mknod(dest, S_IFREG|(mode & ~0111), 0);
1120}
1121
1122int make_mount_point_inode_from_path(const char *source, const char *dest, mode_t mode) {
1123 struct stat st;
1124
1125 assert(source);
1126 assert(dest);
1127
1128 if (stat(source, &st) < 0)
1129 return -errno;
1130
1131 return make_mount_point_inode_from_stat(&st, dest, mode);
1132}