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 // None means to clone the current environment, which is done in the
54 Some(envp) => envp.as_ptr(),
57 let make_action = |local_io: &ChildStdio, target_fd| -> io::Result<fdio_spawn_action_t> {
58 if let Some(local_fd) = local_io.fd() {
59 Ok(fdio_spawn_action_t {
60 action: FDIO_SPAWN_ACTION_TRANSFER_FD,
66 if let ChildStdio::Null = local_io {
68 return Ok(Default::default());
71 let mut handle = ZX_HANDLE_INVALID;
72 let status = fdio_fd_clone(target_fd, &mut handle);
73 if status == ERR_INVALID_ARGS || status == ERR_NOT_SUPPORTED {
74 // This descriptor is closed; skip it rather than generating an
76 return Ok(Default::default());
80 let mut cloned_fd = 0;
81 zx_cvt(fdio_fd_create(handle, &mut cloned_fd))?;
83 Ok(fdio_spawn_action_t {
84 action: FDIO_SPAWN_ACTION_TRANSFER_FD,
85 local_fd: cloned_fd as i32,
92 // Clone stdin, stdout, and stderr
93 let action1 = make_action(&stdio.stdin, 0)?;
94 let action2 = make_action(&stdio.stdout, 1)?;
95 let action3 = make_action(&stdio.stderr, 2)?;
96 let actions = [action1, action2, action3];
98 // We don't want FileDesc::drop to be called on any stdio. fdio_spawn_etc
99 // always consumes transferred file descriptors.
102 for callback in self.get_closures().iter_mut() {
106 let mut process_handle: zx_handle_t = 0;
107 zx_cvt(fdio_spawn_etc(
109 FDIO_SPAWN_CLONE_JOB | FDIO_SPAWN_CLONE_LDSVC | FDIO_SPAWN_CLONE_NAMESPACE
110 | FDIO_SPAWN_CLONE_ENVIRON, // this is ignored when envp is non-null
111 self.get_argv()[0], self.get_argv().as_ptr(), envp,
112 actions.len() as size_t, actions.as_ptr(),
116 // FIXME: See if we want to do something with that err_msg
122 ////////////////////////////////////////////////////////////////////////////////
124 ////////////////////////////////////////////////////////////////////////////////
131 pub fn id(&self) -> u32 {
132 self.handle.raw() as u32
135 pub fn kill(&mut self) -> io::Result<()> {
136 use crate::sys::process::zircon::*;
138 unsafe { zx_cvt(zx_task_kill(self.handle.raw()))?; }
143 pub fn wait(&mut self) -> io::Result<ExitStatus> {
144 use crate::default::Default;
145 use crate::sys::process::zircon::*;
147 let mut proc_info: zx_info_process_t = Default::default();
148 let mut actual: size_t = 0;
149 let mut avail: size_t = 0;
152 zx_cvt(zx_object_wait_one(self.handle.raw(), ZX_TASK_TERMINATED,
153 ZX_TIME_INFINITE, ptr::null_mut()))?;
154 zx_cvt(zx_object_get_info(self.handle.raw(), ZX_INFO_PROCESS,
155 &mut proc_info as *mut _ as *mut libc::c_void,
156 mem::size_of::<zx_info_process_t>(), &mut actual,
160 return Err(io::Error::new(io::ErrorKind::InvalidData,
161 "Failed to get exit status of process"));
163 Ok(ExitStatus::new(proc_info.rec.return_code))
166 pub fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
167 use crate::default::Default;
168 use crate::sys::process::zircon::*;
170 let mut proc_info: zx_info_process_t = Default::default();
171 let mut actual: size_t = 0;
172 let mut avail: size_t = 0;
175 let status = zx_object_wait_one(self.handle.raw(), ZX_TASK_TERMINATED,
179 x if x == ERR_TIMED_OUT => {
182 _ => { panic!("Failed to wait on process handle: {}", status); },
184 zx_cvt(zx_object_get_info(self.handle.raw(), ZX_INFO_PROCESS,
185 &mut proc_info as *mut _ as *mut libc::c_void,
186 mem::size_of::<zx_info_process_t>(), &mut actual,
190 return Err(io::Error::new(io::ErrorKind::InvalidData,
191 "Failed to get exit status of process"));
193 Ok(Some(ExitStatus::new(proc_info.rec.return_code)))