From: Lennart Poettering Date: Wed, 20 Oct 2021 21:27:04 +0000 (+0200) Subject: mount-util: move opening of /proc/self/mountinfo into bind_remount_one_with_mountinfo() X-Git-Tag: v250-rc1~435^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=0289948eb4b22c01fe27e708056e6daaa719b9ae;p=thirdparty%2Fsystemd.git mount-util: move opening of /proc/self/mountinfo into bind_remount_one_with_mountinfo() Let's move things around a bit, and open /proc/self/mountinfo if needed inside of bind_remount_one_with_mountinfo(). That way bind_remount_one() can become a superthin inline wrapper around bind_remount_one_with_mountinfo(). Main benefit is that we don't even have to open /p/s/mi in case mount_setattr() actually worked for us. --- diff --git a/src/shared/mount-util.c b/src/shared/mount-util.c index 2dad667126f..813fdc1664a 100644 --- a/src/shared/mount-util.c +++ b/src/shared/mount-util.c @@ -168,12 +168,12 @@ int bind_remount_recursive_with_mountinfo( char **deny_list, FILE *proc_self_mountinfo) { + _cleanup_fclose_ FILE *proc_self_mountinfo_opened = NULL; _cleanup_set_free_ Set *done = NULL; unsigned n_tries = 0; int r; assert(prefix); - assert(proc_self_mountinfo); if ((flags_mask & ~MS_CONVERTIBLE_FLAGS) == 0 && strv_isempty(deny_list) && !skip_mount_set_attr) { /* Let's take a shortcut for all the flags we know how to convert into mount_setattr() flags */ @@ -199,6 +199,14 @@ int bind_remount_recursive_with_mountinfo( return 0; /* Nice, this worked! */ } + if (!proc_self_mountinfo) { + r = fopen_unlocked("/proc/self/mountinfo", "re", &proc_self_mountinfo_opened); + if (r < 0) + return r; + + proc_self_mountinfo = proc_self_mountinfo_opened; + } + /* Recursively remount a directory (and all its submounts) with desired flags (MS_READONLY, * MS_NOSUID, MS_NOEXEC). If the directory is already mounted, we reuse the mount and simply mark it * MS_BIND|MS_RDONLY (or remove the MS_RDONLY for read-write operation), ditto for other flags. If it @@ -392,22 +400,6 @@ int bind_remount_recursive_with_mountinfo( } } -int bind_remount_recursive( - const char *prefix, - unsigned long new_flags, - unsigned long flags_mask, - char **deny_list) { - - _cleanup_fclose_ FILE *proc_self_mountinfo = NULL; - int r; - - r = fopen_unlocked("/proc/self/mountinfo", "re", &proc_self_mountinfo); - if (r < 0) - return r; - - return bind_remount_recursive_with_mountinfo(prefix, new_flags, flags_mask, deny_list, proc_self_mountinfo); -} - int bind_remount_one_with_mountinfo( const char *path, unsigned long new_flags, diff --git a/src/shared/mount-util.h b/src/shared/mount-util.h index 36501c2c4a6..d9fb801e610 100644 --- a/src/shared/mount-util.h +++ b/src/shared/mount-util.h @@ -40,8 +40,12 @@ int mount_nofollow(const char *source, const char *target, const char *filesyste int repeat_unmount(const char *path, int flags); int umount_recursive(const char *target, int flags); -int bind_remount_recursive(const char *prefix, unsigned long new_flags, unsigned long flags_mask, char **deny_list); + int bind_remount_recursive_with_mountinfo(const char *prefix, unsigned long new_flags, unsigned long flags_mask, char **deny_list, FILE *proc_self_mountinfo); +static inline int bind_remount_recursive(const char *prefix, unsigned long new_flags, unsigned long flags_mask, char **deny_list) { + return bind_remount_recursive_with_mountinfo(prefix, new_flags, flags_mask, deny_list, NULL); +} + int bind_remount_one_with_mountinfo(const char *path, unsigned long new_flags, unsigned long flags_mask, FILE *proc_self_mountinfo); int mount_move_root(const char *path);