qemu/osdep: Split qemu_close_all_open_fd() and add fallback
In order to make it cleaner, split qemu_close_all_open_fd() logic into multiple subfunctions (close with close_range(), with /proc/self/fd and fallback). Signed-off-by: Clément Léger <cleger@rivosinc.com> Reviewed-by: Richard Henderson <richard.henderson@linaro.org> Message-ID: <20240802145423.3232974-3-cleger@rivosinc.com> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
This commit is contained in:
parent
4ec5ebea07
commit
ffa28f9cf5
@ -808,27 +808,16 @@ int qemu_msync(void *addr, size_t length, int fd)
|
|||||||
return msync(addr, length, MS_SYNC);
|
return msync(addr, length, MS_SYNC);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
static bool qemu_close_all_open_fd_proc(void)
|
||||||
* Close all open file descriptors.
|
|
||||||
*/
|
|
||||||
void qemu_close_all_open_fd(void)
|
|
||||||
{
|
{
|
||||||
struct dirent *de;
|
struct dirent *de;
|
||||||
int fd, dfd;
|
int fd, dfd;
|
||||||
DIR *dir;
|
DIR *dir;
|
||||||
|
|
||||||
#ifdef CONFIG_CLOSE_RANGE
|
|
||||||
int r = close_range(0, ~0U, 0);
|
|
||||||
if (!r) {
|
|
||||||
/* Success, no need to try other ways. */
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
dir = opendir("/proc/self/fd");
|
dir = opendir("/proc/self/fd");
|
||||||
if (!dir) {
|
if (!dir) {
|
||||||
/* If /proc is not mounted, there is nothing that can be done. */
|
/* If /proc is not mounted, there is nothing that can be done. */
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
/* Avoid closing the directory. */
|
/* Avoid closing the directory. */
|
||||||
dfd = dirfd(dir);
|
dfd = dirfd(dir);
|
||||||
@ -840,4 +829,39 @@ void qemu_close_all_open_fd(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
closedir(dir);
|
closedir(dir);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool qemu_close_all_open_fd_close_range(void)
|
||||||
|
{
|
||||||
|
#ifdef CONFIG_CLOSE_RANGE
|
||||||
|
int r = close_range(0, ~0U, 0);
|
||||||
|
if (!r) {
|
||||||
|
/* Success, no need to try other ways. */
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void qemu_close_all_open_fd_fallback(void)
|
||||||
|
{
|
||||||
|
int open_max = sysconf(_SC_OPEN_MAX), i;
|
||||||
|
|
||||||
|
/* Fallback */
|
||||||
|
for (i = 0; i < open_max; i++) {
|
||||||
|
close(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Close all open file descriptors.
|
||||||
|
*/
|
||||||
|
void qemu_close_all_open_fd(void)
|
||||||
|
{
|
||||||
|
if (!qemu_close_all_open_fd_close_range() &&
|
||||||
|
!qemu_close_all_open_fd_proc()) {
|
||||||
|
qemu_close_all_open_fd_fallback();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user