]> git.lizzy.rs Git - rust.git/blob - src/libnative/io/c_unix.rs
auto merge of #14198 : thestinger/rust/valgrind, r=luqmana
[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(target_os = "macos")]
23 #[cfg(target_os = "freebsd")]
24 pub static FIONBIO: libc::c_ulong = 0x8004667e;
25 #[cfg(target_os = "linux")]
26 #[cfg(target_os = "android")]
27 pub static FIONBIO: libc::c_ulong = 0x5421;
28 #[cfg(target_os = "macos")]
29 #[cfg(target_os = "freebsd")]
30 pub static FIOCLEX: libc::c_ulong = 0x20006601;
31 #[cfg(target_os = "linux")]
32 #[cfg(target_os = "android")]
33 pub static FIOCLEX: libc::c_ulong = 0x5451;
34
35 #[cfg(target_os = "macos")]
36 #[cfg(target_os = "freebsd")]
37 pub static MSG_DONTWAIT: libc::c_int = 0x80;
38 #[cfg(target_os = "linux")]
39 #[cfg(target_os = "android")]
40 pub static MSG_DONTWAIT: libc::c_int = 0x40;
41
42 pub static WNOHANG: libc::c_int = 1;
43
44 extern {
45     pub fn gettimeofday(timeval: *mut libc::timeval,
46                         tzp: *libc::c_void) -> libc::c_int;
47     pub fn select(nfds: libc::c_int,
48                   readfds: *fd_set,
49                   writefds: *fd_set,
50                   errorfds: *fd_set,
51                   timeout: *libc::timeval) -> libc::c_int;
52     pub fn getsockopt(sockfd: libc::c_int,
53                       level: libc::c_int,
54                       optname: libc::c_int,
55                       optval: *mut libc::c_void,
56                       optlen: *mut libc::socklen_t) -> libc::c_int;
57     pub fn ioctl(fd: libc::c_int, req: libc::c_ulong, ...) -> libc::c_int;
58
59
60     pub fn waitpid(pid: libc::pid_t, status: *mut libc::c_int,
61                    options: libc::c_int) -> libc::pid_t;
62
63     pub fn sigaction(signum: libc::c_int,
64                      act: *sigaction,
65                      oldact: *mut sigaction) -> libc::c_int;
66
67     pub fn sigaddset(set: *mut sigset_t, signum: libc::c_int) -> libc::c_int;
68     pub fn sigdelset(set: *mut sigset_t, signum: libc::c_int) -> libc::c_int;
69     pub fn sigemptyset(set: *mut sigset_t) -> libc::c_int;
70 }
71
72 #[cfg(target_os = "macos")]
73 mod select {
74     pub static FD_SETSIZE: uint = 1024;
75
76     pub struct fd_set {
77         fds_bits: [i32, ..(FD_SETSIZE / 32)]
78     }
79
80     pub fn fd_set(set: &mut fd_set, fd: i32) {
81         set.fds_bits[(fd / 32) as uint] |= 1 << (fd % 32);
82     }
83 }
84
85 #[cfg(target_os = "android")]
86 #[cfg(target_os = "freebsd")]
87 #[cfg(target_os = "linux")]
88 mod select {
89     use std::uint;
90
91     pub static FD_SETSIZE: uint = 1024;
92
93     pub struct fd_set {
94         fds_bits: [uint, ..(FD_SETSIZE / uint::BITS)]
95     }
96
97     pub fn fd_set(set: &mut fd_set, fd: i32) {
98         let fd = fd as uint;
99         set.fds_bits[fd / uint::BITS] |= 1 << (fd % uint::BITS);
100     }
101 }
102
103 #[cfg(target_os = "linux")]
104 #[cfg(target_os = "android")]
105 mod signal {
106     use libc;
107
108     pub static SA_NOCLDSTOP: libc::c_ulong = 0x00000001;
109     pub static SA_NOCLDWAIT: libc::c_ulong = 0x00000002;
110     pub static SA_NODEFER: libc::c_ulong = 0x40000000;
111     pub static SA_ONSTACK: libc::c_ulong = 0x08000000;
112     pub static SA_RESETHAND: libc::c_ulong = 0x80000000;
113     pub static SA_RESTART: libc::c_ulong = 0x10000000;
114     pub static SA_SIGINFO: libc::c_ulong = 0x00000004;
115     pub static SIGCHLD: libc::c_int = 17;
116
117     // This definition is not as accurate as it could be, {pid, uid, status} is
118     // actually a giant union. Currently we're only interested in these fields,
119     // however.
120     pub struct siginfo {
121         si_signo: libc::c_int,
122         si_errno: libc::c_int,
123         si_code: libc::c_int,
124         pub pid: libc::pid_t,
125         pub uid: libc::uid_t,
126         pub status: libc::c_int,
127     }
128
129     pub struct sigaction {
130         pub sa_handler: extern fn(libc::c_int),
131         pub sa_mask: sigset_t,
132         pub sa_flags: libc::c_ulong,
133         sa_restorer: *mut libc::c_void,
134     }
135
136     #[cfg(target_word_size = "32")]
137     pub struct sigset_t {
138         __val: [libc::c_ulong, ..32],
139     }
140     #[cfg(target_word_size = "64")]
141     pub struct sigset_t {
142         __val: [libc::c_ulong, ..16],
143     }
144 }
145
146 #[cfg(target_os = "macos")]
147 #[cfg(target_os = "freebsd")]
148 mod signal {
149     use libc;
150
151     pub static SA_ONSTACK: libc::c_int = 0x0001;
152     pub static SA_RESTART: libc::c_int = 0x0002;
153     pub static SA_RESETHAND: libc::c_int = 0x0004;
154     pub static SA_NOCLDSTOP: libc::c_int = 0x0008;
155     pub static SA_NODEFER: libc::c_int = 0x0010;
156     pub static SA_NOCLDWAIT: libc::c_int = 0x0020;
157     pub static SA_SIGINFO: libc::c_int = 0x0040;
158     pub static SIGCHLD: libc::c_int = 20;
159
160     #[cfg(target_os = "macos")]
161     pub type sigset_t = u32;
162     #[cfg(target_os = "freebsd")]
163     pub struct sigset_t {
164         bits: [u32, ..4],
165     }
166
167     // This structure has more fields, but we're not all that interested in
168     // them.
169     pub struct siginfo {
170         pub si_signo: libc::c_int,
171         pub si_errno: libc::c_int,
172         pub si_code: libc::c_int,
173         pub pid: libc::pid_t,
174         pub uid: libc::uid_t,
175         pub status: libc::c_int,
176     }
177
178     #[cfg(target_os = "macos")]
179     pub struct sigaction {
180         pub sa_handler: extern fn(libc::c_int),
181         sa_tramp: *mut libc::c_void,
182         pub sa_mask: sigset_t,
183         pub sa_flags: libc::c_int,
184     }
185
186     #[cfg(target_os = "freebsd")]
187     pub struct sigaction {
188         pub sa_handler: extern fn(libc::c_int),
189         pub sa_flags: libc::c_int,
190         pub sa_mask: sigset_t,
191     }
192 }