diff options
author | say <say@yandex-team.com> | 2023-05-11 21:33:03 +0300 |
---|---|---|
committer | say <say@yandex-team.com> | 2023-05-11 21:33:03 +0300 |
commit | 7df1977a606ccd826561ecb88ee0f35ddf92e0f9 (patch) | |
tree | 355878658c46e40c14481dfb4f363a8b6c3014c7 | |
parent | bdc94922aa6a71d5293327ac12413f113bc383a5 (diff) | |
download | ydb-7df1977a606ccd826561ecb88ee0f35ddf92e0f9.tar.gz |
Optimize CloseAllFdsOnExec for large MaxOpenFiles
-rw-r--r-- | util/system/shellcommand.cpp | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/util/system/shellcommand.cpp b/util/system/shellcommand.cpp index 025a3ffd2f..03c78b4d92 100644 --- a/util/system/shellcommand.cpp +++ b/util/system/shellcommand.cpp @@ -687,8 +687,22 @@ void TShellCommand::TImpl::OnFork(TPipes& pipes, sigset_t oldmask, char* const* } if (Options_.CloseAllFdsOnExec) { - for (int fd = NSystemInfo::MaxOpenFiles(); fd > STDERR_FILENO; --fd) { - fcntl(fd, F_SETFD, FD_CLOEXEC); + int maxOpenFiles = NSystemInfo::MaxOpenFiles(); + TFsPath fdDir("/proc/self/fd"); + // For a big MaxOpenFiles value directory listing overhead is negligible against useless fcntl calls + if (maxOpenFiles > 1024 && fdDir.IsDirectory()) { + TVector<TString> files; + fdDir.ListNames(files); + for (const TString& f : files) { + int fd; + if (TryFromString(f, fd) && fd > STDERR_FILENO) { + fcntl(fd, F_SETFD, FD_CLOEXEC); + } + } + } else { + for (int fd = maxOpenFiles; fd > STDERR_FILENO; --fd) { + fcntl(fd, F_SETFD, FD_CLOEXEC); + } } } |