mirror of
https://github.com/systemd/systemd
synced 2024-10-06 16:21:34 +00:00
core: port unit_fork_helper_process() and unit_fork_and_watch_rm_rf() to PidRef
This commit is contained in:
parent
37eb258e91
commit
4775b55db0
3
TODO
3
TODO
|
@ -174,10 +174,9 @@ Features:
|
||||||
- pid_is_unwaited() → pidref_is_unwaited()
|
- pid_is_unwaited() → pidref_is_unwaited()
|
||||||
- pid_is_alive() → pidref_is_alive()
|
- pid_is_alive() → pidref_is_alive()
|
||||||
- unit_watch_pid() → unit_watch_pidref()
|
- 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()
|
- exec_spawn()
|
||||||
- serialization of control/main pid in service, socket, mount, swap units
|
- serialization of control/main pid in service, socket, mount, swap units
|
||||||
- unit_fork_and_watch_rm_rf()
|
|
||||||
- cg_pid_get_unit()
|
- cg_pid_get_unit()
|
||||||
- openpt_allocate_in_namespace()
|
- openpt_allocate_in_namespace()
|
||||||
- scope dbus PIDs property needs to gain PIDFDs companion
|
- scope dbus PIDs property needs to gain PIDFDs companion
|
||||||
|
|
|
@ -2220,7 +2220,6 @@ static PidRef* mount_control_pid(Unit *u) {
|
||||||
static int mount_clean(Unit *u, ExecCleanMask mask) {
|
static int mount_clean(Unit *u, ExecCleanMask mask) {
|
||||||
_cleanup_strv_free_ char **l = NULL;
|
_cleanup_strv_free_ char **l = NULL;
|
||||||
Mount *m = MOUNT(u);
|
Mount *m = MOUNT(u);
|
||||||
pid_t pid;
|
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
assert(m);
|
assert(m);
|
||||||
|
@ -2245,11 +2244,7 @@ static int mount_clean(Unit *u, ExecCleanMask mask) {
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
goto fail;
|
goto fail;
|
||||||
|
|
||||||
r = unit_fork_and_watch_rm_rf(u, l, &pid);
|
r = unit_fork_and_watch_rm_rf(u, l, &m->control_pid);
|
||||||
if (r < 0)
|
|
||||||
goto fail;
|
|
||||||
|
|
||||||
r = pidref_set_pid(&m->control_pid, pid);
|
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
goto fail;
|
goto fail;
|
||||||
|
|
||||||
|
|
|
@ -372,8 +372,8 @@ fail:
|
||||||
}
|
}
|
||||||
|
|
||||||
static int scope_enter_start_chown(Scope *s) {
|
static int scope_enter_start_chown(Scope *s) {
|
||||||
|
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
|
||||||
Unit *u = UNIT(s);
|
Unit *u = UNIT(s);
|
||||||
pid_t pid;
|
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
assert(s);
|
assert(s);
|
||||||
|
@ -383,7 +383,7 @@ static int scope_enter_start_chown(Scope *s) {
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return r;
|
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)
|
if (r < 0)
|
||||||
goto fail;
|
goto fail;
|
||||||
|
|
||||||
|
@ -420,7 +420,7 @@ static int scope_enter_start_chown(Scope *s) {
|
||||||
_exit(EXIT_SUCCESS);
|
_exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
r = unit_watch_pid(UNIT(s), pid, true);
|
r = unit_watch_pid(UNIT(s), pidref.pid, /* exclusive= */ true);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
goto fail;
|
goto fail;
|
||||||
|
|
||||||
|
|
|
@ -4890,7 +4890,6 @@ static int service_clean(Unit *u, ExecCleanMask mask) {
|
||||||
_cleanup_strv_free_ char **l = NULL;
|
_cleanup_strv_free_ char **l = NULL;
|
||||||
bool may_clean_fdstore = false;
|
bool may_clean_fdstore = false;
|
||||||
Service *s = SERVICE(u);
|
Service *s = SERVICE(u);
|
||||||
pid_t pid;
|
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
assert(s);
|
assert(s);
|
||||||
|
@ -4931,11 +4930,7 @@ static int service_clean(Unit *u, ExecCleanMask mask) {
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
goto fail;
|
goto fail;
|
||||||
|
|
||||||
r = unit_fork_and_watch_rm_rf(u, l, &pid);
|
r = unit_fork_and_watch_rm_rf(u, l, &s->control_pid);
|
||||||
if (r < 0)
|
|
||||||
goto fail;
|
|
||||||
|
|
||||||
r = pidref_set_pid(&s->control_pid, pid);
|
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
goto fail;
|
goto fail;
|
||||||
|
|
||||||
|
|
|
@ -1503,9 +1503,9 @@ static int socket_address_listen_in_cgroup(
|
||||||
const SocketAddress *address,
|
const SocketAddress *address,
|
||||||
const char *label) {
|
const char *label) {
|
||||||
|
|
||||||
|
_cleanup_(pidref_done) PidRef pid = PIDREF_NULL;
|
||||||
_cleanup_close_pair_ int pair[2] = PIPE_EBADF;
|
_cleanup_close_pair_ int pair[2] = PIPE_EBADF;
|
||||||
int fd, r;
|
int fd, r;
|
||||||
pid_t pid;
|
|
||||||
|
|
||||||
assert(s);
|
assert(s);
|
||||||
assert(address);
|
assert(address);
|
||||||
|
@ -1597,7 +1597,7 @@ static int socket_address_listen_in_cgroup(
|
||||||
fd = receive_one_fd(pair[0], 0);
|
fd = receive_one_fd(pair[0], 0);
|
||||||
|
|
||||||
/* We synchronously wait for the helper, as it shouldn't be slow */
|
/* 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) {
|
if (r < 0) {
|
||||||
safe_close(fd);
|
safe_close(fd);
|
||||||
return r;
|
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) {
|
static int socket_chown(Socket *s, PidRef *ret_pid) {
|
||||||
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
|
_cleanup_(pidref_done) PidRef pid = PIDREF_NULL;
|
||||||
pid_t pid;
|
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
assert(s);
|
assert(s);
|
||||||
|
@ -2030,15 +2029,11 @@ static int socket_chown(Socket *s, PidRef *ret_pid) {
|
||||||
_exit(EXIT_SUCCESS);
|
_exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
r = pidref_set_pid(&pidref, pid);
|
r = unit_watch_pid(UNIT(s), pid.pid, /* exclusive= */ true);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return r;
|
return r;
|
||||||
|
|
||||||
r = unit_watch_pid(UNIT(s), pidref.pid, /* exclusive= */ true);
|
*ret_pid = TAKE_PIDREF(pid);
|
||||||
if (r < 0)
|
|
||||||
return r;
|
|
||||||
|
|
||||||
*ret_pid = TAKE_PIDREF(pidref);
|
|
||||||
return 0;
|
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) {
|
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;
|
_cleanup_close_pair_ int pair[2] = PIPE_EBADF;
|
||||||
int cfd, r;
|
int cfd, r;
|
||||||
pid_t pid;
|
|
||||||
|
|
||||||
assert(s);
|
assert(s);
|
||||||
assert(p);
|
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);
|
cfd = receive_one_fd(pair[0], 0);
|
||||||
|
|
||||||
/* We synchronously wait for the helper, as it shouldn't be slow */
|
/* 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) {
|
if (r < 0) {
|
||||||
safe_close(cfd);
|
safe_close(cfd);
|
||||||
return r;
|
return r;
|
||||||
|
@ -3417,7 +3412,6 @@ static PidRef *socket_control_pid(Unit *u) {
|
||||||
static int socket_clean(Unit *u, ExecCleanMask mask) {
|
static int socket_clean(Unit *u, ExecCleanMask mask) {
|
||||||
_cleanup_strv_free_ char **l = NULL;
|
_cleanup_strv_free_ char **l = NULL;
|
||||||
Socket *s = SOCKET(u);
|
Socket *s = SOCKET(u);
|
||||||
pid_t pid;
|
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
assert(s);
|
assert(s);
|
||||||
|
@ -3442,11 +3436,7 @@ static int socket_clean(Unit *u, ExecCleanMask mask) {
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
goto fail;
|
goto fail;
|
||||||
|
|
||||||
r = unit_fork_and_watch_rm_rf(u, l, &pid);
|
r = unit_fork_and_watch_rm_rf(u, l, &s->control_pid);
|
||||||
if (r < 0)
|
|
||||||
goto fail;
|
|
||||||
|
|
||||||
r = pidref_set_pid(&s->control_pid, pid);
|
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
goto fail;
|
goto fail;
|
||||||
|
|
||||||
|
|
|
@ -1519,7 +1519,6 @@ static PidRef* swap_control_pid(Unit *u) {
|
||||||
static int swap_clean(Unit *u, ExecCleanMask mask) {
|
static int swap_clean(Unit *u, ExecCleanMask mask) {
|
||||||
_cleanup_strv_free_ char **l = NULL;
|
_cleanup_strv_free_ char **l = NULL;
|
||||||
Swap *s = SWAP(u);
|
Swap *s = SWAP(u);
|
||||||
pid_t pid;
|
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
assert(s);
|
assert(s);
|
||||||
|
@ -1544,11 +1543,7 @@ static int swap_clean(Unit *u, ExecCleanMask mask) {
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
goto fail;
|
goto fail;
|
||||||
|
|
||||||
r = unit_fork_and_watch_rm_rf(u, l, &pid);
|
r = unit_fork_and_watch_rm_rf(u, l, &s->control_pid);
|
||||||
if (r < 0)
|
|
||||||
goto fail;
|
|
||||||
|
|
||||||
r = pidref_set_pid(&s->control_pid, pid);
|
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
goto fail;
|
goto fail;
|
||||||
|
|
||||||
|
|
|
@ -5319,7 +5319,8 @@ int unit_set_exec_params(Unit *u, ExecParameters *p) {
|
||||||
return 0;
|
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;
|
int r;
|
||||||
|
|
||||||
assert(u);
|
assert(u);
|
||||||
|
@ -5330,9 +5331,24 @@ int unit_fork_helper_process(Unit *u, const char *name, pid_t *ret) {
|
||||||
|
|
||||||
(void) unit_realize_cgroup(u);
|
(void) unit_realize_cgroup(u);
|
||||||
|
|
||||||
r = safe_fork(name, FORK_REOPEN_LOG|FORK_DEATHSIG, ret);
|
r = safe_fork(name, FORK_REOPEN_LOG|FORK_DEATHSIG, &pid);
|
||||||
if (r != 0)
|
if (r < 0)
|
||||||
return r;
|
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) default_signals(SIGNALS_CRASH_HANDLER, SIGNALS_IGNORE);
|
||||||
(void) ignore_signals(SIGPIPE);
|
(void) ignore_signals(SIGPIPE);
|
||||||
|
@ -5348,8 +5364,8 @@ int unit_fork_helper_process(Unit *u, const char *name, pid_t *ret) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int unit_fork_and_watch_rm_rf(Unit *u, char **paths, pid_t *ret_pid) {
|
int unit_fork_and_watch_rm_rf(Unit *u, char **paths, PidRef *ret_pid) {
|
||||||
pid_t pid;
|
_cleanup_(pidref_done) PidRef pid = PIDREF_NULL;
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
assert(u);
|
assert(u);
|
||||||
|
@ -5372,11 +5388,11 @@ int unit_fork_and_watch_rm_rf(Unit *u, char **paths, pid_t *ret_pid) {
|
||||||
_exit(ret);
|
_exit(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
r = unit_watch_pid(u, pid, true);
|
r = unit_watch_pid(u, pid.pid, /* exclusive= */ true);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return r;
|
return r;
|
||||||
|
|
||||||
*ret_pid = pid;
|
*ret_pid = TAKE_PIDREF(pid);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1024,8 +1024,8 @@ bool unit_shall_confirm_spawn(Unit *u);
|
||||||
|
|
||||||
int unit_set_exec_params(Unit *s, ExecParameters *p);
|
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_helper_process(Unit *u, const char *name, PidRef *ret);
|
||||||
int unit_fork_and_watch_rm_rf(Unit *u, char **paths, pid_t *ret_pid);
|
int unit_fork_and_watch_rm_rf(Unit *u, char **paths, PidRef *ret);
|
||||||
|
|
||||||
void unit_remove_dependencies(Unit *u, UnitDependencyMask mask);
|
void unit_remove_dependencies(Unit *u, UnitDependencyMask mask);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue