ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
698723
From ca634baa10e2249d4a706d59b67be764867e5f32 Mon Sep 17 00:00:00 2001
698723
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
698723
Date: Mon, 30 Nov 2020 10:37:06 +0100
698723
Subject: [PATCH] shared/mount-util: convert to libmount
698723
698723
It seems better to use just a single parsing algorithm for /proc/self/mountinfo.
698723
698723
Also, unify the naming of variables in all places that use mnt_table_next_fs().
698723
It makes it easier to compare the different call sites.
698723
698723
(cherry picked from commit 13dcfe4661b467131c943620d0f44711798bfd54)
698723
698723
Related: #1885143
698723
---
698723
 src/basic/mount-util.c | 133 ++++++++++++++++++-----------------------
698723
 src/core/mount.c       |  22 +++----
698723
 src/core/umount.c      |  14 ++---
698723
 3 files changed, 76 insertions(+), 93 deletions(-)
698723
698723
diff --git a/src/basic/mount-util.c b/src/basic/mount-util.c
698723
index 5b04e21f34..bac1a25cc8 100644
698723
--- a/src/basic/mount-util.c
698723
+++ b/src/basic/mount-util.c
698723
@@ -13,7 +13,6 @@
698723
 #include <libmount.h>
698723
 
698723
 #include "alloc-util.h"
698723
-#include "escape.h"
698723
 #include "extract-word.h"
698723
 #include "fd-util.h"
698723
 #include "fileio.h"
698723
@@ -27,6 +26,9 @@
698723
 #include "string-util.h"
698723
 #include "strv.h"
698723
 
698723
+DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_table*, mnt_free_table);
698723
+DEFINE_TRIVIAL_CLEANUP_FUNC(struct libmnt_iter*, mnt_free_iter);
698723
+
698723
 /* This is the original MAX_HANDLE_SZ definition from the kernel, when the API was introduced. We use that in place of
698723
  * any more currently defined value to future-proof things: if the size is increased in the API headers, and our code
698723
  * is recompiled then it would cease working on old kernels, as those refuse any sizes larger than this value with
698723
@@ -313,55 +315,43 @@ int umount_recursive(const char *prefix, int flags) {
698723
          * unmounting them until they are gone. */
698723
 
