Skip to content

Commit 312b5bf

Browse files
committed
Merge pull request rust-lang#25 from alexcrichton/more-constants
Add more bindings for signals and select/pselect
2 parents fa607f4 + 07d3a0d commit 312b5bf

File tree

10 files changed

+178
-5
lines changed

10 files changed

+178
-5
lines changed

libc-test/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ fn main() {
116116
match ty {
117117
// Just pass all these through, no need for a "struct" prefix
118118
"FILE" |
119+
"fd_set" |
119120
"DIR" => ty.to_string(),
120121

121122
// Fixup a few types on windows that don't actually exist.

src/dox.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod imp {
55
pub use std::option::Option;
66
pub use std::clone::Clone;
77
pub use std::marker::Copy;
8+
pub use std::mem;
89
}
910

1011
#[cfg(dox)]
@@ -43,6 +44,22 @@ mod imp {
4344
)
4445
}
4546

47+
#[lang = "div"]
48+
pub trait Div<RHS> {
49+
type Output;
50+
fn div(self, rhs: RHS) -> Self::Output;
51+
}
52+
53+
macro_rules! impl_div {
54+
($($i:ident)*) => ($(
55+
impl Div<$i> for $i {
56+
type Output = $i;
57+
fn div(self, rhs: $i) -> $i { self / rhs }
58+
}
59+
)*)
60+
}
61+
each_int!(impl_div);
62+
4663
#[lang = "shl"]
4764
pub trait Shl<RHS> {
4865
type Output;
@@ -106,4 +123,8 @@ mod imp {
106123
)*)
107124
}
108125
each_int!(impl_bitor);
126+
127+
pub mod mem {
128+
pub fn size_of_val<T>(_: &T) -> usize { 4 }
129+
}
109130
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
//! Crate docs
1212
13-
#![allow(bad_style, raw_pointer_derive, improper_ctypes)]
13+
#![allow(bad_style, raw_pointer_derive, overflowing_literals, improper_ctypes)]
1414
#![cfg_attr(dox, feature(no_core, lang_items))]
1515
#![cfg_attr(dox, no_core)]
1616
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",

src/macros.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,24 @@ macro_rules! s {
4747
)*)
4848
}
4949

50+
macro_rules! f {
51+
($(pub fn $i:ident($($arg:ident: $argty:ty),*) -> $ret:ty {
52+
$($body:stmt);*
53+
})*) => ($(
54+
#[inline]
55+
#[cfg(not(dox))]
56+
pub unsafe extern fn $i($($arg: $argty),*) -> $ret {
57+
$($body);*
58+
}
59+
60+
#[cfg(dox)]
61+
#[allow(dead_code)]
62+
pub unsafe extern fn $i($($arg: $argty),*) -> $ret {
63+
loop {}
64+
}
65+
)*)
66+
}
67+
5068
macro_rules! __item {
5169
($i:item) => ($i)
5270
}

src/unix/bsd/mod.rs

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,69 @@ s! {
6262
pub ifa_dstaddr: *mut ::sockaddr,
6363
pub ifa_data: *mut ::c_void
6464
}
65+
66+
pub struct fd_set {
67+
fds_bits: [i32; FD_SETSIZE / 32],
68+
}
6569
}
6670

67-
pub const FIOCLEX: c_ulong = 0x20006601;
68-
pub const FIONBIO: ::c_int = 0x8004667e;
71+
pub const FIOCLEX: ::c_ulong = 0x20006601;
72+
pub const FIONBIO: ::c_ulong = 0x8004667e;
6973

7074
pub const SA_ONSTACK: ::c_int = 0x0001;
7175
pub const SA_SIGINFO: ::c_int = 0x0040;
76+
pub const SA_RESTART: ::c_int = 0x0002;
77+
pub const SA_RESETHAND: ::c_int = 0x0004;
78+
pub const SA_NOCLDSTOP: ::c_int = 0x0008;
79+
pub const SA_NODEFER: ::c_int = 0x0010;
80+
pub const SA_NOCLDWAIT: ::c_int = 0x0020;
7281

82+
pub const SIGCHLD: ::c_int = 20;
7383
pub const SIGBUS: ::c_int = 10;
7484
pub const SIG_SETMASK: ::c_int = 3;
7585

