dryang / rpms / systemd

Forked from rpms/systemd a year ago
Clone
be0c12
From 471eda89a25a3ceac91a2d05e39a54aae78038ed Mon Sep 17 00:00:00 2001
be0c12
From: Daan De Meyer <daan.j.demeyer@gmail.com>
be0c12
Date: Tue, 24 Aug 2021 16:46:47 +0100
be0c12
Subject: [PATCH] core: Check unit start rate limiting earlier
be0c12
be0c12
Fixes #17433. Currently, if any of the validations we do before we
be0c12
check start rate limiting fail, we can still enter a busy loop as
be0c12
no rate limiting gets applied. A common occurence of this scenario
be0c12
is path units triggering a service that fails a condition check.
be0c12
be0c12
To fix the issue, we simply move up start rate limiting checks to
be0c12
be the first thing we do when starting a unit. To achieve this,
be0c12
we add a new method to the unit vtable and implement it for the
be0c12
relevant unit types so that we can do the start rate limit checks
be0c12
earlier on.
be0c12
be0c12
(cherry picked from commit 9727f2427ff6b2e1f4ab927cc57ad8e888f04e95)
be0c12
be0c12
Related: #2036608
be0c12
be0c12
[msekleta: I've deleted part of the original commit that adds test for
be0c12
issue #17433. This was necessary because upstream commit assumes newer
be0c12
organization of the test code which we don't have in RHEL-8 tree. As
be0c12
a consequence we must add explicit test for this in the internal
be0c12
test-suite.]
be0c12
---
be0c12
 src/core/automount.c | 23 +++++++++++++++++------
be0c12
 src/core/mount.c     | 23 +++++++++++++++++------
be0c12
 src/core/path.c      | 23 +++++++++++++++++------
be0c12
 src/core/service.c   | 25 ++++++++++++++++++-------
be0c12
 src/core/socket.c    | 23 +++++++++++++++++------
be0c12
 src/core/swap.c      | 23 +++++++++++++++++------
be0c12
 src/core/timer.c     | 22 ++++++++++++++++------
be0c12
 src/core/unit.c      | 14 ++++++++++----
be0c12
 src/core/unit.h      |  4 ++++
be0c12
 9 files changed, 133 insertions(+), 47 deletions(-)
be0c12
be0c12
diff --git a/src/core/automount.c b/src/core/automount.c
be0c12
index 2bc160cb07..5e16adabb5 100644
be0c12
--- a/src/core/automount.c
be0c12
+++ b/src/core/automount.c
be0c12
@@ -808,12 +808,6 @@ static int automount_start(Unit *u) {
be0c12
                 return -ENOENT;
be0c12
         }
be0c12
 
be0c12
-        r = unit_test_start_limit(u);
be0c12
-        if (r < 0) {
be0c12
-                automount_enter_dead(a, AUTOMOUNT_FAILURE_START_LIMIT_HIT);
be0c12
-                return r;
be0c12
-        }
be0c12
-
be0c12
         r = unit_acquire_invocation_id(u);
be0c12
         if (r < 0)
be0c12
                 return r;
be0c12
@@ -1077,6 +1071,21 @@ static bool automount_supported(void) {
be0c12
         return supported;
be0c12
 }
be0c12
 
be0c12
+static int automount_test_start_limit(Unit *u) {
be0c12
+        Automount *a = AUTOMOUNT(u);
be0c12
+        int r;
be0c12
+
be0c12
+        assert(a);
be0c12
+
be0c12
+        r = unit_test_start_limit(u);
be0c12
+        if (r < 0) {
be0c12
+                automount_enter_dead(a, AUTOMOUNT_FAILURE_START_LIMIT_HIT);
be0c12
+                return r;
be0c12
+        }
be0c12
+
be0c12
+        return 0;
be0c12
+}
be0c12
+
be0c12
 static const char* const automount_result_table[_AUTOMOUNT_RESULT_MAX] = {
be0c12
         [AUTOMOUNT_SUCCESS] = "success",
be0c12
         [AUTOMOUNT_FAILURE_RESOURCES] = "resources",
be0c12
@@ -1135,4 +1144,6 @@ const UnitVTable automount_vtable = {
be0c12
                         [JOB_FAILED]     = "Failed to unset automount %s.",
be0c12
                 },
be0c12
         },
