From 36a0e1e01c838e1e47a23ca4233b34ed62e7259e Mon Sep 17 00:00:00 2001 From: Ayush Singh Date: Fri, 29 Mar 2024 17:20:50 +0530 Subject: [PATCH] uefi: process: Add stderr support Implement stderr support in similar fashion. Signed-off-by: Ayush Singh --- std/src/sys/pal/uefi/process.rs | 36 +++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/std/src/sys/pal/uefi/process.rs b/std/src/sys/pal/uefi/process.rs index 7abacc7c12e96..06ce542b0be1f 100644 --- a/std/src/sys/pal/uefi/process.rs +++ b/std/src/sys/pal/uefi/process.rs @@ -92,10 +92,15 @@ impl Command { pub fn output(&mut self) -> io::Result<(ExitStatus, Vec, Vec)> { let mut cmd = uefi_command_internal::Command::load_image(&self.prog)?; + cmd.stdout_init()?; + cmd.stderr_init()?; + let stat = cmd.start_image()?; let stdout = cmd.stdout()?; - Ok((ExitStatus(stat), stdout, Vec::new())) + let stderr = cmd.stderr()?; + + Ok((ExitStatus(stat), stdout, stderr)) } } @@ -263,6 +268,7 @@ mod uefi_command_internal { pub struct Command { handle: NonNull, stdout: Option>, + stderr: Option>, st: Box, } @@ -271,7 +277,7 @@ mod uefi_command_internal { handle: NonNull, st: Box, ) -> Self { - Self { handle, stdout: None, st } + Self { handle, stdout: None, stderr: None, st } } pub fn load_image(p: &OsStr) -> io::Result { @@ -349,6 +355,19 @@ mod uefi_command_internal { Ok(()) } + pub fn stderr_init(&mut self) -> io::Result<()> { + let mut protocol = + helpers::Protocol::create(PipeProtocol::new(), simple_text_output::PROTOCOL_GUID)?; + + self.st.standard_error_handle = protocol.handle().as_ptr(); + self.st.std_err = + protocol.as_mut() as *mut PipeProtocol as *mut simple_text_output::Protocol; + + self.stderr = Some(protocol); + + Ok(()) + } + pub fn stdout(&self) -> io::Result> { if let Some(stdout) = &self.stdout { stdout @@ -361,6 +380,19 @@ mod uefi_command_internal { Err(const_io_error!(io::ErrorKind::NotFound, "stdout not found")) } } + + pub fn stderr(&self) -> io::Result> { + if let Some(stderr) = &self.stderr { + stderr + .as_ref() + .utf8() + .into_string() + .map_err(|_| const_io_error!(io::ErrorKind::Other, "utf8 conversion failed")) + .map(Into::into) + } else { + Err(const_io_error!(io::ErrorKind::NotFound, "stdout not found")) + } + } } impl Drop for Command {