7686
pub const IPV6_MULTICAST_LOOP: ::c_int = 11;
7787
pub const IPV6_V6ONLY: ::c_int = 27;
7888

89+
pub const FD_SETSIZE: usize = 1024;
90+
91+
f! {
92+
pub fn FD_CLR(fd: ::c_int, set: *mut fd_set) -> () {
93+
let fd = fd as usize;
94+
(*set).fds_bits[fd / 32] &= !(1 << (fd % 32));
95+
return
96+
}
97+
98+
pub fn FD_ISSET(fd: ::c_int, set: *mut fd_set) -> bool {
99+
let fd = fd as usize;
100+
return ((*set).fds_bits[fd / 32] & (1 << (fd % 32))) != 0
101+
}
102+
103+
pub fn FD_SET(fd: ::c_int, set: *mut fd_set) -> () {
104+
let fd = fd as usize;
105+
(*set).fds_bits[fd / 32] |= 1 << (fd % 32);
106+
return
107+
}
108+
109+
pub fn FD_ZERO(set: *mut fd_set) -> () {
110+
for slot in (*set).fds_bits.iter_mut() {
111+
*slot = 0;
112+
}
113+
}
114+
115+
pub fn WIFEXITED(status: ::c_int) -> bool {
116+
(status & 0x7f) == 0
117+
}
118+
119+
pub fn WEXITSTATUS(status: ::c_int) -> ::c_int {
120+
status >> 8
121+
}
122+
123+
pub fn WTERMSIG(status: ::c_int) -> ::c_int {
124+
status & 0o177
125+
}
126+
}
127+
79128
extern {
80129
pub fn mincore(addr: *const ::c_void, len: ::size_t,
81130
vec: *mut c_char) -> ::c_int;

src/unix/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,25 @@ extern {
507507
align: ::size_t,
508508
size: ::size_t) -> ::c_int;
509509
pub fn sigemptyset(set: *mut sigset_t) -> ::c_int;
510+
#[cfg_attr(all(target_os = "macos", target_arch = "x86_64"),
511+
link_name = "select$1050")]
512+
#[cfg_attr(all(target_os = "macos", target_arch = "x86"),
513+
link_name = "select$UNIX2003")]
514+
pub fn select(nfds: ::c_int,
515+
readfs: *mut fd_set,
516+
writefds: *mut fd_set,
517+
errorfds: *mut fd_set,
518+
timeout: *mut timeval) -> ::c_int;
519+
#[cfg_attr(all(target_os = "macos", target_arch = "x86_64"),
520+
link_name = "pselect$1050")]
521+
#[cfg_attr(all(target_os = "macos", target_arch = "x86"),
522+
link_name = "pselect$UNIX2003")]
523+
pub fn pselect(nfds: ::c_int,
524+
readfs: *mut fd_set,
525+
writefds: *mut fd_set,
526+
errorfds: *mut fd_set,
527+
timeout: *const timespec,
528+
sigmask: *const sigset_t) -> ::c_int;
510529
}
511530

