chiark / gitweb /
label: udev might be making changes in /dev while we iterate through it
[elogind.git] / src / automount.c
index 465354f5556503f82f790c8b0f69c973ca1d1a04..7289311684caff948922999bb420b157a7191b0f 100644 (file)
@@ -1,4 +1,4 @@
-/*-*- Mode: C; c-basic-offset: 8 -*-*/
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
 
 /***
   This file is part of systemd.
 #include "load-dropin.h"
 #include "unit-name.h"
 #include "dbus-automount.h"
+#include "bus-errors.h"
+#include "special.h"
+#include "label.h"
 
 static const UnitActiveState state_translation_table[_AUTOMOUNT_STATE_MAX] = {
         [AUTOMOUNT_DEAD] = UNIT_INACTIVE,
         [AUTOMOUNT_WAITING] = UNIT_ACTIVE,
         [AUTOMOUNT_RUNNING] = UNIT_ACTIVE,
-        [AUTOMOUNT_MAINTAINANCE] = UNIT_INACTIVE,
+        [AUTOMOUNT_FAILED] = UNIT_FAILED
 };
 
 static int open_dev_autofs(Manager *m);
@@ -53,12 +56,16 @@ static void automount_init(Unit *u) {
 
         a->pipe_watch.fd = a->pipe_fd = -1;
         a->pipe_watch.type = WATCH_INVALID;
+
+        a->directory_mode = 0755;
 }
 
 static void repeat_unmout(const char *path) {
         assert(path);
 
         for (;;) {
+                /* If there are multiple mounts on a mount point, this
+                 * removes them all */
 
                 if (umount2(path, MNT_DETACH) >= 0)
                         continue;
@@ -85,8 +92,8 @@ static void unmount_autofs(Automount *a) {
         /* If we reload/reexecute things we keep the mount point
          * around */
         if (a->where &&
-            (UNIT(a)->meta.manager->exit_code != MANAGER_RELOAD &&
-             UNIT(a)->meta.manager->exit_code != MANAGER_REEXECUTE))
+            (a->meta.manager->exit_code != MANAGER_RELOAD &&
+             a->meta.manager->exit_code != MANAGER_REEXECUTE))
                 repeat_unmout(a->where);
 }
 
