From 7901288ab18c9ffc296199064daf616d472e5796 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sun, 10 Sep 2023 15:12:59 +0200 Subject: [PATCH] core: port over unit_kill_context() to PidRef --- TODO | 1 - src/core/mount.c | 6 +++--- src/core/scope.c | 4 +++- src/core/service.c | 4 ++-- src/core/socket.c | 6 +++--- src/core/swap.c | 13 +++++++------ src/core/unit.c | 32 ++++++++++++++++---------------- src/core/unit.h | 7 ++++--- 8 files changed, 38 insertions(+), 35 deletions(-) diff --git a/TODO b/TODO index 1d7da220588..c75d65e0e46 100644 --- a/TODO +++ b/TODO @@ -175,7 +175,6 @@ Features: - pid_is_alive() → pidref_is_alive() - unit_watch_pid() → unit_watch_pidref() - unit_kill_common() - - unit_kill_context() - actually wait for POLLIN on piref's pidfd in service logic - unit_main_pid() + unit_control_pid() - exec_spawn() diff --git a/src/core/mount.c b/src/core/mount.c index 57045f3da4c..c156a84c1d3 100644 --- a/src/core/mount.c +++ b/src/core/mount.c @@ -1036,9 +1036,9 @@ static void mount_enter_signal(Mount *m, MountState state, MountResult f) { UNIT(m), &m->kill_context, state_to_kill_operation(state), - -1, - m->control_pid.pid, - false); + /* main_pid= */ NULL, + &m->control_pid, + /* main_pid_alien= */ false); if (r < 0) goto fail; diff --git a/src/core/scope.c b/src/core/scope.c index a13c8ddf7a4..31ea5c54832 100644 --- a/src/core/scope.c +++ b/src/core/scope.c @@ -345,7 +345,9 @@ static void scope_enter_signal(Scope *s, ScopeState state, ScopeResult f) { state != SCOPE_STOP_SIGTERM ? KILL_KILL : s->was_abandoned ? KILL_TERMINATE_AND_LOG : KILL_TERMINATE, - -1, -1, false); + /* main_pid= */ NULL, + /* control_pid= */ NULL, + /* main_pid_alien= */ false); if (r < 0) goto fail; } diff --git a/src/core/service.c b/src/core/service.c index a088af51964..34948fa9234 100644 --- a/src/core/service.c +++ b/src/core/service.c @@ -2166,8 +2166,8 @@ static void service_enter_signal(Service *s, ServiceState state, ServiceResult f UNIT(s), &s->kill_context, kill_operation, - s->main_pid.pid, - s->control_pid.pid, + &s->main_pid, + &s->control_pid, s->main_pid_alien); if (r < 0) goto fail; diff --git a/src/core/socket.c b/src/core/socket.c index 9e7809721af..d885581247c 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -2117,9 +2117,9 @@ static void socket_enter_signal(Socket *s, SocketState state, SocketResult f) { UNIT(s), &s->kill_context, state_to_kill_operation(s, state), - -1, - s->control_pid.pid, - false); + /* main_pid= */ NULL, + &s->control_pid, + /* main_pid_alien= */ false); if (r < 0) goto fail; diff --git a/src/core/swap.c b/src/core/swap.c index c6b5f3471a2..d8c08375761 100644 --- a/src/core/swap.c +++ b/src/core/swap.c @@ -763,12 +763,13 @@ static void swap_enter_signal(Swap *s, SwapState state, SwapResult f) { if (s->result == SWAP_SUCCESS) s->result = f; - r = unit_kill_context(UNIT(s), - &s->kill_context, - state_to_kill_operation(s, state), - -1, - s->control_pid.pid, - false); + r = unit_kill_context( + UNIT(s), + &s->kill_context, + state_to_kill_operation(s, state), + /* main_pid= */ NULL, + &s->control_pid, + /* main_pid_alien= */ false); if (r < 0) goto fail; diff --git a/src/core/unit.c b/src/core/unit.c index 6f13348ebf5..c542e4f5042 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -4775,8 +4775,8 @@ int unit_kill_context( Unit *u, KillContext *c, KillOperation k, - pid_t main_pid, - pid_t control_pid, + PidRef* main_pid, + PidRef* control_pid, bool main_pid_alien) { bool wait_for_exit = false, send_sighup; @@ -4803,40 +4803,40 @@ int unit_kill_context( IN_SET(k, KILL_TERMINATE, KILL_TERMINATE_AND_LOG) && sig != SIGHUP; - if (main_pid > 0) { + if (pidref_is_set(main_pid)) { if (log_func) - log_func(main_pid, sig, u); + log_func(main_pid->pid, sig, u); - r = kill_and_sigcont(main_pid, sig); + r = pidref_kill_and_sigcont(main_pid, sig); if (r < 0 && r != -ESRCH) { _cleanup_free_ char *comm = NULL; - (void) get_process_comm(main_pid, &comm); + (void) get_process_comm(main_pid->pid, &comm); - log_unit_warning_errno(u, r, "Failed to kill main process " PID_FMT " (%s), ignoring: %m", main_pid, strna(comm)); + log_unit_warning_errno(u, r, "Failed to kill main process " PID_FMT " (%s), ignoring: %m", main_pid->pid, strna(comm)); } else { if (!main_pid_alien) wait_for_exit = true; if (r != -ESRCH && send_sighup) - (void) kill(main_pid, SIGHUP); + (void) pidref_kill(main_pid, SIGHUP); } } - if (control_pid > 0) { + if (pidref_is_set(control_pid)) { if (log_func) - log_func(control_pid, sig, u); + log_func(control_pid->pid, sig, u); - r = kill_and_sigcont(control_pid, sig); + r = pidref_kill_and_sigcont(control_pid, sig); if (r < 0 && r != -ESRCH) { _cleanup_free_ char *comm = NULL; - (void) get_process_comm(control_pid, &comm); + (void) get_process_comm(control_pid->pid, &comm); - log_unit_warning_errno(u, r, "Failed to kill control process " PID_FMT " (%s), ignoring: %m", control_pid, strna(comm)); + log_unit_warning_errno(u, r, "Failed to kill control process " PID_FMT " (%s), ignoring: %m", control_pid->pid, strna(comm)); } else { wait_for_exit = true; if (r != -ESRCH && send_sighup) - (void) kill(control_pid, SIGHUP); + (void) pidref_kill(control_pid, SIGHUP); } } @@ -4845,7 +4845,7 @@ int unit_kill_context( _cleanup_set_free_ Set *pid_set = NULL; /* Exclude the main/control pids from being killed via the cgroup */ - pid_set = unit_pid_set(main_pid, control_pid); + pid_set = unit_pid_set(main_pid ? main_pid->pid : 0, control_pid ? control_pid->pid : 0); if (!pid_set) return -ENOMEM; @@ -4874,7 +4874,7 @@ int unit_kill_context( if (send_sighup) { set_free(pid_set); - pid_set = unit_pid_set(main_pid, control_pid); + pid_set = unit_pid_set(main_pid ? main_pid->pid : 0, control_pid ? control_pid->pid : 0); if (!pid_set) return -ENOMEM; diff --git a/src/core/unit.h b/src/core/unit.h index c0710299a54..7ce61eb5fd1 100644 --- a/src/core/unit.h +++ b/src/core/unit.h @@ -9,14 +9,15 @@ #include "sd-id128.h" #include "bpf-program.h" +#include "cgroup.h" #include "condition.h" #include "emergency-action.h" #include "install.h" #include "list.h" -#include "show-status.h" +#include "pidref.h" #include "set.h" +#include "show-status.h" #include "unit-file.h" -#include "cgroup.h" typedef struct UnitRef UnitRef; @@ -992,7 +993,7 @@ char* unit_concat_strv(char **l, UnitWriteFlags flags); int unit_write_setting(Unit *u, UnitWriteFlags flags, const char *name, const char *data); int unit_write_settingf(Unit *u, UnitWriteFlags mode, const char *name, const char *format, ...) _printf_(4,5); -int unit_kill_context(Unit *u, KillContext *c, KillOperation k, pid_t main_pid, pid_t control_pid, bool main_pid_alien); +int unit_kill_context(Unit *u, KillContext *c, KillOperation k, PidRef *main_pid, PidRef *control_pid, bool main_pid_alien); int unit_make_transient(Unit *u); -- 2.39.2