Skip to content
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

expose memfd on freebsd #1808

Merged
merged 1 commit into from Sep 12, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
([#1804](https://github.com/nix-rust/nix/pull/1804))
- Added `line_discipline` field to `Termios` on Linux, Android and Haiku
([#1805](https://github.com/nix-rust/nix/pull/1805))
- Expose the memfd module on FreeBSD (memfd was added in FreeBSD 13)
([#1808](https://github.com/nix-rust/nix/pull/1808))

### Changed

Expand Down
19 changes: 18 additions & 1 deletion src/sys/memfd.rs
@@ -1,6 +1,8 @@
//! Interfaces for managing memory-backed files.

use std::os::unix::io::RawFd;
use cfg_if::cfg_if;

use crate::Result;
use crate::errno::Errno;
use std::ffi::CStr;
Expand Down Expand Up @@ -40,7 +42,22 @@ libc_bitflags!(
/// [`memfd_create(2)`]: https://man7.org/linux/man-pages/man2/memfd_create.2.html
pub fn memfd_create(name: &CStr, flags: MemFdCreateFlag) -> Result<RawFd> {
let res = unsafe {
libc::syscall(libc::SYS_memfd_create, name.as_ptr(), flags.bits())
Copy link
Member

Choose a reason for hiding this comment

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

I would rather not bend over backwards to cater to the obscure arches. If they don't define the libc symbol, then I think we should just not bind it. We can do it like this:
#[cfg(any(target_os = "freebsd", target_env = "gnu", target_env = "musl"))]

Copy link
Contributor Author

@i509VCB i509VCB Aug 24, 2022

Choose a reason for hiding this comment

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

Sorry it wasn't architecture, it seems that it's just armv7 uclibceabihf that's failing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have some solution but it's a more ugly cfg block.

Essentially use the libc symbol if it's freebsd or gnu/musl linux and otherwise use the syscall function.

Or was the intention to gate memfd_create from all linuxes that aren't using a gnu or musl libc?

cfg_if! {
if #[cfg(all(
// Android does not have a memfd_create symbol
not(target_os = "android"),
any(
target_os = "freebsd",
// If the OS is Linux, gnu and musl expose a memfd_create symbol but not uclibc
target_env = "gnu",
target_env = "musl",
)))]
{
libc::memfd_create(name.as_ptr(), flags.bits())
} else {
libc::syscall(libc::SYS_memfd_create, name.as_ptr(), flags.bits())
}
}
};

Errno::result(res).map(|r| r as RawFd)
Expand Down
2 changes: 1 addition & 1 deletion src/sys/mod.rs
Expand Up @@ -50,7 +50,7 @@ feature! {
#[macro_use]
pub mod ioctl;

#[cfg(any(target_os = "android", target_os = "linux"))]
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux"))]
feature! {
#![feature = "fs"]
pub mod memfd;
Expand Down