be0c12
+
be0c12
+        .test_start_limit = automount_test_start_limit,
be0c12
 };
be0c12
diff --git a/src/core/mount.c b/src/core/mount.c
be0c12
index aa586d88cb..22848847e5 100644
be0c12
--- a/src/core/mount.c
be0c12
+++ b/src/core/mount.c
be0c12
@@ -1065,12 +1065,6 @@ static int mount_start(Unit *u) {
be0c12
 
be0c12
         assert(IN_SET(m->state, MOUNT_DEAD, MOUNT_FAILED));
be0c12
 
be0c12
-        r = unit_test_start_limit(u);
be0c12
-        if (r < 0) {
be0c12
-                mount_enter_dead(m, MOUNT_FAILURE_START_LIMIT_HIT);
be0c12
-                return r;
be0c12
-        }
be0c12
-
be0c12
         r = unit_acquire_invocation_id(u);
be0c12
         if (r < 0)
be0c12
                 return r;
be0c12
@@ -1957,6 +1951,21 @@ static int mount_control_pid(Unit *u) {
be0c12
         return m->control_pid;
be0c12
 }
be0c12
 
be0c12
+static int mount_test_start_limit(Unit *u) {
be0c12
+        Mount *m = MOUNT(u);
be0c12
+        int r;
be0c12
+
be0c12
+        assert(m);
be0c12
+
be0c12
+        r = unit_test_start_limit(u);
be0c12
+        if (r < 0) {
be0c12
+                mount_enter_dead(m, MOUNT_FAILURE_START_LIMIT_HIT);
be0c12
+                return r;
be0c12
+        }
be0c12
+
be0c12
+        return 0;
be0c12
+}
be0c12
+
be0c12
 static const char* const mount_exec_command_table[_MOUNT_EXEC_COMMAND_MAX] = {
be0c12
         [MOUNT_EXEC_MOUNT] = "ExecMount",
be0c12
         [MOUNT_EXEC_UNMOUNT] = "ExecUnmount",
be0c12
@@ -2048,4 +2057,6 @@ const UnitVTable mount_vtable = {
be0c12
                         [JOB_TIMEOUT]    = "Timed out unmounting %s.",
be0c12
                 },
be0c12
         },
be0c12
+
be0c12
+        .test_start_limit = mount_test_start_limit,
be0c12
 };
be0c12
diff --git a/src/core/path.c b/src/core/path.c
be0c12
index 4bccc0396b..1e69a1f05f 100644
be0c12
--- a/src/core/path.c
be0c12
+++ b/src/core/path.c
be0c12
@@ -565,12 +565,6 @@ static int path_start(Unit *u) {
be0c12
                 return -ENOENT;
be0c12
         }
be0c12
 
be0c12
-        r = unit_test_start_limit(u);
be0c12
-        if (r < 0) {
be0c12
-                path_enter_dead(p, PATH_FAILURE_START_LIMIT_HIT);
be0c12
-                return r;
be0c12
-        }
be0c12
-
be0c12
         r = unit_acquire_invocation_id(u);
be0c12
         if (r < 0)
be0c12
                 return r;
be0c12
@@ -730,6 +724,21 @@ static void path_reset_failed(Unit *u) {
be0c12
         p->result = PATH_SUCCESS;
be0c12
 }
be0c12
 
