core: port over unit_kill_context() to PidRef

This commit is contained in:
Lennart Poettering 2023-09-10 15:12:59 +02:00
parent b1f6901d30
commit 7901288ab1
8 changed files with 38 additions and 35 deletions

1
TODO
View file

@ -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()

View file

@ -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;

View file

@ -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;
}

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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);