]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/unix/process/zircon.rs
Various minor/cosmetic improvements to code
[rust.git] / src / libstd / sys / unix / process / zircon.rs
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![allow(non_camel_case_types, unused)]
12
13 use convert::TryInto;
14 use io;
15 use os::raw::c_char;
16 use u64;
17
18 use libc::{c_int, c_void, size_t};
19
20 pub type zx_handle_t = u32;
21 pub type zx_vaddr_t = usize;
22 pub type zx_rights_t = u32;
23 pub type zx_status_t = i32;
24
25 pub const ZX_HANDLE_INVALID: zx_handle_t = 0;
26
27 pub type zx_time_t = u64;
28 pub const ZX_TIME_INFINITE : zx_time_t = u64::MAX;
29
30 pub type zx_signals_t = u32;
31
32 pub const ZX_OBJECT_SIGNAL_3         : zx_signals_t = 1 << 3;
33
34 pub const ZX_TASK_TERMINATED        : zx_signals_t = ZX_OBJECT_SIGNAL_3;
35
36 pub const ZX_RIGHT_SAME_RIGHTS  : zx_rights_t = 1 << 31;
37
38 pub type zx_object_info_topic_t = u32;
39
40 pub const ZX_INFO_PROCESS         : zx_object_info_topic_t = 3;
41
42 pub fn zx_cvt<T>(t: T) -> io::Result<T> where T: TryInto<zx_status_t>+Copy {
43     if let Ok(status) = TryInto::try_into(t) {
44         if status < 0 {
45             Err(io::Error::from_raw_os_error(status))
46         } else {
47             Ok(t)
48         }
49     } else {
50         Err(io::Error::last_os_error())
51     }
52 }
53
54 // Safe wrapper around zx_handle_t
55 pub struct Handle {
56     raw: zx_handle_t,
57 }
58
59 impl Handle {
60     pub fn new(raw: zx_handle_t) -> Handle {
61         Handle {
62             raw,
63         }
64     }
65
66     pub fn raw(&self) -> zx_handle_t {
67         self.raw
68     }
69 }
70
71 impl Drop for Handle {
72     fn drop(&mut self) {
73         unsafe { zx_cvt(zx_handle_close(self.raw)).expect("Failed to close zx_handle_t"); }
74     }
75 }
76
77 // Common ZX_INFO header
78 #[derive(Default)]
79 #[repr(C)]
80 pub struct zx_info_header_t {
81     pub topic: u32,              // identifies the info struct
82     pub avail_topic_size: u16,   // “native” size of the struct
83     pub topic_size: u16,         // size of the returned struct (<=topic_size)
84     pub avail_count: u32,        // number of records the kernel has
85     pub count: u32,              // number of records returned (limited by buffer size)
86 }
87
88 #[derive(Default)]
89 #[repr(C)]
90 pub struct zx_record_process_t {
91     pub return_code: c_int,
92 }
93
94 // Returned for topic ZX_INFO_PROCESS
95 #[derive(Default)]
96 #[repr(C)]
97 pub struct zx_info_process_t {
98     pub hdr: zx_info_header_t,
99     pub rec: zx_record_process_t,
100 }
101
102 extern {
103     pub fn zx_job_default() -> zx_handle_t;
104
105     pub fn zx_task_kill(handle: zx_handle_t) -> zx_status_t;
106
107     pub fn zx_handle_close(handle: zx_handle_t) -> zx_status_t;
108
109     pub fn zx_handle_duplicate(handle: zx_handle_t, rights: zx_rights_t,
110                                out: *const zx_handle_t) -> zx_handle_t;
111
112     pub fn zx_object_wait_one(handle: zx_handle_t, signals: zx_signals_t, timeout: zx_time_t,
113                               pending: *mut zx_signals_t) -> zx_status_t;
114
115     pub fn zx_object_get_info(handle: zx_handle_t, topic: u32, buffer: *mut c_void,
116                               buffer_size: size_t, actual_size: *mut size_t,
117                               avail: *mut size_t) -> zx_status_t;
118 }
119
120 #[derive(Default)]
121 #[repr(C)]
122 pub struct fdio_spawn_action_t {
123     pub action: u32,
124     pub reserved0: u32,
125     pub local_fd: i32,
126     pub target_fd: i32,
127     pub reserved1: u64,
128 }
129
130 extern {
131     pub fn fdio_spawn_etc(job: zx_handle_t, flags: u32, path: *const c_char,
132                           argv: *const *const c_char, envp: *const *const c_char,
133                           action_count: u64, actions: *const fdio_spawn_action_t,
134                           process: *mut zx_handle_t, err_msg: *mut c_char) -> zx_status_t;
135 }
136
137 // fdio_spawn_etc flags
138
139 pub const FDIO_SPAWN_CLONE_JOB: u32 = 0x0001;
140 pub const FDIO_SPAWN_CLONE_LDSVC: u32 = 0x0002;
141 pub const FDIO_SPAWN_CLONE_NAMESPACE: u32 = 0x0004;
142 pub const FDIO_SPAWN_CLONE_STDIO: u32 = 0x0008;
143 pub const FDIO_SPAWN_CLONE_ENVIRON: u32 = 0x0010;
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)] 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)] pub const ERR_NOT_SUPPORTED: zx_status_t = -2;
158
159 // ERR_NO_RESOURCES: The system was not able to allocate some resource
160 // needed for the operation.
161 #[allow(unused)] pub const ERR_NO_RESOURCES: zx_status_t = -3;
162
163 // ERR_NO_MEMORY: The system was not able to allocate memory needed
164 // for the operation.
165 #[allow(unused)] pub const ERR_NO_MEMORY: zx_status_t = -4;
166
167 // ERR_CALL_FAILED: The second phase of zx_channel_call(; did not complete
168 // successfully.
169 #[allow(unused)] pub const ERR_CALL_FAILED: zx_status_t = -5;
170
171 // ERR_INTERRUPTED_RETRY: The system call was interrupted, but should be
172 // retried.  This should not be seen outside of the VDSO.
173 #[allow(unused)] pub const ERR_INTERRUPTED_RETRY: zx_status_t = -6;
174
175 // ======= Parameter errors =======
176 // ERR_INVALID_ARGS: an argument is invalid, ex. null pointer
177 #[allow(unused)] pub const ERR_INVALID_ARGS: zx_status_t = -10;
178
179 // ERR_BAD_HANDLE: A specified handle value does not refer to a handle.
180 #[allow(unused)] pub const ERR_BAD_HANDLE: zx_status_t = -11;
181
182 // ERR_WRONG_TYPE: The subject of the operation is the wrong type to
183 // perform the operation.
184 // Example: Attempting a message_read on a thread handle.
185 #[allow(unused)] pub const ERR_WRONG_TYPE: zx_status_t = -12;
186
187 // ERR_BAD_SYSCALL: The specified syscall number is invalid.
188 #[allow(unused)] pub const ERR_BAD_SYSCALL: zx_status_t = -13;
189
190 // ERR_OUT_OF_RANGE: An argument is outside the valid range for this
191 // operation.
192 #[allow(unused)] pub const ERR_OUT_OF_RANGE: zx_status_t = -14;
193
194 // ERR_BUFFER_TOO_SMALL: A caller provided buffer is too small for
195 // this operation.
196 #[allow(unused)] pub const ERR_BUFFER_TOO_SMALL: zx_status_t = -15;
197
198 // ======= Precondition or state errors =======
199 // ERR_BAD_STATE: operation failed because the current state of the
200 // object does not allow it, or a precondition of the operation is
201 // not satisfied
202 #[allow(unused)] pub const ERR_BAD_STATE: zx_status_t = -20;
203
204 // ERR_TIMED_OUT: The time limit for the operation elapsed before
205 // the operation completed.
206 #[allow(unused)] pub const ERR_TIMED_OUT: zx_status_t = -21;
207
208 // ERR_SHOULD_WAIT: The operation cannot be performed currently but
209 // potentially could succeed if the caller waits for a prerequisite
210 // to be satisfied, for example waiting for a handle to be readable
211 // or writable.
212 // Example: Attempting to read from a message pipe that has no
213 // messages waiting but has an open remote will return ERR_SHOULD_WAIT.
214 // Attempting to read from a message pipe that has no messages waiting
215 // and has a closed remote end will return ERR_REMOTE_CLOSED.
216 #[allow(unused)] pub const ERR_SHOULD_WAIT: zx_status_t = -22;
217
218 // ERR_CANCELED: The in-progress operation (e.g., a wait) has been
219 // // canceled.
220 #[allow(unused)] pub const ERR_CANCELED: zx_status_t = -23;
221
222 // ERR_PEER_CLOSED: The operation failed because the remote end
223 // of the subject of the operation was closed.
224 #[allow(unused)] pub const ERR_PEER_CLOSED: zx_status_t = -24;
225
226 // ERR_NOT_FOUND: The requested entity is not found.
227 #[allow(unused)] pub const ERR_NOT_FOUND: zx_status_t = -25;
228
229 // ERR_ALREADY_EXISTS: An object with the specified identifier
230 // already exists.
231 // Example: Attempting to create a file when a file already exists
232 // with that name.
233 #[allow(unused)] pub const ERR_ALREADY_EXISTS: zx_status_t = -26;
234
235 // ERR_ALREADY_BOUND: The operation failed because the named entity
236 // is already owned or controlled by another entity. The operation
237 // could succeed later if the current owner releases the entity.
238 #[allow(unused)] pub const ERR_ALREADY_BOUND: zx_status_t = -27;
239
240 // ERR_UNAVAILABLE: The subject of the operation is currently unable
241 // to perform the operation.
242 // Note: This is used when there's no direct way for the caller to
243 // observe when the subject will be able to perform the operation
244 // and should thus retry.
245 #[allow(unused)] pub const ERR_UNAVAILABLE: zx_status_t = -28;
246
247 // ======= Permission check errors =======
248 // ERR_ACCESS_DENIED: The caller did not have permission to perform
249 // the specified operation.
250 #[allow(unused)] pub const ERR_ACCESS_DENIED: zx_status_t = -30;
251
252 // ======= Input-output errors =======
253 // ERR_IO: Otherwise unspecified error occurred during I/O.
254 #[allow(unused)] pub const ERR_IO: zx_status_t = -40;
255
256 // ERR_REFUSED: The entity the I/O operation is being performed on
257 // rejected the operation.
258 // Example: an I2C device NAK'ing a transaction or a disk controller
259 // rejecting an invalid command.
260 #[allow(unused)] pub const ERR_IO_REFUSED: zx_status_t = -41;
261
262 // ERR_IO_DATA_INTEGRITY: The data in the operation failed an integrity
263 // check and is possibly corrupted.
264 // Example: CRC or Parity error.
265 #[allow(unused)] pub const ERR_IO_DATA_INTEGRITY: zx_status_t = -42;
266
267 // ERR_IO_DATA_LOSS: The data in the operation is currently unavailable
268 // and may be permanently lost.
269 // Example: A disk block is irrecoverably damaged.
270 #[allow(unused)] pub const ERR_IO_DATA_LOSS: zx_status_t = -43;
271
272 // Filesystem specific errors
273 #[allow(unused)] pub const ERR_BAD_PATH: zx_status_t = -50;
274 #[allow(unused)] pub const ERR_NOT_DIR: zx_status_t = -51;
275 #[allow(unused)] pub const ERR_NOT_FILE: zx_status_t = -52;
276 // ERR_FILE_BIG: A file exceeds a filesystem-specific size limit.
277 #[allow(unused)] pub const ERR_FILE_BIG: zx_status_t = -53;
278 // ERR_NO_SPACE: Filesystem or device space is exhausted.
279 #[allow(unused)] pub const ERR_NO_SPACE: zx_status_t = -54;