use std::os::fd::BorrowedFd;
use rustix::event::{PollFd, PollFlags};
use rustix::fs::{open, Mode, OFlags};
use rustix::io::Errno;
use super::log::Log;
use crate::wait;
fn poll_in<const N: usize>(fds: [Option<BorrowedFd<'_>>; N], block: bool) -> Result<[bool; N], Errno> {
let mut at = [None; N];
let mut polled = Vec::with_capacity(N);
for (slot, fd) in fds.iter().enumerate() {
if let Some(fd) = fd {
at[slot] = Some(polled.len());
polled.push(PollFd::new(fd, PollFlags::IN));
}
}
if block {
wait::poll_ready(&mut polled)?;
} else {
wait::any_ready_now(&mut polled)?;
}
let mut ready = [false; N];
for (slot, index) in at.iter().enumerate() {
ready[slot] = index.is_some_and(|i| !polled[i].revents().is_empty());
}
Ok(ready)
}
pub(super) fn wait_readable<const N: usize>(fds: [Option<BorrowedFd<'_>>; N]) -> Result<[bool; N], Errno> {
poll_in(fds, true)
}
pub(super) fn readable_now<const N: usize>(fds: [BorrowedFd<'_>; N]) -> Result<[bool; N], Errno> {
poll_in(fds.map(Some), false)
}
pub(super) fn replace_stdio(log: &Log) {
let null = match open("/dev/null", OFlags::RDWR | OFlags::CLOEXEC, Mode::empty()) {
Ok(null) => null,
Err(e) => {
log.warn(format_args!(
"cannot open /dev/null: {e}; the shim keeps its standard descriptors"
));
crate::elevation::shim::stderr::line(format_args!(
"cannot open /dev/null: {e}; the shim keeps its standard descriptors"
));
return;
}
};
for (name, replaced) in [
("stdin", rustix::stdio::dup2_stdin(&null)),
("stdout", rustix::stdio::dup2_stdout(&null)),
("stderr", rustix::stdio::dup2_stderr(&null)),
] {
if let Err(e) = replaced {
log.warn(format_args!("cannot replace {name} with /dev/null: {e}"));
debug_assert!(false, "dup2 of /dev/null onto {name}: {e}");
}
}
}
#[cfg(test)]
#[path = "fds_tests.rs"]
mod fds_tests;