]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/unix/process/magenta.rs
Auto merge of #42414 - frewsxcv:frewsxcv/improve-cow-docs, r=QuietMisdreavus
[rust.git] / src / libstd / sys / unix / process / magenta.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)]
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};
19
20 pub type mx_handle_t = i32;
21 pub type mx_vaddr_t = usize;
22 pub type mx_rights_t = u32;
23 pub type mx_status_t = i32;
24
25 pub type mx_size_t = usize;
26
27 pub const MX_HANDLE_INVALID: mx_handle_t = 0;
28
29 pub type mx_time_t = u64;
30 pub const MX_TIME_INFINITE : mx_time_t = u64::MAX;
31
32 pub type mx_signals_t = u32;
33
34 pub const MX_OBJECT_SIGNAL_3         : mx_signals_t = 1 << 3;
35
36 pub const MX_TASK_TERMINATED        : mx_signals_t = MX_OBJECT_SIGNAL_3;
37
38 pub const MX_RIGHT_SAME_RIGHTS  : mx_rights_t = 1 << 31;
39
40 pub type mx_object_info_topic_t = u32;
41
42 pub const MX_INFO_PROCESS         : mx_object_info_topic_t = 3;
43
44 pub fn mx_cvt<T>(t: T) -> io::Result<T> where T: TryInto<mx_status_t>+Copy {
45     if let Ok(status) = TryInto::try_into(t) {
46         if status < 0 {
47             Err(io::Error::from_raw_os_error(status))
48         } else {
49             Ok(t)
50         }
51     } else {
52         Err(io::Error::last_os_error())
53     }
54 }
55
56 // Safe wrapper around mx_handle_t
57 pub struct Handle {
58     raw: mx_handle_t,
59 }
60
61 impl Handle {
62     pub fn new(raw: mx_handle_t) -> Handle {
63         Handle {
64             raw: raw,
65         }
66     }
67
68     pub fn raw(&self) -> mx_handle_t {
69         self.raw
70     }
71 }
72
73 impl Drop for Handle {
74     fn drop(&mut self) {
75         unsafe { mx_cvt(mx_handle_close(self.raw)).expect("Failed to close mx_handle_t"); }
76     }
77 }
78
79 // Common MX_INFO header
80 #[derive(Default)]
81 #[repr(C)]
82 pub struct mx_info_header_t {
83     pub topic: u32,              // identifies the info struct
84     pub avail_topic_size: u16,   // “native” size of the struct
85     pub topic_size: u16,         // size of the returned struct (<=topic_size)
86     pub avail_count: u32,        // number of records the kernel has
87     pub count: u32,              // number of records returned (limited by buffer size)
88 }
89
90 #[derive(Default)]
91 #[repr(C)]
92 pub struct mx_record_process_t {
93     pub return_code: c_int,
94 }
95
96 // Returned for topic MX_INFO_PROCESS
97 #[derive(Default)]
98 #[repr(C)]
99 pub struct mx_info_process_t {
100     pub hdr: mx_info_header_t,
101     pub rec: mx_record_process_t,
102 }
103
104 extern {
105     static __magenta_job_default: mx_handle_t;
106
107     pub fn mx_task_kill(handle: mx_handle_t) -> mx_status_t;
108
109     pub fn mx_handle_close(handle: mx_handle_t) -> mx_status_t;
110
111     pub fn mx_handle_duplicate(handle: mx_handle_t, rights: mx_rights_t,
112                                out: *const mx_handle_t) -> mx_handle_t;
113
114     pub fn mx_object_wait_one(handle: mx_handle_t, signals: mx_signals_t, timeout: mx_time_t,
115                               pending: *mut mx_signals_t) -> mx_status_t;
116
117     pub fn mx_object_get_info(handle: mx_handle_t, topic: u32, buffer: *mut c_void,
118                               buffer_size: mx_size_t, actual_size: *mut mx_size_t,
119                               avail: *mut mx_size_t) -> mx_status_t;
120 }
121
122 pub fn mx_job_default() -> mx_handle_t {
123     unsafe { return __magenta_job_default; }
124 }
125
126 // From `enum special_handles` in system/ulib/launchpad/launchpad.c
127 // HND_LOADER_SVC = 0
128 // HND_EXEC_VMO = 1
129 pub const HND_SPECIAL_COUNT: usize = 2;
130
131 #[repr(C)]
132 pub struct launchpad_t {
133     argc: u32,
134     envc: u32,
135     args: *const c_char,
136     args_len: usize,
137     env: *const c_char,
138     env_len: usize,
139
140     handles: *mut mx_handle_t,
141     handles_info: *mut u32,
142     handle_count: usize,
143     handle_alloc: usize,
144
145     entry: mx_vaddr_t,
146     base: mx_vaddr_t,
147     vdso_base: mx_vaddr_t,
148
149     stack_size: usize,
150
151     special_handles: [mx_handle_t; HND_SPECIAL_COUNT],
152     loader_message: bool,
153 }
154
155 extern {
156     pub fn launchpad_create(job: mx_handle_t, name: *const c_char,
157                             lp: *mut *mut launchpad_t) -> mx_status_t;
158
159     pub fn launchpad_go(lp: *mut launchpad_t,
160                         proc_handle: *mut mx_handle_t,
161                         err_msg: *mut *const c_char) -> mx_status_t;
162
163     pub fn launchpad_destroy(lp: *mut launchpad_t);
164
165     pub fn launchpad_set_args(lp: *mut launchpad_t, argc: c_int,
166                                argv: *const *const c_char) -> mx_status_t;
167
168     pub fn launchpad_set_environ(lp: *mut launchpad_t, envp: *const *const c_char) -> mx_status_t;
169
170     pub fn launchpad_clone(lp: *mut launchpad_t, what: u32) -> mx_status_t;
171
172     pub fn launchpad_clone_fd(lp: *mut launchpad_t, fd: c_int, target_fd: c_int) -> mx_status_t;
173
174     pub fn launchpad_transfer_fd(lp: *mut launchpad_t, fd: c_int, target_fd: c_int) -> mx_status_t;
175
176     pub fn launchpad_elf_load(lp: *mut launchpad_t, vmo: mx_handle_t) -> mx_status_t;
177
178     pub fn launchpad_add_vdso_vmo(lp: *mut launchpad_t) -> mx_status_t;
179
180     pub fn launchpad_load_vdso(lp: *mut launchpad_t, vmo: mx_handle_t) -> mx_status_t;
181
182     pub fn launchpad_vmo_from_file(filename: *const c_char) -> mx_handle_t;
183 }
184
185 // Launchpad clone constants
186
187 pub const LP_CLONE_MXIO_ROOT: u32 = 0x0001;
188 pub const LP_CLONE_MXIO_CWD: u32 = 0x0002;
189 // LP_CLONE_MXIO_STDIO = 0x0004
190 // LP_CLONE_MXIO_ALL = 0x00FF
191 // LP_CLONE_ENVIRON = 0x0100
192 // LP_CLONE_DEFAULT_JOB = 0x0200
193 // LP_CLONE_ALL = 0xFFFF
194
195 // Errors
196
197 #[allow(unused)] pub const ERR_INTERNAL: mx_status_t = -1;
198
199 // ERR_NOT_SUPPORTED: The operation is not implemented, supported,
200 // or enabled.
201 #[allow(unused)] pub const ERR_NOT_SUPPORTED: mx_status_t = -2;
202
203 // ERR_NO_RESOURCES: The system was not able to allocate some resource
204 // needed for the operation.
205 #[allow(unused)] pub const ERR_NO_RESOURCES: mx_status_t = -3;
206
207 // ERR_NO_MEMORY: The system was not able to allocate memory needed
208 // for the operation.
209 #[allow(unused)] pub const ERR_NO_MEMORY: mx_status_t = -4;
210
211 // ERR_CALL_FAILED: The second phase of mx_channel_call(; did not complete
212 // successfully.
213 #[allow(unused)] pub const ERR_CALL_FAILED: mx_status_t = -5;
214
215 // ERR_INTERRUPTED_RETRY: The system call was interrupted, but should be
216 // retried.  This should not be seen outside of the VDSO.
217 #[allow(unused)] pub const ERR_INTERRUPTED_RETRY: mx_status_t = -6;
218
219 // ======= Parameter errors =======
220 // ERR_INVALID_ARGS: an argument is invalid, ex. null pointer
221 #[allow(unused)] pub const ERR_INVALID_ARGS: mx_status_t = -10;
222
223 // ERR_BAD_HANDLE: A specified handle value does not refer to a handle.
224 #[allow(unused)] pub const ERR_BAD_HANDLE: mx_status_t = -11;
225
226 // ERR_WRONG_TYPE: The subject of the operation is the wrong type to
227 // perform the operation.
228 // Example: Attempting a message_read on a thread handle.
229 #[allow(unused)] pub const ERR_WRONG_TYPE: mx_status_t = -12;
230
231 // ERR_BAD_SYSCALL: The specified syscall number is invalid.
232 #[allow(unused)] pub const ERR_BAD_SYSCALL: mx_status_t = -13;
233
234 // ERR_OUT_OF_RANGE: An argument is outside the valid range for this
235 // operation.
236 #[allow(unused)] pub const ERR_OUT_OF_RANGE: mx_status_t = -14;
237
238 // ERR_BUFFER_TOO_SMALL: A caller provided buffer is too small for
239 // this operation.
240 #[allow(unused)] pub const ERR_BUFFER_TOO_SMALL: mx_status_t = -15;
241
242 // ======= Precondition or state errors =======
243 // ERR_BAD_STATE: operation failed because the current state of the
244 // object does not allow it, or a precondition of the operation is
245 // not satisfied
246 #[allow(unused)] pub const ERR_BAD_STATE: mx_status_t = -20;
247
248 // ERR_TIMED_OUT: The time limit for the operation elapsed before
249 // the operation completed.
250 #[allow(unused)] pub const ERR_TIMED_OUT: mx_status_t = -21;
251
252 // ERR_SHOULD_WAIT: The operation cannot be performed currently but
253 // potentially could succeed if the caller waits for a prerequisite
254 // to be satisfied, for example waiting for a handle to be readable
255 // or writable.
256 // Example: Attempting to read from a message pipe that has no
257 // messages waiting but has an open remote will return ERR_SHOULD_WAIT.
258 // Attempting to read from a message pipe that has no messages waiting
259 // and has a closed remote end will return ERR_REMOTE_CLOSED.
260 #[allow(unused)] pub const ERR_SHOULD_WAIT: mx_status_t = -22;
261
262 // ERR_CANCELED: The in-progress operation (e.g. a wait) has been
263 // // canceled.
264 #[allow(unused)] pub const ERR_CANCELED: mx_status_t = -23;
265
266 // ERR_PEER_CLOSED: The operation failed because the remote end
267 // of the subject of the operation was closed.
268 #[allow(unused)] pub const ERR_PEER_CLOSED: mx_status_t = -24;
269
270 // ERR_NOT_FOUND: The requested entity is not found.
271 #[allow(unused)] pub const ERR_NOT_FOUND: mx_status_t = -25;
272
273 // ERR_ALREADY_EXISTS: An object with the specified identifier
274 // already exists.
275 // Example: Attempting to create a file when a file already exists
276 // with that name.
277 #[allow(unused)] pub const ERR_ALREADY_EXISTS: mx_status_t = -26;
278
279 // ERR_ALREADY_BOUND: The operation failed because the named entity
280 // is already owned or controlled by another entity. The operation
281 // could succeed later if the current owner releases the entity.
282 #[allow(unused)] pub const ERR_ALREADY_BOUND: mx_status_t = -27;
283
284 // ERR_UNAVAILABLE: The subject of the operation is currently unable
285 // to perform the operation.
286 // Note: This is used when there's no direct way for the caller to
287 // observe when the subject will be able to perform the operation
288 // and should thus retry.
289 #[allow(unused)] pub const ERR_UNAVAILABLE: mx_status_t = -28;
290
291 // ======= Permission check errors =======
292 // ERR_ACCESS_DENIED: The caller did not have permission to perform
293 // the specified operation.
294 #[allow(unused)] pub const ERR_ACCESS_DENIED: mx_status_t = -30;
295
296 // ======= Input-output errors =======
297 // ERR_IO: Otherwise unspecified error occurred during I/O.
298 #[allow(unused)] pub const ERR_IO: mx_status_t = -40;
299
300 // ERR_REFUSED: The entity the I/O operation is being performed on
301 // rejected the operation.
302 // Example: an I2C device NAK'ing a transaction or a disk controller
303 // rejecting an invalid command.
304 #[allow(unused)] pub const ERR_IO_REFUSED: mx_status_t = -41;
305
306 // ERR_IO_DATA_INTEGRITY: The data in the operation failed an integrity
307 // check and is possibly corrupted.
308 // Example: CRC or Parity error.
309 #[allow(unused)] pub const ERR_IO_DATA_INTEGRITY: mx_status_t = -42;
310
311 // ERR_IO_DATA_LOSS: The data in the operation is currently unavailable
312 // and may be permanently lost.
313 // Example: A disk block is irrecoverably damaged.
314 #[allow(unused)] pub const ERR_IO_DATA_LOSS: mx_status_t = -43;
315
316 // Filesystem specific errors
317 #[allow(unused)] pub const ERR_BAD_PATH: mx_status_t = -50;
318 #[allow(unused)] pub const ERR_NOT_DIR: mx_status_t = -51;
319 #[allow(unused)] pub const ERR_NOT_FILE: mx_status_t = -52;
320 // ERR_FILE_BIG: A file exceeds a filesystem-specific size limit.
321 #[allow(unused)] pub const ERR_FILE_BIG: mx_status_t = -53;
322 // ERR_NO_SPACE: Filesystem or device space is exhausted.
323 #[allow(unused)] pub const ERR_NO_SPACE: mx_status_t = -54;