]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | // Copyright 2014-2015 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 | ||
62682a34 SL |
11 | //! C definitions used by std::sys that don't belong in liblibc |
12 | ||
13 | // These are definitions sufficient for the users in this directory. | |
14 | // This is not a general-purpose binding to this functionality, and in | |
15 | // some cases (notably the definition of siginfo_t), we intentionally | |
16 | // have incomplete bindings so that we don't need to fight with unions. | |
17 | // | |
18 | // Note that these types need to match the definitions from the platform | |
19 | // libc (currently glibc on Linux), not the kernel definitions / the | |
20 | // syscall interface. This has a few weirdnesses, like glibc's sigset_t | |
21 | // being 1024 bits on all platforms. If you're adding a new GNU/Linux | |
22 | // port, check glibc's sysdeps/unix/sysv/linux, not the kernel headers. | |
1a4d82fc JJ |
23 | |
24 | #![allow(dead_code)] | |
25 | #![allow(non_camel_case_types)] | |
26 | ||
62682a34 SL |
27 | pub use self::signal_os::{sigaction, siginfo, sigset_t, sigaltstack}; |
28 | pub use self::signal_os::{SA_ONSTACK, SA_SIGINFO, SIGBUS, SIGSTKSZ, SIG_SETMASK}; | |
1a4d82fc JJ |
29 | |
30 | use libc; | |
31 | ||
32 | #[cfg(any(target_os = "macos", | |
33 | target_os = "ios", | |
34 | target_os = "freebsd", | |
85aaf69f | 35 | target_os = "dragonfly", |
c34b1796 | 36 | target_os = "bitrig", |
85aaf69f | 37 | target_os = "openbsd"))] |
62682a34 SL |
38 | pub const FIOCLEX: libc::c_ulong = 0x20006601; |
39 | ||
1a4d82fc JJ |
40 | #[cfg(any(all(target_os = "linux", |
41 | any(target_arch = "x86", | |
42 | target_arch = "x86_64", | |
43 | target_arch = "arm", | |
44 | target_arch = "aarch64")), | |
45 | target_os = "android"))] | |
62682a34 SL |
46 | pub const FIOCLEX: libc::c_ulong = 0x5451; |
47 | ||
1a4d82fc | 48 | #[cfg(all(target_os = "linux", |
85aaf69f SL |
49 | any(target_arch = "mips", |
50 | target_arch = "mipsel", | |
51 | target_arch = "powerpc")))] | |
62682a34 | 52 | pub const FIOCLEX: libc::c_ulong = 0x6601; |
1a4d82fc JJ |
53 | |
54 | pub const WNOHANG: libc::c_int = 1; | |
55 | ||
85aaf69f SL |
56 | #[cfg(target_os = "linux")] |
57 | pub const _SC_GETPW_R_SIZE_MAX: libc::c_int = 70; | |
58 | #[cfg(any(target_os = "macos", | |
59 | target_os = "freebsd", | |
60 | target_os = "dragonfly"))] | |
61 | pub const _SC_GETPW_R_SIZE_MAX: libc::c_int = 71; | |
c34b1796 AL |
62 | #[cfg(any(target_os = "bitrig", |
63 | target_os = "openbsd"))] | |
85aaf69f SL |
64 | pub const _SC_GETPW_R_SIZE_MAX: libc::c_int = 101; |
65 | #[cfg(target_os = "android")] | |
66 | pub const _SC_GETPW_R_SIZE_MAX: libc::c_int = 0x0048; | |
67 | ||
68 | #[repr(C)] | |
69 | #[cfg(target_os = "linux")] | |
70 | pub struct passwd { | |
71 | pub pw_name: *mut libc::c_char, | |
72 | pub pw_passwd: *mut libc::c_char, | |
73 | pub pw_uid: libc::uid_t, | |
74 | pub pw_gid: libc::gid_t, | |
75 | pub pw_gecos: *mut libc::c_char, | |
76 | pub pw_dir: *mut libc::c_char, | |
77 | pub pw_shell: *mut libc::c_char, | |
78 | } | |
79 | ||
80 | #[repr(C)] | |
81 | #[cfg(any(target_os = "macos", | |
82 | target_os = "freebsd", | |
83 | target_os = "dragonfly", | |
c34b1796 | 84 | target_os = "bitrig", |
85aaf69f SL |
85 | target_os = "openbsd"))] |
86 | pub struct passwd { | |
87 | pub pw_name: *mut libc::c_char, | |
88 | pub pw_passwd: *mut libc::c_char, | |
89 | pub pw_uid: libc::uid_t, | |
90 | pub pw_gid: libc::gid_t, | |
91 | pub pw_change: libc::time_t, | |
92 | pub pw_class: *mut libc::c_char, | |
93 | pub pw_gecos: *mut libc::c_char, | |
94 | pub pw_dir: *mut libc::c_char, | |
95 | pub pw_shell: *mut libc::c_char, | |
96 | pub pw_expire: libc::time_t, | |
97 | } | |
98 | ||
99 | #[repr(C)] | |
100 | #[cfg(target_os = "android")] | |
101 | pub struct passwd { | |
102 | pub pw_name: *mut libc::c_char, | |
103 | pub pw_passwd: *mut libc::c_char, | |
104 | pub pw_uid: libc::uid_t, | |
105 | pub pw_gid: libc::gid_t, | |
106 | pub pw_dir: *mut libc::c_char, | |
107 | pub pw_shell: *mut libc::c_char, | |
108 | } | |
109 | ||
62682a34 SL |
110 | // This is really a function pointer (or a union of multiple function |
111 | // pointers), except for constants like SIG_DFL. | |
112 | pub type sighandler_t = *mut libc::c_void; | |
113 | ||
114 | pub const SIG_DFL: sighandler_t = 0 as sighandler_t; | |
115 | pub const SIG_ERR: sighandler_t = !0 as sighandler_t; | |
116 | ||
1a4d82fc | 117 | extern { |
1a4d82fc JJ |
118 | pub fn getsockopt(sockfd: libc::c_int, |
119 | level: libc::c_int, | |
120 | optname: libc::c_int, | |
121 | optval: *mut libc::c_void, | |
122 | optlen: *mut libc::socklen_t) -> libc::c_int; | |
123 | pub fn ioctl(fd: libc::c_int, req: libc::c_ulong, ...) -> libc::c_int; | |
124 | ||
125 | ||
126 | pub fn waitpid(pid: libc::pid_t, status: *mut libc::c_int, | |
127 | options: libc::c_int) -> libc::pid_t; | |
128 | ||
62682a34 SL |
129 | pub fn raise(signum: libc::c_int) -> libc::c_int; |
130 | ||
1a4d82fc JJ |
131 | pub fn sigaction(signum: libc::c_int, |
132 | act: *const sigaction, | |
133 | oldact: *mut sigaction) -> libc::c_int; | |
134 | ||
62682a34 SL |
135 | pub fn sigaltstack(ss: *const sigaltstack, |
136 | oss: *mut sigaltstack) -> libc::c_int; | |
137 | ||
138 | #[cfg(not(target_os = "android"))] | |
1a4d82fc | 139 | pub fn sigemptyset(set: *mut sigset_t) -> libc::c_int; |
85aaf69f | 140 | |
62682a34 SL |
141 | pub fn pthread_sigmask(how: libc::c_int, set: *const sigset_t, |
142 | oldset: *mut sigset_t) -> libc::c_int; | |
143 | ||
85aaf69f SL |
144 | #[cfg(not(target_os = "ios"))] |
145 | pub fn getpwuid_r(uid: libc::uid_t, | |
146 | pwd: *mut passwd, | |
147 | buf: *mut libc::c_char, | |
148 | buflen: libc::size_t, | |
149 | result: *mut *mut passwd) -> libc::c_int; | |
150 | ||
151 | pub fn utimes(filename: *const libc::c_char, | |
152 | times: *const libc::timeval) -> libc::c_int; | |
153 | pub fn gai_strerror(errcode: libc::c_int) -> *const libc::c_char; | |
9346a6ac AL |
154 | pub fn setgroups(ngroups: libc::c_int, |
155 | ptr: *const libc::c_void) -> libc::c_int; | |
d9579d0f AL |
156 | pub fn realpath(pathname: *const libc::c_char, resolved: *mut libc::c_char) |
157 | -> *mut libc::c_char; | |
1a4d82fc JJ |
158 | } |
159 | ||
62682a34 SL |
160 | // Ugh. This is only available as an inline until Android API 21. |
161 | #[cfg(target_os = "android")] | |
162 | pub unsafe fn sigemptyset(set: *mut sigset_t) -> libc::c_int { | |
163 | use intrinsics; | |
164 | intrinsics::write_bytes(set, 0, 1); | |
165 | return 0; | |
1a4d82fc JJ |
166 | } |
167 | ||
62682a34 | 168 | #[cfg(any(target_os = "linux", |
1a4d82fc | 169 | target_os = "android"))] |
62682a34 SL |
170 | mod signal_os { |
171 | pub use self::arch::{SA_ONSTACK, SA_SIGINFO, SIGBUS, SIG_SETMASK, | |
172 | sigaction, sigaltstack}; | |
1a4d82fc JJ |
173 | use libc; |
174 | ||
62682a34 SL |
175 | #[cfg(any(target_arch = "x86", |
176 | target_arch = "x86_64", | |
177 | target_arch = "arm", | |
178 | target_arch = "mips", | |
179 | target_arch = "mipsel"))] | |
180 | pub const SIGSTKSZ: libc::size_t = 8192; | |
181 | ||
182 | // This is smaller on musl and Android, but no harm in being generous. | |
183 | #[cfg(any(target_arch = "aarch64", | |
184 | target_arch = "powerpc"))] | |
185 | pub const SIGSTKSZ: libc::size_t = 16384; | |
186 | ||
187 | // This definition is intentionally a subset of the C structure: the | |
188 | // fields after si_code are actually a giant union. We're only | |
189 | // interested in si_addr for this module, though. | |
1a4d82fc JJ |
190 | #[repr(C)] |
191 | pub struct siginfo { | |
62682a34 SL |
192 | _signo: libc::c_int, |
193 | _errno: libc::c_int, | |
194 | _code: libc::c_int, | |
195 | // This structure will need extra padding here for MIPS64. | |
196 | pub si_addr: *mut libc::c_void | |
1a4d82fc JJ |
197 | } |
198 | ||
62682a34 | 199 | #[cfg(all(target_os = "linux", target_pointer_width = "32"))] |
1a4d82fc | 200 | #[repr(C)] |
1a4d82fc JJ |
201 | pub struct sigset_t { |
202 | __val: [libc::c_ulong; 32], | |
203 | } | |
204 | ||
62682a34 | 205 | #[cfg(all(target_os = "linux", target_pointer_width = "64"))] |
1a4d82fc | 206 | #[repr(C)] |
1a4d82fc JJ |
207 | pub struct sigset_t { |
208 | __val: [libc::c_ulong; 16], | |
209 | } | |
1a4d82fc | 210 | |
62682a34 SL |
211 | // Android for MIPS has a 128-bit sigset_t, but we don't currently |
212 | // support it. Android for AArch64 technically has a structure of a | |
213 | // single ulong. | |
214 | #[cfg(target_os = "android")] | |
215 | pub type sigset_t = libc::c_ulong; | |
216 | ||
217 | #[cfg(any(target_arch = "x86", | |
218 | target_arch = "x86_64", | |
219 | target_arch = "powerpc", | |
220 | target_arch = "arm", | |
221 | target_arch = "aarch64"))] | |
222 | mod arch { | |
223 | use libc; | |
224 | use super::super::sighandler_t; | |
225 | use super::sigset_t; | |
226 | ||
227 | pub const SA_ONSTACK: libc::c_ulong = 0x08000000; | |
228 | pub const SA_SIGINFO: libc::c_ulong = 0x00000004; | |
229 | ||
230 | pub const SIGBUS: libc::c_int = 7; | |
231 | ||
232 | pub const SIG_SETMASK: libc::c_int = 2; | |
233 | ||
234 | #[cfg(target_os = "linux")] | |
235 | #[repr(C)] | |
236 | pub struct sigaction { | |
237 | pub sa_sigaction: sighandler_t, | |
238 | pub sa_mask: sigset_t, | |
239 | pub sa_flags: libc::c_ulong, | |
240 | _restorer: *mut libc::c_void, | |
241 | } | |
242 | ||
243 | #[cfg(all(target_os = "android", target_pointer_width = "32"))] | |
244 | #[repr(C)] | |
245 | pub struct sigaction { | |
246 | pub sa_sigaction: sighandler_t, | |
247 | pub sa_flags: libc::c_ulong, | |
248 | _restorer: *mut libc::c_void, | |
249 | pub sa_mask: sigset_t, | |
250 | } | |
251 | ||
252 | #[cfg(all(target_os = "android", target_pointer_width = "64"))] | |
253 | #[repr(C)] | |
254 | pub struct sigaction { | |
255 | pub sa_flags: libc::c_uint, | |
256 | pub sa_sigaction: sighandler_t, | |
257 | pub sa_mask: sigset_t, | |
258 | _restorer: *mut libc::c_void, | |
259 | } | |
260 | ||
261 | #[repr(C)] | |
262 | pub struct sigaltstack { | |
263 | pub ss_sp: *mut libc::c_void, | |
264 | pub ss_flags: libc::c_int, | |
265 | pub ss_size: libc::size_t | |
266 | } | |
1a4d82fc JJ |
267 | } |
268 | ||
62682a34 SL |
269 | #[cfg(any(target_arch = "mips", |
270 | target_arch = "mipsel"))] | |
271 | mod arch { | |
272 | use libc; | |
273 | use super::super::sighandler_t; | |
274 | use super::sigset_t; | |
275 | ||
276 | pub const SA_ONSTACK: libc::c_ulong = 0x08000000; | |
277 | pub const SA_SIGINFO: libc::c_ulong = 0x00000008; | |
278 | ||
279 | pub const SIGBUS: libc::c_int = 10; | |
280 | ||
281 | pub const SIG_SETMASK: libc::c_int = 3; | |
282 | ||
283 | #[cfg(all(target_os = "linux", not(target_env = "musl")))] | |
284 | #[repr(C)] | |
285 | pub struct sigaction { | |
286 | pub sa_flags: libc::c_uint, | |
287 | pub sa_sigaction: sighandler_t, | |
288 | pub sa_mask: sigset_t, | |
289 | _restorer: *mut libc::c_void, | |
290 | _resv: [libc::c_int; 1], | |
291 | } | |
292 | ||
293 | #[cfg(target_env = "musl")] | |
294 | #[repr(C)] | |
295 | pub struct sigaction { | |
296 | pub sa_sigaction: sighandler_t, | |
297 | pub sa_mask: sigset_t, | |
298 | pub sa_flags: libc::c_ulong, | |
299 | _restorer: *mut libc::c_void, | |
300 | } | |
301 | ||
302 | #[cfg(target_os = "android")] | |
303 | #[repr(C)] | |
304 | pub struct sigaction { | |
305 | pub sa_flags: libc::c_uint, | |
306 | pub sa_sigaction: sighandler_t, | |
307 | pub sa_mask: sigset_t, | |
308 | } | |
309 | ||
310 | #[repr(C)] | |
311 | pub struct sigaltstack { | |
312 | pub ss_sp: *mut libc::c_void, | |
313 | pub ss_size: libc::size_t, | |
314 | pub ss_flags: libc::c_int, | |
315 | } | |
1a4d82fc JJ |
316 | } |
317 | } | |
318 | ||
319 | #[cfg(any(target_os = "macos", | |
320 | target_os = "ios", | |
321 | target_os = "freebsd", | |
62682a34 SL |
322 | target_os = "dragonfly", |
323 | target_os = "bitrig", | |
324 | target_os = "openbsd"))] | |
325 | mod signal_os { | |
1a4d82fc | 326 | use libc; |
62682a34 | 327 | use super::sighandler_t; |
1a4d82fc JJ |
328 | |
329 | pub const SA_ONSTACK: libc::c_int = 0x0001; | |
1a4d82fc | 330 | pub const SA_SIGINFO: libc::c_int = 0x0040; |
62682a34 SL |
331 | |
332 | pub const SIGBUS: libc::c_int = 10; | |
333 | ||
334 | #[cfg(any(target_os = "macos", target_os = "ios"))] | |
335 | pub const SIGSTKSZ: libc::size_t = 131072; | |
336 | // FreeBSD's is actually arch-dependent, but never more than 40960. | |
337 | // No harm in being generous. | |
338 | #[cfg(not(any(target_os = "macos", target_os = "ios")))] | |
339 | pub const SIGSTKSZ: libc::size_t = 40960; | |
340 | ||
341 | pub const SIG_SETMASK: libc::c_int = 3; | |
1a4d82fc | 342 | |
85aaf69f | 343 | #[cfg(any(target_os = "macos", |
c34b1796 | 344 | target_os = "ios"))] |
1a4d82fc JJ |
345 | pub type sigset_t = u32; |
346 | #[cfg(any(target_os = "freebsd", target_os = "dragonfly"))] | |
347 | #[repr(C)] | |
348 | pub struct sigset_t { | |
349 | bits: [u32; 4], | |
350 | } | |
62682a34 SL |
351 | #[cfg(any(target_os = "bitrig", target_os = "openbsd"))] |
352 | pub type sigset_t = libc::c_uint; | |
1a4d82fc JJ |
353 | |
354 | // This structure has more fields, but we're not all that interested in | |
355 | // them. | |
62682a34 SL |
356 | #[cfg(any(target_os = "macos", target_os = "ios", |
357 | target_os = "freebsd", target_os = "dragonfly"))] | |
358 | #[repr(C)] | |
359 | pub struct siginfo { | |
360 | pub _signo: libc::c_int, | |
361 | pub _errno: libc::c_int, | |
362 | pub _code: libc::c_int, | |
363 | pub _pid: libc::pid_t, | |
364 | pub _uid: libc::uid_t, | |
365 | pub _status: libc::c_int, | |
366 | pub si_addr: *mut libc::c_void | |
367 | } | |
368 | #[cfg(any(target_os = "bitrig", target_os = "openbsd"))] | |
1a4d82fc JJ |
369 | #[repr(C)] |
370 | pub struct siginfo { | |
371 | pub si_signo: libc::c_int, | |
1a4d82fc | 372 | pub si_code: libc::c_int, |
62682a34 SL |
373 | pub si_errno: libc::c_int, |
374 | pub si_addr: *mut libc::c_void | |
1a4d82fc JJ |
375 | } |
376 | ||
62682a34 SL |
377 | #[cfg(any(target_os = "macos", target_os = "ios", |
378 | target_os = "bitrig", target_os = "openbsd"))] | |
1a4d82fc JJ |
379 | #[repr(C)] |
380 | pub struct sigaction { | |
62682a34 | 381 | pub sa_sigaction: sighandler_t, |
1a4d82fc | 382 | pub sa_mask: sigset_t, |
62682a34 | 383 | pub sa_flags: libc::c_int, |
1a4d82fc | 384 | } |
c34b1796 | 385 | |
62682a34 | 386 | #[cfg(any(target_os = "freebsd", target_os = "dragonfly"))] |
c34b1796 | 387 | #[repr(C)] |
62682a34 SL |
388 | pub struct sigaction { |
389 | pub sa_sigaction: sighandler_t, | |
390 | pub sa_flags: libc::c_int, | |
391 | pub sa_mask: sigset_t, | |
c34b1796 AL |
392 | } |
393 | ||
394 | #[repr(C)] | |
62682a34 SL |
395 | pub struct sigaltstack { |
396 | pub ss_sp: *mut libc::c_void, | |
397 | pub ss_size: libc::size_t, | |
398 | pub ss_flags: libc::c_int, | |
c34b1796 AL |
399 | } |
400 | } |