diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/dir.rs | 3 | ||||
-rw-r--r-- | src/errno.rs | 3 | ||||
-rw-r--r-- | src/fcntl.rs | 5 | ||||
-rw-r--r-- | src/kmod.rs | 1 | ||||
-rw-r--r-- | src/lib.rs | 6 | ||||
-rw-r--r-- | src/mount/bsd.rs | 2 | ||||
-rw-r--r-- | src/mount/linux.rs | 1 | ||||
-rw-r--r-- | src/mount/mod.rs | 2 | ||||
-rw-r--r-- | src/mqueue.rs | 1 | ||||
-rw-r--r-- | src/pty.rs | 12 | ||||
-rw-r--r-- | src/sched.rs | 8 | ||||
-rw-r--r-- | src/sys/aio.rs | 44 | ||||
-rw-r--r-- | src/sys/epoll.rs | 4 | ||||
-rw-r--r-- | src/sys/eventfd.rs | 1 | ||||
-rw-r--r-- | src/sys/memfd.rs | 1 | ||||
-rw-r--r-- | src/sys/mman.rs | 6 | ||||
-rw-r--r-- | src/sys/personality.rs | 4 | ||||
-rw-r--r-- | src/sys/ptrace/linux.rs | 17 | ||||
-rw-r--r-- | src/sys/reboot.rs | 5 | ||||
-rw-r--r-- | src/sys/select.rs | 2 | ||||
-rw-r--r-- | src/sys/signal.rs | 6 | ||||
-rw-r--r-- | src/sys/signalfd.rs | 5 | ||||
-rw-r--r-- | src/sys/socket/addr.rs | 16 | ||||
-rw-r--r-- | src/sys/socket/mod.rs | 11 | ||||
-rw-r--r-- | src/sys/statvfs.rs | 2 | ||||
-rw-r--r-- | src/sys/timerfd.rs | 11 | ||||
-rw-r--r-- | src/time.rs | 4 | ||||
-rw-r--r-- | src/ucontext.rs | 1 | ||||
-rw-r--r-- | src/unistd.rs | 45 |
29 files changed, 100 insertions, 129 deletions
@@ -84,7 +84,7 @@ impl AsRawFd for Dir { impl Drop for Dir { fn drop(&mut self) { let e = Errno::result(unsafe { libc::closedir(self.0.as_ptr()) }); - if !std::thread::panicking() && e == Err(Error::from(Errno::EBADF)) { + if !std::thread::panicking() && e == Err(Errno::EBADF) { panic!("Closing an invalid file descriptor!"); }; } @@ -211,6 +211,7 @@ impl Entry { target_os = "linux", target_os = "macos", target_os = "solaris")))] + #[allow(clippy::useless_conversion)] // Not useless on all OSes pub fn ino(&self) -> u64 { u64::from(self.0.d_fileno) } diff --git a/src/errno.rs b/src/errno.rs index 2be3179c..3da246e8 100644 --- a/src/errno.rs +++ b/src/errno.rs @@ -72,8 +72,9 @@ impl Errno { since = "0.22.0", note = "It's a no-op now; just delete it." )] + #[allow(clippy::wrong_self_convention)] // False positive pub fn from_errno(errno: Errno) -> Error { - Error::from(errno) + errno } /// Create a new invalid argument error (`EINVAL`) diff --git a/src/fcntl.rs b/src/fcntl.rs index 0a50e748..aded27b4 100644 --- a/src/fcntl.rs +++ b/src/fcntl.rs @@ -325,7 +325,7 @@ fn inner_readlink<P: ?Sized + NixPath>(dirfd: Option<RawFd>, path: &P) -> Result Some(next_size) => try_size = next_size, // It's absurd that this would happen, but handle it sanely // anyway. - None => break Err(super::Error::from(Errno::ENAMETOOLONG)), + None => break Err(Errno::ENAMETOOLONG), } } } @@ -646,7 +646,6 @@ pub fn fallocate( ))] mod posix_fadvise { use crate::errno::Errno; - use libc; use std::os::unix::io::RawFd; use crate::Result; @@ -687,6 +686,6 @@ pub fn posix_fallocate(fd: RawFd, offset: libc::off_t, len: libc::off_t) -> Resu match Errno::result(res) { Err(err) => Err(err), Ok(0) => Ok(()), - Ok(errno) => Err(crate::Error::from(Errno::from_i32(errno))), + Ok(errno) => Err(Errno::from_i32(errno)), } } diff --git a/src/kmod.rs b/src/kmod.rs index 89e577a7..c42068c7 100644 --- a/src/kmod.rs +++ b/src/kmod.rs @@ -2,7 +2,6 @@ //! //! For more details see -use libc; use std::ffi::CStr; use std::os::unix::io::AsRawFd; @@ -160,7 +160,7 @@ impl NixPath for CStr { where F: FnOnce(&CStr) -> T { // Equivalence with the [u8] impl. if self.len() >= PATH_MAX as usize { - return Err(Error::from(Errno::ENAMETOOLONG)) + return Err(Errno::ENAMETOOLONG) } Ok(f(self)) @@ -181,11 +181,11 @@ impl NixPath for [u8] { let mut buf = [0u8; PATH_MAX as usize]; if self.len() >= PATH_MAX as usize { - return Err(Error::from(Errno::ENAMETOOLONG)) + return Err(Errno::ENAMETOOLONG) } match self.iter().position(|b| *b == 0) { - Some(_) => Err(Error::from(Errno::EINVAL)), + Some(_) => Err(Errno::EINVAL), None => { unsafe { // TODO: Replace with bytes::copy_memory. rust-lang/rust#24028 diff --git a/src/mount/bsd.rs b/src/mount/bsd.rs index 9913fc20..23331a0a 100644 --- a/src/mount/bsd.rs +++ b/src/mount/bsd.rs @@ -383,7 +383,7 @@ impl<'a> Nmount<'a> { Some(CStr::from_bytes_with_nul(sl).unwrap()) } }; - Err(NmountError::new(error.into(), errmsg)) + Err(NmountError::new(error, errmsg)) } } } diff --git a/src/mount/linux.rs b/src/mount/linux.rs index edb8afbd..4cb2fa54 100644 --- a/src/mount/linux.rs +++ b/src/mount/linux.rs @@ -1,3 +1,4 @@ +#![allow(missing_docs)] use libc::{self, c_ulong, c_int}; use crate::{Result, NixPath}; use crate::errno::Errno; diff --git a/src/mount/mod.rs b/src/mount/mod.rs index 00303b6a..14bf2a96 100644 --- a/src/mount/mod.rs +++ b/src/mount/mod.rs @@ -1,10 +1,8 @@ //! Mount file systems #[cfg(any(target_os = "android", target_os = "linux"))] -#[allow(missing_docs)] mod linux; #[cfg(any(target_os = "android", target_os = "linux"))] -#[allow(missing_docs)] pub use self::linux::*; #[cfg(any(target_os = "dragonfly", diff --git a/src/mqueue.rs b/src/mqueue.rs index 8598eaf2..34fd8027 100644 --- a/src/mqueue.rs +++ b/src/mqueue.rs @@ -155,6 +155,7 @@ pub fn mq_setattr(mqd: mqd_t, newattr: &MqAttr) -> Result<MqAttr> { /// Convenience function. /// Sets the `O_NONBLOCK` attribute for a given message queue descriptor /// Returns the old attributes +#[allow(clippy::useless_conversion)] // Not useless on all OSes pub fn mq_set_nonblock(mqd: mqd_t) -> Result<MqAttr> { let oldattr = mq_getattr(mqd)?; let newattr = MqAttr::new(mq_attr_member_t::from(MQ_OFlag::O_NONBLOCK.bits()), @@ -10,7 +10,7 @@ use std::os::unix::prelude::*; use crate::sys::termios::Termios; use crate::unistd::{self, ForkResult, Pid}; -use crate::{Result, Error, fcntl}; +use crate::{Result, fcntl}; use crate::errno::Errno; /// Representation of a master/slave pty pair @@ -99,7 +99,7 @@ impl io::Write for PtyMaster { #[inline] pub fn grantpt(fd: &PtyMaster) -> Result<()> { if unsafe { libc::grantpt(fd.as_raw_fd()) } < 0 { - return Err(Error::from(Errno::last())); + return Err(Errno::last()); } Ok(()) @@ -145,7 +145,7 @@ pub fn posix_openpt(flags: fcntl::OFlag) -> Result<PtyMaster> { }; if fd < 0 { - return Err(Error::from(Errno::last())); + return Err(Errno::last()); } Ok(PtyMaster(fd)) @@ -171,7 +171,7 @@ pub fn posix_openpt(flags: fcntl::OFlag) -> Result<PtyMaster> { pub unsafe fn ptsname(fd: &PtyMaster) -> Result<String> { let name_ptr = libc::ptsname(fd.as_raw_fd()); if name_ptr.is_null() { - return Err(Error::from(Errno::last())); + return Err(Errno::last()); } let name = CStr::from_ptr(name_ptr); @@ -195,7 +195,7 @@ pub fn ptsname_r(fd: &PtyMaster) -> Result<String> { let cname = unsafe { let cap = name_buf.capacity(); if libc::ptsname_r(fd.as_raw_fd(), name_buf_ptr, cap) != 0 { - return Err(Error::last()); + return Err(crate::Error::last()); } CStr::from_ptr(name_buf.as_ptr()) }; @@ -213,7 +213,7 @@ pub fn ptsname_r(fd: &PtyMaster) -> Result<String> { #[inline] pub fn unlockpt(fd: &PtyMaster) -> Result<()> { if unsafe { libc::unlockpt(fd.as_raw_fd()) } < 0 { - return Err(Error::from(Errno::last())); + return Err(Errno::last()); } Ok(()) diff --git a/src/sched.rs b/src/sched.rs index 69436fdf..c2dd7b84 100644 --- a/src/sched.rs +++ b/src/sched.rs @@ -15,7 +15,7 @@ mod sched_linux_like { use std::option::Option; use std::os::unix::io::RawFd; use crate::unistd::Pid; - use crate::{Error, Result}; + use crate::Result; // For some functions taking with a parameter of type CloneFlags, // only a subset of these flags have an effect. @@ -109,7 +109,7 @@ mod sched_linux_like { /// `field` is the CPU id to test pub fn is_set(&self, field: usize) -> Result<bool> { if field >= CpuSet::count() { - Err(Error::from(Errno::EINVAL)) + Err(Errno::EINVAL) } else { Ok(unsafe { libc::CPU_ISSET(field, &self.cpu_set) }) } @@ -119,7 +119,7 @@ mod sched_linux_like { /// `field` is the CPU id to add pub fn set(&mut self, field: usize) -> Result<()> { if field >= CpuSet::count() { - Err(Error::from(Errno::EINVAL)) + Err(Errno::EINVAL) } else { unsafe { libc::CPU_SET(field, &mut self.cpu_set); } Ok(()) @@ -130,7 +130,7 @@ mod sched_linux_like { /// `field` is the CPU id to remove pub fn unset(&mut self, field: usize) -> Result<()> { if field >= CpuSet::count() { - Err(Error::from(Errno::EINVAL)) + Err(Errno::EINVAL) } else { unsafe { libc::CPU_CLR(field, &mut self.cpu_set);} Ok(()) diff --git a/src/sys/aio.rs b/src/sys/aio.rs index fcee28c5..e64a2a82 100644 --- a/src/sys/aio.rs +++ b/src/sys/aio.rs @@ -21,7 +21,7 @@ //! [`aio_cancel_all`](fn.aio_cancel_all.html), though the operating system may //! not support this for all filesystems and devices. -use crate::{Error, Result}; +use crate::Result; use crate::errno::Errno; use std::os::unix::io::RawFd; use libc::{c_void, off_t, size_t}; @@ -148,15 +148,13 @@ impl<'a> AioCb<'a> { /// # use std::{thread, time}; /// # use std::os::unix::io::AsRawFd; /// # use tempfile::tempfile; - /// # fn main() { /// let f = tempfile().unwrap(); /// let mut aiocb = AioCb::from_fd( f.as_raw_fd(), 0, SigevNone); /// aiocb.fsync(AioFsyncMode::O_SYNC).expect("aio_fsync failed early"); - /// while (aiocb.error() == Err(Error::from(Errno::EINPROGRESS))) { + /// while (aiocb.error() == Err(Errno::EINPROGRESS)) { /// thread::sleep(time::Duration::from_millis(10)); /// } /// aiocb.aio_return().expect("aio_fsync failed late"); - /// # } /// ``` pub fn from_fd(fd: RawFd, prio: libc::c_int, sigev_notify: SigevNotify) -> Pin<Box<AioCb<'a>>> { @@ -229,7 +227,6 @@ impl<'a> AioCb<'a> { /// # use std::io::Write; /// # use std::os::unix::io::AsRawFd; /// # use tempfile::tempfile; - /// # fn main() { /// const INITIAL: &[u8] = b"abcdef123456"; /// const LEN: usize = 4; /// let mut rbuf = vec![0; LEN]; @@ -243,13 +240,12 @@ impl<'a> AioCb<'a> { /// SigevNotify::SigevNone, /// LioOpcode::LIO_NOP); /// aiocb.read().unwrap(); - /// while (aiocb.error() == Err(Error::from(Errno::EINPROGRESS))) { + /// while (aiocb.error() == Err(Errno::EINPROGRESS)) { /// thread::sleep(time::Duration::from_millis(10)); /// } /// assert_eq!(aiocb.aio_return().unwrap() as usize, LEN); /// } /// assert_eq!(rbuf, b"cdef"); - /// # } /// ``` pub fn from_mut_slice(fd: RawFd, offs: off_t, buf: &'a mut [u8], prio: libc::c_int, sigev_notify: SigevNotify, @@ -404,7 +400,6 @@ impl<'a> AioCb<'a> { /// # use std::{thread, time}; /// # use std::os::unix::io::AsRawFd; /// # use tempfile::tempfile; - /// # fn main() { /// const WBUF: &[u8] = b"abcdef123456"; /// let mut f = tempfile().unwrap(); /// let mut aiocb = AioCb::from_slice( f.as_raw_fd(), @@ -414,11 +409,10 @@ impl<'a> AioCb<'a> { /// SigevNotify::SigevNone, /// LioOpcode::LIO_NOP); /// aiocb.write().unwrap(); - /// while (aiocb.error() == Err(Error::from(Errno::EINPROGRESS))) { + /// while (aiocb.error() == Err(Errno::EINPROGRESS)) { /// thread::sleep(time::Duration::from_millis(10)); /// } /// assert_eq!(aiocb.aio_return().unwrap() as usize, WBUF.len()); - /// # } /// ``` // Note: another solution to the problem of writing const buffers would be // to genericize AioCb for both &mut [u8] and &[u8] buffers. AioCb::read @@ -480,7 +474,6 @@ impl<'a> AioCb<'a> { /// # use std::io::Write; /// # use std::os::unix::io::AsRawFd; /// # use tempfile::tempfile; - /// # fn main() { /// let wbuf = b"CDEF"; /// let mut f = tempfile().unwrap(); /// let mut aiocb = AioCb::from_slice( f.as_raw_fd(), @@ -492,13 +485,12 @@ impl<'a> AioCb<'a> { /// aiocb.write().unwrap(); /// let cs = aiocb.cancel().unwrap(); /// if cs == AioCancelStat::AioNotCanceled { - /// while (aiocb.error() == Err(Error::from(Errno::EINPROGRESS))) { + /// while (aiocb.error() == Err(Errno::EINPROGRESS)) { /// thread::sleep(time::Duration::from_millis(10)); /// } /// } /// // Must call `aio_return`, but ignore the result /// let _ = aiocb.aio_return(); - /// # } /// ``` /// /// # References @@ -513,7 +505,7 @@ impl<'a> AioCb<'a> { libc::AIO_CANCELED => Ok(AioCancelStat::AioCanceled), libc::AIO_NOTCANCELED => Ok(AioCancelStat::AioNotCanceled), libc::AIO_ALLDONE => Ok(AioCancelStat::AioAllDone), - -1 => Err(Error::from(Errno::last())), + -1 => Err(Errno::last()), _ => panic!("unknown aio_cancel return value") } } @@ -524,8 +516,8 @@ impl<'a> AioCb<'a> { }; match r { 0 => Ok(()), - num if num > 0 => Err(Error::from(Errno::from_i32(num))), - -1 => Err(Error::from(Errno::last())), + num if num > 0 => Err(Errno::from_i32(num)), + -1 => Err(Errno::last()), num => panic!("unknown aio_error return value {:?}", num) } } @@ -548,7 +540,6 @@ impl<'a> AioCb<'a> { /// # use std::{thread, time}; /// # use std::os::unix::io::AsRawFd; /// # use tempfile::tempfile; - /// # fn main() { /// const WBUF: &[u8] = b"abcdef123456"; /// let mut f = tempfile().unwrap(); /// let mut aiocb = AioCb::from_slice( f.as_raw_fd(), @@ -558,11 +549,10 @@ impl<'a> AioCb<'a> { /// SigevNotify::SigevNone, /// LioOpcode::LIO_NOP); /// aiocb.write().unwrap(); - /// while (aiocb.error() == Err(Error::from(Errno::EINPROGRESS))) { + /// while (aiocb.error() == Err(Errno::EINPROGRESS)) { /// thread::sleep(time::Duration::from_millis(10)); /// } /// assert_eq!(aiocb.aio_return().unwrap() as usize, WBUF.len()); - /// # } /// ``` /// /// # References @@ -711,7 +701,6 @@ impl<'a> AioCb<'a> { /// # use std::io::Write; /// # use std::os::unix::io::AsRawFd; /// # use tempfile::tempfile; -/// # fn main() { /// let wbuf = b"CDEF"; /// let mut f = tempfile().unwrap(); /// let mut aiocb = AioCb::from_slice( f.as_raw_fd(), @@ -723,13 +712,12 @@ impl<'a> AioCb<'a> { /// aiocb.write().unwrap(); /// let cs = aio_cancel_all(f.as_raw_fd()).unwrap(); /// if cs == AioCancelStat::AioNotCanceled { -/// while (aiocb.error() == Err(Error::from(Errno::EINPROGRESS))) { +/// while (aiocb.error() == Err(Errno::EINPROGRESS)) { /// thread::sleep(time::Duration::from_millis(10)); /// } /// } /// // Must call `aio_return`, but ignore the result /// let _ = aiocb.aio_return(); -/// # } /// ``` /// /// # References @@ -740,7 +728,7 @@ pub fn aio_cancel_all(fd: RawFd) -> Result<AioCancelStat> { libc::AIO_CANCELED => Ok(AioCancelStat::AioCanceled), libc::AIO_NOTCANCELED => Ok(AioCancelStat::AioNotCanceled), libc::AIO_ALLDONE => Ok(AioCancelStat::AioAllDone), - -1 => Err(Error::from(Errno::last())), + -1 => Err(Errno::last()), _ => panic!("unknown aio_cancel return value") } } @@ -759,7 +747,6 @@ pub fn aio_cancel_all(fd: RawFd) -> Result<AioCancelStat> { /// # use nix::sys::signal::SigevNotify; /// # use std::os::unix::io::AsRawFd; /// # use tempfile::tempfile; -/// # fn main() { /// const WBUF: &[u8] = b"abcdef123456"; /// let mut f = tempfile().unwrap(); /// let mut aiocb = AioCb::from_slice( f.as_raw_fd(), @@ -771,7 +758,6 @@ pub fn aio_cancel_all(fd: RawFd) -> Result<AioCancelStat> { /// aiocb.write().unwrap(); /// aio_suspend(&[aiocb.as_ref()], None).expect("aio_suspend failed"); /// assert_eq!(aiocb.aio_return().unwrap() as usize, WBUF.len()); -/// # } /// ``` /// # References /// @@ -876,7 +862,6 @@ impl<'a> LioCb<'a> { /// # use nix::sys::signal::SigevNotify; /// # use std::os::unix::io::AsRawFd; /// # use tempfile::tempfile; - /// # fn main() { /// const WBUF: &[u8] = b"abcdef123456"; /// let mut f = tempfile().unwrap(); /// let mut liocb = LioCbBuilder::with_capacity(1) @@ -891,7 +876,6 @@ impl<'a> LioCb<'a> { /// liocb.listio(LioMode::LIO_WAIT, /// SigevNotify::SigevNone).unwrap(); /// assert_eq!(liocb.aio_return(0).unwrap() as usize, WBUF.len()); - /// # } /// ``` /// /// # References @@ -936,7 +920,6 @@ impl<'a> LioCb<'a> { /// # use std::os::unix::io::AsRawFd; /// # use std::{thread, time}; /// # use tempfile::tempfile; - /// # fn main() { /// const WBUF: &[u8] = b"abcdef123456"; /// let mut f = tempfile().unwrap(); /// let mut liocb = LioCbBuilder::with_capacity(1) @@ -949,13 +932,12 @@ impl<'a> LioCb<'a> { /// LioOpcode::LIO_WRITE /// ).finish(); /// let mut err = liocb.listio(LioMode::LIO_WAIT, SigevNotify::SigevNone); - /// while err == Err(Error::from(Errno::EIO)) || - /// err == Err(Error::from(Errno::EAGAIN)) { + /// while err == Err(Errno::EIO) || + /// err == Err(Errno::EAGAIN) { /// thread::sleep(time::Duration::from_millis(10)); /// err = liocb.listio_resubmit(LioMode::LIO_WAIT, SigevNotify::SigevNone); /// } /// assert_eq!(liocb.aio_return(0).unwrap() as usize, WBUF.len()); - /// # } /// ``` /// /// # References diff --git a/src/sys/epoll.rs b/src/sys/epoll.rs index 9f68d5ce..6bc2a253 100644 --- a/src/sys/epoll.rs +++ b/src/sys/epoll.rs @@ -1,4 +1,4 @@ -use crate::{Error, Result}; +use crate::Result; use crate::errno::Errno; use libc::{self, c_int}; use std::os::unix::io::RawFd; @@ -86,7 +86,7 @@ pub fn epoll_ctl<'a, T>(epfd: RawFd, op: EpollOp, fd: RawFd, event: T) -> Result { let mut event: Option<&mut EpollEvent> = event.into(); if event.is_none() && op != EpollOp::EpollCtlDel { - Err(Error::from(Errno::EINVAL)) + Err(Errno::EINVAL) } else { let res = unsafe { if let Some(ref mut event) = event { diff --git a/src/sys/eventfd.rs b/src/sys/eventfd.rs index baaaa89d..c54f952f 100644 --- a/src/sys/eventfd.rs +++ b/src/sys/eventfd.rs @@ -1,4 +1,3 @@ -use libc; use std::os::unix::io::RawFd; use crate::Result; use crate::errno::Errno; diff --git a/src/sys/memfd.rs b/src/sys/memfd.rs index 51b7e6b1..0236eef6 100644 --- a/src/sys/memfd.rs +++ b/src/sys/memfd.rs @@ -1,4 +1,3 @@ -use libc; use std::os::unix::io::RawFd; use crate::Result; use crate::errno::Errno; diff --git a/src/sys/mman.rs b/src/sys/mman.rs index a6fcdd5a..30d53881 100644 --- a/src/sys/mman.rs +++ b/src/sys/mman.rs @@ -1,4 +1,4 @@ -use crate::{Error, Result}; +use crate::Result; #[cfg(not(target_os = "android"))] use crate::NixPath; use crate::errno::Errno; @@ -336,7 +336,7 @@ pub unsafe fn mmap(addr: *mut c_void, length: size_t, prot: ProtFlags, flags: Ma let ret = libc::mmap(addr, length, prot.bits(), flags.bits(), fd, offset); if ret == libc::MAP_FAILED { - Err(Error::from(Errno::last())) + Err(Errno::last()) } else { Ok(ret) } @@ -369,7 +369,7 @@ pub unsafe fn mremap( ); if ret == libc::MAP_FAILED { - Err(Error::from(Errno::last())) + Err(Errno::last()) } else { Ok(ret) } diff --git a/src/sys/personality.rs b/src/sys/personality.rs index 6548b654..b15956c4 100644 --- a/src/sys/personality.rs +++ b/src/sys/personality.rs @@ -39,7 +39,7 @@ pub fn get() -> Result<Persona> { libc::personality(0xFFFFFFFF) }; - Errno::result(res).map(|r| Persona::from_bits_truncate(r)) + Errno::result(res).map(Persona::from_bits_truncate) } /// Set the current process personality. @@ -66,5 +66,5 @@ pub fn set(persona: Persona) -> Result<Persona> { libc::personality(persona.bits() as c_ulong) }; - Errno::result(res).map(|r| Persona::from_bits_truncate(r)) + Errno::result(res).map(Persona::from_bits_truncate) } diff --git a/src/sys/ptrace/linux.rs b/src/sys/ptrace/linux.rs index 1bfde0d2..37236790 100644 --- a/src/sys/ptrace/linux.rs +++ b/src/sys/ptrace/linux.rs @@ -415,16 +415,15 @@ pub fn kill(pid: Pid) -> Result<()> { /// use nix::unistd::Pid; /// use nix::sys::signal::Signal; /// use nix::sys::wait::*; -/// fn main() { -/// // If a process changes state to the stopped state because of a SIGUSR1 -/// // signal, this will step the process forward and forward the user -/// // signal to the stopped process -/// match waitpid(Pid::from_raw(-1), None) { -/// Ok(WaitStatus::Stopped(pid, Signal::SIGUSR1)) => { -/// let _ = step(pid, Signal::SIGUSR1); -/// } -/// _ => {}, +/// +/// // If a process changes state to the stopped state because of a SIGUSR1 +/// // signal, this will step the process forward and forward the user +/// // signal to the stopped process +/// match waitpid(Pid::from_raw(-1), None) { +/// Ok(WaitStatus::Stopped(pid, Signal::SIGUSR1)) => { +/// let _ = step(pid, Signal::SIGUSR1); /// } +/// _ => {}, /// } /// ``` pub fn step<T: Into<Option<Signal>>>(pid: Pid, sig: T) -> Result<()> { diff --git a/src/sys/reboot.rs b/src/sys/reboot.rs index be5067a9..46ab68b6 100644 --- a/src/sys/reboot.rs +++ b/src/sys/reboot.rs @@ -1,8 +1,7 @@ //! Reboot/shutdown or enable/disable Ctrl-Alt-Delete. -use crate::{Error, Result}; +use crate::Result; use crate::errno::Errno; -use libc; use std::convert::Infallible; use std::mem::drop; @@ -27,7 +26,7 @@ pub fn reboot(how: RebootMode) -> Result<Infallible> { unsafe { libc::reboot(how as libc::c_int) }; - Err(Error::from(Errno::last())) + Err(Errno::last()) } /// Enable or disable the reboot keystroke (Ctrl-Alt-Delete). diff --git a/src/sys/select.rs b/src/sys/select.rs index 0a0e830d..5f3337f3 100644 --- a/src/sys/select.rs +++ b/src/sys/select.rs @@ -111,7 +111,7 @@ impl<'a> Iterator for Fds<'a> { type Item = RawFd; fn next(&mut self) -> Option<RawFd> { - while let Some(i) = self.range.next() { + for i in &mut self.range { if self.set.contains(i as RawFd) { return Some(i as RawFd); } diff --git a/src/sys/signal.rs b/src/sys/signal.rs index 1011930f..e8c79d33 100644 --- a/src/sys/signal.rs +++ b/src/sys/signal.rs @@ -156,7 +156,7 @@ impl FromStr for Signal { target_os = "fuchsia", target_os = "linux", target_os = "redox")))] "SIGINFO" => Signal::SIGINFO, - _ => return Err(Error::from(Errno::EINVAL)), + _ => return Err(Errno::EINVAL), }) } } @@ -779,7 +779,7 @@ pub unsafe fn signal(signal: Signal, handler: SigHandler) -> Result<SigHandler> SigHandler::SigIgn => libc::signal(signal, libc::SIG_IGN), SigHandler::Handler(handler) => libc::signal(signal, handler as libc::sighandler_t), #[cfg(not(target_os = "redox"))] - SigHandler::SigAction(_) => return Err(Error::from(Errno::ENOTSUP)), + SigHandler::SigAction(_) => return Err(Errno::ENOTSUP), }; Errno::result(res).map(|oldhandler| { match oldhandler { @@ -1091,7 +1091,7 @@ mod tests { #[test] fn test_from_str_invalid_value() { - let errval = Err(Error::from(Errno::EINVAL)); + let errval = Err(Errno::EINVAL); assert_eq!("NOSIGNAL".parse::<Signal>(), errval); assert_eq!("kill".parse::<Signal>(), errval); assert_eq!("9".parse::<Signal>(), errval); diff --git a/src/sys/signalfd.rs b/src/sys/signalfd.rs index 2ede8b8f..bc4a4522 100644 --- a/src/sys/signalfd.rs +++ b/src/sys/signalfd.rs @@ -15,9 +15,8 @@ //! //! Please note that signal discarding is not specific to `signalfd`, but also happens with regular //! signal handlers. -use libc; use crate::unistd; -use crate::{Error, Result}; +use crate::Result; use crate::errno::Errno; pub use crate::sys::signal::{self, SigSet}; pub use libc::signalfd_siginfo as siginfo; @@ -117,7 +116,7 @@ impl SignalFd { impl Drop for SignalFd { fn drop(&mut self) { let e = unistd::close(self.0); - if !std::thread::panicking() && e == Err(Error::from(Errno::EBADF)) { + if !std::thread::panicking() && e == Err(Errno::EBADF) { panic!("Closing an invalid file descriptor!"); }; } diff --git a/src/sys/socket/addr.rs b/src/sys/socket/addr.rs index 38dd4190..832a153f 100644 --- a/src/sys/socket/addr.rs +++ b/src/sys/socket/addr.rs @@ -1,5 +1,5 @@ use super::sa_family_t; -use crate::{Error, Result, NixPath}; +use crate::{Result, NixPath}; use crate::errno::Errno; use memoffset::offset_of; use std::{fmt, mem, net, ptr, slice}; @@ -270,6 +270,7 @@ pub enum InetAddr { } impl InetAddr { + #[allow(clippy::needless_update)] // It isn't needless on all OSes pub fn from_std(std: &net::SocketAddr) -> InetAddr { match *std { net::SocketAddr::V4(ref addr) => { @@ -293,6 +294,7 @@ impl InetAddr { } } + #[allow(clippy::needless_update)] // It isn't needless on all OSes pub fn new(ip: IpAddr, port: u16) -> InetAddr { match ip { IpAddr::V4(ref ip) => { @@ -565,7 +567,7 @@ impl UnixAddr { let bytes = cstr.to_bytes(); if bytes.len() >= ret.sun_path.len() { - return Err(Error::from(Errno::ENAMETOOLONG)); + return Err(Errno::ENAMETOOLONG); } ptr::copy_nonoverlapping(bytes.as_ptr(), @@ -592,7 +594,7 @@ impl UnixAddr { }; if path.len() >= ret.sun_path.len() { - return Err(Error::from(Errno::ENAMETOOLONG)); + return Err(Errno::ENAMETOOLONG); } // Abstract addresses are represented by sun_path[0] == @@ -751,7 +753,7 @@ impl SockAddr { #[cfg(any(target_os = "ios", target_os = "macos"))] pub fn new_sys_control(sockfd: RawFd, name: &str, unit: u32) -> Result<SockAddr> { - SysControlAddr::from_name(sockfd, name, unit).map(|a| SockAddr::SysControl(a)) + SysControlAddr::from_name(sockfd, name, unit).map(SockAddr::SysControl) } #[cfg(any(target_os = "android", target_os = "linux"))] @@ -1064,7 +1066,7 @@ pub mod sys_control { use libc::{self, c_uchar}; use std::{fmt, mem}; use std::os::unix::io::RawFd; - use crate::{Errno, Error, Result}; + use crate::{Errno, Result}; // FIXME: Move type into `libc` #[repr(C)] @@ -1075,7 +1077,7 @@ pub mod sys_control { pub ctl_name: [c_uchar; MAX_KCTL_NAME], } - const CTL_IOC_MAGIC: u8 = 'N' as u8; + const CTL_IOC_MAGIC: u8 = b'N'; const CTL_IOC_INFO: u8 = 3; const MAX_KCTL_NAME: usize = 96; @@ -1101,7 +1103,7 @@ pub mod sys_control { pub fn from_name(sockfd: RawFd, name: &str, unit: u32) -> Result<SysControlAddr> { if name.len() > MAX_KCTL_NAME { - return Err(Error::from(Errno::ENAMETOOLONG)); + return Err(Errno::ENAMETOOLONG); } let mut ctl_name = [0; MAX_KCTL_NAME]; diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs index 96328842..97eea3dc 100644 --- a/src/sys/socket/mod.rs +++ b/src/sys/socket/mod.rs @@ -2,7 +2,7 @@ //! //! [Further reading](https://man7.org/linux/man-pages/man7/socket.7.html) use cfg_if::cfg_if; -use crate::{Error, Result, errno::Errno}; +use crate::{Result, errno::Errno}; use libc::{self, c_void, c_int, iovec, socklen_t, size_t, CMSG_FIRSTHDR, CMSG_NXTHDR, CMSG_DATA, CMSG_LEN}; use memoffset::offset_of; @@ -311,9 +311,9 @@ cfg_if! { } } - impl Into<libc::ucred> for UnixCredentials { - fn into(self) -> libc::ucred { - self.0 + impl From<UnixCredentials> for libc::ucred { + fn from(uc: UnixCredentials) -> Self { + uc.0 } } } else if #[cfg(any(target_os = "freebsd", target_os = "dragonfly"))] { @@ -1300,6 +1300,7 @@ pub struct RecvMmsgData<'a, I> target_os = "freebsd", target_os = "netbsd", ))] +#[allow(clippy::needless_collect)] // Complicated false positive pub fn recvmmsg<'a, I>( fd: RawFd, data: impl std::iter::IntoIterator<Item=&'a mut RecvMmsgData<'a, I>, @@ -1813,7 +1814,7 @@ pub fn sockaddr_storage_to_addr( assert!(len <= mem::size_of::<sockaddr_storage>()); if len < mem::size_of_val(&addr.ss_family) { - return Err(Error::from(Errno::ENOTCONN)); + return Err(Errno::ENOTCONN); } match c_int::from(addr.ss_family) { diff --git a/src/sys/statvfs.rs b/src/sys/statvfs.rs index 508fa8db..15e7a7d4 100644 --- a/src/sys/statvfs.rs +++ b/src/sys/statvfs.rs @@ -150,7 +150,7 @@ mod test { #[test] fn statvfs_call() { - statvfs("/".as_bytes()).unwrap(); + statvfs(&b"/"[..]).unwrap(); } #[test] diff --git a/src/sys/timerfd.rs b/src/sys/timerfd.rs index 5d87b7c2..705a3c4d 100644 --- a/src/sys/timerfd.rs +++ b/src/sys/timerfd.rs @@ -257,14 +257,9 @@ impl TimerFd { /// /// Note: If the alarm is unset, then you will wait forever. pub fn wait(&self) -> Result<()> { - loop { - if let Err(e) = read(self.fd, &mut [0u8; 8]) { - match e { - Errno::EINTR => continue, - _ => return Err(e), - } - } else { - break; + while let Err(e) = read(self.fd, &mut [0u8; 8]) { + if e != Errno::EINTR { + return Err(e) } } diff --git a/src/time.rs b/src/time.rs index 1731e60e..6275b59c 100644 --- a/src/time.rs +++ b/src/time.rs @@ -6,7 +6,7 @@ use crate::sys::time::TimeSpec; target_os = "android", target_os = "emscripten", ))] -use crate::{unistd::Pid, Error}; +use crate::unistd::Pid; use crate::{Errno, Result}; use libc::{self, clockid_t}; use std::mem::MaybeUninit; @@ -255,6 +255,6 @@ pub fn clock_getcpuclockid(pid: Pid) -> Result<ClockId> { let res = unsafe { clk_id.assume_init() }; Ok(ClockId::from(res)) } else { - Err(Error::from(Errno::from_i32(ret))) + Err(Errno::from_i32(ret)) } } diff --git a/src/ucontext.rs b/src/ucontext.rs index a5b8cc75..f2338bd4 100644 --- a/src/ucontext.rs +++ b/src/ucontext.rs @@ -1,4 +1,3 @@ -use libc; #[cfg(not(target_env = "musl"))] use crate::Result; #[cfg(not(target_env = "musl"))] diff --git a/src/unistd.rs b/src/unistd.rs index 2f47b260..64759dc6 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -180,10 +180,7 @@ impl ForkResult { /// Return `true` if this is the child process of the `fork()` #[inline] pub fn is_child(self) -> bool { - match self { - ForkResult::Child => true, - _ => false - } + matches!(self, ForkResult::Child) } /// Returns `true` if this is the parent process of the `fork()` @@ -398,7 +395,7 @@ pub fn dup3(oldfd: RawFd, newfd: RawFd, flags: OFlag) -> Result<RawFd> { #[inline] fn dup3_polyfill(oldfd: RawFd, newfd: RawFd, flags: OFlag) -> Result<RawFd> { if oldfd == newfd { - return Err(Error::from(Errno::EINVAL)); + return Err(Errno::EINVAL); } let fd = dup2(oldfd, newfd)?; @@ -572,7 +569,7 @@ fn reserve_double_buffer_size<T>(buf: &mut Vec<T>, limit: usize) -> Result<()> { use std::cmp::min; if buf.capacity() >= limit { - return Err(Error::from(Errno::ERANGE)) + return Err(Errno::ERANGE) } let capacity = min(buf.capacity() * 2, limit); @@ -615,9 +612,9 @@ pub fn getcwd() -> Result<PathBuf> { let error = Errno::last(); // ERANGE means buffer was too small to store directory name if error != Errno::ERANGE { - return Err(Error::from(error)); + return Err(error); } - } + } // Trigger the internal buffer resizing logic. reserve_double_buffer_size(&mut buf, PATH_MAX as usize)?; @@ -739,7 +736,7 @@ pub fn execv<S: AsRef<CStr>>(path: &CStr, argv: &[S]) -> Result<Infallible> { libc::execv(path.as_ptr(), args_p.as_ptr()) }; - Err(Error::from(Errno::last())) + Err(Errno::last()) } @@ -764,7 +761,7 @@ pub fn execve<SA: AsRef<CStr>, SE: AsRef<CStr>>(path: &CStr, args: &[SA], env: & libc::execve(path.as_ptr(), args_p.as_ptr(), env_p.as_ptr()) }; - Err(Error::from(Errno::last())) + Err(Errno::last()) } /// Replace the current process image with a new one and replicate shell `PATH` @@ -784,7 +781,7 @@ pub fn execvp<S: AsRef<CStr>>(filename: &CStr, args: &[S]) -> Result<Infallible> libc::execvp(filename.as_ptr(), args_p.as_ptr()) }; - Err(Error::from(Errno::last())) + Err(Errno::last()) } /// Replace the current process image with a new one and replicate shell `PATH` @@ -805,7 +802,7 @@ pub fn execvpe<SA: AsRef<CStr>, SE: AsRef<CStr>>(filename: &CStr, args: &[SA], e libc::execvpe(filename.as_ptr(), args_p.as_ptr(), env_p.as_ptr()) }; - Err(Error::from(Errno::last())) + Err(Errno::last()) } /// Replace the current process image with a new one (see @@ -833,7 +830,7 @@ pub fn fexecve<SA: AsRef<CStr> ,SE: AsRef<CStr>>(fd: RawFd, args: &[SA], env: &[ libc::fexecve(fd, args_p.as_ptr(), env_p.as_ptr()) }; - Err(Error::from(Errno::last())) + Err(Errno::last()) } /// Execute program relative to a directory file descriptor (see @@ -858,7 +855,7 @@ pub fn execveat<SA: AsRef<CStr>,SE: AsRef<CStr>>(dirfd: RawFd, pathname: &CStr, args_p.as_ptr(), env_p.as_ptr(), flags); }; - Err(Error::from(Errno::last())) + Err(Errno::last()) } /// Daemonize this process by detaching from the controlling terminal (see @@ -1138,9 +1135,9 @@ pub fn isatty(fd: RawFd) -> Result<bool> { } else { match Errno::last() { Errno::ENOTTY => Ok(false), - err => Err(Error::from(err)), + err => Err(err), } - } + } } } @@ -1449,7 +1446,7 @@ pub fn getgroups() -> Result<Vec<Gid>> { // EINVAL indicates that the buffer size was too // small, resize it up to ngroups_max as limit. reserve_double_buffer_size(&mut groups, ngroups_max) - .or(Err(Error::from(Errno::EINVAL)))?; + .or(Err(Errno::EINVAL))?; }, Err(e) => return Err(e) } @@ -1571,7 +1568,7 @@ pub fn getgrouplist(user: &CStr, group: Gid) -> Result<Vec<Gid>> { // groups as possible, but Linux manpages do not mention this // behavior. reserve_double_buffer_size(&mut groups, ngroups_max as usize) - .map_err(|_| Error::from(Errno::EINVAL))?; + .map_err(|_| Errno::EINVAL)?; } } } @@ -1930,7 +1927,7 @@ pub fn fpathconf(fd: RawFd, var: PathconfVar) -> Result<Option<c_long>> { if errno::errno() == 0 { Ok(None) } else { - Err(Error::from(Errno::last())) + Err(Errno::last()) } } else { Ok(Some(raw)) @@ -1969,7 +1966,7 @@ pub fn pathconf<P: ?Sized + NixPath>(path: &P, var: PathconfVar) -> Result<Optio if errno::errno() == 0 { Ok(None) } else { - Err(Error::from(Errno::last())) + Err(Errno::last()) } } else { Ok(Some(raw)) @@ -2469,7 +2466,7 @@ pub fn sysconf(var: SysconfVar) -> Result<Option<c_long>> { if errno::errno() == 0 { Ok(None) } else { - Err(Error::from(Errno::last())) + Err(Errno::last()) } } else { Ok(Some(raw)) @@ -2788,7 +2785,7 @@ impl User { // Trigger the internal buffer resizing logic. reserve_double_buffer_size(&mut cbuf, buflimit)?; } else { - return Err(Error::from(Errno::last())); + return Err(Errno::last()); } } } @@ -2909,7 +2906,7 @@ impl Group { // Trigger the internal buffer resizing logic. reserve_double_buffer_size(&mut cbuf, buflimit)?; } else { - return Err(Error::from(Errno::last())); + return Err(Errno::last()); } } } @@ -2968,7 +2965,7 @@ pub fn ttyname(fd: RawFd) -> Result<PathBuf> { let ret = unsafe { libc::ttyname_r(fd, c_buf, buf.len()) }; if ret != 0 { - return Err(Error::from(Errno::from_i32(ret))); + return Err(Errno::from_i32(ret)); } let nul = buf.iter().position(|c| *c == b'\0').unwrap(); |