qemu/osdep: Move close_all_open_fds() to oslib-posix

Move close_all_open_fds() in oslib-posix, rename it
qemu_close_all_open_fds() and export it.

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-2-cleger@rivosinc.com>
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
This commit is contained in:
Clément Léger 2024-08-02 16:54:17 +02:00 committed by Richard Henderson
parent 5b0c2742c8
commit 4ec5ebea07
3 changed files with 42 additions and 36 deletions

View file

@ -807,3 +807,37 @@ int qemu_msync(void *addr, size_t length, int fd)
return msync(addr, length, MS_SYNC);
}
/*
* Close all open file descriptors.
*/
void qemu_close_all_open_fd(void)
{
struct dirent *de;
int fd, dfd;
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");
if (!dir) {
/* If /proc is not mounted, there is nothing that can be done. */
return;
}
/* Avoid closing the directory. */
dfd = dirfd(dir);
for (de = readdir(dir); de; de = readdir(dir)) {
fd = atoi(de->d_name);
if (fd != dfd) {
close(fd);
}
}
closedir(dir);
}