mirror of
https://github.com/nodejs/node.git
synced 2026-11-26 21:58:26 -05:00
src: remap invalid file descriptors using dup2
When checking for the validity of the stdio file descriptors (nodejs#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: https://github.com/nodejs/help/issues/2411 Refs: https://github.com/nodejs/node/pull/875 PR-URL: https://github.com/nodejs/node/pull/44461 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
This commit is contained in:
1 parent
a4158692d7
commit
8d5682266e
1 file changed
+23
-2
+23
-2
@@ -453,11 +453,32 @@ static void PlatformInit(ProcessInitializationFlags::Flags flags) {
|
||||
for (auto& s : stdio) {
|
||||
const int fd = &s - stdio;
|
||||
if (fstat(fd, &s.stat) == 0) continue;
|
||||
|
||||
// Anything but EBADF means something is seriously wrong. We don't
|
||||
// have to special-case EINTR, fstat() is not interruptible.
|
||||
if (errno != EBADF) ABORT();
|
||||
if (fd != open("/dev/null", O_RDWR)) ABORT();
|
||||
if (fstat(fd, &s.stat) != 0) ABORT();
|
||||
|
||||
// If EBADF (file descriptor doesn't exist), open /dev/null and duplicate
|
||||
// its file descriptor to the invalid file descriptor. Make sure *that*
|
||||
// file descriptor is valid. POSIX doesn't guarantee the next file
|
||||
// descriptor open(2) gives us is the lowest available number anymore in
|
||||
// POSIX.1-2017, which is why dup2(2) is needed.
|
||||
int null_fd;
|
||||
|
||||
do {
|
||||
null_fd = open("/dev/null", O_RDWR);
|
||||
} while (null_fd < 0 && errno == EINTR);
|
||||
|
||||
if (null_fd != fd) {
|
||||
int err;
|
||||
|
||||
do {
|
||||
err = dup2(null_fd, fd);
|
||||
} while (err < 0 && errno == EINTR);
|
||||
CHECK_EQ(err, 0);
|
||||
}
|
||||
|
||||
if (fstat(fd, &s.stat) < 0) ABORT();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user