From a821f9630cbe3ee12846625fa63abc768b59d1a0 Mon Sep 17 00:00:00 2001 From: Andreas Fuchs Date: Sun, 1 May 2016 12:52:20 -0700 Subject: Add mkstemp(3) --- src/unistd.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src') diff --git a/src/unistd.rs b/src/unistd.rs index 71448248..31367791 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -374,6 +374,19 @@ pub fn sleep(seconds: libc::c_uint) -> c_uint { unsafe { libc::sleep(seconds) } } +#[inline] +pub fn mkstemp(template: &P) -> Result { + let res = try!(template.with_nix_path(|path| { + let mut path_copy = path.to_bytes_with_nul().to_owned(); + let c_template: *mut c_char = path_copy.as_mut_ptr() as *mut c_char; + unsafe { + libc::mkstemp(c_template) + } + })); + Errno::result(res) + +} + #[cfg(any(target_os = "linux", target_os = "android"))] mod linux { use sys::syscall::{syscall, SYSPIVOTROOT}; -- cgit v1.2.3 From 23a7ea64938cc73d476e42b80a243fefbe7111b2 Mon Sep 17 00:00:00 2001 From: Andreas Fuchs Date: Sun, 1 May 2016 17:20:12 -0700 Subject: Return both the fd and the created path --- src/unistd.rs | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/unistd.rs b/src/unistd.rs index 31367791..8db44163 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -5,8 +5,10 @@ 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 std::mem; -use std::ffi::CString; +use std::ffi::{CString, OsStr}; +use std::os::unix::ffi::OsStrExt; use std::os::unix::io::RawFd; +use std::path::{PathBuf, Path}; use void::Void; #[cfg(any(target_os = "linux", target_os = "android"))] @@ -375,16 +377,23 @@ pub fn sleep(seconds: libc::c_uint) -> c_uint { } #[inline] -pub fn mkstemp(template: &P) -> Result { - let res = try!(template.with_nix_path(|path| { - let mut path_copy = path.to_bytes_with_nul().to_owned(); - let c_template: *mut c_char = path_copy.as_mut_ptr() as *mut c_char; +pub fn mkstemp(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(); unsafe { - libc::mkstemp(c_template) + (libc::mkstemp(path_ptr), CString::from_raw(path_ptr)) } - })); - Errno::result(res) - + }); + match res { + Ok((fd, pathname)) => { + try!(Errno::result(fd)); + Ok((fd, Path::new(OsStr::from_bytes(pathname.as_bytes())).to_owned())) + } + Err(e) => { + Err(e) + } + } } #[cfg(any(target_os = "linux", target_os = "android"))] -- cgit v1.2.3 From aa426634ad2a1c94df8367ce1a8a6ba7a1ac62b6 Mon Sep 17 00:00:00 2001 From: Murarth Date: Thu, 25 Aug 2016 14:10:59 -0700 Subject: Implement `Clone` for `FdSet` on Mac/iOS --- src/sys/select.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'src') 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] } -- cgit v1.2.3 From ae6635f7a6b0a941e05c2d4e4fc2a485df282034 Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Fri, 26 Aug 2016 03:05:29 +0200 Subject: Add FcntlArg::F_FULLFSYNC https://developer.apple.com/library/ios/documentation/System/Conceptual/ManPages_iPhoneOS/man2/fsync.2.html --- src/fcntl.rs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src') 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 { 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!() } -- cgit v1.2.3 From 899a13061fcecfdd9d0e94bf6ebce7c152266b0d Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Mon, 15 Aug 2016 21:02:59 +0200 Subject: Replace parts of ffi module by libc functions in sched.rs --- src/sched.rs | 226 +++++++++++++++++------------------------------------------ 1 file changed, 66 insertions(+), 160 deletions(-) (limited to 'src') 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 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 { + if field >= 8 * mem::size_of::() { + 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::() { + 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::() { + 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::() as size_t, mem::transmute(cpuset)) + libc::sched_setaffinity(pid as libc::pid_t, + mem::size_of::() 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) -> Result { +pub fn clone(mut cb: CloneCb, + stack: &mut [u8], + flags: CloneFlags, + signal: Option) + -> Result { 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) } -- cgit v1.2.3 From 93dc2387bd336d843c3d39f0a44b906b637ec2f8 Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Thu, 11 Aug 2016 21:25:09 +0200 Subject: Use libc in poll.rs --- src/poll.rs | 92 ++++++++++++++++++++++--------------------------------------- 1 file changed, 33 insertions(+), 59 deletions(-) (limited to 'src') 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::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 { +pub fn poll(fds: &mut [PollFd], timeout: libc::c_int) -> Result { 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) -- cgit v1.2.3 From b0e69e6e729a987cb2acc1e9452241b01554c30b Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Wed, 3 Aug 2016 22:12:31 +0200 Subject: Replace ffi module by libc functions in mqueue.rs --- src/lib.rs | 2 +- src/mqueue.rs | 154 ++++++++++++++++++++++++++-------------------------------- 2 files changed, 70 insertions(+), 86 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index b983a9c2..8dbf9fe0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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..2dd2dbd9 100644 --- a/src/mqueue.rs +++ b/src/mqueue.rs @@ -4,114 +4,99 @@ 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 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::() }; + 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 flags(&self) -> c_long { + self.mq_attr.mq_flags + } +} -pub fn mq_open(name: &CString, oflag: MQ_OFlag, mode: Mode, attr: Option<&MqAttr>) -> Result { - 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 mq_open(name: &CString, oflag: MQ_OFlag, mode: Mode, attr: Option<&MqAttr>) -> Result { + 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 { +pub fn mq_receive(mqdes: mqd_t, message: &mut [u8], msg_prio: &mut u32) -> Result { 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 { - 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 { + let mut attr = unsafe { mem::uninitialized::() }; + 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 +104,26 @@ pub fn mq_getattr(mqd: MQd) -> Result { /// 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 { - 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 { + let mut attr = unsafe { mem::uninitialized::() }; + 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) } -- cgit v1.2.3 From 8ed552100dc63589017f09adc0413235d3a89599 Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Sat, 6 Aug 2016 09:49:22 +0200 Subject: Run rustfmt on mqueue.rs --- src/mqueue.rs | 62 +++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/mqueue.rs b/src/mqueue.rs index 2dd2dbd9..9bf6e77e 100644 --- a/src/mqueue.rs +++ b/src/mqueue.rs @@ -4,7 +4,7 @@ use {Errno, Result}; -use libc::{self, c_char,c_long,mode_t,mqd_t,size_t}; +use libc::{self, c_char, c_long, mode_t, mqd_t, size_t}; use std::ffi::CString; use sys::stat::Mode; use std::mem; @@ -30,28 +30,31 @@ libc_bitflags!{ #[repr(C)] #[derive(Clone, Copy)] pub struct MqAttr { - mq_attr: libc::mq_attr + mq_attr: libc::mq_attr, } impl PartialEq 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 + 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 { + 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::() }; attr.mq_flags = mq_flags; attr.mq_maxmsg = mq_maxmsg; attr.mq_msgsize = mq_msgsize; attr.mq_curmsgs = mq_curmsgs; - MqAttr{mq_attr: attr} + MqAttr { mq_attr: attr } } pub fn flags(&self) -> c_long { @@ -60,14 +63,19 @@ impl MqAttr { } -pub fn mq_open(name: &CString, oflag: MQ_OFlag, mode: Mode, attr: Option<&MqAttr>) -> Result { +pub fn mq_open(name: &CString, + oflag: MQ_OFlag, + mode: Mode, + attr: Option<&MqAttr>) + -> Result { 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) } + 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()) } - } + None => unsafe { libc::mq_open(name.as_ptr(), oflag.bits()) }, }; Errno::result(res) } @@ -77,19 +85,29 @@ pub fn mq_unlink(name: &CString) -> Result<()> { Errno::result(res).map(drop) } -pub fn mq_close(mqdes: mqd_t) -> Result<()> { +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_t, message: &mut [u8], msg_prio: &mut u32) -> Result { let len = message.len() as size_t; - let res = unsafe { libc::mq_receive(mqdes, message.as_mut_ptr() as *mut c_char, len, msg_prio as *mut u32) }; + 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_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) }; + let res = unsafe { + libc::mq_send(mqdes, + message.as_ptr() as *const c_char, + message.len(), + msq_prio) + }; Errno::result(res).map(drop) } @@ -115,7 +133,10 @@ pub fn mq_setattr(mqd: mqd_t, newattr: &MqAttr) -> Result { /// Returns the old attributes 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_attr.mq_maxmsg, oldattr.mq_attr.mq_msgsize, oldattr.mq_attr.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) } @@ -124,6 +145,9 @@ pub fn mq_set_nonblock(mqd: mqd_t) -> Result<(MqAttr)> { /// Returns the old attributes pub fn mq_remove_nonblock(mqd: mqd_t) -> Result<(MqAttr)> { let oldattr = try!(mq_getattr(mqd)); - let newattr = MqAttr::new(0, oldattr.mq_attr.mq_maxmsg, oldattr.mq_attr.mq_msgsize, oldattr.mq_attr.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) } -- cgit v1.2.3 From ac642737967525226dc36f43e6e99d58328d5c6c Mon Sep 17 00:00:00 2001 From: Philipp Keller Date: Fri, 2 Sep 2016 22:57:39 +0200 Subject: implemented getcwd (returning Result, reconciling all calls to expect into proper try handling), needs testing still --- src/unistd.rs | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/unistd.rs b/src/unistd.rs index d4da60da..34e9b6a9 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -5,9 +5,10 @@ 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 std::mem; -use std::ffi::CString; +use std::ffi::{CString,CStr}; use std::os::unix::io::RawFd; use void::Void; +use std::path::PathBuf; #[cfg(any(target_os = "linux", target_os = "android"))] pub use self::linux::*; @@ -111,6 +112,45 @@ pub fn chdir(path: &P) -> Result<()> { Errno::result(res).map(drop) } +// #[inline] +// pub fn mkdir(path: &P) -> Result<()> { +// Errno::result(0) +// } + +#[inline] +pub fn getcwd() -> Result { + 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(ptr).to_bytes().len(); + buf.set_len(len); + buf.shrink_to_fit(); + let s = try!(CString::new(buf).map_err(|_| Error::Sys(Errno::EILSEQ))); + let s = try!(s.into_string().map_err(|_| Error::Sys(Errno::EILSEQ))); + return Ok(PathBuf::from(&s)); + } else { + let error = Errno::last(); + 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(path: &P, owner: Option, group: Option) -> Result<()> { let res = try!(path.with_nix_path(|cstr| { -- cgit v1.2.3 From 8b1828a5cd7c133156fc2bac703136e6d5abd31d Mon Sep 17 00:00:00 2001 From: Philipp Keller Date: Mon, 5 Sep 2016 21:50:06 +0200 Subject: implemented mkdir, extended getcwd test to include long path names --- src/unistd.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/unistd.rs b/src/unistd.rs index 34e9b6a9..f8e17153 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -3,12 +3,13 @@ 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,CStr}; +use std::path::PathBuf; use std::os::unix::io::RawFd; use void::Void; -use std::path::PathBuf; +use sys::stat::Mode; #[cfg(any(target_os = "linux", target_os = "android"))] pub use self::linux::*; @@ -112,10 +113,14 @@ pub fn chdir(path: &P) -> Result<()> { Errno::result(res).map(drop) } -// #[inline] -// pub fn mkdir(path: &P) -> Result<()> { -// Errno::result(0) -// } +#[inline] +pub fn mkdir(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) +} #[inline] pub fn getcwd() -> Result { @@ -137,7 +142,8 @@ pub fn getcwd() -> Result { return Ok(PathBuf::from(&s)); } else { let error = Errno::last(); - if error == Errno::ERANGE { + // ERANGE means buffer was too small to store directory name + if error != Errno::ERANGE { return Err(Error::Sys(error)); } } -- cgit v1.2.3 From 8fbd8e91ff9ef518287bb4f2a012d3f62332f1ca Mon Sep 17 00:00:00 2001 From: Philipp Keller Date: Tue, 6 Sep 2016 17:17:21 +0200 Subject: made it running with rust 1.2.0: the code for getcwd is now an exact copy of the implementation in std --- src/unistd.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/unistd.rs b/src/unistd.rs index f8e17153..6c3a1ebf 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -5,7 +5,8 @@ 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, mode_t}; use std::mem; -use std::ffi::{CString,CStr}; +use std::ffi::{CString,CStr,OsString}; +use std::os::unix::ffi::OsStringExt; use std::path::PathBuf; use std::os::unix::io::RawFd; use void::Void; @@ -134,12 +135,10 @@ pub fn getcwd() -> Result { // 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(ptr).to_bytes().len(); + let len = CStr::from_ptr(buf.as_ptr() as *const libc::c_char).to_bytes().len(); buf.set_len(len); buf.shrink_to_fit(); - let s = try!(CString::new(buf).map_err(|_| Error::Sys(Errno::EILSEQ))); - let s = try!(s.into_string().map_err(|_| Error::Sys(Errno::EILSEQ))); - return Ok(PathBuf::from(&s)); + return Ok(PathBuf::from(OsString::from_vec(buf))); } else { let error = Errno::last(); // ERANGE means buffer was too small to store directory name -- cgit v1.2.3 From c0a578539a4ad6b1f8ad48b8e26fbacc0d55852d Mon Sep 17 00:00:00 2001 From: Philipp Keller Date: Tue, 6 Sep 2016 17:19:07 +0200 Subject: fixed the trailing whitespaces --- src/unistd.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/unistd.rs b/src/unistd.rs index 6c3a1ebf..900be379 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -144,7 +144,7 @@ pub fn getcwd() -> Result { // 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 @@ -153,7 +153,7 @@ pub fn getcwd() -> Result { buf.set_len(cap); buf.reserve(1); } - } + } } #[inline] -- cgit v1.2.3 From 50693c11b02f11e13687ee48f5a1e0131542d7f8 Mon Sep 17 00:00:00 2001 From: Philipp Keller Date: Tue, 6 Sep 2016 22:18:19 +0200 Subject: added documentation for getcwd and mkdir, changed test so that it compares against std::env::current_dir --- src/unistd.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'src') diff --git a/src/unistd.rs b/src/unistd.rs index 900be379..52add2cd 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -114,6 +114,35 @@ pub fn chdir(path: &P) -> Result<()> { Errno::result(res).map(drop) } +/// Creates new directory `path` with access rights `mode`. +/// +/// # Errors +/// +/// `Err` is returned in case of an error. There are several situations where mkdir might fail. +/// For a full list consult `man mkdir(2)` +/// +/// - 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) +/// +/// # 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"); +/// +/// // owner has read, write and execute rights on the new directory +/// unistd::mkdir(&tmp_dir, stat::S_IRWXU).expect("couldn't create directory"); +/// } +/// ``` #[inline] pub fn mkdir(path: &P, mode: Mode) -> Result<()> { let res = try!(path.with_nix_path(|cstr| { @@ -123,6 +152,18 @@ pub fn mkdir(path: &P, mode: Mode) -> Result<()> { 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 +/// use nix::unistd; +/// +/// let dir = unistd::getcwd().expect("not allowed to get current directory"); +/// println!("The current directory is {:?}", dir.display()); +/// ``` #[inline] pub fn getcwd() -> Result { let mut buf = Vec::with_capacity(512); -- cgit v1.2.3 From 37e4f9756d7181e6b95f9b812874d582bfc2eec5 Mon Sep 17 00:00:00 2001 From: Philipp Keller Date: Tue, 6 Sep 2016 22:28:13 +0200 Subject: rust 1.2.0 doesn't support expect, switched to proper match block --- src/unistd.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/unistd.rs b/src/unistd.rs index 52add2cd..d7bd3b91 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -139,8 +139,11 @@ pub fn chdir(path: &P) -> Result<()> { /// let mut tmp_dir = TempDir::new("test_mkdir").unwrap().into_path(); /// tmp_dir.push("new_dir"); /// -/// // owner has read, write and execute rights on the new directory -/// unistd::mkdir(&tmp_dir, stat::S_IRWXU).expect("couldn't create directory"); +/// // 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.display()), +/// Err(err) => println!("Error creating directory: {}", err), +/// } /// } /// ``` #[inline] -- cgit v1.2.3 From 5f1e144de965d10b61fbd2c8e3ac50c12099690a Mon Sep 17 00:00:00 2001 From: Philipp Keller Date: Wed, 7 Sep 2016 07:17:36 +0200 Subject: resolving all remarks by @posborne, fixed max line length=99, fixed rust 1.2.0 error in doc-test --- src/unistd.rs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/unistd.rs b/src/unistd.rs index d7bd3b91..74d74c9c 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -5,7 +5,7 @@ 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, mode_t}; use std::mem; -use std::ffi::{CString,CStr,OsString}; +use std::ffi::{CString, CStr, OsString}; use std::os::unix::ffi::OsStringExt; use std::path::PathBuf; use std::os::unix::io::RawFd; @@ -118,13 +118,15 @@ pub fn chdir(path: &P) -> Result<()> { /// /// # Errors /// -/// `Err` is returned in case of an error. There are several situations where mkdir might fail. -/// For a full list consult `man mkdir(2)` +/// 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 @@ -141,7 +143,7 @@ pub fn chdir(path: &P) -> Result<()> { /// /// // 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.display()), +/// Ok(_) => println!("created {:?}", tmp_dir), /// Err(err) => println!("Error creating directory: {}", err), /// } /// } @@ -157,15 +159,21 @@ pub fn mkdir(path: &P, mode: Mode) -> Result<()> { /// 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. +/// 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; /// -/// let dir = unistd::getcwd().expect("not allowed to get current directory"); -/// println!("The current directory is {:?}", dir.display()); +/// 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 { -- cgit v1.2.3 From 7508fd3596b448497681e4a93fcb616939153a9c Mon Sep 17 00:00:00 2001 From: Hiroki Noda Date: Sun, 10 Jul 2016 02:32:11 +0900 Subject: Add epoll_create1 and set EPOLL_CLOEXEC flag by default. --- src/sys/epoll.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src') diff --git a/src/sys/epoll.rs b/src/sys/epoll.rs index 2090a0df..67449e87 100644 --- a/src/sys/epoll.rs +++ b/src/sys/epoll.rs @@ -8,11 +8,19 @@ mod ffi { extern { pub fn epoll_create(size: c_int) -> c_int; + pub fn epoll_create1(flags: c_int) -> c_int; pub fn epoll_ctl(epfd: c_int, op: c_int, fd: c_int, event: *const EpollEvent) -> c_int; pub fn epoll_wait(epfd: c_int, events: *mut EpollEvent, max_events: c_int, timeout: c_int) -> c_int; } } +bitflags!( + flags EpollFdFlag: c_int { + const EPOLL_NONBLOCK = 0x800, + const EPOLL_CLOEXEC = 0x80000 + } +); + bitflags!( #[repr(C)] flags EpollEventKind: u32 { @@ -79,6 +87,13 @@ pub fn epoll_create() -> Result { Errno::result(res) } +#[inline] +pub fn epoll_create1(flags: EpollFdFlag) -> Result { + let res = unsafe { ffi::epoll_create1(flags.bits() | EPOLL_CLOEXEC.bits) }; + + Errno::result(res) +} + #[inline] pub fn epoll_ctl(epfd: RawFd, op: EpollOp, fd: RawFd, event: &EpollEvent) -> Result<()> { let res = unsafe { ffi::epoll_ctl(epfd, op as c_int, fd, event as *const EpollEvent) }; -- cgit v1.2.3 From 26d211710ca4f0ad8b8e8d3e3284d253d953aef5 Mon Sep 17 00:00:00 2001 From: Hiroki Noda Date: Sun, 10 Jul 2016 04:02:37 +0900 Subject: Not to set EPOLL_CLOEXEC by default. --- src/sys/epoll.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/sys/epoll.rs b/src/sys/epoll.rs index 67449e87..af938bd3 100644 --- a/src/sys/epoll.rs +++ b/src/sys/epoll.rs @@ -89,7 +89,7 @@ pub fn epoll_create() -> Result { #[inline] pub fn epoll_create1(flags: EpollFdFlag) -> Result { - let res = unsafe { ffi::epoll_create1(flags.bits() | EPOLL_CLOEXEC.bits) }; + let res = unsafe { ffi::epoll_create1(flags.bits()) }; Errno::result(res) } -- cgit v1.2.3 From dfcf1266769a26004d956aecefa979a0a39a4962 Mon Sep 17 00:00:00 2001 From: Hiroki Noda Date: Sat, 23 Jul 2016 14:33:12 +0900 Subject: Use libc's declarations --- src/sys/epoll.rs | 65 +++++++------------------------------------------------- 1 file changed, 8 insertions(+), 57 deletions(-) (limited to 'src') diff --git a/src/sys/epoll.rs b/src/sys/epoll.rs index af938bd3..9b30f993 100644 --- a/src/sys/epoll.rs +++ b/src/sys/epoll.rs @@ -1,26 +1,7 @@ use {Errno, Result}; -use libc::c_int; +use libc::{self, c_int}; use std::os::unix::io::RawFd; -mod ffi { - use libc::{c_int}; - use super::EpollEvent; - - extern { - pub fn epoll_create(size: c_int) -> c_int; - pub fn epoll_create1(flags: c_int) -> c_int; - pub fn epoll_ctl(epfd: c_int, op: c_int, fd: c_int, event: *const EpollEvent) -> c_int; - pub fn epoll_wait(epfd: c_int, events: *mut EpollEvent, max_events: c_int, timeout: c_int) -> c_int; - } -} - -bitflags!( - flags EpollFdFlag: c_int { - const EPOLL_NONBLOCK = 0x800, - const EPOLL_CLOEXEC = 0x80000 - } -); - bitflags!( #[repr(C)] flags EpollEventKind: u32 { @@ -50,61 +31,31 @@ pub enum EpollOp { EpollCtlMod = 3 } -#[cfg(not(target_arch = "x86_64"))] -#[derive(Clone, Copy)] -#[repr(C)] -pub struct EpollEvent { - pub events: EpollEventKind, - pub data: u64 -} - -#[cfg(target_arch = "x86_64")] -#[derive(Clone, Copy)] -#[repr(C, packed)] -pub struct EpollEvent { - pub events: EpollEventKind, - pub data: u64 -} - -#[cfg(any(target_arch = "x86", target_arch = "x86_64"))] -#[test] -fn test_epoll_event_size() { - use std::mem::size_of; - assert_eq!(size_of::(), 12); -} - -#[cfg(target_arch = "arm")] -#[test] -fn test_epoll_event_size() { - use std::mem::size_of; - assert_eq!(size_of::(), 16); -} - #[inline] pub fn epoll_create() -> Result { - let res = unsafe { ffi::epoll_create(1024) }; + let res = unsafe { libc::epoll_create(1024) }; Errno::result(res) } #[inline] -pub fn epoll_create1(flags: EpollFdFlag) -> Result { - let res = unsafe { ffi::epoll_create1(flags.bits()) }; +pub fn epoll_create1(flags: c_int) -> Result { + let res = unsafe { libc::epoll_create1(flags) }; Errno::result(res) } #[inline] -pub fn epoll_ctl(epfd: RawFd, op: EpollOp, fd: RawFd, event: &EpollEvent) -> Result<()> { - let res = unsafe { ffi::epoll_ctl(epfd, op as c_int, fd, event as *const EpollEvent) }; +pub fn epoll_ctl(epfd: RawFd, op: EpollOp, fd: RawFd, event: &mut libc::epoll_event) -> Result<()> { + let res = unsafe { libc::epoll_ctl(epfd, op as c_int, fd, event as *mut libc::epoll_event) }; Errno::result(res).map(drop) } #[inline] -pub fn epoll_wait(epfd: RawFd, events: &mut [EpollEvent], timeout_ms: isize) -> Result { +pub fn epoll_wait(epfd: RawFd, events: &mut [libc::epoll_event], timeout_ms: isize) -> Result { let res = unsafe { - ffi::epoll_wait(epfd, events.as_mut_ptr(), events.len() as c_int, timeout_ms as c_int) + libc::epoll_wait(epfd, events.as_mut_ptr(), events.len() as c_int, timeout_ms as c_int) }; Errno::result(res).map(|r| r as usize) -- cgit v1.2.3 From da36438f5bef8f885fb3ea9269b73ba85df32a07 Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Wed, 31 Aug 2016 21:05:35 +0200 Subject: Readd EpollEvent --- src/sys/epoll.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/sys/epoll.rs b/src/sys/epoll.rs index 9b30f993..2cdc6638 100644 --- a/src/sys/epoll.rs +++ b/src/sys/epoll.rs @@ -31,6 +31,18 @@ pub enum EpollOp { EpollCtlMod = 3 } +#[derive(Clone, Copy)] +#[repr(C)] +pub struct EpollEvent { + event: libc::epoll_event, +} + +impl EpollEvent { + fn new(events: EpollEventKind, data: u64) -> EpollEvent { + EpollEvent { event: libc::epoll_event { events: events.bits(), u64: data } } + } +} + #[inline] pub fn epoll_create() -> Result { let res = unsafe { libc::epoll_create(1024) }; @@ -46,16 +58,16 @@ pub fn epoll_create1(flags: c_int) -> Result { } #[inline] -pub fn epoll_ctl(epfd: RawFd, op: EpollOp, fd: RawFd, event: &mut libc::epoll_event) -> Result<()> { - let res = unsafe { libc::epoll_ctl(epfd, op as c_int, fd, event as *mut libc::epoll_event) }; +pub fn epoll_ctl(epfd: RawFd, op: EpollOp, fd: RawFd, event: &mut EpollEvent) -> Result<()> { + let res = unsafe { libc::epoll_ctl(epfd, op as c_int, fd, &mut event.event) }; Errno::result(res).map(drop) } #[inline] -pub fn epoll_wait(epfd: RawFd, events: &mut [libc::epoll_event], timeout_ms: isize) -> Result { +pub fn epoll_wait(epfd: RawFd, events: &mut [EpollEvent], timeout_ms: isize) -> Result { let res = unsafe { - libc::epoll_wait(epfd, events.as_mut_ptr(), events.len() as c_int, timeout_ms as c_int) + libc::epoll_wait(epfd, events.as_mut_ptr() as *mut libc::epoll_event, events.len() as c_int, timeout_ms as c_int) }; Errno::result(res).map(|r| r as usize) -- cgit v1.2.3 From 584794d79125f65537d993ae8acd1ea9e981ad0a Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Thu, 8 Sep 2016 21:14:46 +0200 Subject: Add property readers to EpollEvent --- src/sys/epoll.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/sys/epoll.rs b/src/sys/epoll.rs index 2cdc6638..51f789a2 100644 --- a/src/sys/epoll.rs +++ b/src/sys/epoll.rs @@ -38,9 +38,17 @@ pub struct EpollEvent { } impl EpollEvent { - fn new(events: EpollEventKind, data: u64) -> EpollEvent { + pub fn new(events: EpollEventKind, data: u64) -> EpollEvent { EpollEvent { event: libc::epoll_event { events: events.bits(), u64: data } } } + + pub fn events(&self) -> EpollEventKind { + EpollEventKind::from_bits(self.event.events).unwrap() + } + + pub fn data(&self) -> u64 { + self.event.u64 + } } #[inline] -- cgit v1.2.3 From 0f9cc0589c7b31d3e7dd789834d128b1e5508c53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Fri, 9 Sep 2016 18:11:01 +0300 Subject: Add MSG_CMSG_CLOEXEC to MsgFlags on Linux Fixes #421. --- src/sys/socket/consts.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'src') diff --git a/src/sys/socket/consts.rs b/src/sys/socket/consts.rs index aeeea5c3..63eaf28a 100644 --- a/src/sys/socket/consts.rs +++ b/src/sys/socket/consts.rs @@ -98,6 +98,7 @@ mod os { const MSG_DONTWAIT = 0x0040, const MSG_EOR = 0x0080, const MSG_ERRQUEUE = 0x2000, + const MSG_CMSG_CLOEXEC = 0x40000000, } } -- cgit v1.2.3 From 71688a082a042d5c8313491bc5f3f88fcf42f854 Mon Sep 17 00:00:00 2001 From: Jack O'Connor Date: Wed, 14 Sep 2016 15:48:57 -0700 Subject: call pipe2 directly on Linux --- src/unistd.rs | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/unistd.rs b/src/unistd.rs index 74d74c9c..2f8aa523 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -1,8 +1,8 @@ //! Standard symbolic constants and types //! use {Errno, Error, Result, NixPath}; -use fcntl::{fcntl, OFlag, O_NONBLOCK, O_CLOEXEC, FD_CLOEXEC}; -use fcntl::FcntlArg::{F_SETFD, F_SETFL}; +use fcntl::{fcntl, OFlag, O_CLOEXEC, FD_CLOEXEC}; +use fcntl::FcntlArg::F_SETFD; 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, CStr, OsString}; @@ -360,6 +360,25 @@ pub fn pipe() -> Result<(RawFd, RawFd)> { } } +// libc only defines `pipe2` in `libc::notbsd`. +#[cfg(any(target_os = "linux", + target_os = "android", + target_os = "emscripten"))] +pub fn pipe2(flags: OFlag) -> Result<(RawFd, RawFd)> { + unsafe { + let mut fds: [c_int; 2] = mem::uninitialized(); + + let res = libc::pipe2(fds.as_mut_ptr(), flags.bits()); + + try!(Errno::result(res)); + + Ok((fds[0], fds[1])) + } +} + +#[cfg(not(any(target_os = "linux", + target_os = "android", + target_os = "emscripten")))] pub fn pipe2(flags: OFlag) -> Result<(RawFd, RawFd)> { unsafe { let mut fds: [c_int; 2] = mem::uninitialized(); @@ -374,7 +393,13 @@ pub fn pipe2(flags: OFlag) -> Result<(RawFd, RawFd)> { } } +#[cfg(not(any(target_os = "linux", + target_os = "android", + target_os = "emscripten")))] fn pipe2_setflags(fd1: RawFd, fd2: RawFd, flags: OFlag) -> Result<()> { + use fcntl::O_NONBLOCK; + use fcntl::FcntlArg::F_SETFL; + let mut res = Ok(0); if flags.contains(O_CLOEXEC) { -- cgit v1.2.3 From b28005f658129f9130d44ad63b38a74ff2b6e69d Mon Sep 17 00:00:00 2001 From: Jack O'Connor Date: Thu, 15 Sep 2016 09:52:38 -0700 Subject: make unsafe code more fine-grained in pipe2 --- src/unistd.rs | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/unistd.rs b/src/unistd.rs index 2f8aa523..1b2175ea 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -365,32 +365,28 @@ pub fn pipe() -> Result<(RawFd, RawFd)> { target_os = "android", target_os = "emscripten"))] pub fn pipe2(flags: OFlag) -> Result<(RawFd, RawFd)> { - unsafe { - let mut fds: [c_int; 2] = mem::uninitialized(); + let mut fds: [c_int; 2] = unsafe { mem::uninitialized() }; - let res = libc::pipe2(fds.as_mut_ptr(), flags.bits()); + let res = unsafe { libc::pipe2(fds.as_mut_ptr(), flags.bits()) }; - try!(Errno::result(res)); + try!(Errno::result(res)); - Ok((fds[0], fds[1])) - } + Ok((fds[0], fds[1])) } #[cfg(not(any(target_os = "linux", target_os = "android", target_os = "emscripten")))] pub fn pipe2(flags: OFlag) -> Result<(RawFd, RawFd)> { - unsafe { - let mut fds: [c_int; 2] = mem::uninitialized(); + let mut fds: [c_int; 2] = unsafe { mem::uninitialized() }; - let res = libc::pipe(fds.as_mut_ptr()); + let res = unsafe { libc::pipe(fds.as_mut_ptr()) }; - try!(Errno::result(res)); + try!(Errno::result(res)); - try!(pipe2_setflags(fds[0], fds[1], flags)); + try!(pipe2_setflags(fds[0], fds[1], flags)); - Ok((fds[0], fds[1])) - } + Ok((fds[0], fds[1])) } #[cfg(not(any(target_os = "linux", -- cgit v1.2.3 From c9edda389b5edba97165c887aea6f4e5d11541a2 Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Thu, 15 Sep 2016 22:07:43 +0200 Subject: Add EpollCrateFlags bitflag type. --- src/sys/epoll.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/sys/epoll.rs b/src/sys/epoll.rs index 51f789a2..8e18d857 100644 --- a/src/sys/epoll.rs +++ b/src/sys/epoll.rs @@ -31,6 +31,12 @@ pub enum EpollOp { EpollCtlMod = 3 } +libc_bitflags!{ + flags EpollCreateFlags: c_int { + EPOLL_CLOEXEC, + } +} + #[derive(Clone, Copy)] #[repr(C)] pub struct EpollEvent { @@ -59,8 +65,8 @@ pub fn epoll_create() -> Result { } #[inline] -pub fn epoll_create1(flags: c_int) -> Result { - let res = unsafe { libc::epoll_create1(flags) }; +pub fn epoll_create1(flags: EpollCreateFlags) -> Result { + let res = unsafe { libc::epoll_create1(flags.bits()) }; Errno::result(res) } -- cgit v1.2.3 From 2b0c9919acf1aa4abf5f1b1d998c4d7404284fcc Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Thu, 15 Sep 2016 22:08:04 +0200 Subject: Rename EpollEventKind to EpollFlags, according to convention. --- src/sys/epoll.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/sys/epoll.rs b/src/sys/epoll.rs index 8e18d857..9774318f 100644 --- a/src/sys/epoll.rs +++ b/src/sys/epoll.rs @@ -4,7 +4,7 @@ use std::os::unix::io::RawFd; bitflags!( #[repr(C)] - flags EpollEventKind: u32 { + flags EpollFlags: u32 { const EPOLLIN = 0x001, const EPOLLPRI = 0x002, const EPOLLOUT = 0x004, @@ -44,12 +44,12 @@ pub struct EpollEvent { } impl EpollEvent { - pub fn new(events: EpollEventKind, data: u64) -> EpollEvent { + pub fn new(events: EpollFlags, data: u64) -> EpollEvent { EpollEvent { event: libc::epoll_event { events: events.bits(), u64: data } } } - pub fn events(&self) -> EpollEventKind { - EpollEventKind::from_bits(self.event.events).unwrap() + pub fn events(&self) -> EpollFlags { + EpollFlags::from_bits(self.event.events).unwrap() } pub fn data(&self) -> u64 { -- cgit v1.2.3 From 80df83be2024e710c63cd0676ec46f1d2eafc6b0 Mon Sep 17 00:00:00 2001 From: Philipp Keller Date: Fri, 16 Sep 2016 17:19:14 +0200 Subject: fixing build for aarch64-unknown-linux-gnu (which expects the pointer to be *mut u8 whereas x86_64 and i686 expect it to be *mut i8) --- src/unistd.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/unistd.rs b/src/unistd.rs index 2eb218b3..2b1912c1 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -537,7 +537,7 @@ pub fn sleep(seconds: libc::c_uint) -> c_uint { pub fn mkstemp(template: &P) -> Result<(RawFd, PathBuf)> { let res = template.with_nix_path(|path| { let mut path_copy = path.to_bytes_with_nul().to_owned(); - let p: *mut i8 = path_copy.as_mut_ptr() as *mut i8; + let p = path_copy.as_mut_ptr() as *mut _; unsafe { (libc::mkstemp(p), OsStr::from_bytes(CStr::from_ptr(p).to_bytes())) } -- cgit v1.2.3 From 26e1b6d78563e75977c8023179b490516fb2516b Mon Sep 17 00:00:00 2001 From: Mathias Svensson Date: Sat, 17 Sep 2016 19:23:49 +0200 Subject: Fixed a bug where UnixAddr::new_abstract forgot to count the null-byte. --- src/sys/socket/addr.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/sys/socket/addr.rs b/src/sys/socket/addr.rs index 22970d8b..e3c1401c 100644 --- a/src/sys/socket/addr.rs +++ b/src/sys/socket/addr.rs @@ -391,7 +391,7 @@ impl UnixAddr { .. mem::zeroed() }; - if path.len() > ret.sun_path.len() { + if path.len() + 1 > ret.sun_path.len() { return Err(Error::Sys(Errno::ENAMETOOLONG)); } @@ -401,7 +401,7 @@ impl UnixAddr { ret.sun_path.as_mut_ptr().offset(1) as *mut u8, path.len()); - Ok(UnixAddr(ret, path.len())) + Ok(UnixAddr(ret, path.len() + 1)) } } -- cgit v1.2.3 From 1f2d896b9692f138e4c0cfa116deb4d34ab893ea Mon Sep 17 00:00:00 2001 From: Tim Ryan Date: Sat, 17 Sep 2016 23:50:58 +0200 Subject: Removes SIGSTKFLT when cross-compiling to MIPS. --- src/sys/signal.rs | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/sys/signal.rs b/src/sys/signal.rs index 18827332..bdc25b47 100644 --- a/src/sys/signal.rs +++ b/src/sys/signal.rs @@ -28,7 +28,7 @@ pub enum Signal { SIGPIPE = libc::SIGPIPE, SIGALRM = libc::SIGALRM, SIGTERM = libc::SIGTERM, - #[cfg(any(target_os = "linux", target_os = "android", target_os = "emscripten"))] + #[cfg(all(any(target_os = "linux", target_os = "android", target_os = "emscripten"), not(target_arch = "mips")))] SIGSTKFLT = libc::SIGSTKFLT, SIGCHLD = libc::SIGCHLD, SIGCONT = libc::SIGCONT, @@ -54,7 +54,7 @@ pub enum Signal { pub use self::Signal::*; -#[cfg(any(target_os = "linux", target_os = "android", target_os = "emscripten"))] +#[cfg(all(any(target_os = "linux", target_os = "android", target_os = "emscripten"), not(target_arch = "mips")))] const SIGNALS: [Signal; 31] = [ SIGHUP, SIGINT, @@ -87,6 +87,38 @@ const SIGNALS: [Signal; 31] = [ SIGIO, SIGPWR, SIGSYS]; +#[cfg(all(any(target_os = "linux", target_os = "android", target_os = "emscripten"), target_arch = "mips"))] +const SIGNALS: [Signal; 30] = [ + SIGHUP, + SIGINT, + SIGQUIT, + SIGILL, + SIGTRAP, + SIGABRT, + SIGBUS, + SIGFPE, + SIGKILL, + SIGUSR1, + SIGSEGV, + SIGUSR2, + SIGPIPE, + SIGALRM, + SIGTERM, + SIGCHLD, + SIGCONT, + SIGSTOP, + SIGTSTP, + SIGTTIN, + SIGTTOU, + 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, -- cgit v1.2.3 From 95ac98a0b5bcfcbb80b0bd77d0f5c9d8126745cb Mon Sep 17 00:00:00 2001 From: Philipp Keller Date: Tue, 27 Sep 2016 21:45:26 +0200 Subject: move path outside closure to avoid use after free, restructed for easier readability --- src/unistd.rs | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/unistd.rs b/src/unistd.rs index 2b1912c1..53ba3344 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -535,21 +535,13 @@ pub fn sleep(seconds: libc::c_uint) -> c_uint { /// ``` #[inline] pub fn mkstemp(template: &P) -> Result<(RawFd, PathBuf)> { - let res = template.with_nix_path(|path| { - let mut path_copy = path.to_bytes_with_nul().to_owned(); - let p = path_copy.as_mut_ptr() as *mut _; - unsafe { - (libc::mkstemp(p), OsStr::from_bytes(CStr::from_ptr(p).to_bytes())) - } - }); - match res { - Ok((fd, pathname)) => { - try!(Errno::result(fd)); - Ok((fd, PathBuf::from(pathname).to_owned())) - } - Err(e) => { - Err(e) - } + let mut path = try!(template.with_nix_path(|path| {path.to_bytes_with_nul().to_owned()})); + let p = path.as_mut_ptr() as *mut _; + unsafe { + let fd = libc::mkstemp(p); + let pathname = OsStr::from_bytes(CStr::from_ptr(p).to_bytes()); + try!(Errno::result(fd)); + Ok((fd, PathBuf::from(pathname).to_owned())) } } -- cgit v1.2.3 From 60a70c6b1e9fa201b686a6121650f27caa6ae932 Mon Sep 17 00:00:00 2001 From: Philipp Keller Date: Tue, 27 Sep 2016 22:23:36 +0200 Subject: Remove double copy of array (to_owned() and PathBuf::from), use OsString::from_vec on existing path var rather than construct string from pointer --- src/unistd.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/unistd.rs b/src/unistd.rs index 53ba3344..4d4e7529 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -5,8 +5,8 @@ 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, mode_t}; use std::mem; -use std::ffi::{CString, CStr, OsString, OsStr}; -use std::os::unix::ffi::{OsStringExt, OsStrExt}; +use std::ffi::{CString, CStr, OsString}; +use std::os::unix::ffi::{OsStringExt}; use std::os::unix::io::RawFd; use std::path::{PathBuf}; use void::Void; @@ -537,12 +537,11 @@ pub fn sleep(seconds: libc::c_uint) -> c_uint { pub fn mkstemp(template: &P) -> Result<(RawFd, PathBuf)> { let mut path = try!(template.with_nix_path(|path| {path.to_bytes_with_nul().to_owned()})); let p = path.as_mut_ptr() as *mut _; - unsafe { - let fd = libc::mkstemp(p); - let pathname = OsStr::from_bytes(CStr::from_ptr(p).to_bytes()); - try!(Errno::result(fd)); - Ok((fd, PathBuf::from(pathname).to_owned())) - } + let fd = unsafe { libc::mkstemp(p) }; + path.pop(); // drop the trailing nul + let pathname = OsString::from_vec(path); + try!(Errno::result(fd)); + Ok((fd, PathBuf::from(pathname))) } #[cfg(any(target_os = "linux", target_os = "android"))] -- cgit v1.2.3 From 7c0f5e3598c2489d53259253842c7a74ece773cc Mon Sep 17 00:00:00 2001 From: Philipp Keller Date: Wed, 28 Sep 2016 07:02:15 +0200 Subject: Add debug_assert ensuring popped byte is nul --- src/unistd.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/unistd.rs b/src/unistd.rs index 4d4e7529..4226b2f7 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -538,7 +538,8 @@ pub fn mkstemp(template: &P) -> Result<(RawFd, PathBuf)> { let mut path = try!(template.with_nix_path(|path| {path.to_bytes_with_nul().to_owned()})); let p = path.as_mut_ptr() as *mut _; let fd = unsafe { libc::mkstemp(p) }; - path.pop(); // drop the trailing nul + let last = path.pop(); // drop the trailing nul + debug_assert!(last == Some(b'\0')); let pathname = OsString::from_vec(path); try!(Errno::result(fd)); Ok((fd, PathBuf::from(pathname))) -- cgit v1.2.3