fd-util: Expose helper to pack fds into 3,4,5,...
This is useful for situations where an array of FDs is to be passed into a child process (i.e. by passing it through safe_fork). This function can be called in the child (before calling exec) to pack the FDs to all be next to each-other starting from SD_LISTEN_FDS_START (i.e. 3)
This commit is contained in:
committed by
Luca Boccassi
parent
034569150f
commit
85f660d46b
@@ -1468,7 +1468,7 @@ static int fork_flags_to_signal(ForkFlags flags) {
|
||||
int safe_fork_full(
|
||||
const char *name,
|
||||
const int stdio_fds[3],
|
||||
const int except_fds[],
|
||||
int except_fds[],
|
||||
size_t n_except_fds,
|
||||
ForkFlags flags,
|
||||
pid_t *ret_pid) {
|
||||
@@ -1697,6 +1697,19 @@ int safe_fork_full(
|
||||
}
|
||||
}
|
||||
|
||||
if (flags & FORK_PACK_FDS) {
|
||||
/* FORK_CLOSE_ALL_FDS ensures that except_fds are the only FDs >= 3 that are
|
||||
* open, this is including the log. This is required by pack_fds, which will
|
||||
* get stuck in an infinite loop of any FDs other than except_fds are open. */
|
||||
assert(FLAGS_SET(flags, FORK_CLOSE_ALL_FDS));
|
||||
|
||||
r = pack_fds(except_fds, n_except_fds);
|
||||
if (r < 0) {
|
||||
log_full_errno(prio, r, "Failed to pack file descriptors: %m");
|
||||
_exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
if (flags & FORK_CLOEXEC_OFF) {
|
||||
r = fd_cloexec_many(except_fds, n_except_fds, false);
|
||||
if (r < 0) {
|
||||
@@ -1736,7 +1749,7 @@ int safe_fork_full(
|
||||
int pidref_safe_fork_full(
|
||||
const char *name,
|
||||
const int stdio_fds[3],
|
||||
const int except_fds[],
|
||||
int except_fds[],
|
||||
size_t n_except_fds,
|
||||
ForkFlags flags,
|
||||
PidRef *ret_pid) {
|
||||
@@ -1760,7 +1773,7 @@ int pidref_safe_fork_full(
|
||||
int namespace_fork(
|
||||
const char *outer_name,
|
||||
const char *inner_name,
|
||||
const int except_fds[],
|
||||
int except_fds[],
|
||||
size_t n_except_fds,
|
||||
ForkFlags flags,
|
||||
int pidns_fd,
|
||||
|
||||
Reference in New Issue
Block a user