#![allow(dead_code)]
use std::path::PathBuf;
pub fn tmp_dir(name: &str) -> PathBuf {
let dir = PathBuf::from("target").join("oscall-tests").join(name);
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
dir
}
pub fn handle_of(file: &std::fs::File) -> oscall::BorrowedHandle<'_> {
#[cfg(windows)]
let raw = std::os::windows::io::AsRawHandle::as_raw_handle(file);
#[cfg(not(windows))]
let raw = std::os::fd::AsRawFd::as_raw_fd(file);
unsafe { oscall::BorrowedHandle::borrow_raw(raw) }
}
#[cfg(any(target_os = "linux", target_os = "fullrust"))]
pub fn emulated() -> bool {
if let Some(forced) = std::env::var_os("OSCALL_TEST_EMULATED") {
return forced != "0";
}
let program = std::env::consts::ARCH;
if let Ok(kernel) = std::fs::read_to_string("/proc/sys/kernel/arch") {
let kernel = kernel.trim();
let native = match program {
"x86" => kernel == "x86_64" || (kernel.starts_with('i') && kernel.ends_with("86")),
"arm" => kernel.starts_with("arm") || kernel == "aarch64",
program => kernel == program,
};
return !native;
}
std::fs::read_to_string("/proc/self/comm").is_ok_and(|name| name.starts_with("qemu"))
}
pub fn note(line: &str) {
use std::io::Write;
let _ = writeln!(std::io::stderr().lock(), "{line}");
}
#[cfg(any(target_os = "linux", target_os = "fullrust"))]
pub fn confinement() -> String {
let read = |path: &str| match std::fs::read_to_string(path) {
Ok(text) => text.trim().trim_end_matches('\0').to_owned(),
Err(_) => "unknown".to_owned(),
};
format!(
"kernel.apparmor_restrict_unprivileged_userns is {}, the profile of the process is {}",
read("/proc/sys/kernel/apparmor_restrict_unprivileged_userns"),
read("/proc/self/attr/current"),
)
}
#[cfg(any(target_os = "linux", target_os = "fullrust"))]
pub fn privileged_required() -> bool {
std::env::var_os("OSCALL_REQUIRE_PRIVILEGED").is_some_and(|value| value != "0")
}
#[cfg(any(target_os = "linux", target_os = "fullrust"))]
pub fn skip(reason: &str) {
note(&format!("skipped: {reason}"));
assert!(
!privileged_required(),
"skipped in a run that must skip nothing (OSCALL_REQUIRE_PRIVILEGED): {reason}"
);
}
#[cfg(any(target_os = "linux", target_os = "fullrust"))]
pub fn no_time_namespaces() {
let kernel = std::fs::read_to_string("/proc/sys/kernel/arch").unwrap_or_default();
let kernel = kernel.trim();
if kernel == "riscv32" || (kernel.starts_with("arm") && !kernel.starts_with("arm64")) {
note(&format!(
"skipped: {kernel} kernels have no time namespaces"
));
} else {
skip("the kernel has no time namespaces");
}
}
#[cfg(any(target_os = "linux", target_os = "fullrust"))]
pub fn status_field(name: &str, index: usize) -> String {
let status = std::fs::read_to_string("/proc/thread-self/status")
.or_else(|_| std::fs::read_to_string("/proc/self/status"))
.unwrap();
let line = status
.lines()
.find_map(|line| line.strip_prefix(name)?.strip_prefix(':'))
.unwrap_or_else(|| panic!("no {name} in the status of the process"));
line.split_whitespace()
.nth(index)
.unwrap_or_default()
.to_owned()
}
#[cfg(any(target_os = "linux", target_os = "fullrust"))]
pub fn status_capabilities(name: &str) -> u64 {
u64::from_str_radix(&status_field(name, 0), 16).unwrap()
}
#[cfg(any(target_os = "linux", target_os = "fullrust"))]
pub fn namespace_refusal(error: &oscall::Error) -> Option<&'static str> {
match error.raw_os_error() {
1 | 13 => Some(
"not permitted here (user namespaces restricted, or a container's system call filter)",
),
28 => Some("no user namespace may be created (user.max_user_namespaces)"),
38 => Some("the system call is missing (an emulator, a system call filter)"),
22 if emulated() => Some("qemu-user does not create a process in new namespaces"),
_ => None,
}
}