1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-31 05:47:30 +03:00

core: convert Socket logic to PidRef too

Just like the previous commit, but for socket rather than service units.
This commit is contained in:
Lennart Poettering 2023-09-09 09:55:50 +02:00
parent c79ab77cd3
commit 43a19142cb
2 changed files with 56 additions and 30 deletions

View File

@ -97,6 +97,7 @@ static void socket_init(Unit *u) {
s->exec_context.std_output = u->manager->default_std_output;
s->exec_context.std_error = u->manager->default_std_error;
s->control_pid = PIDREF_NULL;
s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID;
s->trigger_limit.interval = USEC_INFINITY;
@ -106,10 +107,11 @@ static void socket_init(Unit *u) {
static void socket_unwatch_control_pid(Socket *s) {
assert(s);
if (s->control_pid <= 0)
if (!pidref_is_set(&s->control_pid))
return;
unit_unwatch_pid(UNIT(s), TAKE_PID(s->control_pid));
unit_unwatch_pid(UNIT(s), s->control_pid.pid);
pidref_done(&s->control_pid);
}
static void socket_cleanup_fd_list(SocketPort *p) {
@ -616,10 +618,10 @@ static void socket_dump(Unit *u, FILE *f, const char *prefix) {
"%sTimestamping: %s\n",
prefix, socket_timestamping_to_string(s->timestamping));
if (s->control_pid > 0)
if (pidref_is_set(&s->control_pid))
fprintf(f,
"%sControl PID: "PID_FMT"\n",
prefix, s->control_pid);
prefix, s->control_pid.pid);
if (s->bind_to_device)
fprintf(f,
@ -1854,8 +1856,8 @@ static int socket_coldplug(Unit *u) {
if (s->deserialized_state == s->state)
return 0;
if (s->control_pid > 0 &&
pid_is_unwaited(s->control_pid) &&
if (pidref_is_set(&s->control_pid) &&
pid_is_unwaited(s->control_pid.pid) &&
IN_SET(s->deserialized_state,
SOCKET_START_PRE,
SOCKET_START_CHOWN,
@ -1868,7 +1870,7 @@ static int socket_coldplug(Unit *u) {
SOCKET_FINAL_SIGKILL,
SOCKET_CLEANING)) {
r = unit_watch_pid(UNIT(s), s->control_pid, false);
r = unit_watch_pid(UNIT(s), s->control_pid.pid, /* exclusive= */ false);
if (r < 0)
return r;
@ -1914,7 +1916,7 @@ static int socket_coldplug(Unit *u) {
return 0;
}
static int socket_spawn(Socket *s, ExecCommand *c, pid_t *_pid) {
static int socket_spawn(Socket *s, ExecCommand *c, PidRef *ret_pid) {
_cleanup_(exec_params_clear) ExecParameters exec_params = {
.flags = EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN,
@ -1923,12 +1925,13 @@ static int socket_spawn(Socket *s, ExecCommand *c, pid_t *_pid) {
.stderr_fd = -EBADF,
.exec_fd = -EBADF,
};
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
pid_t pid;
int r;
assert(s);
assert(c);
assert(_pid);
assert(ret_pid);
r = unit_prepare_exec(UNIT(s));
if (r < 0)
@ -1952,19 +1955,25 @@ static int socket_spawn(Socket *s, ExecCommand *c, pid_t *_pid) {
if (r < 0)
return r;
r = unit_watch_pid(UNIT(s), pid, true);
r = pidref_set_pid(&pidref, pid);
if (r < 0)
return r;
*_pid = pid;
r = unit_watch_pid(UNIT(s), pidref.pid, /* exclusive= */ true);
if (r < 0)
return r;
*ret_pid = TAKE_PIDREF(pidref);
return 0;
}
static int socket_chown(Socket *s, pid_t *_pid) {
static int socket_chown(Socket *s, PidRef *ret_pid) {
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
pid_t pid;
int r;
assert(s);
r = socket_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_usec));
if (r < 0)
goto fail;
@ -2021,11 +2030,15 @@ static int socket_chown(Socket *s, pid_t *_pid) {
_exit(EXIT_SUCCESS);
}
r = unit_watch_pid(UNIT(s), pid, true);
r = pidref_set_pid(&pidref, pid);
if (r < 0)
goto fail;
*_pid = pid;
r = unit_watch_pid(UNIT(s), pidref.pid, /* exclusive= */ true);
if (r < 0)
goto fail;
*ret_pid = TAKE_PIDREF(pidref);
return 0;
fail:
@ -2069,6 +2082,8 @@ static void socket_enter_stop_post(Socket *s, SocketResult f) {
s->control_command = s->exec_command[SOCKET_EXEC_STOP_POST];
if (s->control_command) {
pidref_done(&s->control_pid);
r = socket_spawn(s, s->control_command, &s->control_pid);
if (r < 0)
goto fail;
@ -2107,7 +2122,7 @@ static void socket_enter_signal(Socket *s, SocketState state, SocketResult f) {
&s->kill_context,
state_to_kill_operation(s, state),
-1,
s->control_pid,
s->control_pid.pid,
false);
if (r < 0)
goto fail;
@ -2150,6 +2165,8 @@ static void socket_enter_stop_pre(Socket *s, SocketResult f) {
s->control_command = s->exec_command[SOCKET_EXEC_STOP_PRE];
if (s->control_command) {
pidref_done(&s->control_pid);
r = socket_spawn(s, s->control_command, &s->control_pid);
if (r < 0)
goto fail;
@ -2196,6 +2213,8 @@ static void socket_enter_start_post(Socket *s) {
s->control_command = s->exec_command[SOCKET_EXEC_START_POST];
if (s->control_command) {
pidref_done(&s->control_pid);
r = socket_spawn(s, s->control_command, &s->control_pid);
if (r < 0) {
log_unit_warning_errno(UNIT(s), r, "Failed to run 'start-post' task: %m");
@ -2257,6 +2276,8 @@ static void socket_enter_start_pre(Socket *s) {
s->control_command = s->exec_command[SOCKET_EXEC_START_PRE];
if (s->control_command) {
pidref_done(&s->control_pid);
r = socket_spawn(s, s->control_command, &s->control_pid);
if (r < 0) {
log_unit_warning_errno(UNIT(s), r, "Failed to run 'start-pre' task: %m");
@ -2436,6 +2457,8 @@ static void socket_run_next(Socket *s) {
s->control_command = s->control_command->command_next;
pidref_done(&s->control_pid);
r = socket_spawn(s, s->control_command, &s->control_pid);
if (r < 0)
goto fail;
@ -2562,8 +2585,8 @@ static int socket_serialize(Unit *u, FILE *f, FDSet *fds) {
(void) serialize_item_format(f, "n-accepted", "%u", s->n_accepted);
(void) serialize_item_format(f, "n-refused", "%u", s->n_refused);
if (s->control_pid > 0)
(void) serialize_item_format(f, "control-pid", PID_FMT, s->control_pid);
if (pidref_is_set(&s->control_pid))
(void) serialize_item_format(f, "control-pid", PID_FMT, s->control_pid.pid);
if (s->control_command_id >= 0)
(void) serialize_item(f, "control-command", socket_exec_command_to_string(s->control_command_id));
@ -2644,12 +2667,10 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value,
else
s->n_refused += k;
} else if (streq(key, "control-pid")) {
pid_t pid;
if (parse_pid(value, &pid) < 0)
log_unit_debug(u, "Failed to parse control-pid value: %s", value);
else
s->control_pid = pid;
pidref_done(&s->control_pid);
r = pidref_set_pidstr(&s->control_pid, value);
if (r < 0)
log_debug_errno(r, "Failed to pin control PID '%s', ignoring: %m", value);
} else if (streq(key, "control-command")) {
SocketExecCommand id;
@ -3089,10 +3110,10 @@ static void socket_sigchld_event(Unit *u, pid_t pid, int code, int status) {
assert(s);
assert(pid >= 0);
if (pid != s->control_pid)
if (pid != s->control_pid.pid)
return;
s->control_pid = 0;
pidref_done(&s->control_pid);
if (is_clean_exit(code, status, EXIT_CLEAN_COMMAND, NULL))
f = SOCKET_SUCCESS;
@ -3366,7 +3387,7 @@ static void socket_trigger_notify(Unit *u, Unit *other) {
}
static int socket_kill(Unit *u, KillWho who, int signo, int code, int value, sd_bus_error *error) {
return unit_kill_common(u, who, signo, code, value, -1, SOCKET(u)->control_pid, error);
return unit_kill_common(u, who, signo, code, value, -1, SOCKET(u)->control_pid.pid, error);
}
static int socket_get_timeout(Unit *u, usec_t *timeout) {
@ -3402,12 +3423,13 @@ static int socket_control_pid(Unit *u) {
assert(s);
return s->control_pid;
return s->control_pid.pid;
}
static int socket_clean(Unit *u, ExecCleanMask mask) {
_cleanup_strv_free_ char **l = NULL;
Socket *s = SOCKET(u);
pid_t pid;
int r;
assert(s);
@ -3432,12 +3454,15 @@ static int socket_clean(Unit *u, ExecCleanMask mask) {
if (r < 0)
goto fail;
r = unit_fork_and_watch_rm_rf(u, l, &s->control_pid);
r = unit_fork_and_watch_rm_rf(u, l, &pid);
if (r < 0)
goto fail;
r = pidref_set_pid(&s->control_pid, pid);
if (r < 0)
goto fail;
socket_set_state(s, SOCKET_CLEANING);
return 0;
fail:

View File

@ -5,6 +5,7 @@ typedef struct Socket Socket;
typedef struct SocketPeer SocketPeer;
#include "mount.h"
#include "pidref.h"
#include "socket-util.h"
#include "unit.h"
@ -103,7 +104,7 @@ struct Socket {
ExecCommand* control_command;
SocketExecCommand control_command_id;
pid_t control_pid;
PidRef control_pid;
mode_t directory_mode;
mode_t socket_mode;