]> git.proxmox.com Git - rustc.git/blob - library/std/src/sys/unix/process/process_fuchsia.rs
New upstream version 1.48.0~beta.8+dfsg1
[rustc.git] / library / std / src / sys / unix / process / process_fuchsia.rs
1 use crate::convert::TryInto;
2 use crate::fmt;
3 use crate::io;
4 use crate::mem;
5 use crate::ptr;
6
7 use crate::sys::process::process_common::*;
8 use crate::sys::process::zircon::{zx_handle_t, Handle};
9
10 use libc::{c_int, size_t};
11
12 ////////////////////////////////////////////////////////////////////////////////
13 // Command
14 ////////////////////////////////////////////////////////////////////////////////
15
16 impl Command {
17 pub fn spawn(
18 &mut self,
19 default: Stdio,
20 needs_stdin: bool,
21 ) -> io::Result<(Process, StdioPipes)> {
22 let envp = self.capture_env();
23
24 if self.saw_nul() {
25 return Err(io::Error::new(
26 io::ErrorKind::InvalidInput,
27 "nul byte found in provided data",
28 ));
29 }
30
31 let (ours, theirs) = self.setup_io(default, needs_stdin)?;
32
33 let process_handle = unsafe { self.do_exec(theirs, envp.as_ref())? };
34
35 Ok((Process { 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, "nul byte found in provided data");
41 }
42
43 match self.setup_io(default, true) {
44 Ok((_, _)) => {
45 // FIXME: This is tough because we don't support the exec syscalls
46 unimplemented!();
47 }
48 Err(e) => e,
49 }
50 }
51
52 unsafe fn do_exec(
53 &mut self,
54 stdio: ChildPipes,
55 maybe_envp: Option<&CStringArray>,
56 ) -> io::Result<zx_handle_t> {
57 use crate::sys::process::zircon::*;
58
59 let envp = match maybe_envp {
60 // None means to clone the current environment, which is done in the
61 // flags below.
62 None => ptr::null(),
63 Some(envp) => envp.as_ptr(),
64 };
65
66 let make_action = |local_io: &ChildStdio, target_fd| -> io::Result<fdio_spawn_action_t> {
67 if let Some(local_fd) = local_io.fd() {
68 Ok(fdio_spawn_action_t {
69 action: FDIO_SPAWN_ACTION_TRANSFER_FD,
70 local_fd,
71 target_fd,
72 ..Default::default()
73 })
74 } else {
75 if let ChildStdio::Null = local_io {
76 // acts as no-op
77 return Ok(Default::default());
78 }
79
80 let mut handle = ZX_HANDLE_INVALID;
81 let status = fdio_fd_clone(target_fd, &mut handle);
82 if status == ERR_INVALID_ARGS || status == ERR_NOT_SUPPORTED {
83 // This descriptor is closed; skip it rather than generating an
84 // error.
85 return Ok(Default::default());
86 }
87 zx_cvt(status)?;
88
89 let mut cloned_fd = 0;
90 zx_cvt(fdio_fd_create(handle, &mut cloned_fd))?;
91
92 Ok(fdio_spawn_action_t {
93 action: FDIO_SPAWN_ACTION_TRANSFER_FD,
94 local_fd: cloned_fd as i32,
95 target_fd,
96 ..Default::default()
97 })
98 }
99 };
100
101 // Clone stdin, stdout, and stderr
102 let action1 = make_action(&stdio.stdin, 0)?;
103 let action2 = make_action(&stdio.stdout, 1)?;
104 let action3 = make_action(&stdio.stderr, 2)?;
105 let actions = [action1, action2, action3];
106
107 // We don't want FileDesc::drop to be called on any stdio. fdio_spawn_etc
108 // always consumes transferred file descriptors.
109 mem::forget(stdio);
110
111 for callback in self.get_closures().iter_mut() {
112 callback()?;
113 }
114
115 let mut process_handle: zx_handle_t = 0;
116 zx_cvt(fdio_spawn_etc(
117 ZX_HANDLE_INVALID,
118 FDIO_SPAWN_CLONE_JOB
119 | FDIO_SPAWN_CLONE_LDSVC
120 | FDIO_SPAWN_CLONE_NAMESPACE
121 | FDIO_SPAWN_CLONE_ENVIRON // this is ignored when envp is non-null
122 | FDIO_SPAWN_CLONE_UTC_CLOCK,
123 self.get_program_cstr().as_ptr(),
124 self.get_argv().as_ptr(),
125 envp,
126 actions.len() as size_t,
127 actions.as_ptr(),
128 &mut process_handle,
129 ptr::null_mut(),
130 ))?;
131 // FIXME: See if we want to do something with that err_msg
132
133 Ok(process_handle)
134 }
135 }
136
137 ////////////////////////////////////////////////////////////////////////////////
138 // Processes
139 ////////////////////////////////////////////////////////////////////////////////
140
141 pub struct Process {
142 handle: Handle,
143 }
144
145 impl Process {
146 pub fn id(&self) -> u32 {
147 self.handle.raw() as u32
148 }
149
150 pub fn kill(&mut self) -> io::Result<()> {
151 use crate::sys::process::zircon::*;
152
153 unsafe {
154 zx_cvt(zx_task_kill(self.handle.raw()))?;
155 }
156
157 Ok(())
158 }
159
160 pub fn wait(&mut self) -> io::Result<ExitStatus> {
161 use crate::default::Default;
162 use crate::sys::process::zircon::*;
163
164 let mut proc_info: zx_info_process_t = Default::default();
165 let mut actual: size_t = 0;
166 let mut avail: size_t = 0;
167
168 unsafe {
169 zx_cvt(zx_object_wait_one(
170 self.handle.raw(),
171 ZX_TASK_TERMINATED,
172 ZX_TIME_INFINITE,
173 ptr::null_mut(),
174 ))?;
175 zx_cvt(zx_object_get_info(
176 self.handle.raw(),
177 ZX_INFO_PROCESS,
178 &mut proc_info as *mut _ as *mut libc::c_void,
179 mem::size_of::<zx_info_process_t>(),
180 &mut actual,
181 &mut avail,
182 ))?;
183 }
184 if actual != 1 {
185 return Err(io::Error::new(
186 io::ErrorKind::InvalidData,
187 "Failed to get exit status of process",
188 ));
189 }
190 Ok(ExitStatus(proc_info.return_code))
191 }
192
193 pub fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
194 use crate::default::Default;
195 use crate::sys::process::zircon::*;
196
197 let mut proc_info: zx_info_process_t = Default::default();
198 let mut actual: size_t = 0;
199 let mut avail: size_t = 0;
200
201 unsafe {
202 let status =
203 zx_object_wait_one(self.handle.raw(), ZX_TASK_TERMINATED, 0, ptr::null_mut());
204 match status {
205 0 => {} // Success
206 x if x == ERR_TIMED_OUT => {
207 return Ok(None);
208 }
209 _ => {
210 panic!("Failed to wait on process handle: {}", status);
211 }
212 }
213 zx_cvt(zx_object_get_info(
214 self.handle.raw(),
215 ZX_INFO_PROCESS,
216 &mut proc_info as *mut _ as *mut libc::c_void,
217 mem::size_of::<zx_info_process_t>(),
218 &mut actual,
219 &mut avail,
220 ))?;
221 }
222 if actual != 1 {
223 return Err(io::Error::new(
224 io::ErrorKind::InvalidData,
225 "Failed to get exit status of process",
226 ));
227 }
228 Ok(Some(ExitStatus(proc_info.return_code)))
229 }
230 }
231
232 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
233 pub struct ExitStatus(i64);
234
235 impl ExitStatus {
236 pub fn success(&self) -> bool {
237 self.code() == Some(0)
238 }
239
240 pub fn code(&self) -> Option<i32> {
241 // FIXME: support extracting return code as an i64
242 self.0.try_into().ok()
243 }
244
245 pub fn signal(&self) -> Option<i32> {
246 None
247 }
248 }
249
250 /// Converts a raw `c_int` to a type-safe `ExitStatus` by wrapping it without copying.
251 impl From<c_int> for ExitStatus {
252 fn from(a: c_int) -> ExitStatus {
253 ExitStatus(a as i64)
254 }
255 }
256
257 impl fmt::Display for ExitStatus {
258 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
259 write!(f, "exit code: {}", self.0)
260 }
261 }