From 72a9f4fa453aa7cd70dac6dc3b2f43aba8f6a060 Mon Sep 17 00:00:00 2001
From: Mike Yuan <me@yhndnzj.com>
Date: Thu, 22 Feb 2024 13:03:09 +0800
Subject: [PATCH] shared/mount-setup: minor modernization

---
 src/shared/mount-setup.c | 29 ++++++++++++++---------------
 src/shared/mount-setup.h |  6 +++---
 2 files changed, 17 insertions(+), 18 deletions(-)

diff --git a/src/shared/mount-setup.c b/src/shared/mount-setup.c
index a492e7127ff..5dfb30ccd1b 100644
--- a/src/shared/mount-setup.c
+++ b/src/shared/mount-setup.c
@@ -167,8 +167,11 @@ static int mount_one(const MountPoint *p, bool relabel) {
         int r, priority;
 
         assert(p);
+        assert(p->what);
+        assert(p->where);
+        assert(p->type);
 
-        priority = (p->mode & MNT_FATAL) ? LOG_ERR : LOG_DEBUG;
+        priority = FLAGS_SET(p->mode, MNT_FATAL) ? LOG_ERR : LOG_DEBUG;
 
         if (p->condition_fn && !p->condition_fn())
                 return 0;
@@ -180,13 +183,13 @@ static int mount_one(const MountPoint *p, bool relabel) {
         r = path_is_mount_point_full(p->where, /* root = */ NULL, AT_SYMLINK_FOLLOW);
         if (r < 0 && r != -ENOENT) {
                 log_full_errno(priority, r, "Failed to determine whether %s is a mount point: %m", p->where);
-                return (p->mode & MNT_FATAL) ? r : 0;
+                return FLAGS_SET(p->mode, MNT_FATAL) ? r : 0;
         }
         if (r > 0)
                 return 0;
 
         /* Skip securityfs in a container */
-        if (!(p->mode & MNT_IN_CONTAINER) && detect_container() > 0)
+        if (!FLAGS_SET(p->mode, MNT_IN_CONTAINER) && detect_container() > 0)
                 return 0;
 
         /* The access mode here doesn't really matter too much, since
@@ -207,39 +210,35 @@ static int mount_one(const MountPoint *p, bool relabel) {
         else
                 r = mount_nofollow_verbose(priority, p->what, p->where, p->type, p->flags, p->options);
         if (r < 0)
-                return (p->mode & MNT_FATAL) ? r : 0;
+                return FLAGS_SET(p->mode, MNT_FATAL) ? r : 0;
 
         /* Relabel again, since we now mounted something fresh here */
         if (relabel)
                 (void) label_fix(p->where, 0);
 
-        if (p->mode & MNT_CHECK_WRITABLE) {
+        if (FLAGS_SET(p->mode, MNT_CHECK_WRITABLE))
                 if (access(p->where, W_OK) < 0) {
                         r = -errno;
 
                         (void) umount2(p->where, UMOUNT_NOFOLLOW);
                         (void) rmdir(p->where);
 
-                        log_full_errno(priority, r, "Mount point %s not writable after mounting, undoing: %m", p->where);
-                        return (p->mode & MNT_FATAL) ? r : 0;
+                        log_full_errno(priority, r, "Mount point '%s' not writable after mounting, undoing: %m", p->where);
+                        return FLAGS_SET(p->mode, MNT_FATAL) ? r : 0;
                 }
-        }
 
         return 1;
 }
 
 static int mount_points_setup(size_t n, bool loaded_policy) {
-        int ret = 0, r;
+        int r = 0;
 
         assert(n <= ELEMENTSOF(mount_table));
 
-        FOREACH_ARRAY(mp, mount_table, n) {
-                r = mount_one(mp, loaded_policy);
-                if (r != 0 && ret >= 0)
-                        ret = r;
-        }
+        FOREACH_ARRAY(mp, mount_table, n)
+                RET_GATHER(r, mount_one(mp, loaded_policy));
 
-        return ret;
+        return r;
 }
 
 int mount_setup_early(void) {
diff --git a/src/shared/mount-setup.h b/src/shared/mount-setup.h
index 29bd62f6bf1..584722b48ef 100644
--- a/src/shared/mount-setup.h
+++ b/src/shared/mount-setup.h
@@ -3,10 +3,10 @@
 
 #include <stdbool.h>
 
+bool mount_point_is_api(const char *path);
+bool mount_point_ignore(const char *path);
+
 int mount_setup_early(void);
 int mount_setup(bool loaded_policy, bool leave_propagation);
 
 int mount_cgroup_controllers(void);
-
-bool mount_point_is_api(const char *path);
-bool mount_point_ignore(const char *path);