#[cfg(target_os = "linux")]
use crate::test_groups::{unprivileged, Group};
fn sh(script: &str) -> crate::Child {
super::pump_common_tests::shell(script, "")
}
#[cfg(target_os = "linux")]
#[skuld::test]
fn communicate_starts_no_thread(#[fixture(unprivileged)] _group: &Group) {
crate::test_child::run_fixture(crate::test_child::fixture_path!(
fixture_communicate_under_a_nproc_limit
));
}
#[cfg(target_os = "linux")]
#[skuld::test]
fn fixture_communicate_under_a_nproc_limit() {
if !crate::test_child::is_fixture_reexec() {
return;
}
let mut child = sh("cat; echo done >&2");
let limit = libc::rlimit {
rlim_cur: 0,
rlim_max: 0,
};
let set = unsafe { libc::setrlimit(libc::RLIMIT_NPROC, &limit) };
assert_eq!(set, 0, "lower RLIMIT_NPROC: {}", std::io::Error::last_os_error());
let refused = std::thread::Builder::new().spawn(|| {});
assert!(
refused.is_err(),
"the precondition: RLIMIT_NPROC=0 must refuse a new thread; it does not bind uid 0 or \
CAP_SYS_RESOURCE/CAP_SYS_ADMIN holders, so run as an unprivileged user, or set \
COSCA_TEST_UNPRIVILEGED=0 to opt out"
);
let input = crate::test_pattern::pattern_bytes(1 << 20);
let out = child.communicate(Some(&input)).expect("communicate");
assert!(out.status.success());
assert!(out.stdout == input, "stdout");
assert_eq!(out.stderr, b"done\n");
}
#[skuld::test]
fn a_child_that_stops_reading_stdin_does_not_kill_a_default_sigpipe_host() {
crate::test_child::run_fixture(crate::test_child::fixture_path!(
fixture_communicate_with_default_sigpipe
));
}
#[skuld::test]
fn fixture_communicate_with_default_sigpipe() {
if !crate::test_child::is_fixture_reexec() {
return;
}
let previous = unsafe { libc::signal(libc::SIGPIPE, libc::SIG_DFL) };
assert_ne!(
previous,
libc::SIG_ERR,
"reset SIGPIPE: {}",
std::io::Error::last_os_error()
);
let mut child = sh("echo out; exit 3");
let input = vec![b'x'; 1 << 20];
let out = child.communicate(Some(&input)).expect("communicate");
assert_eq!(out.status.code(), Some(3));
assert_eq!(
out.stdout,
b"out
"
);
}
#[skuld::test]
fn a_sigpipe_pending_in_the_host_stays_pending() {
crate::test_child::run_fixture(crate::test_child::fixture_path!(
fixture_communicate_with_pending_sigpipe
));
}
#[skuld::test]
fn fixture_communicate_with_pending_sigpipe() {
if !crate::test_child::is_fixture_reexec() {
return;
}
unsafe {
assert_ne!(libc::signal(libc::SIGPIPE, libc::SIG_DFL), libc::SIG_ERR);
let mut set: libc::sigset_t = std::mem::zeroed();
libc::sigemptyset(&mut set);
libc::sigaddset(&mut set, libc::SIGPIPE);
assert_eq!(libc::pthread_sigmask(libc::SIG_BLOCK, &set, std::ptr::null_mut()), 0);
assert_eq!(libc::raise(libc::SIGPIPE), 0);
let mut raised: libc::sigset_t = std::mem::zeroed();
assert_eq!(libc::sigpending(&mut raised), 0);
assert_eq!(
libc::sigismember(&raised, libc::SIGPIPE),
1,
"the raised SIGPIPE is pending"
);
let mut child = sh("echo out; exit 3");
let input = vec![b'x'; 1 << 20];
let out = child.communicate(Some(&input)).expect("communicate");
assert_eq!(
out.stdout,
b"out
"
);
let mut pending: libc::sigset_t = std::mem::zeroed();
assert_eq!(libc::sigpending(&mut pending), 0);
assert_eq!(
libc::sigismember(&pending, libc::SIGPIPE),
1,
"the host's SIGPIPE was consumed"
);
}
}