From 4775b55db0065463282922c3560e6e391de93501 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sun, 10 Sep 2023 16:41:43 +0200 Subject: [PATCH] core: port unit_fork_helper_process() and unit_fork_and_watch_rm_rf() to PidRef --- TODO | 3 +-- src/core/mount.c | 7 +------ src/core/scope.c | 6 +++--- src/core/service.c | 7 +------ src/core/socket.c | 26 ++++++++------------------ src/core/swap.c | 7 +------ src/core/unit.c | 30 +++++++++++++++++++++++------- src/core/unit.h | 4 ++-- 8 files changed, 40 insertions(+), 50 deletions(-) diff --git a/TODO b/TODO index bb126377982..349e31f1f75 100644 --- a/TODO +++ b/TODO @@ -174,10 +174,9 @@ Features: - pid_is_unwaited() → pidref_is_unwaited() - pid_is_alive() → pidref_is_alive() - unit_watch_pid() → unit_watch_pidref() - - actually wait for POLLIN on piref's pidfd in service logic + - actually wait for POLLIN on pidref's pidfd in service logic - exec_spawn() - serialization of control/main pid in service, socket, mount, swap units - - unit_fork_and_watch_rm_rf() - cg_pid_get_unit() - openpt_allocate_in_namespace() - scope dbus PIDs property needs to gain PIDFDs companion diff --git a/src/core/mount.c b/src/core/mount.c index 4af2e6810e1..08c2ddf9156 100644 --- a/src/core/mount.c +++ b/src/core/mount.c @@ -2220,7 +2220,6 @@ static PidRef* mount_control_pid(Unit *u) { static int mount_clean(Unit *u, ExecCleanMask mask) { _cleanup_strv_free_ char **l = NULL; Mount *m = MOUNT(u); - pid_t pid; int r; assert(m); @@ -2245,11 +2244,7 @@ static int mount_clean(Unit *u, ExecCleanMask mask) { if (r < 0) goto fail; - r = unit_fork_and_watch_rm_rf(u, l, &pid); - if (r < 0) - goto fail; - - r = pidref_set_pid(&m->control_pid, pid); + r = unit_fork_and_watch_rm_rf(u, l, &m->control_pid); if (r < 0) goto fail; diff --git a/src/core/scope.c b/src/core/scope.c index 13907855c50..eb3ffba084d 100644 --- a/src/core/scope.c +++ b/src/core/scope.c @@ -372,8 +372,8 @@ fail: } static int scope_enter_start_chown(Scope *s) { + _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; Unit *u = UNIT(s); - pid_t pid; int r; assert(s); @@ -383,7 +383,7 @@ static int scope_enter_start_chown(Scope *s) { if (r < 0) return r; - r = unit_fork_helper_process(u, "(sd-chown-cgroup)", &pid); + r = unit_fork_helper_process(u, "(sd-chown-cgroup)", &pidref); if (r < 0) goto fail; @@ -420,7 +420,7 @@ static int scope_enter_start_chown(Scope *s) { _exit(EXIT_SUCCESS); } - r = unit_watch_pid(UNIT(s), pid, true); + r = unit_watch_pid(UNIT(s), pidref.pid, /* exclusive= */ true); if (r < 0) goto fail; diff --git a/src/core/service.c b/src/core/service.c index 0ecfe46cfdf..05a2bb6ddf1 100644 --- a/src/core/service.c +++ b/src/core/service.c @@ -4890,7 +4890,6 @@ static int service_clean(Unit *u, ExecCleanMask mask) { _cleanup_strv_free_ char **l = NULL; bool may_clean_fdstore = false; Service *s = SERVICE(u); - pid_t pid; int r; assert(s); @@ -4931,11 +4930,7 @@ static int service_clean(Unit *u, ExecCleanMask mask) { if (r < 0) goto fail; - r = unit_fork_and_watch_rm_rf(u, l, &pid); - if (r < 0) - goto fail; - - r = pidref_set_pid(&s->control_pid, pid); + r = unit_fork_and_watch_rm_rf(u, l, &s->control_pid); if (r < 0) goto fail; diff --git a/src/core/socket.c b/src/core/socket.c index 861e580b3fe..3304af831db 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -1503,9 +1503,9 @@ static int socket_address_listen_in_cgroup( const SocketAddress *address, const char *label) { + _cleanup_(pidref_done) PidRef pid = PIDREF_NULL; _cleanup_close_pair_ int pair[2] = PIPE_EBADF; int fd, r; - pid_t pid; assert(s); assert(address); @@ -1597,7 +1597,7 @@ static int socket_address_listen_in_cgroup( fd = receive_one_fd(pair[0], 0); /* We synchronously wait for the helper, as it shouldn't be slow */ - r = wait_for_terminate_and_check("(sd-listen)", pid, WAIT_LOG_ABNORMAL); + r = wait_for_terminate_and_check("(sd-listen)", pid.pid, WAIT_LOG_ABNORMAL); if (r < 0) { safe_close(fd); return r; @@ -1968,8 +1968,7 @@ static int socket_spawn(Socket *s, ExecCommand *c, PidRef *ret_pid) { } static int socket_chown(Socket *s, PidRef *ret_pid) { - _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; - pid_t pid; + _cleanup_(pidref_done) PidRef pid = PIDREF_NULL; int r; assert(s); @@ -2030,15 +2029,11 @@ static int socket_chown(Socket *s, PidRef *ret_pid) { _exit(EXIT_SUCCESS); } - r = pidref_set_pid(&pidref, pid); - if (r < 0) - return r; - - r = unit_watch_pid(UNIT(s), pidref.pid, /* exclusive= */ true); + r = unit_watch_pid(UNIT(s), pid.pid, /* exclusive= */ true); if (r < 0) return r; - *ret_pid = TAKE_PIDREF(pidref); + *ret_pid = TAKE_PIDREF(pid); return 0; } @@ -2982,9 +2977,9 @@ static int socket_accept_do(Socket *s, int fd) { } static int socket_accept_in_cgroup(Socket *s, SocketPort *p, int fd) { + _cleanup_(pidref_done) PidRef pid = PIDREF_NULL; _cleanup_close_pair_ int pair[2] = PIPE_EBADF; int cfd, r; - pid_t pid; assert(s); assert(p); @@ -3034,7 +3029,7 @@ static int socket_accept_in_cgroup(Socket *s, SocketPort *p, int fd) { cfd = receive_one_fd(pair[0], 0); /* We synchronously wait for the helper, as it shouldn't be slow */ - r = wait_for_terminate_and_check("(sd-accept)", pid, WAIT_LOG_ABNORMAL); + r = wait_for_terminate_and_check("(sd-accept)", pid.pid, WAIT_LOG_ABNORMAL); if (r < 0) { safe_close(cfd); return r; @@ -3417,7 +3412,6 @@ static PidRef *socket_control_pid(Unit *u) { 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); @@ -3442,11 +3436,7 @@ static int socket_clean(Unit *u, ExecCleanMask mask) { if (r < 0) goto fail; - r = unit_fork_and_watch_rm_rf(u, l, &pid); - if (r < 0) - goto fail; - - r = pidref_set_pid(&s->control_pid, pid); + r = unit_fork_and_watch_rm_rf(u, l, &s->control_pid); if (r < 0) goto fail; diff --git a/src/core/swap.c b/src/core/swap.c index e06de629da3..aa04b994622 100644 --- a/src/core/swap.c +++ b/src/core/swap.c @@ -1519,7 +1519,6 @@ static PidRef* swap_control_pid(Unit *u) { static int swap_clean(Unit *u, ExecCleanMask mask) { _cleanup_strv_free_ char **l = NULL; Swap *s = SWAP(u); - pid_t pid; int r; assert(s); @@ -1544,11 +1543,7 @@ static int swap_clean(Unit *u, ExecCleanMask mask) { if (r < 0) goto fail; - r = unit_fork_and_watch_rm_rf(u, l, &pid); - if (r < 0) - goto fail; - - r = pidref_set_pid(&s->control_pid, pid); + r = unit_fork_and_watch_rm_rf(u, l, &s->control_pid); if (r < 0) goto fail; diff --git a/src/core/unit.c b/src/core/unit.c index c17e0990184..5fd66e7b6e0 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -5319,7 +5319,8 @@ int unit_set_exec_params(Unit *u, ExecParameters *p) { return 0; } -int unit_fork_helper_process(Unit *u, const char *name, pid_t *ret) { +int unit_fork_helper_process(Unit *u, const char *name, PidRef *ret) { + pid_t pid; int r; assert(u); @@ -5330,9 +5331,24 @@ int unit_fork_helper_process(Unit *u, const char *name, pid_t *ret) { (void) unit_realize_cgroup(u); - r = safe_fork(name, FORK_REOPEN_LOG|FORK_DEATHSIG, ret); - if (r != 0) + r = safe_fork(name, FORK_REOPEN_LOG|FORK_DEATHSIG, &pid); + if (r < 0) + return r; + if (r > 0) { + _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; + int q; + + /* Parent */ + + q = pidref_set_pid(&pidref, pid); + if (q < 0) + return q; + + *ret = TAKE_PIDREF(pidref); return r; + } + + /* Child */ (void) default_signals(SIGNALS_CRASH_HANDLER, SIGNALS_IGNORE); (void) ignore_signals(SIGPIPE); @@ -5348,8 +5364,8 @@ int unit_fork_helper_process(Unit *u, const char *name, pid_t *ret) { return 0; } -int unit_fork_and_watch_rm_rf(Unit *u, char **paths, pid_t *ret_pid) { - pid_t pid; +int unit_fork_and_watch_rm_rf(Unit *u, char **paths, PidRef *ret_pid) { + _cleanup_(pidref_done) PidRef pid = PIDREF_NULL; int r; assert(u); @@ -5372,11 +5388,11 @@ int unit_fork_and_watch_rm_rf(Unit *u, char **paths, pid_t *ret_pid) { _exit(ret); } - r = unit_watch_pid(u, pid, true); + r = unit_watch_pid(u, pid.pid, /* exclusive= */ true); if (r < 0) return r; - *ret_pid = pid; + *ret_pid = TAKE_PIDREF(pid); return 0; } diff --git a/src/core/unit.h b/src/core/unit.h index d0d713f6126..f1a80cc8912 100644 --- a/src/core/unit.h +++ b/src/core/unit.h @@ -1024,8 +1024,8 @@ bool unit_shall_confirm_spawn(Unit *u); int unit_set_exec_params(Unit *s, ExecParameters *p); -int unit_fork_helper_process(Unit *u, const char *name, pid_t *ret); -int unit_fork_and_watch_rm_rf(Unit *u, char **paths, pid_t *ret_pid); +int unit_fork_helper_process(Unit *u, const char *name, PidRef *ret); +int unit_fork_and_watch_rm_rf(Unit *u, char **paths, PidRef *ret); void unit_remove_dependencies(Unit *u, UnitDependencyMask mask); -- 2.39.2