GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||
GeezerVison is showing github.com — src: remap invalid file descriptors using `dup2` by obiwac · Pull Request #44461 · nodejs/node Conversation
Contributor
When checking for the validity of the stdio file descriptors in // Make sure file descriptors 0-2 are valid before we start logging anything. for (int fd = STDIN_FILENO; fd <= STDERR_FILENO; fd += 1) { struct stat ignored; if (fstat(fd, &ignored) == 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(); } As I understand it, the intention behind this is to map file descriptors to Instead, this patch uses the
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
Collaborator
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
obiwac
force-pushed
the
main
branch
6 times, most recently
from
69293a1 to
b3c392e
Compare
addaleax
reviewed
Uh oh!
There was an error while loading. Please reload this page.
|
There was an error while loading. Please reload this page.
“hoping” is doing a lot of work here 🙂 This was actually spec’d behavior in previous POSIX versions, and I was quite surprised to learn (through this PR) that the spec actually changed in this regard. I’d leave a comment in the source code about this, since others might be surprised by this as well. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Got you. I'll do that in a little bit ✌️ |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
TBH, this change adds more failure paths and I don't think it's actually necessary.
The "open() returns the lowest available file descriptor" convention is so deeply codified in the UNIX ecosystem that it's never going away, no matter what POSIX says. There's simply too much software built around that assumption.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
While I do agree with this, there still are specific cases where "available" means something different to what's expected. Case in point: #include <fcntl.h> #include <stdio.h> #include <stdlib.h> int main(void) { system("echo && fstat | grep a.out"); int fd = open("/dev/null", O_RDWR); printf("\n/dev/null: %d\n", fd); return 0; } Running this on FreeBSD in a separate session with (This is actually the issue nodejs/help#2411 had.) Here, |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
da101fe to
326a490
Compare
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: nodejs/help#2411 Refs: nodejs#875
|
Ah okay, fair enough. Fd 0 points to a kind of zombie file description in your example, i.e., it's technically still open but not actually usable? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
AFAIU the file descriptor is never actually closed, so it isn't ever marked as unused ( |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Wait, but fstat() still fails with EBADF? How does that work? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
I don't know, there are plenty of places along the line where I could investigate further if you'd like an exact answer but I don't see how it's too relevant outside of the FreeBSD kernel :P |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
"Zombie file descriptor" is good enough for me. :-) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Is there anything left for me to do here? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Landed in 0c8b141 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
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 <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
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 <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
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 <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
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 <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.