daandemeyer / rpms / systemd

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