]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/unix/process/process_fuchsia.rs
fuchsia: Don't fail to spawn if no stdin exists
[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             Some(envp) => envp.as_ptr(),
52             None => ptr::null(),
53         };
54
55         let make_action = |local_io: &ChildStdio, target_fd| -> io::Result<fdio_spawn_action_t> {
56             if let Some(local_fd) = local_io.fd() {
57                 Ok(fdio_spawn_action_t {
58                     action: FDIO_SPAWN_ACTION_TRANSFER_FD,
59                     local_fd,
60                     target_fd,
61                     ..Default::default()
62                 })
63             } else {
64                 if let ChildStdio::Null = local_io {
65                     // acts as no-op
66                     return Ok(Default::default());
67                 }
68
69                 let mut handle = ZX_HANDLE_INVALID;
70                 let status = fdio_fd_clone(target_fd, &mut handle);
71                 if status == ERR_INVALID_ARGS || status == ERR_NOT_SUPPORTED {
72                     // This descriptor is closed; skip it rather than generating an
73                     // error.
74                     return Ok(Default::default());
75                 }
76                 zx_cvt(status)?;
77
78                 let mut cloned_fd = 0;
79                 zx_cvt(fdio_fd_create(handle, &mut cloned_fd))?;
80
81                 Ok(fdio_spawn_action_t {
82                     action: FDIO_SPAWN_ACTION_TRANSFER_FD,
83                     local_fd: cloned_fd as i32,
84                     target_fd,
85                     ..Default::default()
86                 })
87             }
88         };
89
90         // Clone stdin, stdout, and stderr
91         let action1 = make_action(&stdio.stdin, 0)?;
92         let action2 = make_action(&stdio.stdout, 1)?;
93         let action3 = make_action(&stdio.stderr, 2)?;
94         let actions = [action1, action2, action3];
95
96         // We don't want FileDesc::drop to be called on any stdio. fdio_spawn_etc
97         // always consumes transferred file descriptors.
98         mem::forget(stdio);
99
100         for callback in self.get_closures().iter_mut() {
101             callback()?;
102         }
103
104         let mut process_handle: zx_handle_t = 0;
105         zx_cvt(fdio_spawn_etc(
106             ZX_HANDLE_INVALID,
107             FDIO_SPAWN_CLONE_JOB | FDIO_SPAWN_CLONE_LDSVC | FDIO_SPAWN_CLONE_NAMESPACE,
108             self.get_argv()[0], self.get_argv().as_ptr(), envp,
109             actions.len() as size_t, actions.as_ptr(),
110             &mut process_handle,
111             ptr::null_mut(),
112         ))?;
113         // FIXME: See if we want to do something with that err_msg
114
115         Ok(process_handle)
116     }
117 }
118
119 ////////////////////////////////////////////////////////////////////////////////
120 // Processes
121 ////////////////////////////////////////////////////////////////////////////////
122
123 pub struct Process {
124     handle: Handle,
125 }
126
127 impl Process {
128     pub fn id(&self) -> u32 {
129         self.handle.raw() as u32
130     }
131
132     pub fn kill(&mut self) -> io::Result<()> {
133         use crate::sys::process::zircon::*;
134
135         unsafe { zx_cvt(zx_task_kill(self.handle.raw()))?; }
136
137         Ok(())
138     }
139
140     pub fn wait(&mut self) -> io::Result<ExitStatus> {
141         use crate::default::Default;
142         use crate::sys::process::zircon::*;
143
144         let mut proc_info: zx_info_process_t = Default::default();
145         let mut actual: size_t = 0;
146         let mut avail: size_t = 0;
147
148         unsafe {
149             zx_cvt(zx_object_wait_one(self.handle.raw(), ZX_TASK_TERMINATED,
150                                       ZX_TIME_INFINITE, ptr::null_mut()))?;
151             zx_cvt(zx_object_get_info(self.handle.raw(), ZX_INFO_PROCESS,
152                                       &mut proc_info as *mut _ as *mut libc::c_void,
153                                       mem::size_of::<zx_info_process_t>(), &mut actual,
154                                       &mut avail))?;
155         }
156         if actual != 1 {
157             return Err(io::Error::new(io::ErrorKind::InvalidData,
158                                       "Failed to get exit status of process"));
159         }
160         Ok(ExitStatus::new(proc_info.rec.return_code))
161     }
162
163     pub fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
164         use crate::default::Default;
165         use crate::sys::process::zircon::*;
166
167         let mut proc_info: zx_info_process_t = Default::default();
168         let mut actual: size_t = 0;
169         let mut avail: size_t = 0;
170
171         unsafe {
172             let status = zx_object_wait_one(self.handle.raw(), ZX_TASK_TERMINATED,
173                                             0, ptr::null_mut());
174             match status {
175                 0 => { }, // Success
176                 x if x == ERR_TIMED_OUT => {
177                     return Ok(None);
178                 },
179                 _ => { panic!("Failed to wait on process handle: {}", status); },
180             }
181             zx_cvt(zx_object_get_info(self.handle.raw(), ZX_INFO_PROCESS,
182                                       &mut proc_info as *mut _ as *mut libc::c_void,
183                                       mem::size_of::<zx_info_process_t>(), &mut actual,
184                                       &mut avail))?;
185         }
186         if actual != 1 {
187             return Err(io::Error::new(io::ErrorKind::InvalidData,
188                                       "Failed to get exit status of process"));
189         }
190         Ok(Some(ExitStatus::new(proc_info.rec.return_code)))
191     }
192 }