4 use crate::num::{NonZeroI32, NonZeroI64};
7 use crate::sys::process::process_common::*;
8 use crate::sys::process::zircon::{zx_handle_t, Handle};
10 use libc::{c_int, size_t};
12 ////////////////////////////////////////////////////////////////////////////////
14 ////////////////////////////////////////////////////////////////////////////////
21 ) -> io::Result<(Process, StdioPipes)> {
22 let envp = self.capture_env();
25 return Err(io::const_io_error!(
26 io::ErrorKind::InvalidInput,
27 "nul byte found in provided data",
31 let (ours, theirs) = self.setup_io(default, needs_stdin)?;
33 let process_handle = unsafe { self.do_exec(theirs, envp.as_ref())? };
35 Ok((Process { handle: Handle::new(process_handle) }, ours))
38 pub fn output(&mut self) -> io::Result<(ExitStatus, Vec<u8>, Vec<u8>)> {
39 let (proc, pipes) = self.spawn(Stdio::MakePipe, false)?;
40 crate::sys_common::process::wait_with_output(proc, pipes)
43 pub fn exec(&mut self, default: Stdio) -> io::Error {
45 return io::const_io_error!(
46 io::ErrorKind::InvalidInput,
47 "nul byte found in provided data",
51 match self.setup_io(default, true) {
53 // FIXME: This is tough because we don't support the exec syscalls
63 maybe_envp: Option<&CStringArray>,
64 ) -> io::Result<zx_handle_t> {
65 use crate::sys::process::zircon::*;
67 let envp = match maybe_envp {
68 // None means to clone the current environment, which is done in the
71 Some(envp) => envp.as_ptr(),
74 let make_action = |local_io: &ChildStdio, target_fd| -> io::Result<fdio_spawn_action_t> {
75 if let Some(local_fd) = local_io.fd() {
76 Ok(fdio_spawn_action_t {
77 action: FDIO_SPAWN_ACTION_TRANSFER_FD,
83 if let ChildStdio::Null = local_io {
85 return Ok(Default::default());
88 let mut handle = ZX_HANDLE_INVALID;
89 let status = fdio_fd_clone(target_fd, &mut handle);
90 if status == ERR_INVALID_ARGS || status == ERR_NOT_SUPPORTED {
91 // This descriptor is closed; skip it rather than generating an
93 return Ok(Default::default());
97 let mut cloned_fd = 0;
98 zx_cvt(fdio_fd_create(handle, &mut cloned_fd))?;
100 Ok(fdio_spawn_action_t {
101 action: FDIO_SPAWN_ACTION_TRANSFER_FD,
102 local_fd: cloned_fd as i32,
109 // Clone stdin, stdout, and stderr
110 let action1 = make_action(&stdio.stdin, 0)?;
111 let action2 = make_action(&stdio.stdout, 1)?;
112 let action3 = make_action(&stdio.stderr, 2)?;
113 let actions = [action1, action2, action3];
115 // We don't want FileDesc::drop to be called on any stdio. fdio_spawn_etc
116 // always consumes transferred file descriptors.
119 for callback in self.get_closures().iter_mut() {
123 let mut process_handle: zx_handle_t = 0;
124 zx_cvt(fdio_spawn_etc(
127 | FDIO_SPAWN_CLONE_LDSVC
128 | FDIO_SPAWN_CLONE_NAMESPACE
129 | FDIO_SPAWN_CLONE_ENVIRON // this is ignored when envp is non-null
130 | FDIO_SPAWN_CLONE_UTC_CLOCK,
131 self.get_program_cstr().as_ptr(),
132 self.get_argv().as_ptr(),
134 actions.len() as size_t,
139 // FIXME: See if we want to do something with that err_msg
145 ////////////////////////////////////////////////////////////////////////////////
147 ////////////////////////////////////////////////////////////////////////////////
154 pub fn id(&self) -> u32 {
155 self.handle.raw() as u32
158 pub fn kill(&mut self) -> io::Result<()> {
159 use crate::sys::process::zircon::*;
162 zx_cvt(zx_task_kill(self.handle.raw()))?;
168 pub fn wait(&mut self) -> io::Result<ExitStatus> {
169 use crate::default::Default;
170 use crate::sys::process::zircon::*;
172 let mut proc_info: zx_info_process_t = Default::default();
173 let mut actual: size_t = 0;
174 let mut avail: size_t = 0;
177 zx_cvt(zx_object_wait_one(
183 zx_cvt(zx_object_get_info(
186 &mut proc_info as *mut _ as *mut libc::c_void,
187 mem::size_of::<zx_info_process_t>(),
193 return Err(io::const_io_error!(
194 io::ErrorKind::InvalidData,
195 "Failed to get exit status of process",
198 Ok(ExitStatus(proc_info.return_code))
201 pub fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
202 use crate::default::Default;
203 use crate::sys::process::zircon::*;
205 let mut proc_info: zx_info_process_t = Default::default();
206 let mut actual: size_t = 0;
207 let mut avail: size_t = 0;
211 zx_object_wait_one(self.handle.raw(), ZX_TASK_TERMINATED, 0, ptr::null_mut());
214 x if x == ERR_TIMED_OUT => {
218 panic!("Failed to wait on process handle: {status}");
221 zx_cvt(zx_object_get_info(
224 &mut proc_info as *mut _ as *mut libc::c_void,
225 mem::size_of::<zx_info_process_t>(),
231 return Err(io::const_io_error!(
232 io::ErrorKind::InvalidData,
233 "Failed to get exit status of process",
236 Ok(Some(ExitStatus(proc_info.return_code)))
240 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
241 pub struct ExitStatus(i64);
244 pub fn exit_ok(&self) -> Result<(), ExitStatusError> {
245 match NonZeroI64::try_from(self.0) {
246 /* was nonzero */ Ok(failure) => Err(ExitStatusError(failure)),
247 /* was zero, couldn't convert */ Err(_) => Ok(()),
251 pub fn code(&self) -> Option<i32> {
252 // FIXME: support extracting return code as an i64
253 self.0.try_into().ok()
256 pub fn signal(&self) -> Option<i32> {
260 // FIXME: The actually-Unix implementation in process_unix.rs uses WSTOPSIG, WCOREDUMP et al.
261 // I infer from the implementation of `success`, `code` and `signal` above that these are not
262 // available on Fuchsia.
264 // It does not appear that Fuchsia is Unix-like enough to implement ExitStatus (or indeed many
265 // other things from std::os::unix) properly. This veneer is always going to be a bodge. So
266 // while I don't know if these implementations are actually correct, I think they will do for
268 pub fn core_dumped(&self) -> bool {
271 pub fn stopped_signal(&self) -> Option<i32> {
274 pub fn continued(&self) -> bool {
278 pub fn into_raw(&self) -> c_int {
279 // We don't know what someone who calls into_raw() will do with this value, but it should
280 // have the conventional Unix representation. Despite the fact that this is not
281 // standardised in SuS or POSIX, all Unix systems encode the signal and exit status the
282 // same way. (Ie the WIFEXITED, WEXITSTATUS etc. macros have identical behaviour on every
285 // The caller of `std::os::unix::into_raw` is probably wanting a Unix exit status, and may
286 // do their own shifting and masking, or even pass the status to another computer running a
287 // different Unix variant.
289 // The other view would be to say that the caller on Fuchsia ought to know that `into_raw`
290 // will give a raw Fuchsia status (whatever that is - I don't know, personally). That is
291 // not possible here because we must return a c_int because that's what Unix (including
292 // SuS and POSIX) say a wait status is, but Fuchsia apparently uses a u64, so it won't
295 // It seems to me that the right answer would be to provide std::os::fuchsia with its
296 // own ExitStatusExt, rather that trying to provide a not very convincing imitation of
297 // Unix. Ie, std::os::unix::process:ExitStatusExt ought not to exist on Fuchsia. But
298 // fixing this up that is beyond the scope of my efforts now.
299 let exit_status_as_if_unix: u8 = self.0.try_into().expect("Fuchsia process return code bigger than 8 bits, but std::os::unix::ExitStatusExt::into_raw() was called to try to convert the value into a traditional Unix-style wait status, which cannot represent values greater than 255.");
300 let wait_status_as_if_unix = (exit_status_as_if_unix as c_int) << 8;
301 wait_status_as_if_unix
305 /// Converts a raw `c_int` to a type-safe `ExitStatus` by wrapping it without copying.
306 impl From<c_int> for ExitStatus {
307 fn from(a: c_int) -> ExitStatus {
312 impl fmt::Display for ExitStatus {
313 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
314 write!(f, "exit code: {}", self.0)
318 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
319 pub struct ExitStatusError(NonZeroI64);
321 impl Into<ExitStatus> for ExitStatusError {
322 fn into(self) -> ExitStatus {
323 ExitStatus(self.0.into())
327 impl ExitStatusError {
328 pub fn code(self) -> Option<NonZeroI32> {
329 // fixme: affected by the same bug as ExitStatus::code()
330 ExitStatus(self.0.into()).code().map(|st| st.try_into().unwrap())