| 1 |
//! Interfaces for managing memory-backed files. |
| 2 |
|
| 3 |
use cfg_if::cfg_if; |
| 4 |
use std::os::unix::io::{FromRawFd, OwnedFd, RawFd}; |
| 5 |
|
| 6 |
use crate::errno::Errno; |
| 7 |
use crate::{NixPath, Result}; |
| 8 |
|
| 9 |
libc_bitflags!( |
| 10 |
/// Options that change the behavior of [`memfd_create`]. |
| 11 |
pub struct MFdFlags: libc::c_uint { |
| 12 |
/// Set the close-on-exec ([`FD_CLOEXEC`]) flag on the new file descriptor. |
| 13 |
/// |
| 14 |
/// By default, the new file descriptor is set to remain open across an [`execve`] |
| 15 |
/// (the `FD_CLOEXEC` flag is initially disabled). This flag can be used to change |
| 16 |
/// this default. The file offset is set to the beginning of the file (see [`lseek`]). |
| 17 |
/// |
| 18 |
/// See also the description of the `O_CLOEXEC` flag in [`open(2)`]. |
| 19 |
/// |
| 20 |
/// [`execve`]: crate::unistd::execve |
| 21 |
/// [`lseek`]: crate::unistd::lseek |
| 22 |
/// [`FD_CLOEXEC`]: crate::fcntl::FdFlag::FD_CLOEXEC |
| 23 |
/// [`open(2)`]: https://man7.org/linux/man-pages/man2/open.2.html |
| 24 |
MFD_CLOEXEC; |
| 25 |
/// Allow sealing operations on this file. |
| 26 |
/// |
| 27 |
/// See also the file sealing notes given in [`memfd_create(2)`]. |
| 28 |
/// |
| 29 |
/// [`memfd_create(2)`]: https://man7.org/linux/man-pages/man2/memfd_create.2.html |
| 30 |
MFD_ALLOW_SEALING; |
| 31 |
/// Anonymous file will be created using huge pages. It should be safe now to |
| 32 |
/// combine with [`MFD_ALLOW_SEALING`] too. |
| 33 |
/// However, despite its presence, on FreeBSD it is unimplemented for now (ENOSYS). |
| 34 |
/// |
| 35 |
/// See also the hugetlb filesystem in [`memfd_create(2)`]. |
| 36 |
/// |
| 37 |
/// [`memfd_create(2)`]: https://man7.org/linux/man-pages/man2/memfd_create.2.html |
| 38 |
#[cfg(linux_android)] |
| 39 |
MFD_HUGETLB; |
| 40 |
/// Shift to get the huge page size. |
| 41 |
#[cfg(target_env = "ohos")] |
| 42 |
MFD_HUGE_SHIFT; |
| 43 |
/// Mask to get the huge page size. |
| 44 |
#[cfg(target_env = "ohos")] |
| 45 |
MFD_HUGE_MASK; |
| 46 |
/// hugetlb size of 64KB. |
| 47 |
#[cfg(target_env = "ohos")] |
| 48 |
MFD_HUGE_64KB; |
| 49 |
/// hugetlb size of 512KB. |
| 50 |
#[cfg(target_env = "ohos")] |
| 51 |
MFD_HUGE_512KB; |
| 52 |
/// Following are to be used with [`MFD_HUGETLB`], indicating the desired hugetlb size. |
| 53 |
/// |
| 54 |
/// See also the hugetlb filesystem in [`memfd_create(2)`]. |
| 55 |
/// |
| 56 |
/// [`memfd_create(2)`]: https://man7.org/linux/man-pages/man2/memfd_create.2.html |
| 57 |
#[cfg(linux_android)] |
| 58 |
MFD_HUGE_1MB; |
| 59 |
/// hugetlb size of 2MB. |
| 60 |
#[cfg(linux_android)] |
| 61 |
MFD_HUGE_2MB; |
| 62 |
/// hugetlb size of 8MB. |
| 63 |
#[cfg(linux_android)] |
| 64 |
MFD_HUGE_8MB; |
| 65 |
/// hugetlb size of 16MB. |
| 66 |
#[cfg(linux_android)] |
| 67 |
MFD_HUGE_16MB; |
| 68 |
/// hugetlb size of 32MB. |
| 69 |
#[cfg(linux_android)] |
| 70 |
MFD_HUGE_32MB; |
| 71 |
/// hugetlb size of 256MB. |
| 72 |
#[cfg(linux_android)] |
| 73 |
MFD_HUGE_256MB; |
| 74 |
/// hugetlb size of 512MB. |
| 75 |
#[cfg(linux_android)] |
| 76 |
MFD_HUGE_512MB; |
| 77 |
/// hugetlb size of 1GB. |
| 78 |
#[cfg(linux_android)] |
| 79 |
MFD_HUGE_1GB; |
| 80 |
/// hugetlb size of 2GB. |
| 81 |
#[cfg(linux_android)] |
| 82 |
MFD_HUGE_2GB; |
| 83 |
/// hugetlb size of 16GB. |
| 84 |
#[cfg(linux_android)] |
| 85 |
MFD_HUGE_16GB; |
| 86 |
} |
| 87 |
); |
| 88 |
|
| 89 |
#[deprecated(since = "0.30.0", note = "Use `MFdFlags instead`")] |
| 90 |
/// The deprecated MemFdCreateFlag type alias |
| 91 |
pub type MemFdCreateFlag = MFdFlags; |
| 92 |
|
| 93 |
/// Creates an anonymous file that lives in memory, and return a file-descriptor to it. |
| 94 |
/// |
| 95 |
/// The file behaves like a regular file, and so can be modified, truncated, memory-mapped, and so on. |
| 96 |
/// However, unlike a regular file, it lives in RAM and has a volatile backing storage. |
| 97 |
/// |
| 98 |
/// For more information, see [`memfd_create(2)`]. |
| 99 |
/// |
| 100 |
/// [`memfd_create(2)`]: https://man7.org/linux/man-pages/man2/memfd_create.2.html |
| 101 |
#[inline] // Delays codegen, preventing linker errors with dylibs and --no-allow-shlib-undefined |
| 102 |
pub fn memfd_create<P: NixPath + ?Sized>( |
| 103 |
name: &P, |
| 104 |
flags: MFdFlags, |
| 105 |
) -> Result<OwnedFd> { |
| 106 |
let res = name.with_nix_path(|cstr| { |
| 107 |
unsafe { |
| 108 |
cfg_if! { |
| 109 |
if #[cfg(all( |
| 110 |
// Android does not have a memfd_create symbol |
| 111 |
not(target_os = "android"), |
| 112 |
any( |
| 113 |
target_os = "freebsd", |
| 114 |
// If the OS is Linux, gnu/musl/ohos expose a memfd_create symbol but not uclibc |
| 115 |
target_env = "gnu", |
| 116 |
target_env = "musl", |
| 117 |
target_env = "ohos" |
| 118 |
)))] |
| 119 |
{ |
| 120 |
libc::memfd_create(cstr.as_ptr(), flags.bits()) |
| 121 |
} else { |
| 122 |
libc::syscall(libc::SYS_memfd_create, cstr.as_ptr(), flags.bits()) |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
})?; |
| 127 |
|
| 128 |
Errno::result(res).map(|r| unsafe { OwnedFd::from_raw_fd(r as RawFd) }) |
| 129 |
} |