be0c12
+static int path_test_start_limit(Unit *u) {
be0c12
+        Path *p = PATH(u);
be0c12
+        int r;
be0c12
+
be0c12
+        assert(p);
be0c12
+
be0c12
+        r = unit_test_start_limit(u);
be0c12
+        if (r < 0) {
be0c12
+                path_enter_dead(p, PATH_FAILURE_START_LIMIT_HIT);
be0c12
+                return r;
be0c12
+        }
be0c12
+
be0c12
+        return 0;
be0c12
+}
be0c12
+
be0c12
 static const char* const path_type_table[_PATH_TYPE_MAX] = {
be0c12
         [PATH_EXISTS] = "PathExists",
be0c12
         [PATH_EXISTS_GLOB] = "PathExistsGlob",
be0c12
@@ -782,4 +791,6 @@ const UnitVTable path_vtable = {
be0c12
 
be0c12
         .bus_vtable = bus_path_vtable,
be0c12
         .bus_set_property = bus_path_set_property,
be0c12
+
be0c12
+        .test_start_limit = path_test_start_limit,
be0c12
 };
be0c12
diff --git a/src/core/service.c b/src/core/service.c
be0c12
index 1a1de43d0d..c5f408d817 100644
be0c12
--- a/src/core/service.c
be0c12
+++ b/src/core/service.c
be0c12
@@ -2387,13 +2387,6 @@ static int service_start(Unit *u) {
be0c12
 
be0c12
         assert(IN_SET(s->state, SERVICE_DEAD, SERVICE_FAILED));
be0c12
 
be0c12
-        /* Make sure we don't enter a busy loop of some kind. */
be0c12
-        r = unit_test_start_limit(u);
be0c12
-        if (r < 0) {
be0c12
-                service_enter_dead(s, SERVICE_FAILURE_START_LIMIT_HIT, false);
be0c12
-                return r;
be0c12
-        }
be0c12
-
be0c12
         r = unit_acquire_invocation_id(u);
be0c12
         if (r < 0)
be0c12
                 return r;
be0c12
@@ -4081,6 +4074,22 @@ static bool service_needs_console(Unit *u) {
be0c12
                       SERVICE_FINAL_SIGKILL);
be0c12
 }
be0c12
 
be0c12
+static int service_test_start_limit(Unit *u) {
be0c12
+        Service *s = SERVICE(u);
be0c12
+        int r;
be0c12
+
be0c12
+        assert(s);
be0c12
+
be0c12
+        /* Make sure we don't enter a busy loop of some kind. */
be0c12
+        r = unit_test_start_limit(u);
be0c12
+        if (r < 0) {
be0c12
+                service_enter_dead(s, SERVICE_FAILURE_START_LIMIT_HIT, false);
be0c12
+                return r;
be0c12
+        }
be0c12
+
be0c12
+        return 0;
be0c12
+}
be0c12
+
be0c12
 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
be0c12
         [SERVICE_RESTART_NO] = "no",
be0c12
         [SERVICE_RESTART_ON_SUCCESS] = "on-success",
be0c12
@@ -4222,4 +4231,6 @@ const UnitVTable service_vtable = {
be0c12
                         [JOB_FAILED]     = "Stopped (with error) %s.",
be0c12
                 },
be0c12
         },
be0c12
+
be0c12
+        .test_start_limit = service_test_start_limit,
be0c12
 };
