ecbff1
From d7b2f6efd02375af4cf043ef9db6d316b65d4779 Mon Sep 17 00:00:00 2001
ecbff1
From: Jan Synacek <jsynacek@redhat.com>
ecbff1
Date: Fri, 16 Feb 2018 09:56:50 +0100
ecbff1
Subject: [PATCH] core: don't choke if a unit another unit triggers vanishes
ecbff1
 during reload
ecbff1
ecbff1
Fixes: #1981
ecbff1
ecbff1
(cherry picked from e903182e5b0daa941de47a9c08c824106cec7fe0)
ecbff1
Resolves: #1545676
ecbff1
---
ecbff1
 src/core/automount.c | 25 +++++++++++++++++++++----
ecbff1
 src/core/path.c      | 18 +++++++++++++++---
ecbff1
 src/core/timer.c     | 30 ++++++++++++++++++++++++++----
ecbff1
 3 files changed, 62 insertions(+), 11 deletions(-)
ecbff1
ecbff1
diff --git a/src/core/automount.c b/src/core/automount.c
ecbff1
index 182ba5240..679fe071e 100644
ecbff1
--- a/src/core/automount.c
ecbff1
+++ b/src/core/automount.c
ecbff1
@@ -715,6 +715,7 @@ static int automount_start_expire(Automount *a) {
ecbff1
 static void automount_enter_running(Automount *a) {
ecbff1
         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
ecbff1
         struct stat st;
ecbff1
+        Unit *trigger;
ecbff1
         int r;
ecbff1
 
ecbff1
         assert(a);
ecbff1
@@ -753,8 +754,13 @@ static void automount_enter_running(Automount *a) {
ecbff1
                 return;
ecbff1
         }
ecbff1
 
ecbff1
-        r = manager_add_job(UNIT(a)->manager, JOB_START, UNIT_TRIGGER(UNIT(a)),
ecbff1
-                        JOB_REPLACE, true, &error, NULL);
ecbff1
+        trigger = UNIT_TRIGGER(UNIT(a));
ecbff1
+        if (!trigger) {
ecbff1
+                log_unit_error(UNIT(a)->id, "Unit to trigger vanished.");
ecbff1
+                goto fail;
ecbff1
+        }
ecbff1
+
ecbff1
+        r = manager_add_job(UNIT(a)->manager, JOB_START, trigger, JOB_REPLACE, true, &error, NULL);
ecbff1
         if (r < 0) {
ecbff1
                 log_unit_warning(UNIT(a)->id,
ecbff1
                                 "%s failed to queue mount startup job: %s",
ecbff1
@@ -775,6 +781,7 @@ fail:
ecbff1
 
ecbff1
 static int automount_start(Unit *u) {
ecbff1
         Automount *a = AUTOMOUNT(u);
ecbff1
+        Unit *trigger;
ecbff1
 
ecbff1
         assert(a);
ecbff1
         assert(a->state == AUTOMOUNT_DEAD || a->state == AUTOMOUNT_FAILED);
ecbff1
@@ -786,8 +793,11 @@ static int automount_start(Unit *u) {
ecbff1
                 return -EEXIST;
ecbff1
         }
ecbff1
 
ecbff1
-        if (UNIT_TRIGGER(u)->load_state != UNIT_LOADED)
ecbff1
+        trigger = UNIT_TRIGGER(u);
ecbff1
+        if (!trigger || trigger->load_state != UNIT_LOADED) {
ecbff1
+                log_unit_error(u->id, "Refusing to start, unit to trigger not loaded.");
ecbff1
                 return -ENOENT;
ecbff1
+        }
ecbff1
 
ecbff1
         a->result = AUTOMOUNT_SUCCESS;
ecbff1
         automount_enter_waiting(a);
ecbff1
@@ -936,6 +946,7 @@ static int automount_dispatch_io(sd_event_source *s, int fd, uint32_t events, vo
ecbff1
         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
ecbff1
         union autofs_v5_packet_union packet;
ecbff1
         Automount *a = AUTOMOUNT(userdata);
ecbff1
+        Unit *trigger;
ecbff1
         ssize_t l;
ecbff1
         int r;
ecbff1
 
ecbff1
@@ -1002,7 +1013,13 @@ static int automount_dispatch_io(sd_event_source *s, int fd, uint32_t events, vo
ecbff1
                         log_unit_error_errno(UNIT(a)->id, r, "Failed to remember token: %m");
ecbff1
                         goto fail;
ecbff1
                 }
ecbff1
-                r = manager_add_job(UNIT(a)->manager, JOB_STOP, UNIT_TRIGGER(UNIT(a)), JOB_REPLACE, true, &error, NULL);
ecbff1
+
ecbff1
+                trigger = UNIT_TRIGGER(UNIT(a));
ecbff1
+                if (!trigger) {
ecbff1
+                        log_unit_error(UNIT(a)->id, "Unit to trigger vanished.");
ecbff1
+                        goto fail;
ecbff1
+                }
ecbff1
+                r = manager_add_job(UNIT(a)->manager, JOB_STOP, trigger, JOB_REPLACE, true, &error, NULL);
ecbff1
                 if (r < 0) {
ecbff1
                         log_unit_warning(UNIT(a)->id,
ecbff1
                                          "%s failed to queue umount startup job: %s",
ecbff1
diff --git a/src/core/path.c b/src/core/path.c
ecbff1
index 51e36fa8b..0533bb4e2 100644
ecbff1
--- a/src/core/path.c
ecbff1
+++ b/src/core/path.c
ecbff1
@@ -475,6 +475,7 @@ static void path_enter_dead(Path *p, PathResult f) {
ecbff1
 
ecbff1
 static void path_enter_running(Path *p) {
ecbff1
         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
ecbff1
+        Unit *trigger;
ecbff1
         int r;
ecbff1
 
ecbff1
         assert(p);
ecbff1
@@ -483,8 +484,14 @@ static void path_enter_running(Path *p) {
ecbff1
         if (unit_stop_pending(UNIT(p)))
ecbff1
                 return;
ecbff1
 
ecbff1
-        r = manager_add_job(UNIT(p)->manager, JOB_START, UNIT_TRIGGER(UNIT(p)),
ecbff1
-                            JOB_REPLACE, true, &error, NULL);
ecbff1
+        trigger = UNIT_TRIGGER(UNIT(p));
ecbff1
+        if (!trigger) {
ecbff1
+                log_unit_error(UNIT(p)->id, "Unit to trigger vanished.");
ecbff1
+                path_enter_dead(p, TIMER_FAILURE_RESOURCES);
ecbff1
+                return;
ecbff1
+        }
ecbff1
+
ecbff1
+        r = manager_add_job(UNIT(p)->manager, JOB_START, trigger, JOB_REPLACE, true, &error, NULL);
ecbff1
         if (r < 0)
ecbff1
                 goto fail;
ecbff1
 
ecbff1
@@ -566,12 +573,17 @@ static void path_mkdir(Path *p) {
ecbff1
 
ecbff1
 static int path_start(Unit *u) {
ecbff1
         Path *p = PATH(u);
ecbff1
+        Unit *trigger;
ecbff1
 
ecbff1
         assert(p);
ecbff1
         assert(p->state == PATH_DEAD || p->state == PATH_FAILED);
ecbff1
 
ecbff1
-        if (UNIT_TRIGGER(u)->load_state != UNIT_LOADED)
ecbff1
+
ecbff1
+        trigger = UNIT_TRIGGER(u);
ecbff1
+        if (!trigger || trigger->load_state != UNIT_LOADED) {
ecbff1
+                log_unit_error(u->id, "Refusing to start, unit to trigger not loaded.");
ecbff1
                 return -ENOENT;
ecbff1
+        }
ecbff1
 
ecbff1
         path_mkdir(p);
ecbff1
 
ecbff1
diff --git a/src/core/timer.c b/src/core/timer.c
ecbff1
index f318dc6f4..91d8db67e 100644
ecbff1
--- a/src/core/timer.c
ecbff1
+++ b/src/core/timer.c
ecbff1
@@ -343,8 +343,18 @@ static void timer_enter_waiting(Timer *t, bool initial) {
ecbff1
         usec_t ts_realtime, ts_monotonic;
ecbff1
         usec_t base = 0;
ecbff1
         TimerValue *v;
ecbff1
+        Unit *trigger;
ecbff1
         int r;
ecbff1
 
ecbff1
+        assert(t);
ecbff1
+
ecbff1
+        trigger = UNIT_TRIGGER(UNIT(t));
ecbff1
+        if (!trigger) {
ecbff1
+                log_unit_error(UNIT(t)->id, "Unit to trigger vanished.");
ecbff1
+                timer_enter_dead(t, TIMER_FAILURE_RESOURCES);
ecbff1
+                return;
ecbff1
+        }
ecbff1
+
ecbff1
         /* If we shall wake the system we use the boottime clock
ecbff1
          * rather than the monotonic clock. */
ecbff1
 
ecbff1
@@ -399,7 +409,7 @@ static void timer_enter_waiting(Timer *t, bool initial) {
ecbff1
 
ecbff1
                         case TIMER_UNIT_ACTIVE:
ecbff1
 
ecbff1
-                                base = UNIT_TRIGGER(UNIT(t))->inactive_exit_timestamp.monotonic;
ecbff1
+                                base = trigger->inactive_exit_timestamp.monotonic;
ecbff1
 
ecbff1
                                 if (base <= 0)
ecbff1
                                         base = t->last_trigger.monotonic;
ecbff1
@@ -523,6 +533,7 @@ fail:
ecbff1
 
ecbff1
 static void timer_enter_running(Timer *t) {
ecbff1
         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
ecbff1
+        Unit *trigger;
ecbff1
         int r;
ecbff1
 
ecbff1
         assert(t);
ecbff1
@@ -531,8 +542,15 @@ static void timer_enter_running(Timer *t) {
ecbff1
         if (unit_stop_pending(UNIT(t)))
ecbff1
                 return;
ecbff1
 
ecbff1
-        r = manager_add_job(UNIT(t)->manager, JOB_START, UNIT_TRIGGER(UNIT(t)),
ecbff1
-                            JOB_REPLACE, true, &error, NULL);
ecbff1
+
ecbff1
+        trigger = UNIT_TRIGGER(UNIT(t));
ecbff1
+        if (!trigger) {
ecbff1
+                log_unit_error(UNIT(t)->id, "Unit to trigger vanished.");
ecbff1
+                timer_enter_dead(t, TIMER_FAILURE_RESOURCES);
ecbff1
+                return;
ecbff1
+        }
ecbff1
+
ecbff1
+        r = manager_add_job(UNIT(t)->manager, JOB_START, trigger, JOB_REPLACE, true, &error, NULL);
ecbff1
         if (r < 0)
ecbff1
                 goto fail;
ecbff1
 
ecbff1
@@ -554,12 +572,16 @@ fail:
ecbff1
 static int timer_start(Unit *u) {
ecbff1
         Timer *t = TIMER(u);
ecbff1
         TimerValue *v;
ecbff1
+        Unit *trigger;
ecbff1
 
ecbff1
         assert(t);
ecbff1
         assert(t->state == TIMER_DEAD || t->state == TIMER_FAILED);
ecbff1
 
ecbff1
-        if (UNIT_TRIGGER(u)->load_state != UNIT_LOADED)
ecbff1
+        trigger = UNIT_TRIGGER(u);
ecbff1
+        if (!trigger || trigger->load_state != UNIT_LOADED) {
ecbff1
+                log_unit_error(u->id, "Refusing to start, unit to trigger not loaded.");
ecbff1
                 return -ENOENT;
ecbff1
+        }
ecbff1
 
ecbff1
         t->last_trigger = DUAL_TIMESTAMP_NULL;
ecbff1