]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/unix/process/process_fuchsia.rs
Auto merge of #37936 - tedsta:fuchsia_std_process, r=alexcrichton
[rust.git] / src / libstd / sys / unix / process / process_fuchsia.rs
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.
4 //
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.
10
11 use io;
12 use libc;
13 use mem;
14 use ptr;
15
16 use sys::process::magenta::{Handle, launchpad_t, mx_handle_t};
17 use sys::process::process_common::*;
18
19 ////////////////////////////////////////////////////////////////////////////////
20 // Command
21 ////////////////////////////////////////////////////////////////////////////////
22
23 impl Command {
24     pub fn spawn(&mut self, default: Stdio, needs_stdin: bool)
25                  -> io::Result<(Process, StdioPipes)> {
26         if self.saw_nul() {
27             return Err(io::Error::new(io::ErrorKind::InvalidInput,
28                                       "nul byte found in provided data"));
29         }
30
31         let (ours, theirs) = self.setup_io(default, needs_stdin)?;
32
33         let (launchpad, process_handle) = unsafe { self.do_exec(theirs)? };
34
35         Ok((Process { launchpad: launchpad, handle: Handle::new(process_handle) }, ours))
36     }
37
38     pub fn exec(&mut self, default: Stdio) -> io::Error {
39         if self.saw_nul() {
40             return io::Error::new(io::ErrorKind::InvalidInput,
41                                   "nul byte found in provided data")
42         }
43
44         match self.setup_io(default, true) {
45             Ok((_, _)) => {
46                 // FIXME: This is tough because we don't support the exec syscalls
47                 unimplemented!();
48             },
49             Err(e) => e,
50         }
51     }
52
53     unsafe fn do_exec(&mut self, stdio: ChildPipes)
54                       -> io::Result<(*mut launchpad_t, mx_handle_t)> {
55         use sys::process::magenta::*;
56
57         let job_handle = mxio_get_startup_handle(mx_hnd_info(MX_HND_TYPE_JOB, 0));
58         let envp = match *self.get_envp() {
59             Some(ref envp) => envp.as_ptr(),
60             None => ptr::null(),
61         };
62
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); } }
67         }
68
69         let mut launchpad: *mut launchpad_t = ptr::null_mut();
70         let launchpad_destructor = LaunchpadDestructor(launchpad);
71
72         // Duplicate the job handle
73         let mut job_copy: mx_handle_t = MX_HANDLE_INVALID;
74         mx_cvt(mx_handle_duplicate(job_handle, MX_RIGHT_SAME_RIGHTS, &mut job_copy))?;
75         // Create a launchpad
76         mx_cvt(launchpad_create(job_copy, self.get_argv()[0], &mut 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))?;
88
89         // Clone stdin, stdout, and stderr
90         if let Some(fd) = stdio.stdin.fd() {
91             launchpad_transfer_fd(launchpad, fd, 0);
92         } else {
93             launchpad_clone_fd(launchpad, 0, 0);
94         }
95         if let Some(fd) = stdio.stdout.fd() {
96             launchpad_transfer_fd(launchpad, fd, 1);
97         } else {
98             launchpad_clone_fd(launchpad, 1, 1);
99         }
100         if let Some(fd) = stdio.stderr.fd() {
101             launchpad_transfer_fd(launchpad, fd, 2);
102         } else {
103             launchpad_clone_fd(launchpad, 2, 2);
104         }
105
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.
108         mem::forget(stdio);
109
110         for callback in self.get_closures().iter_mut() {
111             callback()?;
112         }
113
114         let process_handle = mx_cvt(launchpad_start(launchpad))?;
115
116         // Successfully started the launchpad
117         mem::forget(launchpad_destructor);
118
119         Ok((launchpad, process_handle))
120     }
121 }
122
123 ////////////////////////////////////////////////////////////////////////////////
124 // Processes
125 ////////////////////////////////////////////////////////////////////////////////
126
127 pub struct Process {
128     launchpad: *mut launchpad_t,
129     handle: Handle,
130 }
131
132 impl Process {
133     pub fn id(&self) -> u32 {
134         self.handle.raw() as u32
135     }
136
137     pub fn kill(&mut self) -> io::Result<()> {
138         use sys::process::magenta::*;
139
140         unsafe { mx_cvt(mx_task_kill(self.handle.raw()))?; }
141
142         Ok(())
143     }
144
145     pub fn wait(&mut self) -> io::Result<ExitStatus> {
146         use default::Default;
147         use sys::process::magenta::*;
148
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;
152
153         unsafe {
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,
159                                       &mut avail))?;
160         }
161         if actual != 1 {
162             return Err(io::Error::new(io::ErrorKind::InvalidData,
163                                       "Failed to get exit status of process"));
164         }
165         Ok(ExitStatus::new(proc_info.rec.return_code))
166     }
167 }
168
169 impl Drop for Process {
170     fn drop(&mut self) {
171         use sys::process::magenta::launchpad_destroy;
172         unsafe { launchpad_destroy(self.launchpad); }
173     }
174 }