#[cfg(not(any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "riscv64",
target_arch = "s390x",
target_arch = "powerpc64",
target_arch = "loongarch64"
)))]
compile_error!("os_process::linux::fork: the raw clone's return convention is checked only on x86_64, aarch64, riscv64, s390x, powerpc64 and loongarch64");
use std::os::fd::{FromRawFd, OwnedFd};
#[derive(Debug, PartialEq, Eq)]
pub(crate) enum Path {
Clone3,
Clone,
}
#[derive(Debug)]
pub(crate) struct NewChild {
pub(crate) pid: libc::pid_t,
pub(crate) pidfd: OwnedFd,
pub(crate) path: Path,
}
#[derive(Debug)]
pub(crate) enum Fork {
Child,
Parent(NewChild),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum CloneKind {
Vfork,
Plain,
}
impl CloneKind {
fn flags(self) -> u64 {
match self {
CloneKind::Vfork => CLONE_VFORK,
CloneKind::Plain => 0,
}
}
}
pub(crate) type ForkFn = unsafe fn(CloneKind) -> Result<Fork, i32>;
pub(crate) fn fails_with(errno: i32) -> Result<Fork, i32> {
Err(errno)
}
#[repr(C)]
#[derive(Default)]
struct CloneArgs {
flags: u64,
pidfd: u64,
child_tid: u64,
parent_tid: u64,
exit_signal: u64,
stack: u64,
stack_size: u64,
tls: u64,
}
const CLONE_PIDFD: u64 = 0x1000;
const CLONE_VFORK: u64 = 0x4000;
pub(crate) unsafe fn fork_with_pidfd(kind: CloneKind) -> Result<Fork, i32> {
unsafe { fallback(clone3_with_pidfd, clone_with_pidfd, kind) }
}
pub(crate) unsafe fn fallback(first: ForkFn, second: ForkFn, kind: CloneKind) -> Result<Fork, i32> {
match unsafe { first(kind) } {
Err(libc::ENOSYS | libc::EPERM) => unsafe { second(kind) },
forked => forked,
}
}
unsafe fn clone3_with_pidfd(kind: CloneKind) -> Result<Fork, i32> {
debug_assert_locked();
let mut pidfd: libc::c_int = -1;
let args = CloneArgs {
flags: CLONE_PIDFD | kind.flags(),
pidfd: (&mut pidfd as *mut libc::c_int) as u64,
exit_signal: libc::SIGCHLD as u64,
..CloneArgs::default()
};
let pid = unsafe { libc::syscall(libc::SYS_clone3, &args, std::mem::size_of::<CloneArgs>()) };
after_clone(pid, pidfd, Path::Clone3)
}
pub(crate) unsafe fn clone_with_pidfd(kind: CloneKind) -> Result<Fork, i32> {
debug_assert_locked();
let mut pidfd: libc::c_int = -1;
let flags = CLONE_PIDFD | kind.flags() | libc::SIGCHLD as u64;
let pid = unsafe { raw_clone(flags, &mut pidfd) };
after_clone(pid, pidfd, Path::Clone)
}
fn debug_assert_locked() {
#[cfg(any(test, debug_assertions))]
debug_assert!(
crate::child::spawn::spawn_lock_held_by_this_thread(),
"a fork runs under spawn_lock"
);
}
fn after_clone(pid: libc::c_long, pidfd: libc::c_int, path: Path) -> Result<Fork, i32> {
if pid == 0 {
return Ok(Fork::Child);
}
if pid < 0 {
return Err(std::io::Error::last_os_error().raw_os_error().unwrap_or(libc::EIO));
}
if pidfd < 0 {
return Err(libc::ENOSYS);
}
let pidfd = unsafe { OwnedFd::from_raw_fd(pidfd) };
Ok(Fork::Parent(NewChild {
pid: pid as libc::pid_t,
pidfd,
path,
}))
}
#[cfg(not(target_arch = "s390x"))]
unsafe fn raw_clone(flags: u64, pidfd: &mut libc::c_int) -> libc::c_long {
unsafe {
libc::syscall(
libc::SYS_clone,
flags,
0usize,
pidfd as *mut libc::c_int,
0usize,
0usize,
)
}
}
#[cfg(target_arch = "s390x")]
unsafe fn raw_clone(flags: u64, pidfd: &mut libc::c_int) -> libc::c_long {
unsafe {
libc::syscall(
libc::SYS_clone,
0usize,
flags,
pidfd as *mut libc::c_int,
0usize,
0usize,
)
}
}
#[cfg(test)]
#[path = "fork_tests.rs"]
mod fork_tests;