]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/unix/c.rs
Register new snapshots.
[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 libnative that don't belong in liblibc
12
13 #![allow(dead_code)]
14 #![allow(non_camel_case_types)]
15
16 pub use self::select::fd_set;
17 pub use self::signal::{sigaction, siginfo, sigset_t};
18 pub use self::signal::{SA_ONSTACK, SA_RESTART, SA_RESETHAND, SA_NOCLDSTOP};
19 pub use self::signal::{SA_NODEFER, SA_NOCLDWAIT, SA_SIGINFO, SIGCHLD};
20
21 use libc;
22
23 #[cfg(any(target_os = "macos",
24           target_os = "ios",
25           target_os = "freebsd",
26           target_os = "dragonfly"))]
27 pub const FIONBIO: libc::c_ulong = 0x8004667e;
28 #[cfg(any(all(target_os = "linux",
29               any(target_arch = "x86",
30                   target_arch = "x86_64",
31                   target_arch = "arm",
32                   target_arch = "aarch64")),
33           target_os = "android"))]
34 pub const FIONBIO: libc::c_ulong = 0x5421;
35 #[cfg(all(target_os = "linux",
36           any(target_arch = "mips",
37               target_arch = "mipsel",
38               target_arch = "powerpc")))]
39 pub const FIONBIO: libc::c_ulong = 0x667e;
40
41 #[cfg(any(target_os = "macos",
42           target_os = "ios",
43           target_os = "freebsd",
44           target_os = "dragonfly"))]
45 pub const FIOCLEX: libc::c_ulong = 0x20006601;
46 #[cfg(any(all(target_os = "linux",
47               any(target_arch = "x86",
48                   target_arch = "x86_64",
49                   target_arch = "arm",
50                   target_arch = "aarch64")),
51           target_os = "android"))]
52 pub const FIOCLEX: libc::c_ulong = 0x5451;
53 #[cfg(all(target_os = "linux",
54           any(target_arch = "mips",
55               target_arch = "mipsel",
56               target_arch = "powerpc")))]
57 pub const FIOCLEX: libc::c_ulong = 0x6601;
58
59 #[cfg(any(target_os = "macos",
60           target_os = "ios",
61           target_os = "freebsd",
62           target_os = "dragonfly"))]
63 pub const MSG_DONTWAIT: libc::c_int = 0x80;
64 #[cfg(any(target_os = "linux", target_os = "android"))]
65 pub const MSG_DONTWAIT: libc::c_int = 0x40;
66
67 pub const WNOHANG: libc::c_int = 1;
68
69 extern {
70     pub fn gettimeofday(timeval: *mut libc::timeval,
71                         tzp: *mut libc::c_void) -> libc::c_int;
72     pub fn select(nfds: libc::c_int,
73                   readfds: *mut fd_set,
74                   writefds: *mut fd_set,
75                   errorfds: *mut fd_set,
76                   timeout: *mut libc::timeval) -> libc::c_int;
77     pub fn getsockopt(sockfd: libc::c_int,
78                       level: libc::c_int,
79                       optname: libc::c_int,
80                       optval: *mut libc::c_void,
81                       optlen: *mut libc::socklen_t) -> libc::c_int;
82     pub fn ioctl(fd: libc::c_int, req: libc::c_ulong, ...) -> libc::c_int;
83
84
85     pub fn waitpid(pid: libc::pid_t, status: *mut libc::c_int,
86                    options: libc::c_int) -> libc::pid_t;
87
88     pub fn sigaction(signum: libc::c_int,
89                      act: *const sigaction,
90                      oldact: *mut sigaction) -> libc::c_int;
91
92     pub fn sigaddset(set: *mut sigset_t, signum: libc::c_int) -> libc::c_int;
93     pub fn sigdelset(set: *mut sigset_t, signum: libc::c_int) -> libc::c_int;
94     pub fn sigemptyset(set: *mut sigset_t) -> libc::c_int;
95 }
96
97 #[cfg(any(target_os = "macos", target_os = "ios"))]
98 mod select {
99     pub const FD_SETSIZE: uint = 1024;
100
101     #[repr(C)]
102     pub struct fd_set {
103         fds_bits: [i32; (FD_SETSIZE / 32)]
104     }
105
106     pub fn fd_set(set: &mut fd_set, fd: i32) {
107         set.fds_bits[(fd / 32) as uint] |= 1 << ((fd % 32) as uint);
108     }
109 }
110
111 #[cfg(any(target_os = "android",
112           target_os = "freebsd",
113           target_os = "dragonfly",
114           target_os = "linux"))]
115 mod select {
116     use uint;
117     use libc;
118
119     pub const FD_SETSIZE: uint = 1024;
120
121     #[repr(C)]
122     pub struct fd_set {
123         // FIXME: shouldn't this be a c_ulong?
124         fds_bits: [libc::uintptr_t; (FD_SETSIZE / uint::BITS)]
125     }
126
127     pub fn fd_set(set: &mut fd_set, fd: i32) {
128         let fd = fd as uint;
129         set.fds_bits[fd / uint::BITS] |= 1 << (fd % uint::BITS);
130     }
131 }
132
133 #[cfg(any(all(target_os = "linux",
134               any(target_arch = "x86",
135                   target_arch = "x86_64",
136                   target_arch = "arm",
137                   target_arch = "aarch64")),
138           target_os = "android"))]
139 mod signal {
140     use libc;
141
142     pub const SA_NOCLDSTOP: libc::c_ulong = 0x00000001;
143     pub const SA_NOCLDWAIT: libc::c_ulong = 0x00000002;
144     pub const SA_NODEFER: libc::c_ulong = 0x40000000;
145     pub const SA_ONSTACK: libc::c_ulong = 0x08000000;
146     pub const SA_RESETHAND: libc::c_ulong = 0x80000000;
147     pub const SA_RESTART: libc::c_ulong = 0x10000000;
148     pub const SA_SIGINFO: libc::c_ulong = 0x00000004;
149     pub const SIGCHLD: libc::c_int = 17;
150
151     // This definition is not as accurate as it could be, {pid, uid, status} is
152     // actually a giant union. Currently we're only interested in these fields,
153     // however.
154     #[repr(C)]
155     pub struct siginfo {
156         si_signo: libc::c_int,
157         si_errno: libc::c_int,
158         si_code: libc::c_int,
159         pub pid: libc::pid_t,
160         pub uid: libc::uid_t,
161         pub status: libc::c_int,
162     }
163
164     #[repr(C)]
165     pub struct sigaction {
166         pub sa_handler: extern fn(libc::c_int),
167         pub sa_mask: sigset_t,
168         pub sa_flags: libc::c_ulong,
169         sa_restorer: *mut libc::c_void,
170     }
171
172     unsafe impl ::marker::Send for sigaction { }
173     unsafe impl ::marker::Sync for sigaction { }
174
175     #[repr(C)]
176     #[cfg(target_pointer_width = "32")]
177     pub struct sigset_t {
178         __val: [libc::c_ulong; 32],
179     }
180
181     #[repr(C)]
182     #[cfg(target_pointer_width = "64")]
183     pub struct sigset_t {
184         __val: [libc::c_ulong; 16],
185     }
186 }
187
188 #[cfg(all(target_os = "linux",
189           any(target_arch = "mips",
190               target_arch = "mipsel",
191               target_arch = "powerpc")))]
192 mod signal {
193     use libc;
194
195     pub const SA_NOCLDSTOP: libc::c_ulong = 0x00000001;
196     pub const SA_NOCLDWAIT: libc::c_ulong = 0x00010000;
197     pub const SA_NODEFER: libc::c_ulong = 0x40000000;
198     pub const SA_ONSTACK: libc::c_ulong = 0x08000000;
199     pub const SA_RESETHAND: libc::c_ulong = 0x80000000;
200     pub const SA_RESTART: libc::c_ulong = 0x10000000;
201     pub const SA_SIGINFO: libc::c_ulong = 0x00000008;
202     pub const SIGCHLD: libc::c_int = 18;
203
204     // This definition is not as accurate as it could be, {pid, uid, status} is
205     // actually a giant union. Currently we're only interested in these fields,
206     // however.
207     #[repr(C)]
208     pub struct siginfo {
209         si_signo: libc::c_int,
210         si_code: libc::c_int,
211         si_errno: libc::c_int,
212         pub pid: libc::pid_t,
213         pub uid: libc::uid_t,
214         pub status: libc::c_int,
215     }
216
217     #[repr(C)]
218     pub struct sigaction {
219         pub sa_flags: libc::c_uint,
220         pub sa_handler: extern fn(libc::c_int),
221         pub sa_mask: sigset_t,
222         sa_restorer: *mut libc::c_void,
223         sa_resv: [libc::c_int; 1],
224     }
225
226     unsafe impl ::marker::Send for sigaction { }
227     unsafe impl ::marker::Sync for sigaction { }
228
229     #[repr(C)]
230     pub struct sigset_t {
231         __val: [libc::c_ulong; 32],
232     }
233 }
234
235 #[cfg(any(target_os = "macos",
236           target_os = "ios",
237           target_os = "freebsd",
238           target_os = "dragonfly"))]
239 mod signal {
240     use libc;
241
242     pub const SA_ONSTACK: libc::c_int = 0x0001;
243     pub const SA_RESTART: libc::c_int = 0x0002;
244     pub const SA_RESETHAND: libc::c_int = 0x0004;
245     pub const SA_NOCLDSTOP: libc::c_int = 0x0008;
246     pub const SA_NODEFER: libc::c_int = 0x0010;
247     pub const SA_NOCLDWAIT: libc::c_int = 0x0020;
248     pub const SA_SIGINFO: libc::c_int = 0x0040;
249     pub const SIGCHLD: libc::c_int = 20;
250
251     #[cfg(any(target_os = "macos", target_os = "ios"))]
252     pub type sigset_t = u32;
253     #[cfg(any(target_os = "freebsd", target_os = "dragonfly"))]
254     #[repr(C)]
255     pub struct sigset_t {
256         bits: [u32; 4],
257     }
258
259     // This structure has more fields, but we're not all that interested in
260     // them.
261     #[repr(C)]
262     pub struct siginfo {
263         pub si_signo: libc::c_int,
264         pub si_errno: libc::c_int,
265         pub si_code: libc::c_int,
266         pub pid: libc::pid_t,
267         pub uid: libc::uid_t,
268         pub status: libc::c_int,
269     }
270
271     #[repr(C)]
272     pub struct sigaction {
273         pub sa_handler: extern fn(libc::c_int),
274         pub sa_flags: libc::c_int,
275         pub sa_mask: sigset_t,
276     }
277 }