aboutsummaryrefslogtreecommitdiff
path: root/src/start-stop-daemon/start-stop-daemon.c
diff options
context:
space:
mode:
authorMatt Whitlock <gentoo@mattwhitlock.name>2022-08-21 09:10:44 -0400
committerWilliam Hubbs <w.d.hubbs@gmail.com>2023-01-20 21:44:37 -0600
commit9dfd2b2737351083e5bed173bda1acd01a70c510 (patch)
tree2887e6ed7bdc12f3dd1b13da94e6f59a25dc950d /src/start-stop-daemon/start-stop-daemon.c
parentde295bd0c63f15e3d4e797e053826dbacbce556e (diff)
start-stop-daemon, supervise-daemon: use closefrom()/close_range()
On systems with a very large RLIMIT_NOFILE, calling close() in a loop from 3 to getdtablesize() effects an enormous number of system calls. There are better alternatives. Both BSD and Linux have the closefrom() system call that closes all file descriptors with indices not less than a specified minimum. Have start-stop-daemon call closefrom() on systems where it's implemented, falling back to the old loop elsewhere. Likewise, calling fcntl(i, F_SETFD, FD_CLOEXEC) in a loop from 3 to getdtablesize() raises a similar performance concern. Linux 5.11 and onward has a close_range() system call with a CLOSE_RANGE_CLOEXEC flag that sets the FD_CLOEXEC flag on all file descriptors in a specified range. Have supervise-daemon utilize this feature on systems where it's implemented, falling back to the old loop elsewhere.
Diffstat (limited to 'src/start-stop-daemon/start-stop-daemon.c')
-rw-r--r--src/start-stop-daemon/start-stop-daemon.c4
1 files changed, 4 insertions, 0 deletions
diff --git a/src/start-stop-daemon/start-stop-daemon.c b/src/start-stop-daemon/start-stop-daemon.c
index b3a8edca..56f85cba 100644
--- a/src/start-stop-daemon/start-stop-daemon.c
+++ b/src/start-stop-daemon/start-stop-daemon.c
@@ -1104,8 +1104,12 @@ int main(int argc, char **argv)
|| rc_yesno(getenv("EINFO_QUIET")))
dup2(stderr_fd, STDERR_FILENO);
+#ifdef HAVE_CLOSEFROM
+ closefrom(3);
+#else
for (i = getdtablesize() - 1; i >= 3; --i)
close(i);
+#endif
if (scheduler != NULL) {
int scheduler_index;