Skip to content

Commit 2ecc1ee

Browse files
a-sivaCommit Queue
authored andcommitted
Close open file descriptors that are not needed during process spawn or exec.
TEST=ci Change-Id: I8ee94b4f54bcb858646739cc6e9add333a25a724 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/430841 Reviewed-by: Ryan Macnak <[email protected]> Commit-Queue: Siva Annamalai <[email protected]>
1 parent 3959470 commit 2ecc1ee

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

runtime/bin/process_linux.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,14 +518,17 @@ class ProcessStarter {
518518
if (TEMP_FAILURE_RETRY(dup2(write_out_[0], STDIN_FILENO)) == -1) {
519519
ReportChildError();
520520
}
521+
close(write_out_[0]);
521522

522523
if (TEMP_FAILURE_RETRY(dup2(read_in_[1], STDOUT_FILENO)) == -1) {
523524
ReportChildError();
524525
}
526+
close(read_in_[1]);
525527

526528
if (TEMP_FAILURE_RETRY(dup2(read_err_[1], STDERR_FILENO)) == -1) {
527529
ReportChildError();
528530
}
531+
close(read_err_[1]);
529532
} else {
530533
ASSERT(mode_ == kInheritStdio);
531534
}
@@ -939,6 +942,7 @@ int Process::Exec(Namespace* namespc,
939942
Utils::StrError(errno, errmsg, errmsg_len);
940943
return -1;
941944
}
945+
942946
// TODO(dart:io) Test for the existence of execveat, and use it instead.
943947
execvp(const_cast<const char*>(realpath),
944948
const_cast<char* const*>(arguments));

runtime/bin/process_macos.cc

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -450,14 +450,17 @@ class ProcessStarter {
450450
if (TEMP_FAILURE_RETRY(dup2(write_out_[0], STDIN_FILENO)) == -1) {
451451
ReportChildError();
452452
}
453+
close(write_out_[0]);
453454

454455
if (TEMP_FAILURE_RETRY(dup2(read_in_[1], STDOUT_FILENO)) == -1) {
455456
ReportChildError();
456457
}
458+
close(read_in_[1]);
457459

458460
if (TEMP_FAILURE_RETRY(dup2(read_err_[1], STDERR_FILENO)) == -1) {
459461
ReportChildError();
460462
}
463+
close(read_err_[1]);
461464
} else {
462465
ASSERT(mode_ == kInheritStdio);
463466
}
@@ -630,34 +633,28 @@ class ProcessStarter {
630633
}
631634

632635
void SetupDetachedWithStdio() {
633-
// Close all open file descriptors except for
634-
// exec_control_[1], write_out_[0], read_in_[1] and
635-
// read_err_[1].
636-
int max_fds = sysconf(_SC_OPEN_MAX);
637-
if (max_fds == -1) {
638-
max_fds = _POSIX_OPEN_MAX;
639-
}
640-
for (int fd = 0; fd < max_fds; fd++) {
641-
if ((fd != exec_control_[1]) && (fd != write_out_[0]) &&
642-
(fd != read_in_[1]) && (fd != read_err_[1])) {
643-
close(fd);
644-
}
645-
}
646-
647636
if (TEMP_FAILURE_RETRY(dup2(write_out_[0], STDIN_FILENO)) == -1) {
648637
ReportChildError();
649638
}
650-
close(write_out_[0]);
651639

652640
if (TEMP_FAILURE_RETRY(dup2(read_in_[1], STDOUT_FILENO)) == -1) {
653641
ReportChildError();
654642
}
655-
close(read_in_[1]);
656643

657644
if (TEMP_FAILURE_RETRY(dup2(read_err_[1], STDERR_FILENO)) == -1) {
658645
ReportChildError();
659646
}
660-
close(read_err_[1]);
647+
648+
// Close all open file descriptors except for std* and exec_control_[1].
649+
int max_fds = sysconf(_SC_OPEN_MAX);
650+
if (max_fds == -1) {
651+
max_fds = _POSIX_OPEN_MAX;
652+
}
653+
for (int fd = 3; fd < max_fds; fd++) {
654+
if (fd != exec_control_[1]) {
655+
close(fd);
656+
}
657+
}
661658
}
662659

663660
int CleanupAndReturnError() {

0 commit comments

Comments
 (0)