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