Fix compiler error when CLOSE_RANGE_CLOEXEC is missing (#14430)
Fixes the issue introduced as a result of #14213, where the agent fails to build successfully on FreeBSD < 13.1 and on environments with Linux kernel version < 5.11, due to missing 'CLOSE_RANGE_CLOEXEC' .
Dim-P committed
Feb 9, 2023 at 15:31 UTC
52e5978f73566291004607c2c06318f61774f834
1 file changed
+8
-5
libnetdata/libnetdata.c
+8
-5
@@ -2011,21 +2011,24 @@ void for_each_open_fd(OPEN_FD_ACTION action, OPEN_FD_EXCLUDE excluded_fds){
2011
if(!(excluded_fds & OPEN_FD_EXCLUDE_STDIN)) (void)close(STDIN_FILENO);
2012
if(!(excluded_fds & OPEN_FD_EXCLUDE_STDOUT)) (void)close(STDOUT_FILENO);
2013
if(!(excluded_fds & OPEN_FD_EXCLUDE_STDERR)) (void)close(STDERR_FILENO);
2014
+#if defined(HAVE_CLOSE_RANGE)
2015
+ if(close_range(STDERR_FILENO + 1, ~0U, 0) == 0) return;
2016
+ error("close_range() failed, will try to close fds one by one");
2017
+#endif
2018
break;
2019
case OPEN_FD_ACTION_FD_CLOEXEC:
2020
if(!(excluded_fds & OPEN_FD_EXCLUDE_STDIN)) (void)fcntl(STDIN_FILENO, F_SETFD, FD_CLOEXEC);
2021
if(!(excluded_fds & OPEN_FD_EXCLUDE_STDOUT)) (void)fcntl(STDOUT_FILENO, F_SETFD, FD_CLOEXEC);
2022
if(!(excluded_fds & OPEN_FD_EXCLUDE_STDERR)) (void)fcntl(STDERR_FILENO, F_SETFD, FD_CLOEXEC);
2023
+#if defined(HAVE_CLOSE_RANGE) && defined(CLOSE_RANGE_CLOEXEC) // Linux >= 5.11, FreeBSD >= 13.1
2024
+ if(close_range(STDERR_FILENO + 1, ~0U, CLOSE_RANGE_CLOEXEC) == 0) return;
2025
+ error("close_range() failed, will try to mark fds for closing one by one");
2026
+#endif
2027
break;
2028
default:
2029
break; // do nothing
2030
}
2031
2024
-#if defined(HAVE_CLOSE_RANGE)
2025
- if(close_range(STDERR_FILENO + 1, ~0U, (action == OPEN_FD_ACTION_FD_CLOEXEC ? CLOSE_RANGE_CLOEXEC : 0)) == 0) return;
2026
- error("close_range() failed, will try to close fds manually");
2027
-#endif
2028
-
2032
DIR *dir = opendir("/proc/self/fd");
2033
if (dir == NULL) {
2034
struct rlimit rl;