Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 325254c

Browse filesBrowse files
obiwacdanielleadams
authored andcommitted
src: remap invalid file descriptors using dup2
When checking for the validity of the stdio file descriptors (#875), ones which don't exist are intended to be remapped to /dev/null (and, if that doesn't work, we abort). This however doesn't work on all platforms and in all cases, and is not anymore required by POSIX; instead, use the `dup2` syscall as a more robust solution (conforms to POSIX.1). Fixes: nodejs/help#2411 Refs: #875 PR-URL: #44461 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent 48cf890 commit 325254c
Copy full SHA for 325254c

File tree

Expand file treeCollapse file tree

1 file changed

+23
-2
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+23
-2
lines changed
Open diff view settings
Collapse file

‎src/node.cc‎

Copy file name to clipboardExpand all lines: src/node.cc
+23-2Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,11 +469,32 @@ static void PlatformInit(ProcessInitializationFlags::Flags flags) {
469469
for (auto& s : stdio) {
470470
const int fd = &s - stdio;
471471
if (fstat(fd, &s.stat) == 0) continue;
472+
472473
// Anything but EBADF means something is seriously wrong. We don't
473474
// have to special-case EINTR, fstat() is not interruptible.
474475
if (errno != EBADF) ABORT();
475-
if (fd != open("/dev/null", O_RDWR)) ABORT();
476-
if (fstat(fd, &s.stat) != 0) ABORT();
476+
477+
// If EBADF (file descriptor doesn't exist), open /dev/null and duplicate
478+
// its file descriptor to the invalid file descriptor. Make sure *that*
479+
// file descriptor is valid. POSIX doesn't guarantee the next file
480+
// descriptor open(2) gives us is the lowest available number anymore in
481+
// POSIX.1-2017, which is why dup2(2) is needed.
482+
int null_fd;
483+
484+
do {
485+
null_fd = open("/dev/null", O_RDWR);
486+
} while (null_fd < 0 && errno == EINTR);
487+
488+
if (null_fd != fd) {
489+
int err;
490+
491+
do {
492+
err = dup2(null_fd, fd);
493+
} while (err < 0 && errno == EINTR);
494+
CHECK_EQ(err, 0);
495+
}
496+
497+
if (fstat(fd, &s.stat) < 0) ABORT();
477498
}
478499
}
479500

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.