]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
mount-util: move opening of /proc/self/mountinfo into bind_remount_one_with_mountinfo() 21077/head
authorLennart Poettering <lennart@poettering.net>
Wed, 20 Oct 2021 21:27:04 +0000 (23:27 +0200)
committerLennart Poettering <lennart@poettering.net>
Mon, 25 Oct 2021 08:41:26 +0000 (10:41 +0200)
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.

src/shared/mount-util.c
src/shared/mount-util.h

index 2dad667126fcf139aa410b5fd3afe52cc1cb101f..813fdc1664a53cd3ef11bb521ef56bc771858b2b 100644 (file)
@@ -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,
index 36501c2c4a6757f6ebe1df202328b175edd24c07..d9fb801e6109582f8efd5bf831ad8c09436fb461 100644 (file)
@@ -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);