use std::io::Read;
#[cfg(not(target_os = "linux"))]
use std::io::{BufRead, BufReader};
use std::os::fd::{AsRawFd, OwnedFd};
use std::os::unix::ffi::OsStrExt;
#[cfg(not(target_os = "linux"))]
use std::process::{Command, Stdio};
use super::InChild;
use crate::child::spawn::fd_map::{self, FdMapping};
#[cfg(not(target_os = "linux"))]
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()
}
#[cfg(not(target_os = "linux"))]
#[skuld::test]
fn every_in_child_step_takes_effect() {
let dir = tempfile::tempdir().expect("tempdir");
let mut steps = InChild::default();
#[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");
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));
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_child::StdChild::spawn(&mut cmd);
drop(cmd);
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 every_in_child_step_takes_effect() {
let dir = tempfile::tempdir().expect("tempdir");
let mut steps = InChild::default();
let leaf_path = dir.path().join("leaf");
std::fs::create_dir(&leaf_path).expect("create the leaf");
let mut leaf = crate::containment::cgroup::CgroupLeaf::for_test_at(leaf_path);
let (mut procs_read, procs_write) = std::io::pipe().expect("pipe");
let (mut mapped_read, mapped_write) = std::io::pipe().expect("pipe");
let (mut stdout_read, stdout_write) = std::io::pipe().expect("pipe");
let mapped_fd = procs_write.as_raw_fd();
steps.map_stdio(fd_map::Plan::stdio(vec![FdMapping {
parent_fd: OwnedFd::from(stdout_write),
child_fd: 1,
}]));
fd_map::install(
&mut steps,
vec![FdMapping {
parent_fd: OwnedFd::from(mapped_write),
child_fd: mapped_fd,
}],
)
.expect("install the mapping");
unsafe { steps.place(procs_write.as_raw_fd(), false) };
steps.session();
steps.enter(std::ffi::CString::new(dir.path().as_os_str().as_bytes()).expect("no NUL"));
let started = super::spawn_sh_placed(
&format!("pwd -P; echo mapped > /dev/fd/{mapped_fd}"),
&mut steps,
Some(&mut leaf),
)
.expect("spawn");
drop(steps);
drop(procs_write);
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");
leaf.take_placement(crate::containment::ChildHandle {
pid: started.pid,
pidfd: started.owned.pidfd().expect("a pidfd box"),
})
.expect("the placement step reports its write");
let mut stdout = String::new();
stdout_read.read_to_string(&mut stdout).expect("read the stdout");
assert_eq!(
std::path::Path::new(stdout.trim_end_matches('\n')),
dir.path().canonicalize().expect("canonicalize"),
"the child runs in the directory the enter step chose, and its stdout is the pipe's"
);
let mut mapped = String::new();
mapped_read.read_to_string(&mut mapped).expect("read the mapped pipe");
assert_eq!(mapped, "mapped\n", "the fd map lands the pipe on fd {mapped_fd}");
let pid = started.pid as libc::pid_t;
assert_eq!(
unsafe { libc::getsid(pid) },
pid,
"the session step makes the child a session leader"
);
assert_eq!(super::exit_code(&started), Some(0));
}
#[cfg(target_os = "linux")]
#[skuld::test]
fn a_failing_enter_step_stops_the_child_before_placement() {
let dir = tempfile::tempdir().expect("tempdir");
let missing = dir.path().join("missing");
let (mut procs_read, procs_write) = std::io::pipe().expect("pipe");
let mut steps = InChild::default();
unsafe { steps.place(procs_write.as_raw_fd(), false) };
steps.enter(std::ffi::CString::new(missing.as_os_str().as_bytes()).expect("no NUL"));
let err = match super::spawn_sh("true", &mut steps) {
Err(err) => err,
Ok(started) => panic!("the child entered a missing directory (pid {})", started.pid),
};
assert_eq!(err.raw_os_error(), Some(libc::ENOENT), "{err}");
drop(steps);
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");
}
#[cfg(not(target_os = "linux"))]
#[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 = "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");
}