use std::os::fd::AsRawFd;
use super::{fd_path, id_of_handle, LeafDir};
#[cfg(target_os = "linux")]
use crate::test_groups::{cgroup, Group};
#[skuld::test]
fn cgroup_fd_path_names_the_calling_threads_own_descriptor(#[fixture(cgroup)] _group: &Group) {
let dir = tempfile::tempdir().expect("tempdir");
let (leader_file, thread_file) = (dir.path().join("leader"), dir.path().join("thread"));
std::fs::write(&leader_file, "").expect("create the leader's file");
std::fs::write(&thread_file, "").expect("create the thread's file");
let shared = std::fs::File::open(&leader_file).expect("open the leader's file");
let n = shared.as_raw_fd();
let thread_file_ = thread_file.clone();
let named = std::thread::spawn(move || {
assert_eq!(
unsafe { libc::unshare(libc::CLONE_FILES) },
0,
"unshare: {}",
std::io::Error::last_os_error()
);
let own = std::fs::File::open(&thread_file_).expect("open the thread's file");
assert_eq!(unsafe { libc::dup2(own.as_raw_fd(), n) }, n, "dup2");
let borrowed = unsafe { std::os::fd::BorrowedFd::borrow_raw(n) };
std::fs::metadata(fd_path(borrowed)).expect("stat through fd_path")
})
.join()
.expect("the thread");
use std::os::unix::fs::MetadataExt as _;
let expected = std::fs::metadata(&thread_file).expect("stat the thread's file");
assert_eq!(
(named.dev(), named.ino()),
(expected.dev(), expected.ino()),
"fd_path must name this thread's descriptor, not the leader's"
);
drop(shared);
}
#[skuld::test]
fn a_leaf_that_cannot_be_held_after_its_mkdir_is_removed() {
let dir = tempfile::tempdir().expect("tempdir");
crate::containment::cgroup::fault::set_force_leaf_open_failure(true);
let parent = LeafDir::open_parent(dir.path()).expect("hold the parent");
let err = match LeafDir::create(parent, "cosca-unheld") {
Err(e) => e,
Ok(_) => panic!("the forced open failure must fail the creation"),
};
assert!(
!crate::containment::cgroup::fault::take_force_leaf_open_failure(),
"the seam must be consumed"
);
assert_eq!(err.raw_os_error(), Some(libc::EMFILE), "got {err}");
assert!(
!dir.path().join("cosca-unheld").exists(),
"the made directory must be removed"
);
}
#[skuld::test]
fn cgroup_the_child_sweep_never_crosses_a_mount(#[fixture(cgroup)] _group: &Group) {
let victim = tempfile::tempdir().expect("tempdir");
std::fs::create_dir_all(victim.path().join("keep/me")).expect("make the victim's empty dirs");
let victim_path = victim.path().to_path_buf();
let own = std::fs::read_to_string("/proc/self/cgroup").expect("read /proc/self/cgroup");
let parent = std::path::Path::new("/sys/fs/cgroup").join(
own.lines()
.find_map(|l| l.strip_prefix("0::/"))
.expect("a unified line"),
);
let name = format!("cosca-{}-sweep", std::process::id());
let sub = parent.join(&name).join("sub");
let sub_in_thread = sub.clone();
let _cleanup = RemoveCgroups {
child: sub.clone(),
leaf: parent.join(&name),
};
let swept = std::thread::spawn(move || {
use std::ffi::CString;
let c = |p: &std::path::Path| CString::new(p.as_os_str().as_encoded_bytes()).expect("no NUL");
unsafe {
assert_eq!(
libc::unshare(libc::CLONE_NEWNS),
0,
"unshare: {}",
std::io::Error::last_os_error()
);
assert_eq!(
libc::mount(
std::ptr::null(),
c(std::path::Path::new("/")).as_ptr(),
std::ptr::null(),
libc::MS_REC | libc::MS_PRIVATE,
std::ptr::null()
),
0,
"make / private: {}",
std::io::Error::last_os_error()
);
}
let held = LeafDir::open_parent(&parent).expect("hold the parent");
let leaf = LeafDir::create(held, &name).expect("create the leaf");
let sub = sub_in_thread;
std::fs::create_dir(&sub).expect("make a child cgroup");
unsafe {
assert_eq!(
libc::mount(
c(&victim_path).as_ptr(),
c(&sub).as_ptr(),
std::ptr::null(),
libc::MS_BIND,
std::ptr::null()
),
0,
"bind the victim over the child: {}",
std::io::Error::last_os_error()
);
}
leaf.remove_children()
})
.join()
.expect("the sweeping thread");
let mounts = std::fs::read_to_string("/proc/self/mountinfo").expect("read mountinfo");
assert!(
!mounts.lines().any(|l| l.contains(&*sub.to_string_lossy())),
"the bind must not outlive its namespace; refusing to clean up"
);
let survived = victim.path().join("keep/me").is_dir();
swept.expect("the sweep");
assert!(
survived,
"the sweep crossed the mount and deleted the victim's directories"
);
}
struct RemoveCgroups {
child: std::path::PathBuf,
leaf: std::path::PathBuf,
}
impl Drop for RemoveCgroups {
fn drop(&mut self) {
match crate::test_groups::cgroup_leaf::is_mounted(&self.child) {
Ok(false) => {}
Ok(true) => return report(format!("{} is mounted; refusing to clean up", self.child.display())),
Err(why) => return report(format!("{why}; refusing to clean up {}", self.child.display())),
}
for path in [&self.child, &self.leaf] {
match std::fs::remove_dir(path) {
Ok(()) => {}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
Err(e) => report(format!("could not remove {}: {e}", path.display())),
}
}
}
}
fn report(message: String) {
crate::test_groups::fail_unless_panicking(message);
}
#[skuld::test]
fn rmdir_removes_the_held_leaf() {
let parent = tempfile::tempdir().expect("tempdir");
let leaf = parent.path().join("cosca-held");
std::fs::create_dir(&leaf).expect("make the leaf");
LeafDir::open_for_test(&leaf).rmdir().expect("rmdir");
assert!(!leaf.exists());
}
fn held_leaf(name: &str) -> (tempfile::TempDir, std::path::PathBuf, LeafDir) {
let parent = tempfile::tempdir().expect("tempdir");
let leaf = parent.path().join(name);
std::fs::create_dir(&leaf).expect("make the leaf");
std::fs::write(leaf.join("cgroup.events"), "populated 0\n").expect("write cgroup.events");
let dir = LeafDir::open_for_test(&leaf);
(parent, leaf, dir)
}
#[skuld::test]
fn rmdir_of_a_leaf_removed_since_is_enoent() {
let (_parent, leaf, dir) = held_leaf("cosca-removed");
std::fs::remove_dir_all(&leaf).expect("remove the leaf");
assert_eq!(dir.rmdir().expect_err("gone").raw_os_error(), Some(libc::ENOENT));
}
#[skuld::test]
fn rmdir_spares_a_directory_that_took_a_removed_leafs_name() {
let (_parent, leaf, dir) = held_leaf("cosca-replaced");
std::fs::remove_dir_all(&leaf).expect("remove the leaf");
std::fs::create_dir(&leaf).expect("make a stranger under the leaf's name");
assert_eq!(dir.rmdir().expect_err("gone").raw_os_error(), Some(libc::ENOENT));
assert!(leaf.exists(), "the stranger under the leaf's name must survive");
}
#[skuld::test]
fn rmdir_spares_a_directory_that_took_a_live_leafs_name() {
let (parent, leaf, dir) = held_leaf("cosca-swapped");
std::fs::rename(&leaf, parent.path().join("elsewhere")).expect("move the leaf away");
std::fs::create_dir(&leaf).expect("make a stranger under the leaf's name");
let e = dir.rmdir().expect_err("the leaf is still there");
assert_ne!(e.raw_os_error(), Some(libc::ENOENT), "{e}");
assert!(leaf.exists(), "the stranger under the leaf's name must survive");
}
#[skuld::test]
fn rmdir_spares_a_symlink_that_took_a_live_leafs_name() {
let (parent, leaf, dir) = held_leaf("cosca-linked");
let moved = parent.path().join("moved");
std::fs::rename(&leaf, &moved).expect("move the leaf away");
std::os::unix::fs::symlink(&moved, &leaf).expect("link the leaf's name to it");
let e = dir.rmdir().expect_err("the leaf is still there");
assert_ne!(e.raw_os_error(), Some(libc::ENOENT), "{e}");
assert!(
moved.exists(),
"the leaf behind the link must not be removed through it"
);
}
#[skuld::test]
fn a_cgroup_id_is_decoded_from_its_kernfs_handle() {
const HIGH: u64 = 0x1_0000_0007;
assert_eq!(
id_of_handle(0xfe, 8, HIGH.to_ne_bytes()).expect("a kernfs handle"),
HIGH
);
id_of_handle(1, 8, HIGH.to_ne_bytes()).expect_err("not a kernfs handle");
id_of_handle(0xfe, 4, HIGH.to_ne_bytes()).expect_err("too short for a kernfs id");
}
#[skuld::test]
fn a_sweep_that_cannot_read_a_childs_id_removes_it_unrecorded_and_says_why() {
crate::log_capture::install();
let dir = tempfile::tempdir().expect("tempdir");
let sub = dir.path().join("unrecorded-sub-9f3a");
std::fs::create_dir(&sub).expect("make the child");
let leaf = LeafDir::open_for_test(dir.path());
let mark = crate::log_capture::mark();
assert_eq!(leaf.remove_children().expect("the sweep"), 1);
assert!(!sub.exists(), "the child is removed");
let logs = crate::log_capture::records_since_on_current_thread(mark, "unrecorded-sub-9f3a");
assert_eq!(logs.len(), 1, "{logs:?}");
assert_eq!(logs[0].0, log::Level::Debug);
assert!(logs[0].1.contains("removed unrecorded"), "{logs:?}");
}