diff mbox series

[trivial,2/2] net/tap: use os_close_all_open_fd() instead of open-coding it

Message ID 14aa02474bc5127910810969b87b659d766e2969.1706221377.git.mjt@tls.msk.ru
State New
Headers show
Series split out os_close_all_open_fd and use it in net/tap.c too | expand

Commit Message

Michael Tokarev Jan. 25, 2024, 10:29 p.m. UTC
Current code loops over every file descriptor up to SC_OPEN_MAX/RLIMIT_NOFILE
which might be huge and the loop might be slow.  But we already have
os_close_all_open_fd() which is fast.  Use it.

Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
---
 net/tap.c | 15 ++-------------
 1 file changed, 2 insertions(+), 13 deletions(-)
diff mbox series

Patch

diff --git a/net/tap.c b/net/tap.c
index c698b70475..11c85c50dc 100644
--- a/net/tap.c
+++ b/net/tap.c
@@ -459,13 +459,7 @@  static void launch_script(const char *setup_script, const char *ifname,
         return;
     }
     if (pid == 0) {
-        int open_max = sysconf(_SC_OPEN_MAX), i;
-
-        for (i = 3; i < open_max; i++) {
-            if (i != fd) {
-                close(i);
-            }
-        }
+        os_close_all_open_fd(3);
         parg = args;
         *parg++ = (char *)setup_script;
         *parg++ = (char *)ifname;
@@ -549,16 +543,11 @@  static int net_bridge_run_helper(const char *helper, const char *bridge,
         return -1;
     }
     if (pid == 0) {
-        int open_max = sysconf(_SC_OPEN_MAX), i;
         char *fd_buf = NULL;
         char *br_buf = NULL;
         char *helper_cmd = NULL;
 
-        for (i = 3; i < open_max; i++) {
-            if (i != sv[1]) {
-                close(i);
-            }
-        }
+        os_close_all_open_fd(3);
 
         fd_buf = g_strdup_printf("%s%d", "--fd=", sv[1]);