use std::os::fd::AsFd;
use crate::containment::cgroup::test_support::{alone, block_on, childs_copy, fork_running, pidfd_of, reap};
use crate::containment::cgroup::PlacementReport;
#[cfg(target_os = "linux")]
#[skuld::test]
fn placement_report_crosses_fork_with_the_childs_errno() {
use std::os::unix::process::CommandExt;
let fresh = crate::containment::cgroup::ReportChannel::new().expect("open a report channel");
assert_eq!(
fresh.report_for_test(),
PlacementReport::NotReported,
"a fresh channel must report nothing, not a fabricated success"
);
let mut channel = crate::containment::cgroup::ReportChannel::new().expect("open the report channel");
let slot = channel.slot();
let mut cmd = std::process::Command::new("/bin/true");
unsafe {
cmd.pre_exec(move || {
_ = crate::containment::cgroup::place_self_in_cgroup_pre_exec(-1, slot);
Ok(())
});
}
let mut child = crate::test_spawn::spawn(&mut cmd).expect("spawn");
let report = channel.wait(pidfd_of(child.id()).as_fd());
let status = child.wait().expect("wait");
assert!(status.success(), "the failed placement must not abort the spawn");
assert_eq!(
report,
PlacementReport::WriteFailed(libc::EBADF),
"the child's own errno must reach the parent verbatim"
);
}
#[cfg(target_os = "linux")]
#[skuld::test]
fn placement_report_records_a_successful_write() {
use std::os::unix::process::CommandExt;
let mut channel = crate::containment::cgroup::ReportChannel::new().expect("open the report channel");
let slot = channel.slot();
let sink = std::fs::OpenOptions::new()
.write(true)
.open("/dev/null")
.expect("open /dev/null");
let fd = std::os::fd::IntoRawFd::into_raw_fd(sink);
let mut cmd = std::process::Command::new("/bin/true");
unsafe {
cmd.pre_exec(move || {
_ = crate::containment::cgroup::place_self_in_cgroup_pre_exec(fd, slot);
Ok(())
});
}
let mut child = crate::test_spawn::spawn(&mut cmd).expect("spawn");
assert_eq!(channel.wait(pidfd_of(child.id()).as_fd()), PlacementReport::Placed);
child.wait().expect("wait");
unsafe { libc::close(fd) };
}
#[cfg(target_os = "linux")]
#[skuld::test]
fn report_channel_wait_returns_a_report_written_after_it_was_called() {
use std::io::Write;
use std::os::fd::AsRawFd;
let mut channel = crate::containment::cgroup::ReportChannel::new().expect("open the report channel");
let slot = channel.slot();
let (gate_read, mut gate_write) = std::io::pipe().expect("open the gate");
let gate = gate_read.as_raw_fd();
let guard = fork_running(move || {
block_on(gate);
_ = unsafe { slot.send_report(crate::containment::cgroup::REPORT_PLACED) };
});
let pid = guard.pid();
let pidfd = pidfd_of(pid);
let (polling_tx, polling_rx) = std::sync::mpsc::channel();
let waiter = std::thread::spawn(move || {
crate::containment::cgroup::fault::set_wait_polling_notifier(polling_tx);
channel.wait(pidfd.as_fd())
});
polling_rx.recv().expect("the wait reaches its poll with nothing sent");
gate_write.write_all(b"x").expect("release the child");
assert_eq!(waiter.join().expect("the waiting thread"), PlacementReport::Placed);
reap(guard.defuse());
}
#[cfg(target_os = "linux")]
#[skuld::test]
fn report_channel_wait_ends_at_the_childs_exit_while_another_process_holds_the_childs_end() {
use std::io::Write;
use std::os::fd::AsRawFd;
let mut channel = crate::containment::cgroup::ReportChannel::new().expect("open the report channel");
let (gate_read, mut gate_write) = std::io::pipe().expect("open the gate");
let gate = gate_read.as_raw_fd();
let holder = fork_running(move || block_on(gate));
let child = fork_running(|| {});
assert_eq!(
channel.wait(pidfd_of(child.pid()).as_fd()),
PlacementReport::NotReported
);
reap(child.defuse());
gate_write.write_all(b"x").expect("release the holder");
reap(holder.defuse());
}
#[cfg(target_os = "linux")]
#[skuld::test]
fn report_channel_is_close_on_exec() {
use std::os::fd::AsRawFd;
let channel = crate::containment::cgroup::ReportChannel::new().expect("open the report channel");
for (end, fd) in [("parent's", channel.read.as_raw_fd()), ("child's", channel.slot().fd)] {
let flags = unsafe { libc::fcntl(fd, libc::F_GETFD) };
assert_ne!(flags, -1, "F_GETFD: {}", std::io::Error::last_os_error());
assert_ne!(flags & libc::FD_CLOEXEC, 0, "the {end} end must be close-on-exec");
}
}
#[cfg(target_os = "linux")]
#[skuld::test]
fn an_abandoned_exchange_still_reads_what_was_sent_before_it() {
let channel = crate::containment::cgroup::ReportChannel::new().expect("open the report channel");
let (_end, slot) = childs_copy(&channel);
unsafe {
assert_eq!(
slot.send_intent().expect("send the intent"),
crate::containment::cgroup::Delivery::Queued
);
assert_eq!(
slot.send_report(crate::containment::cgroup::REPORT_PLACED)
.expect("send the report"),
crate::containment::cgroup::Delivery::Queued
);
}
let received = channel.shut();
assert_eq!(received.pid, Some(std::process::id()));
assert!(received.pidfd.is_some(), "the intent carries a pidfd");
assert_eq!(received.placement(), PlacementReport::Placed);
assert_eq!(
unsafe { slot.send_report(crate::containment::cgroup::REPORT_PLACED) }.expect("send"),
crate::containment::cgroup::Delivery::Abandoned
);
}
#[cfg(target_os = "linux")]
#[skuld::test]
fn a_send_after_the_abandonment_read_fails_rather_than_go_unread() {
let channel = crate::containment::cgroup::ReportChannel::new().expect("open the report channel");
let (end, slot) = childs_copy(&channel);
let outcome = std::rc::Rc::new(std::cell::Cell::new(None));
let seen = outcome.clone();
crate::containment::cgroup::fault::set_after_shut_read(move || {
let sent = unsafe { slot.send_report(crate::containment::cgroup::REPORT_PLACED) }.expect("send");
seen.set(Some(sent));
drop(end);
});
let received = channel.shut();
assert_eq!(received.report, None, "nothing was queued before the read");
assert_eq!(
outcome.get(),
Some(crate::containment::cgroup::Delivery::Abandoned),
"a send in the window must see the abandonment"
);
}
#[cfg(target_os = "linux")]
fn reset_by_the_parent(proceed: bool) -> (std::os::fd::OwnedFd, crate::containment::cgroup::ReportSlot) {
let channel = crate::containment::cgroup::ReportChannel::new().expect("open the report channel");
let (end, slot) = childs_copy(&channel);
let sent = unsafe { slot.send_intent() }.expect("send the intent");
assert_eq!(sent, crate::containment::cgroup::Delivery::Queued);
if proceed {
let sent = rustix::net::send(&channel.read, &[super::PROCEED], rustix::net::SendFlags::NOSIGNAL)
.expect("queue proceed");
assert_eq!(sent, 1);
}
drop(channel);
(end, slot)
}
#[cfg(target_os = "linux")]
#[skuld::test]
fn a_reset_channel_ends_the_exchange_as_a_closed_one_does() {
if !alone("containment::cgroup::channel::channel_tests::a_reset_channel_ends_the_exchange_as_a_closed_one_does") {
return;
}
let (_end, slot) = reset_by_the_parent(false);
let sent = unsafe { libc::send(slot.fd, [0u8; 16].as_ptr().cast(), 16, libc::MSG_NOSIGNAL) };
assert_eq!(sent, -1);
assert_eq!(std::io::Error::last_os_error().raw_os_error(), Some(libc::ECONNRESET));
for (proceed, fate) in [
(true, crate::containment::cgroup::Delivery::Decided),
(false, crate::containment::cgroup::Delivery::Abandoned),
] {
let (_end, slot) = reset_by_the_parent(proceed);
let sent = unsafe { slot.send_report(crate::containment::cgroup::REPORT_PLACED) };
assert_eq!(
sent.expect("a reset is not an error"),
fate,
"proceed queued: {proceed}"
);
}
}
#[cfg(target_os = "linux")]
#[skuld::test]
fn proceed_reads_what_was_sent_before_it_closes() {
if !alone("containment::cgroup::channel::channel_tests::proceed_reads_what_was_sent_before_it_closes") {
return;
}
let channel = crate::containment::cgroup::ReportChannel::new().expect("open the report channel");
let (_end, slot) = childs_copy(&channel);
unsafe {
assert_eq!(
slot.send_intent().expect("send the intent"),
crate::containment::cgroup::Delivery::Queued
);
assert_eq!(
slot.send_report(crate::containment::cgroup::REPORT_PLACED)
.expect("send the report"),
crate::containment::cgroup::Delivery::Queued
);
}
channel.proceed();
let sent = unsafe { libc::send(slot.fd, [0u8; 16].as_ptr().cast(), 16, libc::MSG_NOSIGNAL) };
assert_eq!(sent, -1);
assert_eq!(
std::io::Error::last_os_error().raw_os_error(),
Some(libc::EPIPE),
"closed with the child's messages read"
);
let sent = unsafe { slot.send_report(crate::containment::cgroup::REPORT_PLACED) };
assert_eq!(
sent.expect("a closed channel is not an error"),
crate::containment::cgroup::Delivery::Decided
);
}