From: Yu Watanabe Date: Mon, 5 Jan 2026 04:24:41 +0000 (+0900) Subject: fd-util: close_range() is available since kernel 5.9 X-Git-Tag: v260-rc1~408^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c471cca093509391682fd460a0b2eee2c203704b;p=thirdparty%2Fsystemd.git fd-util: close_range() is available since kernel 5.9 Our baseline on kernel is 5.10, hence we can always use it. --- diff --git a/src/basic/fd-util.c b/src/basic/fd-util.c index fb4262e62b3..79903370856 100644 --- a/src/basic/fd-util.c +++ b/src/basic/fd-util.c @@ -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; }