summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authori509VCB <git@i509.me>2022-08-23 17:05:19 -0500
committeri509VCB <git@i509.me>2022-09-07 22:00:38 -0500
commitcf15c2bb5505231bc3128ef61522336e315e2169 (patch)
treec646f122c6be352495bc584c25ebbee2931b58af
parent1eb589f26fe4b56ce799e05d3b5fb527729cffdf (diff)
downloadnix-cf15c2bb5505231bc3128ef61522336e315e2169.zip
expose memfd on freebsd
-rw-r--r--CHANGELOG.md2
-rw-r--r--src/sys/memfd.rs19
-rw-r--r--src/sys/mod.rs2
3 files changed, 21 insertions, 2 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9aabc244..5891b412 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 642676b4..9f08dbb2 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<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)
diff --git a/src/sys/mod.rs b/src/sys/mod.rs
index 979d623b..2065059d 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;