]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fd-util: make fd_in_set() (and thus close_all_fds()) handle invalidated fds in the...
authorLennart Poettering <lennart@poettering.net>
Fri, 4 Nov 2022 17:19:29 +0000 (18:19 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 4 Nov 2022 17:45:32 +0000 (18:45 +0100)
let's handle gracefully if fds in the specified array are already
invalidated (i.e. negative). This is handy when putting together arrays
on the fly.

src/basic/fd-util.c

index cee20a9a815c2e54bc6f46e25541ca386c361d7f..6ed04449bf08900319d6bfe651bbb22c3abfc9f5 100644 (file)
@@ -177,9 +177,13 @@ int fd_cloexec(int fd, bool cloexec) {
 _pure_ static bool fd_in_set(int fd, const int fdset[], size_t n_fdset) {
         assert(n_fdset == 0 || fdset);
 
-        for (size_t i = 0; i < n_fdset; i++)
+        for (size_t i = 0; i < n_fdset; i++) {
+                if (fdset[i] < 0)
+                        continue;
+
                 if (fdset[i] == fd)
                         return true;
+        }
 
         return false;
 }
@@ -252,6 +256,10 @@ static int close_all_fds_special_case(const int except[], size_t n_except) {
         if (!have_close_range)
                 return 0;
 
+        if (n_except == 1 && except[0] < 0) /* Minor optimization: if we only got one fd, and it's invalid,
+                                             * we got none */
+                n_except = 0;
+
         switch (n_except) {
 
         case 0: