process-util: Migrate namespace_fork() to PidRef

Co-authored-by: Mike Yuan <me@yhndnzj.com>
This commit is contained in:
Daan De Meyer
2025-12-20 13:45:22 +01:00
committed by Mike Yuan
co-authored by Mike Yuan
parent 3532691d72
commit 8c007f68ff
12 changed files with 86 additions and 78 deletions
+21 -14
View File
@@ -1884,7 +1884,7 @@ int safe_fork_full(
return r;
}
int namespace_fork(
int namespace_fork_full(
const char *outer_name,
const char *inner_name,
int except_fds[],
@@ -1895,7 +1895,7 @@ int namespace_fork(
int netns_fd,
int userns_fd,
int root_fd,
pid_t *ret_pid) {
PidRef *ret) {
int r;
@@ -1904,14 +1904,16 @@ int namespace_fork(
* /proc/self/fd works correctly. */
assert(!FLAGS_SET(flags, FORK_ALLOW_DLOPEN)); /* never allow loading shared library from another ns */
r = safe_fork_full(outer_name,
NULL,
except_fds, n_except_fds,
(flags|FORK_DEATHSIG_SIGINT|FORK_DEATHSIG_SIGTERM|FORK_DEATHSIG_SIGKILL) & ~(FORK_REOPEN_LOG|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE), ret_pid);
r = pidref_safe_fork_full(
outer_name,
NULL,
except_fds, n_except_fds,
(flags|FORK_DEATHSIG_SIGINT|FORK_DEATHSIG_SIGTERM|FORK_DEATHSIG_SIGKILL) & ~(FORK_REOPEN_LOG|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE),
ret);
if (r < 0)
return r;
if (r == 0) {
pid_t pid;
_cleanup_(pidref_done) PidRef pidref_inner = PIDREF_NULL;
/* Child */
@@ -1922,20 +1924,25 @@ int namespace_fork(
}
/* We mask a few flags here that either make no sense for the grandchild, or that we don't have to do again */
r = safe_fork_full(inner_name,
NULL,
except_fds, n_except_fds,
flags & ~(FORK_WAIT|FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_REARRANGE_STDIO), &pid);
r = pidref_safe_fork_full(
inner_name,
NULL,
except_fds, n_except_fds,
flags & ~(FORK_WAIT|FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_REARRANGE_STDIO),
&pidref_inner);
if (r < 0)
_exit(EXIT_FAILURE);
if (r == 0) {
/* Child */
if (ret_pid)
*ret_pid = pid;
if (ret)
*ret = TAKE_PIDREF(pidref_inner);
return 0;
}
r = wait_for_terminate_and_check(inner_name, pid, FLAGS_SET(flags, FORK_LOG) ? WAIT_LOG : 0);
r = pidref_wait_for_terminate_and_check(
inner_name,
&pidref_inner,
FLAGS_SET(flags, FORK_LOG) ? WAIT_LOG : 0);
if (r < 0)
_exit(EXIT_FAILURE);
+18 -2
View File
@@ -216,7 +216,7 @@ static inline int safe_fork(const char *name, ForkFlags flags, pid_t *ret_pid) {
return safe_fork_full(name, NULL, NULL, 0, flags, ret_pid);
}
int namespace_fork(
int namespace_fork_full(
const char *outer_name,
const char *inner_name,
int except_fds[],
@@ -227,7 +227,23 @@ int namespace_fork(
int netns_fd,
int userns_fd,
int root_fd,
pid_t *ret_pid);
PidRef *ret);
static inline int namespace_fork(
const char *outer_name,
const char *inner_name,
ForkFlags flags,
int pidns_fd,
int mntns_fd,
int netns_fd,
int userns_fd,
int root_fd,
PidRef *ret) {
return namespace_fork_full(outer_name, inner_name, NULL, 0, flags,
pidns_fd, mntns_fd, netns_fd, userns_fd, root_fd,
ret);
}
int set_oom_score_adjust(int value);
int get_oom_score_adjust(int *ret);
+1 -3
View File
@@ -1665,15 +1665,13 @@ int openpt_allocate_in_namespace(
r = namespace_fork(
"(sd-openptns)",
"(sd-openpt)",
/* except_fds= */ NULL,
/* n_except_fds= */ 0,
FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGKILL|FORK_WAIT,
pidnsfd,
mntnsfd,
/* netns_fd= */ -EBADF,
usernsfd,
rootfd,
/* ret_pid= */ NULL);
/* ret= */ NULL);
if (r < 0)
return r;
if (r == 0) {