Skip to content

Commit 13248d4

Browse files
authored
refactor: change name arg of memfd_create() to &NixPath (#2431)
* 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
1 parent 667cba0 commit 13248d4

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

changelog/2431.changed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Change the type of the `name` argument of `memfd_create()` from `&CStr` to `<P: NixPath>(name: &P)`

src/sys/memfd.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ use cfg_if::cfg_if;
44
use std::os::unix::io::{FromRawFd, OwnedFd, RawFd};
55

66
use crate::errno::Errno;
7-
use crate::Result;
8-
use std::ffi::CStr;
7+
use crate::{NixPath, Result};
98

109
libc_bitflags!(
1110
/// Options that change the behavior of [`memfd_create`].
@@ -84,9 +83,13 @@ libc_bitflags!(
8483
///
8584
/// [`memfd_create(2)`]: https://man7.org/linux/man-pages/man2/memfd_create.2.html
8685
#[inline] // Delays codegen, preventing linker errors with dylibs and --no-allow-shlib-undefined
87-
pub fn memfd_create(name: &CStr, flags: MemFdCreateFlag) -> Result<OwnedFd> {
88-
let res = unsafe {
89-
cfg_if! {
86+
pub fn memfd_create<P: NixPath + ?Sized>(
87+
name: &P,
88+
flags: MemFdCreateFlag,
89+
) -> Result<OwnedFd> {
90+
let res = name.with_nix_path(|cstr| {
91+
unsafe {
92+
cfg_if! {
9093
if #[cfg(all(
9194
// Android does not have a memfd_create symbol
9295
not(target_os = "android"),
@@ -97,12 +100,13 @@ pub fn memfd_create(name: &CStr, flags: MemFdCreateFlag) -> Result<OwnedFd> {
97100
target_env = "musl",
98101
)))]
99102
{
100-
libc::memfd_create(name.as_ptr(), flags.bits())
103+
libc::memfd_create(cstr.as_ptr(), flags.bits())
101104
} else {
102-
libc::syscall(libc::SYS_memfd_create, name.as_ptr(), flags.bits())
105+
libc::syscall(libc::SYS_memfd_create, cstr.as_ptr(), flags.bits())
103106
}
104107
}
105-
};
108+
}
109+
})?;
106110

107111
Errno::result(res).map(|r| unsafe { OwnedFd::from_raw_fd(r as RawFd) })
108112
}

0 commit comments

Comments
 (0)