use std::ffi::OsString;
use std::io::Write;
use std::path::Path;
use super::link::probe::Probe;
use super::link::ShimLink;
const FLAG: &str = "--cosca-shim-test-owner";
pub(crate) const SEND_EXIT: &str = "send-exit ";
pub(crate) const READ_FRAMES: &str = "read-frames";
pub(crate) fn run_if_requested() {
let args: Vec<OsString> = std::env::args_os().collect();
if args.get(1).is_none_or(|a| a != FLAG) {
return;
}
let mode = args[2].to_string_lossy().into_owned();
std::process::exit(run(&mode, Path::new(&args[3])));
}
pub(crate) fn argv(mode: &str, work: &Path) -> Vec<OsString> {
vec![FLAG.into(), mode.into(), work.into()]
}
fn say(line: &str) {
let mut out = std::io::stdout().lock();
writeln!(out, "{line}").expect("the test is reading");
out.flush().expect("the test is reading");
}
fn run(mode: &str, work: &Path) -> i32 {
let euid = unsafe { libc::geteuid() };
let (probe, events) = Probe::new();
let link = ShimLink::bind_probed(work, euid, probe.clone()).expect("the link binds");
if mode != "plain" {
probe.hold_acceptor();
}
say(&format!("dir={}", link.dir().display()));
say(&format!("pid={}", std::process::id()));
std::thread::spawn(move || {
for event in events {
say(&format!("event {event:?}"));
}
});
match mode {
"plain" | "hold" => {}
"hold-copy" => fork_copy(|| {}),
"leak-before" => fork_copy(|| leak(false)),
"leak-after" => fork_copy(|| leak(true)),
other => panic!("unknown mode {other}"),
}
say("ready");
for line in std::io::stdin().lines() {
match line.expect("stdin").as_str() {
"fork" => {
fork_copy(|| {});
say("forked");
}
READ_FRAMES => {
let conn = link.connection_fd().expect("the shim is connected");
fork_copy(|| read_frames(conn));
say("forked");
}
command if command.starts_with(SEND_EXIT) => {
for byte in command[SEND_EXIT.len()..].bytes() {
link.send_control(byte).expect("the byte reaches the shim");
}
std::process::exit(0);
}
other => panic!("unknown command {other}"),
}
}
drop(link);
0
}
fn fork_copy(body: impl FnOnce()) {
match unsafe { libc::fork() } {
-1 => panic!("fork: {}", std::io::Error::last_os_error()),
0 => {
body();
let mut byte = 0u8;
unsafe {
while libc::read(0, (&mut byte as *mut u8).cast(), 1) > 0 {}
libc::_exit(0)
}
}
_ => {}
}
}
pub(crate) const POLL_FAILED: i32 = 92;
unsafe fn wait_readable(fd: libc::c_int) {
let mut poll = libc::pollfd {
fd,
events: libc::POLLIN,
revents: 0,
};
unsafe {
while libc::poll(&mut poll, 1, -1) < 0 {
if *libc::__errno_location() != libc::EINTR {
libc::_exit(POLL_FAILED);
}
}
}
}
fn read_frames(conn: std::os::fd::RawFd) {
unsafe {
let mut bytes = [0u8; 64];
let mut len = 0;
while len < bytes.len() {
wait_readable(conn);
let n = libc::read(conn, bytes.as_mut_ptr().add(len).cast(), bytes.len() - len);
if n == 0 {
break;
}
if n > 0 {
len += n as usize;
} else if *libc::__errno_location() != libc::EAGAIN && *libc::__errno_location() != libc::EINTR {
break;
}
}
let mut line = [0u8; 8 + 2 * 64 + 1];
line[..7].copy_from_slice(b"frames ");
let digits = b"0123456789abcdef";
for i in 0..len {
line[7 + 2 * i] = digits[(bytes[i] >> 4) as usize];
line[7 + 2 * i + 1] = digits[(bytes[i] & 15) as usize];
}
line[7 + 2 * len] = b'\n';
libc::write(1, line.as_ptr().cast(), 7 + 2 * len + 1);
libc::_exit(0)
}
}
fn leak(after_hello: bool) {
unsafe {
let listener = (3..1024)
.find(|&fd| {
let mut accepting: libc::c_int = 0;
let mut len = std::mem::size_of::<libc::c_int>() as libc::socklen_t;
libc::getsockopt(
fd,
libc::SOL_SOCKET,
libc::SO_ACCEPTCONN,
(&mut accepting as *mut libc::c_int).cast(),
&mut len,
) == 0
&& accepting == 1
})
.unwrap_or_else(|| libc::_exit(90));
wait_readable(listener);
let conn = libc::accept(listener, std::ptr::null_mut(), std::ptr::null_mut());
if conn < 0 {
libc::_exit(91);
}
if after_hello {
let mut hello = 0u8;
libc::read(conn, (&mut hello as *mut u8).cast(), 1);
}
libc::write(conn, b"A".as_ptr().cast(), 1);
}
}
#[cfg(test)]
#[path = "owner_helper_tests.rs"]
mod owner_helper_tests;