diff --git a/Cargo.toml b/Cargo.toml index 0e01a3f..77651ec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,7 +31,7 @@ rand = "0.8" log = { version = "0.4", optional = true } [target.'cfg(unix)'.dependencies] -nix = "0.23" +nix = { version = "0.30", default-features = false, features = ["fs", "mman"] } libc = "0.2" [target.'cfg(windows)'.dependencies] diff --git a/src/unix.rs b/src/unix.rs index 695b5f5..3c7fbb7 100644 --- a/src/unix.rs +++ b/src/unix.rs @@ -1,5 +1,6 @@ -use std::os::unix::io::RawFd; -use std::ptr::null_mut; +use std::num::NonZeroUsize; +use std::os::unix::io::{BorrowedFd, IntoRawFd, RawFd}; +use std::ptr::{null_mut, NonNull}; use crate::log::*; use nix::fcntl::OFlag; @@ -44,7 +45,9 @@ impl Drop for MapData { self.map_ptr, self.map_size ); - if let Err(_e) = unsafe { munmap(self.map_ptr as *mut _, self.map_size) } { + if let Err(_e) = unsafe { + munmap(NonNull::new_unchecked(self.map_ptr as *mut _), self.map_size) + } { debug!("Failed to munmap() shared memory mapping : {}", _e); }; } @@ -89,6 +92,7 @@ pub fn create_mapping(unique_id: &str, map_size: usize) -> Result { + let v = v.into_raw_fd(); trace!( "shm_open({}, {:X}, {:X}) == {}", unique_id, @@ -113,7 +117,10 @@ pub fn create_mapping(unique_id: &str, map_size: usize) -> Result {} Err(e) => return Err(ShmemError::UnknownOsError(e as u32)), }; @@ -122,11 +129,11 @@ pub fn create_mapping(unique_id: &str, map_size: usize) -> Result Result return Err(ShmemError::MapCreateFailed(e as u32)), }; @@ -161,6 +168,7 @@ pub fn open_mapping( Mode::S_IRUSR, ) { Ok(v) => { + let v = v.into_raw_fd(); trace!( "shm_open({}, {:X}, {:X}) == {}", unique_id, @@ -182,7 +190,7 @@ pub fn open_mapping( }; //Get mmap size - new_map.map_size = match fstat(new_map.map_fd) { + new_map.map_size = match fstat(unsafe { BorrowedFd::borrow_raw(new_map.map_fd) }) { Ok(v) => v.st_size as usize, Err(e) => return Err(ShmemError::MapOpenFailed(e as u32)), }; @@ -191,11 +199,11 @@ pub fn open_mapping( debug!("Loading mapping into address space"); new_map.map_ptr = match unsafe { mmap( - null_mut(), //Desired addr - new_map.map_size, //size of mapping + None, //Desired addr + NonZeroUsize::new(new_map.map_size).unwrap(), //size of mapping ProtFlags::PROT_READ | ProtFlags::PROT_WRITE, //Permissions on pages MapFlags::MAP_SHARED, //What kind of mapping - new_map.map_fd, //fd + BorrowedFd::borrow_raw(new_map.map_fd), //fd 0, //Offset into fd ) } { @@ -208,7 +216,7 @@ pub fn open_mapping( new_map.map_fd, v ); - v as *mut _ + v.as_ptr() as *mut _ } Err(e) => return Err(ShmemError::MapOpenFailed(e as u32)), };