]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
mount-util: make mount_switch_root() take a mount propagation flag 25735/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 14 Dec 2022 06:38:54 +0000 (15:38 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 15 Dec 2022 05:17:22 +0000 (14:17 +0900)
src/core/namespace.c
src/nspawn/nspawn.c
src/shared/mount-util.c
src/shared/mount-util.h

index af30f40503ddecaae6f1de12668a0fcbf6170073..3bac6bb6a3b9bcdfc79c101b3500d0b0e8e40188 100644 (file)
@@ -2488,7 +2488,7 @@ int setup_namespace(
                 goto finish;
 
         /* MS_MOVE does not work on MS_SHARED so the remount MS_SHARED will be done later */
-        r = mount_switch_root(root, MOUNT_ATTR_PROPAGATION_INHERIT);
+        r = mount_switch_root(root, /* mount_propagation_flag = */ 0);
         if (r == -EINVAL && root_directory) {
                 /* If we are using root_directory and we don't have privileges (ie: user manager in a user
                  * namespace) and the root_directory is already a mount point in the parent namespace,
@@ -2498,7 +2498,7 @@ int setup_namespace(
                 r = mount_nofollow_verbose(LOG_DEBUG, root, root, NULL, MS_BIND|MS_REC, NULL);
                 if (r < 0)
                         goto finish;
-                r = mount_switch_root(root, MOUNT_ATTR_PROPAGATION_INHERIT);
+                r = mount_switch_root(root, /* mount_propagation_flag = */ 0);
         }
         if (r < 0) {
                 log_debug_errno(r, "Failed to mount root with MS_MOVE: %m");
index 324cd0e69aaa2063d531c6a47ed92c2b7933455b..a1bf5bb1d19bc8841c8aca98b44022c388432d37 100644 (file)
@@ -3973,7 +3973,7 @@ static int outer_child(
          * directory mount to root later on.
          * https://github.com/systemd/systemd/issues/3847#issuecomment-562735251
          */
-        r = mount_switch_root(directory, MOUNT_ATTR_PROPAGATION_SHARED);
+        r = mount_switch_root(directory, MS_SHARED);
         if (r < 0)
                 return log_error_errno(r, "Failed to move root directory: %m");
 
index 7817ebc0128ffd19c12b44a1bda5f65f6d85d10e..8fce74297bd2ee5e60dd434abc213507a9178804 100644 (file)
@@ -430,30 +430,6 @@ int bind_remount_one_with_mountinfo(
         return 0;
 }
 
-static const char *const mount_attr_propagation_type_table[_MOUNT_ATTR_PROPAGATION_TYPE_MAX] = {
-        [MOUNT_ATTR_PROPAGATION_INHERIT]   = "inherited",
-        [MOUNT_ATTR_PROPAGATION_PRIVATE]   = "private",
-        [MOUNT_ATTR_PROPAGATION_DEPENDENT] = "dependent",
-        [MOUNT_ATTR_PROPAGATION_SHARED]    = "shared",
-};
-
-DEFINE_STRING_TABLE_LOOKUP(mount_attr_propagation_type, MountAttrPropagationType);
-
-static unsigned long mount_attr_propagation_type_to_flag(MountAttrPropagationType t) {
-        switch (t) {
-        case MOUNT_ATTR_PROPAGATION_INHERIT:
-                return 0;
-        case MOUNT_ATTR_PROPAGATION_PRIVATE:
-                return MS_PRIVATE;
-        case MOUNT_ATTR_PROPAGATION_DEPENDENT:
-                return MS_SLAVE;
-        case MOUNT_ATTR_PROPAGATION_SHARED:
-                return MS_SHARED;
-        default:
-                assert_not_reached();
-        }
-}
-
 static int mount_switch_root_pivot(const char *path, int fd_newroot) {
         _cleanup_close_ int fd_oldroot = -EBADF;
 
@@ -497,12 +473,12 @@ static int mount_switch_root_move(const char *path) {
         return 0;
 }
 
-int mount_switch_root(const char *path, MountAttrPropagationType type) {
+int mount_switch_root(const char *path, unsigned long mount_propagation_flag) {
         _cleanup_close_ int fd_newroot = -EBADF;
-        unsigned long flags;
         int r;
 
         assert(path);
+        assert(mount_propagation_flag_is_valid(mount_propagation_flag));
 
         fd_newroot = open(path, O_PATH|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
         if (fd_newroot < 0)
@@ -522,11 +498,13 @@ int mount_switch_root(const char *path, MountAttrPropagationType type) {
         if (r < 0)
                 return log_debug_errno(r, "Failed to switch to new rootfs '%s': %m", path);
 
-        /* Finally, let's establish the requested propagation type. */
-        flags = mount_attr_propagation_type_to_flag(type);
-        if ((flags != 0) && mount(NULL, ".", NULL, flags|MS_REC, 0) < 0)
+        /* Finally, let's establish the requested propagation flags. */
+        if (mount_propagation_flag == 0)
+                return 0;
+
+        if (mount(NULL, ".", NULL, mount_propagation_flag | MS_REC, 0) < 0)
                 return log_debug_errno(errno, "Failed to turn new rootfs '%s' into %s mount: %m",
-                                       mount_attr_propagation_type_to_string(type), path);
+                                       mount_propagation_flag_to_string(mount_propagation_flag), path);
 
         return 0;
 }
index bc28063d6133e748145c659ff2ece315befaeeeb..7554bf828e123f0595738154a253f4dd6f497adc 100644 (file)
 #include "errno-util.h"
 #include "macro.h"
 
-typedef enum MountAttrPropagationType {
-        MOUNT_ATTR_PROPAGATION_INHERIT,   /* no special MS_* propagation flags */
-        MOUNT_ATTR_PROPAGATION_PRIVATE,   /* MS_PRIVATE */
-        MOUNT_ATTR_PROPAGATION_DEPENDENT, /* MS_SLAVE */
-        MOUNT_ATTR_PROPAGATION_SHARED,    /* MS_SHARE */
-
-        _MOUNT_ATTR_PROPAGATION_TYPE_MAX,
-        _MOUNT_ATTR_PROPAGATION_TYPE_INVALID = -EINVAL,
-} MountAttrPropagationType;
-
-const char* mount_attr_propagation_type_to_string(MountAttrPropagationType t) _const_;
-MountAttrPropagationType mount_attr_propagation_type_from_string(const char *s) _pure_;
-
 int repeat_unmount(const char *path, int flags);
 int umount_recursive(const char *target, int flags);
 
@@ -34,7 +21,7 @@ static inline int bind_remount_recursive(const char *prefix, unsigned long new_f
 
 int bind_remount_one_with_mountinfo(const char *path, unsigned long new_flags, unsigned long flags_mask, FILE *proc_self_mountinfo);
 
-int mount_switch_root(const char *path, MountAttrPropagationType type);
+int mount_switch_root(const char *path, unsigned long mount_propagation_flag);
 
 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(FILE*, endmntent, NULL);
 #define _cleanup_endmntent_ _cleanup_(endmntentp)