84b277
From 53b2695b96c6e8717a375cc2a17fcc4384d04790 Mon Sep 17 00:00:00 2001
84b277
From: Chris Leech <cleech@redhat.com>
84b277
Date: Sun, 23 Nov 2014 20:33:37 -0800
84b277
Subject: [PATCH] mount: use libmount to enumerate /proc/self/mountinfo
84b277
84b277
This lets libmount add in user options from /run/mount/utab, like
84b277
_netdev which is needed to get proper ordering against remote-fs.target
84b277
84b277
Conflicts:
84b277
	Makefile.am
84b277
	README
84b277
	configure.ac
84b277
	src/core/mount.c
84b277
84b277
(cherry-picked from 8d3ae2bd4c9bf9fc2e57f7b3776325a1c750ca30)
84b277
84b277
Related: #1161417
84b277
---
84b277
 .travis.yml      |  2 +-
84b277
 Makefile.am      |  4 +++-
84b277
 README           |  1 +
84b277
 configure.ac     | 10 +++++++++
84b277
 src/core/mount.c | 65 ++++++++++++++++++++++++++------------------------------
84b277
 5 files changed, 45 insertions(+), 37 deletions(-)
84b277
84b277
diff --git a/.travis.yml b/.travis.yml
84b277
index 7e5251c..4ea2bc2 100644
84b277
--- a/.travis.yml
84b277
+++ b/.travis.yml
84b277
@@ -3,7 +3,7 @@ compiler:
84b277
   - gcc
84b277
 before_install:
84b277
  - sudo apt-get update -qq
84b277
- - sudo apt-get install autotools-dev automake autoconf libtool libdbus-1-dev libcap-dev libblkid-dev libpam-dev libcryptsetup-dev libaudit-dev libacl1-dev libattr1-dev libselinux-dev liblzma-dev libgcrypt-dev libqrencode-dev libmicrohttpd-dev gtk-doc-tools gperf python2.7-dev
84b277
+ - sudo apt-get install autotools-dev automake autoconf libtool libdbus-1-dev libcap-dev libblkid-dev libmount-dev libpam-dev libcryptsetup-dev libaudit-dev libacl1-dev libattr1-dev libselinux-dev liblzma-dev libgcrypt-dev libqrencode-dev libmicrohttpd-dev gtk-doc-tools gperf python2.7-dev
84b277
 script: ./autogen.sh && ./configure --enable-gtk-doc --enable-gtk-doc-pdf && make V=1 && sudo ./systemd-machine-id-setup && make check && make distcheck
84b277
 after_failure: cat test-suite.log
84b277
 notifications:
84b277
diff --git a/Makefile.am b/Makefile.am
84b277
index faf2a32..0891bca 100644
84b277
--- a/Makefile.am
84b277
+++ b/Makefile.am
84b277
@@ -997,6 +997,7 @@ libsystemd_core_la_CFLAGS = \
84b277
 	$(PAM_CFLAGS) \
84b277
 	$(AUDIT_CFLAGS) \
84b277
 	$(KMOD_CFLAGS) \
84b277
+	$(MOUNT_CFLAGS) \
84b277
 	-pthread
84b277
 
84b277
 libsystemd_core_la_LIBADD = \
84b277
@@ -1013,7 +1014,8 @@ libsystemd_core_la_LIBADD = \
84b277
 	$(PAM_LIBS) \
84b277
 	$(AUDIT_LIBS) \
84b277
 	$(CAP_LIBS) \
84b277
-	$(KMOD_LIBS)
84b277
+	$(KMOD_LIBS) \
84b277
+	$(MOUNT_LIBS)
84b277
 
84b277
 src/core/load-fragment-gperf-nulstr.c: src/core/load-fragment-gperf.gperf
84b277
 	$(AM_V_at)$(MKDIR_P) $(dir $@)
84b277
diff --git a/README b/README
84b277
index b39cd37..42487dd 100644
84b277
--- a/README
84b277
+++ b/README
84b277
@@ -94,6 +94,7 @@ REQUIREMENTS:
84b277
 
84b277
         dbus >= 1.4.0
84b277
         libcap
84b277
+        libmount >= 2.20 (from util-linux)
84b277
         libblkid >= 2.20 (from util-linux) (optional)
84b277
         libkmod >= 14 (optional)
84b277
         PAM >= 1.1.2 (optional)
84b277
diff --git a/configure.ac b/configure.ac
84b277
index 4f26092..b7dfc5f 100644
84b277
--- a/configure.ac
84b277
+++ b/configure.ac
84b277
@@ -292,6 +292,15 @@ fi
84b277
 AM_CONDITIONAL(HAVE_BLKID, [test "$have_blkid" = "yes"])
84b277
 
84b277
 # ------------------------------------------------------------------------------
84b277
+have_libmount=no
84b277
+PKG_CHECK_MODULES(MOUNT, [ mount >= 2.20 ],
84b277
+        [AC_DEFINE(HAVE_LIBMOUNT, 1, [Define if libmount is available]) have_libmount=yes], have_libmount=no)
84b277
+if test "x$have_libmount" = xno; then
84b277
+        AC_MSG_ERROR([*** libmount support required but libraries not found])
84b277
+fi
84b277
+AM_CONDITIONAL(HAVE_LIBMOUNT, [test "$have_libmount" = "yes"])
84b277
+
84b277
+# ------------------------------------------------------------------------------
84b277
 have_ima=yes
