Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Libc 024 #219

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ preadv_pwritev = []
signalfd = []

[dependencies]
libc = "0.1.12"
bitflags = "0.3.2"
libc = "0.2.4"
bitflags = "0.3.3"

[dev-dependencies]
rand = "0.3.8"
Expand Down
8 changes: 4 additions & 4 deletions src/fcntl.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {Error, Result, NixPath};
use errno::Errno;
use libc::{mode_t, c_int};
use libc::c_int;
use sys::stat::Mode;
use std::os::unix::io::RawFd;

Expand All @@ -11,8 +11,8 @@ pub use self::ffi::flock;
mod ffi {
pub use libc::{open, fcntl};
pub use self::os::*;
pub use libc::funcs::bsd44::flock as libc_flock;
pub use libc::consts::os::bsd44::{LOCK_SH, LOCK_EX, LOCK_NB, LOCK_UN};
pub use libc::flock as libc_flock;
pub use libc::{LOCK_SH, LOCK_EX, LOCK_NB, LOCK_UN};

#[cfg(any(target_os = "linux", target_os = "android"))]
mod os {
Expand Down Expand Up @@ -99,7 +99,7 @@ mod ffi {

pub fn open<P: ?Sized + NixPath>(path: &P, oflag: OFlag, mode: Mode) -> Result<RawFd> {
let fd = try!(path.with_nix_path(|cstr| {
unsafe { ffi::open(cstr.as_ptr(), oflag.bits(), mode.bits() as mode_t) }
unsafe { ffi::open(cstr.as_ptr(), oflag.bits(), mode.bits() as c_int) }
}));

if fd < 0 {
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub mod mqueue;
pub mod sched;

pub mod sys;
#[allow(improper_ctypes)]
pub mod unistd;

/*
Expand All @@ -43,14 +44,15 @@ pub mod unistd;
*
*/

use libc::{c_char, PATH_MAX};
use libc::c_char;
use std::{ptr, result};
use std::ffi::CStr;
use std::path::{Path, PathBuf};
use std::os::unix::ffi::OsStrExt;
use std::io;
use std::fmt;
use std::error;
use libc::PATH_MAX;

pub type Result<T> = result::Result<T, Error>;

Expand Down
1 change: 1 addition & 0 deletions src/mqueue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ mod ffi {
use super::MQd;
use super::MqAttr;

#[allow(improper_ctypes)]
extern "C" {
pub fn mq_open(name: *const c_char, oflag: c_int, ...) -> MQd;

Expand Down
2 changes: 1 addition & 1 deletion src/sys/mman.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ mod ffi {

pub use libc::{mmap, munmap};


#[allow(improper_ctypes)]
extern {
pub fn shm_open(name: *const c_char, oflag: c_int, mode: mode_t) -> c_int;
pub fn shm_unlink(name: *const c_char) -> c_int;
Expand Down
1 change: 1 addition & 0 deletions src/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub mod utsname;

pub mod wait;

#[allow(improper_ctypes)]
pub mod mman;

pub mod uio;
Expand Down
4 changes: 2 additions & 2 deletions src/sys/quota.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub mod quota {

impl QuotaCmd {
pub fn as_int(&self) -> c_int {
((self.0 << 8) | (self.1 & 0x00ff)) as c_int
((self.0 << 8) | (self.1 & 0x00ff)) as c_int
}
}

Expand Down Expand Up @@ -97,7 +97,7 @@ fn quotactl<P: ?Sized + NixPath>(cmd: quota::QuotaCmd, special: Option<&P>, id:
pub fn quotactl_on<P: ?Sized + NixPath>(which: quota::QuotaType, special: &P, format: quota::QuotaFmt, quota_file: &P) -> Result<()> {
try!(quota_file.with_nix_path(|path| {
let mut path_copy = path.to_bytes_with_nul().to_owned();
let p: *mut i8 = path_copy.as_mut_ptr() as *mut i8;
let p: *mut c_char = path_copy.as_mut_ptr() as *mut c_char;
quotactl(quota::QuotaCmd(quota::Q_QUOTAON, which), Some(special), format as c_int, p)
}))
}
Expand Down
2 changes: 1 addition & 1 deletion src/sys/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::mem;
use std::ptr;
use {Error, Result};

pub use libc::consts::os::posix88::{
pub use libc::{
SIGHUP, // 1
SIGINT, // 2
SIGQUIT, // 3
Expand Down
44 changes: 24 additions & 20 deletions src/sys/socket/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,20 +278,31 @@ impl fmt::Display for Ipv4Addr {
#[derive(Clone, Copy)]
pub struct Ipv6Addr(pub libc::in6_addr);

macro_rules! to_u8_array {
($($num:ident),*) => {
if cfg!(target_endian = "big") {
[ $(($num>>8) as u8, ($num&0xff) as u8,)* ]
} else {
[ $(($num&0xff) as u8, ($num>>8) as u8,)* ]
}
}
}

macro_rules! to_u16_array {
($slf:ident, $($first:expr, $second:expr),*) => {
if cfg!(target_endian = "big") {
[$( (($slf.0.s6_addr[$first] as u16) << 8) + $slf.0.s6_addr[$second] as u16,)*]
} else {
[$( (($slf.0.s6_addr[$second] as u16) << 8) + $slf.0.s6_addr[$first] as u16,)*]
}
}
}

impl Ipv6Addr {
pub fn new(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16, g: u16, h: u16) -> Ipv6Addr {
Ipv6Addr(libc::in6_addr {
s6_addr: [
a.to_be(),
b.to_be(),
c.to_be(),
d.to_be(),
e.to_be(),
f.to_be(),
g.to_be(),
h.to_be(),
]
})
let mut in6_addr_var: libc::in6_addr = unsafe{mem::uninitialized()};
in6_addr_var.s6_addr = to_u8_array!(a,b,c,d,e,f,g,h);
Ipv6Addr(in6_addr_var)
}

pub fn from_std(std: &net::Ipv6Addr) -> Ipv6Addr {
Expand All @@ -301,14 +312,7 @@ impl Ipv6Addr {

/// Return the eight 16-bit segments that make up this address
pub fn segments(&self) -> [u16; 8] {
[u16::from_be(self.0.s6_addr[0]),
u16::from_be(self.0.s6_addr[1]),
u16::from_be(self.0.s6_addr[2]),
u16::from_be(self.0.s6_addr[3]),
u16::from_be(self.0.s6_addr[4]),
u16::from_be(self.0.s6_addr[5]),
u16::from_be(self.0.s6_addr[6]),
u16::from_be(self.0.s6_addr[7])]
to_u16_array!(self, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)
}

pub fn to_std(&self) -> net::Ipv6Addr {
Expand Down
16 changes: 6 additions & 10 deletions src/sys/socket/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,16 +204,10 @@ mod os {
pub const IP_ADD_MEMBERSHIP: c_int = 12;
pub const IP_DROP_MEMBERSHIP: c_int = 13;

#[cfg(not(target_os = "netbsd"))]
pub const IPV6_ADD_MEMBERSHIP: c_int = libc::IPV6_ADD_MEMBERSHIP;
#[cfg(not(target_os = "netbsd"))]
pub const IPV6_DROP_MEMBERSHIP: c_int = libc::IPV6_DROP_MEMBERSHIP;

#[cfg(target_os = "netbsd")]
pub const IPV6_JOIN_GROUP: c_int = 12;

#[cfg(target_os = "netbsd")]
pub const IPV6_LEAVE_GROUP: c_int = 13;
#[cfg(not(target_os = "openbsd"))]
pub const IPV6_JOIN_GROUP: c_int = libc::IPV6_JOIN_GROUP;
#[cfg(not(target_os = "openbsd"))]
pub const IPV6_LEAVE_GROUP: c_int = libc::IPV6_LEAVE_GROUP;

pub type InAddrT = u32;

Expand Down Expand Up @@ -295,6 +289,8 @@ mod os {
pub const IP_MULTICAST_LOOP: c_int = 11;
pub const IP_ADD_MEMBERSHIP: c_int = 12;
pub const IP_DROP_MEMBERSHIP: c_int = 13;
pub const IPV6_JOIN_GROUP: c_int = libc::IPV6_JOIN_GROUP;
pub const IPV6_LEAVE_GROUP: c_int = libc::IPV6_LEAVE_GROUP;

pub type InAddrT = u32;

Expand Down
8 changes: 4 additions & 4 deletions src/sys/socket/sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,13 @@ sockopt_impl!(Both, TcpNoDelay, consts::IPPROTO_TCP, consts::TCP_NODELAY, bool);
sockopt_impl!(Both, Linger, consts::SOL_SOCKET, consts::SO_LINGER, super::linger);
sockopt_impl!(SetOnly, IpAddMembership, consts::IPPROTO_IP, consts::IP_ADD_MEMBERSHIP, super::ip_mreq);
sockopt_impl!(SetOnly, IpDropMembership, consts::IPPROTO_IP, consts::IP_DROP_MEMBERSHIP, super::ip_mreq);
#[cfg(not(target_os = "netbsd"))]
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "netbsd", target_os = "dragonfly", target_os = "freebsd")))]
sockopt_impl!(SetOnly, Ipv6AddMembership, consts::IPPROTO_IPV6, consts::IPV6_ADD_MEMBERSHIP, super::ipv6_mreq);
#[cfg(not(target_os = "netbsd"))]
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "netbsd", target_os = "dragonfly", target_os = "freebsd")))]
sockopt_impl!(SetOnly, Ipv6DropMembership, consts::IPPROTO_IPV6, consts::IPV6_DROP_MEMBERSHIP, super::ipv6_mreq);
#[cfg(target_os = "netbsd")]
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "netbsd", target_os = "dragonfly", target_os = "freebsd"))]
sockopt_impl!(SetOnly, Ipv6AddMembership, consts::IPPROTO_IPV6, consts::IPV6_JOIN_GROUP, super::ipv6_mreq);
#[cfg(target_os = "netbsd")]
#[cfg(any(target_os = "macos", target_os = "ios", target_os = "netbsd", target_os = "dragonfly", target_os = "freebsd"))]
sockopt_impl!(SetOnly, Ipv6DropMembership, consts::IPPROTO_IPV6, consts::IPV6_LEAVE_GROUP, super::ipv6_mreq);
sockopt_impl!(Both, IpMulticastTtl, consts::IPPROTO_IP, consts::IP_MULTICAST_TTL, u8);
sockopt_impl!(Both, IpMulticastLoop, consts::IPPROTO_IP, consts::IP_MULTICAST_LOOP, bool);
Expand Down
4 changes: 2 additions & 2 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ pub use self::linux::*;

mod ffi {
use libc::{c_char, c_int, size_t};
pub use libc::{close, read, write, pipe, ftruncate, unlink, setpgid};
pub use libc::funcs::posix88::unistd::{fork, getegid, geteuid, getgid, getpid, getppid, getuid};
pub use libc::{close, read, write, pipe, ftruncate, unlink, setpgid, fork, getpid, getppid, getuid, geteuid, getgid, getegid};

#[allow(improper_ctypes)]
extern {
// duplicate a file descriptor
// doc: http://man7.org/linux/man-pages/man2/dup.2.html
Expand Down
3 changes: 2 additions & 1 deletion test/sys/test_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::path::Path;
use std::str::FromStr;
use std::os::unix::io::{AsRawFd, RawFd};
use ports::localhost;
use libc::c_char;

#[test]
pub fn test_inetv4_addr_to_sock_addr() {
Expand Down Expand Up @@ -32,7 +33,7 @@ pub fn test_path_to_sock_addr() {
let actual = Path::new("/foo/bar");
let addr = UnixAddr::new(actual).unwrap();

let expect: &'static [i8] = unsafe { mem::transmute(&b"/foo/bar"[..]) };
let expect: &'static [c_char] = unsafe { mem::transmute(&b"/foo/bar"[..]) };
assert_eq!(&addr.0.sun_path[..8], expect);

assert_eq!(addr.path(), Some(actual));
Expand Down
2 changes: 1 addition & 1 deletion test/sys/test_wait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use nix::unistd::*;
use nix::unistd::Fork::*;
use nix::sys::signal::*;
use nix::sys::wait::*;
use libc::funcs::c95::stdlib::exit;
use libc::exit;

#[test]
fn test_wait_signal() {
Expand Down
5 changes: 2 additions & 3 deletions test/test_stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fs::File;
use std::os::unix::fs::symlink;
use std::os::unix::prelude::AsRawFd;

use libc::consts::os::posix88;
use libc::{S_IFMT, S_IFLNK};

use nix::sys::stat::{stat, fstat, lstat};

Expand Down Expand Up @@ -46,8 +46,7 @@ fn assert_lstat_results(stat_result: Result<FileStat>) {
// st_mode is c_uint (u32 on Android) while S_IFMT is mode_t
// (u16 on Android), and that will be a compile error.
// On other platforms they are the same (either both are u16 or u32).
assert!((stats.st_mode as usize) & (posix88::S_IFMT as usize)
== posix88::S_IFLNK as usize); // should be a link
assert!((stats.st_mode as usize) & (S_IFMT as usize) == S_IFLNK as usize); // should be a link
assert!(stats.st_nlink == 1); // there links created, must be 1
assert!(valid_uid_gid(stats)); // must be positive integers
assert!(stats.st_size > 0); // size is > 0 because it points to another file
Expand Down