From e3d2257f64bcf058ead7f8d76b91113f086faab9 Mon Sep 17 00:00:00 2001 From: R Aadarsh Date: Sat, 16 May 2026 15:12:34 +0530 Subject: [PATCH] * Make `posix_spawn_file_actions_t` an iterator * Fix bug that caused the spawned process to not start * Make the spawned process inherit the parent's file descriptos --- redox-rt/src/proc.rs | 2 +- src/header/spawn/file_actions.rs | 28 +++++++++++++++++++++++++++- src/header/spawn/mod.rs | 4 +++- src/platform/redox/mod.rs | 14 +++++++++++++- 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/redox-rt/src/proc.rs b/redox-rt/src/proc.rs index 0c067af24d..7ae7c7502a 100644 --- a/redox-rt/src/proc.rs +++ b/redox-rt/src/proc.rs @@ -495,9 +495,9 @@ pub fn fexec_impl( } // Dropping this FD will cause the address space switch. - drop(addrspace_selection_fd); if extrainfo.same_process { + drop(addrspace_selection_fd); unreachable!(); } else { Ok(None) diff --git a/src/header/spawn/file_actions.rs b/src/header/spawn/file_actions.rs index 69369b4a08..f8a3add872 100644 --- a/src/header/spawn/file_actions.rs +++ b/src/header/spawn/file_actions.rs @@ -33,7 +33,7 @@ pub enum Operation { } pub struct OperationNode { - operation: Operation, + pub operation: Operation, next: *const OperationNode, } @@ -44,6 +44,32 @@ pub struct posix_spawn_file_actions_t { tail: *mut OperationNode, } +pub struct FileActionsIter<'a> { + curr: Option<&'a OperationNode>, +} + +impl<'a> Iterator for FileActionsIter<'a> { + type Item = &'a OperationNode; + + fn next(&mut self) -> Option { + let curr = self.curr?; + self.curr = unsafe { curr.next.as_ref() }; + Some(curr) + } +} + +impl<'a> IntoIterator for &'a posix_spawn_file_actions_t { + type Item = &'a OperationNode; + + type IntoIter = FileActionsIter<'a>; + + fn into_iter(self) -> Self::IntoIter { + FileActionsIter { + curr: unsafe { self.head.as_ref() }, + } + } +} + fn copy_op(file_actions: &mut posix_spawn_file_actions_t, op: Operation) -> Result<()> { let new = unsafe { malloc(size_of::()) }; let new_ref = unsafe { diff --git a/src/header/spawn/mod.rs b/src/header/spawn/mod.rs index 2ba437220a..ad55f00908 100644 --- a/src/header/spawn/mod.rs +++ b/src/header/spawn/mod.rs @@ -79,8 +79,10 @@ fn spawn( if let Some(pid) = pid { *pid = v; } - }) + })?; } + + Ok(()) } #[unsafe(no_mangle)] diff --git a/src/platform/redox/mod.rs b/src/platform/redox/mod.rs index 55a0551a56..fb36b17447 100644 --- a/src/platform/redox/mod.rs +++ b/src/platform/redox/mod.rs @@ -1209,6 +1209,15 @@ impl Pal for Sys { let executable = File::open(program, fcntl::O_RDONLY)?; let cwd = path::clone_cwd().unwrap(); let proc_fd = child.proc_fd.unwrap(); + let curr_proc_fd = redox_rt::current_proc_fd(); + let file_table = RtTcb::current() + .thread_fd() + .dup(b"filetable")? + .dup(b"copy")?; + + let new_file_table = child.thr_fd.dup(b"current-filetable")?; + + new_file_table.write(&file_table.as_raw_fd().to_ne_bytes())?; let extra_info = redox_rt::proc::ExtraInfo { cwd: Some(cwd.as_bytes()), @@ -1280,7 +1289,10 @@ impl Pal for Sys { )?; } - Ok(i32::try_from(child.pid).unwrap()) + let start_fd = child.thr_fd.dup(b"start")?; + start_fd.write(&[0])?; + + Ok(pid_t::try_from(child.pid).unwrap()) } fn symlinkat(path1: CStr, fd: c_int, path2: CStr) -> Result<()> {