chiark / gitweb /
util: replace close_pipe() with new safe_close_pair()
[elogind.git] / src / core / automount.c
index 64b6cff72ed39bc69cbb2e8af5e76a78108b8223..65e6d6f179219aba14996e61b9f59c226e9e021b 100644 (file)
 
 #include "unit.h"
 #include "automount.h"
+#include "mount.h"
 #include "load-fragment.h"
 #include "load-dropin.h"
 #include "unit-name.h"
-#include "dbus-automount.h"
-#include "bus-errors.h"
 #include "special.h"
 #include "label.h"
 #include "mkdir.h"
 #include "path-util.h"
+#include "dbus-automount.h"
+#include "bus-util.h"
+#include "bus-error.h"
 
 static const UnitActiveState state_translation_table[_AUTOMOUNT_STATE_MAX] = {
         [AUTOMOUNT_DEAD] = UNIT_INACTIVE,
@@ -49,6 +51,7 @@ static const UnitActiveState state_translation_table[_AUTOMOUNT_STATE_MAX] = {
 };
 
 static int open_dev_autofs(Manager *m);
+static int automount_dispatch_io(sd_event_source *s, int fd, uint32_t events, void *userdata);
 
 static void automount_init(Unit *u) {
         Automount *a = AUTOMOUNT(u);
@@ -56,15 +59,12 @@ static void automount_init(Unit *u) {
         assert(u);
         assert(u->load_state == UNIT_STUB);
 
-        a->pipe_watch.fd = a->pipe_fd = -1;
-        a->pipe_watch.type = WATCH_INVALID;
-
+        a->pipe_fd = -1;
         a->directory_mode = 0755;
-
         UNIT(a)->ignore_on_isolate = true;
 }
 
-static void repeat_unmout(const char *path) {
+static void repeat_unmount(const char *path) {
         assert(path);
 
         for (;;) {
@@ -89,16 +89,15 @@ static void unmount_autofs(Automount *a) {
 
         automount_send_ready(a, -EHOSTDOWN);
 
-        unit_unwatch_fd(UNIT(a), &a->pipe_watch);
-        close_nointr_nofail(a->pipe_fd);
-        a->pipe_fd = -1;
+        a->pipe_event_source = sd_event_source_unref(a->pipe_event_source);
+        a->pipe_fd = safe_close(a->pipe_fd);
 
         /* If we reload/reexecute things we keep the mount point
          * around */
         if (a->where &&
             (UNIT(a)->manager->exit_code != MANAGER_RELOAD &&
              UNIT(a)->manager->exit_code != MANAGER_REEXECUTE))
-                repeat_unmout(a->where);
+                repeat_unmount(a->where);
 }
 
 static void automount_done(Unit *u) {
@@ -107,7 +106,6 @@ static void automount_done(Unit *u) {
         assert(a);
 
         unmount_autofs(a);
-        unit_ref_unset(&a->mount);
 
         free(a->where);
         a->where = NULL;
@@ -116,39 +114,17 @@ static void automount_done(Unit *u) {
         a->tokens = NULL;
 }
 
-int automount_add_one_mount_link(Automount *a, Mount *m) {
-        int r;
-
-        assert(a);
-        assert(m);
-
-        if (UNIT(a)->load_state != UNIT_LOADED ||
-            UNIT(m)->load_state != UNIT_LOADED)
-                return 0;
-
-        if (!path_startswith(a->where, m->where))
-                return 0;
-
-        if (path_equal(a->where, m->where))
-                return 0;
-
-        if ((r = unit_add_two_dependencies(UNIT(a), UNIT_AFTER, UNIT_REQUIRES, UNIT(m), true)) < 0)
-                return r;
-
-        return 0;
-}
-
 static int automount_add_mount_links(Automount *a) {
-        Unit *other;
+        _cleanup_free_ char *parent = NULL;
         int r;
 
         assert(a);
 
-        LIST_FOREACH(units_by_type, other, UNIT(a)->manager->units_by_type[UNIT_MOUNT])
-                if ((r = automount_add_one_mount_link(a, MOUNT(other))) < 0)
-                        return r;
+        r = path_get_parent(a->where, &parent);
+        if (r < 0)
+                return r;
 
-        return 0;
+        return unit_require_mounts_for(UNIT(a), parent);
 }
 
 static int automount_add_default_dependencies(Automount *a) {
@@ -156,7 +132,7 @@ static int automount_add_default_dependencies(Automount *a) {
 
         assert(a);
 
-        if (UNIT(a)->manager->running_as != MANAGER_SYSTEM)
+        if (UNIT(a)->manager->running_as != SYSTEMD_SYSTEM)
                 return 0;
 
         r = unit_add_two_dependencies_by_name(UNIT(a), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
@@ -175,18 +151,19 @@ static int automount_verify(Automount *a) {
                 return 0;
 
         if (path_equal(a->where, "/")) {
-                log_error("Cannot have an automount unit for the root directory. Refusing.");
+                log_error_unit(UNIT(a)->id, "Cannot have an automount unit for the root directory. Refusing.");
                 return -EINVAL;
         }
 
-        if (!(e = unit_name_from_path(a->where, ".automount")))
+        e = unit_name_from_path(a->where, ".automount");
+        if (!e)
                 return -ENOMEM;
 
         b = unit_has_name(UNIT(a), e);
         free(e);
 
         if (!b) {
-                log_error("%s's Where setting doesn't match unit name. Refusing.", UNIT(a)->id);
+                log_error_unit(UNIT(a)->id, "%s's Where setting doesn't match unit name. Refusing.", UNIT(a)->id);
                 return -EINVAL;
         }
 
@@ -194,41 +171,45 @@ static int automount_verify(Automount *a) {
 }
 
 static int automount_load(Unit *u) {
-        int r;
         Automount *a = AUTOMOUNT(u);
+        int r;
 
         assert(u);
         assert(u->load_state == UNIT_STUB);
 
         /* Load a .automount file */
-        if ((r = unit_load_fragment_and_dropin_optional(u)) < 0)
+        r = unit_load_fragment_and_dropin_optional(u);
+        if (r < 0)
                 return r;
 
         if (u->load_state == UNIT_LOADED) {
                 Unit *x;
 
-                if (!a->where)
-                        if (!(a->where = unit_name_to_path(u->id)))
+                if (!a->where) {
+                        a->where = unit_name_to_path(u->id);
+                        if (!a->where)
                                 return -ENOMEM;
+                }
 
                 path_kill_slashes(a->where);
 
-                if ((r = automount_add_mount_links(a)) < 0)
-                        return r;
-
                 r = unit_load_related_unit(u, ".mount", &x);
                 if (r < 0)
                         return r;
 
-                unit_ref_set(&a->mount, x);
+                r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, x, true);
+                if (r < 0)
+                        return r;
 
-                r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(a->mount), true);
+                r = automount_add_mount_links(a);
                 if (r < 0)
                         return r;
 
-                if (UNIT(a)->default_dependencies)
-                        if ((r = automount_add_default_dependencies(a)) < 0)
+                if (UNIT(a)->default_dependencies) {
+                        r = automount_add_default_dependencies(a);
+                        if (r < 0)
                                 return r;
+                }
         }
 
         return automount_verify(a);
@@ -246,10 +227,11 @@ static void automount_set_state(Automount *a, AutomountState state) {
                 unmount_autofs(a);
 
         if (state != old_state)
-                log_debug("%s changed %s -> %s",
-                          UNIT(a)->id,
-                          automount_state_to_string(old_state),
-                          automount_state_to_string(state));
+                log_debug_unit(UNIT(a)->id,
+                               "%s changed %s -> %s",
+                               UNIT(a)->id,
+                               automount_state_to_string(old_state),
+                               automount_state_to_string(state));
 
         unit_notify(UNIT(a), state_translation_table[old_state], state_translation_table[state], true);
 }
@@ -263,7 +245,8 @@ static int automount_coldplug(Unit *u) {
 
         if (a->deserialized_state != a->state) {
 
-                if ((r = open_dev_autofs(u->manager)) < 0)
+                r = open_dev_autofs(u->manager);
+                if (r < 0)
                         return r;
 
                 if (a->deserialized_state == AUTOMOUNT_WAITING ||
@@ -271,7 +254,8 @@ static int automount_coldplug(Unit *u) {
 
                         assert(a->pipe_fd >= 0);
 
-                        if ((r = unit_watch_fd(UNIT(a), a->pipe_fd, EPOLLIN, &a->pipe_watch)) < 0)
+                        r = sd_event_add_io(u->manager->event, &a->pipe_event_source, a->pipe_fd, EPOLLIN, automount_dispatch_io, u);
+                        if (r < 0)
                                 return r;
                 }
 
@@ -314,17 +298,17 @@ static int open_dev_autofs(Manager *m) {
         if (m->dev_autofs_fd >= 0)
                 return m->dev_autofs_fd;
 
-        label_fix("/dev/autofs", false);
+        label_fix("/dev/autofs", false, false);
 
-        if ((m->dev_autofs_fd = open("/dev/autofs", O_CLOEXEC|O_RDONLY)) < 0) {
-                log_error("Failed to open /dev/autofs: %s", strerror(errno));
+        m->dev_autofs_fd = open("/dev/autofs", O_CLOEXEC|O_RDONLY);
+        if (m->dev_autofs_fd < 0) {
+                log_error("Failed to open /dev/autofs: %m");
                 return -errno;
         }
 
         init_autofs_dev_ioctl(&param);
         if (ioctl(m->dev_autofs_fd, AUTOFS_DEV_IOCTL_VERSION, &param) < 0) {
-                close_nointr_nofail(m->dev_autofs_fd);
-                m->dev_autofs_fd = -1;
+                m->dev_autofs_fd = safe_close(m->dev_autofs_fd);
                 return -errno;
         }
 
@@ -336,15 +320,12 @@ static int open_dev_autofs(Manager *m) {
 static int open_ioctl_fd(int dev_autofs_fd, const char *where, dev_t devid) {
         struct autofs_dev_ioctl *param;
         size_t l;
-        int r;
 
         assert(dev_autofs_fd >= 0);
         assert(where);
 
         l = sizeof(struct autofs_dev_ioctl) + strlen(where) + 1;
-
-        if (!(param = malloc(l)))
-                return -ENOMEM;
+        param = alloca(l);
 
         init_autofs_dev_ioctl(param);
         param->size = l;
@@ -352,22 +333,14 @@ static int open_ioctl_fd(int dev_autofs_fd, const char *where, dev_t devid) {
         param->openmount.devid = devid;
         strcpy(param->path, where);
 
-        if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_OPENMOUNT, param) < 0) {
-                r = -errno;
-                goto finish;
-        }
+        if (ioctl(dev_autofs_fd, AUTOFS_DEV_IOCTL_OPENMOUNT, param) < 0)
+                return -errno;
 
-        if (param->ioctlfd < 0) {
-                r = -EIO;
-                goto finish;
-        }
+        if (param->ioctlfd < 0)
+                return -EIO;
 
         fd_cloexec(param->ioctlfd, true);
-        r = param->ioctlfd;
-
-finish:
-        free(param);
-        return r;
+        return param->ioctlfd;
 }
 
 static int autofs_protocol(int dev_autofs_fd, int ioctl_fd) {
@@ -435,8 +408,9 @@ static int autofs_send_ready(int dev_autofs_fd, int ioctl_fd, uint32_t token, in
 }
 
 int automount_send_ready(Automount *a, int status) {
-        int ioctl_fd, r;
+        _cleanup_close_ int ioctl_fd = -1;
         unsigned token;
+        int r;
 
         assert(a);
         assert(status <= 0);
@@ -444,15 +418,14 @@ int automount_send_ready(Automount *a, int status) {
         if (set_isempty(a->tokens))
                 return 0;
 
-        if ((ioctl_fd = open_ioctl_fd(UNIT(a)->manager->dev_autofs_fd, a->where, a->dev_id)) < 0) {
-                r = ioctl_fd;
-                goto fail;
-        }
+        ioctl_fd = open_ioctl_fd(UNIT(a)->manager->dev_autofs_fd, a->where, a->dev_id);
+        if (ioctl_fd < 0)
+                return ioctl_fd;
 
         if (status)
-                log_debug("Sending failure: %s", strerror(-status));
+                log_debug_unit(UNIT(a)->id, "Sending failure: %s", strerror(-status));
         else
-                log_debug("Sending success.");
+                log_debug_unit(UNIT(a)->id, "Sending success.");
 
         r = 0;
 
@@ -465,25 +438,23 @@ int automount_send_ready(Automount *a, int status) {
                  * if you pass a positive status code here, the kernel will
                  * freeze! Yay! */
 
-                if ((k = autofs_send_ready(UNIT(a)->manager->dev_autofs_fd,
-                                           ioctl_fd,
-                                           token,
-                                           status)) < 0)
+                k = autofs_send_ready(UNIT(a)->manager->dev_autofs_fd,
+                                      ioctl_fd,
+                                      token,
+                                      status);
+                if (k < 0)
                         r = k;
         }
 
-fail:
-        if (ioctl_fd >= 0)
-                close_nointr_nofail(ioctl_fd);
-
         return r;
 }
 
 static void automount_enter_waiting(Automount *a) {
+        _cleanup_close_ int ioctl_fd = -1;
         int p[2] = { -1, -1 };
         char name[32], options[128];
         bool mounted = false;
-        int r, ioctl_fd = -1, dev_autofs_fd;
+        int r, dev_autofs_fd;
         struct stat st;
 
         assert(a);
@@ -493,7 +464,8 @@ static void automount_enter_waiting(Automount *a) {
         if (a->tokens)
                 set_clear(a->tokens);
 
-        if ((dev_autofs_fd = open_dev_autofs(UNIT(a)->manager)) < 0) {
+        dev_autofs_fd = open_dev_autofs(UNIT(a)->manager);
+        if (dev_autofs_fd < 0) {
                 r = dev_autofs_fd;
                 goto fail;
         }
@@ -501,6 +473,8 @@ static void automount_enter_waiting(Automount *a) {
         /* We knowingly ignore the results of this call */
         mkdir_p_label(a->where, 0555);
 
+        warn_if_dir_nonempty(a->meta.id, a->where);
+
         if (pipe2(p, O_NONBLOCK|O_CLOEXEC) < 0) {
                 r = -errno;
                 goto fail;
@@ -519,23 +493,25 @@ static void automount_enter_waiting(Automount *a) {
 
         mounted = true;
 
-        close_nointr_nofail(p[1]);
-        p[1] = -1;
+        p[1] = safe_close(p[1]);
 
         if (stat(a->where, &st) < 0) {
                 r = -errno;
                 goto fail;
         }
 
-        if ((ioctl_fd = open_ioctl_fd(dev_autofs_fd, a->where, st.st_dev)) < 0) {
+        ioctl_fd = open_ioctl_fd(dev_autofs_fd, a->where, st.st_dev);
+        if (ioctl_fd < 0) {
                 r = ioctl_fd;
                 goto fail;
         }
 
-        if ((r = autofs_protocol(dev_autofs_fd, ioctl_fd)) < 0)
+        r = autofs_protocol(dev_autofs_fd, ioctl_fd);
+        if (r < 0)
                 goto fail;
 
-        if ((r = autofs_set_timeout(dev_autofs_fd, ioctl_fd, 300)) < 0)
+        r = autofs_set_timeout(dev_autofs_fd, ioctl_fd, 300);
+        if (r < 0)
                 goto fail;
 
         /* Autofs fun fact:
@@ -544,10 +520,8 @@ static void automount_enter_waiting(Automount *a) {
          * the direct mount will not receive events from the
          * kernel. */
 
-        close_nointr_nofail(ioctl_fd);
-        ioctl_fd = -1;
-
-        if ((r = unit_watch_fd(UNIT(a), p[0], EPOLLIN, &a->pipe_watch)) < 0)
+        r = sd_event_add_io(UNIT(a)->manager->event, &a->pipe_event_source, p[0], EPOLLIN, automount_dispatch_io, a);
+        if (r < 0)
                 goto fail;
 
         a->pipe_fd = p[0];
@@ -558,32 +532,28 @@ static void automount_enter_waiting(Automount *a) {
         return;
 
 fail:
-        assert_se(close_pipe(p) == 0);
-
-        if (ioctl_fd >= 0)
-                close_nointr_nofail(ioctl_fd);
+        safe_close_pair(p);
 
         if (mounted)
-                repeat_unmout(a->where);
+                repeat_unmount(a->where);
 
-        log_error("Failed to initialize automounter: %s", strerror(-r));
+        log_error_unit(UNIT(a)->id,
+                       "Failed to initialize automounter: %s", strerror(-r));
         automount_enter_dead(a, AUTOMOUNT_FAILURE_RESOURCES);
 }
 
 static void automount_enter_runnning(Automount *a) {
-        int r;
+        _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
         struct stat st;
-        DBusError error;
+        int r;
 
         assert(a);
-        assert(UNIT_DEREF(a->mount));
-
-        dbus_error_init(&error);
 
         /* We don't take mount requests anymore if we are supposed to
          * shut down anyway */
-        if (unit_pending_inactive(UNIT(a))) {
-                log_debug("Suppressing automount request on %s since unit stop is scheduled.", UNIT(a)->id);
+        if (unit_stop_pending(UNIT(a))) {
+                log_debug_unit(UNIT(a)->id,
+                               "Suppressing automount request on %s since unit stop is scheduled.", UNIT(a)->id);
                 automount_send_ready(a, -EHOSTDOWN);
                 return;
         }
@@ -592,15 +562,23 @@ static void automount_enter_runnning(Automount *a) {
 
         /* Before we do anything, let's see if somebody is playing games with us? */
         if (lstat(a->where, &st) < 0) {
-                log_warning("%s failed to stat automount point: %m", UNIT(a)->id);
+                log_warning_unit(UNIT(a)->id,
+                                 "%s failed to stat automount point: %m", UNIT(a)->id);
                 goto fail;
         }
 
         if (!S_ISDIR(st.st_mode) || st.st_dev != a->dev_id)
-                log_info("%s's automount point already active?", UNIT(a)->id);
-        else if ((r = manager_add_job(UNIT(a)->manager, JOB_START, UNIT_DEREF(a->mount), JOB_REPLACE, true, &error, NULL)) < 0) {
-                log_warning("%s failed to queue mount startup job: %s", UNIT(a)->id, bus_error(&error, r));
-                goto fail;
+                log_info_unit(UNIT(a)->id,
+                              "%s's automount point already active?", UNIT(a)->id);
+        else {
+                r = manager_add_job(UNIT(a)->manager, JOB_START, UNIT_TRIGGER(UNIT(a)),
+                                    JOB_REPLACE, true, &error, NULL);
+                if (r < 0) {
+                        log_warning_unit(UNIT(a)->id,
+                                         "%s failed to queue mount startup job: %s",
+                                         UNIT(a)->id, bus_error_message(&error, r));
+                        goto fail;
+                }
         }
 
         automount_set_state(a, AUTOMOUNT_RUNNING);
@@ -608,22 +586,22 @@ static void automount_enter_runnning(Automount *a) {
 
 fail:
         automount_enter_dead(a, AUTOMOUNT_FAILURE_RESOURCES);
-        dbus_error_free(&error);
 }
 
 static int automount_start(Unit *u) {
         Automount *a = AUTOMOUNT(u);
 
         assert(a);
-
         assert(a->state == AUTOMOUNT_DEAD || a->state == AUTOMOUNT_FAILED);
 
         if (path_is_mount_point(a->where, false)) {
-                log_error("Path %s is already a mount point, refusing start for %s", a->where, u->id);
+                log_error_unit(u->id,
+                               "Path %s is already a mount point, refusing start for %s",
+                               a->where, u->id);
                 return -EEXIST;
         }
 
-        if (UNIT_DEREF(a->mount)->load_state != UNIT_LOADED)
+        if (UNIT_TRIGGER(u)->load_state != UNIT_LOADED)
                 return -ENOENT;
 
         a->result = AUTOMOUNT_SUCCESS;
@@ -635,7 +613,6 @@ static int automount_stop(Unit *u) {
         Automount *a = AUTOMOUNT(u);
 
         assert(a);
-
         assert(a->state == AUTOMOUNT_WAITING || a->state == AUTOMOUNT_RUNNING);
 
         automount_enter_dead(a, AUTOMOUNT_SUCCESS);
@@ -661,7 +638,8 @@ static int automount_serialize(Unit *u, FILE *f, FDSet *fds) {
         if (a->pipe_fd >= 0) {
                 int copy;
 
-                if ((copy = fdset_put_dup(fds, a->pipe_fd)) < 0)
+                copy = fdset_put_dup(fds, a->pipe_fd);
+                if (copy < 0)
                         return copy;
 
                 unit_serialize_item_format(u, f, "pipe-fd", "%i", copy);
@@ -680,8 +658,9 @@ static int automount_deserialize_item(Unit *u, const char *key, const char *valu
         if (streq(key, "state")) {
                 AutomountState state;
 
-                if ((state = automount_state_from_string(value)) < 0)
-                        log_debug("Failed to parse state value %s", value);
+                state = automount_state_from_string(value);
+                if (state < 0)
+                        log_debug_unit(u->id, "Failed to parse state value %s", value);
                 else
                         a->deserialized_state = state;
         } else if (streq(key, "result")) {
@@ -689,7 +668,7 @@ static int automount_deserialize_item(Unit *u, const char *key, const char *valu
 
                 f = automount_result_from_string(value);
                 if (f < 0)
-                        log_debug("Failed to parse result value %s", value);
+                        log_debug_unit(u->id, "Failed to parse result value %s", value);
                 else if (f != AUTOMOUNT_SUCCESS)
                         a->result = f;
 
@@ -697,35 +676,34 @@ static int automount_deserialize_item(Unit *u, const char *key, const char *valu
                 unsigned d;
 
                 if (safe_atou(value, &d) < 0)
-                        log_debug("Failed to parse dev-id value %s", value);
+                        log_debug_unit(u->id, "Failed to parse dev-id value %s", value);
                 else
                         a->dev_id = (unsigned) d;
         } else if (streq(key, "token")) {
                 unsigned token;
 
                 if (safe_atou(value, &token) < 0)
-                        log_debug("Failed to parse token value %s", value);
+                        log_debug_unit(u->id, "Failed to parse token value %s", value);
                 else {
                         if (!a->tokens)
                                 if (!(a->tokens = set_new(trivial_hash_func, trivial_compare_func)))
                                         return -ENOMEM;
 
-                        if ((r = set_put(a->tokens, UINT_TO_PTR(token))) < 0)
+                        r = set_put(a->tokens, UINT_TO_PTR(token));
+                        if (r < 0)
                                 return r;
                 }
         } else if (streq(key, "pipe-fd")) {
                 int fd;
 
                 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
-                        log_debug("Failed to parse pipe-fd value %s", value);
+                        log_debug_unit(u->id, "Failed to parse pipe-fd value %s", value);
                 else {
-                        if (a->pipe_fd >= 0)
-                                close_nointr_nofail(a->pipe_fd);
-
+                        safe_close(a->pipe_fd);
                         a->pipe_fd = fdset_remove(fds, fd);
                 }
         } else
-                log_debug("Unknown serialization key '%s'", key);
+                log_debug_unit(u->id, "Unknown serialization key '%s'", key);
 
         return 0;
 }
@@ -743,19 +721,17 @@ static const char *automount_sub_state_to_string(Unit *u) {
 }
 
 static bool automount_check_gc(Unit *u) {
-        Automount *a = AUTOMOUNT(u);
-
-        assert(a);
+        assert(u);
 
-        if (!UNIT_DEREF(a->mount))
+        if (!UNIT_TRIGGER(u))
                 return false;
 
-        return UNIT_VTABLE(UNIT_DEREF(a->mount))->check_gc(UNIT_DEREF(a->mount));
+        return UNIT_VTABLE(UNIT_TRIGGER(u))->check_gc(UNIT_TRIGGER(u));
 }
 
-static void automount_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
-        Automount *a = AUTOMOUNT(u);
+static int automount_dispatch_io(sd_event_source *s, int fd, uint32_t events, void *userdata) {
         union autofs_v5_packet_union packet;
+        Automount *a = AUTOMOUNT(userdata);
         ssize_t l;
         int r;
 
@@ -763,12 +739,13 @@ static void automount_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
         assert(fd == a->pipe_fd);
 
         if (events != EPOLLIN) {
-                log_error("Got invalid poll event on pipe.");
+                log_error_unit(UNIT(a)->id, "Got invalid poll event on pipe.");
                 goto fail;
         }
 
-        if ((l = loop_read(a->pipe_fd, &packet, sizeof(packet), true)) != sizeof(packet)) {
-                log_error("Invalid read from pipe: %s", l < 0 ? strerror(-l) : "short read");
+        l = loop_read(a->pipe_fd, &packet, sizeof(packet), true);
+        if (l != sizeof(packet)) {
+                log_error_unit(UNIT(a)->id, "Invalid read from pipe: %s", l < 0 ? strerror(-l) : "short read");
                 goto fail;
         }
 
@@ -777,23 +754,24 @@ static void automount_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
         case autofs_ptype_missing_direct:
 
                 if (packet.v5_packet.pid > 0) {
-                        char *p = NULL;
+                        _cleanup_free_ char *p = NULL;
 
                         get_process_comm(packet.v5_packet.pid, &p);
-                        log_debug("Got direct mount request for %s, triggered by %lu (%s)", packet.v5_packet.name, (unsigned long) packet.v5_packet.pid, strna(p));
-                        free(p);
-
+                        log_info_unit(UNIT(a)->id,
+                                       "Got automount request for %s, triggered by "PID_FMT" (%s)",
+                                       a->where, packet.v5_packet.pid, strna(p));
                 } else
-                        log_debug("Got direct mount request for %s", packet.v5_packet.name);
+                        log_debug_unit(UNIT(a)->id, "Got direct mount request on %s", a->where);
 
-                if (!a->tokens)
-                        if (!(a->tokens = set_new(trivial_hash_func, trivial_compare_func))) {
-                                log_error("Failed to allocate token set.");
-                                goto fail;
-                        }
+                r = set_ensure_allocated(&a->tokens, trivial_hash_func, trivial_compare_func);
+                if (r < 0) {
+                        log_error_unit(UNIT(a)->id, "Failed to allocate token set.");
+                        goto fail;
+                }
 
-                if ((r = set_put(a->tokens, UINT_TO_PTR(packet.v5_packet.wait_queue_token))) < 0) {
-                        log_error("Failed to remember token: %s", strerror(-r));
+                r = set_put(a->tokens, UINT_TO_PTR(packet.v5_packet.wait_queue_token));
+                if (r < 0) {
+                        log_error_unit(UNIT(a)->id, "Failed to remember token: %s", strerror(-r));
                         goto fail;
                 }
 
@@ -801,21 +779,21 @@ static void automount_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
                 break;
 
         default:
-                log_error("Received unknown automount request %i", packet.hdr.type);
+                log_error_unit(UNIT(a)->id, "Received unknown automount request %i", packet.hdr.type);
                 break;
         }
 
-        return;
+        return 0;
 
 fail:
         automount_enter_dead(a, AUTOMOUNT_FAILURE_RESOURCES);
+        return 0;
 }
 
 static void automount_shutdown(Manager *m) {
         assert(m);
 
-        if (m->dev_autofs_fd >= 0)
-                close_nointr_nofail(m->dev_autofs_fd);
+        m->dev_autofs_fd = safe_close(m->dev_autofs_fd);
 }
 
 static void automount_reset_failed(Unit *u) {
@@ -846,8 +824,8 @@ static const char* const automount_result_table[_AUTOMOUNT_RESULT_MAX] = {
 DEFINE_STRING_TABLE_LOOKUP(automount_result, AutomountResult);
 
 const UnitVTable automount_vtable = {
-        .suffix = ".automount",
         .object_size = sizeof(Automount),
+
         .sections =
                 "Unit\0"
                 "Automount\0"
@@ -875,13 +853,10 @@ const UnitVTable automount_vtable = {
 
         .check_gc = automount_check_gc,
 
-        .fd_event = automount_fd_event,
-
         .reset_failed = automount_reset_failed,
 
         .bus_interface = "org.freedesktop.systemd1.Automount",
-        .bus_message_handler = bus_automount_message_handler,
-        .bus_invalidating_properties = bus_automount_invalidating_properties,
+        .bus_vtable = bus_automount_vtable,
 
         .shutdown = automount_shutdown,