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