Skip to content

Commit

Permalink
expose memfd on freebsd
Browse files Browse the repository at this point in the history
  • Loading branch information
i509VCB committed Aug 25, 2022
1 parent 97d6b43 commit b0531ae
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).

- 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())
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

0 comments on commit b0531ae

Please sign in to comment.