You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Want way to get BorrowedFd<'static> for 0 1 2 #90809
Logically, stdin stdout and stderr are global static file descriptors. Everyone in the program is entitled to assume they exist (this is logically implied, for example, by the contract of io::stdin()). No-one can own (and therefore drop) them.
Everyone in the program is entitled to assume they exist
It looks like it isn't strictly guaranteed by POSIX that this will always be the case on program start?
If file descriptor 0, 1, or 2 would otherwise be closed after a successful call to one of the exec family of functions, implementations may open an unspecified file for the file descriptor in the new process image. If a standard utility or a conforming application is executed with file descriptor 0 not open for reading or with file descriptor 1 or 2 not open for writing, the environment in which the utility or application is executed shall be deemed non-conforming, and consequently the utility or application might not behave as described in this standard.
Linux seems to do this so file descriptors 0, 1, and 2 will exist after exec. But it's not technically guaranteed.
Std tries to ensure that standard file descriptors are open at startup. There are ways those assumptions may be violated, but they involve non-Rust code (in which case we just blame that), unsafe or libraries that ought to be unsafe.
So the current stance is that the standard descriptors can be assumed to be open, anything else is a bug.
Activity
the8472 commentedon Nov 11, 2021
ijackson commentedon Nov 12, 2021
Wow. Thank you. That is inspired. But also a bit bonkers :-). I'm not sure this is the most ergonomic API for this operation...
ijackson commentedon Nov 12, 2021
(Just looked at the definition of io::Stdout and it's not a ZST.)
sunfishcode commentedon Feb 10, 2022
Could you say more about what the user of such a function would look like? In what situations does one, as a user of std, need a
BorrowedFd<'static>?ids1024 commentedon Jul 8, 2022
It looks like it isn't strictly guaranteed by POSIX that this will always be the case on program start?
Linux seems to do this so file descriptors 0, 1, and 2 will exist after exec. But it's not technically guaranteed.
the8472 commentedon Jul 8, 2022
Std tries to ensure that standard file descriptors are open at startup. There are ways those assumptions may be violated, but they involve non-Rust code (in which case we just blame that),
unsafeor libraries that ought to be unsafe.So the current stance is that the standard descriptors can be assumed to be open, anything else is a bug.
sunfishcode commentedon Jul 8, 2022
With Rust 1.61.0, std now provides
'staticreferences:https://blog.rust-lang.org/2022/05/19/Rust-1.61.0.html#static-handles-for-locked-stdio
When the io_safety feature is available, you can call
as_fd()on these to obtain aBorrowedFd<'static>without anyunsafe.@ijackson Does that meet your needs?
Dylan-DPC commentedon Feb 17, 2023
Closing this to clean up the issue tracker. Since there are alternatives possible already as explained above