Skip to content

Commit

Permalink
refactor: change name arg of memfd_create() to &NixPath
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveLauC committed Jun 8, 2024
1 parent 4dcc996 commit a5fb074
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
1 change: 1 addition & 0 deletions changelog/2431.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Change the type of the `name` argument of `memfd_create()` from `&CStr` to `<P: NixPath>(name: &P)`
20 changes: 12 additions & 8 deletions src/sys/memfd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use cfg_if::cfg_if;
use std::os::unix::io::{FromRawFd, OwnedFd, RawFd};

use crate::errno::Errno;
use crate::Result;
use std::ffi::CStr;
use crate::{NixPath, Result};

libc_bitflags!(
/// Options that change the behavior of [`memfd_create`].
Expand Down Expand Up @@ -84,9 +83,13 @@ libc_bitflags!(
///
/// [`memfd_create(2)`]: https://man7.org/linux/man-pages/man2/memfd_create.2.html
#[inline] // Delays codegen, preventing linker errors with dylibs and --no-allow-shlib-undefined
pub fn memfd_create(name: &CStr, flags: MemFdCreateFlag) -> Result<OwnedFd> {
let res = unsafe {
cfg_if! {
pub fn memfd_create<P: NixPath + ?Sized>(
name: &P,
flags: MemFdCreateFlag,
) -> Result<OwnedFd> {
let res = name.with_nix_path(|cstr| {
unsafe {
cfg_if! {
if #[cfg(all(
// Android does not have a memfd_create symbol
not(target_os = "android"),
Expand All @@ -97,12 +100,13 @@ pub fn memfd_create(name: &CStr, flags: MemFdCreateFlag) -> Result<OwnedFd> {
target_env = "musl",
)))]
{
libc::memfd_create(name.as_ptr(), flags.bits())
libc::memfd_create(cstr.as_ptr(), flags.bits())
} else {
libc::syscall(libc::SYS_memfd_create, name.as_ptr(), flags.bits())
libc::syscall(libc::SYS_memfd_create, cstr.as_ptr(), flags.bits())
}
}
};
}
})?;

Errno::result(res).map(|r| unsafe { OwnedFd::from_raw_fd(r as RawFd) })
}
3 changes: 3 additions & 0 deletions test/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,6 @@ mod test_statfs;
target_os = "haiku"
)))]
mod test_resource;

#[cfg(any(linux_android, target_os = "freebsd"))]
mod test_memfd;
22 changes: 22 additions & 0 deletions test/sys/test_memfd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use nix::sys::memfd::memfd_create;
use nix::sys::memfd::MemFdCreateFlag;
use nix::unistd::lseek;
use nix::unistd::read;
use nix::unistd::{write, Whence};
use std::os::fd::{AsFd, AsRawFd};

#[test]
fn test_memfd_create() {
let fd =
memfd_create("test_memfd_create_name", MemFdCreateFlag::MFD_CLOEXEC)
.unwrap();
let contents = b"hello";
assert_eq!(write(fd.as_fd(), contents).unwrap(), 5);

lseek(fd.as_raw_fd(), 0, Whence::SeekSet).unwrap();

let mut buf = vec![0_u8; contents.len()];
assert_eq!(read(fd.as_raw_fd(), &mut buf).unwrap(), 5);

assert_eq!(contents, buf.as_slice());
}

0 comments on commit a5fb074

Please sign in to comment.