mirror of
https://github.com/openai/codex.git
synced 2026-12-03 00:41:03 +00:00
Route pipe processes through the shared child launcher (#47605)
## Why Pipe spawning installs Unix `pre_exec` hooks, forcing a fork on macOS even though the shared child launcher supports native spawning. ## What changed - Use the shared `Command` for pipe processes, enabling native macOS spawning while preserving session isolation, explicit descriptor inheritance, and Linux parent-death handling. - Select `ReapOnly` so the pipe adapter retains ownership of process-tree termination. - Track embedded NUL bytes in original Unix command inputs and reject them before spawning, including when invalid `current_dir` or `arg0` values are later replaced. ## Testing Add macOS integration coverage verifying fork avoidance, executable path lookup, pipe I/O, session isolation, and descriptor inheritance. Add Unix regression tests for pipe and PTY fallback under descriptor pressure and for rejecting NUL inputs without running the child. GitOrigin-RevId: f4fa2c1d3c1aee2c4ffa1dc2ff946f9f0b08eb20
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
//! change settings that the native backend cannot inspect. Children receive only
|
||||
//! explicitly supplied environment variables and default to kill-on-drop. Stdio,
|
||||
//! descriptor inheritance, and compatibility fallbacks are configured independently.
|
||||
//! Original Unix inputs retain their NUL validation even when std replaces them.
|
||||
|
||||
use std::ffi::OsStr;
|
||||
#[cfg(unix)]
|
||||
@@ -53,10 +54,6 @@ pub(crate) enum ChildDropPolicy {
|
||||
/// Kill the direct child, then reap it. This is the default.
|
||||
KillAndReap,
|
||||
/// Reap the child when it exits, leaving termination to the caller.
|
||||
#[allow(
|
||||
dead_code,
|
||||
reason = "Used by the pipe adapter in the next stacked change."
|
||||
)]
|
||||
ReapOnly,
|
||||
}
|
||||
|
||||
@@ -71,6 +68,8 @@ pub enum ChildStdin {
|
||||
/// A local command whose complete launch contract is known to both backends.
|
||||
pub struct Command {
|
||||
pub(crate) inner: tokio::process::Command,
|
||||
#[cfg(unix)]
|
||||
saw_nul: bool,
|
||||
pub(crate) process_mode: ProcessMode,
|
||||
pub(crate) descriptor_policy: DescriptorPolicy,
|
||||
pub(crate) fallback: SpawnFallback,
|
||||
@@ -86,6 +85,7 @@ pub struct Command {
|
||||
|
||||
impl Command {
|
||||
pub fn new(program: impl AsRef<OsStr>) -> Self {
|
||||
let program = program.as_ref();
|
||||
let mut inner = tokio::process::Command::new(program);
|
||||
inner
|
||||
.env_clear()
|
||||
@@ -95,6 +95,8 @@ impl Command {
|
||||
.stderr(TokioStdio::piped());
|
||||
Self {
|
||||
inner,
|
||||
#[cfg(unix)]
|
||||
saw_nul: program.as_encoded_bytes().contains(&0),
|
||||
process_mode: ProcessMode::Inherit,
|
||||
descriptor_policy: DescriptorPolicy::Inherit,
|
||||
fallback: SpawnFallback::Compatible,
|
||||
@@ -110,12 +112,19 @@ impl Command {
|
||||
}
|
||||
|
||||
pub fn arg(&mut self, arg: impl AsRef<OsStr>) -> &mut Self {
|
||||
let arg = arg.as_ref();
|
||||
#[cfg(unix)]
|
||||
{
|
||||
self.saw_nul |= arg.as_encoded_bytes().contains(&0);
|
||||
}
|
||||
self.inner.arg(arg);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn args(&mut self, args: impl IntoIterator<Item = impl AsRef<OsStr>>) -> &mut Self {
|
||||
self.inner.args(args);
|
||||
for arg in args {
|
||||
self.arg(arg);
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
@@ -134,6 +143,11 @@ impl Command {
|
||||
}
|
||||
|
||||
pub fn current_dir(&mut self, cwd: impl AsRef<Path>) -> &mut Self {
|
||||
let cwd = cwd.as_ref();
|
||||
#[cfg(unix)]
|
||||
{
|
||||
self.saw_nul |= cwd.as_os_str().as_encoded_bytes().contains(&0);
|
||||
}
|
||||
self.inner.current_dir(cwd);
|
||||
self
|
||||
}
|
||||
@@ -144,10 +158,6 @@ impl Command {
|
||||
}
|
||||
|
||||
/// Choose who owns termination when the child handle is dropped.
|
||||
#[allow(
|
||||
dead_code,
|
||||
reason = "Used by the pipe adapter in the next stacked change."
|
||||
)]
|
||||
pub(crate) fn drop_policy(&mut self, policy: ChildDropPolicy) -> &mut Self {
|
||||
self.drop_policy = policy;
|
||||
self.inner.kill_on_drop(match policy {
|
||||
@@ -200,6 +210,7 @@ impl Command {
|
||||
|
||||
#[cfg(unix)]
|
||||
pub fn arg0(&mut self, arg0: impl AsRef<OsStr>) -> &mut Self {
|
||||
self.saw_nul |= arg0.as_ref().as_encoded_bytes().contains(&0);
|
||||
self.inner.arg0(arg0.as_ref());
|
||||
self.arg0 = Some(arg0.as_ref().to_owned());
|
||||
self
|
||||
@@ -211,8 +222,22 @@ impl Command {
|
||||
job.prepare_suspended_spawn(&mut self.inner);
|
||||
}
|
||||
|
||||
/// Reject original inputs that std replaced with a NUL-free placeholder.
|
||||
#[cfg(unix)]
|
||||
pub(crate) fn validate(&self) -> io::Result<()> {
|
||||
if self.saw_nul {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::InvalidInput,
|
||||
"nul byte found in provided data",
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Launch with native macOS path handling and the existing compatibility fallback.
|
||||
pub fn spawn(mut self) -> io::Result<Child> {
|
||||
#[cfg(unix)]
|
||||
self.validate()?;
|
||||
#[cfg(unix)]
|
||||
if let ProcessMode::NewGroup = self.process_mode {
|
||||
self.inner.process_group(/*pgroup*/ 0);
|
||||
@@ -295,6 +320,10 @@ impl Command {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(test, unix))]
|
||||
#[path = "command_validation_tests.rs"]
|
||||
mod validation_tests;
|
||||
|
||||
#[cfg(all(test, target_os = "macos"))]
|
||||
#[path = "macos_child_tests.rs"]
|
||||
mod tests;
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
//! Invalid command inputs must fail before a child can run on either backend.
|
||||
|
||||
use std::io;
|
||||
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
use crate::Command;
|
||||
use crate::ProcessMode;
|
||||
|
||||
enum Input {
|
||||
Program,
|
||||
Arg,
|
||||
Args,
|
||||
Cwd,
|
||||
Arg0,
|
||||
EnvKey,
|
||||
EnvValue,
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn embedded_nul_is_rejected_before_spawning() -> anyhow::Result<()> {
|
||||
for mode in [ProcessMode::Inherit, ProcessMode::NewSession] {
|
||||
for input in [
|
||||
Input::Program,
|
||||
Input::Arg,
|
||||
Input::Args,
|
||||
Input::Cwd,
|
||||
Input::Arg0,
|
||||
Input::EnvKey,
|
||||
Input::EnvValue,
|
||||
] {
|
||||
let root = tempfile::tempdir()?;
|
||||
let marker = root.path().join("ran");
|
||||
let mut command = Command::new(match input {
|
||||
Input::Program => "/bin/sh\0",
|
||||
Input::Arg
|
||||
| Input::Args
|
||||
| Input::Cwd
|
||||
| Input::Arg0
|
||||
| Input::EnvKey
|
||||
| Input::EnvValue => "/bin/sh",
|
||||
});
|
||||
command
|
||||
.process_mode(mode)
|
||||
.args(["-c", "printf ran > \"$1\"", "sh"])
|
||||
.arg(&marker);
|
||||
match input {
|
||||
Input::Program => {}
|
||||
Input::Arg => {
|
||||
command.arg("bad\0arg");
|
||||
}
|
||||
Input::Args => {
|
||||
command.args(["bad\0arg"]);
|
||||
}
|
||||
Input::Cwd => {
|
||||
command.current_dir("bad\0cwd").current_dir(root.path());
|
||||
}
|
||||
Input::Arg0 => {
|
||||
command.arg0("bad\0arg0").arg0("sh");
|
||||
}
|
||||
Input::EnvKey => {
|
||||
command.env("bad\0key", "value");
|
||||
}
|
||||
Input::EnvValue => {
|
||||
command.env("key", "bad\0value");
|
||||
}
|
||||
}
|
||||
let error = match command.spawn() {
|
||||
Err(error) => error,
|
||||
Ok(child) => {
|
||||
let output = child.wait_with_output().await?;
|
||||
anyhow::bail!(
|
||||
"invalid command ran: {output:?}, marker={}",
|
||||
marker.exists()
|
||||
);
|
||||
}
|
||||
};
|
||||
assert_eq!(error.kind(), io::ErrorKind::InvalidInput);
|
||||
assert!(!marker.exists());
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn literal_nul_placeholder_is_a_valid_argument() -> anyhow::Result<()> {
|
||||
let mut command = Command::new("/bin/echo");
|
||||
command
|
||||
.process_mode(ProcessMode::NewSession)
|
||||
.arg("<string-with-nul>");
|
||||
let output = command.spawn()?.wait_with_output().await?;
|
||||
assert!(output.status.success());
|
||||
assert_eq!(output.stdout, b"<string-with-nul>\n");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn pipe_rejects_nul_arguments_without_side_effects() -> anyhow::Result<()> {
|
||||
let root = tempfile::tempdir()?;
|
||||
let marker = root.path().join("ran");
|
||||
let result = crate::spawn_pipe_process(
|
||||
"/usr/bin/touch",
|
||||
&[
|
||||
marker.to_string_lossy().into_owned(),
|
||||
"bad\0arg".to_string(),
|
||||
],
|
||||
root.path(),
|
||||
&std::collections::HashMap::new(),
|
||||
/*arg0*/ &None,
|
||||
&[],
|
||||
)
|
||||
.await;
|
||||
let error = match result {
|
||||
Err(error) => error,
|
||||
Ok(child) => {
|
||||
let _ = child.exit_rx.await;
|
||||
anyhow::bail!("invalid pipe command ran, marker={}", marker.exists());
|
||||
}
|
||||
};
|
||||
assert_eq!(
|
||||
error.downcast_ref::<io::Error>().map(io::Error::kind),
|
||||
Some(io::ErrorKind::InvalidInput)
|
||||
);
|
||||
assert!(!marker.exists());
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,19 +1,26 @@
|
||||
//! Pipe I/O and process-tree ownership on top of the shared local child launcher.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::ffi::OsStr;
|
||||
use std::io;
|
||||
use std::io::ErrorKind;
|
||||
use std::path::Path;
|
||||
use std::process::Stdio;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex as StdMutex;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
|
||||
use crate::ChildStdin;
|
||||
use crate::Command;
|
||||
#[cfg(unix)]
|
||||
use crate::DescriptorPolicy;
|
||||
#[cfg(unix)]
|
||||
use crate::ProcessMode;
|
||||
use crate::child_command::ChildDropPolicy;
|
||||
use anyhow::Result;
|
||||
use tokio::io::AsyncRead;
|
||||
use tokio::io::AsyncReadExt;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use tokio::io::BufReader;
|
||||
use tokio::process::Command;
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::sync::oneshot;
|
||||
use tokio::task::JoinHandle;
|
||||
@@ -24,9 +31,6 @@ use crate::process::ProcessSignal;
|
||||
use crate::process::SpawnedProcess;
|
||||
use crate::process::exit_code_from_status;
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
use libc;
|
||||
|
||||
#[cfg(windows)]
|
||||
enum WindowsChildTerminator {
|
||||
Job(Arc<crate::win::JobObject>),
|
||||
@@ -147,40 +151,24 @@ async fn spawn_process_with_stdin_mode(
|
||||
if let Some(arg0) = arg0 {
|
||||
command.arg0(arg0);
|
||||
}
|
||||
#[cfg(unix)]
|
||||
command
|
||||
.process_mode(ProcessMode::NewSession)
|
||||
.descriptor_policy(DescriptorPolicy::Explicit)
|
||||
.preserve_fds(inherited_fds);
|
||||
#[cfg(target_os = "linux")]
|
||||
let parent_pid = unsafe { libc::getpid() };
|
||||
#[cfg(unix)]
|
||||
let inherited_fds = inherited_fds.to_vec();
|
||||
#[cfg(unix)]
|
||||
unsafe {
|
||||
command.pre_exec(move || {
|
||||
crate::process_group::detach_from_tty()?;
|
||||
#[cfg(target_os = "linux")]
|
||||
crate::process_group::set_parent_death_signal(parent_pid)?;
|
||||
crate::pty::close_inherited_fds_except(&inherited_fds);
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
command.terminate_on_parent_death();
|
||||
#[cfg(not(unix))]
|
||||
let _ = arg0;
|
||||
command.current_dir(cwd);
|
||||
command.env_clear();
|
||||
for (key, value) in env {
|
||||
command.env(key, value);
|
||||
}
|
||||
for arg in args {
|
||||
command.arg(arg);
|
||||
}
|
||||
match stdin_mode {
|
||||
PipeStdinMode::Piped => {
|
||||
command.stdin(Stdio::piped());
|
||||
}
|
||||
PipeStdinMode::Null => {
|
||||
command.stdin(Stdio::null());
|
||||
}
|
||||
}
|
||||
command.stdout(Stdio::piped());
|
||||
command.stderr(Stdio::piped());
|
||||
command
|
||||
.current_dir(cwd)
|
||||
.envs(env)
|
||||
.args(args)
|
||||
.drop_policy(ChildDropPolicy::ReapOnly)
|
||||
.stdin(match stdin_mode {
|
||||
PipeStdinMode::Piped => ChildStdin::Piped,
|
||||
PipeStdinMode::Null => ChildStdin::Null,
|
||||
});
|
||||
|
||||
#[cfg(windows)]
|
||||
let job = crate::win::JobObject::create().map(Arc::new);
|
||||
@@ -193,6 +181,7 @@ async fn spawn_process_with_stdin_mode(
|
||||
.id()
|
||||
.ok_or_else(|| io::Error::other("missing child pid"))?;
|
||||
let assigned_job = job.and_then(|job| {
|
||||
let crate::child::ChildKind::Tokio(child) = &child.inner;
|
||||
let process_handle = child
|
||||
.raw_handle()
|
||||
.ok_or_else(|| io::Error::other("missing child process handle"))?;
|
||||
@@ -368,3 +357,7 @@ pub async fn spawn_process_no_stdin(
|
||||
#[cfg(all(test, windows))]
|
||||
#[path = "pipe_tests.rs"]
|
||||
mod tests;
|
||||
|
||||
#[cfg(all(test, unix))]
|
||||
#[path = "pipe_unix_tests.rs"]
|
||||
mod unix_tests;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use super::*;
|
||||
use std::process::Stdio;
|
||||
|
||||
#[test]
|
||||
fn process_fallback_interrupt_terminates_root() -> anyhow::Result<()> {
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
//! Pipe and PTY compatibility under inherited-descriptor pressure.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::io;
|
||||
use std::io::Seek;
|
||||
use std::os::fd::AsRawFd;
|
||||
use std::os::fd::FromRawFd;
|
||||
use std::os::fd::OwnedFd;
|
||||
use std::path::Path;
|
||||
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[tokio::test]
|
||||
async fn descriptor_capture_pressure_falls_back_to_direct_spawn() -> anyhow::Result<()> {
|
||||
if std::env::var_os("CODEX_TEST_PIPE_FD_PRESSURE").is_none() {
|
||||
let output = std::process::Command::new(std::env::current_exe()?)
|
||||
.args([
|
||||
"--exact",
|
||||
"pipe::unix_tests::descriptor_capture_pressure_falls_back_to_direct_spawn",
|
||||
"--nocapture",
|
||||
])
|
||||
.env("CODEX_TEST_PIPE_FD_PRESSURE", "1")
|
||||
.output()?;
|
||||
assert!(output.status.success(), "{output:?}");
|
||||
return Ok(());
|
||||
}
|
||||
let root = tempfile::tempdir()?;
|
||||
let path = root.path().join("input");
|
||||
std::fs::write(&path, "preserved")?;
|
||||
let mut source = std::fs::File::open(path)?;
|
||||
let descriptors = (0..17)
|
||||
.map(|_| {
|
||||
// SAFETY: fcntl duplicates a live source into a newly owned slot.
|
||||
let fd = unsafe { libc::fcntl(source.as_raw_fd(), libc::F_DUPFD, 3) };
|
||||
anyhow::ensure!(fd >= 3, "{}", io::Error::last_os_error());
|
||||
Ok(unsafe { OwnedFd::from_raw_fd(fd) })
|
||||
})
|
||||
.collect::<anyhow::Result<Vec<_>>>()?;
|
||||
let targets = descriptors[..16]
|
||||
.iter()
|
||||
.map(AsRawFd::as_raw_fd)
|
||||
.collect::<Vec<_>>();
|
||||
let excluded = descriptors[16].as_raw_fd();
|
||||
let script = format!(
|
||||
"for fd in \"$@\"; do test -e /dev/fd/$fd || exit 10; done; \
|
||||
test ! -e /dev/fd/{excluded} || exit 11; /bin/cat /dev/fd/$1"
|
||||
);
|
||||
let mut args = vec!["-c".to_owned(), script, "sh".to_owned()];
|
||||
args.extend(targets.iter().map(ToString::to_string));
|
||||
let mut limit = std::mem::MaybeUninit::<libc::rlimit>::uninit();
|
||||
// SAFETY: getrlimit initializes writable storage.
|
||||
assert_eq!(
|
||||
unsafe { libc::getrlimit(libc::RLIMIT_NOFILE, limit.as_mut_ptr()) },
|
||||
0
|
||||
);
|
||||
let mut limit = unsafe { limit.assume_init() };
|
||||
limit.rlim_cur = limit.rlim_cur.min(128);
|
||||
// SAFETY: Only this isolated subprocess's soft descriptor limit changes.
|
||||
assert_eq!(unsafe { libc::setrlimit(libc::RLIMIT_NOFILE, &limit) }, 0);
|
||||
enum Transport {
|
||||
Pipe(super::PipeStdinMode),
|
||||
Pty,
|
||||
}
|
||||
for (spare_slots, transport) in [
|
||||
(12, Transport::Pipe(super::PipeStdinMode::Piped)),
|
||||
(12, Transport::Pipe(super::PipeStdinMode::Null)),
|
||||
(12, Transport::Pty),
|
||||
(20, Transport::Pipe(super::PipeStdinMode::Piped)),
|
||||
(20, Transport::Pipe(super::PipeStdinMode::Null)),
|
||||
(20, Transport::Pty),
|
||||
] {
|
||||
source.rewind()?;
|
||||
let mut occupied = Vec::new();
|
||||
loop {
|
||||
let fd = unsafe { libc::fcntl(source.as_raw_fd(), libc::F_DUPFD_CLOEXEC, 3) };
|
||||
if fd == -1 {
|
||||
assert_eq!(
|
||||
io::Error::last_os_error().raw_os_error(),
|
||||
Some(libc::EMFILE)
|
||||
);
|
||||
break;
|
||||
}
|
||||
occupied.push(unsafe { OwnedFd::from_raw_fd(fd) });
|
||||
}
|
||||
// Twelve slots reject capture itself; twenty allow all sixteen copies
|
||||
// but leave too little room for stdio. Both must preserve the old launch.
|
||||
anyhow::ensure!(
|
||||
occupied.len() >= spare_slots,
|
||||
"not enough descriptors to reserve"
|
||||
);
|
||||
occupied.truncate(occupied.len() - spare_slots);
|
||||
let mut spawned = match transport {
|
||||
Transport::Pipe(stdin) => {
|
||||
super::spawn_process_with_stdin_mode(
|
||||
std::ffi::OsStr::new("/bin/sh"),
|
||||
&args,
|
||||
Path::new("."),
|
||||
&HashMap::new(),
|
||||
&None,
|
||||
stdin,
|
||||
&targets,
|
||||
)
|
||||
.await?
|
||||
}
|
||||
Transport::Pty => {
|
||||
crate::spawn_pty_process(
|
||||
"/bin/sh",
|
||||
&args,
|
||||
Path::new("."),
|
||||
&HashMap::new(),
|
||||
&None,
|
||||
crate::TerminalSize::default(),
|
||||
&targets,
|
||||
)
|
||||
.await?
|
||||
}
|
||||
};
|
||||
drop(occupied);
|
||||
spawned.session.close_stdin();
|
||||
let mut stdout = Vec::new();
|
||||
while let Some(bytes) = spawned.stdout_rx.recv().await {
|
||||
stdout.extend(bytes);
|
||||
}
|
||||
let mut stderr = Vec::new();
|
||||
while let Some(bytes) = spawned.stderr_rx.recv().await {
|
||||
stderr.extend(bytes);
|
||||
}
|
||||
assert_eq!(
|
||||
(spawned.exit_rx.await?, stdout, stderr),
|
||||
(0, b"preserved".to_vec(), vec![])
|
||||
);
|
||||
for fd in &descriptors {
|
||||
assert_eq!(unsafe { libc::fcntl(fd.as_raw_fd(), libc::F_GETFD) }, 0);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
//! Exercise the public pipe adapter with native spawning and explicit inheritance.
|
||||
//! This binary isolates atfork registration from the rest of the test suite.
|
||||
|
||||
#![cfg(target_os = "macos")]
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::io::Read;
|
||||
use std::io::Seek;
|
||||
use std::os::fd::AsRawFd;
|
||||
use std::os::fd::FromRawFd;
|
||||
use std::os::fd::OwnedFd;
|
||||
use std::os::unix::fs::symlink;
|
||||
use std::os::unix::process::CommandExt;
|
||||
use std::sync::atomic::AtomicUsize;
|
||||
use std::sync::atomic::Ordering;
|
||||
use std::time::Duration;
|
||||
|
||||
use codex_utils_pty::spawn_pipe_process;
|
||||
use codex_utils_pty::spawn_pipe_process_no_stdin;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
static PARENT_FORKS: AtomicUsize = AtomicUsize::new(0);
|
||||
|
||||
extern "C" fn record_parent_fork() {
|
||||
PARENT_FORKS.fetch_add(1, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn pipe_commands_avoid_fork() -> anyhow::Result<()> {
|
||||
let directory = tempfile::tempdir()?;
|
||||
let cwd = directory.path().canonicalize()?;
|
||||
let executable = std::env::current_exe()?;
|
||||
symlink(&executable, cwd.join("probe"))?;
|
||||
let mut file = std::fs::File::create(cwd.join("preserved"))?;
|
||||
let preserved = inheritable_fd(&file, /*minimum*/ 100)?;
|
||||
let excluded = inheritable_fd(&file, /*minimum*/ 200)?;
|
||||
let args = ["--exact", "pipe_child", "--ignored", "--nocapture"].map(str::to_owned);
|
||||
let mut env = HashMap::from([
|
||||
("PATH".to_string(), ".".to_string()),
|
||||
("PRESERVED".to_string(), preserved.as_raw_fd().to_string()),
|
||||
("EXCLUDED".to_string(), excluded.as_raw_fd().to_string()),
|
||||
(
|
||||
"EXPECTED_CWD".to_string(),
|
||||
cwd.to_string_lossy().into_owned(),
|
||||
),
|
||||
]);
|
||||
// SAFETY: This is the binary's only active test; the permanent parent hook
|
||||
// only updates a lock-free atomic and remains valid until process exit.
|
||||
assert_eq!(
|
||||
unsafe {
|
||||
libc::pthread_atfork(
|
||||
/*prepare*/ None,
|
||||
Some(record_parent_fork),
|
||||
/*child*/ None,
|
||||
)
|
||||
},
|
||||
0
|
||||
);
|
||||
let mut control = std::process::Command::new("/usr/bin/true");
|
||||
// SAFETY: A no-op pre-exec hook forces fork for the positive control.
|
||||
unsafe {
|
||||
control.pre_exec(|| Ok(()));
|
||||
}
|
||||
assert!(control.status()?.success());
|
||||
assert_eq!(PARENT_FORKS.load(Ordering::Relaxed), 1);
|
||||
|
||||
for program in [executable.as_os_str(), "./probe".as_ref(), "probe".as_ref()] {
|
||||
for input in ["hello", ""] {
|
||||
env.insert("EXPECTED_INPUT".to_string(), input.to_string());
|
||||
file.set_len(0)?;
|
||||
file.rewind()?;
|
||||
let inherited = [preserved.as_raw_fd()];
|
||||
let arg0 = Some("pipe-probe".to_string());
|
||||
let mut child = if input.is_empty() {
|
||||
spawn_pipe_process_no_stdin(program, &args, &cwd, &env, &arg0, &inherited).await?
|
||||
} else {
|
||||
spawn_pipe_process(program, &args, &cwd, &env, &arg0, &inherited).await?
|
||||
};
|
||||
if !input.is_empty() {
|
||||
child
|
||||
.session
|
||||
.writer_sender()
|
||||
.send(input.as_bytes().to_vec())
|
||||
.await?;
|
||||
}
|
||||
child.session.close_stdin();
|
||||
let (status, stdout, stderr) = tokio::time::timeout(Duration::from_secs(10), async {
|
||||
let stdout = async {
|
||||
let mut output = Vec::new();
|
||||
while let Some(bytes) = child.stdout_rx.recv().await {
|
||||
output.extend(bytes);
|
||||
}
|
||||
output
|
||||
};
|
||||
let stderr = async {
|
||||
let mut output = Vec::new();
|
||||
while let Some(bytes) = child.stderr_rx.recv().await {
|
||||
output.extend(bytes);
|
||||
}
|
||||
output
|
||||
};
|
||||
tokio::join!(child.exit_rx, stdout, stderr)
|
||||
})
|
||||
.await?;
|
||||
assert_eq!(
|
||||
status?,
|
||||
0,
|
||||
"{}\n{}",
|
||||
String::from_utf8_lossy(&stdout),
|
||||
String::from_utf8_lossy(&stderr)
|
||||
);
|
||||
assert!(String::from_utf8_lossy(&stdout).contains("pipe child assertions completed"));
|
||||
assert!(String::from_utf8_lossy(&stderr).contains("pipe diagnostic"));
|
||||
assert_eq!(std::fs::read(cwd.join("preserved"))?, b"fd");
|
||||
assert_eq!(PARENT_FORKS.load(Ordering::Relaxed), 1);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "child process for pipe_commands_avoid_fork"]
|
||||
fn pipe_child() -> anyhow::Result<()> {
|
||||
assert_eq!(std::env::args().next().as_deref(), Some("pipe-probe"));
|
||||
assert_eq!(
|
||||
std::env::current_dir()?,
|
||||
std::path::PathBuf::from(std::env::var("EXPECTED_CWD")?)
|
||||
);
|
||||
assert!(std::env::var_os("HOME").is_none());
|
||||
// SAFETY: These calls inspect the current process's group and session.
|
||||
assert_eq!(
|
||||
unsafe {
|
||||
(libc::getpgrp(), libc::getsid(/*pid*/ 0))
|
||||
},
|
||||
unsafe { (libc::getpid(), libc::getpid()) }
|
||||
);
|
||||
let preserved = std::env::var("PRESERVED")?.parse::<i32>()?;
|
||||
let excluded = std::env::var("EXCLUDED")?.parse::<i32>()?;
|
||||
// SAFETY: The buffer is valid; the syscall reports an error if the fd was not inherited.
|
||||
assert_eq!(
|
||||
unsafe { libc::write(preserved, b"fd".as_ptr().cast(), 2) },
|
||||
2
|
||||
);
|
||||
// SAFETY: fcntl safely probes whether the unrelated descriptor survived exec.
|
||||
assert_eq!(unsafe { libc::fcntl(excluded, libc::F_GETFD) }, -1);
|
||||
assert_eq!(
|
||||
std::io::Error::last_os_error().raw_os_error(),
|
||||
Some(libc::EBADF)
|
||||
);
|
||||
let mut input = String::new();
|
||||
std::io::stdin().read_to_string(&mut input)?;
|
||||
assert_eq!(input, std::env::var("EXPECTED_INPUT")?);
|
||||
eprintln!("pipe diagnostic");
|
||||
println!("pipe child assertions completed");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn inheritable_fd(file: &std::fs::File, minimum: i32) -> anyhow::Result<OwnedFd> {
|
||||
// SAFETY: The live file is duplicated into a new descriptor without CLOEXEC.
|
||||
let raw = unsafe { libc::fcntl(file.as_raw_fd(), libc::F_DUPFD, minimum) };
|
||||
if raw == -1 {
|
||||
return Err(std::io::Error::last_os_error().into());
|
||||
}
|
||||
// SAFETY: fcntl returned a new owned descriptor.
|
||||
Ok(unsafe { OwnedFd::from_raw_fd(raw) })
|
||||
}
|
||||
Reference in New Issue
Block a user