Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
34 changes: 21 additions & 13 deletions src/unix.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
};
}
Expand Down Expand Up @@ -89,6 +92,7 @@ pub fn create_mapping(unique_id: &str, map_size: usize) -> Result<MapData, Shmem
Mode::S_IRUSR | Mode::S_IWUSR, //Permission allow user+rw
) {
Ok(v) => {
let v = v.into_raw_fd();
trace!(
"shm_open({}, {:X}, {:X}) == {}",
unique_id,
Expand All @@ -113,7 +117,10 @@ pub fn create_mapping(unique_id: &str, map_size: usize) -> Result<MapData, Shmem
//Enlarge the memory descriptor file size to the requested map size
debug!("Creating memory mapping");
trace!("ftruncate({}, {})", new_map.map_fd, new_map.map_size);
match ftruncate(new_map.map_fd, new_map.map_size as _) {
match ftruncate(
unsafe { BorrowedFd::borrow_raw(new_map.map_fd) },
new_map.map_size as _,
) {
Ok(_) => {}
Err(e) => return Err(ShmemError::UnknownOsError(e as u32)),
};
Expand All @@ -122,11 +129,11 @@ pub fn create_mapping(unique_id: &str, map_size: usize) -> Result<MapData, Shmem
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
)
} {
Expand All @@ -139,7 +146,7 @@ pub fn create_mapping(unique_id: &str, map_size: usize) -> Result<MapData, Shmem
new_map.map_fd,
v
);
v as *mut _
v.as_ptr() as *mut _
}
Err(e) => return Err(ShmemError::MapCreateFailed(e as u32)),
};
Expand All @@ -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,
Expand All @@ -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)),
};
Expand All @@ -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
)
} {
Expand All @@ -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)),
};
Expand Down