]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fd-util: close_range() is available since kernel 5.9
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 5 Jan 2026 04:24:41 +0000 (13:24 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 13 Jan 2026 01:21:36 +0000 (10:21 +0900)
Our baseline on kernel is 5.10, hence we can always use it.

src/basic/fd-util.c

index fb4262e62b3983b8ab0bb2150d5e9bf9e232e6f0..79903370856bb5939c2c63581e6e79b0da008c3d 100644 (file)
@@ -316,8 +316,6 @@ int close_all_fds_by_proc(const int except[], size_t n_except) {
         return r;
 }
 
-static bool have_close_range = true; /* Assume we live in the future */
-
 static int close_all_fds_special_case(const int except[], size_t n_except) {
         assert(n_except == 0 || except);
 
@@ -325,9 +323,6 @@ static int close_all_fds_special_case(const int except[], size_t n_except) {
          * nicely, since we won't need sorting for them. Returns > 0 if the special casing worked, 0
          * otherwise. */
 
-        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;
@@ -336,31 +331,22 @@ static int close_all_fds_special_case(const int except[], size_t n_except) {
 
         case 0:
                 /* Close everything. Yay! */
+                if (close_range(3, INT_MAX, 0) < 0)
+                        return -errno;
 
-                if (close_range(3, INT_MAX, 0) >= 0)
-                        return 1;
-
-                if (ERRNO_IS_NOT_SUPPORTED(errno) || ERRNO_IS_PRIVILEGE(errno)) {
-                        have_close_range = false;
-                        return 0;
-                }
-
-                return -errno;
+                return 1;
 
         case 1:
                 /* Close all but exactly one, then we don't need no sorting. This is a pretty common
                  * case, hence let's handle it specially. */
 
-                if ((except[0] <= 3 || close_range(3, except[0]-1, 0) >= 0) &&
-                    (except[0] >= INT_MAX || close_range(MAX(3, except[0]+1), -1, 0) >= 0))
-                        return 1;
+                if (except[0] > 3 && close_range(3, except[0] - 1, 0) < 0)
+                        return -errno;
 
-                if (ERRNO_IS_NOT_SUPPORTED(errno) || ERRNO_IS_PRIVILEGE(errno)) {
-                        have_close_range = false;
-                        return 0;
-                }
+                if (except[0] < INT_MAX && close_range(MAX(3, except[0] + 1), -1, 0) < 0)
+                        return -errno;
 
-                return -errno;
+                return 1;
 
         default:
                 return 0;
@@ -392,9 +378,6 @@ int close_all_fds(const int except[], size_t n_except) {
         if (r > 0) /* special case worked! */
                 return 0;
 
-        if (!have_close_range)
-                return close_all_fds_by_proc(except, n_except);
-
         _cleanup_free_ int *sorted_malloc = NULL;
         size_t n_sorted;
         int *sorted;
@@ -436,13 +419,8 @@ int close_all_fds(const int except[], size_t n_except) {
                         continue;
 
                 /* Close everything between the start and end fds (both of which shall stay open) */
-                if (close_range(start + 1, end - 1, 0) < 0) {
-                        if (!ERRNO_IS_NOT_SUPPORTED(errno) && !ERRNO_IS_PRIVILEGE(errno))
-                                return -errno;
-
-                        have_close_range = false;
-                        return close_all_fds_by_proc(except, n_except);
-                }
+                if (close_range(start + 1, end - 1, 0) < 0)
+                        return -errno;
         }
 
         /* The loop succeeded. Let's now close everything beyond the end */
@@ -450,13 +428,8 @@ int close_all_fds(const int except[], size_t n_except) {
         if (sorted[n_sorted-1] >= INT_MAX) /* Dont let the addition below overflow */
                 return 0;
 
-        if (close_range(sorted[n_sorted-1] + 1, INT_MAX, 0) < 0) {
-                if (!ERRNO_IS_NOT_SUPPORTED(errno) && !ERRNO_IS_PRIVILEGE(errno))
-                        return -errno;
-
-                have_close_range = false;
-                return close_all_fds_by_proc(except, n_except);
-        }
+        if (close_range(sorted[n_sorted-1] + 1, INT_MAX, 0) < 0)
+                return -errno;
 
         return 0;
 }