ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
99f2d6
From a246b1c63f082565c4492d8bdd945623863df07d Mon Sep 17 00:00:00 2001
99f2d6
From: Ryan Wilson <ryantimwilson@meta.com>
99f2d6
Date: Tue, 29 Oct 2024 10:25:53 -0700
99f2d6
Subject: [PATCH] core: Add debug logging for systemd killing services/units
99f2d6
99f2d6
---
99f2d6
 src/basic/pidref.c |  2 ++
99f2d6
 src/core/service.c |  3 +++
99f2d6
 src/core/unit.c    | 12 ++++++++++++
99f2d6
 src/core/unit.h    |  3 +++
99f2d6
 4 files changed, 20 insertions(+)
99f2d6
99f2d6
diff --git a/src/basic/pidref.c b/src/basic/pidref.c
99f2d6
index 69a010210d..6bd7198d0d 100644
99f2d6
--- a/src/basic/pidref.c
99f2d6
+++ b/src/basic/pidref.c
99f2d6
@@ -268,6 +268,8 @@ int pidref_kill(const PidRef *pidref, int sig) {
99f2d6
         if (!pidref)
99f2d6
                 return -ESRCH;
99f2d6
 
99f2d6
+        log_debug("Sending signal %s for PID " PID_FMT " and PIDFD %d", signal_to_string(sig), pidref->pid, pidref->fd);
99f2d6
+
99f2d6
         if (pidref->fd >= 0)
99f2d6
                 return RET_NERRNO(pidfd_send_signal(pidref->fd, sig, NULL, 0));
99f2d6
 
99f2d6
diff --git a/src/core/service.c b/src/core/service.c
99f2d6
index 6e81460ad0..577f1f067d 100644
99f2d6
--- a/src/core/service.c
99f2d6
+++ b/src/core/service.c
99f2d6
@@ -2138,6 +2138,9 @@ static void service_enter_signal(Service *s, ServiceState state, ServiceResult f
99f2d6
          * died now */
99f2d6
         (void) unit_enqueue_rewatch_pids(UNIT(s));
99f2d6
 
99f2d6
+        log_unit_debug(UNIT(s), "Sending signal to unit for state %s with result %s",
99f2d6
+                       service_state_to_string(state), service_result_to_string(f));
99f2d6
+
99f2d6
         kill_operation = state_to_kill_operation(s, state);
99f2d6
         r = unit_kill_context(UNIT(s), kill_operation);
99f2d6
         if (r < 0) {
99f2d6
diff --git a/src/core/unit.c b/src/core/unit.c
99f2d6
index 136b7aacb0..3a47331985 100644
99f2d6
--- a/src/core/unit.c
99f2d6
+++ b/src/core/unit.c
99f2d6
@@ -4796,6 +4796,16 @@ static int unit_kill_context_one(
99f2d6
         return !is_alien;
99f2d6
 }
99f2d6
 
99f2d6
+static const char* const kill_operation_table[_KILL_OPERATION_MAX] = {
99f2d6
+        [KILL_TERMINATE]         = "terminate",
99f2d6
+        [KILL_TERMINATE_AND_LOG] = "terminate-and-log",
99f2d6
+        [KILL_RESTART]           = "restart",
99f2d6
+        [KILL_KILL]              = "kill",
99f2d6
+        [KILL_WATCHDOG]          = "watchdog",
99f2d6
+};
99f2d6
+
99f2d6
+DEFINE_STRING_TABLE_LOOKUP(kill_operation, KillOperation);
99f2d6
+
99f2d6
 int unit_kill_context(Unit *u, KillOperation k) {
99f2d6
         bool wait_for_exit = false, send_sighup;
99f2d6
         cg_kill_log_func_t log_func = NULL;
99f2d6
@@ -4807,6 +4817,8 @@ int unit_kill_context(Unit *u, KillOperation k) {
99f2d6
          * if we killed something worth waiting for, 0 otherwise. Do not confuse with unit_kill_common()
99f2d6
          * which is used for user-requested killing of unit processes. */
99f2d6
 
99f2d6
+        log_unit_debug(u, "Killing unit context with operation %s", kill_operation_to_string(k));
99f2d6
+
99f2d6
         KillContext *c = unit_get_kill_context(u);
99f2d6
         if (!c || c->kill_mode == KILL_NONE)
99f2d6
                 return 0;
99f2d6
diff --git a/src/core/unit.h b/src/core/unit.h
99f2d6
index b135fecc51..e46dfb7c58 100644
99f2d6
--- a/src/core/unit.h
99f2d6
+++ b/src/core/unit.h
99f2d6
@@ -1053,6 +1053,9 @@ UnitMountDependencyType unit_mount_dependency_type_from_string(const char *s) _c
99f2d6
 const char* unit_mount_dependency_type_to_string(UnitMountDependencyType t) _const_;
99f2d6
 UnitDependency unit_mount_dependency_type_to_dependency_type(UnitMountDependencyType t) _pure_;
99f2d6
 
99f2d6
+const char* kill_operation_to_string(KillOperation t) _const_;
99f2d6
+KillOperation kill_operation_from_string(const char *s) _pure_;
99f2d6
+
99f2d6
 /* Macros which append UNIT= or USER_UNIT= to the message */
99f2d6
 
99f2d6
 #define log_unit_full_errno_zerook(unit, level, error, ...)             \
99f2d6
-- 
99f2d6
2.43.5
99f2d6