dd65c9
From 85eeadc898d2c0f8b7524982c84b88b01a5dcb89 Mon Sep 17 00:00:00 2001
dd65c9
From: Lennart Poettering <lennart@poettering.net>
dd65c9
Date: Wed, 1 Mar 2017 04:03:48 +0100
dd65c9
Subject: [PATCH] automount: if an automount unit is masked, don't react to
dd65c9
 activation anymore (#5445)
dd65c9
dd65c9
Otherwise we'll hit an assert sooner or later.
dd65c9
dd65c9
This requires us to initialize ->where even if we come back in "masked"
dd65c9
mode, as otherwise we don't know how to operate on the automount and
dd65c9
detach it.
dd65c9
dd65c9
Fixes: #5441
dd65c9
(cherry picked from commit e350ca3f1ecb6672b74cd25d09ef23c7b309aa5a)
dd65c9
dd65c9
Resolves: #1498318
dd65c9
---
dd65c9
 src/core/automount.c | 74 +++++++++++++++++++++++++++++++++++-----------------
dd65c9
 1 file changed, 50 insertions(+), 24 deletions(-)
dd65c9
dd65c9
diff --git a/src/core/automount.c b/src/core/automount.c
dd65c9
index 4e066613d..20a5de8ca 100644
dd65c9
--- a/src/core/automount.c
dd65c9
+++ b/src/core/automount.c
dd65c9
@@ -106,18 +106,18 @@ static void unmount_autofs(Automount *a) {
dd65c9
         if (a->pipe_fd < 0)
dd65c9
                 return;
dd65c9
 
dd65c9
-        automount_send_ready(a, a->tokens, -EHOSTDOWN);
dd65c9
-        automount_send_ready(a, a->expire_tokens, -EHOSTDOWN);
dd65c9
-
dd65c9
         a->pipe_event_source = sd_event_source_unref(a->pipe_event_source);
dd65c9
         a->pipe_fd = safe_close(a->pipe_fd);
dd65c9
 
dd65c9
-        /* If we reload/reexecute things we keep the mount point
dd65c9
-         * around */
dd65c9
-        if (a->where &&
dd65c9
-            (UNIT(a)->manager->exit_code != MANAGER_RELOAD &&
dd65c9
-             UNIT(a)->manager->exit_code != MANAGER_REEXECUTE))
dd65c9
-                repeat_unmount(a->where);
dd65c9
+        /* If we reload/reexecute things we keep the mount point around */
dd65c9
+        if (!IN_SET(UNIT(a)->manager->exit_code, MANAGER_RELOAD, MANAGER_REEXECUTE)) {
dd65c9
+
dd65c9
+                automount_send_ready(a, a->tokens, -EHOSTDOWN);
dd65c9
+                automount_send_ready(a, a->expire_tokens, -EHOSTDOWN);
dd65c9
+
dd65c9
+                if (a->where)
dd65c9
+                        repeat_unmount(a->where);
dd65c9
+        }
dd65c9
 }
dd65c9
 
dd65c9
 static void automount_done(Unit *u) {
dd65c9
@@ -193,6 +193,20 @@ static int automount_verify(Automount *a) {
dd65c9
         return 0;
dd65c9
 }
dd65c9
 
dd65c9
+static int automount_set_where(Automount *a) {
dd65c9
+        assert(a);
dd65c9
+
dd65c9
+        if (a->where)
dd65c9
+                return 0;
dd65c9
+
dd65c9
+        a->where = unit_name_to_path(UNIT(a)->id);
dd65c9
+        if (!a->where)
dd65c9
+                return -ENOMEM;
dd65c9
+
dd65c9
+        path_kill_slashes(a->where);
dd65c9
+        return 1;
dd65c9
+}
dd65c9
+
dd65c9
 static int automount_load(Unit *u) {
dd65c9
         Automount *a = AUTOMOUNT(u);
dd65c9
         int r;
dd65c9
@@ -208,13 +222,9 @@ static int automount_load(Unit *u) {
dd65c9
         if (u->load_state == UNIT_LOADED) {
dd65c9
                 Unit *x;
dd65c9
 
dd65c9
-                if (!a->where) {
dd65c9
-                        a->where = unit_name_to_path(u->id);
dd65c9
-                        if (!a->where)
dd65c9
-                                return -ENOMEM;
dd65c9
-                }
dd65c9
-
dd65c9
-                path_kill_slashes(a->where);
dd65c9
+                r = automount_set_where(a);
dd65c9
+                if (r < 0)
dd65c9
+                        return r;
dd65c9
 
dd65c9
                 r = unit_load_related_unit(u, ".mount", &x);
dd65c9
                 if (r < 0)
dd65c9
@@ -259,6 +269,8 @@ static void automount_set_state(Automount *a, AutomountState state) {
dd65c9
         unit_notify(UNIT(a), state_translation_table[old_state], state_translation_table[state], true);
dd65c9
 }
dd65c9
 
dd65c9
+static int automount_start_expire(Automount *a);
dd65c9
+
dd65c9
 static int automount_coldplug(Unit *u, Hashmap *deferred_work) {
dd65c9
         Automount *a = AUTOMOUNT(u);
dd65c9
         int r;
dd65c9
@@ -266,20 +278,30 @@ static int automount_coldplug(Unit *u, Hashmap *deferred_work) {
dd65c9
         assert(a);
dd65c9
         assert(a->state == AUTOMOUNT_DEAD);
dd65c9
 
dd65c9
-        if (a->deserialized_state != a->state) {
dd65c9
+        if (a->deserialized_state == a->state)
dd65c9
+                return 0;
dd65c9
+
dd65c9
+        if (IN_SET(a->deserialized_state, AUTOMOUNT_WAITING, AUTOMOUNT_RUNNING)) {
dd65c9
+
dd65c9
+                r = automount_set_where(a);
dd65c9
+                if (r < 0)
dd65c9
+                        return r;
dd65c9
 
dd65c9
                 r = open_dev_autofs(u->manager);
dd65c9
                 if (r < 0)
dd65c9
                         return r;
dd65c9
 
dd65c9
-                if (a->deserialized_state == AUTOMOUNT_WAITING ||
dd65c9
-                    a->deserialized_state == AUTOMOUNT_RUNNING) {
dd65c9
+                assert(a->pipe_fd >= 0);
dd65c9
 
dd65c9
-                        assert(a->pipe_fd >= 0);
dd65c9
+                r = sd_event_add_io(u->manager->event, &a->pipe_event_source, a->pipe_fd, EPOLLIN, automount_dispatch_io, u);
dd65c9
+                if (r < 0)
dd65c9
+                        return r;
dd65c9
 
dd65c9
-                        r = sd_event_add_io(u->manager->event, &a->pipe_event_source, a->pipe_fd, EPOLLIN, automount_dispatch_io, u);
dd65c9
+                (void) sd_event_source_set_description(a->pipe_event_source, "automount-io");
dd65c9
+                if (a->deserialized_state == AUTOMOUNT_RUNNING) {
dd65c9
+                        r = automount_start_expire(a);
dd65c9
                         if (r < 0)
dd65c9
-                                return r;
dd65c9
+                                log_unit_warning_errno(UNIT(a)->id, r, "Failed to start expiration timer, ignoring: %m");
dd65c9
                 }
dd65c9
 
dd65c9
                 automount_set_state(a, a->deserialized_state);
dd65c9
@@ -636,8 +658,6 @@ static void *expire_thread(void *p) {
dd65c9
         return NULL;
dd65c9
 }
dd65c9
 
dd65c9
-static int automount_start_expire(Automount *a);
dd65c9
-
dd65c9
 static int automount_dispatch_expire(sd_event_source *source, usec_t usec, void *userdata) {
dd65c9
         Automount *a = AUTOMOUNT(userdata);
dd65c9
         _cleanup_(expire_data_freep) struct expire_data *data = NULL;
dd65c9
@@ -699,6 +719,12 @@ static void automount_enter_runnning(Automount *a) {
dd65c9
 
dd65c9
         assert(a);
dd65c9
 
dd65c9
+        /* If the user masked our unit in the meantime, fail */
dd65c9
+        if (UNIT(a)->load_state != UNIT_LOADED) {
dd65c9
+                log_unit_error(UNIT(a)->id, "Suppressing automount event since unit is no longer loaded.");
dd65c9
+                goto fail;
dd65c9
+        }
dd65c9
+
dd65c9
         /* We don't take mount requests anymore if we are supposed to
dd65c9
          * shut down anyway */
dd65c9
         if (unit_stop_pending(UNIT(a))) {