1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
16 use sys::process::magenta::{Handle, launchpad_t, mx_handle_t};
17 use sys::process::process_common::*;
19 ////////////////////////////////////////////////////////////////////////////////
21 ////////////////////////////////////////////////////////////////////////////////
24 pub fn spawn(&mut self, default: Stdio, needs_stdin: bool)
25 -> io::Result<(Process, StdioPipes)> {
27 return Err(io::Error::new(io::ErrorKind::InvalidInput,
28 "nul byte found in provided data"));
31 let (ours, theirs) = self.setup_io(default, needs_stdin)?;
33 let (launchpad, process_handle) = unsafe { self.do_exec(theirs)? };
35 Ok((Process { launchpad: launchpad, handle: Handle::new(process_handle) }, ours))
38 pub fn exec(&mut self, default: Stdio) -> io::Error {
40 return io::Error::new(io::ErrorKind::InvalidInput,
41 "nul byte found in provided data")
44 match self.setup_io(default, true) {
46 // FIXME: This is tough because we don't support the exec syscalls
53 unsafe fn do_exec(&mut self, stdio: ChildPipes)
54 -> io::Result<(*mut launchpad_t, mx_handle_t)> {
55 use sys::process::magenta::*;
57 let job_handle = mx_job_default();
58 let envp = match *self.get_envp() {
59 Some(ref envp) => envp.as_ptr(),
63 // To make sure launchpad_destroy gets called on the launchpad if this function fails
64 struct LaunchpadDestructor(*mut launchpad_t);
65 impl Drop for LaunchpadDestructor {
66 fn drop(&mut self) { unsafe { launchpad_destroy(self.0); } }
69 // Duplicate the job handle
70 let mut job_copy: mx_handle_t = MX_HANDLE_INVALID;
71 mx_cvt(mx_handle_duplicate(job_handle, MX_RIGHT_SAME_RIGHTS, &mut job_copy))?;
73 let mut launchpad: *mut launchpad_t = ptr::null_mut();
74 mx_cvt(launchpad_create(job_copy, self.get_argv()[0], &mut launchpad))?;
75 let launchpad_destructor = LaunchpadDestructor(launchpad);
77 // Set the process argv
78 mx_cvt(launchpad_arguments(launchpad, self.get_argv().len() as i32 - 1,
79 self.get_argv().as_ptr()))?;
80 // Setup the environment vars
81 mx_cvt(launchpad_environ(launchpad, envp))?;
82 mx_cvt(launchpad_add_vdso_vmo(launchpad))?;
83 mx_cvt(launchpad_clone_mxio_root(launchpad))?;
84 // Load the executable
85 mx_cvt(launchpad_elf_load(launchpad, launchpad_vmo_from_file(self.get_argv()[0])))?;
86 mx_cvt(launchpad_load_vdso(launchpad, MX_HANDLE_INVALID))?;
87 mx_cvt(launchpad_clone_mxio_cwd(launchpad))?;
89 // Clone stdin, stdout, and stderr
90 if let Some(fd) = stdio.stdin.fd() {
91 mx_cvt(launchpad_transfer_fd(launchpad, fd, 0))?;
93 mx_cvt(launchpad_clone_fd(launchpad, 0, 0))?;
95 if let Some(fd) = stdio.stdout.fd() {
96 mx_cvt(launchpad_transfer_fd(launchpad, fd, 1))?;
98 mx_cvt(launchpad_clone_fd(launchpad, 1, 1))?;
100 if let Some(fd) = stdio.stderr.fd() {
101 mx_cvt(launchpad_transfer_fd(launchpad, fd, 2))?;
103 mx_cvt(launchpad_clone_fd(launchpad, 2, 2))?;
106 // We don't want FileDesc::drop to be called on any stdio. It would close their fds. The
107 // fds will be closed once the child process finishes.
110 for callback in self.get_closures().iter_mut() {
114 let process_handle = mx_cvt(launchpad_start(launchpad))?;
116 // Successfully started the launchpad
117 mem::forget(launchpad_destructor);
119 Ok((launchpad, process_handle))
123 ////////////////////////////////////////////////////////////////////////////////
125 ////////////////////////////////////////////////////////////////////////////////
128 launchpad: *mut launchpad_t,
133 pub fn id(&self) -> u32 {
134 self.handle.raw() as u32
137 pub fn kill(&mut self) -> io::Result<()> {
138 use sys::process::magenta::*;
140 unsafe { mx_cvt(mx_task_kill(self.handle.raw()))?; }
145 pub fn wait(&mut self) -> io::Result<ExitStatus> {
146 use default::Default;
147 use sys::process::magenta::*;
149 let mut proc_info: mx_info_process_t = Default::default();
150 let mut actual: mx_size_t = 0;
151 let mut avail: mx_size_t = 0;
154 mx_cvt(mx_handle_wait_one(self.handle.raw(), MX_TASK_TERMINATED,
155 MX_TIME_INFINITE, ptr::null_mut()))?;
156 mx_cvt(mx_object_get_info(self.handle.raw(), MX_INFO_PROCESS,
157 &mut proc_info as *mut _ as *mut libc::c_void,
158 mem::size_of::<mx_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::new(proc_info.rec.return_code))
168 pub fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
169 use default::Default;
170 use sys::process::magenta::*;
172 let mut proc_info: mx_info_process_t = Default::default();
173 let mut actual: mx_size_t = 0;
174 let mut avail: mx_size_t = 0;
177 let status = mx_handle_wait_one(self.handle.raw(), MX_TASK_TERMINATED,
181 x if x == ERR_TIMED_OUT => {
184 _ => { panic!("Failed to wait on process handle: {}", status); },
186 mx_cvt(mx_object_get_info(self.handle.raw(), MX_INFO_PROCESS,
187 &mut proc_info as *mut _ as *mut libc::c_void,
188 mem::size_of::<mx_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::new(proc_info.rec.return_code)))
199 impl Drop for Process {
201 use sys::process::magenta::launchpad_destroy;
202 unsafe { launchpad_destroy(self.launchpad); }