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