Optimize startup time, making closeallfiles() faster, by doing less system calls. Instead of calling close for each possible file, use poll() to check if file exist at all. On linux with open file limit set to 1048576, it should do 1024 poll() calls instead of 1048576 close().
Bug: 55618 Signed-off-by: Amadeusz Sławiński <am...@asmblr.net> --- src/misc.c | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/misc.c b/src/misc.c index 43e93a2a..52f8514e 100644 --- a/src/misc.c +++ b/src/misc.c @@ -26,6 +26,7 @@ **************************************************************** */ +#include <poll.h> #include <sys/types.h> #include <sys/stat.h> /* mkdir() declaration */ #include <signal.h> @@ -379,12 +380,38 @@ int except; #endif /* SVR4 */ #if defined(SYSV) && defined(NOFILE) && !defined(ISC) f = NOFILE; -#else /* SYSV && !ISC */ - f = getdtablesize(); -#endif /* SYSV && !ISC */ while (--f > 2) if (f != except) close(f); +#else /* SYSV && !ISC */ + { + struct pollfd pfd[1024]; + int maxfd, i, ret, z; + + i = 3; /* skip stdin, stdout and stderr */ + maxfd = getdtablesize(); + + while (i < maxfd) + { + memset(pfd, 0, sizeof(pfd)); + + z = 0; + for (f = i; f < maxfd && f < i + 1024; f++) + pfd[z++].fd = f; + + ret = poll(pfd, f - i, 0); + if (ret < 0) + Panic(errno, "poll"); + + z = 0; + for (f = i; f < maxfd && f < i + 1024; f++) + if (!(pfd[z++].revents & POLLNVAL) && f != except) + close(f); + + i = f; + } + } +#endif /* SYSV && !ISC */ } #endif /* HAVE_FDWALK */ -- 2.23.0