net/tap: Factorize fd closing after forking
The same code is used twice to actually close all open file descriptors after forking. Factorize it in a single place. Signed-off-by: Clément Léger <cleger@rivosinc.com> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Reviewed-by: Richard Henderson <richard.henderson@linaro.org> Message-ID: <20240802145423.3232974-4-cleger@rivosinc.com> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
This commit is contained in:
parent
ffa28f9cf5
commit
a9b5d6e536
27
net/tap.c
27
net/tap.c
@ -385,6 +385,17 @@ static TAPState *net_tap_fd_init(NetClientState *peer,
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void close_all_fds_after_fork(int excluded_fd)
|
||||||
|
{
|
||||||
|
int open_max = sysconf(_SC_OPEN_MAX), i;
|
||||||
|
|
||||||
|
for (i = 3; i < open_max; i++) {
|
||||||
|
if (i != excluded_fd) {
|
||||||
|
close(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void launch_script(const char *setup_script, const char *ifname,
|
static void launch_script(const char *setup_script, const char *ifname,
|
||||||
int fd, Error **errp)
|
int fd, Error **errp)
|
||||||
{
|
{
|
||||||
@ -400,13 +411,7 @@ static void launch_script(const char *setup_script, const char *ifname,
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (pid == 0) {
|
if (pid == 0) {
|
||||||
int open_max = sysconf(_SC_OPEN_MAX), i;
|
close_all_fds_after_fork(fd);
|
||||||
|
|
||||||
for (i = 3; i < open_max; i++) {
|
|
||||||
if (i != fd) {
|
|
||||||
close(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
parg = args;
|
parg = args;
|
||||||
*parg++ = (char *)setup_script;
|
*parg++ = (char *)setup_script;
|
||||||
*parg++ = (char *)ifname;
|
*parg++ = (char *)ifname;
|
||||||
@ -490,17 +495,11 @@ static int net_bridge_run_helper(const char *helper, const char *bridge,
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (pid == 0) {
|
if (pid == 0) {
|
||||||
int open_max = sysconf(_SC_OPEN_MAX), i;
|
|
||||||
char *fd_buf = NULL;
|
char *fd_buf = NULL;
|
||||||
char *br_buf = NULL;
|
char *br_buf = NULL;
|
||||||
char *helper_cmd = NULL;
|
char *helper_cmd = NULL;
|
||||||
|
|
||||||
for (i = 3; i < open_max; i++) {
|
close_all_fds_after_fork(sv[1]);
|
||||||
if (i != sv[1]) {
|
|
||||||
close(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fd_buf = g_strdup_printf("%s%d", "--fd=", sv[1]);
|
fd_buf = g_strdup_printf("%s%d", "--fd=", sv[1]);
|
||||||
|
|
||||||
if (strrchr(helper, ' ') || strrchr(helper, '\t')) {
|
if (strrchr(helper, ' ') || strrchr(helper, '\t')) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user