diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index d9e4d8f039..825de0b06a 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -3589,6 +3589,7 @@ dependencies = [ "codex-utils-path-uri", "codex-utils-pty", "futures", + "libc", "pretty_assertions", "regex", "schemars 0.8.22", diff --git a/codex-rs/hooks/Cargo.toml b/codex-rs/hooks/Cargo.toml index 0cdf84418a..cb234830aa 100644 --- a/codex-rs/hooks/Cargo.toml +++ b/codex-rs/hooks/Cargo.toml @@ -32,6 +32,7 @@ tracing = { workspace = true } uuid = { workspace = true, features = ["v4"] } [dev-dependencies] +libc = { workspace = true } pretty_assertions = { workspace = true } tempfile = { workspace = true } tokio = { workspace = true, features = ["macros", "rt-multi-thread", "time"] } diff --git a/codex-rs/hooks/src/engine/command_runner.rs b/codex-rs/hooks/src/engine/command_runner.rs index 36914c6287..d721500c17 100644 --- a/codex-rs/hooks/src/engine/command_runner.rs +++ b/codex-rs/hooks/src/engine/command_runner.rs @@ -1,10 +1,10 @@ use std::collections::HashMap; -#[cfg(not(windows))] use std::ffi::OsStr; use std::ffi::OsString; use std::future::Future; use std::io::ErrorKind; use std::path::Path; +#[cfg(not(unix))] use std::process::Stdio; use std::sync::Arc; use std::sync::Mutex; @@ -13,11 +13,14 @@ use std::time::Duration; use std::time::Instant; use async_channel::Sender; -use codex_protocol::shell_environment::scrub_non_inheritable_env_vars; +use codex_protocol::shell_environment::is_non_inheritable_env_var; +#[cfg(unix)] +use codex_utils_pty::Command; #[cfg(windows)] use codex_utils_pty::JobObject; use futures::future::try_join; use tokio::io::AsyncWriteExt; +#[cfg(not(unix))] use tokio::process::Command; use tokio::sync::Semaphore; use tokio::task::JoinSet; @@ -206,7 +209,7 @@ impl CommandHookRuntime { pub(crate) async fn run_command( runtime: &CommandHookRuntime, handler: &ConfiguredHandler, - command: &str, + command_line: &str, env: &HashMap, input_json: &str, cwd: &Path, @@ -214,21 +217,8 @@ pub(crate) async fn run_command( let started_at = chrono::Utc::now().timestamp(); let started = Instant::now(); - let mut command = build_command(&runtime.shell, command, &runtime.environment, env); - command - .current_dir(cwd) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .stderr(Stdio::piped()) - .kill_on_drop(true); - - #[cfg(unix)] - // Keep process-group cleanup without inheriting the controlling terminal, where - // shell startup can otherwise stop the hook on background terminal I/O. - // SAFETY: detach_from_tty only performs async-signal-safe process setup. - unsafe { - command.pre_exec(codex_utils_pty::process_group::detach_from_tty); - } + let mut command = build_command(&runtime.shell, command_line, &runtime.environment, env); + command.current_dir(cwd); #[cfg(windows)] let mut process_tree_job = JobObject::create().ok(); @@ -401,45 +391,60 @@ fn build_command( env: &HashMap, ) -> Command { let mut command = if shell.program.is_empty() { - default_shell_command(environment) + Command::new(default_shell_program(environment)) } else { Command::new(&shell.program) }; if shell.program.is_empty() { #[cfg(windows)] - command.raw_arg(format!(r#""{command_line}""#)); - + command.arg("/C"); #[cfg(not(windows))] - command.arg(command_line); + command.arg("-lc"); } else { command.args(&shell.args); - - #[cfg(windows)] - if shell.args.iter().any(|arg| arg.eq_ignore_ascii_case("/c")) { - command.raw_arg(format!(r#""{command_line}""#)); - } else { - command.arg(command_line); - } - - #[cfg(not(windows))] + } + #[cfg(windows)] + if shell.program.is_empty() || shell.args.iter().any(|arg| arg.eq_ignore_ascii_case("/c")) { + command.raw_arg(format!(r#""{command_line}""#)); + } else { command.arg(command_line); } - // Replay the session snapshot instead of inheriting the live process environment. - command.env_clear(); - command.envs(environment.iter().cloned()); - command.envs(env); - scrub_non_inheritable_env_vars(command.as_std_mut()); + #[cfg(not(windows))] + command.arg(command_line); + + #[cfg(unix)] + command.process_mode(codex_utils_pty::ProcessMode::NewSession); + #[cfg(not(unix))] + command + .env_clear() + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .kill_on_drop(true); + + // Both launchers start with an empty environment. Replay the session snapshot + // before hook overrides, filtering restricted names from both sources. + command.envs( + environment + .iter() + .map(|(key, value)| (key.as_os_str(), value.as_os_str())) + .chain( + env.iter() + .map(|(key, value)| (OsStr::new(key), OsStr::new(value))), + ) + .filter(|(key, _)| !key.to_str().is_some_and(is_non_inheritable_env_var)), + ); command } -fn default_shell_command(environment: &[(OsString, OsString)]) -> Command { +fn default_shell_program(environment: &[(OsString, OsString)]) -> OsString { #[cfg(windows)] - let (environment_variable, fallback_program, argument) = ("COMSPEC", "cmd.exe", "/C"); + let (environment_variable, fallback_program) = ("COMSPEC", "cmd.exe"); #[cfg(not(windows))] - let (environment_variable, fallback_program, argument) = ("SHELL", "/bin/sh", "-lc"); + let (environment_variable, fallback_program) = ("SHELL", "/bin/sh"); - let program = environment + environment .iter() .find(|(key, _)| { #[cfg(windows)] @@ -454,11 +459,7 @@ fn default_shell_command(environment: &[(OsString, OsString)]) -> Command { } }) .map(|(_, value)| value.clone()) - .unwrap_or_else(|| OsString::from(fallback_program)); - - let mut command = Command::new(program); - command.arg(argument); - command + .unwrap_or_else(|| OsString::from(fallback_program)) } #[cfg(test)] diff --git a/codex-rs/hooks/src/engine/command_runner_tests.rs b/codex-rs/hooks/src/engine/command_runner_tests.rs index d11da77656..1fb23b761c 100644 --- a/codex-rs/hooks/src/engine/command_runner_tests.rs +++ b/codex-rs/hooks/src/engine/command_runner_tests.rs @@ -1,5 +1,4 @@ use std::collections::HashMap; -use std::ffi::OsStr; use std::ffi::OsString; #[cfg(windows)] use std::fs; @@ -33,6 +32,7 @@ use super::CommandShell; use super::ConfiguredHandler; use super::MAX_CONCURRENT_ASYNC_HOOKS; use super::build_command; +use super::default_shell_program; use super::run_command; use crate::events::user_prompt_submit::UserPromptSubmitRequest; @@ -139,8 +139,60 @@ async fn hook_shell_startup_does_not_stop_on_controlling_terminal() { result_sender, ); - let result = run_command(&runtime, &handler, command, &env, "{}", temp.path()).await; + #[cfg(any(target_os = "linux", target_os = "macos"))] + static FORKS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); + #[cfg(any(target_os = "linux", target_os = "macos"))] + { + use std::sync::atomic::Ordering; + extern "C" fn record_parent_fork() { + FORKS.fetch_add(1, Ordering::SeqCst); + } + // Registration cannot be undone, so keep it in this isolated TTY child. + // SAFETY: The callback only updates a lock-free atomic in the parent. + assert_eq!( + unsafe { + libc::pthread_atfork( + /*prepare*/ None, + Some(record_parent_fork), + /*child*/ None, + ) + }, + 0 + ); + let mut baseline = tokio::process::Command::new("/bin/sh"); + baseline.args(["-c", "exit 0"]); + // SAFETY: A no-op callback forces fork for the positive control. + unsafe { + baseline.pre_exec(|| Ok(())); + } + assert!(baseline.status().await.expect("fork control").success()); + assert_eq!(FORKS.load(Ordering::SeqCst), 1); + } + let result = run_command(&runtime, &handler, command, &env, "{}", temp.path()).await; + #[cfg(any(target_os = "linux", target_os = "macos"))] + { + #[cfg(all(target_os = "linux", target_env = "gnu"))] + let native_chdir_supported = { + // SAFETY: Only inspect symbol availability, matching the launcher. + !unsafe { + libc::dlsym( + libc::RTLD_DEFAULT, + c"posix_spawn_file_actions_addchdir_np".as_ptr(), + ) + } + .is_null() + }; + #[cfg(not(all(target_os = "linux", target_env = "gnu")))] + let native_chdir_supported = true; + if native_chdir_supported { + assert_eq!( + FORKS.load(std::sync::atomic::Ordering::SeqCst), + 1, + "hook must avoid forking its parent" + ); + } + } assert_eq!(result.exit_code, Some(0), "stderr: {}", result.stderr); assert_eq!(result.stdout, "hook-ran"); assert_eq!(result.error, None); @@ -330,8 +382,8 @@ async fn command_hook_does_not_expose_configured_noise_auth_token() { assert_eq!(result.error, None); } -#[test] -fn build_command_replays_snapshot_before_hook_overrides_and_scrubbing() { +#[tokio::test] +async fn build_command_replays_snapshot_before_hook_overrides_and_scrubbing() { #[cfg(unix)] let non_unicode_value = OsString::from_vec(vec![b'v', 0xff]); let environment = vec![ @@ -348,10 +400,7 @@ fn build_command_replays_snapshot_before_hook_overrides_and_scrubbing() { OsString::from("captured-noise-token"), ), #[cfg(unix)] - ( - OsString::from("CODEX_HOOK_NON_UNICODE"), - non_unicode_value.clone(), - ), + (OsString::from("CODEX_HOOK_NON_UNICODE"), non_unicode_value), ]; let env = HashMap::from([ ("CODEX_HOOK_OVERRIDE".to_string(), "configured".to_string()), @@ -361,36 +410,45 @@ fn build_command_replays_snapshot_before_hook_overrides_and_scrubbing() { "configured-noise-token".to_string(), ), ]); + #[cfg(not(windows))] + let (program, args, command_line) = ( + "/bin/sh", + vec!["-c".to_string()], + "printf '%s\\n' \"$CODEX_HOOK_SNAPSHOT\" \"$CODEX_HOOK_OVERRIDE\" \"$CODEX_HOOK_SAFE_ENV\" \"${CODEX_EXEC_SERVER_NOISE_AUTH_TOKEN-absent}\" \"$CODEX_HOOK_NON_UNICODE\"", + ); + #[cfg(windows)] + let (program, args, command_line) = ( + "cmd.exe", + vec!["/D".to_string(), "/C".to_string()], + "echo %CODEX_HOOK_SNAPSHOT%&echo %CODEX_HOOK_OVERRIDE%&echo %CODEX_HOOK_SAFE_ENV%&if defined CODEX_EXEC_SERVER_NOISE_AUTH_TOKEN (echo leaked) else (echo absent)", + ); let command = build_command( &CommandShell { - program: "configured-shell".to_string(), - args: vec!["-c".to_string()], + program: program.to_string(), + args, }, - "echo hook-ran", + command_line, &environment, &env, ); - - assert_eq!( - configured_environment_value(&command, "CODEX_HOOK_SNAPSHOT"), - Some(Some(OsString::from("captured"))) - ); - assert_eq!( - configured_environment_value(&command, "CODEX_HOOK_OVERRIDE"), - Some(Some(OsString::from("configured"))) - ); - assert_eq!( - configured_environment_value(&command, "CODEX_HOOK_SAFE_ENV"), - Some(Some(OsString::from("visible"))) - ); - assert_eq!( - configured_environment_value(&command, CODEX_EXEC_SERVER_NOISE_AUTH_TOKEN_ENV_VAR), - None - ); + #[cfg(not(unix))] + let mut command = command; + let output = command + .spawn() + .expect("spawn hook") + .wait_with_output() + .await + .expect("hook output"); + assert!(output.status.success(), "{output:?}"); #[cfg(unix)] assert_eq!( - configured_environment_value(&command, "CODEX_HOOK_NON_UNICODE"), - Some(Some(non_unicode_value)) + output.stdout, + b"captured\nconfigured\nvisible\nabsent\nv\xff\n" + ); + #[cfg(windows)] + assert_eq!( + output.stdout, + b"captured\r\nconfigured\r\nvisible\r\nabsent\r\n" ); } @@ -400,25 +458,9 @@ fn fallback_shell_uses_snapshot() { let (name, program) = ("comspec", r"C:\captured\cmd.exe"); #[cfg(not(windows))] let (name, program) = ("SHELL", "/captured/shell"); - let command = build_command( - &CommandShell { - program: String::new(), - args: Vec::new(), - }, - "echo hook-ran", - &[(OsString::from(name), OsString::from(program))], - &HashMap::new(), - ); - - assert_eq!(command.as_std().get_program(), OsStr::new(program)); - #[cfg(not(windows))] assert_eq!( - command - .as_std() - .get_args() - .map(OsStr::to_os_string) - .collect::>(), - vec![OsString::from("-lc"), OsString::from("echo hook-ran")] + default_shell_program(&[(OsString::from(name), OsString::from(program))]), + OsString::from(program), ); } @@ -445,17 +487,6 @@ fn runtime_with_environment( (runtime, result_receiver) } -fn configured_environment_value( - command: &tokio::process::Command, - name: &str, -) -> Option> { - command - .as_std() - .get_envs() - .find(|(key, _)| *key == OsStr::new(name)) - .map(|(_, value)| value.map(OsStr::to_os_string)) -} - fn write_handler(temp: &TempDir, source: &str) -> ConfiguredHandler { let script_path = temp.path().join("async_hook.py"); std::fs::write(&script_path, source).expect("write async test hook"); @@ -693,3 +724,229 @@ print(json.dumps({{ "shutdown must not deliver a late async result" ); } + +#[cfg(unix)] +async fn process_is_running(pid: &str) -> bool { + let output = tokio::process::Command::new("ps") + .args(["-p", pid, "-o", "stat="]) + .output() + .await + .expect("inspect hook process state"); + assert!(output.stderr.is_empty(), "{output:?}"); + assert!(output.status.success() || output.status.code() == Some(1)); + String::from_utf8_lossy(&output.stdout) + .split_whitespace() + .any(|state| !state.starts_with('Z')) +} + +#[cfg(unix)] +#[tokio::test] +async fn cancelled_and_timed_out_hooks_kill_their_process_group() { + enum Termination { + Cancellation, + Timeout, + } + + for termination in [Termination::Cancellation, Termination::Timeout] { + let temp = tempdir().expect("create temp dir"); + let mut handler = write_handler( + &temp, + r#"import os +from pathlib import Path +import subprocess +import time + +child = subprocess.Popen(['sleep', '60']) +Path('hook-pids.tmp').write_text(f'{os.getpid()} {child.pid}') +Path('hook-pids.tmp').rename('hook-pids') +time.sleep(60) +"#, + ); + handler.timeout_sec = 2; + let ConfiguredHandlerKind::Command { command, env, .. } = &handler.kind else { + panic!("expected command hook"); + }; + let (mut runtime, _result_receiver) = runtime(); + runtime.shell.program = "/bin/sh".into(); + runtime.shell.args = vec!["-c".into()]; + let mut run = Box::pin(run_command( + &runtime, + &handler, + command, + env, + "{}", + temp.path(), + )); + let pids = timeout(Duration::from_secs(10), async { + loop { + if let Ok(pids) = std::fs::read_to_string(temp.path().join("hook-pids")) { + break pids; + } + tokio::select! { + result = &mut run => panic!("hook exited before cancellation: {result:?}"), + _ = sleep(Duration::from_millis(10)) => {} + } + } + }) + .await + .expect("hook and descendant must start"); + match termination { + Termination::Cancellation => drop(run), + Termination::Timeout => { + let result = timeout(Duration::from_secs(10), run) + .await + .expect("hook timeout completes"); + assert_eq!(result.error, Some("hook timed out after 2s".to_string())); + } + } + timeout(Duration::from_secs(10), async { + loop { + let mut all_stopped = true; + for pid in pids.split_whitespace() { + all_stopped &= !process_is_running(pid).await; + } + if all_stopped { + break; + } + sleep(Duration::from_millis(10)).await; + } + }) + .await + .expect("cancellation and timeout must stop both hook and descendant"); + } +} + +#[cfg(unix)] +#[tokio::test] +async fn completed_hook_preserves_background_descendant() { + for exit_code in [0, 23] { + let temp = tempdir().expect("create temp dir"); + let handler = write_handler( + &temp, + &format!( + r#"import subprocess + +subprocess.Popen( + ['sh', '-c', 'i=0; while [ ! -f release-descendant ] && [ "$i" -lt 500 ]; do sleep 0.01; i=$((i + 1)); done; printf survived > descendant-result.tmp; mv descendant-result.tmp descendant-result'], + stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) +raise SystemExit({exit_code}) +"# + ), + ); + let ConfiguredHandlerKind::Command { command, env, .. } = &handler.kind else { + panic!("expected command hook"); + }; + let (mut runtime, _result_receiver) = runtime(); + runtime.shell.program = "/bin/sh".into(); + runtime.shell.args = vec!["-c".into()]; + let result = run_command(&runtime, &handler, command, env, "{}", temp.path()).await; + // Release even on a failed assertion so a failed test does not leak a waiter. + std::fs::write(temp.path().join("release-descendant"), "").expect("release descendant"); + assert_eq!((result.exit_code, result.error), (Some(exit_code), None)); + let result_path = temp.path().join("descendant-result"); + timeout(Duration::from_secs(10), async { + while !result_path.exists() { + sleep(Duration::from_millis(10)).await; + } + }) + .await + .expect("completed hook must leave its descendant running"); + assert_eq!(std::fs::read_to_string(result_path).unwrap(), "survived"); + } +} + +#[cfg(unix)] +#[tokio::test] +async fn exited_hook_leader_with_open_descendant_output_still_times_out() { + let temp = tempdir().expect("create temp dir"); + let mut handler = write_handler( + &temp, + r#"from pathlib import Path +import subprocess + +child = subprocess.Popen(['sleep', '60']) +Path('descendant-pid').write_text(str(child.pid)) +"#, + ); + handler.timeout_sec = 2; + let ConfiguredHandlerKind::Command { command, env, .. } = &handler.kind else { + panic!("expected command hook"); + }; + let (mut runtime, _result_receiver) = runtime(); + runtime.shell.program = "/bin/sh".into(); + runtime.shell.args = vec!["-c".into()]; + let result = run_command(&runtime, &handler, command, env, "{}", temp.path()).await; + assert_eq!(result.error, Some("hook timed out after 2s".to_string())); + let pid = std::fs::read_to_string(temp.path().join("descendant-pid")) + .expect("descendant started before its parent exited"); + timeout(Duration::from_secs(10), async { + while process_is_running(pid.trim()).await { + sleep(Duration::from_millis(10)).await; + } + }) + .await + .expect("hook timeout must stop the descendant after the leader exits"); +} + +#[cfg(unix)] +#[tokio::test] +async fn hook_rejects_nul_arguments_before_execution() { + for nul_in_shell_args in [false, true] { + let root = tempdir().expect("tempdir"); + let (mut runtime, _receiver) = runtime(); + runtime.shell = CommandShell { + program: "/usr/bin/touch".to_string(), + args: if nul_in_shell_args { + vec!["bad\0arg".to_string()] + } else { + Vec::new() + }, + }; + let command = if nul_in_shell_args { "ran" } else { "bad\0arg" }; + let handler = write_handler(&root, ""); + let result = run_command( + &runtime, + &handler, + command, + &HashMap::new(), + "", + root.path(), + ) + .await; + assert!(result.error.is_some(), "invalid hook ran: {result:?}"); + assert_eq!(result.exit_code, None); + assert!(!root.path().join("ran").exists()); + assert!(!root.path().join("").exists()); + } +} + +#[cfg(unix)] +#[tokio::test] +async fn hook_rejects_nul_in_configured_and_default_shell_program() { + for configured in [false, true] { + let root = tempdir().expect("tempdir"); + let (mut runtime, _receiver) = runtime_with_environment(Arc::new(vec![( + OsString::from("SHELL"), + OsString::from("/bin/sh\0"), + )])); + if configured { + runtime.shell.program = "/bin/sh\0".to_string(); + } + let handler = write_handler(&root, ""); + let result = run_command( + &runtime, + &handler, + "printf ran > ran", + &HashMap::new(), + "", + root.path(), + ) + .await; + assert_eq!( + result.error.as_deref(), + Some("nul byte found in provided data") + ); + assert_eq!(result.exit_code, None); + assert!(!root.path().join("ran").exists()); + } +} diff --git a/codex-rs/utils/pty/src/child.rs b/codex-rs/utils/pty/src/child.rs index d9a8365e9b..dfb6ff2bf4 100644 --- a/codex-rs/utils/pty/src/child.rs +++ b/codex-rs/utils/pty/src/child.rs @@ -11,9 +11,9 @@ use tokio::process::ChildStderr; use tokio::process::ChildStdin; use tokio::process::ChildStdout; -#[cfg(target_os = "macos")] -#[path = "macos_child.rs"] -pub(super) mod macos; +#[cfg(any(target_os = "macos", target_os = "linux"))] +#[path = "posix_child.rs"] +pub(super) mod posix; #[cfg(unix)] #[path = "child_reaper.rs"] @@ -34,8 +34,8 @@ pub(super) enum ChildKind { child: Option, reaper: std::sync::mpsc::Sender, }, - #[cfg(target_os = "macos")] - Native(macos::NativeChild), + #[cfg(any(target_os = "macos", target_os = "linux"))] + Native(posix::NativeChild), } impl Child { @@ -46,7 +46,7 @@ impl Child { ChildKind::TokioReapOnly { child, .. } => { child.as_ref().and_then(tokio::process::Child::id) } - #[cfg(target_os = "macos")] + #[cfg(any(target_os = "macos", target_os = "linux"))] ChildKind::Native(child) => child.id(), } } @@ -64,7 +64,7 @@ impl Child { .wait() .await } - #[cfg(target_os = "macos")] + #[cfg(any(target_os = "macos", target_os = "linux"))] ChildKind::Native(child) => child.wait().await, } } @@ -114,7 +114,7 @@ impl Child { .kill() .await } - #[cfg(target_os = "macos")] + #[cfg(any(target_os = "macos", target_os = "linux"))] ChildKind::Native(child) => child.kill().await, } } diff --git a/codex-rs/utils/pty/src/child_command.rs b/codex-rs/utils/pty/src/child_command.rs index 7207351314..ad5d6ee118 100644 --- a/codex-rs/utils/pty/src/child_command.rs +++ b/codex-rs/utils/pty/src/child_command.rs @@ -234,7 +234,7 @@ impl Command { Ok(()) } - /// Launch with native macOS path handling and the existing compatibility fallback. + /// Launch with native platform settings and the existing compatibility fallback. pub fn spawn(mut self) -> io::Result { #[cfg(unix)] self.validate()?; @@ -242,17 +242,22 @@ impl Command { if let ProcessMode::NewGroup = self.process_mode { self.inner.process_group(/*pgroup*/ 0); } - #[cfg(target_os = "macos")] + #[cfg(any(target_os = "macos", target_os = "linux"))] { let command = self.inner.as_std(); let program = command.get_program(); - if (Path::new(program).is_relative() + #[cfg(target_os = "macos")] + let use_native = Path::new(program).is_relative() || self.descriptor_policy == DescriptorPolicy::Explicit || self.fallback == SpawnFallback::ReturnError || matches!(self.process_mode, ProcessMode::NewSession) - || !self.inherited_fds.is_empty()) + || !self.inherited_fds.is_empty(); + #[cfg(target_os = "linux")] + let use_native = + matches!(self.process_mode, ProcessMode::NewSession) && self.parent_pid.is_none(); + if use_native && !program.is_empty() - && let Some(child) = crate::child::macos::NativeChild::spawn(&self)? + && let Some(child) = crate::child::posix::NativeChild::spawn(&self)? { return Ok(child); } @@ -331,3 +336,7 @@ mod tests; #[cfg(all(test, target_os = "macos"))] #[path = "macos_descriptor_tests.rs"] mod descriptor_tests; + +#[cfg(all(test, target_os = "linux"))] +#[path = "linux_child_tests.rs"] +mod linux_tests; diff --git a/codex-rs/utils/pty/src/child_reaper.rs b/codex-rs/utils/pty/src/child_reaper.rs index 3fb871769a..46f25ade9a 100644 --- a/codex-rs/utils/pty/src/child_reaper.rs +++ b/codex-rs/utils/pty/src/child_reaper.rs @@ -5,7 +5,7 @@ //! ownership until reaped, so one live child cannot delay cleanup of another. use std::io; -#[cfg(target_os = "macos")] +#[cfg(any(target_os = "macos", target_os = "linux"))] use std::ptr; use std::sync::Mutex; use std::sync::PoisonError; @@ -14,7 +14,7 @@ use std::time::Duration; /// Exclusive ownership transferred by a child handle after its caller drops it. pub(crate) enum ChildToReap { - #[cfg(target_os = "macos")] + #[cfg(any(target_os = "macos", target_os = "linux"))] Native(libc::pid_t), Tokio(tokio::process::Child), } @@ -47,7 +47,7 @@ pub(crate) fn sender() -> io::Result> { // Amortize scans across bursts without starving reaping when drops continue. pending.extend(receiver.try_iter().take(REAP_BATCH_SIZE - 1)); pending.retain_mut(|child| match child { - #[cfg(target_os = "macos")] + #[cfg(any(target_os = "macos", target_os = "linux"))] ChildToReap::Native(pid) => { // SAFETY: Drop transferred exclusive ownership of this PID. let result = unsafe { libc::waitpid(*pid, ptr::null_mut(), libc::WNOHANG) }; diff --git a/codex-rs/utils/pty/src/child_tests.rs b/codex-rs/utils/pty/src/child_tests.rs index b3dedb376e..452e34374c 100644 --- a/codex-rs/utils/pty/src/child_tests.rs +++ b/codex-rs/utils/pty/src/child_tests.rs @@ -12,6 +12,7 @@ use tokio::io::AsyncReadExt; use tokio::io::AsyncWriteExt; use crate::Command; +use crate::ProcessMode; #[tokio::test] async fn wait_with_output_keeps_eof_pipes_open_until_exit() -> anyhow::Result<()> { @@ -329,3 +330,76 @@ async fn preserving_descriptors_keeps_parent_record_locks() -> anyhow::Result<() assert!(output.status.success(), "{output:?}"); Ok(()) } + +#[tokio::test] +async fn detached_spawn_preserves_child_path_cwd_environment_and_arg0() -> anyhow::Result<()> { + let root = tempfile::tempdir()?; + std::fs::create_dir(root.path().join("bin"))?; + std::os::unix::fs::symlink("/bin/sh", root.path().join("bin/child-shell"))?; + let mut command = Command::new("child-shell"); + command + .current_dir(root.path()) + .env("PATH", "bin") + .env("MARKER", "child-value") + .arg0("custom-shell") + .args(["-c", "printf '%s|%s|' \"$0\" \"$MARKER\"; pwd; read -r line; printf '%s' \"$line\"; printf diagnostic >&2"]) + .process_mode(ProcessMode::NewSession); + let mut child = command.spawn()?; + child + .stdin + .take() + .expect("piped stdin") + .write_all(b"input\n") + .await?; + assert_eq!( + child.wait_with_output().await?, + std::process::Output { + status: std::process::ExitStatus::from_raw(0), + stdout: format!( + "custom-shell|child-value|{}\ninput", + root.path().canonicalize()?.display() + ) + .into_bytes(), + stderr: b"diagnostic".to_vec(), + } + ); + Ok(()) +} + +#[tokio::test] +async fn detached_spawn_preserves_exec_errors_and_executable_text_handling() -> anyhow::Result<()> { + use std::os::unix::fs::PermissionsExt; + + let root = tempfile::tempdir()?; + let program = root.path().join("script"); + let mut missing = Command::new(&program); + missing.process_mode(ProcessMode::NewSession); + let error = missing.spawn().err().expect("missing executable must fail"); + assert_eq!(error.kind(), std::io::ErrorKind::NotFound); + + std::fs::write(&program, "printf '%s' \"$1\"")?; + std::fs::set_permissions(&program, std::fs::Permissions::from_mode(0o755))?; + // Match the existing libc behavior: glibc/macOS retry executable text through + // a shell, whereas musl returns ENOEXEC. + let mut baseline = tokio::process::Command::new(&program); + baseline.env_clear().arg("shell fallback"); + // SAFETY: The legacy route only performs async-signal-safe session setup. + unsafe { + baseline.pre_exec(crate::process_group::detach_from_tty); + } + let expected = baseline.output().await; + let mut script = Command::new(&program); + script + .arg("shell fallback") + .process_mode(ProcessMode::NewSession); + let actual = match script.spawn() { + Ok(child) => child.wait_with_output().await, + Err(error) => Err(error), + }; + let error_details = |error: std::io::Error| (error.kind(), error.raw_os_error()); + assert_eq!( + actual.map_err(error_details), + expected.map_err(error_details) + ); + Ok(()) +} diff --git a/codex-rs/utils/pty/src/linux_child_tests.rs b/codex-rs/utils/pty/src/linux_child_tests.rs new file mode 100644 index 0000000000..dd36e1772d --- /dev/null +++ b/codex-rs/utils/pty/src/linux_child_tests.rs @@ -0,0 +1,63 @@ +//! Linux executable lookup must match the compatibility launcher's PATH semantics. + +use std::os::unix::fs::PermissionsExt; + +use pretty_assertions::assert_eq; + +use crate::Command; +use crate::ProcessMode; + +#[tokio::test] +async fn path_search_stops_at_invalid_candidates() -> anyhow::Result<()> { + let root = tempfile::tempdir()?; + std::fs::create_dir(root.path().join("bin"))?; + std::os::unix::fs::symlink("loop", root.path().join("loop"))?; + let script = root.path().join("bin/server"); + std::fs::write(&script, "#!/bin/sh\nprintf later\n")?; + std::fs::set_permissions(script, std::fs::Permissions::from_mode(/*mode*/ 0o755))?; + for candidate in ["loop".to_owned(), "x".repeat(256)] { + let mut command = Command::new("server"); + command + .current_dir(root.path()) + .env("PATH", format!("{candidate}:bin")); + let expected = command + .inner + .spawn() + .expect_err("invalid first candidate must stop lookup"); + command.process_mode(ProcessMode::NewSession); + let actual = command.spawn().err().expect("native lookup must stop too"); + assert_eq!(actual.raw_os_error(), expected.raw_os_error()); + } + Ok(()) +} + +#[tokio::test] +async fn empty_path_entries_preserve_script_argv0() -> anyhow::Result<()> { + // A sibling test can fork while the script is open for writing, retaining + // the writable descriptor and making exec fail with ETXTBSY. + if std::env::var_os("CODEX_TEST_PATH_ARGV0").is_none() { + let output = std::process::Command::new(std::env::current_exe()?) + .args([ + "--exact", + "child_command::linux_tests::empty_path_entries_preserve_script_argv0", + "--nocapture", + ]) + .env("CODEX_TEST_PATH_ARGV0", "1") + .output()?; + assert!(output.status.success(), "{output:?}"); + return Ok(()); + } + let root = tempfile::tempdir()?; + let script = root.path().join("server"); + std::fs::write(&script, "#!/bin/sh\nprintf '%s' \"$0\"\n")?; + std::fs::set_permissions(script, std::fs::Permissions::from_mode(/*mode*/ 0o755))?; + for path in ["", ":missing", "missing:", ".", "./"] { + let mut command = Command::new("server"); + command.current_dir(root.path()).env("PATH", path); + let expected = command.inner.output().await?; + command.process_mode(ProcessMode::NewSession); + let actual = command.spawn()?.wait_with_output().await?; + assert_eq!(actual, expected); + } + Ok(()) +} diff --git a/codex-rs/utils/pty/src/macos_child_tests.rs b/codex-rs/utils/pty/src/macos_child_tests.rs index b2a29876ff..ad41af3880 100644 --- a/codex-rs/utils/pty/src/macos_child_tests.rs +++ b/codex-rs/utils/pty/src/macos_child_tests.rs @@ -2,7 +2,7 @@ use super::*; use crate::child::ChildKind; -use crate::child::macos::*; +use crate::child::posix::*; use pretty_assertions::assert_eq; use std::fs; use std::os::fd::AsRawFd; diff --git a/codex-rs/utils/pty/src/macos_child.rs b/codex-rs/utils/pty/src/posix_child.rs similarity index 76% rename from codex-rs/utils/pty/src/macos_child.rs rename to codex-rs/utils/pty/src/posix_child.rs index 7d3f5a34d0..5ef37588c7 100644 --- a/codex-rs/utils/pty/src/macos_child.rs +++ b/codex-rs/utils/pty/src/posix_child.rs @@ -1,13 +1,15 @@ -//! Native macOS spawning without rewriting executable paths or argv[0]. +//! Native POSIX spawning without rewriting executable paths or argv[0]. //! //! Spawn attributes and file actions implement the shared command's process-group //! and descriptor policies. Bare commands search the child's PATH. Callers choose //! whether incompatible executable formats and failed searches may retry through -//! Tokio. Each native child owns its PID until it has been reaped. +//! Tokio. Linux uses this for detached sessions with ambient descriptor inheritance. +//! Each native child owns its PID until it has been reaped. use std::ffi::CString; use std::ffi::OsStr; use std::io; +use std::mem::MaybeUninit; use std::os::fd::AsRawFd; use std::os::fd::FromRawFd; use std::os::fd::OwnedFd; @@ -27,12 +29,15 @@ use crate::child_command::ChildDropPolicy; use crate::child::reaper; -// Apple's spawn.h defines this extension on our minimum macOS 12, but libc -// does not expose it. A new session also creates a new process group. +#[cfg(target_os = "linux")] +use libc::POSIX_SPAWN_SETSID; +// Apple exposes this extension in spawn.h, but libc does not yet bind it. +#[cfg(target_os = "macos")] const POSIX_SPAWN_SETSID: libc::c_int = 0x0400; -// libc does not expose this Apple extension. It is available since macOS 10.15, -// before Codex's minimum supported macOS version (12). +// Available since macOS 10.15 and in Rust's bundled musl. Older glibc needs +// runtime detection so this optimization does not raise our minimum libc version. +#[cfg(any(target_os = "macos", target_env = "musl"))] unsafe extern "C" { fn posix_spawn_file_actions_addchdir_np( actions: *mut libc::posix_spawn_file_actions_t, @@ -51,10 +56,25 @@ pub(crate) struct NativeChild { impl NativeChild { /// Spawn the explicit command, retaining the caller's executable spelling and argv[0]. + /// Returns `Ok(None)` when native setup is unsupported or execution needs the + /// caller's compatibility fallback. pub(crate) fn spawn(request: &crate::Command) -> io::Result> { + #[cfg(target_os = "linux")] + if request.descriptor_policy != crate::DescriptorPolicy::Inherit { + return Ok(None); + } let command = request.inner.as_std(); let program = c_string(command.get_program())?; let search_path = !program.as_bytes().contains(&b'/'); + #[cfg(target_os = "linux")] + if search_path + && !command + .get_envs() + .any(|(key, value)| key == "PATH" && value.is_some()) + { + // Preserve the libc-specific default search path in the fallback. + return Ok(None); + } let args = std::iter::once(request.arg0.as_deref().unwrap_or(command.get_program())) .chain(command.get_args()) .map(c_string) @@ -90,6 +110,31 @@ impl NativeChild { ChildDropPolicy::ReapOnly => Some(reaper::sender()?), }; + #[cfg(all(target_os = "linux", target_env = "gnu"))] + let addchdir = { + // SAFETY: dlsym returns the documented function signature when present. + let symbol = unsafe { + libc::dlsym( + libc::RTLD_DEFAULT, + c"posix_spawn_file_actions_addchdir_np".as_ptr(), + ) + }; + if symbol.is_null() { + return Ok(None); + } + unsafe { + std::mem::transmute::< + *mut libc::c_void, + unsafe extern "C" fn( + *mut libc::posix_spawn_file_actions_t, + *const libc::c_char, + ) -> libc::c_int, + >(symbol) + } + }; + #[cfg(any(target_os = "macos", target_env = "musl"))] + let addchdir = posix_spawn_file_actions_addchdir_np; + // Subscribe before spawning so a child that exits immediately cannot be missed. let sigchld = signal(SignalKind::child())?; let (stdin_read, stdin) = match &request.stdin { @@ -113,19 +158,20 @@ impl NativeChild { let stdout = ChildStdout::from_std(OwnedFd::from(stdout_read).into())?; let stderr = ChildStderr::from_std(OwnedFd::from(stderr_read).into())?; - let mut actions = FileActions(ptr::null_mut()); - let mut attrs = Attributes(ptr::null_mut()); + let mut actions = MaybeUninit::uninit(); + // SAFETY: Successful initialization makes each object valid; RAII only + // takes ownership after that success, including if the next init fails. + cvt(unsafe { libc::posix_spawn_file_actions_init(actions.as_mut_ptr()) })?; + let mut actions = FileActions(unsafe { actions.assume_init() }); + let mut attrs = MaybeUninit::uninit(); + cvt(unsafe { libc::posix_spawnattr_init(attrs.as_mut_ptr()) })?; + let mut attrs = Attributes(unsafe { attrs.assume_init() }); let mut pid = 0; // SAFETY: All C strings and pipe descriptors outlive this synchronous // spawn. The initialized action/attribute objects are destroyed by RAII. let result = unsafe { - cvt(libc::posix_spawn_file_actions_init(&mut actions.0))?; - cvt(libc::posix_spawnattr_init(&mut attrs.0))?; if let Some(cwd) = &cwd { - cvt(posix_spawn_file_actions_addchdir_np( - &mut actions.0, - cwd.as_ptr(), - ))?; + cvt(addchdir(&mut actions.0, cwd.as_ptr()))?; } for (target, source) in child_fds.iter().enumerate() { cvt(libc::posix_spawn_file_actions_adddup2( @@ -146,7 +192,6 @@ impl NativeChild { } let group_flags = match request.process_mode { crate::ProcessMode::Inherit => 0, - crate::ProcessMode::NewSession => POSIX_SPAWN_SETSID, crate::ProcessMode::NewGroup => { cvt(libc::posix_spawnattr_setpgroup( &mut attrs.0, @@ -154,19 +199,28 @@ impl NativeChild { ))?; libc::POSIX_SPAWN_SETPGROUP } + crate::ProcessMode::NewSession => POSIX_SPAWN_SETSID, }; - let mut defaults = 0; + let mut defaults = std::mem::zeroed(); cvt_errno(libc::sigemptyset(&mut defaults))?; cvt_errno(libc::sigaddset(&mut defaults, libc::SIGPIPE))?; cvt(libc::posix_spawnattr_setsigdefault(&mut attrs.0, &defaults))?; + #[cfg(target_os = "macos")] let descriptor_flags = match request.descriptor_policy { crate::DescriptorPolicy::Inherit => 0, crate::DescriptorPolicy::Explicit => libc::POSIX_SPAWN_CLOEXEC_DEFAULT, }; - cvt(libc::posix_spawnattr_setflags( + #[cfg(target_os = "linux")] + let descriptor_flags = 0; + let result = libc::posix_spawnattr_setflags( &mut attrs.0, (group_flags | descriptor_flags | libc::POSIX_SPAWN_SETSIGDEF) as _, - ))?; + ); + // Unsupported attributes fail before posix_spawn can select its fallback. + if result == libc::EINVAL && request.fallback == crate::SpawnFallback::Compatible { + return Ok(None); + } + cvt(result)?; let mut spawn = |executable: &CString| { libc::posix_spawn( &mut pid, @@ -190,12 +244,15 @@ impl NativeChild { let mut result = libc::ENOENT; for directory in std::env::split_paths(path) { let mut executable = directory.into_os_string(); + #[cfg(target_os = "macos")] if executable.is_empty() { executable.push("."); } // Preserve the spelling execvp would pass to a shebang // interpreter, including empty entries and trailing slashes. - executable.push("/"); + if !executable.is_empty() { + executable.push("/"); + } executable.push(command.get_program()); if executable.as_bytes().len() >= libc::PATH_MAX as usize { return if request.fallback == crate::SpawnFallback::Compatible { @@ -205,15 +262,13 @@ impl NativeChild { }; } result = spawn(&c_string(&executable)?); - if !matches!( - result, - libc::ENOENT - | libc::ENOTDIR - | libc::EACCES - | libc::ELOOP - | libc::ENAMETOOLONG - ) { - break; + match result { + libc::ENOENT | libc::ENOTDIR | libc::EACCES => {} + #[cfg(target_os = "macos")] + libc::ELOOP | libc::ENAMETOOLONG => {} + #[cfg(all(target_os = "linux", target_env = "gnu"))] + libc::ESTALE | libc::ENODEV | libc::ETIMEDOUT => {} + _ => break, } } result @@ -221,7 +276,10 @@ impl NativeChild { }; // Retain Command's shell fallback and exact PATH search errors. if request.fallback == crate::SpawnFallback::Compatible - && (result == libc::ENOEXEC || (search_path && result != 0)) + && (result == libc::ENOEXEC + || (search_path && result != 0) + || (matches!(request.process_mode, crate::ProcessMode::NewSession) + && matches!(result, libc::EINVAL | libc::EPERM))) { return Ok(None); } @@ -380,22 +438,18 @@ struct Attributes(libc::posix_spawnattr_t); impl Drop for FileActions { fn drop(&mut self) { - if !self.0.is_null() { - // SAFETY: This object was initialized by posix_spawn_file_actions_init. - unsafe { - libc::posix_spawn_file_actions_destroy(&mut self.0); - } + // SAFETY: This object was initialized by posix_spawn_file_actions_init. + unsafe { + libc::posix_spawn_file_actions_destroy(&mut self.0); } } } impl Drop for Attributes { fn drop(&mut self) { - if !self.0.is_null() { - // SAFETY: This object was initialized by posix_spawnattr_init. - unsafe { - libc::posix_spawnattr_destroy(&mut self.0); - } + // SAFETY: This object was initialized by posix_spawnattr_init. + unsafe { + libc::posix_spawnattr_destroy(&mut self.0); } } }