diff options
Diffstat (limited to 'src/sys')
-rw-r--r-- | src/sys/epoll.rs | 44 | ||||
-rw-r--r-- | src/sys/event.rs | 118 | ||||
-rw-r--r-- | src/sys/eventfd.rs | 8 | ||||
-rw-r--r-- | src/sys/mman.rs | 72 | ||||
-rw-r--r-- | src/sys/quota.rs | 36 | ||||
-rw-r--r-- | src/sys/select.rs | 111 | ||||
-rw-r--r-- | src/sys/signal.rs | 48 | ||||
-rw-r--r-- | src/sys/signalfd.rs | 6 | ||||
-rw-r--r-- | src/sys/socket/mod.rs | 132 | ||||
-rw-r--r-- | src/sys/stat.rs | 53 | ||||
-rw-r--r-- | src/sys/termios.rs | 184 | ||||
-rw-r--r-- | src/sys/utsname.rs | 39 | ||||
-rw-r--r-- | src/sys/wait.rs | 24 |
13 files changed, 435 insertions, 440 deletions
diff --git a/src/sys/epoll.rs b/src/sys/epoll.rs index df48b9af..5ab766dc 100644 --- a/src/sys/epoll.rs +++ b/src/sys/epoll.rs @@ -6,38 +6,38 @@ use std::mem; use ::Error; libc_bitflags!( - pub flags EpollFlags: libc::c_int { - EPOLLIN, - EPOLLPRI, - EPOLLOUT, - EPOLLRDNORM, - EPOLLRDBAND, - EPOLLWRNORM, - EPOLLWRBAND, - EPOLLMSG, - EPOLLERR, - EPOLLHUP, - EPOLLRDHUP, + pub struct EpollFlags: libc::c_int { + EPOLLIN; + EPOLLPRI; + EPOLLOUT; + EPOLLRDNORM; + EPOLLRDBAND; + EPOLLWRNORM; + EPOLLWRBAND; + EPOLLMSG; + EPOLLERR; + EPOLLHUP; + EPOLLRDHUP; #[cfg(target_os = "linux")] // Added in 4.5; not in Android. - EPOLLEXCLUSIVE, + EPOLLEXCLUSIVE; #[cfg(not(target_arch = "mips"))] - EPOLLWAKEUP, - EPOLLONESHOT, - EPOLLET, + EPOLLWAKEUP; + EPOLLONESHOT; + EPOLLET; } ); #[derive(Clone, Copy, Eq, PartialEq)] -#[repr(C)] +#[repr(i32)] pub enum EpollOp { - EpollCtlAdd = 1, - EpollCtlDel = 2, - EpollCtlMod = 3 + EpollCtlAdd = libc::EPOLL_CTL_ADD, + EpollCtlDel = libc::EPOLL_CTL_DEL, + EpollCtlMod = libc::EPOLL_CTL_MOD, } libc_bitflags!{ - pub flags EpollCreateFlags: c_int { - EPOLL_CLOEXEC, + pub struct EpollCreateFlags: c_int { + EPOLL_CLOEXEC; } } diff --git a/src/sys/event.rs b/src/sys/event.rs index 9215c654..1f9c1251 100644 --- a/src/sys/event.rs +++ b/src/sys/event.rs @@ -84,11 +84,11 @@ pub type type_of_event_flag = u16; #[cfg(any(target_os = "netbsd"))] pub type type_of_event_flag = u32; libc_bitflags!{ - pub flags EventFlag: type_of_event_flag { - EV_ADD, - EV_CLEAR, - EV_DELETE, - EV_DISABLE, + pub struct EventFlag: type_of_event_flag { + EV_ADD; + EV_CLEAR; + EV_DELETE; + EV_DISABLE; // No released version of OpenBSD supports EV_DISPATCH or EV_RECEIPT. // These have been commited to the -current branch though and are // expected to be part of the OpenBSD 6.2 release in Nov 2017. @@ -97,117 +97,117 @@ libc_bitflags!{ #[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "ios", target_os = "macos", target_os = "netbsd"))] - EV_DISPATCH, + EV_DISPATCH; #[cfg(target_os = "freebsd")] - EV_DROP, - EV_ENABLE, - EV_EOF, - EV_ERROR, + EV_DROP; + EV_ENABLE; + EV_EOF; + EV_ERROR; #[cfg(any(target_os = "macos", target_os = "ios"))] - EV_FLAG0, - EV_FLAG1, + EV_FLAG0; + EV_FLAG1; #[cfg(target_os = "dragonfly")] - EV_NODATA, - EV_ONESHOT, + EV_NODATA; + EV_ONESHOT; #[cfg(any(target_os = "macos", target_os = "ios"))] - EV_OOBAND, + EV_OOBAND; #[cfg(any(target_os = "macos", target_os = "ios"))] - EV_POLL, + EV_POLL; #[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "ios", target_os = "macos", target_os = "netbsd"))] - EV_RECEIPT, - EV_SYSFLAGS, + EV_RECEIPT; + EV_SYSFLAGS; } } libc_bitflags!( - pub flags FilterFlag: u32 { + pub struct FilterFlag: u32 { #[cfg(any(target_os = "macos", target_os = "ios"))] - NOTE_ABSOLUTE, - NOTE_ATTRIB, - NOTE_CHILD, - NOTE_DELETE, + NOTE_ABSOLUTE; + NOTE_ATTRIB; + NOTE_CHILD; + NOTE_DELETE; #[cfg(target_os = "openbsd")] - NOTE_EOF, - NOTE_EXEC, - NOTE_EXIT, + NOTE_EOF; + NOTE_EXEC; + NOTE_EXIT; #[cfg(any(target_os = "macos", target_os = "ios"))] - NOTE_EXIT_REPARENTED, + NOTE_EXIT_REPARENTED; #[cfg(any(target_os = "macos", target_os = "ios"))] - NOTE_EXITSTATUS, - NOTE_EXTEND, + NOTE_EXITSTATUS; + NOTE_EXTEND; #[cfg(any(target_os = "macos", target_os = "ios", target_os = "freebsd", target_os = "dragonfly"))] - NOTE_FFAND, + NOTE_FFAND; #[cfg(any(target_os = "macos", target_os = "ios", target_os = "freebsd", target_os = "dragonfly"))] - NOTE_FFCOPY, + NOTE_FFCOPY; #[cfg(any(target_os = "macos", target_os = "ios", target_os = "freebsd", target_os = "dragonfly"))] - NOTE_FFCTRLMASK, + NOTE_FFCTRLMASK; #[cfg(any(target_os = "macos", target_os = "ios", target_os = "freebsd", target_os = "dragonfly"))] - NOTE_FFLAGSMASK, + NOTE_FFLAGSMASK; #[cfg(any(target_os = "macos", target_os = "ios", target_os = "freebsd", target_os = "dragonfly"))] - NOTE_FFNOP, + NOTE_FFNOP; #[cfg(any(target_os = "macos", target_os = "ios", target_os = "freebsd", target_os = "dragonfly"))] - NOTE_FFOR, - NOTE_FORK, - NOTE_LINK, - NOTE_LOWAT, + NOTE_FFOR; + NOTE_FORK; + NOTE_LINK; + NOTE_LOWAT; #[cfg(target_os = "freebsd")] - NOTE_MSECONDS, + NOTE_MSECONDS; #[cfg(any(target_os = "macos", target_os = "ios"))] - NOTE_NONE, + NOTE_NONE; #[cfg(any(target_os = "macos", target_os = "ios", target_os = "freebsd"))] - NOTE_NSECONDS, + NOTE_NSECONDS; #[cfg(target_os = "dragonfly")] - NOTE_OOB, - NOTE_PCTRLMASK, - NOTE_PDATAMASK, + NOTE_OOB; + NOTE_PCTRLMASK; + NOTE_PDATAMASK; #[cfg(any(target_os = "macos", target_os = "ios"))] - NOTE_REAP, - NOTE_RENAME, - NOTE_REVOKE, + NOTE_REAP; + NOTE_RENAME; + NOTE_REVOKE; #[cfg(any(target_os = "macos", target_os = "ios", target_os = "freebsd"))] - NOTE_SECONDS, + NOTE_SECONDS; #[cfg(any(target_os = "macos", target_os = "ios"))] - NOTE_SIGNAL, - NOTE_TRACK, - NOTE_TRACKERR, + NOTE_SIGNAL; + NOTE_TRACK; + NOTE_TRACKERR; #[cfg(any(target_os = "macos", target_os = "ios", target_os = "freebsd", target_os = "dragonfly"))] - NOTE_TRIGGER, + NOTE_TRIGGER; #[cfg(target_os = "openbsd")] - NOTE_TRUNCATE, + NOTE_TRUNCATE; #[cfg(any(target_os = "macos", target_os = "ios", target_os = "freebsd"))] - NOTE_USECONDS, + NOTE_USECONDS; #[cfg(any(target_os = "macos", target_os = "ios"))] - NOTE_VM_ERROR, + NOTE_VM_ERROR; #[cfg(any(target_os = "macos", target_os = "ios"))] - NOTE_VM_PRESSURE, + NOTE_VM_PRESSURE; #[cfg(any(target_os = "macos", target_os = "ios"))] - NOTE_VM_PRESSURE_SUDDEN_TERMINATE, + NOTE_VM_PRESSURE_SUDDEN_TERMINATE; #[cfg(any(target_os = "macos", target_os = "ios"))] - NOTE_VM_PRESSURE_TERMINATE, - NOTE_WRITE, + NOTE_VM_PRESSURE_TERMINATE; + NOTE_WRITE; } ); diff --git a/src/sys/eventfd.rs b/src/sys/eventfd.rs index ad23dec7..51091478 100644 --- a/src/sys/eventfd.rs +++ b/src/sys/eventfd.rs @@ -3,10 +3,10 @@ use std::os::unix::io::RawFd; use {Errno, Result}; libc_bitflags! { - pub flags EfdFlags: libc::c_int { - EFD_CLOEXEC, // Since Linux 2.6.27 - EFD_NONBLOCK, // Since Linux 2.6.27 - EFD_SEMAPHORE, // Since Linux 2.6.30 + pub struct EfdFlags: libc::c_int { + EFD_CLOEXEC; // Since Linux 2.6.27 + EFD_NONBLOCK; // Since Linux 2.6.27 + EFD_SEMAPHORE; // Since Linux 2.6.30 } } diff --git a/src/sys/mman.rs b/src/sys/mman.rs index e1ddc38f..dc21899b 100644 --- a/src/sys/mman.rs +++ b/src/sys/mman.rs @@ -6,94 +6,94 @@ use std::os::unix::io::RawFd; libc_bitflags!{ /// Desired memory protection of a memory mapping. - pub flags ProtFlags: libc::c_int { + pub struct ProtFlags: libc::c_int { /// Pages cannot be accessed. - PROT_NONE, + PROT_NONE; /// Pages can be read. - PROT_READ, + PROT_READ; /// Pages can be written. - PROT_WRITE, + PROT_WRITE; /// Pages can be executed - PROT_EXEC, + PROT_EXEC; /// Apply protection up to the end of a mapping that grows upwards. #[cfg(any(target_os = "android", target_os = "linux"))] - PROT_GROWSDOWN, + PROT_GROWSDOWN; /// Apply protection down to the beginning of a mapping that grows downwards. #[cfg(any(target_os = "android", target_os = "linux"))] - PROT_GROWSUP, + PROT_GROWSUP; } } libc_bitflags!{ /// Additional parameters for `mmap()`. - pub flags MapFlags: c_int { + pub struct MapFlags: c_int { /// Compatibility flag. Ignored. - MAP_FILE, + MAP_FILE; /// Share this mapping. Mutually exclusive with `MAP_PRIVATE`. - MAP_SHARED, + MAP_SHARED; /// Create a private copy-on-write mapping. Mutually exclusive with `MAP_SHARED`. - MAP_PRIVATE, + MAP_PRIVATE; /// Place the mapping at exactly the address specified in `addr`. - MAP_FIXED, + MAP_FIXED; /// Synonym for `MAP_ANONYMOUS`. - MAP_ANON, + MAP_ANON; /// The mapping is not backed by any file. #[cfg(any(target_os = "android", target_os = "linux", target_os = "freebsd"))] - MAP_ANONYMOUS, + MAP_ANONYMOUS; /// Put the mapping into the first 2GB of the process address space. #[cfg(any(all(any(target_os = "android", target_os = "linux"), any(target_arch = "x86", target_arch = "x86_64")), all(target_os = "linux", target_env = "musl", any(target_arch = "x86", target_pointer_width = "64")), all(target_os = "freebsd", target_pointer_width = "64")))] - MAP_32BIT, + MAP_32BIT; /// Used for stacks; indicates to the kernel that the mapping should extend downward in memory. #[cfg(any(target_os = "android", target_os = "linux"))] - MAP_GROWSDOWN, + MAP_GROWSDOWN; /// Compatibility flag. Ignored. #[cfg(any(target_os = "android", target_os = "linux"))] - MAP_DENYWRITE, + MAP_DENYWRITE; /// Compatibility flag. Ignored. #[cfg(any(target_os = "android", target_os = "linux"))] - MAP_EXECUTABLE, + MAP_EXECUTABLE; /// Mark the mmaped region to be locked in the same way as `mlock(2)`. #[cfg(any(target_os = "android", target_os = "linux"))] - MAP_LOCKED, + MAP_LOCKED; /// Do not reserve swap space for this mapping. /// /// This was removed in FreeBSD 11. #[cfg(not(target_os = "freebsd"))] - MAP_NORESERVE, + MAP_NORESERVE; /// Populate page tables for a mapping. #[cfg(any(target_os = "android", target_os = "linux"))] - MAP_POPULATE, + MAP_POPULATE; /// Only meaningful when used with `MAP_POPULATE`. Don't perform read-ahead. #[cfg(any(target_os = "android", target_os = "linux"))] - MAP_NONBLOCK, + MAP_NONBLOCK; /// Allocate the mapping using "huge pages." #[cfg(any(target_os = "android", target_os = "linux"))] - MAP_HUGETLB, + MAP_HUGETLB; /// Lock the mapped region into memory as with `mlock(2)`. #[cfg(target_os = "netbsd")] - MAP_WIRED, + MAP_WIRED; /// Causes dirtied data in the specified range to be flushed to disk only when necessary. #[cfg(any(target_os = "dragonfly", target_os = "freebsd"))] - MAP_NOSYNC, + MAP_NOSYNC; /// Rename private pages to a file. /// /// This was removed in FreeBSD 11. #[cfg(any(target_os = "dragonfly", target_os = "netbsd", target_os = "openbsd"))] - MAP_RENAME, + MAP_RENAME; /// Region may contain semaphores. #[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))] - MAP_HASSEMAPHORE, + MAP_HASSEMAPHORE; /// Region grows down, like a stack. #[cfg(any(target_os = "android", target_os = "dragonfly", target_os = "freebsd", target_os = "linux"))] - MAP_STACK, + MAP_STACK; /// Pages in this mapping are not retained in the kernel's memory cache. #[cfg(any(target_os = "ios", target_os = "macos"))] - MAP_NOCACHE, + MAP_NOCACHE; #[cfg(any(target_os = "ios", target_os = "macos"))] - MAP_JIT, + MAP_JIT; } } @@ -185,19 +185,19 @@ libc_enum!{ libc_bitflags!{ /// Configuration flags for `msync`. - pub flags MsFlags: c_int { + pub struct MsFlags: c_int { /// Schedule an update but return immediately. - MS_ASYNC, + MS_ASYNC; /// Invalidate all cached data. - MS_INVALIDATE, + MS_INVALIDATE; /// Invalidate pages, but leave them mapped. #[cfg(any(target_os = "ios", target_os = "macos"))] - MS_KILLPAGES, + MS_KILLPAGES; /// Deactivate pages, but leave them mapped. #[cfg(any(target_os = "ios", target_os = "macos"))] - MS_DEACTIVATE, + MS_DEACTIVATE; /// Perform an update and wait for it to complete. - MS_SYNC, + MS_SYNC; } } diff --git a/src/sys/quota.rs b/src/sys/quota.rs index 0b39fcf2..1cd143bd 100644 --- a/src/sys/quota.rs +++ b/src/sys/quota.rs @@ -1,5 +1,5 @@ use {Errno, Result, NixPath}; -use libc::{c_int, c_char}; +use libc::{self, c_int, c_char}; #[cfg(all(target_os = "linux", any(target_arch = "x86", @@ -41,17 +41,17 @@ pub mod quota { libc_bitflags!( #[derive(Default)] - pub flags QuotaValidFlags: u32 { - QIF_BLIMITS, - QIF_SPACE, - QIF_ILIMITS, - QIF_INODES, - QIF_BTIME, - QIF_ITIME, - QIF_LIMITS, - QIF_USAGE, - QIF_TIMES, - QIF_ALL, + pub struct QuotaValidFlags: u32 { + QIF_BLIMITS; + QIF_SPACE; + QIF_ILIMITS; + QIF_INODES; + QIF_BTIME; + QIF_ITIME; + QIF_LIMITS; + QIF_USAGE; + QIF_TIMES; + QIF_ALL; } ); @@ -70,14 +70,6 @@ pub mod quota { } } -mod ffi { - use libc::{c_int, c_char}; - - extern { - pub fn quotactl(cmd: c_int, special: * const c_char, id: c_int, data: *mut c_char) -> c_int; - } -} - use std::ptr; fn quotactl<P: ?Sized + NixPath>(cmd: quota::QuotaCmd, special: Option<&P>, id: c_int, addr: *mut c_char) -> Result<()> { @@ -85,8 +77,8 @@ fn quotactl<P: ?Sized + NixPath>(cmd: quota::QuotaCmd, special: Option<&P>, id: Errno::clear(); let res = try!( match special { - Some(dev) => dev.with_nix_path(|path| ffi::quotactl(cmd.as_int(), path.as_ptr(), id, addr)), - None => Ok(ffi::quotactl(cmd.as_int(), ptr::null(), id, addr)), + Some(dev) => dev.with_nix_path(|path| libc::quotactl(cmd.as_int(), path.as_ptr(), id, addr)), + None => Ok(libc::quotactl(cmd.as_int(), ptr::null(), id, addr)), } ); diff --git a/src/sys/select.rs b/src/sys/select.rs index 82b2aad3..07190fa1 100644 --- a/src/sys/select.rs +++ b/src/sys/select.rs @@ -1,57 +1,37 @@ -use std::ptr::null_mut; +use std::mem; use std::os::unix::io::RawFd; -use libc::{c_int, timeval}; +use std::ptr::null_mut; +use libc::{self, c_int}; use {Errno, Result}; use sys::time::TimeVal; -pub const FD_SETSIZE: RawFd = 1024; +pub use libc::FD_SETSIZE; -#[cfg(any(target_os = "macos", target_os = "ios"))] +// FIXME: Change to repr(transparent) once it's stable #[repr(C)] -#[derive(Clone)] -pub struct FdSet { - bits: [i32; FD_SETSIZE as usize / 32] -} - -#[cfg(any(target_os = "macos", target_os = "ios"))] -const BITS: usize = 32; - -#[cfg(not(any(target_os = "macos", target_os = "ios")))] -#[repr(C)] -#[derive(Clone)] -pub struct FdSet { - bits: [u64; FD_SETSIZE as usize / 64] -} - -#[cfg(not(any(target_os = "macos", target_os = "ios")))] -const BITS: usize = 64; +pub struct FdSet(libc::fd_set); impl FdSet { pub fn new() -> FdSet { - FdSet { - bits: [0; FD_SETSIZE as usize / BITS] - } + let mut fdset = unsafe { mem::uninitialized() }; + unsafe { libc::FD_ZERO(&mut fdset) }; + FdSet(fdset) } pub fn insert(&mut self, fd: RawFd) { - let fd = fd as usize; - self.bits[fd / BITS] |= 1 << (fd % BITS); + unsafe { libc::FD_SET(fd, &mut self.0) }; } pub fn remove(&mut self, fd: RawFd) { - let fd = fd as usize; - self.bits[fd / BITS] &= !(1 << (fd % BITS)); + unsafe { libc::FD_CLR(fd, &mut self.0) }; } - pub fn contains(&self, fd: RawFd) -> bool { - let fd = fd as usize; - self.bits[fd / BITS] & (1 << (fd % BITS)) > 0 + pub fn contains(&mut self, fd: RawFd) -> bool { + unsafe { libc::FD_ISSET(fd, &mut self.0) } } pub fn clear(&mut self) { - for bits in &mut self.bits { - *bits = 0 - } + unsafe { libc::FD_ZERO(&mut self.0) }; } /// Finds the highest file descriptor in the set. @@ -74,16 +54,11 @@ impl FdSet { /// ``` /// /// [`select`]: fn.select.html - pub fn highest(&self) -> Option<RawFd> { - for (i, &block) in self.bits.iter().enumerate().rev() { - if block != 0 { - // Highest bit is located at `BITS - 1 - n.leading_zeros()`. Examples: - // 0b00000001 - // 7 leading zeros, result should be 0 (bit at index 0 is 1) - // 0b001xxxxx - // 2 leading zeros, result should be 5 (bit at index 5 is 1) - x may be 0 or 1 - - return Some((i * BITS + BITS - 1 - block.leading_zeros() as usize) as RawFd); + pub fn highest(&mut self) -> Option<RawFd> { + for i in (0..libc::FD_SETSIZE).rev() { + let i = i as RawFd; + if unsafe { libc::FD_ISSET(i, self as *mut _ as *mut libc::fd_set) } { + return Some(i) } } @@ -91,19 +66,6 @@ impl FdSet { } } -mod ffi { - use libc::{c_int, timeval}; - use super::FdSet; - - extern { - pub fn select(nfds: c_int, - readfds: *mut FdSet, - writefds: *mut FdSet, - errorfds: *mut FdSet, - timeout: *mut timeval) -> c_int; - } -} - /// Monitors file descriptors for readiness (see [select(2)]). /// /// Returns the total number of ready file descriptors in all sets. The sets are changed so that all @@ -136,28 +98,28 @@ where E: Into<Option<&'a mut FdSet>>, T: Into<Option<&'a mut TimeVal>>, { - let readfds = readfds.into(); - let writefds = writefds.into(); - let errorfds = errorfds.into(); + let mut readfds = readfds.into(); + let mut writefds = writefds.into(); + let mut errorfds = errorfds.into(); let timeout = timeout.into(); let nfds = nfds.into().unwrap_or_else(|| { - readfds.iter() - .chain(writefds.iter()) - .chain(errorfds.iter()) + readfds.iter_mut() + .chain(writefds.iter_mut()) + .chain(errorfds.iter_mut()) .map(|set| set.highest().unwrap_or(-1)) .max() .unwrap_or(-1) + 1 }); - let readfds = readfds.map(|set| set as *mut FdSet).unwrap_or(null_mut()); - let writefds = writefds.map(|set| set as *mut FdSet).unwrap_or(null_mut()); - let errorfds = errorfds.map(|set| set as *mut FdSet).unwrap_or(null_mut()); - let timeout = timeout.map(|tv| tv as *mut TimeVal as *mut timeval) + let readfds = readfds.map(|set| set as *mut _ as *mut libc::fd_set).unwrap_or(null_mut()); + let writefds = writefds.map(|set| set as *mut _ as *mut libc::fd_set).unwrap_or(null_mut()); + let errorfds = errorfds.map(|set| set as *mut _ as *mut libc::fd_set).unwrap_or(null_mut()); + let timeout = timeout.map(|tv| tv as *mut _ as *mut libc::timeval) .unwrap_or(null_mut()); let res = unsafe { - ffi::select(nfds, readfds, writefds, errorfds, timeout) + libc::select(nfds, readfds, writefds, errorfds, timeout) }; Errno::result(res) @@ -166,6 +128,7 @@ where #[cfg(test)] mod tests { use super::*; + use std::os::unix::io::RawFd; use sys::time::{TimeVal, TimeValLike}; use unistd::{write, pipe}; @@ -174,7 +137,7 @@ mod tests { let mut fd_set = FdSet::new(); for i in 0..FD_SETSIZE { - assert!(!fd_set.contains(i)); + assert!(!fd_set.contains(i as RawFd)); } fd_set.insert(7); @@ -187,14 +150,14 @@ mod tests { let mut fd_set = FdSet::new(); for i in 0..FD_SETSIZE { - assert!(!fd_set.contains(i)); + assert!(!fd_set.contains(i as RawFd)); } fd_set.insert(7); fd_set.remove(7); for i in 0..FD_SETSIZE { - assert!(!fd_set.contains(i)); + assert!(!fd_set.contains(i as RawFd)); } } @@ -202,13 +165,13 @@ mod tests { fn fdset_clear() { let mut fd_set = FdSet::new(); fd_set.insert(1); - fd_set.insert(FD_SETSIZE / 2); - fd_set.insert(FD_SETSIZE - 1); + fd_set.insert((FD_SETSIZE / 2) as RawFd); + fd_set.insert((FD_SETSIZE - 1) as RawFd); fd_set.clear(); for i in 0..FD_SETSIZE { - assert!(!fd_set.contains(i)); + assert!(!fd_set.contains(i as RawFd)); } } diff --git a/src/sys/signal.rs b/src/sys/signal.rs index a5ec9e3c..c53b5f5c 100644 --- a/src/sys/signal.rs +++ b/src/sys/signal.rs @@ -201,14 +201,14 @@ pub const SIGUNUSED : Signal = SIGSYS; #[cfg(not(target_os = "android"))] libc_bitflags!{ - pub flags SaFlags: libc::c_int { - SA_NOCLDSTOP, - SA_NOCLDWAIT, - SA_NODEFER, - SA_ONSTACK, - SA_RESETHAND, - SA_RESTART, - SA_SIGINFO, + pub struct SaFlags: libc::c_int { + SA_NOCLDSTOP; + SA_NOCLDWAIT; + SA_NODEFER; + SA_ONSTACK; + SA_RESETHAND; + SA_RESTART; + SA_SIGINFO; } } @@ -217,27 +217,27 @@ libc_bitflags!{ // FIXME: https://github.com/rust-lang/libc/pull/511 #[cfg(all(target_os = "android", target_pointer_width = "32"))] libc_bitflags!{ - pub flags SaFlags: libc::c_ulong { - SA_NOCLDSTOP as libc::c_ulong, - SA_NOCLDWAIT as libc::c_ulong, - SA_NODEFER as libc::c_ulong, - SA_ONSTACK as libc::c_ulong, - SA_RESETHAND as libc::c_ulong, - SA_RESTART as libc::c_ulong, - SA_SIGINFO as libc::c_ulong, + pub struct SaFlags: libc::c_ulong { + SA_NOCLDSTOP as libc::c_ulong; + SA_NOCLDWAIT as libc::c_ulong; + SA_NODEFER as libc::c_ulong; + SA_ONSTACK as libc::c_ulong; + SA_RESETHAND as libc::c_ulong; + SA_RESTART as libc::c_ulong; + SA_SIGINFO as libc::c_ulong; } } #[cfg(all(target_os = "android", target_pointer_width = "64"))] libc_bitflags!{ - pub flags SaFlags: libc::c_uint { - SA_NOCLDSTOP as libc::c_uint, - SA_NOCLDWAIT as libc::c_uint, - SA_NODEFER as libc::c_uint, - SA_ONSTACK as libc::c_uint, - SA_RESETHAND as libc::c_uint, - SA_RESTART as libc::c_uint, - SA_SIGINFO as libc::c_uint, + pub struct SaFlags: libc::c_uint { + SA_NOCLDSTOP as libc::c_uint; + SA_NOCLDWAIT as libc::c_uint; + SA_NODEFER as libc::c_uint; + SA_ONSTACK as libc::c_uint; + SA_RESETHAND as libc::c_uint; + SA_RESTART as libc::c_uint; + SA_SIGINFO as libc::c_uint; } } diff --git a/src/sys/signalfd.rs b/src/sys/signalfd.rs index fcf2efa9..abc96b8c 100644 --- a/src/sys/signalfd.rs +++ b/src/sys/signalfd.rs @@ -26,9 +26,9 @@ use std::mem; libc_bitflags!{ - pub flags SfdFlags: libc::c_int { - SFD_NONBLOCK, - SFD_CLOEXEC, + pub struct SfdFlags: libc::c_int { + SFD_NONBLOCK; + SFD_CLOEXEC; } } diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs index e0f957d8..eccd5e2d 100644 --- a/src/sys/socket/mod.rs +++ b/src/sys/socket/mod.rs @@ -3,8 +3,6 @@ //! [Further reading](http://man7.org/linux/man-pages/man7/socket.7.html) use {Error, Errno, Result}; use features; -use fcntl::{fcntl, FD_CLOEXEC, O_NONBLOCK}; -use fcntl::FcntlArg::{F_SETFD, F_SETFL}; use libc::{self, c_void, c_int, socklen_t, size_t, pid_t, uid_t, gid_t}; use std::{mem, ptr, slice}; use std::os::unix::io::RawFd; @@ -92,25 +90,46 @@ pub enum SockProtocol { KextControl = libc::SYSPROTO_CONTROL, } -bitflags!( - /// Extra flags - Supported by Linux 2.6.27, normalized on other platforms +libc_bitflags!{ + /// Additional socket options pub struct SockFlag: c_int { - const SOCK_NONBLOCK = 0o0004000; - const SOCK_CLOEXEC = 0o2000000; + /// Set non-blocking mode on the new socket + #[cfg(any(target_os = "android", + target_os = "dragonfly", + target_os = "freebsd", + target_os = "linux", + target_os = "netbsd", + target_os = "openbsd"))] + SOCK_NONBLOCK; + /// Set close-on-exec on the new descriptor + #[cfg(any(target_os = "android", + target_os = "dragonfly", + target_os = "freebsd", + target_os = "linux", + target_os = "netbsd", + target_os = "openbsd"))] + SOCK_CLOEXEC; + /// Return `EPIPE` instead of raising `SIGPIPE` + #[cfg(target_os = "netbsd")] + SOCK_NOSIGPIPE; + /// For domains `AF_INET(6)`, only allow `connect(2)`, `sendto(2)`, or `sendmsg(2)` + /// to the DNS port (typically 53) + #[cfg(target_os = "openbsd")] + SOCK_DNS; } -); +} libc_bitflags!{ /// Flags for send/recv and their relatives - pub flags MsgFlags: libc::c_int { + pub struct MsgFlags: libc::c_int { /// Sends or requests out-of-band data on sockets that support this notion /// (e.g., of type [`Stream`](enum.SockType.html)); the underlying protocol must also /// support out-of-band data. - MSG_OOB, + MSG_OOB; /// Peeks at an incoming message. The data is treated as unread and the next /// [`recv()`](fn.recv.html) /// or similar function shall still return this data. - MSG_PEEK, + MSG_PEEK; /// Enables nonblocking operation; if the operation would block, /// `EAGAIN` or `EWOULDBLOCK` is returned. This provides similar /// behavior to setting the `O_NONBLOCK` flag @@ -121,9 +140,9 @@ libc_bitflags!{ /// which will affect all threads in /// the calling process and as well as other processes that hold /// file descriptors referring to the same open file description. - MSG_DONTWAIT, + MSG_DONTWAIT; /// Receive flags: Control Data was discarded (buffer too small) - MSG_CTRUNC, + MSG_CTRUNC; /// For raw ([`Packet`](addr/enum.AddressFamily.html)), Internet datagram /// (since Linux 2.4.27/2.6.8), /// netlink (since Linux 2.6.22) and UNIX datagram (since Linux 3.4) @@ -132,15 +151,15 @@ libc_bitflags!{ /// domain ([unix(7)](https://linux.die.net/man/7/unix)) sockets. /// /// For use with Internet stream sockets, see [tcp(7)](https://linux.die.net/man/7/tcp). - MSG_TRUNC, + MSG_TRUNC; /// Terminates a record (when this notion is supported, as for /// sockets of type [`SeqPacket`](enum.SockType.html)). - MSG_EOR, + MSG_EOR; /// This flag specifies that queued errors should be received from /// the socket error queue. (For more details, see /// [recvfrom(2)](https://linux.die.net/man/2/recvfrom)) #[cfg(any(target_os = "linux", target_os = "android"))] - MSG_ERRQUEUE, + MSG_ERRQUEUE; /// Set the `close-on-exec` flag for the file descriptor received via a UNIX domain /// file descriptor using the `SCM_RIGHTS` operation (described in /// [unix(7)](https://linux.die.net/man/7/unix)). @@ -149,7 +168,7 @@ libc_bitflags!{ /// /// Only used in [`recvmsg`](fn.recvmsg.html) function. #[cfg(any(target_os = "linux", target_os = "android"))] - MSG_CMSG_CLOEXEC, + MSG_CMSG_CLOEXEC; } } @@ -449,13 +468,24 @@ pub fn socket<T: Into<Option<SockProtocol>>>(domain: AddressFamily, ty: SockType // TODO: Check the kernel version let res = try!(Errno::result(unsafe { ffi::socket(domain as c_int, ty, protocol) })); - if !feat_atomic { - if flags.contains(SOCK_CLOEXEC) { - try!(fcntl(res, F_SETFD(FD_CLOEXEC))); - } + #[cfg(any(target_os = "android", + target_os = "dragonfly", + target_os = "freebsd", + target_os = "linux", + target_os = "netbsd", + target_os = "openbsd"))] + { + use fcntl::{fcntl, FD_CLOEXEC, O_NONBLOCK}; + use fcntl::FcntlArg::{F_SETFD, F_SETFL}; - if flags.contains(SOCK_NONBLOCK) { - try!(fcntl(res, F_SETFL(O_NONBLOCK))); + if !feat_atomic { + if flags.contains(SOCK_CLOEXEC) { + try!(fcntl(res, F_SETFD(FD_CLOEXEC))); + } + + if flags.contains(SOCK_NONBLOCK) { + try!(fcntl(res, F_SETFL(O_NONBLOCK))); + } } } @@ -483,15 +513,26 @@ pub fn socketpair<T: Into<Option<SockProtocol>>>(domain: AddressFamily, ty: Sock }; try!(Errno::result(res)); - if !feat_atomic { - if flags.contains(SOCK_CLOEXEC) { - try!(fcntl(fds[0], F_SETFD(FD_CLOEXEC))); - try!(fcntl(fds[1], F_SETFD(FD_CLOEXEC))); - } + #[cfg(any(target_os = "android", + target_os = "dragonfly", + target_os = "freebsd", + target_os = "linux", + target_os = "netbsd", + target_os = "openbsd"))] + { + use fcntl::{fcntl, FD_CLOEXEC, O_NONBLOCK}; + use fcntl::FcntlArg::{F_SETFD, F_SETFL}; - if flags.contains(SOCK_NONBLOCK) { - try!(fcntl(fds[0], F_SETFL(O_NONBLOCK))); - try!(fcntl(fds[1], F_SETFL(O_NONBLOCK))); + if !feat_atomic { + if flags.contains(SOCK_CLOEXEC) { + try!(fcntl(fds[0], F_SETFD(FD_CLOEXEC))); + try!(fcntl(fds[1], F_SETFD(FD_CLOEXEC))); + } + + if flags.contains(SOCK_NONBLOCK) { + try!(fcntl(fds[0], F_SETFL(O_NONBLOCK))); + try!(fcntl(fds[1], F_SETFL(O_NONBLOCK))); + } } } Ok((fds[0], fds[1])) @@ -554,14 +595,37 @@ pub fn accept4(sockfd: RawFd, flags: SockFlag) -> Result<RawFd> { fn accept4_polyfill(sockfd: RawFd, flags: SockFlag) -> Result<RawFd> { let res = try!(Errno::result(unsafe { ffi::accept(sockfd, ptr::null_mut(), ptr::null_mut()) })); - if flags.contains(SOCK_CLOEXEC) { - try!(fcntl(res, F_SETFD(FD_CLOEXEC))); + #[cfg(any(target_os = "android", + target_os = "dragonfly", + target_os = "freebsd", + target_os = "linux", + target_os = "netbsd", + target_os = "openbsd"))] + { + use fcntl::{fcntl, FD_CLOEXEC, O_NONBLOCK}; + use fcntl::FcntlArg::{F_SETFD, F_SETFL}; + + if flags.contains(SOCK_CLOEXEC) { + try!(fcntl(res, F_SETFD(FD_CLOEXEC))); + } + + if flags.contains(SOCK_NONBLOCK) { + try!(fcntl(res, F_SETFL(O_NONBLOCK))); + } } - if flags.contains(SOCK_NONBLOCK) { - try!(fcntl(res, F_SETFL(O_NONBLOCK))); + // Disable unused variable warning on some platforms + #[cfg(not(any(target_os = "android", + target_os = "dragonfly", + target_os = "freebsd", + target_os = "linux", + target_os = "netbsd", + target_os = "openbsd")))] + { + let _ = flags; } + Ok(res) } diff --git a/src/sys/stat.rs b/src/sys/stat.rs index b089d3b5..7a0b3970 100644 --- a/src/sys/stat.rs +++ b/src/sys/stat.rs @@ -8,38 +8,35 @@ use std::mem; use std::os::unix::io::RawFd; libc_bitflags!( - pub flags SFlag: mode_t { - S_IFIFO, - S_IFCHR, - S_IFDIR, - S_IFBLK, - S_IFREG, - S_IFLNK, - S_IFSOCK, - S_IFMT, + pub struct SFlag: mode_t { + S_IFIFO; + S_IFCHR; + S_IFDIR; + S_IFBLK; + S_IFREG; + S_IFLNK; + S_IFSOCK; + S_IFMT; } ); -bitflags! { +libc_bitflags! { pub struct Mode: mode_t { - const S_IRWXU = libc::S_IRWXU; - const S_IRUSR = libc::S_IRUSR; - const S_IWUSR = libc::S_IWUSR; - const S_IXUSR = libc::S_IXUSR; - - const S_IRWXG = libc::S_IRWXG; - const S_IRGRP = libc::S_IRGRP; - const S_IWGRP = libc::S_IWGRP; - const S_IXGRP = libc::S_IXGRP; - - const S_IRWXO = libc::S_IRWXO; - const S_IROTH = libc::S_IROTH; - const S_IWOTH = libc::S_IWOTH; - const S_IXOTH = libc::S_IXOTH; - - const S_ISUID = libc::S_ISUID as mode_t; - const S_ISGID = libc::S_ISGID as mode_t; - const S_ISVTX = libc::S_ISVTX as mode_t; + S_IRWXU; + S_IRUSR; + S_IWUSR; + S_IXUSR; + S_IRWXG; + S_IRGRP; + S_IWGRP; + S_IXGRP; + S_IRWXO; + S_IROTH; + S_IWOTH; + S_IXOTH; + S_ISUID as mode_t; + S_ISGID as mode_t; + S_ISVTX as mode_t; } } diff --git a/src/sys/termios.rs b/src/sys/termios.rs index 8651a84d..89f18ee1 100644 --- a/src/sys/termios.rs +++ b/src/sys/termios.rs @@ -456,163 +456,163 @@ pub use libc::_POSIX_VDISABLE; libc_bitflags! { /// Flags for configuring the input mode of a terminal - pub flags InputFlags: tcflag_t { - IGNBRK, - BRKINT, - IGNPAR, - PARMRK, - INPCK, - ISTRIP, - INLCR, - IGNCR, - ICRNL, - IXON, - IXOFF, - IXANY, - IMAXBEL, + pub struct InputFlags: tcflag_t { + IGNBRK; + BRKINT; + IGNPAR; + PARMRK; + INPCK; + ISTRIP; + INLCR; + IGNCR; + ICRNL; + IXON; + IXOFF; + IXANY; + IMAXBEL; #[cfg(any(target_os = "android", target_os = "linux", target_os = "macos"))] - IUTF8, + IUTF8; } } libc_bitflags! { /// Flags for configuring the output mode of a terminal - pub flags OutputFlags: tcflag_t { - OPOST, + pub struct OutputFlags: tcflag_t { + OPOST; #[cfg(any(target_os = "android", target_os = "haiku", target_os = "linux", target_os = "openbsd"))] - OLCUC, - ONLCR, - OCRNL as tcflag_t, - ONOCR as tcflag_t, - ONLRET as tcflag_t, + OLCUC; + ONLCR; + OCRNL as tcflag_t; + ONOCR as tcflag_t; + ONLRET as tcflag_t; #[cfg(any(target_os = "android", target_os = "haiku", target_os = "ios", target_os = "linux", target_os = "macos"))] - OFILL as tcflag_t, + OFILL as tcflag_t; #[cfg(any(target_os = "android", target_os = "haiku", target_os = "ios", target_os = "linux", target_os = "macos"))] - OFDEL as tcflag_t, + OFDEL as tcflag_t; #[cfg(any(target_os = "android", target_os = "haiku", target_os = "ios", target_os = "linux", target_os = "macos"))] - NL0 as tcflag_t, + NL0 as tcflag_t; #[cfg(any(target_os = "android", target_os = "haiku", target_os = "ios", target_os = "linux", target_os = "macos"))] - NL1 as tcflag_t, + NL1 as tcflag_t; #[cfg(any(target_os = "android", target_os = "haiku", target_os = "ios", target_os = "linux", target_os = "macos"))] - CR0 as tcflag_t, + CR0 as tcflag_t; #[cfg(any(target_os = "android", target_os = "haiku", target_os = "ios", target_os = "linux", target_os = "macos"))] - CR1 as tcflag_t, + CR1 as tcflag_t; #[cfg(any(target_os = "android", target_os = "haiku", target_os = "ios", target_os = "linux", target_os = "macos"))] - CR2 as tcflag_t, + CR2 as tcflag_t; #[cfg(any(target_os = "android", target_os = "haiku", target_os = "ios", target_os = "linux", target_os = "macos"))] - CR3 as tcflag_t, + CR3 as tcflag_t; #[cfg(any(target_os = "android", target_os = "freebsd", target_os = "haiku", target_os = "ios", target_os = "linux", target_os = "macos"))] - TAB0 as tcflag_t, + TAB0 as tcflag_t; #[cfg(any(target_os = "android", target_os = "haiku", target_os = "ios", target_os = "linux", target_os = "macos"))] - TAB1 as tcflag_t, + TAB1 as tcflag_t; #[cfg(any(target_os = "android", target_os = "haiku", target_os = "ios", target_os = "linux", target_os = "macos"))] - TAB2 as tcflag_t, + TAB2 as tcflag_t; #[cfg(any(target_os = "android", target_os = "freebsd", target_os = "haiku", target_os = "ios", target_os = "linux", target_os = "macos"))] - TAB3 as tcflag_t, + TAB3 as tcflag_t; #[cfg(any(target_os = "android", target_os = "linux"))] - XTABS, + XTABS; #[cfg(any(target_os = "android", target_os = "haiku", target_os = "ios", target_os = "linux", target_os = "macos"))] - BS0 as tcflag_t, + BS0 as tcflag_t; #[cfg(any(target_os = "android", target_os = "haiku", target_os = "ios", target_os = "linux", target_os = "macos"))] - BS1 as tcflag_t, + BS1 as tcflag_t; #[cfg(any(target_os = "android", target_os = "haiku", target_os = "ios", target_os = "linux", target_os = "macos"))] - VT0 as tcflag_t, + VT0 as tcflag_t; #[cfg(any(target_os = "android", target_os = "haiku", target_os = "ios", target_os = "linux", target_os = "macos"))] - VT1 as tcflag_t, + VT1 as tcflag_t; #[cfg(any(target_os = "android", target_os = "haiku", target_os = "ios", target_os = "linux", target_os = "macos"))] - FF0 as tcflag_t, + FF0 as tcflag_t; #[cfg(any(target_os = "android", target_os = "haiku", target_os = "ios", target_os = "linux", target_os = "macos"))] - FF1 as tcflag_t, + FF1 as tcflag_t; #[cfg(any(target_os = "freebsd", target_os = "dragonfly", target_os = "ios", target_os = "macos", target_os = "netbsd", target_os = "openbsd"))] - OXTABS, + OXTABS; #[cfg(any(target_os = "freebsd", target_os = "dragonfly", target_os = "macos", target_os = "netbsd", target_os = "openbsd"))] - ONOEOT as tcflag_t, + ONOEOT as tcflag_t; // Bitmasks for use with OutputFlags to select specific settings // These should be moved to be a mask once https://github.com/rust-lang-nursery/bitflags/issues/110 @@ -623,140 +623,140 @@ libc_bitflags! { target_os = "ios", target_os = "linux", target_os = "macos"))] - NLDLY as tcflag_t, // FIXME: Datatype needs to be corrected in libc for mac + NLDLY as tcflag_t; // FIXME: Datatype needs to be corrected in libc for mac #[cfg(any(target_os = "android", target_os = "haiku", target_os = "ios", target_os = "linux", target_os = "macos"))] - CRDLY as tcflag_t, + CRDLY as tcflag_t; #[cfg(any(target_os = "android", target_os = "freebsd", target_os = "haiku", target_os = "ios", target_os = "linux", target_os = "macos"))] - TABDLY as tcflag_t, + TABDLY as tcflag_t; #[cfg(any(target_os = "android", target_os = "haiku", target_os = "ios", target_os = "linux", target_os = "macos"))] - BSDLY as tcflag_t, + BSDLY as tcflag_t; #[cfg(any(target_os = "android", target_os = "haiku", target_os = "ios", target_os = "linux", target_os = "macos"))] - VTDLY as tcflag_t, + VTDLY as tcflag_t; #[cfg(any(target_os = "android", target_os = "haiku", target_os = "ios", target_os = "linux", target_os = "macos"))] - FFDLY as tcflag_t, + FFDLY as tcflag_t; } } libc_bitflags! { /// Flags for setting the control mode of a terminal - pub flags ControlFlags: tcflag_t { + pub struct ControlFlags: tcflag_t { #[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "ios", target_os = "macos", target_os = "netbsd", target_os = "openbsd"))] - CIGNORE, - CS5, - CS6, - CS7, - CS8, - CSTOPB, - CREAD, - PARENB, - PARODD, - HUPCL, - CLOCAL, - CRTSCTS, + CIGNORE; + CS5; + CS6; + CS7; + CS8; + CSTOPB; + CREAD; + PARENB; + PARODD; + HUPCL; + CLOCAL; + CRTSCTS; #[cfg(any(target_os = "android", target_os = "linux"))] - CBAUD, + CBAUD; #[cfg(any(target_os = "android", all(target_os = "linux", not(target_arch = "mips"))))] - CMSPAR, + CMSPAR; #[cfg(any(target_os = "android", all(target_os = "linux", not(any(target_arch = "powerpc", target_arch = "powerpc64")))))] - CIBAUD, + CIBAUD; #[cfg(any(target_os = "android", target_os = "linux"))] - CBAUDEX, + CBAUDEX; #[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "macos", target_os = "netbsd", target_os = "openbsd"))] - MDMBUF, + MDMBUF; #[cfg(any(target_os = "netbsd", target_os = "openbsd"))] - CHWFLOW, + CHWFLOW; #[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))] - CCTS_OFLOW, + CCTS_OFLOW; #[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))] - CRTS_IFLOW, + CRTS_IFLOW; #[cfg(any(target_os = "dragonfly", target_os = "freebsd"))] - CDTR_IFLOW, + CDTR_IFLOW; #[cfg(any(target_os = "dragonfly", target_os = "freebsd"))] - CDSR_OFLOW, + CDSR_OFLOW; #[cfg(any(target_os = "dragonfly", target_os = "freebsd"))] - CCAR_OFLOW, + CCAR_OFLOW; // Bitmasks for use with ControlFlags to select specific settings // These should be moved to be a mask once https://github.com/rust-lang-nursery/bitflags/issues/110 // is resolved. - CSIZE, + CSIZE; } } libc_bitflags! { /// Flags for setting any local modes - pub flags LocalFlags: tcflag_t { - ECHOKE, - ECHOE, - ECHOK, - ECHO, - ECHONL, - ECHOPRT, - ECHOCTL, - ISIG, - ICANON, + pub struct LocalFlags: tcflag_t { + ECHOKE; + ECHOE; + ECHOK; + ECHO; + ECHONL; + ECHOPRT; + ECHOCTL; + ISIG; + ICANON; #[cfg(any(target_os = "freebsd", target_os = "dragonfly", target_os = "ios", target_os = "macos", target_os = "netbsd", target_os = "openbsd"))] - ALTWERASE, - IEXTEN, - EXTPROC, - TOSTOP, - FLUSHO, + ALTWERASE; + IEXTEN; + EXTPROC; + TOSTOP; + FLUSHO; #[cfg(any(target_os = "freebsd", target_os = "dragonfly", target_os = "ios", target_os = "macos", target_os = "netbsd", target_os = "openbsd"))] - NOKERNINFO, - PENDIN, - NOFLSH, + NOKERNINFO; + PENDIN; + NOFLSH; } } diff --git a/src/sys/utsname.rs b/src/sys/utsname.rs index 7eeb323b..104249d8 100644 --- a/src/sys/utsname.rs +++ b/src/sys/utsname.rs @@ -1,62 +1,41 @@ use std::mem; -use libc::{c_char}; +use libc::{self, c_char}; use std::ffi::CStr; -use std::str::from_utf8_unchecked; - -mod ffi { - use libc::c_int; - use super::UtsName; - - extern { - pub fn uname(buf: *mut UtsName) -> c_int; - } -} - - -const UTSNAME_LEN: usize = 65; +use std::str::from_utf8_unchecked; #[repr(C)] #[derive(Copy)] -pub struct UtsName { - sysname: [c_char; UTSNAME_LEN], - nodename: [c_char; UTSNAME_LEN], - release: [c_char; UTSNAME_LEN], - version: [c_char; UTSNAME_LEN], - machine: [c_char; UTSNAME_LEN], - // ifdef _GNU_SOURCE - #[allow(dead_code)] - domainname: [c_char; UTSNAME_LEN] -} +pub struct UtsName(libc::utsname); // workaround for `derive(Clone)` not working for fixed-length arrays impl Clone for UtsName { fn clone(&self) -> UtsName { *self } } impl UtsName { pub fn sysname<'a>(&'a self) -> &'a str { - to_str(&(&self.sysname as *const c_char ) as *const *const c_char) + to_str(&(&self.0.sysname as *const c_char ) as *const *const c_char) } pub fn nodename<'a>(&'a self) -> &'a str { - to_str(&(&self.nodename as *const c_char ) as *const *const c_char) + to_str(&(&self.0.nodename as *const c_char ) as *const *const c_char) } pub fn release<'a>(&'a self) -> &'a str { - to_str(&(&self.release as *const c_char ) as *const *const c_char) + to_str(&(&self.0.release as *const c_char ) as *const *const c_char) } pub fn version<'a>(&'a self) -> &'a str { - to_str(&(&self.version as *const c_char ) as *const *const c_char) + to_str(&(&self.0.version as *const c_char ) as *const *const c_char) } pub fn machine<'a>(&'a self) -> &'a str { - to_str(&(&self.machine as *const c_char ) as *const *const c_char) + to_str(&(&self.0.machine as *const c_char ) as *const *const c_char) } } pub fn uname() -> UtsName { unsafe { let mut ret: UtsName = mem::uninitialized(); - ffi::uname(&mut ret as *mut UtsName); + libc::uname(&mut ret.0); ret } } diff --git a/src/sys/wait.rs b/src/sys/wait.rs index b2ca3bd6..2ad530ce 100644 --- a/src/sys/wait.rs +++ b/src/sys/wait.rs @@ -15,24 +15,24 @@ mod ffi { #[cfg(not(any(target_os = "linux", target_os = "android")))] libc_bitflags!( - pub flags WaitPidFlag: c_int { - WNOHANG, - WUNTRACED, + pub struct WaitPidFlag: c_int { + WNOHANG; + WUNTRACED; } ); #[cfg(any(target_os = "linux", target_os = "android"))] libc_bitflags!( - pub flags WaitPidFlag: c_int { - WNOHANG, - WUNTRACED, - WEXITED, - WCONTINUED, - WNOWAIT, // Don't reap, just poll status. - __WNOTHREAD, // Don't wait on children of other threads in this group - __WALL, // Wait on all children, regardless of type - __WCLONE, + pub struct WaitPidFlag: c_int { + WNOHANG; + WUNTRACED; + WEXITED; + WCONTINUED; + WNOWAIT; // Don't reap, just poll status. + __WNOTHREAD; // Don't wait on children of other threads in this group + __WALL; // Wait on all children, regardless of type + __WCLONE; } ); |