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