Skip to content

Commit cf15c2b

Browse files
committed
expose memfd on freebsd
1 parent 1eb589f commit cf15c2b

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
1010
([#1804](https://github.com/nix-rust/nix/pull/1804))
1111
- Added `line_discipline` field to `Termios` on Linux, Android and Haiku
1212
([#1805](https://github.com/nix-rust/nix/pull/1805))
13+
- Expose the memfd module on FreeBSD (memfd was added in FreeBSD 13)
14+
([#1808](https://github.com/nix-rust/nix/pull/1808))
1315

1416
### Changed
1517

src/sys/memfd.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Interfaces for managing memory-backed files.
22
33
use std::os::unix::io::RawFd;
4+
use cfg_if::cfg_if;
5+
46
use crate::Result;
57
use crate::errno::Errno;
68
use std::ffi::CStr;
@@ -40,7 +42,22 @@ libc_bitflags!(
4042
/// [`memfd_create(2)`]: https://man7.org/linux/man-pages/man2/memfd_create.2.html
4143
pub fn memfd_create(name: &CStr, flags: MemFdCreateFlag) -> Result<RawFd> {
4244
let res = unsafe {
43-
libc::syscall(libc::SYS_memfd_create, name.as_ptr(), flags.bits())
45+
cfg_if! {
46+
if #[cfg(all(
47+
// Android does not have a memfd_create symbol
48+
not(target_os = "android"),
49+
any(
50+
target_os = "freebsd",
51+
// If the OS is Linux, gnu and musl expose a memfd_create symbol but not uclibc
52+
target_env = "gnu",
53+
target_env = "musl",
54+
)))]
55+
{
56+
libc::memfd_create(name.as_ptr(), flags.bits())
57+
} else {
58+
libc::syscall(libc::SYS_memfd_create, name.as_ptr(), flags.bits())
59+
}
60+
}
4461
};
4562

4663
Errno::result(res).map(|r| r as RawFd)

src/sys/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ feature! {
5050
#[macro_use]
5151
pub mod ioctl;
5252

53-
#[cfg(any(target_os = "android", target_os = "linux"))]
53+
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux"))]
5454
feature! {
5555
#![feature = "fs"]
5656
pub mod memfd;

0 commit comments

Comments
 (0)