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