698723
         do {
698723
-                _cleanup_fclose_ FILE *proc_self_mountinfo = NULL;
698723
+                _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
698723
+                _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
698723
 
698723
                 again = false;
698723
-                r = 0;
698723
 
698723
-                proc_self_mountinfo = fopen("/proc/self/mountinfo", "re");
698723
-                if (!proc_self_mountinfo)
698723
-                        return -errno;
698723
+                table = mnt_new_table();
698723
+                iter = mnt_new_iter(MNT_ITER_FORWARD);
698723
+                if (!table || !iter)
698723
+                        return -ENOMEM;
698723
 
698723
-                (void) __fsetlocking(proc_self_mountinfo, FSETLOCKING_BYCALLER);
698723
+                r = mnt_table_parse_mtab(table, NULL);
698723
+                if (r < 0)
698723
+                        return log_debug_errno(r, "Failed to parse /proc/self/mountinfo: %m");
698723
 
698723
                 for (;;) {
698723
-                        _cleanup_free_ char *path = NULL, *p = NULL;
698723
-                        int k;
698723
-
698723
-                        k = fscanf(proc_self_mountinfo,
698723
-                                   "%*s "       /* (1) mount id */
698723
-                                   "%*s "       /* (2) parent id */
698723
-                                   "%*s "       /* (3) major:minor */
698723
-                                   "%*s "       /* (4) root */
698723
-                                   "%ms "       /* (5) mount point */
698723
-                                   "%*s"        /* (6) mount options */
698723
-                                   "%*[^-]"     /* (7) optional fields */
698723
-                                   "- "         /* (8) separator */
698723
-                                   "%*s "       /* (9) file system type */
698723
-                                   "%*s"        /* (10) mount source */
698723
-                                   "%*s"        /* (11) mount options 2 */
698723
-                                   "%*[^\n]",   /* some rubbish at the end */
698723
-                                   &path);
698723
-                        if (k != 1) {
698723
-                                if (k == EOF)
698723
-                                        break;
698723
+                        struct libmnt_fs *fs;
698723
+                        const char *path;
698723
 
698723
-                                continue;
698723
-                        }
698723
-
698723
-                        r = cunescape(path, UNESCAPE_RELAX, &p);
698723
+                        r = mnt_table_next_fs(table, iter, &fs);
698723
+                        if (r == 1)
698723
+                                break;
698723
                         if (r < 0)
698723
-                                return r;
698723
+                                return log_debug_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
698723
 
698723
-                        if (!path_startswith(p, prefix))
698723
+                        path = mnt_fs_get_target(fs);
698723
+                        if (!path)
698723
                                 continue;
698723
 
698723
-                        if (umount2(p, flags) < 0) {
698723
-                                r = log_debug_errno(errno, "Failed to umount %s: %m", p);
698723
+                        if (!path_startswith(path, prefix))
698723
+                                continue;
698723
+
698723
+                        if (umount2(path, flags) < 0) {
698723
+                                r = log_debug_errno(errno, "Failed to umount %s: %m", path);
698723
                                 continue;
698723
                         }
698723
 
698723
-                        log_debug("Successfully unmounted %s", p);
698723
+                        log_debug("Successfully unmounted %s", path);
698723
 
698723
                         again = true;
698723
                         n++;
698723
@@ -416,6 +406,8 @@ int bind_remount_recursive_with_mountinfo(const char *prefix, bool ro, char **bl
698723
 
698723
         for (;;) {
698723
                 _cleanup_set_free_free_ Set *todo = NULL;
698723
+                _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
698723
+                _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
698723
                 bool top_autofs = false;
698723
                 char *x;
698723
                 unsigned long orig_flags;
698723
@@ -424,58 +416,52 @@ int bind_remount_recursive_with_mountinfo(const char *prefix, bool ro, char **bl
698723
                 if (!todo)
698723
                         return -ENOMEM;
698723
 
698723
+                table = mnt_new_table();
698723
+                iter = mnt_new_iter(MNT_ITER_FORWARD);
698723
+                if (!table || !iter)
698723
+                        return -ENOMEM;
698723
+
698723
                 rewind(proc_self_mountinfo);
698723
 
698723
-                for (;;) {
698723
-                        _cleanup_free_ char *path = NULL, *p = NULL, *type = NULL;
698723
-                        int k;
698723
-
698723
-                        k = fscanf(proc_self_mountinfo,
698723
-                                   "%*s "       /* (1) mount id */
698723
-                                   "%*s "       /* (2) parent id */
698723
-                                   "%*s "       /* (3) major:minor */
698723
-                                   "%*s "       /* (4) root */
698723
-                                   "%ms "       /* (5) mount point */
698723
-                                   "%*s"        /* (6) mount options (superblock) */
698723
-                                   "%*[^-]"     /* (7) optional fields */
698723
-                                   "- "         /* (8) separator */
698723
-                                   "%ms "       /* (9) file system type */
698723
-                                   "%*s"        /* (10) mount source */
698723
-                                   "%*s"        /* (11) mount options (bind mount) */
698723
-                                   "%*[^\n]",   /* some rubbish at the end */
698723
-                                   &path,
698723
-                                   &type);
698723
-                        if (k != 2) {
698723
-                                if (k == EOF)
698723
-                                        break;
698723
+                r = mnt_table_parse_stream(table, proc_self_mountinfo, "/proc/self/mountinfo");
698723
+                if (r < 0)
698723
+                        return log_debug_errno(r, "Failed to parse /proc/self/mountinfo: %m");
698723
 
698723
-                                continue;
698723
-                        }
698723
+                for (;;) {
698723
+                        struct libmnt_fs *fs;
698723
+                        const char *path, *type;
698723
 
698723
-                        r = cunescape(path, UNESCAPE_RELAX, &p);
698723
+                        r = mnt_table_next_fs(table, iter, &fs);
698723
+                        if (r == 1)
698723
+                                break;
698723
                         if (r < 0)
698723
-                                return r;
698723
+                                return log_debug_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
698723
+
698723
+                        path = mnt_fs_get_target(fs);
698723
+                        type = mnt_fs_get_fstype(fs);
698723
+                        if (!path || !type)
698723
+                                continue;
698723
 
698723
-                        if (!path_startswith(p, cleaned))
698723
+                        if (!path_startswith(path, cleaned))
698723
                                 continue;
698723
 
698723
-                        /* Ignore this mount if it is blacklisted, but only if it isn't the top-level mount we shall
698723
-                         * operate on. */
698723
-                        if (!path_equal(cleaned, p)) {
698723
+                        /* Ignore this mount if it is blacklisted, but only if it isn't the top-level mount
698723
+                         * we shall operate on. */
698723
+                        if (!path_equal(path, cleaned)) {
698723
                                 bool blacklisted = false;
698723
                                 char **i;
698723
 
698723
                                 STRV_FOREACH(i, blacklist) {
698723
-
698723
                                         if (path_equal(*i, cleaned))
698723
                                                 continue;
698723
 
698723
                                         if (!path_startswith(*i, cleaned))
698723
                                                 continue;
698723
 
698723
-                                        if (path_startswith(p, *i)) {
698723
+                                        if (path_startswith(path, *i)) {
698723
                                                 blacklisted = true;
698723
-                                                log_debug("Not remounting %s, because blacklisted by %s, called for %s", p, *i, cleaned);
698723
+                                                log_debug("Not remounting %s blacklisted by %s, called for %s",
698723
+                                                          path, *i, cleaned);
698723
                                                 break;
698723
                                         }
698723
                                 }
698723
@@ -490,15 +476,12 @@ int bind_remount_recursive_with_mountinfo(const char *prefix, bool ro, char **bl
698723
                          * already triggered, then we will find
698723
                          * another entry for this. */
698723
                         if (streq(type, "autofs")) {
698723
-                                top_autofs = top_autofs || path_equal(cleaned, p);
698723
+                                top_autofs = top_autofs || path_equal(path, cleaned);
698723
                                 continue;
698723
                         }
698723
 
698723
-                        if (!set_contains(done, p)) {
698723
-                                r = set_consume(todo, p);
698723
-                                p = NULL;
698723
-                                if (r == -EEXIST)
698723
-                                        continue;
698723
+                        if (!set_contains(done, path)) {
698723
+                                r = set_put_strdup(todo, path);
698723
                                 if (r < 0)
698723
                                         return r;
698723
                         }
698723
diff --git a/src/core/mount.c b/src/core/mount.c
698723
index 076dfd06a3..7e80a0c974 100644
698723
--- a/src/core/mount.c
698723
+++ b/src/core/mount.c
698723
@@ -1606,18 +1606,18 @@ fail:
698723
 }
698723
 
698723
 static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
698723
-        _cleanup_(mnt_free_tablep) struct libmnt_table *t = NULL;
698723
-        _cleanup_(mnt_free_iterp) struct libmnt_iter *i = NULL;
698723
-        int r = 0;
698723
+        _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
698723
+        _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
698723
+        int r;
698723
 
698723
         assert(m);
698723
 
698723
-        t = mnt_new_table();
698723
-        i = mnt_new_iter(MNT_ITER_FORWARD);
698723
-        if (!t || !i)
698723
+        table = mnt_new_table();
698723
+        iter = mnt_new_iter(MNT_ITER_FORWARD);
698723
+        if (!table || !iter)
698723
                 return log_oom();
698723
 
698723
-        r = mnt_table_parse_mtab(t, NULL);
698723
+        r = mnt_table_parse_mtab(table, NULL);
698723
         if (r < 0)
698723
                 return log_error_errno(r, "Failed to parse /proc/self/mountinfo: %m");
698723
 
698723
@@ -1628,11 +1628,11 @@ static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
698723
                 _cleanup_free_ char *d = NULL, *p = NULL;
698723
                 int k;
698723
 
698723
-                k = mnt_table_next_fs(t, i, &fs);
698723
-                if (k == 1)
698723
+                r = mnt_table_next_fs(table, iter, &fs);
698723
+                if (r == 1)
698723
                         break;
698723
-                if (k < 0)
698723
-                        return log_error_errno(k, "Failed to get next entry from /proc/self/mountinfo: %m");
698723
+                if (r < 0)
698723
+                        return log_error_errno(r, "Failed to get next entry from /proc/self/mountinfo: %m");
698723
 
698723
                 device = mnt_fs_get_source(fs);
698723
                 path = mnt_fs_get_target(fs);
698723
diff --git a/src/core/umount.c b/src/core/umount.c
698723
index 241fe6fc62..3f02bf141a 100644
698723
--- a/src/core/umount.c
698723
+++ b/src/core/umount.c
698723
@@ -55,18 +55,18 @@ void mount_points_list_free(MountPoint **head) {
698723
 }
698723
 
698723
 int mount_points_list_get(const char *mountinfo, MountPoint **head) {
698723
-        _cleanup_(mnt_free_tablep) struct libmnt_table *t = NULL;
698723
-        _cleanup_(mnt_free_iterp) struct libmnt_iter *i = NULL;
698723
+        _cleanup_(mnt_free_tablep) struct libmnt_table *table = NULL;
698723
+        _cleanup_(mnt_free_iterp) struct libmnt_iter *iter = NULL;
698723
         int r;
698723
 
698723
         assert(head);
698723
 
698723
-        t = mnt_new_table();
698723
-        i = mnt_new_iter(MNT_ITER_FORWARD);
698723
-        if (!t || !i)
698723
+        table = mnt_new_table();
698723
+        iter = mnt_new_iter(MNT_ITER_FORWARD);
698723
+        if (!table || !iter)
698723
                 return log_oom();
698723
 
698723
-        r = mnt_table_parse_mtab(t, mountinfo);
698723
+        r = mnt_table_parse_mtab(table, mountinfo);
698723
         if (r < 0)
698723
                 return log_error_errno(r, "Failed to parse %s: %m", mountinfo);
698723
 
698723
@@ -79,7 +79,7 @@ int mount_points_list_get(const char *mountinfo, MountPoint **head) {
698723
                 bool try_remount_ro;
698723
                 MountPoint *m;
698723
 
698723
-                r = mnt_table_next_fs(t, i, &fs);
698723
+                r = mnt_table_next_fs(table, iter, &fs);
698723
                 if (r == 1)
698723
                         break;
698723
                 if (r < 0)