84b277
 AC_ARG_ENABLE([ima], AS_HELP_STRING([--disable-ima],[Disable optional IMA support]),
84b277
                 [case "${enableval}" in
84b277
@@ -1046,6 +1055,7 @@ AC_MSG_RESULT([
84b277
         efi:                     ${have_efi}
84b277
         kmod:                    ${have_kmod}
84b277
         blkid:                   ${have_blkid}
84b277
+        libmount:                ${have_libmount}
84b277
         nss-myhostname:          ${have_myhostname}
84b277
         gudev:                   ${enable_gudev}
84b277
         gintrospection:          ${enable_introspection}
84b277
diff --git a/src/core/mount.c b/src/core/mount.c
84b277
index bbceb92..d78269c 100644
84b277
--- a/src/core/mount.c
84b277
+++ b/src/core/mount.c
84b277
@@ -24,6 +24,7 @@
84b277
 #include <mntent.h>
84b277
 #include <sys/epoll.h>
84b277
 #include <signal.h>
84b277
+#include <libmount.h>
84b277
 
84b277
 #include "manager.h"
84b277
 #include "unit.h"
84b277
@@ -1563,55 +1564,47 @@ fail:
84b277
         return r;
84b277
 }
84b277
 
84b277
+static inline void mnt_free_table_p(struct libmnt_table **tb) {
84b277
+        mnt_free_table(*tb);
84b277
+}
84b277
+
84b277
+static inline void mnt_free_iter_p(struct libmnt_iter **itr) {
84b277
+        mnt_free_iter(*itr);
84b277
+}
84b277
+
84b277
 static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
84b277
+        _cleanup_(mnt_free_table_p) struct libmnt_table *tb = NULL;
84b277
+        _cleanup_(mnt_free_iter_p) struct libmnt_iter *itr = NULL;
84b277
+        struct libmnt_fs *fs;
84b277
         int r = 0;
84b277
-        unsigned i;
84b277
 
84b277
         assert(m);
84b277
 
84b277
-        rewind(m->proc_self_mountinfo);
84b277
+        tb = mnt_new_table();
84b277
+        itr = mnt_new_iter(MNT_ITER_FORWARD);
84b277
+        if (!tb || !itr)
84b277
+                return log_oom();
84b277
 
84b277
-        for (i = 1;; i++) {
84b277
-                _cleanup_free_ char *device = NULL, *path = NULL, *options = NULL, *options2 = NULL, *fstype = NULL, *d = NULL, *p = NULL, *o = NULL;
84b277
-                int k;
84b277
+        mnt_table_parse_mtab(tb, NULL);
84b277
+        if (r)
84b277
+                return r;
84b277
 
84b277
-                k = fscanf(m->proc_self_mountinfo,
84b277
-                           "%*s "       /* (1) mount id */
84b277
-                           "%*s "       /* (2) parent id */
84b277
-                           "%*s "       /* (3) major:minor */
84b277
-                           "%*s "       /* (4) root */
84b277
-                           "%ms "       /* (5) mount point */
84b277
-                           "%ms"        /* (6) mount options */
84b277
-                           "%*[^-]"     /* (7) optional fields */
84b277
-                           "- "         /* (8) separator */
84b277
-                           "%ms "       /* (9) file system type */
84b277
-                           "%ms"        /* (10) mount source */
84b277
-                           "%ms"        /* (11) mount options 2 */
84b277
-                           "%*[^\n]",   /* some rubbish at the end */
84b277
-                           &path,
84b277
-                           &options,
84b277
-                           &fstype,
84b277
-                           &device,
84b277
-                           &options2);
84b277
-
84b277
-                if (k == EOF)
84b277
-                        break;
84b277
-
84b277
-                if (k != 5) {
84b277
-                        log_warning("Failed to parse /proc/self/mountinfo:%u.", i);
84b277
-                        continue;
84b277
-                }
84b277
+        while (mnt_table_next_fs(tb, itr, &fs) == 0) {
84b277
+                const char *device, *path, *options, *fstype;
84b277
+                _cleanup_free_ const char *d = NULL, *p = NULL;
84b277
+                int k;
84b277
 
84b277
-                o = strjoin(options, ",", options2, NULL);
84b277
-                if (!o)
84b277
-                        return log_oom();
84b277
+                device = mnt_fs_get_source(fs);
84b277
+                path = mnt_fs_get_target(fs);
84b277
+                options = mnt_fs_get_options(fs);
84b277
+                fstype = mnt_fs_get_fstype(fs);
84b277
 
84b277
                 d = cunescape(device);
84b277
                 p = cunescape(path);
84b277
                 if (!d || !p)
84b277
                         return log_oom();
84b277
 
84b277
-                k = mount_add_one(m, d, p, o, fstype, 0, set_flags);
84b277
+                k = mount_add_one(m, d, p, options, fstype, 0, set_flags);
84b277
                 if (k < 0)
84b277
                         r = k;
84b277
         }
84b277
@@ -1632,6 +1625,8 @@ static int mount_enumerate(Manager *m) {
84b277
         int r;
84b277
         assert(m);
84b277
 
84b277
+        mnt_init_debug(0);
84b277
+
84b277
         if (!m->proc_self_mountinfo) {
84b277
                 struct epoll_event ev = {
84b277
                         .events = EPOLLPRI,