]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
mount-util: make mount_flags_to_string() show flag name instead of number
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 21 Jun 2021 05:52:55 +0000 (14:52 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 21 Jun 2021 12:14:20 +0000 (21:14 +0900)
This also adds missing MS_NOSYMFOLLOW flag. Moreover, this makes the
function always add unhandled flags in number.

src/basic/meson.build
src/basic/missing_mount.h [new file with mode: 0644]
src/shared/mount-util.c

index 846301fec5e79b132da4c3a8e0070ad09d6b5ef2..d1aa86e7b5e7cdc655f6493215316067a0d51388 100644 (file)
@@ -144,6 +144,7 @@ basic_sources = files('''
         missing_keyctl.h
         missing_magic.h
         missing_mman.h
+        missing_mount.h
         missing_network.h
         missing_prctl.h
         missing_random.h
diff --git a/src/basic/missing_mount.h b/src/basic/missing_mount.h
new file mode 100644 (file)
index 0000000..c60acf0
--- /dev/null
@@ -0,0 +1,8 @@
+#pragma once
+
+#include <sys/mount.h>
+
+/* dab741e0e02bd3c4f5e2e97be74b39df2523fc6e (5.10) */
+#ifndef MS_NOSYMFOLLOW
+#define MS_NOSYMFOLLOW 256
+#endif
index 34e0d53bc658a697e621fb676182c8811e4cf843..f73527a9d4e999dfe1376d182bc5eb6850d1a970 100644 (file)
@@ -17,6 +17,7 @@
 #include "fs-util.h"
 #include "hashmap.h"
 #include "libmount-util.h"
+#include "missing_mount.h"
 #include "missing_syscall.h"
 #include "mkdir.h"
 #include "mount-util.h"
@@ -530,71 +531,52 @@ int mode_to_inaccessible_node(
         return 0;
 }
 
-#define FLAG(name) (flags & name ? STRINGIFY(name) "|" : "")
-static char* mount_flags_to_string(long unsigned flags) {
-        char *x;
-        _cleanup_free_ char *y = NULL;
-        long unsigned overflow;
-
-        overflow = flags & ~(MS_RDONLY |
-                             MS_NOSUID |
-                             MS_NODEV |
-                             MS_NOEXEC |
-                             MS_SYNCHRONOUS |
-                             MS_REMOUNT |
-                             MS_MANDLOCK |
-                             MS_DIRSYNC |
-                             MS_NOATIME |
-                             MS_NODIRATIME |
-                             MS_BIND |
-                             MS_MOVE |
-                             MS_REC |
-                             MS_SILENT |
-                             MS_POSIXACL |
-                             MS_UNBINDABLE |
-                             MS_PRIVATE |
-                             MS_SLAVE |
-                             MS_SHARED |
-                             MS_RELATIME |
-                             MS_KERNMOUNT |
-                             MS_I_VERSION |
-                             MS_STRICTATIME |
-                             MS_LAZYTIME);
-
-        if (flags == 0 || overflow != 0)
-                if (asprintf(&y, "%lx", overflow) < 0)
-                        return NULL;
-
-        x = strjoin(FLAG(MS_RDONLY),
-                    FLAG(MS_NOSUID),
-                    FLAG(MS_NODEV),
-                    FLAG(MS_NOEXEC),
-                    FLAG(MS_SYNCHRONOUS),
-                    FLAG(MS_REMOUNT),
-                    FLAG(MS_MANDLOCK),
-                    FLAG(MS_DIRSYNC),
-                    FLAG(MS_NOATIME),
-                    FLAG(MS_NODIRATIME),
-                    FLAG(MS_BIND),
-                    FLAG(MS_MOVE),
-                    FLAG(MS_REC),
-                    FLAG(MS_SILENT),
-                    FLAG(MS_POSIXACL),
-                    FLAG(MS_UNBINDABLE),
-                    FLAG(MS_PRIVATE),
-                    FLAG(MS_SLAVE),
-                    FLAG(MS_SHARED),
-                    FLAG(MS_RELATIME),
-                    FLAG(MS_KERNMOUNT),
-                    FLAG(MS_I_VERSION),
-                    FLAG(MS_STRICTATIME),
-                    FLAG(MS_LAZYTIME),
-                    y);
-        if (!x)
-                return NULL;
-        if (!y)
-                x[strlen(x) - 1] = '\0'; /* truncate the last | */
-        return x;
+static int mount_flags_to_string(long unsigned flags, char **ret) {
+        static const struct {
+                long unsigned flag;
+                const char *name;
+        } map[] = {
+                { .flag = MS_RDONLY,      .name = "MS_RDONLY",      },
+                { .flag = MS_NOSUID,      .name = "MS_NOSUID",      },
+                { .flag = MS_NODEV,       .name = "MS_NODEV",       },
+                { .flag = MS_NOEXEC,      .name = "MS_NOEXEC",      },
+                { .flag = MS_SYNCHRONOUS, .name = "MS_SYNCHRONOUS", },
+                { .flag = MS_REMOUNT,     .name = "MS_REMOUNT",     },
+                { .flag = MS_MANDLOCK,    .name = "MS_MANDLOCK",    },
+                { .flag = MS_DIRSYNC,     .name = "MS_DIRSYNC",     },
+                { .flag = MS_NOSYMFOLLOW, .name = "MS_NOSYMFOLLOW", },
+                { .flag = MS_NOATIME,     .name = "MS_NOATIME",     },
+                { .flag = MS_NODIRATIME,  .name = "MS_NODIRATIME",  },
+                { .flag = MS_BIND,        .name = "MS_BIND",        },
+                { .flag = MS_MOVE,        .name = "MS_MOVE",        },
+                { .flag = MS_REC,         .name = "MS_REC",         },
+                { .flag = MS_SILENT,      .name = "MS_SILENT",      },
+                { .flag = MS_POSIXACL,    .name = "MS_POSIXACL",    },
+                { .flag = MS_UNBINDABLE,  .name = "MS_UNBINDABLE",  },
+                { .flag = MS_PRIVATE,     .name = "MS_PRIVATE",     },
+                { .flag = MS_SLAVE,       .name = "MS_SLAVE",       },
+                { .flag = MS_SHARED,      .name = "MS_SHARED",      },
+                { .flag = MS_RELATIME,    .name = "MS_RELATIME",    },
+                { .flag = MS_KERNMOUNT,   .name = "MS_KERNMOUNT",   },
+                { .flag = MS_I_VERSION,   .name = "MS_I_VERSION",   },
+                { .flag = MS_STRICTATIME, .name = "MS_STRICTATIME", },
+                { .flag = MS_LAZYTIME,    .name = "MS_LAZYTIME",    },
+        };
+        _cleanup_free_ char *str = NULL;
+
+        for (size_t i = 0; i < ELEMENTSOF(map); i++)
+                if (flags & map[i].flag) {
+                        if (!strextend_with_separator(&str, "|", map[i].name))
+                                return -ENOMEM;
+                        flags &= ~map[i].flag;
+                }
+
+        if (!str || flags != 0)
+                if (strextendf_with_separator(&str, "|", "%lx", flags) < 0)
+                        return -ENOMEM;
+
+        *ret = TAKE_PTR(str);
+        return 0;
 }
 
 int mount_verbose_full(
@@ -616,7 +598,7 @@ int mount_verbose_full(
                                       "Failed to mangle mount options %s: %m",
                                       strempty(options));
 
-        fl = mount_flags_to_string(f);
+        (void) mount_flags_to_string(f, &fl);
 
         if ((f & MS_REMOUNT) && !what && !type)
                 log_debug("Remounting %s (%s \"%s\")...",