]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: port unit/process kill logic to pidref
authorLennart Poettering <lennart@poettering.net>
Tue, 17 Oct 2023 08:34:00 +0000 (10:34 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 18 Oct 2023 12:39:33 +0000 (14:39 +0200)
src/basic/cgroup-util.c
src/basic/cgroup-util.h
src/core/unit.c
src/core/unit.h
src/oom/oomd-util.c

index b9100c5a5cb456b5a9c799d75f1f8f79049e10e9..f38262ca11530e558fd45cbf33e952c1502c7d00 100644 (file)
@@ -291,7 +291,6 @@ static int cg_kill_items(
         _cleanup_set_free_ Set *allocated_set = NULL;
         bool done = false;
         int r, ret = 0, ret_log_kill = 0;
-        pid_t my_pid;
 
         assert(sig >= 0);
 
@@ -310,8 +309,6 @@ static int cg_kill_items(
                         return -ENOMEM;
         }
 
-        my_pid = getpid_cached();
-
         do {
                 _cleanup_fclose_ FILE *f = NULL;
                 pid_t pid = 0;
@@ -325,25 +322,32 @@ static int cg_kill_items(
                         return ret;
                 }
 
-                while ((r = cg_read_pid(f, &pid)) > 0) {
+                for (;;) {
+                        _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
+
+                        r = cg_read_pidref(f, &pidref);
+                        if (r < 0)
+                                return r;
+                        if (r == 0)
+                                break;
 
-                        if ((flags & CGROUP_IGNORE_SELF) && pid == my_pid)
+                        if ((flags & CGROUP_IGNORE_SELF) && pidref_is_self(&pidref))
                                 continue;
 
-                        if (set_get(s, PID_TO_PTR(pid)) == PID_TO_PTR(pid))
+                        if (set_get(s, PID_TO_PTR(pidref.pid)) == PID_TO_PTR(pidref.pid))
                                 continue;
 
                         if (log_kill)
-                                ret_log_kill = log_kill(pid, sig, userdata);
+                                ret_log_kill = log_kill(&pidref, sig, userdata);
 
-                        /* If we haven't killed this process yet, kill
-                         * it */
-                        if (kill(pid, sig) < 0) {
-                                if (ret >= 0 && errno != ESRCH)
-                                        ret = -errno;
+                        /* If we haven't killed this process yet, kill it */
+                        r = pidref_kill(&pidref, sig);
+                        if (r < 0) {
+                                if (ret >= 0 && r != -ESRCH)
+                                        ret = r;
                         } else {
                                 if (flags & CGROUP_SIGCONT)
-                                        (void) kill(pid, SIGCONT);
+                                        (void) pidref_kill(&pidref, SIGCONT);
 
                                 if (ret == 0) {
                                         if (log_kill)
index 1d268d8c7ec43dcb46399616384308526dbb9b40..1022caf23c958bb8443041530a9d3e093441913b 100644 (file)
@@ -191,7 +191,7 @@ typedef enum CGroupFlags {
         CGROUP_REMOVE      = 1 << 2,
 } CGroupFlags;
 
-typedef int (*cg_kill_log_func_t)(pid_t pid, int sig, void *userdata);
+typedef int (*cg_kill_log_func_t)(const PidRef *pid, int sig, void *userdata);
 
 int cg_kill(const char *path, int sig, CGroupFlags flags, Set *s, cg_kill_log_func_t kill_log, void *userdata);
 int cg_kill_kernel_sigkill(const char *path);
index 4300f18d9bb411c62f108f038d1ae986f18036be..da71e09f15685d2ea1fafd61e20088d5709e8e0b 100644 (file)
@@ -4006,13 +4006,14 @@ static Set *unit_pid_set(pid_t main_pid, pid_t control_pid) {
         return TAKE_PTR(pid_set);
 }
 
-static int kill_common_log(pid_t pid, int signo, void *userdata) {
+static int kill_common_log(const PidRef *pid, int signo, void *userdata) {
         _cleanup_free_ char *comm = NULL;
         Unit *u = ASSERT_PTR(userdata);
 
-        (void) pid_get_comm(pid, &comm);
+        (void) pidref_get_comm(pid, &comm);
+
         log_unit_info(u, "Sending signal SIG%s to process " PID_FMT " (%s) on client request.",
-                      signal_to_string(signo), pid, strna(comm));
+                      signal_to_string(signo), pid->pid, strna(comm));
 
         return 1;
 }
@@ -4105,7 +4106,6 @@ int unit_kill(
 
         if (pidref_is_set(main_pid) &&
             IN_SET(who, KILL_MAIN, KILL_MAIN_FAIL, KILL_ALL, KILL_ALL_FAIL)) {
-
                 _cleanup_free_ char *comm = NULL;
                 (void) pidref_get_comm(main_pid, &comm);
 
@@ -4729,10 +4729,12 @@ int unit_make_transient(Unit *u) {
         return 0;
 }
 
-static int log_kill(pid_t pid, int sig, void *userdata) {
+static int log_kill(const PidRef *pid, int sig, void *userdata) {
         _cleanup_free_ char *comm = NULL;
 
-        (void) pid_get_comm(pid, &comm);
+        assert(pidref_is_set(pid));
+
+        (void) pidref_get_comm(pid, &comm);
 
         /* Don't log about processes marked with brackets, under the assumption that these are temporary processes
            only, like for example systemd's own PAM stub process. */
@@ -4743,7 +4745,7 @@ static int log_kill(pid_t pid, int sig, void *userdata) {
 
         log_unit_notice(userdata,
                         "Killing process " PID_FMT " (%s) with signal SIG%s.",
-                        pid,
+                        pid->pid,
                         strna(comm),
                         signal_to_string(sig));
 
@@ -4815,7 +4817,7 @@ int unit_kill_context(
 
         if (pidref_is_set(main_pid)) {
                 if (log_func)
-                        log_func(main_pid->pid, sig, u);
+                        log_func(main_pid, sig, u);
 
                 r = pidref_kill_and_sigcont(main_pid, sig);
                 if (r < 0 && r != -ESRCH) {
@@ -4834,7 +4836,7 @@ int unit_kill_context(
 
         if (pidref_is_set(control_pid)) {
                 if (log_func)
-                        log_func(control_pid->pid, sig, u);
+                        log_func(control_pid, sig, u);
 
                 r = pidref_kill_and_sigcont(control_pid, sig);
                 if (r < 0 && r != -ESRCH) {
@@ -5862,10 +5864,12 @@ static bool ignore_leftover_process(const char *comm) {
         return comm && comm[0] == '('; /* Most likely our own helper process (PAM?), ignore */
 }
 
-int unit_log_leftover_process_start(pid_t pid, int sig, void *userdata) {
+int unit_log_leftover_process_start(const PidRef *pid, int sig, void *userdata) {
         _cleanup_free_ char *comm = NULL;
 
-        (void) pid_get_comm(pid, &comm);
+        assert(pidref_is_set(pid));
+
+        (void) pidref_get_comm(pid, &comm);
 
         if (ignore_leftover_process(comm))
                 return 0;
@@ -5875,15 +5879,17 @@ int unit_log_leftover_process_start(pid_t pid, int sig, void *userdata) {
         log_unit_warning(userdata,
                          "Found left-over process " PID_FMT " (%s) in control group while starting unit. Ignoring.\n"
                          "This usually indicates unclean termination of a previous run, or service implementation deficiencies.",
-                         pid, strna(comm));
+                         pid->pid, strna(comm));
 
         return 1;
 }
 
-int unit_log_leftover_process_stop(pid_t pid, int sig, void *userdata) {
+int unit_log_leftover_process_stop(const PidRef *pid, int sig, void *userdata) {
         _cleanup_free_ char *comm = NULL;
 
-        (void) pid_get_comm(pid, &comm);
+        assert(pidref_is_set(pid));
+
+        (void) pidref_get_comm(pid, &comm);
 
         if (ignore_leftover_process(comm))
                 return 0;
@@ -5892,7 +5898,7 @@ int unit_log_leftover_process_stop(pid_t pid, int sig, void *userdata) {
 
         log_unit_info(userdata,
                       "Unit process " PID_FMT " (%s) remains running after unit stopped.",
-                      pid, strna(comm));
+                      pid->pid, strna(comm));
 
         return 1;
 }
index b5d7eb5f26126145cd35f1deaafac70bc1eec191..2427e2e765b6ede5f63186f4c599ac87b5a34f63 100644 (file)
@@ -1036,8 +1036,9 @@ void unit_unlink_state_files(Unit *u);
 
 int unit_prepare_exec(Unit *u);
 
-int unit_log_leftover_process_start(pid_t pid, int sig, void *userdata);
-int unit_log_leftover_process_stop(pid_t pid, int sig, void *userdata);
+int unit_log_leftover_process_start(const PidRef* pid, int sig, void *userdata);
+int unit_log_leftover_process_stop(const PidRef* pid, int sig, void *userdata);
+
 int unit_warn_leftover_processes(Unit *u, cg_kill_log_func_t log_func);
 
 bool unit_needs_console(Unit *u);
index 99e1de182f73e31f1df02b944fcd6b301ee759f7..f9f0af2d0499b790fd1bc04d3803cfe98087991b 100644 (file)
@@ -26,8 +26,8 @@ DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
                 OomdCGroupContext,
                 oomd_cgroup_context_free);
 
-static int log_kill(pid_t pid, int sig, void *userdata) {
-        log_debug("oomd attempting to kill " PID_FMT " with %s", pid, signal_to_string(sig));
+static int log_kill(const PidRef *pid, int sig, void *userdata) {
+        log_debug("oomd attempting to kill " PID_FMT " with %s", pid->pid, signal_to_string(sig));
         return 0;
 }