]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/unix/process/process_fuchsia.rs
Rollup merge of #63630 - andjo403:bump_compiler, r=nikomatsakis
[rust.git] / src / libstd / sys / unix / process / process_fuchsia.rs
1 use crate::io;
2 use crate::mem;
3 use crate::ptr;
4
5 use crate::sys::process::zircon::{Handle, zx_handle_t};
6 use crate::sys::process::process_common::*;
7
8 use libc::size_t;
9
10 ////////////////////////////////////////////////////////////////////////////////
11 // Command
12 ////////////////////////////////////////////////////////////////////////////////
13
14 impl Command {
15     pub fn spawn(&mut self, default: Stdio, needs_stdin: bool)
16                  -> io::Result<(Process, StdioPipes)> {
17         let envp = self.capture_env();
18
19         if self.saw_nul() {
20             return Err(io::Error::new(io::ErrorKind::InvalidInput,
21                                       "nul byte found in provided data"));
22         }
23
24         let (ours, theirs) = self.setup_io(default, needs_stdin)?;
25
26         let process_handle = unsafe { self.do_exec(theirs, envp.as_ref())? };
27
28         Ok((Process { handle: Handle::new(process_handle) }, ours))
29     }
30
31     pub fn exec(&mut self, default: Stdio) -> io::Error {
32         if self.saw_nul() {
33             return io::Error::new(io::ErrorKind::InvalidInput,
34                                   "nul byte found in provided data")
35         }
36
37         match self.setup_io(default, true) {
38             Ok((_, _)) => {
39                 // FIXME: This is tough because we don't support the exec syscalls
40                 unimplemented!();
41             },
42             Err(e) => e,
43         }
44     }
45
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::*;
49
50         let envp = match maybe_envp {
51             // None means to clone the current environment, which is done in the
52             // flags below.
53             None => ptr::null(),
54             Some(envp) => envp.as_ptr(),
55         };
56
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,
61                     local_fd,
62                     target_fd,
63                     ..Default::default()
64                 })
65             } else {
66                 if let ChildStdio::Null = local_io {
67                     // acts as no-op
68                     return Ok(Default::default());
69                 }
70
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
75                     // error.
76                     return Ok(Default::default());
77                 }
78                 zx_cvt(status)?;
79
80                 let mut cloned_fd = 0;
81                 zx_cvt(fdio_fd_create(handle, &mut cloned_fd))?;
82
83                 Ok(fdio_spawn_action_t {
84                     action: FDIO_SPAWN_ACTION_TRANSFER_FD,
85                     local_fd: cloned_fd as i32,
86                     target_fd,
87                     ..Default::default()
88                 })
89             }
90         };
91
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];
97
98         // We don't want FileDesc::drop to be called on any stdio. fdio_spawn_etc
99         // always consumes transferred file descriptors.
100         mem::forget(stdio);
101
102         for callback in self.get_closures().iter_mut() {
103             callback()?;
104         }
105
106         let mut process_handle: zx_handle_t = 0;
107         zx_cvt(fdio_spawn_etc(
108             ZX_HANDLE_INVALID,
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(),
113             &mut process_handle,
114             ptr::null_mut(),
115         ))?;
116         // FIXME: See if we want to do something with that err_msg
117
118         Ok(process_handle)
119     }
120 }
121
122 ////////////////////////////////////////////////////////////////////////////////
123 // Processes
124 ////////////////////////////////////////////////////////////////////////////////
125
126 pub struct Process {
127     handle: Handle,
128 }
129
130 impl Process {
131     pub fn id(&self) -> u32 {
132         self.handle.raw() as u32
133     }
134
135     pub fn kill(&mut self) -> io::Result<()> {
136         use crate::sys::process::zircon::*;
137
138         unsafe { zx_cvt(zx_task_kill(self.handle.raw()))?; }
139
140         Ok(())
141     }
142
143     pub fn wait(&mut self) -> io::Result<ExitStatus> {
144         use crate::default::Default;
145         use crate::sys::process::zircon::*;
146
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;
150
151         unsafe {
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,
157                                       &mut avail))?;
158         }
159         if actual != 1 {
160             return Err(io::Error::new(io::ErrorKind::InvalidData,
161                                       "Failed to get exit status of process"));
162         }
163         Ok(ExitStatus::new(proc_info.rec.return_code))
164     }
165
166     pub fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
167         use crate::default::Default;
168         use crate::sys::process::zircon::*;
169
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;
173
174         unsafe {
175             let status = zx_object_wait_one(self.handle.raw(), ZX_TASK_TERMINATED,
176                                             0, ptr::null_mut());
177             match status {
178                 0 => { }, // Success
179                 x if x == ERR_TIMED_OUT => {
180                     return Ok(None);
181                 },
182                 _ => { panic!("Failed to wait on process handle: {}", status); },
183             }
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,
187                                       &mut avail))?;
188         }
189         if actual != 1 {
190             return Err(io::Error::new(io::ErrorKind::InvalidData,
191                                       "Failed to get exit status of process"));
192         }
193         Ok(Some(ExitStatus::new(proc_info.rec.return_code)))
194     }
195 }