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