512531
cfg_if! {

src/unix/notbsd/android/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,9 @@ pub const FIOCLEX: ::c_ulong = 0x5451;
187187

188188
pub const SA_ONSTACK: ::c_ulong = 0x08000000;
189189
pub const SA_SIGINFO: ::c_ulong = 0x00000004;
190+
pub const SA_NOCLDWAIT: ::c_int = 0x00000002;
190191

192+
pub const SIGCHLD: ::c_int = 17;
191193
pub const SIGBUS: ::c_int = 7;
192194
pub const SIG_SETMASK: ::c_int = 2;
193195

src/unix/notbsd/linux/mips.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,13 @@ pub const __SIZEOF_PTHREAD_RWLOCK_T: usize = 32;
202202
pub const __SIZEOF_PTHREAD_MUTEXATTR_T: usize = 4;
203203

204204
pub const FIOCLEX: ::c_ulong = 0x6601;
205-
pub const FIONBIO: ::c_int = 0x667e;
205+
pub const FIONBIO: ::c_ulong = 0x667e;
206206

207207
pub const SA_ONSTACK: ::c_ulong = 0x08000000;
208208
pub const SA_SIGINFO: ::c_ulong = 0x00000008;
209+
pub const SA_NOCLDWAIT: ::c_int = 0x00010000;
209210

211+
pub const SIGCHLD: ::c_int = 18;
210212
pub const SIGBUS: ::c_int = 10;
211213

212214
pub const SIG_SETMASK: ::c_int = 3;

src/unix/notbsd/linux/notmips/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,13 @@ pub const TCP_TIMESTAMP: ::c_int = 24;
167167
pub const SO_REUSEPORT: ::c_int = 15;
168168

169169
pub const FIOCLEX: ::c_ulong = 0x5451;
170-
pub const FIONBIO: ::c_int = 0x5421;
170+
pub const FIONBIO: ::c_ulong = 0x5421;
171171

172172
pub const SA_ONSTACK: ::c_ulong = 0x08000000;
173173
pub const SA_SIGINFO: ::c_ulong = 0x00000004;
174+
pub const SA_NOCLDWAIT: ::c_int = 0x00000002;
174175

176+
pub const SIGCHLD: ::c_int = 17;
175177
pub const SIGBUS: ::c_int = 7;
176178
pub const SIG_SETMASK: ::c_int = 2;
177179

src/unix/notbsd/mod.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use dox::mem;
2+
13
pub type rlim_t = c_ulong;
24
pub type sa_family_t = u16;
35
pub type pthread_key_t = ::c_uint;
@@ -66,8 +68,18 @@ s! {
6668
pub sll_halen: ::c_uchar,
6769
pub sll_addr: [::c_uchar; 8]
6870
}
71+
72+
pub struct fd_set {
73+
fds_bits: [::c_ulong; FD_SETSIZE / ULONG_SIZE],
74+
}
6975
}
7076

77+
// intentionally not public, only used for fd_set
78+
#[cfg(target_pointer_width = "32")]
79+
const ULONG_SIZE: usize = 32;
80+
#[cfg(target_pointer_width = "64")]
81+
const ULONG_SIZE: usize = 64;
82+
7183
pub const EXIT_FAILURE: ::c_int = 1;
7284
pub const EXIT_SUCCESS: ::c_int = 0;
7385
pub const RAND_MAX: ::c_int = 2147483647;
@@ -287,6 +299,53 @@ pub const LOCK_UN: ::c_int = 8;
287299

288300
pub const SIGSTKSZ: ::size_t = 8192;
289301

302+
pub const SA_NODEFER: ::c_int = 0x40000000;
303+
pub const SA_RESETHAND: ::c_int = 0x80000000;
304+
pub const SA_RESTART: ::c_int = 0x10000000;
305+
pub const SA_NOCLDSTOP: ::c_int = 0x00000001;
306+
307+
pub const FD_SETSIZE: usize = 1024;
308+
309+
f! {
310+
pub fn FD_CLR(fd: ::c_int, set: *mut fd_set) -> () {
311+
let fd = fd as usize;
312+
let size = mem::size_of_val(&(*set).fds_bits[0]);
313+
(*set).fds_bits[fd / size] &= !(1 << (fd % size));
314+
return
315+
}
316+
317+
pub fn FD_ISSET(fd: ::c_int, set: *mut fd_set) -> bool {
318+
let fd = fd as usize;
319+
let size = mem::size_of_val(&(*set).fds_bits[0]);
320+
return ((*set).fds_bits[fd / size] & (1 << (fd % size))) != 0
321+
}
322+
323+
pub fn FD_SET(fd: ::c_int, set: *mut fd_set) -> () {
324+
let fd = fd as usize;
325+
let size = mem::size_of_val(&(*set).fds_bits[0]);
326+
(*set).fds_bits[fd / size] |= 1 << (fd % size);
327+
return
328+
}
329+
330+
pub fn FD_ZERO(set: *mut fd_set) -> () {
331+
for slot in (*set).fds_bits.iter_mut() {
332+
*slot = 0;
333+
}
334+
}
335+
336+
pub fn WIFEXITED(status: ::c_int) -> bool {
337+
(status & 0xff) == 0
338+
}
339+
340+
pub fn WEXITSTATUS(status: ::c_int) -> ::c_int {
341+
(status >> 8) & 0xff
342+
}
343+
344+
pub fn WTERMSIG(status: ::c_int) -> ::c_int {
345+
status & 0x7f
346+
}
347+
}
348+
290349
extern {
291350
pub fn fdatasync(fd: ::c_int) -> ::c_int;
292351
pub fn mincore(addr: *mut ::c_void, len: ::size_t,

0 commit comments

Comments
 (0)