daandemeyer / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
ff2b41
From f57ee0c7862260f3c7054b224f360cdef5140685 Mon Sep 17 00:00:00 2001
ff2b41
From: Lennart Poettering <lennart@poettering.net>
ff2b41
Date: Tue, 19 May 2015 16:00:24 +0200
ff2b41
Subject: [PATCH] core: enforce a ratelimiter when stopping units due to
ff2b41
 StopWhenUnneeded=1
ff2b41
ff2b41
Otherwise we might end up in an endless stop loop.
ff2b41
ff2b41
http://lists.freedesktop.org/archives/systemd-devel/2015-April/030224.html
ff2b41
(cherry picked from commit bea355dac94e82697aa98e25d80ee4248263bf92)
ff2b41
(cherry picked from commit 6c72a575c7c61e17c8e96b23042f7b4a5ac39339)
ff2b41
ff2b41
Related: #1810576
ff2b41
---
ff2b41
 src/core/unit.c | 10 ++++++++++
ff2b41
 src/core/unit.h |  3 +++
ff2b41
 2 files changed, 13 insertions(+)
ff2b41
ff2b41
diff --git a/src/core/unit.c b/src/core/unit.c
ff2b41
index eff9fdbe70..583b9fae28 100644
ff2b41
--- a/src/core/unit.c
ff2b41
+++ b/src/core/unit.c
ff2b41
@@ -97,6 +97,8 @@ Unit *unit_new(Manager *m, size_t size) {
ff2b41
         u->on_failure_job_mode = JOB_REPLACE;
ff2b41
         u->sigchldgen = 0;
ff2b41
 
ff2b41
+        RATELIMIT_INIT(u->check_unneeded_ratelimit, 10 * USEC_PER_SEC, 16);
ff2b41
+
ff2b41
         return u;
ff2b41
 }
ff2b41
 
ff2b41
@@ -1594,6 +1596,14 @@ static void unit_check_unneeded(Unit *u) {
ff2b41
                 if (unit_active_or_pending(other))
ff2b41
                         return;
ff2b41
 
ff2b41
+        /* If stopping a unit fails continously we might enter a stop
ff2b41
+         * loop here, hence stop acting on the service being
ff2b41
+         * unnecessary after a while. */
ff2b41
+        if (!ratelimit_test(&u->check_unneeded_ratelimit)) {
ff2b41
+                log_unit_warning(u->id, "Unit not needed anymore, but not stopping since we tried this too often recently.");
ff2b41
+                return;
ff2b41
+        }
ff2b41
+
ff2b41
         log_unit_info(u->id, "Unit %s is not needed anymore. Stopping.", u->id);
ff2b41
 
ff2b41
         /* Ok, nobody needs us anymore. Sniff. Then let's commit suicide */
ff2b41
diff --git a/src/core/unit.h b/src/core/unit.h
ff2b41
index 3b0fd8d9df..29353ea81c 100644
ff2b41
--- a/src/core/unit.h
ff2b41
+++ b/src/core/unit.h
ff2b41
@@ -185,6 +185,9 @@ struct Unit {
ff2b41
         /* Error code when we didn't manage to load the unit (negative) */
ff2b41
         int load_error;
ff2b41
 
ff2b41
+        /* Make sure we never enter endless loops with the check unneeded logic */
ff2b41
+        RateLimit check_unneeded_ratelimit;
ff2b41
+
ff2b41
         /* Cached unit file state and preset */
ff2b41
         UnitFileState unit_file_state;
ff2b41
         int unit_file_preset;