+ if (live) {
+ if (set_flags)
+ MOUNT(u)->just_created = !MOUNT(u)->from_proc_self_mountinfo;
+ MOUNT(u)->from_proc_self_mountinfo = true;
+ } else {
+ if (set_flags)
+ MOUNT(u)->just_created = !MOUNT(u)->from_etc_fstab;
+ MOUNT(u)->from_etc_fstab = true;
+ }
+
+ if ((r = mount_add_node_links(MOUNT(u))) < 0)
+ goto fail;
+
+ if ((r = mount_add_path_links(MOUNT(u))) < 0)
+ goto fail;
+
+ unit_add_to_dbus_queue(u);
+
+ return 0;
+
+fail:
+ if (delete && u)
+ unit_free(u);
+
+ return 0;
+}
+
+static char *fstab_node_to_udev_node(char *p) {
+ char *dn, *t;
+ int r;
+
+ /* FIXME: to follow udev's logic 100% we need to leave valid
+ * UTF8 chars unescaped */
+
+ if (startswith(p, "LABEL=")) {
+
+ if (!(t = xescape(p+6, "/ ")))
+ return NULL;
+
+ r = asprintf(&dn, "/dev/disk/by-label/%s", t);
+ free(t);
+
+ if (r < 0)
+ return NULL;
+
+ return dn;
+ }
+
+ if (startswith(p, "UUID=")) {
+
+ if (!(t = xescape(p+5, "/ ")))
+ return NULL;
+
+ r = asprintf(&dn, "/dev/disk/by-uuid/%s", ascii_strlower(t));
+ free(t);
+
+ if (r < 0)
+ return NULL;
+
+ return dn;
+ }
+
+ return strdup(p);
+}
+
+static int mount_load_etc_fstab(Manager *m, bool set_flags) {
+ FILE *f;
+ int r;
+ struct mntent* me;
+
+ assert(m);
+
+ errno = 0;
+ if (!(f = setmntent("/etc/fstab", "r")))
+ return -errno;
+
+ while ((me = getmntent(f))) {
+ char *where, *what;
+
+ if (!(what = fstab_node_to_udev_node(me->mnt_fsname))) {
+ r = -ENOMEM;
+ goto finish;
+ }
+
+ if (!(where = strdup(me->mnt_dir))) {
+ free(what);
+ r = -ENOMEM;
+ goto finish;
+ }
+
+ if (what[0] == '/')
+ path_kill_slashes(what);
+
+ if (where[0] == '/')
+ path_kill_slashes(where);
+
+ r = mount_add_one(m, what, where, false, set_flags);
+ free(what);
+ free(where);
+
+ if (r < 0)
+ goto finish;
+ }
+
+ r = 0;
+finish:
+
+ endmntent(f);
+ return r;
+}
+
+static int mount_load_proc_self_mountinfo(Manager *m, bool set_flags) {
+ int r;
+
+ assert(m);
+
+ rewind(m->proc_self_mountinfo);
+
+ for (;;) {
+ int k;
+ char *device, *path, *d, *p;
+
+ if ((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 */
+ "%*s" /* (6) mount options */
+ "%*[^-]" /* (7) optional fields */
+ "- " /* (8) seperator */
+ "%*s " /* (9) file system type */
+ "%ms" /* (10) mount source */
+ "%*[^\n]", /* some rubbish at the end */
+ &path,
+ &device)) != 2) {
+
+ if (k == EOF)
+ break;
+
+ return -EBADMSG;
+ }
+
+ if (!(d = cunescape(device))) {
+ free(device);
+ free(path);
+ return -ENOMEM;
+ }
+ free(device);
+
+ if (!(p = cunescape(path))) {
+ free(d);
+ free(path);
+ return -ENOMEM;
+ }
+ free(path);
+
+ r = mount_add_one(m, d, p, true, set_flags);
+ free(d);
+ free(p);
+
+ if (r < 0)
+ return r;
+ }
+
+ return 0;
+}
+
+static int mount_enumerate(Manager *m) {
+ int r;
+ struct epoll_event ev;
+ assert(m);
+
+ if (!(m->proc_self_mountinfo = fopen("/proc/self/mountinfo", "r")))
+ return -errno;
+
+ m->mount_watch.type = WATCH_MOUNT;
+ m->mount_watch.fd = fileno(m->proc_self_mountinfo);
+
+ zero(ev);
+ ev.events = EPOLLERR;
+ ev.data.ptr = &m->mount_watch;
+
+ if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->mount_watch.fd, &ev) < 0)
+ return -errno;
+
+ if ((r = mount_load_etc_fstab(m, false)) < 0)
+ goto fail;
+
+ if ((r = mount_load_proc_self_mountinfo(m, false)) < 0)
+ goto fail;
+
+ return 0;
+
+fail:
+ mount_shutdown(m);
+ return r;
+}
+
+void mount_fd_event(Manager *m, int events) {
+ Meta *meta;
+ int r;
+
+ assert(m);
+ assert(events == EPOLLERR);
+
+ /* The manager calls this for every fd event happening on the
+ * /proc/self/mountinfo file, which informs us about mounting
+ * table changes */
+
+ if ((r = mount_load_proc_self_mountinfo(m, true)) < 0) {
+ log_error("Failed to reread /proc/self/mountinfo: %s", strerror(-errno));
+ return;
+ }
+
+ manager_dispatch_load_queue(m);
+
+ LIST_FOREACH(units_per_type, meta, m->units_per_type[UNIT_MOUNT]) {
+ Mount *mount = (Mount*) meta;
+
+ if (mount->just_created && mount->state == MOUNT_DEAD)
+ mount_set_state(mount, MOUNT_MOUNTED);
+ else if (!mount->still_exists && mount->state == MOUNT_MOUNTED) {
+ mount_set_state(mount, MOUNT_DEAD);
+ mount->from_proc_self_mountinfo = false;
+ }
+
+ /* Clear the flags for later calls */
+ mount->just_created = false;
+ mount->still_exists = false;
+ }