5 use crate::sys::process::zircon::{Handle, zx_handle_t};
6 use crate::sys::process::process_common::*;
10 ////////////////////////////////////////////////////////////////////////////////
12 ////////////////////////////////////////////////////////////////////////////////
15 pub fn spawn(&mut self, default: Stdio, needs_stdin: bool)
16 -> io::Result<(Process, StdioPipes)> {
17 let envp = self.capture_env();
20 return Err(io::Error::new(io::ErrorKind::InvalidInput,
21 "nul byte found in provided data"));
24 let (ours, theirs) = self.setup_io(default, needs_stdin)?;
26 let process_handle = unsafe { self.do_exec(theirs, envp.as_ref())? };
28 Ok((Process { handle: Handle::new(process_handle) }, ours))
31 pub fn exec(&mut self, default: Stdio) -> io::Error {
33 return io::Error::new(io::ErrorKind::InvalidInput,
34 "nul byte found in provided data")
37 match self.setup_io(default, true) {
39 // FIXME: This is tough because we don't support the exec syscalls
46 unsafe fn do_exec(&mut self, stdio: ChildPipes, maybe_envp: Option<&CStringArray>)
47 -> io::Result<zx_handle_t> {
48 use crate::sys::process::zircon::*;
50 let envp = match maybe_envp {
51 Some(envp) => envp.as_ptr(),
55 let make_action = |local_io: &ChildStdio, target_fd| -> io::Result<fdio_spawn_action_t> {
56 if let Some(local_fd) = local_io.fd() {
57 Ok(fdio_spawn_action_t {
58 action: FDIO_SPAWN_ACTION_TRANSFER_FD,
64 if let ChildStdio::Null = local_io {
66 return Ok(Default::default());
69 let mut handle = ZX_HANDLE_INVALID;
70 let status = fdio_fd_clone(target_fd, &mut handle);
71 if status == ERR_INVALID_ARGS || status == ERR_NOT_SUPPORTED {
72 // This descriptor is closed; skip it rather than generating an
74 return Ok(Default::default());
78 let mut cloned_fd = 0;
79 zx_cvt(fdio_fd_create(handle, &mut cloned_fd))?;
81 Ok(fdio_spawn_action_t {
82 action: FDIO_SPAWN_ACTION_TRANSFER_FD,
83 local_fd: cloned_fd as i32,
90 // Clone stdin, stdout, and stderr
91 let action1 = make_action(&stdio.stdin, 0)?;
92 let action2 = make_action(&stdio.stdout, 1)?;
93 let action3 = make_action(&stdio.stderr, 2)?;
94 let actions = [action1, action2, action3];
96 // We don't want FileDesc::drop to be called on any stdio. fdio_spawn_etc
97 // always consumes transferred file descriptors.
100 for callback in self.get_closures().iter_mut() {
104 let mut process_handle: zx_handle_t = 0;
105 zx_cvt(fdio_spawn_etc(
107 FDIO_SPAWN_CLONE_JOB | FDIO_SPAWN_CLONE_LDSVC | FDIO_SPAWN_CLONE_NAMESPACE,
108 self.get_argv()[0], self.get_argv().as_ptr(), envp,
109 actions.len() as size_t, actions.as_ptr(),
113 // FIXME: See if we want to do something with that err_msg
119 ////////////////////////////////////////////////////////////////////////////////
121 ////////////////////////////////////////////////////////////////////////////////
128 pub fn id(&self) -> u32 {
129 self.handle.raw() as u32
132 pub fn kill(&mut self) -> io::Result<()> {
133 use crate::sys::process::zircon::*;
135 unsafe { zx_cvt(zx_task_kill(self.handle.raw()))?; }
140 pub fn wait(&mut self) -> io::Result<ExitStatus> {
141 use crate::default::Default;
142 use crate::sys::process::zircon::*;
144 let mut proc_info: zx_info_process_t = Default::default();
145 let mut actual: size_t = 0;
146 let mut avail: size_t = 0;
149 zx_cvt(zx_object_wait_one(self.handle.raw(), ZX_TASK_TERMINATED,
150 ZX_TIME_INFINITE, ptr::null_mut()))?;
151 zx_cvt(zx_object_get_info(self.handle.raw(), ZX_INFO_PROCESS,
152 &mut proc_info as *mut _ as *mut libc::c_void,
153 mem::size_of::<zx_info_process_t>(), &mut actual,
157 return Err(io::Error::new(io::ErrorKind::InvalidData,
158 "Failed to get exit status of process"));
160 Ok(ExitStatus::new(proc_info.rec.return_code))
163 pub fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
164 use crate::default::Default;
165 use crate::sys::process::zircon::*;
167 let mut proc_info: zx_info_process_t = Default::default();
168 let mut actual: size_t = 0;
169 let mut avail: size_t = 0;
172 let status = zx_object_wait_one(self.handle.raw(), ZX_TASK_TERMINATED,
176 x if x == ERR_TIMED_OUT => {
179 _ => { panic!("Failed to wait on process handle: {}", status); },
181 zx_cvt(zx_object_get_info(self.handle.raw(), ZX_INFO_PROCESS,
182 &mut proc_info as *mut _ as *mut libc::c_void,
183 mem::size_of::<zx_info_process_t>(), &mut actual,
187 return Err(io::Error::new(io::ErrorKind::InvalidData,
188 "Failed to get exit status of process"));
190 Ok(Some(ExitStatus::new(proc_info.rec.return_code)))