@@ -118,10 +125,10 @@ int automount_add_one_mount_link(Automount *a, Mount *m) {
         if (!path_startswith(a->where, m->where))
                 return 0;
 
-        if ((r = unit_add_dependency(UNIT(m), UNIT_BEFORE, UNIT(a), true)) < 0)
-                return r;
+        if (path_equal(a->where, m->where))
+                return 0;
 
-        if ((r = unit_add_dependency(UNIT(a), UNIT_REQUIRES, UNIT(m), true)) < 0)
+        if ((r = unit_add_two_dependencies(UNIT(a), UNIT_AFTER, UNIT_REQUIRES, UNIT(m), true)) < 0)
                 return r;
 
         return 0;
@@ -140,14 +147,36 @@ static int automount_add_mount_links(Automount *a) {
         return 0;
 }
 
+static int automount_add_default_dependencies(Automount *a) {
+        int r;
+
+        assert(a);
+
+        if (a->meta.manager->running_as == MANAGER_SYSTEM) {
+
+                if ((r = unit_add_dependency_by_name(UNIT(a), UNIT_BEFORE, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
+                        return r;
+
+                if ((r = unit_add_two_dependencies_by_name(UNIT(a), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true)) < 0)
+                        return r;
+        }
+
+        return 0;
+}
+
 static int automount_verify(Automount *a) {
         bool b;
         char *e;
         assert(a);
 
-        if (UNIT(a)->meta.load_state != UNIT_LOADED)
+        if (a->meta.load_state != UNIT_LOADED)
                 return 0;
 
+        if (path_equal(a->where, "/")) {
+                log_error("Cannot have an automount unit for the root directory. Refusing.");
+                return -EINVAL;
+        }
+
         if (!(e = unit_name_from_path(a->where, ".automount")))
                 return -ENOMEM;
 
@@ -155,7 +184,7 @@ static int automount_verify(Automount *a) {
         free(e);
 
         if (!b) {
-                log_error("%s's Where setting doesn't match unit name. Refusing.", UNIT(a)->meta.id);
+                log_error("%s's Where setting doesn't match unit name. Refusing.", a->meta.id);
                 return -EINVAL;
         }
 
@@ -189,6 +218,10 @@ static int automount_load(Unit *u) {
 
                 if ((r = unit_add_dependency(u, UNIT_BEFORE, UNIT(a->mount), true)) < 0)
                         return r;
+
+                if (a->meta.default_dependencies)
+                        if ((r = automount_add_default_dependencies(a)) < 0)
+                                return r;
         }
 
         return automount_verify(a);
@@ -207,11 +240,11 @@ static void automount_set_state(Automount *a, AutomountState state) {
 
         if (state != old_state)
                 log_debug("%s changed %s -> %s",
-                          UNIT(a)->meta.id,
+                          a->meta.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]);
+        unit_notify(UNIT(a), state_translation_table[old_state], state_translation_table[state], true);
 }
 
 static int automount_coldplug(Unit *u) {
@@ -248,9 +281,11 @@ static void automount_dump(Unit *u, FILE *f, const char *prefix) {
 
         fprintf(f,
                 "%sAutomount State: %s\n"
-                "%sWhere: %s\n",
+                "%sWhere: %s\n"
+                "%sDirectoryMode: %04o\n",
                 prefix, automount_state_to_string(a->state),
-                prefix, a->where);
+                prefix, a->where,
+                prefix, a->directory_mode);
 }
 
 static void automount_enter_dead(Automount *a, bool success) {
@@ -259,7 +294,7 @@ static void automount_enter_dead(Automount *a, bool success) {
         if (!success)
                 a->failure = true;
 
-        automount_set_state(a, a->failure ? AUTOMOUNT_MAINTAINANCE : AUTOMOUNT_DEAD);
+        automount_set_state(a, a->failure ? AUTOMOUNT_FAILED : AUTOMOUNT_DEAD);
 }
 
 static int open_dev_autofs(Manager *m) {
@@ -270,6 +305,8 @@ static int open_dev_autofs(Manager *m) {
         if (m->dev_autofs_fd >= 0)
                 return m->dev_autofs_fd;
 
+       label_fix("/dev/autofs", false);
+
         if ((m->dev_autofs_fd = open("/dev/autofs", O_CLOEXEC|O_RDONLY)) < 0) {
                 log_error("Failed to open /dev/autofs: %s", strerror(errno));
                 return -errno;
@@ -398,7 +435,7 @@ int automount_send_ready(Automount *a, int status) {
         if (set_isempty(a->tokens))
                 return 0;
 
-        if ((ioctl_fd = open_ioctl_fd(UNIT(a)->meta.manager->dev_autofs_fd, a->where, a->dev_id)) < 0) {
+        if ((ioctl_fd = open_ioctl_fd(a->meta.manager->dev_autofs_fd, a->where, a->dev_id)) < 0) {
                 r = ioctl_fd;
                 goto fail;
         }
@@ -408,6 +445,8 @@ int automount_send_ready(Automount *a, int status) {
         else
                 log_debug("Sending success.");
 
+        r = 0;
+
         /* Autofs thankfully does not hand out 0 as a token */
         while ((token = PTR_TO_UINT(set_steal_first(a->tokens)))) {
                 int k;
@@ -417,15 +456,13 @@ 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)->meta.manager->dev_autofs_fd,
+                if ((k = autofs_send_ready(a->meta.manager->dev_autofs_fd,
                                            ioctl_fd,
                                            token,
                                            status)) < 0)
                         r = k;
         }
 
-        r = 0;
-
 fail:
         if (ioctl_fd >= 0)
                 close_nointr_nofail(ioctl_fd);
@@ -447,7 +484,7 @@ static void automount_enter_waiting(Automount *a) {
         if (a->tokens)
                 set_clear(a->tokens);
 
-        if ((dev_autofs_fd = open_dev_autofs(UNIT(a)->meta.manager)) < 0) {
+        if ((dev_autofs_fd = open_dev_autofs(a->meta.manager)) < 0) {
                 r = dev_autofs_fd;
                 goto fail;
         }
@@ -527,21 +564,33 @@ fail:
 static void automount_enter_runnning(Automount *a) {
         int r;
         struct stat st;
+        DBusError error;
 
         assert(a);
         assert(a->mount);
 
-        /* Before we do anything, let's see if somebody is playing games with us? */
+        dbus_error_init(&error);
 
-        if (stat(a->where, &st) < 0) {
+        /* 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.", a->meta.id);
+                automount_send_ready(a, -EHOSTDOWN);
+                return;
+        }
+
+        mkdir_p(a->where, a->directory_mode);
+
+        /* Before we do anything, let's see if somebody is playing games with us? */
+        if (lstat(a->where, &st) < 0) {
                 log_warning("%s failed stat automount point: %m", a->meta.id);
                 goto fail;
         }
 
         if (!S_ISDIR(st.st_mode) || st.st_dev != a->dev_id)
                 log_info("%s's automount point already active?", a->meta.id);
-        else if ((r = manager_add_job(UNIT(a)->meta.manager, JOB_START, UNIT(a->mount), JOB_REPLACE, true, NULL)) < 0) {
-                log_warning("%s failed to queue mount startup job: %s", a->meta.id, strerror(-r));
+        else if ((r = manager_add_job(a->meta.manager, JOB_START, UNIT(a->mount), JOB_REPLACE, true, &error, NULL)) < 0) {
+                log_warning("%s failed to queue mount startup job: %s", a->meta.id, bus_error(&error, r));
                 goto fail;
         }
 
@@ -550,6 +599,7 @@ static void automount_enter_runnning(Automount *a) {
 
 fail:
         automount_enter_dead(a, false);
+        dbus_error_free(&error);
 }
 
 static int automount_start(Unit *u) {
@@ -557,12 +607,15 @@ static int automount_start(Unit *u) {
 
         assert(a);
 
+        assert(a->state == AUTOMOUNT_DEAD || a->state == AUTOMOUNT_FAILED);
+
         if (path_is_mount_point(a->where)) {
                 log_error("Path %s is already a mount point, refusing start for %s", a->where, u->meta.id);
                 return -EEXIST;
         }
 
-        assert(a->state == AUTOMOUNT_DEAD || a->state == AUTOMOUNT_MAINTAINANCE);
+        if (a->mount->meta.load_state != UNIT_LOADED)
+                return -ENOENT;
 
         a->failure = false;
         automount_enter_waiting(a);
@@ -683,6 +736,9 @@ static bool automount_check_gc(Unit *u) {
 
         assert(a);
 
+        if (!a->mount)
+                return false;
+
         return UNIT_VTABLE(UNIT(a->mount))->check_gc(UNIT(a->mount));
 }
 
@@ -700,7 +756,7 @@ static void automount_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
                 goto fail;
         }
 
-        if ((l = loop_read(a->pipe_fd, &packet, sizeof(packet))) != sizeof(packet)) {
+        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");
                 goto fail;
         }
@@ -708,7 +764,16 @@ static void automount_fd_event(Unit *u, int fd, uint32_t events, Watch *w) {
         switch (packet.hdr.type) {
 
         case autofs_ptype_missing_direct:
-                log_debug("Got direct mount request for %s", packet.v5_packet.name);
+
+                if (packet.v5_packet.pid > 0) {
+                        char *p = NULL;
+
+                        get_process_name(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);
+
+                } else
+                        log_debug("Got direct mount request for %s", packet.v5_packet.name);
 
                 if (!a->tokens)
                         if (!(a->tokens = set_new(trivial_hash_func, trivial_compare_func))) {
@@ -742,11 +807,22 @@ static void automount_shutdown(Manager *m) {
                 close_nointr_nofail(m->dev_autofs_fd);
 }
 
+static void automount_reset_failed(Unit *u) {
+        Automount *a = AUTOMOUNT(u);
+
+        assert(a);
+
+        if (a->state == AUTOMOUNT_FAILED)
+                automount_set_state(a, AUTOMOUNT_DEAD);
+
+        a->failure = false;
+}
+
 static const char* const automount_state_table[_AUTOMOUNT_STATE_MAX] = {
         [AUTOMOUNT_DEAD] = "dead",
         [AUTOMOUNT_WAITING] = "waiting",
         [AUTOMOUNT_RUNNING] = "running",
-        [AUTOMOUNT_MAINTAINANCE] = "maintainance"
+        [AUTOMOUNT_FAILED] = "failed"
 };
 
 DEFINE_STRING_TABLE_LOOKUP(automount_state, AutomountState);
@@ -778,6 +854,9 @@ const UnitVTable automount_vtable = {
 
         .fd_event = automount_fd_event,
 
+        .reset_failed = automount_reset_failed,
+
+        .bus_interface = "org.freedesktop.systemd1.Automount",
         .bus_message_handler = bus_automount_message_handler,
 
         .shutdown = automount_shutdown