aboutsummaryrefslogtreecommitdiffstats
path: root/util
diff options
context:
space:
mode:
authorsay <say@yandex-team.com>2023-05-11 21:33:03 +0300
committersay <say@yandex-team.com>2023-05-11 21:33:03 +0300
commit7df1977a606ccd826561ecb88ee0f35ddf92e0f9 (patch)
tree355878658c46e40c14481dfb4f363a8b6c3014c7 /util
parentbdc94922aa6a71d5293327ac12413f113bc383a5 (diff)
downloadydb-7df1977a606ccd826561ecb88ee0f35ddf92e0f9.tar.gz
Optimize CloseAllFdsOnExec for large MaxOpenFiles
Diffstat (limited to 'util')
-rw-r--r--util/system/shellcommand.cpp18
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);
+ }
}
}