From c5fc34a106a706ac12925c4df273a926811d260b Mon Sep 17 00:00:00 2001 From: nsfisis Date: Wed, 24 Jun 2026 03:09:17 +0900 Subject: feat(process): redesign proc_* on PhpResource process handles proc_open/proc_close/proc_get_status/proc_terminate represented the process handle as a PhpMixed, which cannot hold a live child process or its pipes, so they were stubs or todo!(). Model the handle as a new PhpResource::Process variant and child pipes as a StreamBacking::Pipe, with a native Descriptor enum for descriptorspec; proc_open now returns io::Result and fills pipes as IndexMap. Rewire the Symfony Process pipes and the Console terminal/cursor onto the new types, removing the "PhpMixed cannot carry a PhpResource" todo!()s. The remaining todo!()s are genuine syscall leaves (proc_terminate signal delivery, stream_select, stream_set_blocking, posix_kill, pty, fd>=3) left unimplemented since no syscall crate is introduced. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../src/symfony/process/pipes/unix_pipes.rs | 105 ++++++++++++++++----- 1 file changed, 84 insertions(+), 21 deletions(-) (limited to 'crates/shirabe-external-packages/src/symfony/process/pipes/unix_pipes.rs') diff --git a/crates/shirabe-external-packages/src/symfony/process/pipes/unix_pipes.rs b/crates/shirabe-external-packages/src/symfony/process/pipes/unix_pipes.rs index 983ed07..795bb64 100644 --- a/crates/shirabe-external-packages/src/symfony/process/pipes/unix_pipes.rs +++ b/crates/shirabe-external-packages/src/symfony/process/pipes/unix_pipes.rs @@ -1,10 +1,10 @@ //! ref: composer/vendor/symfony/process/Pipes/UnixPipes.php use indexmap::IndexMap; -use shirabe_php_shim::PhpMixed; +use shirabe_php_shim::{self as php, Descriptor, PhpMixed, PhpResource}; use crate::symfony::process::pipes::abstract_pipes::AbstractPipes; -use crate::symfony::process::pipes::pipes_interface::PipesInterface; +use crate::symfony::process::pipes::pipes_interface::{CHUNK_SIZE, PipesInterface}; use crate::symfony::process::process::Process; /// UnixPipes implementation uses unix pipes as handles. @@ -32,24 +32,24 @@ impl UnixPipes { } } -fn descriptor(items: &[&str]) -> PhpMixed { - PhpMixed::List( - items - .iter() - .map(|s| PhpMixed::String(s.to_string())) - .collect(), - ) +fn descriptor(items: &[&str]) -> Descriptor { + match items { + ["pipe", mode] => Descriptor::Pipe(mode.to_string()), + ["file", path, mode] => Descriptor::File(path.to_string(), mode.to_string()), + ["pty"] => Descriptor::Pty, + _ => panic!("unsupported descriptor spec: {:?}", items), + } } impl PipesInterface for UnixPipes { - fn get_descriptors(&mut self) -> Vec { + fn get_descriptors(&mut self) -> Vec { if !self.have_read_support { - // TODO(phase-d): /dev/null is opened as a stream resource and placed directly into the - // proc_open descriptor spec, but the descriptor list is a Vec that cannot - // carry a PhpResource. - todo!( - "UnixPipes::get_descriptors: the /dev/null resource cannot be represented in a PhpMixed descriptor list" - ); + let nullstream = php::fopen("/dev/null", "c").expect("fopen('/dev/null') failed"); + return vec![ + descriptor(&["pipe", "r"]), + Descriptor::Resource(nullstream.clone()), + Descriptor::Resource(nullstream), + ]; } if self.tty_mode == Some(true) { @@ -79,8 +79,71 @@ impl PipesInterface for UnixPipes { IndexMap::new() } - fn read_and_write(&mut self, _blocking: bool, _close: bool) -> IndexMap { - todo!() + fn read_and_write(&mut self, blocking: bool, close: bool) -> IndexMap { + self.inner.unblock(); + let w = self.inner.write(); + + let mut read: IndexMap = IndexMap::new(); + // $r = $this->pipes; unset($r[0]); + let r: Vec<(i64, PhpResource)> = self + .inner + .pipes + .iter() + .filter(|(fd, _)| **fd != 0) + .map(|(fd, pipe)| (*fd, pipe.clone())) + .collect(); + + // TODO(plugin): set_error_handler/restore_error_handler around stream_select is not modeled. + let mut r_sel: Vec = r.iter().map(|(_, p)| p.clone()).collect(); + let mut w_sel: Vec = w.clone().unwrap_or_default(); + let mut e_sel: Vec = Vec::new(); + + // let's have a look if something changed in streams + if (!r_sel.is_empty() || w.is_some()) + && php::stream_select( + &mut r_sel, + &mut w_sel, + &mut e_sel, + 0, + Some(if blocking { + (Process::TIMEOUT_PRECISION * 1e6) as i64 + } else { + 0 + }), + ) + .is_none() + { + // if a system call has been interrupted, forget about it, let's try again + // otherwise, an error occurred, let's reset pipes + if !self.inner.has_system_call_been_interrupted() { + self.inner.pipes = IndexMap::new(); + } + + return read; + } + + for (fd, pipe) in &r { + let mut data = String::new(); + loop { + let chunk = php::fread(pipe, CHUNK_SIZE).unwrap_or_default(); + let len = chunk.len() as i64; + data.push_str(&chunk); + if !(len > 0 && (close || len >= CHUNK_SIZE)) { + break; + } + } + + if !data.is_empty() { + read.insert(*fd, data); + } + + if close && php::feof(pipe) { + php::fclose(pipe); + self.inner.pipes.shift_remove(fd); + } + } + + read } fn have_read_support(&self) -> bool { @@ -88,18 +151,18 @@ impl PipesInterface for UnixPipes { } fn are_open(&self) -> bool { - shirabe_php_shim::php_truthy(&self.inner.pipes) + !self.inner.pipes.is_empty() } fn close(&mut self) { self.inner.close(); } - fn pipes(&self) -> &PhpMixed { + fn pipes(&self) -> &IndexMap { &self.inner.pipes } - fn pipes_mut(&mut self) -> &mut PhpMixed { + fn pipes_mut(&mut self) -> &mut IndexMap { &mut self.inner.pipes } } -- cgit v1.3.1