chiark / gitweb /
mount: use libmount to enumerate /proc/self/mountinfo
[elogind.git] / src / core / mount.c
index 36375f65bcde4d88790a82d4e9d144e4dd60dfca..d257925fa7a74d0a9105d5e42556d7ff1d7fc90a 100644 (file)
@@ -24,6 +24,7 @@
 #include <mntent.h>
 #include <sys/epoll.h>
 #include <signal.h>
+#include <libmount.h>
 
 #include "manager.h"
 #include "unit.h"
@@ -823,12 +824,12 @@ void warn_if_dir_nonempty(const char *unit, const char* where) {
         if (r > 0)
                 return;
         else if (r == 0)
-                log_unit_struct(LOG_NOTICE,
-                                unit,
-                                "MESSAGE=%s: Directory %s to mount over is not empty, mounting anyway.",
-                                unit, where,
+                log_unit_struct(unit,
+                                LOG_NOTICE,
+                                LOG_MESSAGE_ID(SD_MESSAGE_OVERMOUNTING),
+                                LOG_MESSAGE("%s: Directory %s to mount over is not empty, mounting anyway.",
+                                            unit, where),
                                 "WHERE=%s", where,
-                                MESSAGE_ID(SD_MESSAGE_OVERMOUNTING),
                                 NULL);
         else
                 log_unit_warning(unit,
@@ -840,12 +841,12 @@ static int fail_if_symlink(const char *unit, const char* where) {
         assert(where);
 
         if (is_symlink(where) > 0) {
-                log_unit_struct(LOG_WARNING,
-                                unit,
-                                "MESSAGE=%s: Mount on symlink %s not allowed.",
-                                unit, where,
+                log_unit_struct(unit,
+                                LOG_ERR,
+                                LOG_MESSAGE_ID(SD_MESSAGE_OVERMOUNTING),
+                                LOG_MESSAGE("%s: Mount on symlink %s not allowed.",
+                                            unit, where),
                                 "WHERE=%s", where,
-                                MESSAGE_ID(SD_MESSAGE_OVERMOUNTING),
                                 NULL);
 
                 return -ELOOP;
@@ -1500,55 +1501,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;
-                int k;
+        mnt_table_parse_mtab(tb, NULL);
+        if (r)
+                return r;
 
-                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;
-                }
+        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;
 
-                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;
         }
@@ -1585,6 +1578,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)
@@ -1627,7 +1622,7 @@ static int mount_dispatch_io(sd_event_source *source, int fd, uint32_t revents,
 
         r = mount_load_proc_self_mountinfo(m, true);
         if (r < 0) {
-                log_error("Failed to reread /proc/self/mountinfo: %s", strerror(-r));
+                log_error_errno(r, "Failed to reread /proc/self/mountinfo: %m");
 
                 /* Reset flags, just in case, for later calls */
                 LIST_FOREACH(units_by_type, u, m->units_by_type[UNIT_MOUNT]) {