1b8a62b91Sopenharmony_ci//! Functions returning the stdio file descriptors. 2b8a62b91Sopenharmony_ci//! 3b8a62b91Sopenharmony_ci//! # Safety 4b8a62b91Sopenharmony_ci//! 5b8a62b91Sopenharmony_ci//! These access the file descriptors by absolute index value, and nothing 6b8a62b91Sopenharmony_ci//! prevents them from being closed and reused. They should only be used in 7b8a62b91Sopenharmony_ci//! `main` or other situations where one is in control of the process' 8b8a62b91Sopenharmony_ci//! stdio streams. 9b8a62b91Sopenharmony_ci#![allow(unsafe_code)] 10b8a62b91Sopenharmony_ci 11b8a62b91Sopenharmony_ciuse crate::backend; 12b8a62b91Sopenharmony_ciuse crate::fd::OwnedFd; 13b8a62b91Sopenharmony_ciuse backend::fd::{BorrowedFd, FromRawFd, RawFd}; 14b8a62b91Sopenharmony_ci 15b8a62b91Sopenharmony_ci/// `STDIN_FILENO`—Standard input, borrowed. 16b8a62b91Sopenharmony_ci/// 17b8a62b91Sopenharmony_ci/// # Safety 18b8a62b91Sopenharmony_ci/// 19b8a62b91Sopenharmony_ci/// This function must be called from code which knows how the process' 20b8a62b91Sopenharmony_ci/// standard input is being used. Often, this will be the `main` function or 21b8a62b91Sopenharmony_ci/// code that knows its relationship with the `main` function. 22b8a62b91Sopenharmony_ci/// 23b8a62b91Sopenharmony_ci/// The stdin file descriptor can be closed, potentially on other threads, in 24b8a62b91Sopenharmony_ci/// which case the file descriptor index value could be dynamically reused for 25b8a62b91Sopenharmony_ci/// other purposes, potentially on different threads. 26b8a62b91Sopenharmony_ci/// 27b8a62b91Sopenharmony_ci/// # Other hazards 28b8a62b91Sopenharmony_ci/// 29b8a62b91Sopenharmony_ci/// Stdin could be redirected from arbitrary input sources, and unless one 30b8a62b91Sopenharmony_ci/// knows how the process' standard input is being used, one could consume 31b8a62b91Sopenharmony_ci/// bytes that are expected to be consumed by other parts of the process. 32b8a62b91Sopenharmony_ci/// 33b8a62b91Sopenharmony_ci/// # References 34b8a62b91Sopenharmony_ci/// - [POSIX] 35b8a62b91Sopenharmony_ci/// - [Linux] 36b8a62b91Sopenharmony_ci/// 37b8a62b91Sopenharmony_ci/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/stdin.html 38b8a62b91Sopenharmony_ci/// [Linux]: https://man7.org/linux/man-pages/man3/stdin.3.html 39b8a62b91Sopenharmony_ci#[doc(alias = "STDIN_FILENO")] 40b8a62b91Sopenharmony_ci#[inline] 41b8a62b91Sopenharmony_cipub const unsafe fn stdin() -> BorrowedFd<'static> { 42b8a62b91Sopenharmony_ci BorrowedFd::borrow_raw(backend::io::types::STDIN_FILENO as RawFd) 43b8a62b91Sopenharmony_ci} 44b8a62b91Sopenharmony_ci 45b8a62b91Sopenharmony_ci/// `STDIN_FILENO`—Standard input, owned. 46b8a62b91Sopenharmony_ci/// 47b8a62b91Sopenharmony_ci/// This is similar to [`stdin`], however it returns an `OwnedFd` which closes 48b8a62b91Sopenharmony_ci/// standard input when it is dropped. 49b8a62b91Sopenharmony_ci/// 50b8a62b91Sopenharmony_ci/// # Safety 51b8a62b91Sopenharmony_ci/// 52b8a62b91Sopenharmony_ci/// This is unsafe for the same reasons as [`stdin`]. 53b8a62b91Sopenharmony_ci/// 54b8a62b91Sopenharmony_ci/// # Other hazards 55b8a62b91Sopenharmony_ci/// 56b8a62b91Sopenharmony_ci/// This has the same hazards as [`stdin`]. 57b8a62b91Sopenharmony_ci/// 58b8a62b91Sopenharmony_ci/// And, when the `OwnedFd` is dropped, subsequent newly created file 59b8a62b91Sopenharmony_ci/// descriptors may unknowingly reuse the stdin file descriptor number, which 60b8a62b91Sopenharmony_ci/// may break common assumptions, so it should typically only be dropped at the 61b8a62b91Sopenharmony_ci/// end of a program when no more file descriptors will be created. 62b8a62b91Sopenharmony_ci/// 63b8a62b91Sopenharmony_ci/// # References 64b8a62b91Sopenharmony_ci/// - [POSIX] 65b8a62b91Sopenharmony_ci/// - [Linux] 66b8a62b91Sopenharmony_ci/// 67b8a62b91Sopenharmony_ci/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/stdin.html 68b8a62b91Sopenharmony_ci/// [Linux]: https://man7.org/linux/man-pages/man3/stdin.3.html 69b8a62b91Sopenharmony_ci#[doc(alias = "STDIN_FILENO")] 70b8a62b91Sopenharmony_ci#[inline] 71b8a62b91Sopenharmony_cipub unsafe fn take_stdin() -> OwnedFd { 72b8a62b91Sopenharmony_ci backend::fd::OwnedFd::from_raw_fd(backend::io::types::STDIN_FILENO as RawFd) 73b8a62b91Sopenharmony_ci} 74b8a62b91Sopenharmony_ci 75b8a62b91Sopenharmony_ci/// `STDOUT_FILENO`—Standard output, borrowed. 76b8a62b91Sopenharmony_ci/// 77b8a62b91Sopenharmony_ci/// # Safety 78b8a62b91Sopenharmony_ci/// 79b8a62b91Sopenharmony_ci/// This function must be called from code which knows how the process' 80b8a62b91Sopenharmony_ci/// standard output is being used. Often, this will be the `main` function or 81b8a62b91Sopenharmony_ci/// code that knows its relationship with the `main` function. 82b8a62b91Sopenharmony_ci/// 83b8a62b91Sopenharmony_ci/// The stdout file descriptor can be closed, potentially on other threads, in 84b8a62b91Sopenharmony_ci/// which case the file descriptor index value could be dynamically reused for 85b8a62b91Sopenharmony_ci/// other purposes, potentially on different threads. 86b8a62b91Sopenharmony_ci/// 87b8a62b91Sopenharmony_ci/// # Other hazards 88b8a62b91Sopenharmony_ci/// 89b8a62b91Sopenharmony_ci/// Stdout could be redirected to arbitrary output sinks, and unless one 90b8a62b91Sopenharmony_ci/// knows how the process' standard output is being used, one could 91b8a62b91Sopenharmony_ci/// unexpectedly inject bytes into a stream being written by another part of 92b8a62b91Sopenharmony_ci/// the process. 93b8a62b91Sopenharmony_ci/// 94b8a62b91Sopenharmony_ci/// # References 95b8a62b91Sopenharmony_ci/// - [POSIX] 96b8a62b91Sopenharmony_ci/// - [Linux] 97b8a62b91Sopenharmony_ci/// 98b8a62b91Sopenharmony_ci/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/stdout.html 99b8a62b91Sopenharmony_ci/// [Linux]: https://man7.org/linux/man-pages/man3/stdout.3.html 100b8a62b91Sopenharmony_ci#[doc(alias = "STDOUT_FILENO")] 101b8a62b91Sopenharmony_ci#[inline] 102b8a62b91Sopenharmony_cipub const unsafe fn stdout() -> BorrowedFd<'static> { 103b8a62b91Sopenharmony_ci BorrowedFd::borrow_raw(backend::io::types::STDOUT_FILENO as RawFd) 104b8a62b91Sopenharmony_ci} 105b8a62b91Sopenharmony_ci 106b8a62b91Sopenharmony_ci/// `STDOUT_FILENO`—Standard output, owned. 107b8a62b91Sopenharmony_ci/// 108b8a62b91Sopenharmony_ci/// This is similar to [`stdout`], however it returns an `OwnedFd` which closes 109b8a62b91Sopenharmony_ci/// standard output when it is dropped. 110b8a62b91Sopenharmony_ci/// 111b8a62b91Sopenharmony_ci/// # Safety 112b8a62b91Sopenharmony_ci/// 113b8a62b91Sopenharmony_ci/// This is unsafe for the same reasons as [`stdout`]. 114b8a62b91Sopenharmony_ci/// 115b8a62b91Sopenharmony_ci/// # Other hazards 116b8a62b91Sopenharmony_ci/// 117b8a62b91Sopenharmony_ci/// This has the same hazards as [`stdout`]. 118b8a62b91Sopenharmony_ci/// 119b8a62b91Sopenharmony_ci/// And, when the `OwnedFd` is dropped, subsequent newly created file 120b8a62b91Sopenharmony_ci/// descriptors may unknowingly reuse the stdout file descriptor number, which 121b8a62b91Sopenharmony_ci/// may break common assumptions, so it should typically only be dropped at the 122b8a62b91Sopenharmony_ci/// end of a program when no more file descriptors will be created. 123b8a62b91Sopenharmony_ci/// 124b8a62b91Sopenharmony_ci/// # References 125b8a62b91Sopenharmony_ci/// - [POSIX] 126b8a62b91Sopenharmony_ci/// - [Linux] 127b8a62b91Sopenharmony_ci/// 128b8a62b91Sopenharmony_ci/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/stdout.html 129b8a62b91Sopenharmony_ci/// [Linux]: https://man7.org/linux/man-pages/man3/stdout.3.html 130b8a62b91Sopenharmony_ci#[doc(alias = "STDOUT_FILENO")] 131b8a62b91Sopenharmony_ci#[inline] 132b8a62b91Sopenharmony_cipub unsafe fn take_stdout() -> OwnedFd { 133b8a62b91Sopenharmony_ci backend::fd::OwnedFd::from_raw_fd(backend::io::types::STDOUT_FILENO as RawFd) 134b8a62b91Sopenharmony_ci} 135b8a62b91Sopenharmony_ci 136b8a62b91Sopenharmony_ci/// `STDERR_FILENO`—Standard error, borrowed. 137b8a62b91Sopenharmony_ci/// 138b8a62b91Sopenharmony_ci/// # Safety 139b8a62b91Sopenharmony_ci/// 140b8a62b91Sopenharmony_ci/// This function must be called from code which knows how the process' 141b8a62b91Sopenharmony_ci/// standard error is being used. Often, this will be the `main` function or 142b8a62b91Sopenharmony_ci/// code that knows its relationship with the `main` function. 143b8a62b91Sopenharmony_ci/// 144b8a62b91Sopenharmony_ci/// The stderr file descriptor can be closed, potentially on other threads, in 145b8a62b91Sopenharmony_ci/// which case the file descriptor index value could be dynamically reused for 146b8a62b91Sopenharmony_ci/// other purposes, potentially on different threads. 147b8a62b91Sopenharmony_ci/// 148b8a62b91Sopenharmony_ci/// # Other hazards 149b8a62b91Sopenharmony_ci/// 150b8a62b91Sopenharmony_ci/// Stderr could be redirected to arbitrary output sinks, and unless one 151b8a62b91Sopenharmony_ci/// knows how the process' standard error is being used, one could unexpectedly 152b8a62b91Sopenharmony_ci/// inject bytes into a stream being written by another part of the process. 153b8a62b91Sopenharmony_ci/// 154b8a62b91Sopenharmony_ci/// # References 155b8a62b91Sopenharmony_ci/// - [POSIX] 156b8a62b91Sopenharmony_ci/// - [Linux] 157b8a62b91Sopenharmony_ci/// 158b8a62b91Sopenharmony_ci/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/stderr.html 159b8a62b91Sopenharmony_ci/// [Linux]: https://man7.org/linux/man-pages/man3/stderr.3.html 160b8a62b91Sopenharmony_ci#[doc(alias = "STDERR_FILENO")] 161b8a62b91Sopenharmony_ci#[inline] 162b8a62b91Sopenharmony_cipub const unsafe fn stderr() -> BorrowedFd<'static> { 163b8a62b91Sopenharmony_ci BorrowedFd::borrow_raw(backend::io::types::STDERR_FILENO as RawFd) 164b8a62b91Sopenharmony_ci} 165b8a62b91Sopenharmony_ci 166b8a62b91Sopenharmony_ci/// `STDERR_FILENO`—Standard error, owned. 167b8a62b91Sopenharmony_ci/// 168b8a62b91Sopenharmony_ci/// This is similar to [`stdout`], however it returns an `OwnedFd` which closes 169b8a62b91Sopenharmony_ci/// standard output when it is dropped. 170b8a62b91Sopenharmony_ci/// 171b8a62b91Sopenharmony_ci/// # Safety 172b8a62b91Sopenharmony_ci/// 173b8a62b91Sopenharmony_ci/// This is unsafe for the same reasons as [`stderr`]. 174b8a62b91Sopenharmony_ci/// 175b8a62b91Sopenharmony_ci/// # Other hazards 176b8a62b91Sopenharmony_ci/// 177b8a62b91Sopenharmony_ci/// This has the same hazards as [`stderr`]. 178b8a62b91Sopenharmony_ci/// 179b8a62b91Sopenharmony_ci/// And, when the `OwnedFd` is dropped, subsequent newly created file 180b8a62b91Sopenharmony_ci/// descriptors may unknowingly reuse the stderr file descriptor number, which 181b8a62b91Sopenharmony_ci/// may break common assumptions, so it should typically only be dropped at the 182b8a62b91Sopenharmony_ci/// end of a program when no more file descriptors will be created. 183b8a62b91Sopenharmony_ci/// 184b8a62b91Sopenharmony_ci/// # References 185b8a62b91Sopenharmony_ci/// - [POSIX] 186b8a62b91Sopenharmony_ci/// - [Linux] 187b8a62b91Sopenharmony_ci/// 188b8a62b91Sopenharmony_ci/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/stderr.html 189b8a62b91Sopenharmony_ci/// [Linux]: https://man7.org/linux/man-pages/man3/stderr.3.html 190b8a62b91Sopenharmony_ci#[doc(alias = "STDERR_FILENO")] 191b8a62b91Sopenharmony_ci#[inline] 192b8a62b91Sopenharmony_cipub unsafe fn take_stderr() -> OwnedFd { 193b8a62b91Sopenharmony_ci backend::fd::OwnedFd::from_raw_fd(backend::io::types::STDERR_FILENO as RawFd) 194b8a62b91Sopenharmony_ci} 195b8a62b91Sopenharmony_ci 196b8a62b91Sopenharmony_ci/// `STDIN_FILENO`—Standard input, raw. 197b8a62b91Sopenharmony_ci/// 198b8a62b91Sopenharmony_ci/// This is similar to [`stdin`], however it returns a `RawFd`. 199b8a62b91Sopenharmony_ci/// 200b8a62b91Sopenharmony_ci/// # Other hazards 201b8a62b91Sopenharmony_ci/// 202b8a62b91Sopenharmony_ci/// This has the same hazards as [`stdin`]. 203b8a62b91Sopenharmony_ci/// 204b8a62b91Sopenharmony_ci/// # References 205b8a62b91Sopenharmony_ci/// - [POSIX] 206b8a62b91Sopenharmony_ci/// - [Linux] 207b8a62b91Sopenharmony_ci/// 208b8a62b91Sopenharmony_ci/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/stdin.html 209b8a62b91Sopenharmony_ci/// [Linux]: https://man7.org/linux/man-pages/man3/stdin.3.html 210b8a62b91Sopenharmony_ci#[doc(alias = "STDIN_FILENO")] 211b8a62b91Sopenharmony_ci#[inline] 212b8a62b91Sopenharmony_cipub const fn raw_stdin() -> RawFd { 213b8a62b91Sopenharmony_ci backend::io::types::STDIN_FILENO as RawFd 214b8a62b91Sopenharmony_ci} 215b8a62b91Sopenharmony_ci 216b8a62b91Sopenharmony_ci/// `STDOUT_FILENO`—Standard output, raw. 217b8a62b91Sopenharmony_ci/// 218b8a62b91Sopenharmony_ci/// This is similar to [`stdout`], however it returns a `RawFd`. 219b8a62b91Sopenharmony_ci/// 220b8a62b91Sopenharmony_ci/// # Other hazards 221b8a62b91Sopenharmony_ci/// 222b8a62b91Sopenharmony_ci/// This has the same hazards as [`stdout`]. 223b8a62b91Sopenharmony_ci/// 224b8a62b91Sopenharmony_ci/// # References 225b8a62b91Sopenharmony_ci/// - [POSIX] 226b8a62b91Sopenharmony_ci/// - [Linux] 227b8a62b91Sopenharmony_ci/// 228b8a62b91Sopenharmony_ci/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/stdout.html 229b8a62b91Sopenharmony_ci/// [Linux]: https://man7.org/linux/man-pages/man3/stdout.3.html 230b8a62b91Sopenharmony_ci#[doc(alias = "STDOUT_FILENO")] 231b8a62b91Sopenharmony_ci#[inline] 232b8a62b91Sopenharmony_cipub const fn raw_stdout() -> RawFd { 233b8a62b91Sopenharmony_ci backend::io::types::STDOUT_FILENO as RawFd 234b8a62b91Sopenharmony_ci} 235b8a62b91Sopenharmony_ci 236b8a62b91Sopenharmony_ci/// `STDERR_FILENO`—Standard error, raw. 237b8a62b91Sopenharmony_ci/// 238b8a62b91Sopenharmony_ci/// This is similar to [`stderr`], however it returns a `RawFd`. 239b8a62b91Sopenharmony_ci/// 240b8a62b91Sopenharmony_ci/// # Other hazards 241b8a62b91Sopenharmony_ci/// 242b8a62b91Sopenharmony_ci/// This has the same hazards as [`stderr`]. 243b8a62b91Sopenharmony_ci/// 244b8a62b91Sopenharmony_ci/// # References 245b8a62b91Sopenharmony_ci/// - [POSIX] 246b8a62b91Sopenharmony_ci/// - [Linux] 247b8a62b91Sopenharmony_ci/// 248b8a62b91Sopenharmony_ci/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/stderr.html 249b8a62b91Sopenharmony_ci/// [Linux]: https://man7.org/linux/man-pages/man3/stderr.3.html 250b8a62b91Sopenharmony_ci#[doc(alias = "STDERR_FILENO")] 251b8a62b91Sopenharmony_ci#[inline] 252b8a62b91Sopenharmony_cipub const fn raw_stderr() -> RawFd { 253b8a62b91Sopenharmony_ci backend::io::types::STDERR_FILENO as RawFd 254b8a62b91Sopenharmony_ci} 255