use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::fs;
use std::os::unix::fs::FileTypeExt;
use std::os::unix::fs::MetadataExt;
use std::path::Path;
use std::sync::Mutex;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use detcore_model::futex::MmId;
static GUEST_OPENS: Mutex<BTreeMap<(u64, u64), Ends>> = Mutex::new(BTreeMap::new());
static GUEST_MADE: Mutex<BTreeSet<(u64, u64)>> = Mutex::new(BTreeSet::new());
pub(crate) fn note_guest_made(fifo: (u64, u64)) {
GUEST_MADE.lock().unwrap().insert(fifo);
forget_fifo_channel(fifo);
}
type FifoChannelNames = (BTreeMap<(u64, u64), u64>, u64);
static FIFO_CHANNELS: Mutex<FifoChannelNames> = Mutex::new((BTreeMap::new(), 0));
pub(crate) fn fifo_channel(fifo: (u64, u64)) -> u64 {
let mut channels = FIFO_CHANNELS.lock().unwrap();
let (names, next) = &mut *channels;
*names.entry(fifo).or_insert_with(|| {
*next += 1;
*next - 1
})
}
fn forget_fifo_channel(fifo: (u64, u64)) {
FIFO_CHANNELS.lock().unwrap().0.remove(&fifo);
}
pub(crate) fn forget_guest_made(fifo: (u64, u64)) {
forget_fifo_channel(fifo);
if GUEST_MADE.lock().unwrap().remove(&fifo) {
tracing::info!(
"[detcore] a guest removed the name of the FIFO it made; its number is forgotten"
);
}
}
pub(crate) fn is_guest_made(fifo: (u64, u64)) -> bool {
GUEST_MADE.lock().unwrap().contains(&fifo)
}
pub(crate) fn any_guest_made() -> bool {
!GUEST_MADE.lock().unwrap().is_empty()
}
static PENDING_OPENS: Mutex<BTreeMap<(u64, u64), Ends>> = Mutex::new(BTreeMap::new());
static STAND_IN_WRITERS: Mutex<BTreeMap<(u64, u64), fs::File>> = Mutex::new(BTreeMap::new());
pub(crate) fn stand_in_for_waiting_writers(fifo: (u64, u64), path: &std::ffi::OsStr) {
use std::os::unix::fs::OpenOptionsExt;
if pending_opens(fifo).writers == 0 {
return;
}
let mut stand_ins = STAND_IN_WRITERS.lock().unwrap();
if stand_ins.contains_key(&fifo) {
return;
}
if let Ok(file) = fs::OpenOptions::new()
.write(true)
.custom_flags(libc::O_NONBLOCK | libc::O_CLOEXEC)
.open(path)
&& file
.metadata()
.is_ok_and(|meta| (meta.dev(), meta.ino()) == fifo)
{
stand_ins.insert(fifo, file);
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) struct Ends {
pub(crate) readers: u64,
pub(crate) writers: u64,
}
impl Ends {
fn add(&mut self, flags: i32, by: i64) {
let access = flags & libc::O_ACCMODE;
if access != libc::O_WRONLY {
self.readers = self.readers.saturating_add_signed(by);
}
if access != libc::O_RDONLY {
self.writers = self.writers.saturating_add_signed(by);
}
}
pub(crate) fn of(self, peer: FifoPeer) -> u64 {
match peer {
FifoPeer::Reader => self.readers,
FifoPeer::Writer => self.writers,
}
}
}
pub(crate) fn note_guest_open(fifo: (u64, u64), flags: i32) {
GUEST_OPENS
.lock()
.unwrap()
.entry(fifo)
.or_default()
.add(flags, 1);
}
pub(crate) fn guest_opens(fifo: (u64, u64)) -> Ends {
GUEST_OPENS
.lock()
.unwrap()
.get(&fifo)
.copied()
.unwrap_or_default()
}
pub(crate) fn pending_opens(fifo: (u64, u64)) -> Ends {
PENDING_OPENS
.lock()
.unwrap()
.get(&fifo)
.copied()
.unwrap_or_default()
}
pub(crate) struct PendingOpen {
fifo: (u64, u64),
flags: i32,
handle: Option<fs::File>,
_reader: Option<fs::File>,
}
impl PendingOpen {
pub(crate) fn begin(fifo: (u64, u64), path: &std::ffi::OsStr, flags: i32) -> Self {
use std::os::unix::fs::OpenOptionsExt;
let handle = fs::OpenOptions::new()
.read(true)
.custom_flags(libc::O_PATH | libc::O_CLOEXEC)
.open(path)
.ok()
.filter(|file| {
file.metadata()
.is_ok_and(|meta| (meta.dev(), meta.ino()) == fifo)
});
let mut pending = PendingOpen {
fifo,
flags,
handle,
_reader: None,
};
if flags & libc::O_ACCMODE == libc::O_RDONLY {
pending._reader = pending.open_in_tracer(libc::O_RDONLY).ok();
}
PENDING_OPENS
.lock()
.unwrap()
.entry(fifo)
.or_default()
.add(flags, 1);
pending
}
pub(crate) fn guest_path(&self) -> Option<String> {
use std::os::fd::AsRawFd;
let handle = self.handle.as_ref()?;
Some(format!(
"/proc/{}/fd/{}",
std::process::id(),
handle.as_raw_fd()
))
}
pub(crate) fn stand_in_for_waiting_writers(&self) {
if pending_opens(self.fifo).writers == 0 {
return;
}
let mut stand_ins = STAND_IN_WRITERS.lock().unwrap();
if !stand_ins.contains_key(&self.fifo)
&& let Ok(file) = self.open_in_tracer(libc::O_WRONLY)
{
stand_ins.insert(self.fifo, file);
}
}
pub(crate) fn open_in_tracer(&self, access: i32) -> std::io::Result<fs::File> {
use std::os::fd::AsRawFd;
use std::os::unix::fs::OpenOptionsExt;
let handle = self
.handle
.as_ref()
.ok_or_else(|| std::io::Error::from_raw_os_error(libc::EBADF))?;
fs::OpenOptions::new()
.read(access == libc::O_RDONLY)
.write(access == libc::O_WRONLY)
.custom_flags(libc::O_NONBLOCK | libc::O_CLOEXEC)
.open(format!("/proc/self/fd/{}", handle.as_raw_fd()))
}
}
impl Drop for PendingOpen {
fn drop(&mut self) {
let writers_left = PENDING_OPENS
.lock()
.unwrap()
.get_mut(&self.fifo)
.map(|ends| {
ends.add(self.flags, -1);
ends.writers
})
.unwrap_or(0);
if writers_left == 0 {
STAND_IN_WRITERS.lock().unwrap().remove(&self.fifo);
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum FifoPeer {
Writer,
Reader,
}
impl FifoPeer {
fn accepts_flags(self, flags: i32) -> bool {
flags & libc::O_PATH == 0 && self.accepts(flags & libc::O_ACCMODE)
}
fn accepts(self, access_mode: i32) -> bool {
match self {
FifoPeer::Writer => access_mode == libc::O_WRONLY || access_mode == libc::O_RDWR,
FifoPeer::Reader => access_mode == libc::O_RDONLY || access_mode == libc::O_RDWR,
}
}
}
fn fdinfo_flags(table: &Path, fd: &str) -> Option<i32> {
let info = fs::read_to_string(table.join("fdinfo").join(fd)).ok()?;
let flags = info.lines().find_map(|line| line.strip_prefix("flags:"))?;
i32::from_str_radix(flags.trim(), 8).ok()
}
pub(crate) fn guest_descriptors_are_visible(
backend: &reverie::BackendCapabilities,
physical_tid: Option<i32>,
) -> bool {
!backend.tool_shares_guest_descriptor_table
&& crate::procfs_inode::guest_tids_name_host_tasks(backend, physical_tid)
}
pub(crate) fn fifo_at(
backend: &reverie::BackendCapabilities,
physical_tid: Option<i32>,
tid: i32,
dirfd: i32,
path: &Path,
) -> Option<((u64, u64), std::ffi::OsString)> {
if !guest_descriptors_are_visible(backend, physical_tid) {
return None;
}
let base = if path.is_absolute() {
format!("/proc/{tid}/root")
} else if dirfd == libc::AT_FDCWD {
format!("/proc/{tid}/cwd/")
} else {
format!("/proc/{tid}/fd/{dirfd}/")
};
let mut full = std::ffi::OsString::from(base);
full.push(path.as_os_str());
let meta = fs::metadata(&full).ok()?;
meta.file_type()
.is_fifo()
.then(|| ((meta.dev(), meta.ino()), full))
}
pub(crate) fn fifo_of(
backend: &reverie::BackendCapabilities,
physical_tid: Option<i32>,
tid: i32,
fd: i32,
) -> Option<(u64, u64)> {
if !guest_descriptors_are_visible(backend, physical_tid) {
return None;
}
fifo_behind(tid, fd)
}
fn fifo_behind(tid: i32, fd: i32) -> Option<(u64, u64)> {
let meta = fs::metadata(format!("/proc/{tid}/fd/{fd}")).ok()?;
meta.file_type().is_fifo().then(|| (meta.dev(), meta.ino()))
}
pub(crate) fn kernel_nonblocking(tid: i32, fd: i32) -> Option<bool> {
fdinfo_flags(&Path::new("/proc").join(tid.to_string()), &fd.to_string())
.map(|flags| flags & libc::O_NONBLOCK != 0)
}
pub(crate) fn guest_holds_peer(tid: i32, fd: i32, peer: FifoPeer) -> bool {
fifo_behind(tid, fd)
.is_some_and(|fifo| pending_opens(fifo).of(peer) > 0 || scan(fifo, peer, Some((tid, fd))))
}
pub(crate) fn guest_holds_peer_of(fifo: (u64, u64), peer: FifoPeer) -> bool {
scan(fifo, peer, None)
}
fn scan(fifo: (u64, u64), peer: FifoPeer, own: Option<(i32, i32)>) -> bool {
let tracer = std::process::id();
if tracer != 1 {
return false;
}
let proc_root = Path::new("/proc");
let tracer = tracer.to_string();
crate::util::find_in_directory(proc_root, |name| {
let name = name.to_str()?;
(name.bytes().all(|b| b.is_ascii_digit())
&& name != tracer
&& descriptor_tables(&proc_root.join(name))
.iter()
.any(|(table, task)| holds_peer(table, *task, fifo, peer, own)))
.then_some(())
})
.is_ok_and(|found| found.is_some())
}
fn holds_peer(
table: &Path,
task: i32,
(dev, ino): (u64, u64),
peer: FifoPeer,
own: Option<(i32, i32)>,
) -> bool {
let descriptors = table.join("fd");
crate::util::find_in_directory(&descriptors, |name| {
let fd = name.to_str()?.parse::<i32>().ok()?;
(fs::metadata(descriptors.join(name))
.is_ok_and(|meta| meta.file_type().is_fifo() && meta.dev() == dev && meta.ino() == ino)
&& fdinfo_flags(table, &fd.to_string()).is_some_and(|flags| peer.accepts_flags(flags))
&& !own.is_some_and(|(tid, own_fd)| same_description(tid, own_fd, task, fd)))
.then_some(())
})
.is_ok_and(|found| found.is_some())
}
fn descriptor_tables(process: &Path) -> Vec<(std::path::PathBuf, i32)> {
let task_root = process.join("task");
let mut tasks: Vec<(std::path::PathBuf, i32)> = Vec::new();
let _ = crate::util::find_in_directory(&task_root, |name| {
if let Some(id) = name.to_str().and_then(|id| id.parse::<i32>().ok()) {
tasks.push((task_root.join(name), id));
}
None::<()>
});
tasks.sort_by_key(|(_, id)| *id);
let mut tables: Vec<(std::path::PathBuf, i32)> = Vec::new();
for (dir, id) in tasks {
let fd_directory = dir.join("fd");
let listed = match crate::util::find_in_directory(&fd_directory, |_| Some(())) {
Ok(found) => found.is_some(),
Err(_) => crate::util::raw_stat(&fd_directory).is_ok(),
};
if listed && !tables.iter().any(|(_, seen)| same_table(*seen, id)) {
tables.push((dir, id));
}
}
tables
}
fn same_table(a: i32, b: i32) -> bool {
const KCMP_FILES: libc::c_int = 2;
let order = unsafe {
libc::syscall(
libc::SYS_kcmp,
a as libc::pid_t,
b as libc::pid_t,
KCMP_FILES,
0 as libc::c_ulong,
0 as libc::c_ulong,
)
};
order == 0
}
fn same_description(pid1: i32, fd1: i32, pid2: i32, fd2: i32) -> bool {
const KCMP_FILE: libc::c_int = 0;
let order = unsafe {
libc::syscall(
libc::SYS_kcmp,
pid1 as libc::pid_t,
pid2 as libc::pid_t,
KCMP_FILE,
fd1 as libc::c_ulong,
fd2 as libc::c_ulong,
)
};
!(1..=3).contains(&order)
}
pub(crate) fn free_staging_address(tid: i32) -> Option<usize> {
pick_staging_address(&fs::read_to_string(format!("/proc/{tid}/maps")).ok()?)
}
fn pick_staging_address(maps: &str) -> Option<usize> {
const PAGE: usize = 4096;
let mut ranges: Vec<(usize, usize, bool)> = maps
.lines()
.filter_map(|line| {
let (range, rest) = line.split_once(' ')?;
let (start, end) = range.split_once('-')?;
Some((
usize::from_str_radix(start, 16).ok()?,
usize::from_str_radix(end, 16).ok()?,
rest.trim_end().ends_with("[stack]"),
))
})
.collect();
ranges.sort_unstable();
let stack = ranges
.iter()
.find(|(_, _, is_stack)| *is_stack)
.map(|(start, _, _)| *start)?;
ranges
.windows(2)
.rev()
.find(|pair| {
let ((_, below_end, _), (start, _, is_stack)) = (pair[0], pair[1]);
start <= stack && !is_stack && start.saturating_sub(below_end) >= 3 * PAGE
})
.map(|pair| pair[1].0 - 2 * PAGE)
}
static ORPHANED_PAGES: Mutex<Vec<OrphanedPage>> = Mutex::new(Vec::new());
static ANY_ORPHANED_PAGE: AtomicBool = AtomicBool::new(false);
pub(crate) const STAGED_PAGE_LENGTH: usize = 64;
#[derive(Clone, Copy)]
struct OrphanedPage {
mm: MmId,
address: usize,
contents: [u8; STAGED_PAGE_LENGTH],
}
pub(crate) struct StagedPage {
page: OrphanedPage,
unmapped: bool,
}
impl StagedPage {
pub(crate) fn new(mm: MmId, address: usize, contents: [u8; STAGED_PAGE_LENGTH]) -> Self {
StagedPage {
page: OrphanedPage {
mm,
address,
contents,
},
unmapped: false,
}
}
pub(crate) fn unmapped(mut self) {
self.unmapped = true;
}
pub(crate) fn address(&self) -> usize {
self.page.address
}
pub(crate) fn contents(&self) -> &[u8; STAGED_PAGE_LENGTH] {
&self.page.contents
}
}
impl Drop for StagedPage {
fn drop(&mut self) {
if !self.unmapped {
leave_orphaned_page(self.page);
}
}
}
fn leave_orphaned_page(page: OrphanedPage) {
ORPHANED_PAGES.lock().unwrap().push(page);
ANY_ORPHANED_PAGE.store(true, Ordering::SeqCst);
}
pub(crate) fn take_orphaned_pages(mm: MmId) -> Vec<StagedPage> {
if !ANY_ORPHANED_PAGE.load(Ordering::SeqCst) {
return Vec::new();
}
let mut orphaned = ORPHANED_PAGES.lock().unwrap();
let (taken, kept): (Vec<_>, Vec<_>) = orphaned.drain(..).partition(|page| page.mm == mm);
*orphaned = kept;
if orphaned.is_empty() {
ANY_ORPHANED_PAGE.store(false, Ordering::SeqCst);
}
taken
.into_iter()
.map(|page| StagedPage {
page,
unmapped: false,
})
.collect()
}
pub(crate) fn is_lone_staging_mapping(tid: i32, address: usize) -> bool {
fs::read_to_string(format!("/proc/{tid}/maps"))
.is_ok_and(|maps| maps_have_lone_staging_mapping(&maps, address))
}
fn maps_have_lone_staging_mapping(maps: &str, address: usize) -> bool {
let wanted = format!(
"{:08x}-{:08x} rw-p 00000000 00:00 0",
address,
address + 4096
);
maps.lines().any(|line| line.trim_end() == wanted)
}
#[cfg(test)]
mod tests {
use super::maps_have_lone_staging_mapping;
use super::pick_staging_address;
#[test]
fn only_a_lone_anonymous_page_is_taken_for_a_staging_page() {
let lone = "7ffff7fbe000-7ffff7fbf000 rw-p 00000000 00:00 0 \n";
assert!(maps_have_lone_staging_mapping(lone, 0x7ffff7fbe000));
let larger = "7ffff7fbe000-7ffff7fc0000 rw-p 00000000 00:00 0\n";
assert!(!maps_have_lone_staging_mapping(larger, 0x7ffff7fbe000));
let covering = "7ffff7fbd000-7ffff7fbf000 rw-p 00000000 00:00 0\n";
assert!(!maps_have_lone_staging_mapping(covering, 0x7ffff7fbe000));
let file = "7ffff7fbe000-7ffff7fbf000 rw-p 00000000 00:20 7 /tmp/f\n";
assert!(!maps_have_lone_staging_mapping(file, 0x7ffff7fbe000));
let read_only = "7ffff7fbe000-7ffff7fbf000 r--p 00000000 00:00 0\n";
assert!(!maps_have_lone_staging_mapping(read_only, 0x7ffff7fbe000));
}
#[test]
fn a_staging_page_goes_below_the_highest_mapping_with_room() {
let maps = "\
00400000-00401000 r--p 00000000 00:20 1 /bin/guest
7ffff7fc0000-7ffff7fc4000 r--p 00000000 00:00 0 [vvar]
7ffff7fc4000-7ffff7fc6000 r-xp 00000000 00:00 0 [vdso]
7ffff7fc6000-7ffff7fc8000 r--p 00000000 00:20 2 /lib/ld.so
7ffffffde000-7ffffffff000 rw-p 00000000 00:00 0 [stack]
ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0 [vsyscall]
";
assert_eq!(pick_staging_address(maps), Some(0x7ffff7fbe000));
let tight = maps.replace(
"00400000-00401000 r--p 00000000 00:20 1 /bin/guest",
"00400000-00401000 r--p 00000000 00:20 1 /bin/guest\n7ffff7fbe000-7ffff7fc0000 rw-p 00000000 00:00 0",
);
assert_eq!(pick_staging_address(&tight), Some(0x7ffff7fbc000));
assert_eq!(
pick_staging_address("00400000-00401000 r--p 0 00:20 1 /bin/guest\n"),
None
);
}
use std::fs::File;
use std::fs::OpenOptions;
use std::os::unix::fs::OpenOptionsExt;
use super::*;
#[test]
fn guest_descriptors_are_visible_only_under_a_host_task_tracer() {
use reverie::BackendCapabilities;
assert!(guest_descriptors_are_visible(
&BackendCapabilities::PTRACE,
None
));
assert!(guest_descriptors_are_visible(
&BackendCapabilities::LITEINST_IN_GUEST,
None
));
assert!(!guest_descriptors_are_visible(
&BackendCapabilities::KVM,
None
));
assert!(!guest_descriptors_are_visible(
&BackendCapabilities::DBT,
Some(4_321)
));
assert!(!guest_descriptors_are_visible(
&BackendCapabilities::SABRE,
None
));
assert!(!guest_descriptors_are_visible(
&BackendCapabilities::PTRACE,
Some(4_321)
));
}
#[test]
fn a_fifo_is_seen_only_where_guest_descriptors_are_visible() {
use reverie::BackendCapabilities;
let dir = tempfile::tempdir().unwrap();
let path = fifo_in(dir.path());
let end = OpenOptions::new()
.read(true)
.write(true)
.open(&path)
.unwrap();
let fd = std::os::fd::AsRawFd::as_raw_fd(&end);
let tid = nix::unistd::gettid().as_raw();
assert!(fifo_of(&BackendCapabilities::PTRACE, None, tid, fd).is_some());
assert!(fifo_of(&BackendCapabilities::KVM, None, tid, fd).is_none());
assert!(fifo_of(&BackendCapabilities::DBT, Some(tid), tid, fd).is_none());
}
fn fifo_in(dir: &Path) -> std::path::PathBuf {
let path = dir.join("fifo");
nix::unistd::mkfifo(&path, nix::sys::stat::Mode::S_IRWXU).unwrap();
path
}
fn identity(file: &File) -> (u64, u64) {
let meta = file.metadata().unwrap();
(meta.dev(), meta.ino())
}
#[test]
fn a_peer_is_a_descriptor_for_the_same_fifo_with_the_other_access() {
let dir = tempfile::tempdir().unwrap();
let path = fifo_in(dir.path());
let me = std::process::id().to_string();
let proc_root = Path::new("/proc");
let reader = OpenOptions::new()
.read(true)
.custom_flags(libc::O_NONBLOCK)
.open(&path)
.unwrap();
let fifo = identity(&reader);
assert!(holds_peer(
&proc_root.join(&me),
me.parse().unwrap(),
fifo,
FifoPeer::Reader,
None
));
assert!(
!holds_peer(
&proc_root.join(&me),
me.parse().unwrap(),
fifo,
FifoPeer::Writer,
None
),
"a reader is not the writer a read waits for"
);
let writer = OpenOptions::new().write(true).open(&path).unwrap();
assert!(holds_peer(
&proc_root.join(&me),
me.parse().unwrap(),
fifo,
FifoPeer::Writer,
None
));
let other_dir = tempfile::tempdir().unwrap();
let other = fifo_in(other_dir.path());
let _other_reader = OpenOptions::new()
.read(true)
.custom_flags(libc::O_NONBLOCK)
.open(&other)
.unwrap();
let _other_writer = OpenOptions::new().write(true).open(&other).unwrap();
drop(writer);
assert!(
!holds_peer(
&proc_root.join(&me),
me.parse().unwrap(),
fifo,
FifoPeer::Writer,
None
),
"the other FIFO's writer must not count"
);
}
#[test]
fn a_read_write_descriptor_is_both_ends() {
let dir = tempfile::tempdir().unwrap();
let path = fifo_in(dir.path());
let me = std::process::id().to_string();
let both = OpenOptions::new()
.read(true)
.write(true)
.open(&path)
.unwrap();
let fifo = identity(&both);
assert!(holds_peer(
&Path::new("/proc").join(&me),
me.parse().unwrap(),
fifo,
FifoPeer::Writer,
None
));
assert!(holds_peer(
&Path::new("/proc").join(&me),
me.parse().unwrap(),
fifo,
FifoPeer::Reader,
None
));
}
#[test]
fn the_callers_own_description_is_not_its_peer() {
let dir = tempfile::tempdir().unwrap();
let path = fifo_in(dir.path());
let me = std::process::id() as i32;
let both = OpenOptions::new()
.read(true)
.write(true)
.open(&path)
.unwrap();
let fd = std::os::fd::AsRawFd::as_raw_fd(&both);
let fifo = identity(&both);
let own = Some((me, fd));
let proc_root = Path::new("/proc");
assert!(
!holds_peer(
&proc_root.join(me.to_string()),
me,
fifo,
FifoPeer::Writer,
own
),
"a read-write description must not be its own writer"
);
let alias = both.try_clone().unwrap();
assert!(
!holds_peer(
&proc_root.join(me.to_string()),
me,
fifo,
FifoPeer::Writer,
own
),
"a dup of the caller's description must not count either"
);
let writer = OpenOptions::new().write(true).open(&path).unwrap();
assert!(
holds_peer(
&proc_root.join(me.to_string()),
me,
fifo,
FifoPeer::Writer,
own
),
"another description open for writing is a peer"
);
drop((alias, writer));
}
#[test]
fn an_o_path_handle_is_neither_end() {
let dir = tempfile::tempdir().unwrap();
let path = fifo_in(dir.path());
let me = std::process::id().to_string();
let table = Path::new("/proc").join(&me);
let task: i32 = me.parse().unwrap();
let observer = OpenOptions::new()
.read(true)
.custom_flags(libc::O_PATH)
.open(&path)
.unwrap();
let fifo = identity(&observer);
assert!(
!holds_peer(&table, task, fifo, FifoPeer::Reader, None),
"an O_PATH handle must not count as a reader"
);
assert!(!holds_peer(&table, task, fifo, FifoPeer::Writer, None));
assert!(FifoPeer::Reader.accepts_flags(libc::O_RDONLY));
assert!(!FifoPeer::Reader.accepts_flags(libc::O_PATH | libc::O_CLOEXEC));
}
#[test]
fn fdinfo_flags_are_the_kernel_status_flags() {
let dir = tempfile::tempdir().unwrap();
let path = fifo_in(dir.path());
let reader = OpenOptions::new()
.read(true)
.custom_flags(libc::O_NONBLOCK)
.open(&path)
.unwrap();
let fd = std::os::fd::AsRawFd::as_raw_fd(&reader);
let pid = std::process::id() as i32;
assert!(fifo_of(&reverie::BackendCapabilities::PTRACE, None, pid, fd).is_some());
assert_eq!(kernel_nonblocking(pid, fd), Some(true));
unsafe { libc::fcntl(fd, libc::F_SETFL, 0) };
assert_eq!(kernel_nonblocking(pid, fd), Some(false));
}
}