summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/fcntl.rs4
-rw-r--r--src/lib.rs4
-rw-r--r--src/mqueue.rs178
-rw-r--r--src/poll.rs92
-rw-r--r--src/sched.rs226
-rw-r--r--src/sys/event.rs2
-rw-r--r--src/sys/eventfd.rs26
-rw-r--r--src/sys/mman.rs2
-rw-r--r--src/sys/mod.rs3
-rw-r--r--src/sys/reboot.rs43
-rw-r--r--src/sys/select.rs1
-rw-r--r--src/sys/signal.rs254
-rw-r--r--src/sys/socket/consts.rs3
-rw-r--r--src/sys/socket/ffi.rs14
-rw-r--r--src/sys/socket/mod.rs32
-rw-r--r--src/sys/socket/sockopt.rs62
-rw-r--r--src/sys/wait.rs61
-rw-r--r--src/ucontext.rs12
-rw-r--r--src/unistd.rs181
19 files changed, 717 insertions, 483 deletions
diff --git a/src/fcntl.rs b/src/fcntl.rs
index 75e12549..1d9ba499 100644
--- a/src/fcntl.rs
+++ b/src/fcntl.rs
@@ -46,6 +46,8 @@ pub enum FcntlArg<'a> {
F_ADD_SEALS(SealFlag),
#[cfg(target_os = "linux")]
F_GET_SEALS,
+ #[cfg(any(target_os = "macos", target_os = "ios"))]
+ F_FULLFSYNC,
// TODO: Rest of flags
}
@@ -69,6 +71,8 @@ pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result<c_int> {
F_ADD_SEALS(flag) => libc::fcntl(fd, ffi::F_ADD_SEALS, flag.bits()),
#[cfg(target_os = "linux")]
F_GET_SEALS => libc::fcntl(fd, ffi::F_GET_SEALS),
+ #[cfg(any(target_os = "macos", target_os = "ios"))]
+ F_FULLFSYNC => libc::fcntl(fd, libc::F_FULLFSYNC),
#[cfg(any(target_os = "linux", target_os = "android"))]
_ => unimplemented!()
}
diff --git a/src/lib.rs b/src/lib.rs
index e746c6b2..8dbf9fe0 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -8,7 +8,7 @@
// latest bitflags triggers a rustc bug with cross-crate macro expansions causing dead_code
// warnings even though the macro expands into something with allow(dead_code)
#![allow(dead_code)]
-#![deny(warnings)]
+#![cfg_attr(test, deny(warnings))]
#[macro_use]
extern crate bitflags;
@@ -40,7 +40,7 @@ pub mod fcntl;
#[cfg(any(target_os = "linux", target_os = "android"))]
pub mod mount;
-#[cfg(any(target_os = "linux"))]
+#[cfg(target_os = "linux")]
pub mod mqueue;
#[cfg(any(target_os = "linux", target_os = "macos"))]
diff --git a/src/mqueue.rs b/src/mqueue.rs
index b8a2250e..9bf6e77e 100644
--- a/src/mqueue.rs
+++ b/src/mqueue.rs
@@ -4,114 +4,117 @@
use {Errno, Result};
-use libc::{c_int, c_long, c_char, size_t, mode_t};
+use libc::{self, c_char, c_long, mode_t, mqd_t, size_t};
use std::ffi::CString;
use sys::stat::Mode;
-use std::ptr;
-
-pub use self::consts::*;
-
-pub type MQd = c_int;
-
-#[cfg(target_os = "linux")]
-mod consts {
- use libc::c_int;
-
- bitflags!(
- flags MQ_OFlag: c_int {
- const O_RDONLY = 0o00000000,
- const O_WRONLY = 0o00000001,
- const O_RDWR = 0o00000002,
- const O_CREAT = 0o00000100,
- const O_EXCL = 0o00000200,
- const O_NONBLOCK = 0o00004000,
- const O_CLOEXEC = 0o02000000,
- }
- );
-
- bitflags!(
- flags FdFlag: c_int {
- const FD_CLOEXEC = 1
- }
- );
+use std::mem;
+
+libc_bitflags!{
+ flags MQ_OFlag: libc::c_int {
+ O_RDONLY,
+ O_WRONLY,
+ O_RDWR,
+ O_CREAT,
+ O_EXCL,
+ O_NONBLOCK,
+ O_CLOEXEC,
+ }
}
-mod ffi {
- use libc::{c_char, size_t, ssize_t, c_uint, c_int};
- use super::MQd;
- use super::MqAttr;
-
- #[allow(improper_ctypes)]
- extern "C" {
- pub fn mq_open(name: *const c_char, oflag: c_int, ...) -> MQd;
-
- pub fn mq_close (mqd: MQd) -> c_int;
-
- pub fn mq_unlink(name: *const c_char) -> c_int;
-
- pub fn mq_receive (mqd: MQd, msg_ptr: *const c_char, msg_len: size_t, msq_prio: *const c_uint) -> ssize_t;
-
- pub fn mq_send (mqd: MQd, msg_ptr: *const c_char, msg_len: size_t, msq_prio: c_uint) -> c_int;
-
- pub fn mq_getattr(mqd: MQd, attr: *mut MqAttr) -> c_int;
-
- pub fn mq_setattr(mqd: MQd, newattr: *const MqAttr, oldattr: *mut MqAttr) -> c_int;
+libc_bitflags!{
+ flags FdFlag: libc::c_int {
+ FD_CLOEXEC,
}
}
#[repr(C)]
-#[derive(Clone, Copy, Debug, Eq, PartialEq)]
+#[derive(Clone, Copy)]
pub struct MqAttr {
- pub mq_flags: c_long,
- pub mq_maxmsg: c_long,
- pub mq_msgsize: c_long,
- pub mq_curmsgs: c_long,
- pad: [c_long; 4]
+ mq_attr: libc::mq_attr,
}
-impl MqAttr {
- pub fn new(mq_flags: c_long, mq_maxmsg: c_long, mq_msgsize: c_long, mq_curmsgs: c_long) -> MqAttr {
- MqAttr { mq_flags: mq_flags, mq_maxmsg: mq_maxmsg, mq_msgsize: mq_msgsize, mq_curmsgs: mq_curmsgs, pad: [0; 4] }
- }
+impl PartialEq<MqAttr> for MqAttr {
+ fn eq(&self, other: &MqAttr) -> bool {
+ let self_attr = self.mq_attr;
+ let other_attr = other.mq_attr;
+ self_attr.mq_flags == other_attr.mq_flags && self_attr.mq_maxmsg == other_attr.mq_maxmsg &&
+ self_attr.mq_msgsize == other_attr.mq_msgsize &&
+ self_attr.mq_curmsgs == other_attr.mq_curmsgs
+ }
}
+impl MqAttr {
+ pub fn new(mq_flags: c_long,
+ mq_maxmsg: c_long,
+ mq_msgsize: c_long,
+ mq_curmsgs: c_long)
+ -> MqAttr {
+ let mut attr = unsafe { mem::uninitialized::<libc::mq_attr>() };
+ attr.mq_flags = mq_flags;
+ attr.mq_maxmsg = mq_maxmsg;
+ attr.mq_msgsize = mq_msgsize;
+ attr.mq_curmsgs = mq_curmsgs;
+ MqAttr { mq_attr: attr }
+ }
-pub fn mq_open(name: &CString, oflag: MQ_OFlag, mode: Mode, attr: Option<&MqAttr>) -> Result<MQd> {
- let attr_p = attr.map(|attr| attr as *const MqAttr).unwrap_or(ptr::null());
- let res = unsafe { ffi::mq_open(name.as_ptr(), oflag.bits(), mode.bits() as mode_t, attr_p) };
+ pub fn flags(&self) -> c_long {
+ self.mq_attr.mq_flags
+ }
+}
+
+pub fn mq_open(name: &CString,
+ oflag: MQ_OFlag,
+ mode: Mode,
+ attr: Option<&MqAttr>)
+ -> Result<mqd_t> {
+ let res = match attr {
+ Some(mq_attr) => unsafe {
+ libc::mq_open(name.as_ptr(),
+ oflag.bits(),
+ mode.bits() as mode_t,
+ &mq_attr.mq_attr as *const libc::mq_attr)
+ },
+ None => unsafe { libc::mq_open(name.as_ptr(), oflag.bits()) },
+ };
Errno::result(res)
}
pub fn mq_unlink(name: &CString) -> Result<()> {
- let res = unsafe { ffi::mq_unlink(name.as_ptr()) };
+ let res = unsafe { libc::mq_unlink(name.as_ptr()) };
Errno::result(res).map(drop)
}
-pub fn mq_close(mqdes: MQd) -> Result<()> {
- let res = unsafe { ffi::mq_close(mqdes) };
+pub fn mq_close(mqdes: mqd_t) -> Result<()> {
+ let res = unsafe { libc::mq_close(mqdes) };
Errno::result(res).map(drop)
}
-
-pub fn mq_receive(mqdes: MQd, message: &mut [u8], msq_prio: u32) -> Result<usize> {
+pub fn mq_receive(mqdes: mqd_t, message: &mut [u8], msg_prio: &mut u32) -> Result<usize> {
let len = message.len() as size_t;
- let res = unsafe { ffi::mq_receive(mqdes, message.as_mut_ptr() as *mut c_char, len, &msq_prio) };
-
+ let res = unsafe {
+ libc::mq_receive(mqdes,
+ message.as_mut_ptr() as *mut c_char,
+ len,
+ msg_prio as *mut u32)
+ };
Errno::result(res).map(|r| r as usize)
}
-pub fn mq_send(mqdes: MQd, message: &[u8], msq_prio: u32) -> Result<()> {
- let res = unsafe { ffi::mq_send(mqdes, message.as_ptr() as *const c_char, message.len(), msq_prio) };
-
+pub fn mq_send(mqdes: mqd_t, message: &[u8], msq_prio: u32) -> Result<()> {
+ let res = unsafe {
+ libc::mq_send(mqdes,
+ message.as_ptr() as *const c_char,
+ message.len(),
+ msq_prio)
+ };
Errno::result(res).map(drop)
}
-pub fn mq_getattr(mqd: MQd) -> Result<MqAttr> {
- let mut attr = MqAttr::new(0, 0, 0, 0);
- let res = unsafe { ffi::mq_getattr(mqd, &mut attr) };
- try!(Errno::result(res));
- Ok(attr)
+pub fn mq_getattr(mqd: mqd_t) -> Result<MqAttr> {
+ let mut attr = unsafe { mem::uninitialized::<libc::mq_attr>() };
+ let res = unsafe { libc::mq_getattr(mqd, &mut attr) };
+ Errno::result(res).map(|_| MqAttr { mq_attr: attr })
}
/// Set the attributes of the message queue. Only `O_NONBLOCK` can be set, everything else will be ignored
@@ -119,27 +122,32 @@ pub fn mq_getattr(mqd: MQd) -> Result<MqAttr> {
/// It is recommend to use the `mq_set_nonblock()` and `mq_remove_nonblock()` convenience functions as they are easier to use
///
/// [Further reading](http://man7.org/linux/man-pages/man3/mq_setattr.3.html)
-pub fn mq_setattr(mqd: MQd, newattr: &MqAttr) -> Result<MqAttr> {
- let mut attr = MqAttr::new(0, 0, 0, 0);
- let res = unsafe { ffi::mq_setattr(mqd, newattr as *const MqAttr, &mut attr) };
- try!(Errno::result(res));
- Ok(attr)
+pub fn mq_setattr(mqd: mqd_t, newattr: &MqAttr) -> Result<MqAttr> {
+ let mut attr = unsafe { mem::uninitialized::<libc::mq_attr>() };
+ let res = unsafe { libc::mq_setattr(mqd, &newattr.mq_attr as *const libc::mq_attr, &mut attr) };
+ Errno::result(res).map(|_| MqAttr { mq_attr: attr })
}
/// Convenience function.
/// Sets the `O_NONBLOCK` attribute for a given message queue descriptor
/// Returns the old attributes
-pub fn mq_set_nonblock(mqd: MQd) -> Result<(MqAttr)> {
+pub fn mq_set_nonblock(mqd: mqd_t) -> Result<(MqAttr)> {
let oldattr = try!(mq_getattr(mqd));
- let newattr = MqAttr::new(O_NONBLOCK.bits() as c_long, oldattr.mq_maxmsg, oldattr.mq_msgsize, oldattr.mq_curmsgs);
+ let newattr = MqAttr::new(O_NONBLOCK.bits() as c_long,
+ oldattr.mq_attr.mq_maxmsg,
+ oldattr.mq_attr.mq_msgsize,
+ oldattr.mq_attr.mq_curmsgs);
mq_setattr(mqd, &newattr)
}
/// Convenience function.
/// Removes `O_NONBLOCK` attribute for a given message queue descriptor
/// Returns the old attributes
-pub fn mq_remove_nonblock(mqd: MQd) -> Result<(MqAttr)> {
+pub fn mq_remove_nonblock(mqd: mqd_t) -> Result<(MqAttr)> {
let oldattr = try!(mq_getattr(mqd));
- let newattr = MqAttr::new(0, oldattr.mq_maxmsg, oldattr.mq_msgsize, oldattr.mq_curmsgs);
+ let newattr = MqAttr::new(0,
+ oldattr.mq_attr.mq_maxmsg,
+ oldattr.mq_attr.mq_msgsize,
+ oldattr.mq_attr.mq_curmsgs);
mq_setattr(mqd, &newattr)
}
diff --git a/src/poll.rs b/src/poll.rs
index 88ca9825..6ba9f5e4 100644
--- a/src/poll.rs
+++ b/src/poll.rs
@@ -1,74 +1,48 @@
-use libc::c_int;
+use libc;
use {Errno, Result};
-pub use self::ffi::PollFd;
-pub use self::ffi::consts::*;
-
-mod ffi {
- use libc::c_int;
- pub use self::consts::*;
-
- #[derive(Clone, Copy, Debug)]
- #[repr(C)]
- pub struct PollFd {
- pub fd: c_int,
- pub events: EventFlags,
- pub revents: EventFlags
- }
-
- #[cfg(target_os = "linux")]
- pub mod consts {
- use libc::{c_short, c_ulong};
+#[repr(C)]
+#[derive(Clone, Copy)]
+pub struct PollFd {
+ pollfd: libc::pollfd,
+}
- bitflags! {
- flags EventFlags: c_short {
- const POLLIN = 0x001,
- const POLLPRI = 0x002,
- const POLLOUT = 0x004,
- const POLLRDNORM = 0x040,
- const POLLWRNORM = 0x100,
- const POLLRDBAND = 0x080,
- const POLLWRBAND = 0x200,
- const POLLERR = 0x008,
- const POLLHUP = 0x010,
- const POLLNVAL = 0x020,
- }
+impl PollFd {
+ pub fn new(fd: libc::c_int, events: EventFlags, revents: EventFlags) -> PollFd {
+ PollFd {
+ pollfd: libc::pollfd {
+ fd: fd,
+ events: events.bits(),
+ revents: revents.bits(),
+ },
}
-
- pub type nfds_t = c_ulong;
}
- #[cfg(target_os = "macos")]
- pub mod consts {
- use libc::{c_short, c_uint};
-
- bitflags! {
- flags EventFlags: c_short {
- const POLLIN = 0x0001,
- const POLLPRI = 0x0002,
- const POLLOUT = 0x0004,
- const POLLRDNORM = 0x0040,
- const POLLWRNORM = 0x0004,
- const POLLRDBAND = 0x0080,
- const POLLWRBAND = 0x0100,
- const POLLERR = 0x0008,
- const POLLHUP = 0x0010,
- const POLLNVAL = 0x0020,
- }
- }
-
- pub type nfds_t = c_uint;
+ pub fn revents(&self) -> Option<EventFlags> {
+ EventFlags::from_bits(self.pollfd.revents)
}
+}
- #[allow(improper_ctypes)]
- extern {
- pub fn poll(fds: *mut PollFd, nfds: nfds_t, timeout: c_int) -> c_int;
+libc_bitflags! {
+ flags EventFlags: libc::c_short {
+ POLLIN,
+ POLLPRI,
+ POLLOUT,
+ POLLRDNORM,
+ POLLWRNORM,
+ POLLRDBAND,
+ POLLWRBAND,
+ POLLERR,
+ POLLHUP,
+ POLLNVAL,
}
}
-pub fn poll(fds: &mut [PollFd], timeout: c_int) -> Result<c_int> {
+pub fn poll(fds: &mut [PollFd], timeout: libc::c_int) -> Result<libc::c_int> {
let res = unsafe {
- ffi::poll(fds.as_mut_ptr(), fds.len() as ffi::nfds_t, timeout)
+ libc::poll(fds.as_mut_ptr() as *mut libc::pollfd,
+ fds.len() as libc::nfds_t,
+ timeout)
};
Errno::result(res)
diff --git a/src/sched.rs b/src/sched.rs
index 934ce13f..91a7c42a 100644
--- a/src/sched.rs
+++ b/src/sched.rs
@@ -1,204 +1,110 @@
use std::mem;
use std::os::unix::io::RawFd;
use std::option::Option;
-use libc::{self, c_int, c_void, c_ulong, pid_t};
-use {Errno, Result};
+use libc::{self, c_int, c_void, pid_t};
+use {Errno, Error, Result};
// For some functions taking with a parameter of type CloneFlags,
// only a subset of these flags have an effect.
-bitflags!{
- flags CloneFlags: c_int {
- const CLONE_VM = libc::CLONE_VM,
- const CLONE_FS = libc::CLONE_FS,
- const CLONE_FILES = libc::CLONE_FILES,
- const CLONE_SIGHAND = libc::CLONE_SIGHAND,
- const CLONE_PTRACE = libc::CLONE_PTRACE,
- const CLONE_VFORK = libc::CLONE_VFORK,
- const CLONE_PARENT = libc::CLONE_PARENT,
- const CLONE_THREAD = libc::CLONE_THREAD,
- const CLONE_NEWNS = libc::CLONE_NEWNS,
- const CLONE_SYSVSEM = libc::CLONE_SYSVSEM,
- const CLONE_SETTLS = libc::CLONE_SETTLS,
- const CLONE_PARENT_SETTID = libc::CLONE_PARENT_SETTID,
- const CLONE_CHILD_CLEARTID = libc::CLONE_CHILD_CLEARTID,
- const CLONE_DETACHED = libc::CLONE_DETACHED,
- const CLONE_UNTRACED = libc::CLONE_UNTRACED,
- const CLONE_CHILD_SETTID = libc::CLONE_CHILD_SETTID,
- // TODO: Once, we use a version containing
- // https://github.com/rust-lang-nursery/libc/pull/147
- // get rid of the casts.
- const CLONE_NEWUTS = libc::CLONE_NEWUTS as c_int,
- const CLONE_NEWIPC = libc::CLONE_NEWIPC as c_int,
- const CLONE_NEWUSER = libc::CLONE_NEWUSER as c_int,
- const CLONE_NEWPID = libc::CLONE_NEWPID as c_int,
- const CLONE_NEWNET = libc::CLONE_NEWNET as c_int,
- const CLONE_IO = libc::CLONE_IO as c_int,
- }
-}
-
-// Support a maximum CPU set of 1024 nodes
-#[cfg(all(target_arch = "x86_64", target_os = "linux"))]
-mod cpuset_attribs {
- use super::CpuMask;
- pub const CPU_SETSIZE: usize = 1024;
- pub const CPU_MASK_BITS: usize = 64;
-
- #[inline]
- pub fn set_cpu_mask_flag(cur: CpuMask, bit: usize) -> CpuMask {
- cur | (1u64 << bit)
- }
-
- #[inline]
- pub fn clear_cpu_mask_flag(cur: CpuMask, bit: usize) -> CpuMask {
- cur & !(1u64 << bit)
- }
-}
-
-#[cfg(all(target_arch = "x86", target_os = "linux"))]
-mod cpuset_attribs {
- use super::CpuMask;
- pub const CPU_SETSIZE: usize = 1024;
- pub const CPU_MASK_BITS: usize = 32;
-
- #[inline]
- pub fn set_cpu_mask_flag(cur: CpuMask, bit: usize) -> CpuMask {
- cur | (1u32 << bit)
- }
-
- #[inline]
- pub fn clear_cpu_mask_flag(cur: CpuMask, bit: usize) -> CpuMask {
- cur & !(1u32 << bit)
- }
-}
-
-#[cfg(all(target_arch = "aarch64", any(target_os = "linux", target_os = "android")))]
-mod cpuset_attribs {
- use super::CpuMask;
- pub const CPU_SETSIZE: usize = 1024;
- pub const CPU_MASK_BITS: usize = 64;
-
- #[inline]
- pub fn set_cpu_mask_flag(cur: CpuMask, bit: usize) -> CpuMask {
- cur | (1u64 << bit)
- }
-
- #[inline]
- pub fn clear_cpu_mask_flag(cur: CpuMask, bit: usize) -> CpuMask {
- cur & !(1u64 << bit)
- }
-}
-
-#[cfg(all(any(target_arch = "arm", target_arch = "mips"), target_os = "android"))]
-mod cpuset_attribs {
- use super::CpuMask;
- // bionic only supports up to 32 independent CPUs, instead of 1024.
- pub const CPU_SETSIZE: usize = 32;
- pub const CPU_MASK_BITS: usize = 32;
-
- #[inline]
- pub fn set_cpu_mask_flag(cur: CpuMask, bit: usize) -> CpuMask {
- cur | (1u32 << bit)
- }
-
- #[inline]
- pub fn clear_cpu_mask_flag(cur: CpuMask, bit: usize) -> CpuMask {
- cur & !(1u32 << bit)
- }
-}
-
-#[cfg(all(any(target_arch = "arm", target_arch = "mips"), target_os = "linux"))]
-mod cpuset_attribs {
- use super::CpuMask;
- pub const CPU_SETSIZE: usize = 1024;
- pub const CPU_MASK_BITS: usize = 32;
-
- #[inline]
- pub fn set_cpu_mask_flag(cur: CpuMask, bit: usize) -> CpuMask {
- cur | (1u32 << bit)
- }
-
- #[inline]
- pub fn clear_cpu_mask_flag(cur: CpuMask, bit: usize) -> CpuMask {
- cur & !(1u32 << bit)
+libc_bitflags!{
+ flags CloneFlags: libc::c_int {
+ CLONE_VM,
+ CLONE_FS,
+ CLONE_FILES,
+ CLONE_SIGHAND,
+ CLONE_PTRACE,
+ CLONE_VFORK,
+ CLONE_PARENT,
+ CLONE_THREAD,
+ CLONE_NEWNS,
+ CLONE_SYSVSEM,
+ CLONE_SETTLS,
+ CLONE_PARENT_SETTID,
+ CLONE_CHILD_CLEARTID,
+ CLONE_DETACHED,
+ CLONE_UNTRACED,
+ CLONE_CHILD_SETTID,
+ CLONE_NEWUTS,
+ CLONE_NEWIPC,
+ CLONE_NEWUSER,
+ CLONE_NEWPID,
+ CLONE_NEWNET,
+ CLONE_IO,
}
}
pub type CloneCb<'a> = Box<FnMut() -> isize + 'a>;
-// A single CPU mask word
-pub type CpuMask = c_ulong;
-
-// Structure representing the CPU set to apply
#[repr(C)]
#[derive(Clone, Copy)]
pub struct CpuSet {
- cpu_mask: [CpuMask; cpuset_attribs::CPU_SETSIZE/cpuset_attribs::CPU_MASK_BITS]
+ cpu_set: libc::cpu_set_t,
}
impl CpuSet {
pub fn new() -> CpuSet {
- CpuSet {
- cpu_mask: unsafe { mem::zeroed() }
- }
+ CpuSet { cpu_set: unsafe { mem::zeroed() } }
}
- pub fn set(&mut self, field: usize) {
- let word = field / cpuset_attribs::CPU_MASK_BITS;
- let bit = field % cpuset_attribs::CPU_MASK_BITS;
-
- self.cpu_mask[word] = cpuset_attribs::set_cpu_mask_flag(self.cpu_mask[word], bit);
+ pub fn is_set(&self, field: usize) -> Result<bool> {
+ if field >= 8 * mem::size_of::<libc::cpu_set_t>() {
+ Err(Error::Sys(Errno::EINVAL))
+ } else {
+ Ok(unsafe { libc::CPU_ISSET(field, &self.cpu_set) })
+ }
}
- pub fn unset(&mut self, field: usize) {
- let word = field / cpuset_attribs::CPU_MASK_BITS;
- let bit = field % cpuset_attribs::CPU_MASK_BITS;
+ pub fn set(&mut self, field: usize) -> Result<()> {
+ if field >= 8 * mem::size_of::<libc::cpu_set_t>() {
+ Err(Error::Sys(Errno::EINVAL))
+ } else {
+ Ok(unsafe { libc::CPU_SET(field, &mut self.cpu_set) })
+ }
+ }
- self.cpu_mask[word] = cpuset_attribs::clear_cpu_mask_flag(self.cpu_mask[word], bit);
+ pub fn unset(&mut self, field: usize) -> Result<()> {
+ if field >= 8 * mem::size_of::<libc::cpu_set_t>() {
+ Err(Error::Sys(Errno::EINVAL))
+ } else {
+ Ok(unsafe { libc::CPU_CLR(field, &mut self.cpu_set) })
+ }
}
}
mod ffi {
- use libc::{c_void, c_int, pid_t, size_t};
- use super::CpuSet;
+ use libc::{c_void, c_int};
- pub type CloneCb = extern "C" fn (data: *const super::CloneCb) -> c_int;
+ pub type CloneCb = extern "C" fn(data: *const super::CloneCb) -> c_int;
// We cannot give a proper #[repr(C)] to super::CloneCb
#[allow(improper_ctypes)]
- extern {
+ extern "C" {
// create a child process
// doc: http://man7.org/linux/man-pages/man2/clone.2.html
- pub fn clone(
- cb: *const CloneCb,
- child_stack: *mut c_void,
- flags: c_int,
- arg: *mut super::CloneCb,
- ...) -> c_int;
-
- // disassociate parts of the process execution context
- // doc: http://man7.org/linux/man-pages/man2/unshare.2.html
- pub fn unshare(flags: c_int) -> c_int;
-
- // reassociate thread with a namespace
- // doc: http://man7.org/linux/man-pages/man2/setns.2.html
- pub fn setns(fd: c_int, nstype: c_int) -> c_int;
-
- // Set the current CPU set that a task is allowed to run on
- pub fn sched_setaffinity(__pid: pid_t, __cpusetsize: size_t, __cpuset: *const CpuSet) -> c_int;
+ pub fn clone(cb: *const CloneCb,
+ child_stack: *mut c_void,
+ flags: c_int,
+ arg: *mut super::CloneCb,
+ ...)
+ -> c_int;
}
}
pub fn sched_setaffinity(pid: isize, cpuset: &CpuSet) -> Result<()> {
- use libc::{pid_t, size_t};
-
let res = unsafe {
- ffi::sched_setaffinity(pid as pid_t, mem::size_of::<CpuSet>() as size_t, mem::transmute(cpuset))
+ libc::sched_setaffinity(pid as libc::pid_t,
+ mem::size_of::<CpuSet>() as libc::size_t,
+ mem::transmute(cpuset))
};
Errno::result(res).map(drop)
}
-pub fn clone(mut cb: CloneCb, stack: &mut [u8], flags: CloneFlags, signal: Option<c_int>) -> Result<pid_t> {
+pub fn clone(mut cb: CloneCb,
+ stack: &mut [u8],
+ flags: CloneFlags,
+ signal: Option<c_int>)
+ -> Result<pid_t> {
extern "C" fn callback(data: *mut CloneCb) -> c_int {
let cb: &mut CloneCb = unsafe { &mut *data };
(*cb)() as c_int
@@ -217,13 +123,13 @@ pub fn clone(mut cb: CloneCb, stack: &mut [u8], flags: CloneFlags, signal: Optio
}
pub fn unshare(flags: CloneFlags) -> Result<()> {
- let res = unsafe { ffi::unshare(flags.bits()) };
+ let res = unsafe { libc::unshare(flags.bits()) };
Errno::result(res).map(drop)
}
pub fn setns(fd: RawFd, nstype: CloneFlags) -> Result<()> {
- let res = unsafe { ffi::setns(fd, nstype.bits()) };
+ let res = unsafe { libc::setns(fd, nstype.bits()) };
Errno::result(res).map(drop)
}
diff --git a/src/sys/event.rs b/src/sys/event.rs
index 8b112689..0e94475e 100644
--- a/src/sys/event.rs
+++ b/src/sys/event.rs
@@ -355,7 +355,7 @@ pub fn ev_set(ev: &mut KEvent,
filter: EventFilter,
flags: EventFlag,
fflags: FilterFlag,
- udata: i64) {
+ udata: isize) {
ev.ident = ident as uintptr_t;
ev.filter = filter;
diff --git a/src/sys/eventfd.rs b/src/sys/eventfd.rs
index cd740341..e6e410ec 100644
--- a/src/sys/eventfd.rs
+++ b/src/sys/eventfd.rs
@@ -2,26 +2,16 @@ use libc;
use std::os::unix::io::RawFd;
use {Errno, Result};
-bitflags!(
- flags EventFdFlag: libc::c_int {
- const EFD_CLOEXEC = 0o2000000, // Since Linux 2.6.27
- const EFD_NONBLOCK = 0o0004000, // Since Linux 2.6.27
- const EFD_SEMAPHORE = 0o0000001, // Since Linux 2.6.30
- }
-);
-
-mod ffi {
- use libc;
-
- extern {
- pub fn eventfd(initval: libc::c_uint, flags: libc::c_int) -> libc::c_int;
+libc_bitflags! {
+ flags EfdFlags: libc::c_int {
+ const EFD_CLOEXEC, // Since Linux 2.6.27
+ const EFD_NONBLOCK, // Since Linux 2.6.27
+ const EFD_SEMAPHORE, // Since Linux 2.6.30
}
}
-pub fn eventfd(initval: usize, flags: EventFdFlag) -> Result<RawFd> {
- unsafe {
- let res = ffi::eventfd(initval as libc::c_uint, flags.bits());
+pub fn eventfd(initval: libc::c_uint, flags: EfdFlags) -> Result<RawFd> {
+ let res = unsafe { libc::eventfd(initval, flags.bits()) };
- Errno::result(res).map(|r| r as RawFd)
- }
+ Errno::result(res).map(|r| r as RawFd)
}
diff --git a/src/sys/mman.rs b/src/sys/mman.rs
index 5bc1c82d..a1bf6134 100644
--- a/src/sys/mman.rs
+++ b/src/sys/mman.rs
@@ -131,7 +131,7 @@ mod consts {
const MAP_RENAME = libc::MAP_RENAME,
const MAP_NORESERVE = libc::MAP_NORESERVE,
const MAP_HASSEMAPHORE = libc::MAP_HASSEMAPHORE,
- #[cfg(not(target_os = "openbsd"))]
+ #[cfg(not(any(target_os = "openbsd", target_os = "netbsd")))]
const MAP_STACK = libc::MAP_STACK,
#[cfg(target_os = "netbsd")]
const MAP_WIRED = libc::MAP_WIRED,
diff --git a/src/sys/mod.rs b/src/sys/mod.rs
index 82934164..793bc70e 100644
--- a/src/sys/mod.rs
+++ b/src/sys/mod.rs
@@ -31,6 +31,9 @@ pub mod stat;
#[cfg(any(target_os = "linux", target_os = "android"))]
pub mod syscall;
+#[cfg(any(target_os = "linux"))]
+pub mod reboot;
+
#[cfg(not(target_os = "ios"))]
pub mod termios;
diff --git a/src/sys/reboot.rs b/src/sys/reboot.rs
new file mode 100644
index 00000000..94f30f62
--- /dev/null
+++ b/src/sys/reboot.rs
@@ -0,0 +1,43 @@
+//! Reboot/shutdown or enable/disable Ctrl-Alt-Delete.
+
+use {Errno, Error, Result};
+use libc;
+use void::Void;
+use std::mem::drop;
+
+/// How exactly should the system be rebooted.
+///
+/// See [`set_cad_enabled()`](fn.set_cad_enabled.html) for
+/// enabling/disabling Ctrl-Alt-Delete.
+#[repr(i32)]
+#[derive(Clone, Copy, Debug, Eq, PartialEq)]
+pub enum RebootMode {
+ RB_HALT_SYSTEM = libc::RB_HALT_SYSTEM,
+ RB_KEXEC = libc::RB_KEXEC,
+ RB_POWER_OFF = libc::RB_POWER_OFF,
+ RB_AUTOBOOT = libc::RB_AUTOBOOT,
+ // we do not support Restart2,
+ RB_SW_SUSPEND = libc::RB_SW_SUSPEND,
+}
+
+pub fn reboot(how: RebootMode) -> Result<Void> {
+ unsafe {
+ libc::reboot(how as libc::c_int)
+ };
+ Err(Error::Sys(Errno::last()))
+}
+
+/// Enable or disable the reboot keystroke (Ctrl-Alt-Delete).
+///
+/// Corresponds to calling `reboot(RB_ENABLE_CAD)` or `reboot(RB_DISABLE_CAD)` in C.
+pub fn set_cad_enabled(enable: bool) -> Result<()> {
+ let cmd = if enable {
+ libc::RB_ENABLE_CAD
+ } else {
+ libc::RB_DISABLE_CAD
+ };
+ let res = unsafe {
+ libc::reboot(cmd)
+ };
+ Errno::result(res).map(drop)
+}
diff --git a/src/sys/select.rs b/src/sys/select.rs
index 1b47d759..28b664aa 100644
--- a/src/sys/select.rs
+++ b/src/sys/select.rs
@@ -8,6 +8,7 @@ pub const FD_SETSIZE: RawFd = 1024;
#[cfg(any(target_os = "macos", target_os = "ios"))]
#[repr(C)]
+#[derive(Clone)]
pub struct FdSet {
bits: [i32; FD_SETSIZE as usize / 32]
}
diff --git a/src/sys/signal.rs b/src/sys/signal.rs
index 753c1562..18827332 100644
--- a/src/sys/signal.rs
+++ b/src/sys/signal.rs
@@ -2,48 +2,166 @@
// See http://rust-lang.org/COPYRIGHT.
use libc;
-use {Errno, Result};
+use {Errno, Error, Result};
use std::mem;
use std::ptr;
-pub use libc::{
+// Currently there is only one definition of c_int in libc, as well as only one
+// type for signal constants.
+// We would prefer to use the libc::c_int alias in the repr attribute. Unfortunately
+// this is not (yet) possible.
+#[derive(Clone, Copy, Debug, Eq, PartialEq)]
+#[repr(i32)]
+pub enum Signal {
+ SIGHUP = libc::SIGHUP,
+ SIGINT = libc::SIGINT,
+ SIGQUIT = libc::SIGQUIT,
+ SIGILL = libc::SIGILL,
+ SIGTRAP = libc::SIGTRAP,
+ SIGABRT = libc::SIGABRT,
+ SIGBUS = libc::SIGBUS,
+ SIGFPE = libc::SIGFPE,
+ SIGKILL = libc::SIGKILL,
+ SIGUSR1 = libc::SIGUSR1,
+ SIGSEGV = libc::SIGSEGV,
+ SIGUSR2 = libc::SIGUSR2,
+ SIGPIPE = libc::SIGPIPE,
+ SIGALRM = libc::SIGALRM,
+ SIGTERM = libc::SIGTERM,
+ #[cfg(any(target_os = "linux", target_os = "android", target_os = "emscripten"))]
+ SIGSTKFLT = libc::SIGSTKFLT,
+ SIGCHLD = libc::SIGCHLD,
+ SIGCONT = libc::SIGCONT,
+ SIGSTOP = libc::SIGSTOP,
+ SIGTSTP = libc::SIGTSTP,
+ SIGTTIN = libc::SIGTTIN,
+ SIGTTOU = libc::SIGTTOU,
+ SIGURG = libc::SIGURG,
+ SIGXCPU = libc::SIGXCPU,
+ SIGXFSZ = libc::SIGXFSZ,
+ SIGVTALRM = libc::SIGVTALRM,
+ SIGPROF = libc::SIGPROF,
+ SIGWINCH = libc::SIGWINCH,
+ SIGIO = libc::SIGIO,
+ #[cfg(any(target_os = "linux", target_os = "android", target_os = "emscripten"))]
+ SIGPWR = libc::SIGPWR,
+ SIGSYS = libc::SIGSYS,
+ #[cfg(not(any(target_os = "linux", target_os = "android", target_os = "emscripten")))]
+ SIGEMT = libc::SIGEMT,
+ #[cfg(not(any(target_os = "linux", target_os = "android", target_os = "emscripten")))]
+ SIGINFO = libc::SIGINFO,
+}
+
+pub use self::Signal::*;
+
+#[cfg(any(target_os = "linux", target_os = "android", target_os = "emscripten"))]
+const SIGNALS: [Signal; 31] = [
SIGHUP,
SIGINT,
SIGQUIT,
SIGILL,
+ SIGTRAP,
SIGABRT,
+ SIGBUS,
SIGFPE,
SIGKILL,
+ SIGUSR1,
SIGSEGV,
+ SIGUSR2,
SIGPIPE,
SIGALRM,
SIGTERM,
- SIGTRAP,
- SIGIOT,
- SIGBUS,
- SIGSYS,
- SIGURG,
+ SIGSTKFLT,
+ SIGCHLD,
+ SIGCONT,
SIGSTOP,
SIGTSTP,
- SIGCONT,
- SIGCHLD,
SIGTTIN,
SIGTTOU,
- SIGIO,
+ SIGURG,
SIGXCPU,
SIGXFSZ,
SIGVTALRM,
SIGPROF,
SIGWINCH,
+ SIGIO,
+ SIGPWR,
+ SIGSYS];
+#[cfg(not(any(target_os = "linux", target_os = "android", target_os = "emscripten")))]
+const SIGNALS: [Signal; 31] = [
+ SIGHUP,
+ SIGINT,
+ SIGQUIT,
+ SIGILL,
+ SIGTRAP,
+ SIGABRT,
+ SIGBUS,
+ SIGFPE,
+ SIGKILL,
SIGUSR1,
+ SIGSEGV,
SIGUSR2,
-};
-
-// This doesn't always exist, but when it does, it's 7
-pub const SIGEMT: libc::c_int = 7;
+ SIGPIPE,
+ SIGALRM,
+ SIGTERM,
+ SIGCHLD,
+ SIGCONT,
+ SIGSTOP,
+ SIGTSTP,
+ SIGTTIN,
+ SIGTTOU,
+ SIGURG,
+ SIGXCPU,
+ SIGXFSZ,
+ SIGVTALRM,
+ SIGPROF,
+ SIGWINCH,
+ SIGIO,
+ SIGSYS,
+ SIGEMT,
+ SIGINFO];
pub const NSIG: libc::c_int = 32;
+pub struct SignalIterator {
+ next: usize,
+}
+
+impl Iterator for SignalIterator {
+ type Item = Signal;
+
+ fn next(&mut self) -> Option<Signal> {
+ if self.next < SIGNALS.len() {
+ let next_signal = SIGNALS[self.next];
+ self.next += 1;
+ Some(next_signal)
+ } else {
+ None
+ }
+ }
+}
+
+impl Signal {
+ pub fn iterator() -> SignalIterator {
+ SignalIterator{next: 0}
+ }
+
+ // We do not implement the From trait, because it is supposed to be infallible.
+ // With Rust RFC 1542 comes the appropriate trait TryFrom. Once it is
+ // implemented, we'll replace this function.
+ #[inline]
+ pub fn from_c_int(signum: libc::c_int) -> Result<Signal> {
+ match 0 < signum && signum < NSIG {
+ true => Ok(unsafe { mem::transmute(signum) }),
+ false => Err(Error::invalid_argument()),
+ }
+ }
+}
+
+pub const SIGIOT : Signal = SIGABRT;
+pub const SIGPOLL : Signal = SIGIO;
+pub const SIGUNUSED : Signal = SIGSYS;
+
bitflags!{
flags SaFlags: libc::c_int {
const SA_NOCLDSTOP = libc::SA_NOCLDSTOP,
@@ -69,7 +187,6 @@ pub struct SigSet {
sigset: libc::sigset_t
}
-pub type SigNum = libc::c_int;
impl SigSet {
pub fn all() -> SigSet {
@@ -86,40 +203,33 @@ impl SigSet {
SigSet { sigset: sigset }
}
- pub fn add(&mut self, signum: SigNum) -> Result<()> {
- let res = unsafe { libc::sigaddset(&mut self.sigset as *mut libc::sigset_t, signum) };
-
- Errno::result(res).map(drop)
+ pub fn add(&mut self, signal: Signal) {
+ unsafe { libc::sigaddset(&mut self.sigset as *mut libc::sigset_t, signal as libc::c_int) };
}
- pub fn clear(&mut self) -> Result<()> {
- let res = unsafe { libc::sigemptyset(&mut self.sigset as *mut libc::sigset_t) };
-
- Errno::result(res).map(drop)
+ pub fn clear(&mut self) {
+ unsafe { libc::sigemptyset(&mut self.sigset as *mut libc::sigset_t) };
}
- pub fn remove(&mut self, signum: SigNum) -> Result<()> {
- let res = unsafe { libc::sigdelset(&mut self.sigset as *mut libc::sigset_t, signum) };
-
- Errno::result(res).map(drop)
+ pub fn remove(&mut self, signal: Signal) {
+ unsafe { libc::sigdelset(&mut self.sigset as *mut libc::sigset_t, signal as libc::c_int) };
}
- pub fn extend(&mut self, other: &SigSet) -> Result<()> {
- for i in 1..NSIG {
- if try!(other.contains(i)) {
- try!(self.add(i));
- }
+ pub fn contains(&self, signal: Signal) -> bool {
+ let res = unsafe { libc::sigismember(&self.sigset as *const libc::sigset_t, signal as libc::c_int) };
+
+ match res {
+ 1 => true,
+ 0 => false,
+ _ => unreachable!("unexpected value from sigismember"),
}
- Ok(())
}
- pub fn contains(&self, signum: SigNum) -> Result<bool> {
- let res = unsafe { libc::sigismember(&self.sigset as *const libc::sigset_t, signum) };
-
- match try!(Errno::result(res)) {
- 1 => Ok(true),
- 0 => Ok(false),
- _ => unreachable!("unexpected value from sigismember"),
+ pub fn extend(&mut self, other: &SigSet) {
+ for signal in Signal::iterator() {
+ if other.contains(signal) {
+ self.add(signal);
+ }
}
}
@@ -154,11 +264,11 @@ impl SigSet {
/// Suspends execution of the calling thread until one of the signals in the
/// signal mask becomes pending, and returns the accepted signal.
- pub fn wait(&self) -> Result<SigNum> {
- let mut signum: SigNum = unsafe { mem::uninitialized() };
+ pub fn wait(&self) -> Result<Signal> {
+ let mut signum: libc::c_int = unsafe { mem::uninitialized() };
let res = unsafe { libc::sigwait(&self.sigset as *const libc::sigset_t, &mut signum) };
- Errno::result(res).map(|_| signum)
+ Errno::result(res).map(|_| Signal::from_c_int(signum).unwrap())
}
}
@@ -174,8 +284,8 @@ impl AsRef<libc::sigset_t> for SigSet {
pub enum SigHandler {
SigDfl,
SigIgn,
- Handler(extern fn(SigNum)),
- SigAction(extern fn(SigNum, *mut libc::siginfo_t, *mut libc::c_void))
+ Handler(extern fn(libc::c_int)),
+ SigAction(extern fn(libc::c_int, *mut libc::siginfo_t, *mut libc::c_void))
}
pub struct SigAction {
@@ -203,11 +313,11 @@ impl SigAction {
}
}
-pub unsafe fn sigaction(signum: SigNum, sigaction: &SigAction) -> Result<SigAction> {
+pub unsafe fn sigaction(signal: Signal, sigaction: &SigAction) -> Result<SigAction> {
let mut oldact = mem::uninitialized::<libc::sigaction>();
let res =
- libc::sigaction(signum, &sigaction.sigaction as *const libc::sigaction, &mut oldact as *mut libc::sigaction);
+ libc::sigaction(signal as libc::c_int, &sigaction.sigaction as *const libc::sigaction, &mut oldact as *mut libc::sigaction);
Errno::result(res).map(|_| SigAction { sigaction: oldact })
}
@@ -246,14 +356,14 @@ pub fn pthread_sigmask(how: SigFlags,
Errno::result(res).map(drop)
}
-pub fn kill(pid: libc::pid_t, signum: SigNum) -> Result<()> {
- let res = unsafe { libc::kill(pid, signum) };
+pub fn kill(pid: libc::pid_t, signal: Signal) -> Result<()> {
+ let res = unsafe { libc::kill(pid, signal as libc::c_int) };
Errno::result(res).map(drop)
}
-pub fn raise(signum: SigNum) -> Result<()> {
- let res = unsafe { libc::raise(signum) };
+pub fn raise(signal: Signal) -> Result<()> {
+ let res = unsafe { libc::raise(signal as libc::c_int) };
Errno::result(res).map(drop)
}
@@ -265,42 +375,42 @@ mod tests {
#[test]
fn test_contains() {
let mut mask = SigSet::empty();
- mask.add(SIGUSR1).unwrap();
+ mask.add(SIGUSR1);
- assert_eq!(mask.contains(SIGUSR1), Ok(true));
- assert_eq!(mask.contains(SIGUSR2), Ok(false));
+ assert!(mask.contains(SIGUSR1));
+ assert!(!mask.contains(SIGUSR2));
let all = SigSet::all();
- assert_eq!(all.contains(SIGUSR1), Ok(true));
- assert_eq!(all.contains(SIGUSR2), Ok(true));
+ assert!(all.contains(SIGUSR1));
+ assert!(all.contains(SIGUSR2));
}
#[test]
fn test_clear() {
let mut set = SigSet::all();
- set.clear().unwrap();
- for i in 1..NSIG {
- assert_eq!(set.contains(i), Ok(false));
+ set.clear();
+ for signal in Signal::iterator() {
+ assert!(!set.contains(signal));
}
}
#[test]
fn test_extend() {
let mut one_signal = SigSet::empty();
- one_signal.add(SIGUSR1).unwrap();
+ one_signal.add(SIGUSR1);
let mut two_signals = SigSet::empty();
- two_signals.add(SIGUSR2).unwrap();
- two_signals.extend(&one_signal).unwrap();
+ two_signals.add(SIGUSR2);
+ two_signals.extend(&one_signal);
- assert_eq!(two_signals.contains(SIGUSR1), Ok(true));
- assert_eq!(two_signals.contains(SIGUSR2), Ok(true));
+ assert!(two_signals.contains(SIGUSR1));
+ assert!(two_signals.contains(SIGUSR2));
}
#[test]
fn test_thread_signal_block() {
let mut mask = SigSet::empty();
- mask.add(SIGUSR1).unwrap();
+ mask.add(SIGUSR1);
assert!(mask.thread_block().is_ok());
}
@@ -308,18 +418,18 @@ mod tests {
#[test]
fn test_thread_signal_swap() {
let mut mask = SigSet::empty();
- mask.add(SIGUSR1).unwrap();
+ mask.add(SIGUSR1);
mask.thread_block().unwrap();
- assert!(SigSet::thread_get_mask().unwrap().contains(SIGUSR1).unwrap());
+ assert!(SigSet::thread_get_mask().unwrap().contains(SIGUSR1));
let mask2 = SigSet::empty();
- mask.add(SIGUSR2).unwrap();
+ mask.add(SIGUSR2);
let oldmask = mask2.thread_swap_mask(SIG_SETMASK).unwrap();
- assert!(oldmask.contains(SIGUSR1).unwrap());
- assert!(!oldmask.contains(SIGUSR2).unwrap());
+ assert!(oldmask.contains(SIGUSR1));
+ assert!(!oldmask.contains(SIGUSR2));
}
// TODO(#251): Re-enable after figuring out flakiness.
@@ -327,8 +437,8 @@ mod tests {
#[test]
fn test_sigwait() {
let mut mask = SigSet::empty();
- mask.add(SIGUSR1).unwrap();
- mask.add(SIGUSR2).unwrap();
+ mask.add(SIGUSR1);
+ mask.add(SIGUSR2);
mask.thread_block().unwrap();
raise(SIGUSR1).unwrap();
diff --git a/src/sys/socket/consts.rs b/src/sys/socket/consts.rs
index ddd8f6a9..63eaf28a 100644
--- a/src/sys/socket/consts.rs
+++ b/src/sys/socket/consts.rs
@@ -59,6 +59,8 @@ mod os {
pub const SO_TIMESTAMP: c_int = 29;
pub const SO_TYPE: c_int = 3;
pub const SO_BUSY_POLL: c_int = 46;
+ #[cfg(target_os = "linux")]
+ pub const SO_ORIGINAL_DST: c_int = 80;
// Socket options for TCP sockets
pub const TCP_NODELAY: c_int = 1;
@@ -96,6 +98,7 @@ mod os {
const MSG_DONTWAIT = 0x0040,
const MSG_EOR = 0x0080,
const MSG_ERRQUEUE = 0x2000,
+ const MSG_CMSG_CLOEXEC = 0x40000000,
}
}
diff --git a/src/sys/socket/ffi.rs b/src/sys/socket/ffi.rs
index 1cbf766c..55a47eb6 100644
--- a/src/sys/socket/ffi.rs
+++ b/src/sys/socket/ffi.rs
@@ -4,8 +4,11 @@
pub use libc::{socket, listen, bind, accept, connect, setsockopt, sendto, recvfrom, getsockname, getpeername, recv, send};
use libc::{c_int, c_void, socklen_t, size_t, ssize_t};
-use sys::uio::IoVec;
+#[cfg(target_os = "macos")]
+use libc::c_uint;
+
+use sys::uio::IoVec;
#[cfg(target_os = "linux")]
pub type type_of_cmsg_len = size_t;
@@ -13,6 +16,13 @@ pub type type_of_cmsg_len = size_t;
#[cfg(not(target_os = "linux"))]
pub type type_of_cmsg_len = socklen_t;
+// OSX always aligns struct cmsghdr as if it were a 32-bit OS
+#[cfg(target_os = "macos")]
+pub type type_of_cmsg_data = c_uint;
+
+#[cfg(not(target_os = "macos"))]
+pub type type_of_cmsg_data = size_t;
+
// Private because we don't expose any external functions that operate
// directly on this type; we just use it internally at FFI boundaries.
// Note that in some cases we store pointers in *const fields that the
@@ -37,7 +47,7 @@ pub struct cmsghdr {
pub cmsg_len: type_of_cmsg_len,
pub cmsg_level: c_int,
pub cmsg_type: c_int,
- pub cmsg_data: [type_of_cmsg_len; 0]
+ pub cmsg_data: [type_of_cmsg_data; 0]
}
extern {
diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs
index c96a5c8d..69f26aa0 100644
--- a/src/sys/socket/mod.rs
+++ b/src/sys/socket/mod.rs
@@ -94,7 +94,7 @@ unsafe fn copy_bytes<'a, 'b, T: ?Sized>(src: &T, dst: &'a mut &'b mut [u8]) {
}
-use self::ffi::{cmsghdr, msghdr, type_of_cmsg_len};
+use self::ffi::{cmsghdr, msghdr, type_of_cmsg_len, type_of_cmsg_data};
/// A structure used to make room in a cmsghdr passed to recvmsg. The
/// size and alignment match that of a cmsghdr followed by a T, but the
@@ -169,8 +169,7 @@ impl<'a> Iterator for CmsgIterator<'a> {
(SOL_SOCKET, SCM_RIGHTS) => unsafe {
Some(ControlMessage::ScmRights(
slice::from_raw_parts(
- &cmsg.cmsg_data as *const _ as *const _,
- len / mem::size_of::<RawFd>())))
+ &cmsg.cmsg_data as *const _ as *const _, 1)))
},
(_, _) => unsafe {
Some(ControlMessage::Unknown(UnknownCmsg(
@@ -201,12 +200,8 @@ pub enum ControlMessage<'a> {
pub struct UnknownCmsg<'a>(&'a cmsghdr, &'a [u8]);
fn cmsg_align(len: usize) -> usize {
- let round_to = mem::size_of::<type_of_cmsg_len>();
- if len % round_to == 0 {
- len
- } else {
- len + round_to - (len % round_to)
- }
+ let align_bytes = mem::size_of::<type_of_cmsg_data>() - 1;
+ (len + align_bytes) & !align_bytes
}
impl<'a> ControlMessage<'a> {
@@ -217,7 +212,7 @@ impl<'a> ControlMessage<'a> {
/// The value of CMSG_LEN on this message.
fn len(&self) -> usize {
- mem::size_of::<cmsghdr>() + match *self {
+ cmsg_align(mem::size_of::<cmsghdr>()) + match *self {
ControlMessage::ScmRights(fds) => {
mem::size_of_val(fds)
},
@@ -240,7 +235,11 @@ impl<'a> ControlMessage<'a> {
cmsg_data: [],
};
copy_bytes(&cmsg, buf);
- copy_bytes(fds, buf);
+
+ let padlen = cmsg_align(mem::size_of_val(&cmsg)) -
+ mem::size_of_val(&cmsg);
+ let buf2 = &mut &mut buf[padlen..];
+ copy_bytes(fds, buf2);
},
ControlMessage::Unknown(UnknownCmsg(orig_cmsg, bytes)) => {
copy_bytes(orig_cmsg, buf);
@@ -267,10 +266,10 @@ pub fn sendmsg<'a>(fd: RawFd, iov: &[IoVec<&'a [u8]>], cmsgs: &[ControlMessage<'
// multiple of size_t. Note also that the resulting vector claims
// to have length == capacity, so it's presently uninitialized.
let mut cmsg_buffer = unsafe {
- let mut vec = Vec::<size_t>::with_capacity(capacity / mem::size_of::<size_t>());
+ let mut vec = Vec::<u8>::with_capacity(len);
let ptr = vec.as_mut_ptr();
mem::forget(vec);
- Vec::<u8>::from_raw_parts(ptr as *mut _, capacity, capacity)
+ Vec::<u8>::from_raw_parts(ptr as *mut _, len, len)
};
{
let mut ptr = &mut cmsg_buffer[..];
@@ -290,7 +289,7 @@ pub fn sendmsg<'a>(fd: RawFd, iov: &[IoVec<&'a [u8]>], cmsgs: &[ControlMessage<'
msg_iov: iov.as_ptr(),
msg_iovlen: iov.len() as size_t,
msg_control: cmsg_buffer.as_ptr() as *const c_void,
- msg_controllen: len as size_t,
+ msg_controllen: capacity as size_t,
msg_flags: 0,
};
let ret = unsafe { ffi::sendmsg(fd, &mhdr, flags.bits()) };
@@ -630,6 +629,11 @@ pub unsafe fn sockaddr_storage_to_addr(
consts::AF_UNIX => {
Ok(SockAddr::Unix(UnixAddr(*(addr as *const _ as *const sockaddr_un), len)))
}
+ #[cfg(any(target_os = "linux", target_os = "android"))]
+ consts::AF_NETLINK => {
+ use libc::sockaddr_nl;
+ Ok(SockAddr::Netlink(NetlinkAddr(*(addr as *const _ as *const sockaddr_nl))))
+ }
af => panic!("unexpected address family {}", af),
}
}
diff --git a/src/sys/socket/sockopt.rs b/src/sys/socket/sockopt.rs
index 17de2d27..bf17347c 100644
--- a/src/sys/socket/sockopt.rs
+++ b/src/sys/socket/sockopt.rs
@@ -2,6 +2,8 @@ use super::{ffi, consts, GetSockOpt, SetSockOpt};
use {Errno, Result};
use sys::time::TimeVal;
use libc::{c_int, uint8_t, c_void, socklen_t};
+#[cfg(target_os = "linux")]
+use libc::sockaddr_in;
use std::mem;
use std::os::unix::io::RawFd;
@@ -47,10 +49,6 @@ macro_rules! getsockopt_impl {
// Helper to generate the sockopt accessors
macro_rules! sockopt_impl {
- (GetOnly, $name:ident, $level:path, $flag:path, $ty:ty) => {
- sockopt_impl!(GetOnly, $name, $level, $flag, $ty, GetStruct<$ty>);
- };
-
(GetOnly, $name:ident, $level:path, $flag:path, bool) => {
sockopt_impl!(GetOnly, $name, $level, $flag, bool, GetBool);
};
@@ -63,17 +61,6 @@ macro_rules! sockopt_impl {
sockopt_impl!(GetOnly, $name, $level, $flag, usize, GetUsize);
};
- (GetOnly, $name:ident, $level:path, $flag:path, $ty:ty, $getter:ty) => {
- #[derive(Copy, Clone, Debug)]
- pub struct $name;
-
- getsockopt_impl!($name, $level, $flag, $ty, $getter);
- };
-
- (SetOnly, $name:ident, $level:path, $flag:path, $ty:ty) => {
- sockopt_impl!(SetOnly, $name, $level, $flag, $ty, SetStruct<$ty>);
- };
-
(SetOnly, $name:ident, $level:path, $flag:path, bool) => {
sockopt_impl!(SetOnly, $name, $level, $flag, bool, SetBool);
};
@@ -86,31 +73,50 @@ macro_rules! sockopt_impl {
sockopt_impl!(SetOnly, $name, $level, $flag, usize, SetUsize);
};
- (SetOnly, $name:ident, $level:path, $flag:path, $ty:ty, $setter:ty) => {
- #[derive(Copy, Clone, Debug)]
- pub struct $name;
+ (Both, $name:ident, $level:path, $flag:path, bool) => {
+ sockopt_impl!(Both, $name, $level, $flag, bool, GetBool, SetBool);
+ };
- setsockopt_impl!($name, $level, $flag, $ty, $setter);
+ (Both, $name:ident, $level:path, $flag:path, u8) => {
+ sockopt_impl!(Both, $name, $level, $flag, u8, GetU8, SetU8);
};
- (Both, $name:ident, $level:path, $flag:path, $ty:ty, $getter:ty, $setter:ty) => {
+ (Both, $name:ident, $level:path, $flag:path, usize) => {
+ sockopt_impl!(Both, $name, $level, $flag, usize, GetUsize, SetUsize);
+ };
+
+ /*
+ * Matchers with generic getter types must be placed at the end, so
+ * they'll only match _after_ specialized matchers fail
+ */
+ (GetOnly, $name:ident, $level:path, $flag:path, $ty:ty) => {
+ sockopt_impl!(GetOnly, $name, $level, $flag, $ty, GetStruct<$ty>);
+ };
+
+ (GetOnly, $name:ident, $level:path, $flag:path, $ty:ty, $getter:ty) => {
#[derive(Copy, Clone, Debug)]
pub struct $name;
- setsockopt_impl!($name, $level, $flag, $ty, $setter);
getsockopt_impl!($name, $level, $flag, $ty, $getter);
};
- (Both, $name:ident, $level:path, $flag:path, bool) => {
- sockopt_impl!(Both, $name, $level, $flag, bool, GetBool, SetBool);
+ (SetOnly, $name:ident, $level:path, $flag:path, $ty:ty) => {
+ sockopt_impl!(SetOnly, $name, $level, $flag, $ty, SetStruct<$ty>);
};
- (Both, $name:ident, $level:path, $flag:path, u8) => {
- sockopt_impl!(Both, $name, $level, $flag, u8, GetU8, SetU8);
+ (SetOnly, $name:ident, $level:path, $flag:path, $ty:ty, $setter:ty) => {
+ #[derive(Copy, Clone, Debug)]
+ pub struct $name;
+
+ setsockopt_impl!($name, $level, $flag, $ty, $setter);
};
- (Both, $name:ident, $level:path, $flag:path, usize) => {
- sockopt_impl!(Both, $name, $level, $flag, usize, GetUsize, SetUsize);
+ (Both, $name:ident, $level:path, $flag:path, $ty:ty, $getter:ty, $setter:ty) => {
+ #[derive(Copy, Clone, Debug)]
+ pub struct $name;
+
+ setsockopt_impl!($name, $level, $flag, $ty, $setter);
+ getsockopt_impl!($name, $level, $flag, $ty, $getter);
};
(Both, $name:ident, $level:path, $flag:path, $ty:ty) => {
@@ -168,6 +174,8 @@ sockopt_impl!(GetOnly, SockType, consts::SOL_SOCKET, consts::SO_TYPE, super::Soc
target_os = "linux",
target_os = "nacl"))]
sockopt_impl!(GetOnly, AcceptConn, consts::SOL_SOCKET, consts::SO_ACCEPTCONN, bool);
+#[cfg(target_os = "linux")]
+sockopt_impl!(GetOnly, OriginalDst, consts::SOL_IP, consts::SO_ORIGINAL_DST, sockaddr_in);
/*
*
diff --git a/src/sys/wait.rs b/src/sys/wait.rs
index 3d9b3a50..259efb70 100644
--- a/src/sys/wait.rs
+++ b/src/sys/wait.rs
@@ -1,7 +1,7 @@
-use libc::{pid_t, c_int};
+use libc::{self, pid_t, c_int};
use {Errno, Result};
-use sys::signal;
+use sys::signal::Signal;
mod ffi {
use libc::{pid_t, c_int};
@@ -15,7 +15,8 @@ mod ffi {
target_os = "android")))]
bitflags!(
flags WaitPidFlag: c_int {
- const WNOHANG = 0x00000001,
+ const WNOHANG = libc::WNOHANG,
+ const WUNTRACED = libc::WUNTRACED,
}
);
@@ -23,14 +24,14 @@ bitflags!(
target_os = "android"))]
bitflags!(
flags WaitPidFlag: c_int {
- const WNOHANG = 0x00000001,
- const WUNTRACED = 0x00000002,
- const WEXITED = 0x00000004,
- const WCONTINUED = 0x00000008,
- const WNOWAIT = 0x01000000, // Don't reap, just poll status.
- const __WNOTHREAD = 0x20000000, // Don't wait on children of other threads in this group
- const __WALL = 0x40000000, // Wait on all children, regardless of type
- // const __WCLONE = 0x80000000,
+ const WNOHANG = libc::WNOHANG,
+ const WUNTRACED = libc::WUNTRACED,
+ const WEXITED = libc::WEXITED,
+ const WCONTINUED = libc::WCONTINUED,
+ const WNOWAIT = libc::WNOWAIT, // Don't reap, just poll status.
+ const __WNOTHREAD = libc::__WNOTHREAD, // Don't wait on children of other threads in this group
+ const __WALL = libc::__WALL, // Wait on all children, regardless of type
+ const __WCLONE = libc::__WCLONE,
}
);
@@ -41,8 +42,8 @@ const WSTOPPED: WaitPidFlag = WUNTRACED;
#[derive(Eq, PartialEq, Clone, Copy, Debug)]
pub enum WaitStatus {
Exited(pid_t, i8),
- Signaled(pid_t, signal::SigNum, bool),
- Stopped(pid_t, signal::SigNum),
+ Signaled(pid_t, Signal, bool),
+ Stopped(pid_t, Signal),
Continued(pid_t),
StillAlive
}
@@ -50,7 +51,7 @@ pub enum WaitStatus {
#[cfg(any(target_os = "linux",
target_os = "android"))]
mod status {
- use sys::signal;
+ use sys::signal::Signal;
pub fn exited(status: i32) -> bool {
(status & 0x7F) == 0
@@ -64,8 +65,8 @@ mod status {
((((status & 0x7f) + 1) as i8) >> 1) > 0
}
- pub fn term_signal(status: i32) -> signal::SigNum {
- (status & 0x7f) as signal::SigNum
+ pub fn term_signal(status: i32) -> Signal {
+ Signal::from_c_int(status & 0x7f).unwrap()
}
pub fn dumped_core(status: i32) -> bool {
@@ -76,8 +77,8 @@ mod status {
(status & 0xff) == 0x7f
}
- pub fn stop_signal(status: i32) -> signal::SigNum {
- ((status & 0xFF00) >> 8) as signal::SigNum
+ pub fn stop_signal(status: i32) -> Signal {
+ Signal::from_c_int((status & 0xFF00) >> 8).unwrap()
}
pub fn continued(status: i32) -> bool {
@@ -88,7 +89,7 @@ mod status {
#[cfg(any(target_os = "macos",
target_os = "ios"))]
mod status {
- use sys::signal;
+ use sys::signal::{Signal,SIGCONT};
const WCOREFLAG: i32 = 0x80;
const WSTOPPED: i32 = 0x7f;
@@ -101,16 +102,16 @@ mod status {
((status >> 8) & 0xFF) as i8
}
- pub fn stop_signal(status: i32) -> signal::SigNum {
- (status >> 8) as signal::SigNum
+ pub fn stop_signal(status: i32) -> Signal {
+ Signal::from_c_int(status >> 8).unwrap()
}
pub fn continued(status: i32) -> bool {
- wstatus(status) == WSTOPPED && stop_signal(status) == 0x13
+ wstatus(status) == WSTOPPED && stop_signal(status) == SIGCONT
}
pub fn stopped(status: i32) -> bool {
- wstatus(status) == WSTOPPED && stop_signal(status) != 0x13
+ wstatus(status) == WSTOPPED && stop_signal(status) != SIGCONT
}
pub fn exited(status: i32) -> bool {
@@ -121,8 +122,8 @@ mod status {
wstatus(status) != WSTOPPED && wstatus(status) != 0
}
- pub fn term_signal(status: i32) -> signal::SigNum {
- wstatus(status) as signal::SigNum
+ pub fn term_signal(status: i32) -> Signal {
+ Signal::from_c_int(wstatus(status)).unwrap()
}
pub fn dumped_core(status: i32) -> bool {
@@ -135,7 +136,7 @@ mod status {
target_os = "dragonfly",
target_os = "netbsd"))]
mod status {
- use sys::signal;
+ use sys::signal::Signal;
const WCOREFLAG: i32 = 0x80;
const WSTOPPED: i32 = 0x7f;
@@ -148,16 +149,16 @@ mod status {
wstatus(status) == WSTOPPED
}
- pub fn stop_signal(status: i32) -> signal::SigNum {
- (status >> 8) as signal::SigNum
+ pub fn stop_signal(status: i32) -> Signal {
+ Signal::from_c_int(status >> 8).unwrap()
}
pub fn signaled(status: i32) -> bool {
wstatus(status) != WSTOPPED && wstatus(status) != 0 && status != 0x13
}
- pub fn term_signal(status: i32) -> signal::SigNum {
- wstatus(status) as signal::SigNum
+ pub fn term_signal(status: i32) -> Signal {
+ Signal::from_c_int(wstatus(status)).unwrap()
}
pub fn exited(status: i32) -> bool {
diff --git a/src/ucontext.rs b/src/ucontext.rs
index f77b4815..6886dd41 100644
--- a/src/ucontext.rs
+++ b/src/ucontext.rs
@@ -1,6 +1,8 @@
use libc;
+#[cfg(not(target_env = "musl"))]
use {Errno, Result};
use std::mem;
+use sys::signal::SigSet;
#[derive(Clone, Copy)]
pub struct UContext {
@@ -8,6 +10,7 @@ pub struct UContext {
}
impl UContext {
+ #[cfg(not(target_env = "musl"))]
pub fn get() -> Result<UContext> {
let mut context: libc::ucontext_t = unsafe { mem::uninitialized() };
let res = unsafe {
@@ -16,10 +19,19 @@ impl UContext {
Errno::result(res).map(|_| UContext { context: context })
}
+ #[cfg(not(target_env = "musl"))]
pub fn set(&self) -> Result<()> {
let res = unsafe {
libc::setcontext(&self.context as *const libc::ucontext_t)
};
Errno::result(res).map(drop)
}
+
+ pub fn sigmask_mut(&mut self) -> &mut SigSet {
+ unsafe { mem::transmute(&mut self.context.uc_sigmask) }
+ }
+
+ pub fn sigmask(&self) -> &SigSet {
+ unsafe { mem::transmute(&self.context.uc_sigmask) }
+ }
}
diff --git a/src/unistd.rs b/src/unistd.rs
index 8db44163..2eb218b3 100644
--- a/src/unistd.rs
+++ b/src/unistd.rs
@@ -3,13 +3,14 @@
use {Errno, Error, Result, NixPath};
use fcntl::{fcntl, OFlag, O_NONBLOCK, O_CLOEXEC, FD_CLOEXEC};
use fcntl::FcntlArg::{F_SETFD, F_SETFL};
-use libc::{self, c_char, c_void, c_int, c_uint, size_t, pid_t, off_t, uid_t, gid_t};
+use libc::{self, c_char, c_void, c_int, c_uint, size_t, pid_t, off_t, uid_t, gid_t, mode_t};
use std::mem;
-use std::ffi::{CString, OsStr};
-use std::os::unix::ffi::OsStrExt;
+use std::ffi::{CString, CStr, OsString, OsStr};
+use std::os::unix::ffi::{OsStringExt, OsStrExt};
use std::os::unix::io::RawFd;
-use std::path::{PathBuf, Path};
+use std::path::{PathBuf};
use void::Void;
+use sys::stat::Mode;
#[cfg(any(target_os = "linux", target_os = "android"))]
pub use self::linux::*;
@@ -113,11 +114,109 @@ pub fn chdir<P: ?Sized + NixPath>(path: &P) -> Result<()> {
Errno::result(res).map(drop)
}
+/// Creates new directory `path` with access rights `mode`.
+///
+/// # Errors
+///
+/// There are several situations where mkdir might fail:
+///
+/// - current user has insufficient rights in the parent directory
+/// - the path already exists
+/// - the path name is too long (longer than `PATH_MAX`, usually 4096 on linux, 1024 on OS X)
+///
+/// For a full list consult
+/// [man mkdir(2)](http://man7.org/linux/man-pages/man2/mkdir.2.html#ERRORS)
+///
+/// # Example
+///
+/// ```rust
+/// extern crate tempdir;
+/// extern crate nix;
+///
+/// use nix::unistd;
+/// use nix::sys::stat;
+/// use tempdir::TempDir;
+///
+/// fn main() {
+/// let mut tmp_dir = TempDir::new("test_mkdir").unwrap().into_path();
+/// tmp_dir.push("new_dir");
+///
+/// // create new directory and give read, write and execute rights to the owner
+/// match unistd::mkdir(&tmp_dir, stat::S_IRWXU) {
+/// Ok(_) => println!("created {:?}", tmp_dir),
+/// Err(err) => println!("Error creating directory: {}", err),
+/// }
+/// }
+/// ```
+#[inline]
+pub fn mkdir<P: ?Sized + NixPath>(path: &P, mode: Mode) -> Result<()> {
+ let res = try!(path.with_nix_path(|cstr| {
+ unsafe { libc::mkdir(cstr.as_ptr(), mode.bits() as mode_t) }
+ }));
+
+ Errno::result(res).map(drop)
+}
+
+/// Returns the current directory as a PathBuf
+///
+/// Err is returned if the current user doesn't have the permission to read or search a component
+/// of the current path.
+///
+/// # Example
+///
+/// ```rust
+/// extern crate nix;
+///
+/// use nix::unistd;
+///
+/// fn main() {
+/// // assume that we are allowed to get current directory
+/// let dir = unistd::getcwd().unwrap();
+/// println!("The current directory is {:?}", dir);
+/// }
+/// ```
+#[inline]
+pub fn getcwd() -> Result<PathBuf> {
+ let mut buf = Vec::with_capacity(512);
+ loop {
+ unsafe {
+ let ptr = buf.as_mut_ptr() as *mut libc::c_char;
+
+ // The buffer must be large enough to store the absolute pathname plus
+ // a terminating null byte, or else null is returned.
+ // To safely handle this we start with a reasonable size (512 bytes)
+ // and double the buffer size upon every error
+ if !libc::getcwd(ptr, buf.capacity()).is_null() {
+ let len = CStr::from_ptr(buf.as_ptr() as *const libc::c_char).to_bytes().len();
+ buf.set_len(len);
+ buf.shrink_to_fit();
+ return Ok(PathBuf::from(OsString::from_vec(buf)));
+ } else {
+ let error = Errno::last();
+ // ERANGE means buffer was too small to store directory name
+ if error != Errno::ERANGE {
+ return Err(Error::Sys(error));
+ }
+ }
+
+ // Trigger the internal buffer resizing logic of `Vec` by requiring
+ // more space than the current capacity.
+ let cap = buf.capacity();
+ buf.set_len(cap);
+ buf.reserve(1);
+ }
+ }
+}
+
#[inline]
pub fn chown<P: ?Sized + NixPath>(path: &P, owner: Option<uid_t>, group: Option<gid_t>) -> Result<()> {
let res = try!(path.with_nix_path(|cstr| {
- // We use `0 - 1` to get `-1 : {u,g}id_t` which is specified as the no-op value for chown(3).
- unsafe { libc::chown(cstr.as_ptr(), owner.unwrap_or(0 - 1), group.unwrap_or(0 - 1)) }
+ // According to the POSIX specification, -1 is used to indicate that
+ // owner and group, respectively, are not to be changed. Since uid_t and
+ // gid_t are unsigned types, we use wrapping_sub to get '-1'.
+ unsafe { libc::chown(cstr.as_ptr(),
+ owner.unwrap_or((0 as uid_t).wrapping_sub(1)),
+ group.unwrap_or((0 as gid_t).wrapping_sub(1))) }
}));
Errno::result(res).map(drop)
@@ -174,7 +273,10 @@ pub fn daemon(nochdir: bool, noclose: bool) -> Result<()> {
pub fn sethostname(name: &[u8]) -> Result<()> {
// Handle some differences in type of the len arg across platforms.
cfg_if! {
- if #[cfg(any(target_os = "macos", target_os = "ios"))] {
+ if #[cfg(any(target_os = "dragonfly",
+ target_os = "freebsd",
+ target_os = "ios",
+ target_os = "macos", ))] {
type sethostname_len_t = c_int;
} else {
type sethostname_len_t = size_t;
@@ -212,6 +314,40 @@ pub fn write(fd: RawFd, buf: &[u8]) -> Result<usize> {
Errno::result(res).map(|r| r as usize)
}
+pub enum Whence {
+ SeekSet,
+ SeekCur,
+ SeekEnd,
+ SeekData,
+ SeekHole
+}
+
+impl Whence {
+ fn to_libc_type(&self) -> c_int {
+ match self {
+ &Whence::SeekSet => libc::SEEK_SET,
+ &Whence::SeekCur => libc::SEEK_CUR,
+ &Whence::SeekEnd => libc::SEEK_END,
+ &Whence::SeekData => 3,
+ &Whence::SeekHole => 4
+ }
+ }
+
+}
+
+pub fn lseek(fd: RawFd, offset: libc::off_t, whence: Whence) -> Result<libc::off_t> {
+ let res = unsafe { libc::lseek(fd, offset, whence.to_libc_type()) };
+
+ Errno::result(res).map(|r| r as libc::off_t)
+}
+
+#[cfg(any(target_os = "linux", target_os = "android"))]
+pub fn lseek64(fd: RawFd, offset: libc::off64_t, whence: Whence) -> Result<libc::off64_t> {
+ let res = unsafe { libc::lseek64(fd, offset, whence.to_libc_type()) };
+
+ Errno::result(res).map(|r| r as libc::off64_t)
+}
+
pub fn pipe() -> Result<(RawFd, RawFd)> {
unsafe {
let mut fds: [c_int; 2] = mem::uninitialized();
@@ -290,7 +426,6 @@ pub fn unlink<P: ?Sized + NixPath>(path: &P) -> Result<()> {
libc::unlink(cstr.as_ptr())
}
}));
-
Errno::result(res).map(drop)
}
@@ -376,19 +511,41 @@ pub fn sleep(seconds: libc::c_uint) -> c_uint {
unsafe { libc::sleep(seconds) }
}
+/// Creates a regular file which persists even after process termination
+///
+/// * `template`: a path whose 6 rightmost characters must be X, e.g. /tmp/tmpfile_XXXXXX
+/// * returns: tuple of file descriptor and filename
+///
+/// Err is returned either if no temporary filename could be created or the template doesn't
+/// end with XXXXXX
+///
+/// # Example
+///
+/// ```rust
+/// use nix::unistd;
+///
+/// let fd = match unistd::mkstemp("/tmp/tempfile_XXXXXX") {
+/// Ok((fd, path)) => {
+/// unistd::unlink(path.as_path()).unwrap(); // flag file to be deleted at app termination
+/// fd
+/// }
+/// Err(e) => panic!("mkstemp failed: {}", e)
+/// };
+/// // do something with fd
+/// ```
#[inline]
pub fn mkstemp<P: ?Sized + NixPath>(template: &P) -> Result<(RawFd, PathBuf)> {
let res = template.with_nix_path(|path| {
- let owned_path = path.to_owned();
- let path_ptr = owned_path.into_raw();
+ let mut path_copy = path.to_bytes_with_nul().to_owned();
+ let p: *mut i8 = path_copy.as_mut_ptr() as *mut i8;
unsafe {
- (libc::mkstemp(path_ptr), CString::from_raw(path_ptr))
+ (libc::mkstemp(p), OsStr::from_bytes(CStr::from_ptr(p).to_bytes()))
}
});
match res {
Ok((fd, pathname)) => {
try!(Errno::result(fd));
- Ok((fd, Path::new(OsStr::from_bytes(pathname.as_bytes())).to_owned()))
+ Ok((fd, PathBuf::from(pathname).to_owned()))
}
Err(e) => {
Err(e)