[systemd-devel] [PATCH v2 1/5] mount: use libmount to enumerate /proc/self/mountinfo

Chris Leech cleech at redhat.com
Sun Nov 23 20:33:37 PST 2014


This lets libmount add in user options from /run/mount/utab, like
_netdev which is needed to get proper ordering against remote-fs.target

Signed-off-by: Chris Leech <cleech at redhat.com>
---
 .travis.yml      |  2 +-
 Makefile.am      |  4 +++-
 README           |  1 +
 configure.ac     | 10 +++++++++
 src/core/build.h |  7 ++++++
 src/core/mount.c | 65 ++++++++++++++++++++++++++------------------------------
 6 files changed, 52 insertions(+), 37 deletions(-)

diff --git a/.travis.yml b/.travis.yml
index 7e5251c..4ea2bc2 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -3,7 +3,7 @@ compiler:
   - gcc
 before_install:
  - sudo apt-get update -qq
- - 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
+ - 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
 script: ./autogen.sh && ./configure --enable-gtk-doc --enable-gtk-doc-pdf && make V=1 && sudo ./systemd-machine-id-setup && make check && make distcheck
 after_failure: cat test-suite.log
 notifications:
diff --git a/Makefile.am b/Makefile.am
index 3f9f3fa..042fb0e 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1149,6 +1149,7 @@ libsystemd_core_la_CFLAGS = \
 	$(KMOD_CFLAGS) \
 	$(APPARMOR_CFLAGS) \
 	$(SECCOMP_CFLAGS) \
+	$(MOUNT_CFLAGS) \
 	-pthread
 
 libsystemd_core_la_LIBADD = \
@@ -1161,7 +1162,8 @@ libsystemd_core_la_LIBADD = \
 	$(AUDIT_LIBS) \
 	$(KMOD_LIBS) \
 	$(APPARMOR_LIBS) \
-	$(SECCOMP_LIBS)
+	$(SECCOMP_LIBS) \
+	$(MOUNT_LIBS)
 
 if HAVE_SECCOMP
 libsystemd_core_la_LIBADD += \
diff --git a/README b/README
index aefb349..84c6ed4 100644
--- a/README
+++ b/README
@@ -106,6 +106,7 @@ REQUIREMENTS:
 
         glibc >= 2.14
         libcap
+        libmount >= 2.20 (from util-linux)
         libseccomp >= 1.0.0 (optional)
         libblkid >= 2.20 (from util-linux) (optional)
         libkmod >= 15 (optional)
diff --git a/configure.ac b/configure.ac
index 2c8be53..ecd53a4 100644
--- a/configure.ac
+++ b/configure.ac
@@ -427,6 +427,15 @@ fi
 AM_CONDITIONAL(HAVE_BLKID, [test "$have_blkid" = "yes"])
 
 # ------------------------------------------------------------------------------
+have_libmount=no
+PKG_CHECK_MODULES(MOUNT, [ mount >= 2.20 ],
+        [AC_DEFINE(HAVE_LIBMOUNT, 1, [Define if libmount is available]) have_libmount=yes], have_libmount=no)
+if test "x$have_libmount" = xno; then
+        AC_MSG_ERROR([*** libmount support required but libraries not found])
+fi
+AM_CONDITIONAL(HAVE_LIBMOUNT, [test "$have_libmount" = "yes"])
+
+# ------------------------------------------------------------------------------
 have_seccomp=no
 AC_ARG_ENABLE(seccomp, AS_HELP_STRING([--disable-seccomp], [Disable optional SECCOMP support]))
 if test "x$enable_seccomp" != "xno"; then
