1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-20 18:04:03 +03:00

core: use helper functions like unit_main_pid() in unit_kill_context()

No functional changes. Just refactoring.
This commit is contained in:
Yu Watanabe 2024-01-24 14:02:46 +09:00
parent 3e22239da7
commit b826e31754
7 changed files with 13 additions and 45 deletions

View File

@ -1033,13 +1033,7 @@ static void mount_enter_signal(Mount *m, MountState state, MountResult f) {
if (m->result == MOUNT_SUCCESS)
m->result = f;
r = unit_kill_context(
UNIT(m),
&m->kill_context,
state_to_kill_operation(state),
/* main_pid= */ NULL,
&m->control_pid,
/* main_pid_alien= */ false);
r = unit_kill_context(UNIT(m), state_to_kill_operation(state));
if (r < 0) {
log_unit_warning_errno(UNIT(m), r, "Failed to kill processes: %m");
goto fail;

View File

@ -317,13 +317,9 @@ static void scope_enter_signal(Scope *s, ScopeState state, ScopeResult f) {
else {
r = unit_kill_context(
UNIT(s),
&s->kill_context,
state != SCOPE_STOP_SIGTERM ? KILL_KILL :
s->was_abandoned ? KILL_TERMINATE_AND_LOG :
KILL_TERMINATE,
/* main_pid= */ NULL,
/* control_pid= */ NULL,
/* main_pid_alien= */ false);
KILL_TERMINATE);
if (r < 0) {
log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m");
goto fail;

View File

@ -2121,13 +2121,7 @@ static void service_enter_signal(Service *s, ServiceState state, ServiceResult f
(void) unit_enqueue_rewatch_pids(UNIT(s));
kill_operation = state_to_kill_operation(s, state);
r = unit_kill_context(
UNIT(s),
&s->kill_context,
kill_operation,
&s->main_pid,
&s->control_pid,
s->main_pid_alien);
r = unit_kill_context(UNIT(s), kill_operation);
if (r < 0) {
log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m");
goto fail;

View File

@ -2070,13 +2070,7 @@ static void socket_enter_signal(Socket *s, SocketState state, SocketResult f) {
if (s->result == SOCKET_SUCCESS)
s->result = f;
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);
r = unit_kill_context(UNIT(s), state_to_kill_operation(s, state));
if (r < 0) {
log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m");
goto fail;

View File

@ -734,13 +734,7 @@ 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),
/* main_pid= */ NULL,
&s->control_pid,
/* main_pid_alien= */ false);
r = unit_kill_context(UNIT(s), state_to_kill_operation(s, state));
if (r < 0) {
log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m");
goto fail;

View File

@ -4749,26 +4749,19 @@ static int operation_to_signal(
}
}
int unit_kill_context(
Unit *u,
KillContext *c,
KillOperation k,
PidRef* main_pid,
PidRef* control_pid,
bool main_pid_alien) {
int unit_kill_context(Unit *u, KillOperation k) {
bool wait_for_exit = false, send_sighup;
cg_kill_log_func_t log_func = NULL;
int sig, r;
assert(u);
assert(c);
/* Kill the processes belonging to this unit, in preparation for shutting the unit down. Returns > 0
* if we killed something worth waiting for, 0 otherwise. Do not confuse with unit_kill_common()
* which is used for user-requested killing of unit processes. */
if (c->kill_mode == KILL_NONE)
KillContext *c = unit_get_kill_context(u);
if (!c || c->kill_mode == KILL_NONE)
return 0;
bool noteworthy;
@ -4781,6 +4774,8 @@ int unit_kill_context(
IN_SET(k, KILL_TERMINATE, KILL_TERMINATE_AND_LOG) &&
sig != SIGHUP;
bool is_alien;
PidRef *main_pid = unit_main_pid_full(u, &is_alien);
if (pidref_is_set(main_pid)) {
if (log_func)
log_func(main_pid, sig, u);
@ -4792,7 +4787,7 @@ int unit_kill_context(
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)
if (!is_alien)
wait_for_exit = true;
if (r != -ESRCH && send_sighup)
@ -4800,6 +4795,7 @@ int unit_kill_context(
}
}
PidRef *control_pid = unit_control_pid(u);
if (pidref_is_set(control_pid)) {
if (log_func)
log_func(control_pid, sig, u);

View File

@ -1006,7 +1006,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, PidRef *main_pid, PidRef *control_pid, bool main_pid_alien);
int unit_kill_context(Unit *u, KillOperation k);
int unit_make_transient(Unit *u);