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