This system call was added while back, but does not have glibc support yet.
Lets try to use the new facility early on. The promise in reference link is
that the new call is faster, and works when /proc is not mounted.
Reference: https://lwn.net/Articles/789000/
Reference: https://lwn.net/Articles/789023/
Reference: https://kernelnewbies.org/LinuxChanges#Linux_5.9.New_close_range.28.29_system_call_for_easier_closing_of_file_descriptors
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
AC_CHECK_FUNCS([ \
clearenv \
+ close_range \
__fpurge \
fpurge \
__fpending \
UL_CHECK_SYSCALL([pidfd_open])
UL_CHECK_SYSCALL([pidfd_send_signal])
+UL_CHECK_SYSCALL([close_range])
AC_CHECK_FUNCS([isnan], [],
[AC_CHECK_LIB([m], [isnan], [MATH_LIBS="-lm"])]
return d;
}
+#if defined(__linux__)
+# include <sys/syscall.h>
+# if defined(SYS_close_range)
+# include <sys/types.h>
+# ifndef HAVE_CLOSE_RANGE
+static inline int close_range(unsigned int first, unsigned int last)
+{
+ return syscall(SYS_close_range, first, last);
+}
+# endif
+# define HAVE_CLOSE_RANGE 1
+# endif /* SYS_close_range */
+#endif /* __linux__ */
+
extern void close_all_fds(const int exclude[], size_t exsz);
#define UL_COPY_READ_ERROR (-1)
{"version", no_argument, NULL, 'V'},
{NULL, 0, NULL, 0}
};
+#ifndef HAVE_CLOSE_RANGE
const int wanted_fds[] = {
STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO
};
+#endif
timeout = (unsigned int)getlogindefs_num("LOGIN_TIMEOUT", LOGIN_TIMEOUT);
*p++ = ' ';
#endif
}
-
+#ifdef HAVE_CLOSE_RANGE
+ close_range(STDERR_FILENO + 1, ~0U);
+#else
close_all_fds(wanted_fds, ARRAY_SIZE(wanted_fds));
+#endif
}
int main(int argc, char **argv)