diff --git a/CHANGELOG.md b/CHANGELOG.md index 9aabc244d9..5891b4126f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/sys/memfd.rs b/src/sys/memfd.rs index 642676b431..9f08dbb2bf 100644 --- a/src/sys/memfd.rs +++ b/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; @@ -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 { 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) diff --git a/src/sys/mod.rs b/src/sys/mod.rs index 979d623b89..2065059de8 100644 --- a/src/sys/mod.rs +++ b/src/sys/mod.rs @@ -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;