Skip to content

refactor: change name arg of memfd_create() to &NixPath #2431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 8, 2024
Merged
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
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())
Comment on lines +103 to +105
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible we always use the else branch? Or does it have any drawback?

From tokio-rs/tracing#1879 I'm afraid that using libc::memfd_create may fail on some old GNU toolchain.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, because the glibc wrapper may only be available since a specific version, and if you are using an older glibc and this Nix wrapper, the code won't compile. But raw syscalls are inherently unsafe, Nix tries to avoid it if possible.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, this kind of thing has always been bothering me. I just created a tracking issue #2538 for these interfaces. May I ask about the issue you encountered with this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the issue you encountered with this?

Not in real-world, but a blocker I don't use nix at fast/logforth#80 (memfd.rs)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we should wrap the raw syscall. 😪

If we want to discuss this further, let's discuss it there #2538.

}
}
};
}
})?;

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