be0c12
diff --git a/src/core/socket.c b/src/core/socket.c
be0c12
index 09491c6677..36d2e4f823 100644
be0c12
--- a/src/core/socket.c
be0c12
+++ b/src/core/socket.c
be0c12
@@ -2469,12 +2469,6 @@ static int socket_start(Unit *u) {
be0c12
 
be0c12
         assert(IN_SET(s->state, SOCKET_DEAD, SOCKET_FAILED));
be0c12
 
be0c12
-        r = unit_test_start_limit(u);
be0c12
-        if (r < 0) {
be0c12
-                socket_enter_dead(s, SOCKET_FAILURE_START_LIMIT_HIT);
be0c12
-                return r;
be0c12
-        }
be0c12
-
be0c12
         r = unit_acquire_invocation_id(u);
be0c12
         if (r < 0)
be0c12
                 return r;
be0c12
@@ -3267,6 +3261,21 @@ static int socket_control_pid(Unit *u) {
be0c12
         return s->control_pid;
be0c12
 }
be0c12
 
be0c12
+static int socket_test_start_limit(Unit *u) {
be0c12
+        Socket *s = SOCKET(u);
be0c12
+        int r;
be0c12
+
be0c12
+        assert(s);
be0c12
+
be0c12
+        r = unit_test_start_limit(u);
be0c12
+        if (r < 0) {
be0c12
+                socket_enter_dead(s, SOCKET_FAILURE_START_LIMIT_HIT);
be0c12
+                return r;
be0c12
+        }
be0c12
+
be0c12
+        return 0;
be0c12
+}
be0c12
+
be0c12
 static const char* const socket_exec_command_table[_SOCKET_EXEC_COMMAND_MAX] = {
be0c12
         [SOCKET_EXEC_START_PRE] = "ExecStartPre",
be0c12
         [SOCKET_EXEC_START_CHOWN] = "ExecStartChown",
be0c12
@@ -3359,4 +3368,6 @@ const UnitVTable socket_vtable = {
be0c12
                         [JOB_TIMEOUT]    = "Timed out stopping %s.",
be0c12
                 },
be0c12
         },
be0c12
+
be0c12
+        .test_start_limit = socket_test_start_limit,
be0c12
 };
be0c12
diff --git a/src/core/swap.c b/src/core/swap.c
be0c12
index 823699699e..90fcd69300 100644
be0c12
--- a/src/core/swap.c
be0c12
+++ b/src/core/swap.c
be0c12
@@ -851,12 +851,6 @@ static int swap_start(Unit *u) {
be0c12
                 if (UNIT(other)->job && UNIT(other)->job->state == JOB_RUNNING)
be0c12
                         return -EAGAIN;
be0c12
 
be0c12
-        r = unit_test_start_limit(u);
be0c12
-        if (r < 0) {
be0c12
-                swap_enter_dead(s, SWAP_FAILURE_START_LIMIT_HIT);
be0c12
-                return r;
be0c12
-        }
be0c12
-
be0c12
         r = unit_acquire_invocation_id(u);
be0c12
         if (r < 0)
be0c12
                 return r;
be0c12
@@ -1458,6 +1452,21 @@ static int swap_control_pid(Unit *u) {
be0c12
         return s->control_pid;
be0c12
 }
be0c12
 
be0c12
+static int swap_test_start_limit(Unit *u) {
be0c12
+        Swap *s = SWAP(u);
be0c12
+        int r;
be0c12
+
be0c12
+        assert(s);
be0c12
+
be0c12
+        r = unit_test_start_limit(u);
be0c12
+        if (r < 0) {
be0c12
+                swap_enter_dead(s, SWAP_FAILURE_START_LIMIT_HIT);
be0c12
+                return r;
be0c12
+        }
be0c12
+
be0c12
+        return 0;
be0c12
+}
be0c12
+
be0c12
 static const char* const swap_exec_command_table[_SWAP_EXEC_COMMAND_MAX] = {
be0c12
         [SWAP_EXEC_ACTIVATE] = "ExecActivate",
be0c12
         [SWAP_EXEC_DEACTIVATE] = "ExecDeactivate",
be0c12
@@ -1547,4 +1556,6 @@ const UnitVTable swap_vtable = {
be0c12
                         [JOB_TIMEOUT]    = "Timed out deactivating swap %s.",
be0c12
                 },
be0c12
         },
be0c12
+
be0c12
+        .test_start_limit = swap_test_start_limit,
be0c12
 };
be0c12
diff --git a/src/core/timer.c b/src/core/timer.c
be0c12
index be16321296..fb9ae17990 100644
be0c12
--- a/src/core/timer.c
be0c12
+++ b/src/core/timer.c
be0c12
@@ -599,12 +599,6 @@ static int timer_start(Unit *u) {
be0c12
                 return -ENOENT;
be0c12
         }
