Skip to content

Commit

Permalink
refactor: change name arg of memfd_create() to &NixPath (#2431)
Browse files Browse the repository at this point in the history
* refactor: change name arg of memfd_create() to &NixPath

* style: format test/sys/mod.rs

* disable test and see if this symbol is still needed for build

* test: disable test under QEMU since symbol is unavailable

* test: move imports to function body

* test: remove the test
  • Loading branch information
SteveLauC authored Jun 8, 2024
1 parent 667cba0 commit 13248d4
Show file tree
Hide file tree
Showing 2 changed files with 13 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) })
}

0 comments on commit 13248d4

Please sign in to comment.