Skip to content

Commit 8a8beb9

Browse files
committed
Allow compilation of nix on android
1 parent e12ff77 commit 8a8beb9

File tree

6 files changed

+86
-5
lines changed

6 files changed

+86
-5
lines changed

src/errno.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,18 @@ unsafe fn errno_location() -> *mut c_int {
3535
__errno()
3636
}
3737

38-
#[cfg(any(target_os = "linux", target_os = "android"))]
38+
#[cfg(target_os = "linux")]
3939
unsafe fn errno_location() -> *mut c_int {
4040
extern { fn __errno_location() -> *mut c_int; }
4141
__errno_location()
4242
}
4343

44+
#[cfg(target_os = "android")]
45+
unsafe fn errno_location() -> *mut c_int {
46+
extern { fn __errno() -> *mut c_int; }
47+
__errno()
48+
}
49+
4450
/// Sets the platform-specific errno to no-error
4551
unsafe fn clear() -> () {
4652
*errno_location() = 0;

src/macros.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,22 @@ macro_rules! libc_bitflags {
130130
}
131131
};
132132

133+
// Munch last ident with as
134+
(@accumulate_flags
135+
$prefix:tt,
136+
[$($flags:tt)*];
137+
$flag:ident as $ty:ty
138+
) => {
139+
libc_bitflags! {
140+
@accumulate_flags
141+
$prefix,
142+
[
143+
$($flags)*
144+
const $flag = libc::$flag as $ty;
145+
];
146+
}
147+
};
148+
133149
// Munch an ident; covers terminating comma case.
134150
(@accumulate_flags
135151
$prefix:tt,
@@ -147,6 +163,23 @@ macro_rules! libc_bitflags {
147163
}
148164
};
149165

166+
// Munch an ident; covers terminating comma case with as.
167+
(@accumulate_flags
168+
$prefix:tt,
169+
[$($flags:tt)*];
170+
$flag:ident as $ty:ty, $($tail:tt)*
171+
) => {
172+
libc_bitflags! {
173+
@accumulate_flags
174+
$prefix,
175+
[
176+
$($flags)*
177+
const $flag = libc::$flag as $ty;
178+
];
179+
$($tail)*
180+
}
181+
};
182+
150183
// (non-pub) Entry rule.
151184
(
152185
$(#[$attr:meta])*

src/sys/signal.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ pub const SIGIOT : Signal = SIGABRT;
198198
pub const SIGPOLL : Signal = SIGIO;
199199
pub const SIGUNUSED : Signal = SIGSYS;
200200

201+
#[cfg(not(target_os = "android"))]
201202
libc_bitflags!{
202203
pub flags SaFlags: libc::c_int {
203204
SA_NOCLDSTOP,
@@ -210,6 +211,35 @@ libc_bitflags!{
210211
}
211212
}
212213

214+
// On 64-bit android, sa_flags is c_uint while on 32-bit android, it is
215+
// c_ulong.
216+
// FIXME: https://github.com/rust-lang/libc/pull/511
217+
#[cfg(all(target_os = "android", target_pointer_width = "32"))]
218+
libc_bitflags!{
219+
pub flags SaFlags: libc::c_ulong {
220+
SA_NOCLDSTOP as libc::c_ulong,
221+
SA_NOCLDWAIT as libc::c_ulong,
222+
SA_NODEFER as libc::c_ulong,
223+
SA_ONSTACK as libc::c_ulong,
224+
SA_RESETHAND as libc::c_ulong,
225+
SA_RESTART as libc::c_ulong,
226+
SA_SIGINFO as libc::c_ulong,
227+
}
228+
}
229+
230+
#[cfg(all(target_os = "android", target_pointer_width = "64"))]
231+
libc_bitflags!{
232+
pub flags SaFlags: libc::c_uint {
233+
SA_NOCLDSTOP as libc::c_uint,
234+
SA_NOCLDWAIT as libc::c_uint,
235+
SA_NODEFER as libc::c_uint,
236+
SA_ONSTACK as libc::c_uint,
237+
SA_RESETHAND as libc::c_uint,
238+
SA_RESTART as libc::c_uint,
239+
SA_SIGINFO as libc::c_uint,
240+
}
241+
}
242+
213243
#[repr(i32)]
214244
#[derive(Clone, Copy, PartialEq)]
215245
pub enum SigmaskHow {

src/sys/socket/consts.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,8 @@ mod os {
4040
pub const SO_LINGER: c_int = libc::SO_LINGER;
4141
pub const SO_MARK: c_int = libc::SO_MARK;
4242
pub const SO_OOBINLINE: c_int = libc::SO_OOBINLINE;
43-
#[cfg(not(target_arch="arm"))]
4443
pub const SO_PASSCRED: c_int = libc::SO_PASSCRED;
4544
pub const SO_PEEK_OFF: c_int = libc::SO_PEEK_OFF;
46-
#[cfg(not(target_arch="arm"))]
4745
pub const SO_PEERCRED: c_int = libc::SO_PEERCRED;
4846
pub const SO_PRIORITY: c_int = libc::SO_PRIORITY;
4947
pub const SO_PROTOCOL: c_int = libc::SO_PROTOCOL;
@@ -57,7 +55,6 @@ mod os {
5755
pub const SO_REUSEPORT: c_int = libc::SO_REUSEPORT;
5856
pub const SO_RXQ_OVFL: c_int = libc::SO_RXQ_OVFL;
5957
pub const SO_SNDBUF: c_int = libc::SO_SNDBUF;
60-
#[cfg(not(target_arch="arm"))]
6158
pub const SO_SNDBUFFORCE: c_int = libc::SO_SNDBUFFORCE;
6259
pub const SO_TIMESTAMP: c_int = libc::SO_TIMESTAMP;
6360
pub const SO_TYPE: c_int = libc::SO_TYPE;

src/sys/socket/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ pub fn listen(sockfd: RawFd, backlog: usize) -> Result<()> {
394394
/// Bind a name to a socket
395395
///
396396
/// [Further reading](http://man7.org/linux/man-pages/man2/bind.2.html)
397+
#[cfg(not(all(target_os="android", target_pointer_width="64")))]
397398
pub fn bind(fd: RawFd, addr: &SockAddr) -> Result<()> {
398399
let res = unsafe {
399400
let (ptr, len) = addr.as_ffi_pair();
@@ -403,6 +404,21 @@ pub fn bind(fd: RawFd, addr: &SockAddr) -> Result<()> {
403404
Errno::result(res).map(drop)
404405
}
405406

407+
/// Bind a name to a socket
408+
///
409+
/// [Further reading](http://man7.org/linux/man-pages/man2/bind.2.html)
410+
// Android has some weirdness. Its 64-bit bind takes a c_int instead of a
411+
// socklen_t
412+
#[cfg(all(target_os="android", target_pointer_width="64"))]
413+
pub fn bind(fd: RawFd, addr: &SockAddr) -> Result<()> {
414+
let res = unsafe {
415+
let (ptr, len) = addr.as_ffi_pair();
416+
ffi::bind(fd, ptr, len as c_int)
417+
};
418+
419+
Errno::result(res).map(drop)
420+
}
421+
406422
/// Accept a connection on a socket
407423
///
408424
/// [Further reading](http://man7.org/linux/man-pages/man2/accept.2.html)

src/sys/termios.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ mod ffi {
3939
pub use self::non_android::*;
4040

4141
// On Android before 5.0, Bionic directly inline these to ioctl() calls.
42-
#[inline]
4342
#[cfg(all(target_os = "android", not(target_arch = "mips")))]
4443
mod android {
4544
use libc;

0 commit comments

Comments
 (0)