be0c12
 
be0c12
-        r = unit_test_start_limit(u);
be0c12
-        if (r < 0) {
be0c12
-                timer_enter_dead(t, TIMER_FAILURE_START_LIMIT_HIT);
be0c12
-                return r;
be0c12
-        }
be0c12
-
be0c12
         r = unit_acquire_invocation_id(u);
be0c12
         if (r < 0)
be0c12
                 return r;
be0c12
@@ -829,6 +823,21 @@ static void timer_timezone_change(Unit *u) {
be0c12
         timer_enter_waiting(t, false);
be0c12
 }
be0c12
 
be0c12
+static int timer_test_start_limit(Unit *u) {
be0c12
+        Timer *t = TIMER(u);
be0c12
+        int r;
be0c12
+
be0c12
+        assert(t);
be0c12
+
be0c12
+        r = unit_test_start_limit(u);
be0c12
+        if (r < 0) {
be0c12
+                timer_enter_dead(t, TIMER_FAILURE_START_LIMIT_HIT);
be0c12
+                return r;
be0c12
+        }
be0c12
+
be0c12
+        return 0;
be0c12
+}
be0c12
+
be0c12
 static const char* const timer_base_table[_TIMER_BASE_MAX] = {
be0c12
         [TIMER_ACTIVE] = "OnActiveSec",
be0c12
         [TIMER_BOOT] = "OnBootSec",
be0c12
@@ -884,4 +893,5 @@ const UnitVTable timer_vtable = {
be0c12
         .bus_set_property = bus_timer_set_property,
be0c12
 
be0c12
         .can_transient = true,
be0c12
+        .test_start_limit = timer_test_start_limit,
be0c12
 };
be0c12
diff --git a/src/core/unit.c b/src/core/unit.c
be0c12
index 9013186d8a..f0df7452fa 100644
be0c12
--- a/src/core/unit.c
be0c12
+++ b/src/core/unit.c
be0c12
@@ -1728,10 +1728,16 @@ int unit_start(Unit *u) {
be0c12
 
be0c12
         assert(u);
be0c12
 
be0c12
-        /* If this is already started, then this will succeed. Note
be0c12
-         * that this will even succeed if this unit is not startable
be0c12
-         * by the user. This is relied on to detect when we need to
be0c12
-         * wait for units and when waiting is finished. */
be0c12
+        /* Check start rate limiting early so that failure conditions don't cause us to enter a busy loop. */
be0c12
+        if (UNIT_VTABLE(u)->test_start_limit) {
be0c12
+                int r = UNIT_VTABLE(u)->test_start_limit(u);
be0c12
+                if (r < 0)
be0c12
+                        return r;
be0c12
+        }
be0c12
+
be0c12
+        /* If this is already started, then this will succeed. Note that this will even succeed if this unit
be0c12
+         * is not startable by the user. This is relied on to detect when we need to wait for units and when
be0c12
+         * waiting is finished. */
be0c12
         state = unit_active_state(u);
be0c12
         if (UNIT_IS_ACTIVE_OR_RELOADING(state))
be0c12
                 return -EALREADY;
be0c12
diff --git a/src/core/unit.h b/src/core/unit.h
be0c12
index a8bc350b66..9e6f1bcf81 100644
be0c12
--- a/src/core/unit.h
be0c12
+++ b/src/core/unit.h
be0c12
@@ -567,6 +567,10 @@ typedef struct UnitVTable {
be0c12
         /* The bus vtable */
be0c12
         const sd_bus_vtable *bus_vtable;
be0c12
 
be0c12
+        /* If this function is set, it's invoked first as part of starting a unit to allow start rate
be0c12
+         * limiting checks to occur before we do anything else. */
be0c12
+        int (*test_start_limit)(Unit *u);
be0c12
+
be0c12
         /* The strings to print in status messages */
be0c12
         UnitStatusMessageFormats status_message_formats;
be0c12