naccyde / rpms / systemd

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