#[cfg(target_os = "android")]
pub mod android;
pub mod child;
#[cfg(unix)]
pub mod confinement;
#[cfg(unix)]
pub mod descriptor;
#[cfg(feature = "native-editor")]
pub mod editor;
pub mod environment;
pub use tilo_platform_caps::{Availability, availability, available};
#[cfg(unix)]
pub mod local_socket;
pub mod login_shell;
#[cfg(unix)]
pub mod pty;
pub mod rendezvous;
pub mod shared_memory;
pub mod tools;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Feature {
ProcessId,
Rendezvous,
}
impl tilo_platform_caps::Feature for Feature {
fn targets(self) -> [Availability; 3] {
use Availability::{Available, Unsupported};
match self {
Self::ProcessId => [
Available,
Available,
Unsupported("a page has no process id"),
],
Self::Rendezvous => [
Available,
Available,
Unsupported("a page has no local sockets"),
],
}
}
fn availability(self) -> Availability {
let here = match self {
Self::ProcessId => backend::HAS_PROCESS_ID,
Self::Rendezvous => cfg!(unix),
};
if here {
Availability::Available
} else {
self.targets()[2]
}
}
}
#[must_use]
pub fn id() -> Option<u32> {
backend::id()
}
#[cfg(unix)]
#[must_use]
pub fn uid() -> u32 {
rustix::process::getuid().as_raw()
}
#[cfg(unix)]
#[must_use]
pub fn effective_uid() -> u32 {
rustix::process::geteuid().as_raw()
}
#[must_use]
pub fn open_file_count(pid: u32) -> Option<usize> {
table::open_file_count(pid)
}
#[must_use]
pub fn start_time(pid: u32) -> Option<u64> {
table::start_time(pid)
}
#[must_use]
pub fn identity_record(pid: u32) -> Option<String> {
Some(format!("{pid} {}\n", start_time(pid)?))
}
#[must_use]
pub fn identity_names(record: &str, pid: u32) -> bool {
let mut fields = record.split_whitespace();
let (Some(record_pid), Some(record_start), None) =
(fields.next(), fields.next(), fields.next())
else {
return false;
};
record_pid.parse::<u32>() == Ok(pid) && record_start.parse::<u64>().ok() == start_time(pid)
}
pub fn probe_process_table() -> std::io::Result<()> {
table::probe()
}
pub fn raise_open_file_limit() {
limits::raise_open_files();
}
#[cfg(target_os = "macos")]
mod limits {
const OPEN_FILES: libc::rlim_t = 1024;
pub fn raise_open_files() {
let mut limit = libc::rlimit {
rlim_cur: 0,
rlim_max: 0,
};
if unsafe { libc::getrlimit(libc::RLIMIT_NOFILE, &raw mut limit) } != 0 {
return;
}
let wanted = OPEN_FILES.min(limit.rlim_max);
if limit.rlim_cur >= wanted {
return;
}
limit.rlim_cur = wanted;
let _ = unsafe { libc::setrlimit(libc::RLIMIT_NOFILE, &raw const limit) };
}
}
#[cfg(not(target_os = "macos"))]
mod limits {
pub const fn raise_open_files() {}
}
#[cfg(target_os = "linux")]
mod table {
pub fn open_file_count(pid: u32) -> Option<usize> {
std::fs::read_dir(format!("/proc/{pid}/fd"))
.ok()
.map(|entries| entries.flatten().count())
}
pub fn start_time(pid: u32) -> Option<u64> {
let stat = std::fs::read_to_string(format!("/proc/{pid}/stat")).ok()?;
let mut fields = stat.rsplit_once(')')?.1.split_whitespace();
if matches!(fields.next()?, "Z" | "X") {
return None;
}
fields.nth(18)?.parse().ok()
}
pub fn probe() -> std::io::Result<()> {
std::fs::read_dir("/proc").map(|_| ())
}
}
#[cfg(not(target_os = "linux"))]
mod table {
pub const fn open_file_count(_pid: u32) -> Option<usize> {
None
}
pub const fn start_time(_pid: u32) -> Option<u64> {
None
}
#[allow(
clippy::unnecessary_wraps,
reason = "the signature is shared with the Linux backend, whose read can fail"
)]
pub const fn probe() -> std::io::Result<()> {
Ok(())
}
}
#[cfg(not(target_family = "wasm"))]
mod backend {
pub const HAS_PROCESS_ID: bool = true;
#[allow(
clippy::unnecessary_wraps,
reason = "the signature is shared with the browser backend, which has no id"
)]
pub fn id() -> Option<u32> {
Some(std::process::id())
}
}
#[cfg(target_family = "wasm")]
mod backend {
pub const HAS_PROCESS_ID: bool = false;
pub fn id() -> Option<u32> {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn native_processes_have_an_id() {
assert_eq!(id().is_some(), cfg!(not(target_family = "wasm")));
}
#[test]
fn open_files_are_counted_only_where_the_system_lists_them() {
let own = id().unwrap_or(0);
assert_eq!(open_file_count(own).is_some(), cfg!(target_os = "linux"));
assert_eq!(open_file_count(u32::MAX), None);
assert!(probe_process_table().is_ok());
}
#[test]
fn a_process_record_names_its_id_and_start_time_and_nothing_else() {
let own = id().expect("a native process has an id");
let record = identity_record(own).expect("the system describes this process");
assert!(identity_names(&record, own), "{record:?}");
assert!(!identity_names(&record, own.wrapping_add(1)));
let start = start_time(own).expect("a start time");
assert!(!identity_names(&format!("{own} {}\n", start + 1), own));
for malformed in ["", "\n", "not a record", "1", "1 2 3"] {
assert!(!identity_names(malformed, own), "{malformed:?}");
}
}
#[cfg(target_os = "linux")]
#[test]
fn an_exited_process_not_yet_reaped_has_no_start_time() {
use tilo_test_env::SpawnUnderEnvLock as _;
let mut child = std::process::Command::new("true")
.spawn_locked()
.expect("a child starts");
let pid = child.id();
let mut info = std::mem::MaybeUninit::<libc::siginfo_t>::zeroed();
let waited = unsafe {
libc::waitid(
libc::P_PID,
pid,
info.as_mut_ptr(),
libc::WEXITED | libc::WNOWAIT,
)
};
assert_eq!(waited, 0, "{}", std::io::Error::last_os_error());
assert!(
std::fs::metadata(format!("/proc/{pid}")).is_ok(),
"the zombie is still in the table"
);
assert_eq!(start_time(pid), None);
child.wait().expect("reaped");
}
}