1 use crate::convert::TryInto;
7 use crate::sys::process::zircon::{Handle, zx_handle_t};
8 use crate::sys::process::process_common::*;
10 use libc::{c_int, size_t};
12 ////////////////////////////////////////////////////////////////////////////////
14 ////////////////////////////////////////////////////////////////////////////////
17 pub fn spawn(&mut self, default: Stdio, needs_stdin: bool)
18 -> io::Result<(Process, StdioPipes)> {
19 let envp = self.capture_env();
22 return Err(io::Error::new(io::ErrorKind::InvalidInput,
23 "nul byte found in provided data"));
26 let (ours, theirs) = self.setup_io(default, needs_stdin)?;
28 let process_handle = unsafe { self.do_exec(theirs, envp.as_ref())? };
30 Ok((Process { handle: Handle::new(process_handle) }, ours))
33 pub fn exec(&mut self, default: Stdio) -> io::Error {
35 return io::Error::new(io::ErrorKind::InvalidInput,
36 "nul byte found in provided data")
39 match self.setup_io(default, true) {
41 // FIXME: This is tough because we don't support the exec syscalls
48 unsafe fn do_exec(&mut self, stdio: ChildPipes, maybe_envp: Option<&CStringArray>)
49 -> io::Result<zx_handle_t> {
50 use crate::sys::process::zircon::*;
52 let envp = match maybe_envp {
53 // None means to clone the current environment, which is done in the
56 Some(envp) => envp.as_ptr(),
59 let make_action = |local_io: &ChildStdio, target_fd| -> io::Result<fdio_spawn_action_t> {
60 if let Some(local_fd) = local_io.fd() {
61 Ok(fdio_spawn_action_t {
62 action: FDIO_SPAWN_ACTION_TRANSFER_FD,
68 if let ChildStdio::Null = local_io {
70 return Ok(Default::default());
73 let mut handle = ZX_HANDLE_INVALID;
74 let status = fdio_fd_clone(target_fd, &mut handle);
75 if status == ERR_INVALID_ARGS || status == ERR_NOT_SUPPORTED {
76 // This descriptor is closed; skip it rather than generating an
78 return Ok(Default::default());
82 let mut cloned_fd = 0;
83 zx_cvt(fdio_fd_create(handle, &mut cloned_fd))?;
85 Ok(fdio_spawn_action_t {
86 action: FDIO_SPAWN_ACTION_TRANSFER_FD,
87 local_fd: cloned_fd as i32,
94 // Clone stdin, stdout, and stderr
95 let action1 = make_action(&stdio.stdin, 0)?;
96 let action2 = make_action(&stdio.stdout, 1)?;
97 let action3 = make_action(&stdio.stderr, 2)?;
98 let actions = [action1, action2, action3];
100 // We don't want FileDesc::drop to be called on any stdio. fdio_spawn_etc
101 // always consumes transferred file descriptors.
104 for callback in self.get_closures().iter_mut() {
108 let mut process_handle: zx_handle_t = 0;
109 zx_cvt(fdio_spawn_etc(
111 FDIO_SPAWN_CLONE_JOB | FDIO_SPAWN_CLONE_LDSVC | FDIO_SPAWN_CLONE_NAMESPACE
112 | FDIO_SPAWN_CLONE_ENVIRON, // this is ignored when envp is non-null
113 self.get_argv()[0], self.get_argv().as_ptr(), envp,
114 actions.len() as size_t, actions.as_ptr(),
118 // FIXME: See if we want to do something with that err_msg
124 ////////////////////////////////////////////////////////////////////////////////
126 ////////////////////////////////////////////////////////////////////////////////
133 pub fn id(&self) -> u32 {
134 self.handle.raw() as u32
137 pub fn kill(&mut self) -> io::Result<()> {
138 use crate::sys::process::zircon::*;
140 unsafe { zx_cvt(zx_task_kill(self.handle.raw()))?; }
145 pub fn wait(&mut self) -> io::Result<ExitStatus> {
146 use crate::default::Default;
147 use crate::sys::process::zircon::*;
149 let mut proc_info: zx_info_process_t = Default::default();
150 let mut actual: size_t = 0;
151 let mut avail: size_t = 0;
154 zx_cvt(zx_object_wait_one(self.handle.raw(), ZX_TASK_TERMINATED,
155 ZX_TIME_INFINITE, ptr::null_mut()))?;
156 zx_cvt(zx_object_get_info(self.handle.raw(), ZX_INFO_PROCESS,
157 &mut proc_info as *mut _ as *mut libc::c_void,
158 mem::size_of::<zx_info_process_t>(), &mut actual,
162 return Err(io::Error::new(io::ErrorKind::InvalidData,
163 "Failed to get exit status of process"));
165 Ok(ExitStatus(proc_info.return_code))
168 pub fn try_wait(&mut self) -> io::Result<Option<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 let status = zx_object_wait_one(self.handle.raw(), ZX_TASK_TERMINATED,
181 x if x == ERR_TIMED_OUT => {
184 _ => { panic!("Failed to wait on process handle: {}", status); },
186 zx_cvt(zx_object_get_info(self.handle.raw(), ZX_INFO_PROCESS,
187 &mut proc_info as *mut _ as *mut libc::c_void,
188 mem::size_of::<zx_info_process_t>(), &mut actual,
192 return Err(io::Error::new(io::ErrorKind::InvalidData,
193 "Failed to get exit status of process"));
195 Ok(Some(ExitStatus(proc_info.return_code)))
199 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
200 pub struct ExitStatus(i64);
203 pub fn success(&self) -> bool {
204 self.code() == Some(0)
207 pub fn code(&self) -> Option<i32> {
208 // FIXME: support extracting return code as an i64
209 self.0.try_into().ok()
212 pub fn signal(&self) -> Option<i32> {
217 impl From<c_int> for ExitStatus {
218 fn from(a: c_int) -> ExitStatus {
223 impl fmt::Display for ExitStatus {
224 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
225 write!(f, "exit code: {}", self.0)