summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/sys/mman.rs21
-rw-r--r--test/sys/test_mman.rs14
2 files changed, 21 insertions, 14 deletions
diff --git a/src/sys/mman.rs b/src/sys/mman.rs
index d4c6ecb0..deef7005 100644
--- a/src/sys/mman.rs
+++ b/src/sys/mman.rs
@@ -8,7 +8,7 @@ use crate::Result;
#[cfg(feature = "fs")]
use crate::{fcntl::OFlag, sys::stat::Mode};
use libc::{self, c_int, c_void, off_t, size_t};
-use std::{num::NonZeroUsize, os::unix::io::RawFd};
+use std::{num::NonZeroUsize, os::unix::io::{AsRawFd, AsFd}};
libc_bitflags! {
/// Desired memory protection of a memory mapping.
@@ -416,17 +416,18 @@ pub fn munlockall() -> Result<()> {
/// See the [`mmap(2)`] man page for detailed requirements.
///
/// [`mmap(2)`]: https://man7.org/linux/man-pages/man2/mmap.2.html
-pub unsafe fn mmap(
+pub unsafe fn mmap<F: AsFd>(
addr: Option<NonZeroUsize>,
length: NonZeroUsize,
prot: ProtFlags,
flags: MapFlags,
- fd: RawFd,
+ f: Option<&F>,
offset: off_t,
) -> Result<*mut c_void> {
let ptr =
addr.map_or(std::ptr::null_mut(), |a| usize::from(a) as *mut c_void);
+ let fd = f.map(|f| f.as_fd().as_raw_fd()).unwrap_or(-1);
let ret =
libc::mmap(ptr, length.into(), prot.bits(), flags.bits(), fd, offset);
@@ -518,11 +519,12 @@ pub unsafe fn madvise(
/// # use nix::libc::size_t;
/// # use nix::sys::mman::{mmap, mprotect, MapFlags, ProtFlags};
/// # use std::ptr;
+/// # use std::os::unix::io::BorrowedFd;
/// const ONE_K: size_t = 1024;
/// let one_k_non_zero = std::num::NonZeroUsize::new(ONE_K).unwrap();
/// let mut slice: &mut [u8] = unsafe {
-/// let mem = mmap(None, one_k_non_zero, ProtFlags::PROT_NONE,
-/// MapFlags::MAP_ANON | MapFlags::MAP_PRIVATE, -1, 0).unwrap();
+/// let mem = mmap::<BorrowedFd>(None, one_k_non_zero, ProtFlags::PROT_NONE,
+/// MapFlags::MAP_ANON | MapFlags::MAP_PRIVATE, None, 0).unwrap();
/// mprotect(mem, ONE_K, ProtFlags::PROT_READ | ProtFlags::PROT_WRITE).unwrap();
/// std::slice::from_raw_parts_mut(mem as *mut u8, ONE_K)
/// };
@@ -566,9 +568,11 @@ pub fn shm_open<P>(
name: &P,
flag: OFlag,
mode: Mode
- ) -> Result<RawFd>
+ ) -> Result<std::os::unix::io::OwnedFd>
where P: ?Sized + NixPath
{
+ use std::os::unix::io::{FromRawFd, OwnedFd};
+
let ret = name.with_nix_path(|cstr| {
#[cfg(any(target_os = "macos", target_os = "ios"))]
unsafe {
@@ -580,7 +584,10 @@ pub fn shm_open<P>(
}
})?;
- Errno::result(ret)
+ match ret {
+ -1 => Err(Errno::last()),
+ fd => Ok(unsafe{ OwnedFd::from_raw_fd(fd) })
+ }
}
}
diff --git a/test/sys/test_mman.rs b/test/sys/test_mman.rs
index e748427b..b4674e53 100644
--- a/test/sys/test_mman.rs
+++ b/test/sys/test_mman.rs
@@ -1,15 +1,15 @@
use nix::sys::mman::{mmap, MapFlags, ProtFlags};
-use std::num::NonZeroUsize;
+use std::{num::NonZeroUsize, os::unix::io::BorrowedFd};
#[test]
fn test_mmap_anonymous() {
unsafe {
- let ptr = mmap(
+ let ptr = mmap::<BorrowedFd>(
None,
NonZeroUsize::new(1).unwrap(),
ProtFlags::PROT_READ | ProtFlags::PROT_WRITE,
MapFlags::MAP_PRIVATE | MapFlags::MAP_ANONYMOUS,
- -1,
+ None,
0,
)
.unwrap() as *mut u8;
@@ -29,12 +29,12 @@ fn test_mremap_grow() {
let one_k_non_zero = NonZeroUsize::new(ONE_K).unwrap();
let slice: &mut [u8] = unsafe {
- let mem = mmap(
+ let mem = mmap::<BorrowedFd>(
None,
one_k_non_zero,
ProtFlags::PROT_READ | ProtFlags::PROT_WRITE,
MapFlags::MAP_ANONYMOUS | MapFlags::MAP_PRIVATE,
- -1,
+ None,
0,
)
.unwrap();
@@ -87,12 +87,12 @@ fn test_mremap_shrink() {
const ONE_K: size_t = 1024;
let ten_one_k = NonZeroUsize::new(10 * ONE_K).unwrap();
let slice: &mut [u8] = unsafe {
- let mem = mmap(
+ let mem = mmap::<BorrowedFd>(
None,
ten_one_k,
ProtFlags::PROT_READ | ProtFlags::PROT_WRITE,
MapFlags::MAP_ANONYMOUS | MapFlags::MAP_PRIVATE,
- -1,
+ None,
0,
)
.unwrap();