1 #![allow(non_camel_case_types, unused)]
2 
3 use crate::convert::TryInto;
4 use crate::io;
5 use crate::mem::MaybeUninit;
6 use crate::os::raw::c_char;
7 
8 use libc::{c_int, c_void, size_t};
9 
10 pub type zx_handle_t = u32;
11 pub type zx_vaddr_t = usize;
12 pub type zx_rights_t = u32;
13 pub type zx_status_t = i32;
14 
15 pub const ZX_HANDLE_INVALID: zx_handle_t = 0;
16 
17 pub type zx_time_t = i64;
18 pub const ZX_TIME_INFINITE: zx_time_t = i64::MAX;
19 
20 pub type zx_signals_t = u32;
21 
22 pub const ZX_OBJECT_SIGNAL_3: zx_signals_t = 1 << 3;
23 
24 pub const ZX_TASK_TERMINATED: zx_signals_t = ZX_OBJECT_SIGNAL_3;
25 
26 pub const ZX_RIGHT_SAME_RIGHTS: zx_rights_t = 1 << 31;
27 
28 // The upper four bits gives the minor version.
29 pub type zx_object_info_topic_t = u32;
30 
31 pub const ZX_INFO_PROCESS: zx_object_info_topic_t = 3 | (1 << 28);
32 
33 pub type zx_info_process_flags_t = u32;
34 
zx_cvt<T>(t: T) -> io::Result<T> where T: TryInto<zx_status_t> + Copy,35 pub fn zx_cvt<T>(t: T) -> io::Result<T>
36 where
37     T: TryInto<zx_status_t> + Copy,
38 {
39     if let Ok(status) = TryInto::try_into(t) {
40         if status < 0 { Err(io::Error::from_raw_os_error(status)) } else { Ok(t) }
41     } else {
42         Err(io::Error::last_os_error())
43     }
44 }
45 
46 // Safe wrapper around zx_handle_t
47 pub struct Handle {
48     raw: zx_handle_t,
49 }
50 
51 impl Handle {
new(raw: zx_handle_t) -> Handle52     pub fn new(raw: zx_handle_t) -> Handle {
53         Handle { raw }
54     }
55 
raw(&self) -> zx_handle_t56     pub fn raw(&self) -> zx_handle_t {
57         self.raw
58     }
59 }
60 
61 impl Drop for Handle {
drop(&mut self)62     fn drop(&mut self) {
63         unsafe {
64             zx_cvt(zx_handle_close(self.raw)).expect("Failed to close zx_handle_t");
65         }
66     }
67 }
68 
69 // Returned for topic ZX_INFO_PROCESS
70 #[derive(Default)]
71 #[repr(C)]
72 pub struct zx_info_process_t {
73     pub return_code: i64,
74     pub start_time: zx_time_t,
75     pub flags: zx_info_process_flags_t,
76     pub reserved1: u32,
77 }
78 
79 extern "C" {
zx_job_default() -> zx_handle_t80     pub fn zx_job_default() -> zx_handle_t;
81 
zx_task_kill(handle: zx_handle_t) -> zx_status_t82     pub fn zx_task_kill(handle: zx_handle_t) -> zx_status_t;
83 
zx_handle_close(handle: zx_handle_t) -> zx_status_t84     pub fn zx_handle_close(handle: zx_handle_t) -> zx_status_t;
85 
zx_handle_duplicate( handle: zx_handle_t, rights: zx_rights_t, out: *const zx_handle_t, ) -> zx_handle_t86     pub fn zx_handle_duplicate(
87         handle: zx_handle_t,
88         rights: zx_rights_t,
89         out: *const zx_handle_t,
90     ) -> zx_handle_t;
91 
zx_object_wait_one( handle: zx_handle_t, signals: zx_signals_t, timeout: zx_time_t, pending: *mut zx_signals_t, ) -> zx_status_t92     pub fn zx_object_wait_one(
93         handle: zx_handle_t,
94         signals: zx_signals_t,
95         timeout: zx_time_t,
96         pending: *mut zx_signals_t,
97     ) -> zx_status_t;
98 
zx_object_get_info( handle: zx_handle_t, topic: u32, buffer: *mut c_void, buffer_size: size_t, actual_size: *mut size_t, avail: *mut size_t, ) -> zx_status_t99     pub fn zx_object_get_info(
100         handle: zx_handle_t,
101         topic: u32,
102         buffer: *mut c_void,
103         buffer_size: size_t,
104         actual_size: *mut size_t,
105         avail: *mut size_t,
106     ) -> zx_status_t;
107 }
108 
109 #[derive(Default)]
110 #[repr(C)]
111 pub struct fdio_spawn_action_t {
112     pub action: u32,
113     pub reserved0: u32,
114     pub local_fd: i32,
115     pub target_fd: i32,
116     pub reserved1: u64,
117 }
118 
119 extern "C" {
fdio_spawn_etc( job: zx_handle_t, flags: u32, path: *const c_char, argv: *const *const c_char, envp: *const *const c_char, action_count: size_t, actions: *const fdio_spawn_action_t, process: *mut zx_handle_t, err_msg: *mut c_char, ) -> zx_status_t120     pub fn fdio_spawn_etc(
121         job: zx_handle_t,
122         flags: u32,
123         path: *const c_char,
124         argv: *const *const c_char,
125         envp: *const *const c_char,
126         action_count: size_t,
127         actions: *const fdio_spawn_action_t,
128         process: *mut zx_handle_t,
129         err_msg: *mut c_char,
130     ) -> zx_status_t;
131 
fdio_fd_clone(fd: c_int, out_handle: *mut zx_handle_t) -> zx_status_t132     pub fn fdio_fd_clone(fd: c_int, out_handle: *mut zx_handle_t) -> zx_status_t;
fdio_fd_create(handle: zx_handle_t, fd: *mut c_int) -> zx_status_t133     pub fn fdio_fd_create(handle: zx_handle_t, fd: *mut c_int) -> zx_status_t;
134 }
135 
136 // fdio_spawn_etc flags
137 
138 pub const FDIO_SPAWN_CLONE_JOB: u32 = 0x0001;
139 pub const FDIO_SPAWN_CLONE_LDSVC: u32 = 0x0002;
140 pub const FDIO_SPAWN_CLONE_NAMESPACE: u32 = 0x0004;
141 pub const FDIO_SPAWN_CLONE_STDIO: u32 = 0x0008;
142 pub const FDIO_SPAWN_CLONE_ENVIRON: u32 = 0x0010;
143 pub const FDIO_SPAWN_CLONE_UTC_CLOCK: u32 = 0x0020;
144 pub const FDIO_SPAWN_CLONE_ALL: u32 = 0xFFFF;
145 
146 // fdio_spawn_etc actions
147 
148 pub const FDIO_SPAWN_ACTION_CLONE_FD: u32 = 0x0001;
149 pub const FDIO_SPAWN_ACTION_TRANSFER_FD: u32 = 0x0002;
150 
151 // Errors
152 
153 #[allow(unused)]
154 pub const ERR_INTERNAL: zx_status_t = -1;
155 
156 // ERR_NOT_SUPPORTED: The operation is not implemented, supported,
157 // or enabled.
158 #[allow(unused)]
159 pub const ERR_NOT_SUPPORTED: zx_status_t = -2;
160 
161 // ERR_NO_RESOURCES: The system was not able to allocate some resource
162 // needed for the operation.
163 #[allow(unused)]
164 pub const ERR_NO_RESOURCES: zx_status_t = -3;
165 
166 // ERR_NO_MEMORY: The system was not able to allocate memory needed
167 // for the operation.
168 #[allow(unused)]
169 pub const ERR_NO_MEMORY: zx_status_t = -4;
170 
171 // ERR_CALL_FAILED: The second phase of zx_channel_call(; did not complete
172 // successfully.
173 #[allow(unused)]
174 pub const ERR_CALL_FAILED: zx_status_t = -5;
175 
176 // ERR_INTERRUPTED_RETRY: The system call was interrupted, but should be
177 // retried.  This should not be seen outside of the VDSO.
178 #[allow(unused)]
179 pub const ERR_INTERRUPTED_RETRY: zx_status_t = -6;
180 
181 // ======= Parameter errors =======
182 // ERR_INVALID_ARGS: an argument is invalid, ex. null pointer
183 #[allow(unused)]
184 pub const ERR_INVALID_ARGS: zx_status_t = -10;
185 
186 // ERR_BAD_HANDLE: A specified handle value does not refer to a handle.
187 #[allow(unused)]
188 pub const ERR_BAD_HANDLE: zx_status_t = -11;
189 
190 // ERR_WRONG_TYPE: The subject of the operation is the wrong type to
191 // perform the operation.
192 // Example: Attempting a message_read on a thread handle.
193 #[allow(unused)]
194 pub const ERR_WRONG_TYPE: zx_status_t = -12;
195 
196 // ERR_BAD_SYSCALL: The specified syscall number is invalid.
197 #[allow(unused)]
198 pub const ERR_BAD_SYSCALL: zx_status_t = -13;
199 
200 // ERR_OUT_OF_RANGE: An argument is outside the valid range for this
201 // operation.
202 #[allow(unused)]
203 pub const ERR_OUT_OF_RANGE: zx_status_t = -14;
204 
205 // ERR_BUFFER_TOO_SMALL: A caller provided buffer is too small for
206 // this operation.
207 #[allow(unused)]
208 pub const ERR_BUFFER_TOO_SMALL: zx_status_t = -15;
209 
210 // ======= Precondition or state errors =======
211 // ERR_BAD_STATE: operation failed because the current state of the
212 // object does not allow it, or a precondition of the operation is
213 // not satisfied
214 #[allow(unused)]
215 pub const ERR_BAD_STATE: zx_status_t = -20;
216 
217 // ERR_TIMED_OUT: The time limit for the operation elapsed before
218 // the operation completed.
219 #[allow(unused)]
220 pub const ERR_TIMED_OUT: zx_status_t = -21;
221 
222 // ERR_SHOULD_WAIT: The operation cannot be performed currently but
223 // potentially could succeed if the caller waits for a prerequisite
224 // to be satisfied, for example waiting for a handle to be readable
225 // or writable.
226 // Example: Attempting to read from a message pipe that has no
227 // messages waiting but has an open remote will return ERR_SHOULD_WAIT.
228 // Attempting to read from a message pipe that has no messages waiting
229 // and has a closed remote end will return ERR_REMOTE_CLOSED.
230 #[allow(unused)]
231 pub const ERR_SHOULD_WAIT: zx_status_t = -22;
232 
233 // ERR_CANCELED: The in-progress operation (e.g., a wait) has been
234 // // canceled.
235 #[allow(unused)]
236 pub const ERR_CANCELED: zx_status_t = -23;
237 
238 // ERR_PEER_CLOSED: The operation failed because the remote end
239 // of the subject of the operation was closed.
240 #[allow(unused)]
241 pub const ERR_PEER_CLOSED: zx_status_t = -24;
242 
243 // ERR_NOT_FOUND: The requested entity is not found.
244 #[allow(unused)]
245 pub const ERR_NOT_FOUND: zx_status_t = -25;
246 
247 // ERR_ALREADY_EXISTS: An object with the specified identifier
248 // already exists.
249 // Example: Attempting to create a file when a file already exists
250 // with that name.
251 #[allow(unused)]
252 pub const ERR_ALREADY_EXISTS: zx_status_t = -26;
253 
254 // ERR_ALREADY_BOUND: The operation failed because the named entity
255 // is already owned or controlled by another entity. The operation
256 // could succeed later if the current owner releases the entity.
257 #[allow(unused)]
258 pub const ERR_ALREADY_BOUND: zx_status_t = -27;
259 
260 // ERR_UNAVAILABLE: The subject of the operation is currently unable
261 // to perform the operation.
262 // Note: This is used when there's no direct way for the caller to
263 // observe when the subject will be able to perform the operation
264 // and should thus retry.
265 #[allow(unused)]
266 pub const ERR_UNAVAILABLE: zx_status_t = -28;
267 
268 // ======= Permission check errors =======
269 // ERR_ACCESS_DENIED: The caller did not have permission to perform
270 // the specified operation.
271 #[allow(unused)]
272 pub const ERR_ACCESS_DENIED: zx_status_t = -30;
273 
274 // ======= Input-output errors =======
275 // ERR_IO: Otherwise unspecified error occurred during I/O.
276 #[allow(unused)]
277 pub const ERR_IO: zx_status_t = -40;
278 
279 // ERR_REFUSED: The entity the I/O operation is being performed on
280 // rejected the operation.
281 // Example: an I2C device NAK'ing a transaction or a disk controller
282 // rejecting an invalid command.
283 #[allow(unused)]
284 pub const ERR_IO_REFUSED: zx_status_t = -41;
285 
286 // ERR_IO_DATA_INTEGRITY: The data in the operation failed an integrity
287 // check and is possibly corrupted.
288 // Example: CRC or Parity error.
289 #[allow(unused)]
290 pub const ERR_IO_DATA_INTEGRITY: zx_status_t = -42;
291 
292 // ERR_IO_DATA_LOSS: The data in the operation is currently unavailable
293 // and may be permanently lost.
294 // Example: A disk block is irrecoverably damaged.
295 #[allow(unused)]
296 pub const ERR_IO_DATA_LOSS: zx_status_t = -43;
297 
298 // Filesystem specific errors
299 #[allow(unused)]
300 pub const ERR_BAD_PATH: zx_status_t = -50;
301 #[allow(unused)]
302 pub const ERR_NOT_DIR: zx_status_t = -51;
303 #[allow(unused)]
304 pub const ERR_NOT_FILE: zx_status_t = -52;
305 // ERR_FILE_BIG: A file exceeds a filesystem-specific size limit.
306 #[allow(unused)]
307 pub const ERR_FILE_BIG: zx_status_t = -53;
308 // ERR_NO_SPACE: Filesystem or device space is exhausted.
309 #[allow(unused)]
310 pub const ERR_NO_SPACE: zx_status_t = -54;
311