@@ -1375,6 +1384,7 @@ AC_MSG_RESULT([
         efi:                     ${have_efi}
         kmod:                    ${have_kmod}
         blkid:                   ${have_blkid}
+        libmount:                ${have_libmount}
         dbus:                    ${have_dbus}
         nss-myhostname:          ${have_myhostname}
         gudev:                   ${enable_gudev}
diff --git a/src/core/build.h b/src/core/build.h
index 24873ab..4f639ed 100644
--- a/src/core/build.h
+++ b/src/core/build.h
@@ -117,6 +117,12 @@
 #define _BLKID_FEATURE_ "-BLKID"
 #endif
 
+#ifdef HAVE_LIBMOUNT
+#define _LIBMOUNT_FEATURE_ "+LIBMOUNT"
+#else
+#define _LIBMOUNT_FEATURE_ "-LIBMOUNT"
+#endif
+
 #ifdef HAVE_ELFUTILS
 #define _ELFUTILS_FEATURE_ "+ELFUTILS"
 #else
@@ -152,6 +158,7 @@
         _LZ4_FEATURE_ " "                                               \
         _SECCOMP_FEATURE_ " "                                           \
         _BLKID_FEATURE_ " "                                             \
+        _LIBMOUNT_FEATURE_ " "                                          \
         _ELFUTILS_FEATURE_ " "                                          \
         _KMOD_FEATURE_ " "                                              \
         _IDN_FEATURE_
diff --git a/src/core/mount.c b/src/core/mount.c
index 8b787f6..74a1da8 100644
--- a/src/core/mount.c
+++ b/src/core/mount.c
@@ -24,6 +24,7 @@
 #include <mntent.h>
 #include <sys/epoll.h>
 #include <signal.h>
+#include <libmount.h>
 
 #include "manager.h"
 #include "unit.h"
@@ -1492,55 +1493,47 @@ fail:
         return r;
 }
 
+static inline void mnt_free_table_p(struct libmnt_table **tb) {
+        mnt_free_table(*tb);
+}
+
+static inline void mnt_free_iter_p(struct libmnt_iter **itr) {
+        mnt_free_iter(*itr);
+}
+
 static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
+        _cleanup_(mnt_free_table_p) struct libmnt_table *tb = NULL;
+        _cleanup_(mnt_free_iter_p) struct libmnt_iter *itr = NULL;
+        struct libmnt_fs *fs;
         int r = 0;
-        unsigned i;
 
         assert(m);
 
-        rewind(m->proc_self_mountinfo);
+        tb = mnt_new_table();
+        itr = mnt_new_iter(MNT_ITER_FORWARD);
+        if (!tb || !itr)
+                return log_oom();
 
-        for (i = 1;; i++) {
-                _cleanup_free_ char *device = NULL, *path = NULL, *options = NULL, *options2 = NULL, *fstype = NULL, *d = NULL, *p = NULL, *o = NULL;
+        mnt_table_parse_mtab(tb, NULL);
+        if (r)
+                return r;
+
+        while (mnt_table_next_fs(tb, itr, &fs) == 0) {
+                const char *device, *path, *options, *fstype;
+                _cleanup_free_ const char *d = NULL, *p = NULL;
                 int k;
 
-                k = fscanf(m->proc_self_mountinfo,
-                           "%*s "       /* (1) mount id */
-                           "%*s "       /* (2) parent id */
-                           "%*s "       /* (3) major:minor */
-                           "%*s "       /* (4) root */
-                           "%ms "       /* (5) mount point */
-                           "%ms"        /* (6) mount options */
-                           "%*[^-]"     /* (7) optional fields */
-                           "- "         /* (8) separator */
-                           "%ms "       /* (9) file system type */
-                           "%ms"        /* (10) mount source */
-                           "%ms"        /* (11) mount options 2 */
-                           "%*[^\n]",   /* some rubbish at the end */
-                           &path,
-                           &options,
-                           &fstype,
-                           &device,
-                           &options2);
-
-                if (k == EOF)
-                        break;
-
-                if (k != 5) {
-                        log_warning("Failed to parse /proc/self/mountinfo:%u.", i);
-                        continue;
-                }
-
-                o = strjoin(options, ",", options2, NULL);
-                if (!o)
-                        return log_oom();
+                device = mnt_fs_get_source(fs);
+                path = mnt_fs_get_target(fs);
+                options = mnt_fs_get_options(fs);
+                fstype = mnt_fs_get_fstype(fs);
 
                 d = cunescape(device);
                 p = cunescape(path);
                 if (!d || !p)
                         return log_oom();
 
-                k = mount_add_one(m, d, p, o, fstype, set_flags);
+                k = mount_add_one(m, d, p, options, fstype, set_flags);
                 if (k < 0)
                         r = k;
         }
@@ -1577,6 +1570,8 @@ static int mount_enumerate(Manager *m) {
         int r;
         assert(m);
 
+        mnt_init_debug(0);
+
         if (!m->proc_self_mountinfo) {
                 m->proc_self_mountinfo = fopen("/proc/self/mountinfo", "re");
                 if (!m->proc_self_mountinfo)
-- 
1.9.3



More information about the systemd-devel mailing list