summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Somers <asomers@gmail.com>2020-05-16 17:03:47 -0600
committerAlan Somers <asomers@gmail.com>2020-06-27 17:36:46 -0600
commit46a869814ddbb2678e968fcb87cfd7fb8043f177 (patch)
tree09d17da8e0da3f43fdd97d76bd829cbcb8bdd8e4
parentaf45859aa00670b8cea6a9704d8fd3b35db2ac93 (diff)
downloadnix-46a869814ddbb2678e968fcb87cfd7fb8043f177.zip
misc clippy cleanup
-rw-r--r--src/fcntl.rs4
-rw-r--r--src/sched.rs10
-rw-r--r--src/sys/mman.rs54
-rw-r--r--src/ucontext.rs8
-rw-r--r--src/unistd.rs8
5 files changed, 64 insertions, 20 deletions
diff --git a/src/fcntl.rs b/src/fcntl.rs
index ea036427..1581d3a7 100644
--- a/src/fcntl.rs
+++ b/src/fcntl.rs
@@ -161,6 +161,8 @@ libc_bitflags!(
}
);
+// The conversion is not identical on all operating systems.
+#[allow(clippy::identity_conversion)]
pub fn open<P: ?Sized + NixPath>(path: &P, oflag: OFlag, mode: Mode) -> Result<RawFd> {
let fd = path.with_nix_path(|cstr| {
unsafe { libc::open(cstr.as_ptr(), oflag.bits(), mode.bits() as c_uint) }
@@ -169,6 +171,8 @@ pub fn open<P: ?Sized + NixPath>(path: &P, oflag: OFlag, mode: Mode) -> Result<R
Errno::result(fd)
}
+// The conversion is not identical on all operating systems.
+#[allow(clippy::identity_conversion)]
#[cfg(not(target_os = "redox"))]
pub fn openat<P: ?Sized + NixPath>(
dirfd: RawFd,
diff --git a/src/sched.rs b/src/sched.rs
index b6601ca4..3b48b4ad 100644
--- a/src/sched.rs
+++ b/src/sched.rs
@@ -80,7 +80,8 @@ mod sched_linux_like {
if field >= CpuSet::count() {
Err(Error::Sys(Errno::EINVAL))
} else {
- Ok(unsafe { libc::CPU_SET(field, &mut self.cpu_set) })
+ unsafe { libc::CPU_SET(field, &mut self.cpu_set); }
+ Ok(())
}
}
@@ -90,7 +91,8 @@ mod sched_linux_like {
if field >= CpuSet::count() {
Err(Error::Sys(Errno::EINVAL))
} else {
- Ok(unsafe { libc::CPU_CLR(field, &mut self.cpu_set) })
+ unsafe { libc::CPU_CLR(field, &mut self.cpu_set);}
+ Ok(())
}
}
@@ -187,8 +189,8 @@ mod sched_linux_like {
let res = unsafe {
let combined = flags.bits() | signal.unwrap_or(0);
- let ptr = stack.as_mut_ptr().offset(stack.len() as isize);
- let ptr_aligned = ptr.offset((ptr as usize % 16) as isize * -1);
+ let ptr = stack.as_mut_ptr().add(stack.len());
+ let ptr_aligned = ptr.sub(ptr as usize % 16);
libc::clone(
mem::transmute(
callback as extern "C" fn(*mut Box<dyn FnMut() -> isize>) -> i32,
diff --git a/src/sys/mman.rs b/src/sys/mman.rs
index 5a5dd89e..b2bed6e4 100644
--- a/src/sys/mman.rs
+++ b/src/sys/mman.rs
@@ -260,20 +260,37 @@ libc_bitflags!{
}
}
-/// Locks all memory pages that contain part of the address range with `length` bytes starting at
-/// `addr`. Locked pages never move to the swap area.
+/// Locks all memory pages that contain part of the address range with `length`
+/// bytes starting at `addr`.
+///
+/// Locked pages never move to the swap area.
+///
+/// # Safety
+///
+/// `addr` must meet all the requirements described in the `mlock(2)` man page.
pub unsafe fn mlock(addr: *const c_void, length: size_t) -> Result<()> {
Errno::result(libc::mlock(addr, length)).map(drop)
}
-/// Unlocks all memory pages that contain part of the address range with `length` bytes starting at
-/// `addr`.
+/// Unlocks all memory pages that contain part of the address range with
+/// `length` bytes starting at `addr`.
+///
+/// # Safety
+///
+/// `addr` must meet all the requirements described in the `munlock(2)` man
+/// page.
pub unsafe fn munlock(addr: *const c_void, length: size_t) -> Result<()> {
Errno::result(libc::munlock(addr, length)).map(drop)
}
-/// Locks all memory pages mapped into this process' address space. Locked pages never move to the
-/// swap area.
+/// Locks all memory pages mapped into this process' address space.
+///
+/// Locked pages never move to the swap area.
+///
+/// # Safety
+///
+/// `addr` must meet all the requirements described in the `mlockall(2)` man
+/// page.
pub fn mlockall(flags: MlockAllFlags) -> Result<()> {
unsafe { Errno::result(libc::mlockall(flags.bits())) }.map(drop)
}
@@ -283,8 +300,11 @@ pub fn munlockall() -> Result<()> {
unsafe { Errno::result(libc::munlockall()) }.map(drop)
}
-/// Calls to mmap are inherently unsafe, so they must be made in an unsafe block. Typically
-/// a higher-level abstraction will hide the unsafe interactions with the mmap'd region.
+/// allocate memory, or map files or devices into memory
+///
+/// # Safety
+///
+/// See the `mmap(2)` man page for detailed requirements.
pub unsafe fn mmap(addr: *mut c_void, length: size_t, prot: ProtFlags, flags: MapFlags, fd: RawFd, offset: off_t) -> Result<*mut c_void> {
let ret = libc::mmap(addr, length, prot.bits(), flags.bits(), fd, offset);
@@ -295,10 +315,22 @@ pub unsafe fn mmap(addr: *mut c_void, length: size_t, prot: ProtFlags, flags: Ma
}
}
+/// remove a mapping
+///
+/// # Safety
+///
+/// `addr` must meet all the requirements described in the `munmap(2)` man
+/// page.
pub unsafe fn munmap(addr: *mut c_void, len: size_t) -> Result<()> {
Errno::result(libc::munmap(addr, len)).map(drop)
}
+/// give advice about use of memory
+///
+/// # Safety
+///
+/// See the `madvise(2)` man page. Take special care when using
+/// `MmapAdvise::MADV_FREE`.
pub unsafe fn madvise(addr: *mut c_void, length: size_t, advise: MmapAdvise) -> Result<()> {
Errno::result(libc::madvise(addr, length, advise as i32)).map(drop)
}
@@ -332,6 +364,12 @@ pub unsafe fn mprotect(addr: *mut c_void, length: size_t, prot: ProtFlags) -> Re
Errno::result(libc::mprotect(addr, length, prot.bits())).map(drop)
}
+/// synchronize a mapped region
+///
+/// # Safety
+///
+/// `addr` must meet all the requirements described in the `msync(2)` man
+/// page.
pub unsafe fn msync(addr: *mut c_void, length: size_t, flags: MsFlags) -> Result<()> {
Errno::result(libc::msync(addr, length, flags.bits())).map(drop)
}
diff --git a/src/ucontext.rs b/src/ucontext.rs
index 9c1e8d2a..d264c5d8 100644
--- a/src/ucontext.rs
+++ b/src/ucontext.rs
@@ -30,10 +30,14 @@ impl UContext {
}
pub fn sigmask_mut(&mut self) -> &mut SigSet {
- unsafe { mem::transmute(&mut self.context.uc_sigmask) }
+ unsafe {
+ &mut *(&mut self.context.uc_sigmask as *mut libc::sigset_t as *mut SigSet)
+ }
}
pub fn sigmask(&self) -> &SigSet {
- unsafe { mem::transmute(&self.context.uc_sigmask) }
+ unsafe {
+ &*(&self.context.uc_sigmask as *const libc::sigset_t as *const SigSet)
+ }
}
}
diff --git a/src/unistd.rs b/src/unistd.rs
index 375ae82e..f2902bb4 100644
--- a/src/unistd.rs
+++ b/src/unistd.rs
@@ -1465,9 +1465,7 @@ pub fn getgroups() -> Result<Vec<Gid>> {
/// # Ok(())
/// # }
/// #
-/// # fn main() {
-/// # try_main().unwrap();
-/// # }
+/// # try_main().unwrap();
/// ```
#[cfg(not(any(target_os = "ios", target_os = "macos", target_os = "redox")))]
pub fn setgroups(groups: &[Gid]) -> Result<()> {
@@ -1589,9 +1587,7 @@ pub fn getgrouplist(user: &CStr, group: Gid) -> Result<Vec<Gid>> {
/// # Ok(())
/// # }
/// #
-/// # fn main() {
-/// # try_main().unwrap();
-/// # }
+/// # try_main().unwrap();
/// ```
#[cfg(not(any(target_os = "ios", target_os = "macos", target_os = "redox")))]
pub fn initgroups(user: &CStr, group: Gid) -> Result<()> {