fdset: optionally, close remaining fds asynchronously
authorLennart Poettering <lennart@poettering.net>
Thu, 17 Oct 2024 07:43:37 +0000 (09:43 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 17 Oct 2024 07:48:05 +0000 (09:48 +0200)
src/core/bpf-restrict-ifaces.c
src/core/bpf-socket-bind.c
src/nspawn/nspawn.c
src/shared/fdset.c
src/shared/fdset.h

index a39f4895f2b6f59ee7c604bbdde18537496506de..af49abd677bd1de69efd0017302716feb7f134da 100644 (file)
@@ -159,7 +159,7 @@ int bpf_restrict_ifaces_install(Unit *u) {
                 return 0;
 
         r = restrict_ifaces_install_impl(u);
-        fdset_close(crt->initial_restrict_ifaces_link_fds);
+        fdset_close(crt->initial_restrict_ifaces_link_fds, /* async= */ false);
         return r;
 }
 
index 2a1a0278d5a380012df2ca752a7b82c852886333..8853f3eecc9a8f7208703dd0609b62d573e1a19d 100644 (file)
@@ -229,7 +229,7 @@ int bpf_socket_bind_install(Unit *u) {
                 return 0;
 
         r = socket_bind_install_impl(u);
-        fdset_close(crt->initial_socket_bind_link_fds);
+        fdset_close(crt->initial_socket_bind_link_fds, /* async= */ false);
         return r;
 }
 
index 70ce4e6d7964ba0e542ce124e0e9f49ee91cb321..8fc58665e4dce9f3bd8353b568bf73e60a534a75 100644 (file)
@@ -5267,7 +5267,7 @@ static int run_container(
 
         barrier_set_role(&barrier, BARRIER_PARENT);
 
-        fdset_close(fds);
+        fdset_close(fds, /* async= */ false);
 
         fd_inner_socket_pair[1] = safe_close(fd_inner_socket_pair[1]);
         fd_outer_socket_pair[1] = safe_close(fd_outer_socket_pair[1]);
index cb5a69ef22467f66098c13e20772adc1b4efd1c8..b3d3cfa784d36d1b9a3e1c956c1b13c6f4e82116 100644 (file)
@@ -8,6 +8,7 @@
 #include "sd-daemon.h"
 
 #include "alloc-util.h"
+#include "async.h"
 #include "dirent-util.h"
 #include "fd-util.h"
 #include "fdset.h"
@@ -51,7 +52,7 @@ int fdset_new_array(FDSet **ret, const int fds[], size_t n_fds) {
         return 0;
 }
 
-void fdset_close(FDSet *s) {
+void fdset_close(FDSet *s, bool async) {
         void *p;
 
         while ((p = set_steal_first(MAKE_SET(s)))) {
@@ -72,12 +73,21 @@ void fdset_close(FDSet *s) {
                         log_debug("Closing set fd %i (%s)", fd, strna(path));
                 }
 
-                (void) close(fd);
+                if (async)
+                        (void) asynchronous_close(fd);
+                else
+                        (void) close(fd);
         }
 }
 
 FDSet* fdset_free(FDSet *s) {
-        fdset_close(s);
+        fdset_close(s, /* async= */ false);
+        set_free(MAKE_SET(s));
+        return NULL;
+}
+
+FDSet* fdset_free_async(FDSet *s) {
+        fdset_close(s, /* async= */ true);
         set_free(MAKE_SET(s));
         return NULL;
 }
index 70a764fb4d3748366c88d9d390196bf3321460d7..3e69d32146c868b33c273aa5906640cf58b4437e 100644 (file)
@@ -11,6 +11,7 @@ typedef struct FDSet FDSet;
 
 FDSet* fdset_new(void);
 FDSet* fdset_free(FDSet *s);
+FDSet* fdset_free_async(FDSet *s);
 
 int fdset_put(FDSet *s, int fd);
 int fdset_consume(FDSet *s, int fd);
@@ -36,7 +37,7 @@ int fdset_iterate(FDSet *s, Iterator *i);
 
 int fdset_steal_first(FDSet *fds);
 
-void fdset_close(FDSet *fds);
+void fdset_close(FDSet *fds, bool async);
 
 #define _FDSET_FOREACH(fd, fds, i) \
         for (Iterator i = ITERATOR_FIRST; ((fd) = fdset_iterate((fds), &i)) >= 0; )
@@ -45,3 +46,5 @@ void fdset_close(FDSet *fds);
 
 DEFINE_TRIVIAL_CLEANUP_FUNC(FDSet*, fdset_free);
 #define _cleanup_fdset_free_ _cleanup_(fdset_freep)
+
+DEFINE_TRIVIAL_CLEANUP_FUNC(FDSet*, fdset_free_async);