use std::io::{BufRead, BufReader, Read};
use std::os::fd::{AsRawFd, OwnedFd};
use std::os::unix::ffi::OsStrExt;
use std::process::{Command, Stdio};
use super::InChild;
use crate::child::spawn::fd_map::{self, FdMapping};
fn line_from(pipe: impl Read) -> String {
let mut line = String::new();
BufReader::new(pipe).read_line(&mut line).expect("read a line");
line.trim_end_matches('\n').to_owned()
}
#[skuld::test]
fn every_in_child_step_takes_effect() {
let dir = tempfile::tempdir().expect("tempdir");
let mut steps = InChild::default();
#[cfg(target_os = "linux")]
let (mut channel, mut procs_read, procs_write) = {
let channel = crate::containment::cgroup::ReportChannel::new().expect("open the report channel");
let (procs_read, procs_write) = std::io::pipe().expect("pipe");
(channel, procs_read, procs_write)
};
#[cfg(target_os = "macos")]
let (marker_read, marker_write) = std::io::pipe().expect("pipe");
#[cfg(target_os = "macos")]
let marker_fd = marker_write.as_raw_fd();
let (mapped_read, mapped_write) = std::io::pipe().expect("pipe");
#[cfg(target_os = "linux")]
let mapped_fd = procs_write.as_raw_fd();
#[cfg(not(target_os = "linux"))]
let mapped_fd = {
let highest = mapped_read.as_raw_fd();
#[cfg(target_os = "macos")]
let highest = highest.max(marker_fd);
highest + 8
};
let mapping = FdMapping {
parent_fd: OwnedFd::from(mapped_write),
child_fd: mapped_fd,
};
fd_map::install(&mut steps, vec![mapping]).expect("install the mapping");
#[cfg(target_os = "macos")]
steps.preserve(OwnedFd::from(marker_write));
#[cfg(target_os = "linux")]
unsafe {
steps.place(procs_write.as_raw_fd(), channel.slot());
}
steps.session();
steps.enter(std::ffi::CString::new(dir.path().as_os_str().as_bytes()).expect("no NUL"));
#[cfg(target_os = "macos")]
let marker_line = format!("echo marker > /dev/fd/{marker_fd}; ");
#[cfg(not(target_os = "macos"))]
let marker_line = String::new();
let mut cmd = Command::new("/bin/sh");
cmd.arg("-c")
.arg(format!(
"pwd -P; echo mapped > /dev/fd/{mapped_fd}; {marker_line}read _ || true"
))
.stdin(Stdio::piped())
.stdout(Stdio::piped());
steps.register(&mut cmd);
let mut child = crate::test_spawn::spawn(&mut cmd).expect("spawn");
drop(cmd);
#[cfg(target_os = "linux")]
drop(procs_write);
#[cfg(target_os = "linux")]
{
let mut written = String::new();
procs_read.read_to_string(&mut written).expect("read the placement");
assert_eq!(written, "0", "the placement step writes \"0\" to its cgroup.procs fd");
let pidfd = crate::containment::cgroup::test_support::pidfd_of(child.id());
assert_eq!(
channel.wait(std::os::fd::AsFd::as_fd(&pidfd)),
crate::containment::cgroup::PlacementReport::Placed,
"the placement step reports its write"
);
}
let stdout = child.stdout.take().expect("stdout");
let mut stdout = BufReader::new(stdout);
let mut cwd = String::new();
stdout.read_line(&mut cwd).expect("read the cwd");
assert_eq!(
std::path::Path::new(cwd.trim_end_matches('\n')),
dir.path().canonicalize().expect("canonicalize"),
"the child runs in the directory the enter step chose"
);
assert_eq!(
line_from(mapped_read),
"mapped",
"the fd map lands the pipe on fd {mapped_fd}"
);
#[cfg(target_os = "macos")]
assert_eq!(
line_from(marker_read),
"marker",
"the marker survives exec at its own number"
);
let pid = child.id() as libc::pid_t;
assert_eq!(
unsafe { libc::getsid(pid) },
pid,
"the session step makes the child a session leader"
);
drop(child.stdin.take());
assert!(child.wait().expect("wait").success());
}
#[cfg(target_os = "linux")]
#[skuld::test]
fn no_step_registers_no_hook() {
use std::os::unix::fs::PermissionsExt;
let dir = tempfile::tempdir().expect("tempdir");
let script = dir.path().join("no-shebang");
std::fs::write(&script, "exit 0\n").expect("write the script");
std::fs::set_permissions(&script, std::fs::Permissions::from_mode(0o755)).expect("chmod");
let mut cmd = Command::new(&script);
InChild::default().register(&mut cmd);
match crate::test_spawn::spawn(&mut cmd) {
Err(e) => assert_eq!(e.raw_os_error(), Some(libc::ENOEXEC), "{e}"),
Ok(mut child) => {
let status = child.wait().expect("wait");
panic!("the fork route ran the file through sh ({status}): a hook was registered");
}
}
}
#[cfg(target_os = "linux")]
#[skuld::test]
fn a_failing_enter_step_stops_the_hook_before_placement() {
let dir = tempfile::tempdir().expect("tempdir");
let missing = dir.path().join("missing");
let channel = crate::containment::cgroup::ReportChannel::new().expect("open the report channel");
let (mut procs_read, procs_write) = std::io::pipe().expect("pipe");
let mut steps = InChild::default();
unsafe { steps.place(procs_write.as_raw_fd(), channel.slot()) };
steps.enter(std::ffi::CString::new(missing.as_os_str().as_bytes()).expect("no NUL"));
let mut cmd = Command::new("/bin/sh");
cmd.arg("-c").arg("true");
steps.register(&mut cmd);
let err = crate::test_spawn::spawn(&mut cmd).expect_err("the child cannot enter a missing directory");
assert_eq!(err.raw_os_error(), Some(libc::ENOENT), "{err}");
drop(cmd);
drop(procs_write);
let mut written = String::new();
procs_read.read_to_string(&mut written).expect("read the placement");
assert_eq!(written, "", "the placement step ran although the enter step failed");
}
#[skuld::test]
fn is_empty_counts_every_step() {
assert!(InChild::default().is_empty(), "no step");
let mut steps = InChild::default();
steps.enter(std::ffi::CString::new("/").expect("no NUL"));
assert!(!steps.is_empty(), "is_empty ignores the enter step");
let mut steps = InChild::default();
steps.session();
assert!(!steps.is_empty(), "is_empty ignores the session step");
#[cfg(target_os = "linux")]
{
let channel = crate::containment::cgroup::ReportChannel::new().expect("open the report channel");
let (_procs_read, procs_write) = std::io::pipe().expect("pipe");
let mut steps = InChild::default();
unsafe { steps.place(procs_write.as_raw_fd(), channel.slot()) };
assert!(!steps.is_empty(), "is_empty ignores the place step");
}
#[cfg(target_os = "macos")]
{
let (_read, write) = std::io::pipe().expect("pipe");
let mut steps = InChild::default();
steps.preserve(OwnedFd::from(write));
assert!(!steps.is_empty(), "is_empty ignores the preserve step");
}
let (_read, write) = std::io::pipe().expect("pipe");
let mut steps = InChild::default();
let mapping = FdMapping {
parent_fd: OwnedFd::from(write),
child_fd: 9,
};
fd_map::install(&mut steps, vec![mapping]).expect("install the mapping");
assert!(!steps.is_empty(), "is_empty ignores the fd map step");
}