On some systems (MacOS for instance), sysconf(_SC_OPEN_MAX) can return -1. In that case we should fallback to using the OPEN_MAX define. According to "man sysconf", the OPEN_MAX define should be present and provided by either unistd.h and/or limits.h so include them for that purpose. For other OSes, just assume a maximum of 1024 files descriptors as a fallback.
Fixes: 4ec5ebea078e ("qemu/osdep: Move close_all_open_fds() to oslib-posix") Reported-by: Daniel P. Berrangé <berra...@redhat.com> Signed-off-by: Clément Léger <cle...@rivosinc.com> --- util/oslib-posix.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/util/oslib-posix.c b/util/oslib-posix.c index 901dcccd73..abf3aa597d 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -44,6 +44,11 @@ #include "qemu/thread-context.h" #include "qemu/main-loop.h" +#ifdef CONFIG_DARWIN +#include <limits.h> +#include <unistd.h> +#endif + #ifdef CONFIG_LINUX #include <sys/syscall.h> #endif @@ -928,6 +933,13 @@ static void qemu_close_all_open_fd_fallback(const int *skip, unsigned int nskip, void qemu_close_all_open_fd(const int *skip, unsigned int nskip) { int open_max = sysconf(_SC_OPEN_MAX); + if (open_max == -1) { +#ifdef CONFIG_DARWIN + open_max = OPEN_MAX; +#else + open_max = 1024; +#endif + } assert(skip != NULL || nskip == 0); -- 2.45.2