[PATCH v6 2/5] qemu/osdep: Split qemu_close_all_open_fd() and add fallback

Clément Léger posted 5 patches 3 months, 3 weeks ago
There is a newer version of this series
[PATCH v6 2/5] qemu/osdep: Split qemu_close_all_open_fd() and add fallback
Posted by Clément Léger 3 months, 3 weeks ago
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>
---
 util/oslib-posix.c | 51 ++++++++++++++++++++++++++++++++++------------
 1 file changed, 38 insertions(+), 13 deletions(-)

diff --git a/util/oslib-posix.c b/util/oslib-posix.c
index 1e867efa47..a6749d9f9b 100644
--- a/util/oslib-posix.c
+++ b/util/oslib-posix.c
@@ -808,27 +808,16 @@ 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)
+static bool qemu_close_all_open_fd_proc(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;
+        return false;
     }
     /* Avoid closing the directory. */
     dfd = dirfd(dir);
@@ -840,4 +829,40 @@ void qemu_close_all_open_fd(void)
         }
     }
     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;
+}
+
+/*
+ * Close all open file descriptors.
+ */
+void qemu_close_all_open_fd(void)
+{
+    int open_max = sysconf(_SC_OPEN_MAX);
+    int i;
+
+    if (qemu_close_all_open_fd_close_range()) {
+        return;
+    }
+
+    if (qemu_close_all_open_fd_proc()) {
+        return;
+    }
+
+    /* Fallback */
+    for (i = 0; i < open_max; i++) {
+        close(i);
+    }
 }
-- 
2.45.2


Re: [PATCH v6 2/5] qemu/osdep: Split qemu_close_all_open_fd() and add fallback
Posted by Richard Henderson 3 months, 3 weeks ago
On 7/30/24 22:24, Clément Léger wrote:
> +/*
> + * Close all open file descriptors.
> + */
> +void qemu_close_all_open_fd(void)
> +{
> +    int open_max = sysconf(_SC_OPEN_MAX);
> +    int i;
> +
> +    if (qemu_close_all_open_fd_close_range()) {
> +        return;
> +    }
> +
> +    if (qemu_close_all_open_fd_proc()) {
> +        return;
> +    }
> +
> +    /* Fallback */
> +    for (i = 0; i < open_max; i++) {
> +        close(i);
> +    }
>   }

Split out fallback too, because (so far) open_max is only required within the fallback.
Then

     if (!qemu_close_all_open_fd_close_range() &&
         !qemu_close_all_open_fd_proc()) {
         qemu_close_all_open_fd_fallback();
     }


r~