memfd/
lib.rs

1//! A pure-Rust library to work with Linux memfd and seals.
2//!
3//! It provides support for creating `memfd` objects on Linux
4//! and handling seals on them. This was first introduced in
5//! Linux kernel 3.17.
6//! For further details, see `memfd_create(2)` manpage.
7//!
8//! ```rust
9//! use memfd;
10//!
11//! fn new_sized_memfd() -> Result<memfd::Memfd, Box<dyn std::error::Error>> {
12//!     // Create a sealable memfd.
13//!     let opts = memfd::MemfdOptions::default().allow_sealing(true);
14//!     let mfd = opts.create("sized-1K")?;
15//!
16//!     // Resize to 1024B.
17//!     mfd.as_file().set_len(1024)?;
18//!
19//!     // Add seals to prevent further resizing.
20//!     mfd.add_seals(&[
21//!         memfd::FileSeal::SealShrink,
22//!         memfd::FileSeal::SealGrow
23//!     ])?;
24//!
25//!     // Prevent further sealing changes.
26//!     mfd.add_seal(memfd::FileSeal::SealSeal)?;
27//!
28//!     Ok(mfd)
29//! }
30//! ```
31#![deny(
32    missing_docs,
33    broken_intra_doc_links,
34    clippy::all,
35    unreachable_pub,
36    unused
37)]
38#![cfg_attr(docsrs, feature(doc_cfg))]
39#![cfg_attr(docsrs, doc(cfg(any(target_os = "android", target_os = "linux"))))]
40// No-op crate on platforms that do not support memfd_create, instead of failing to link, or at
41// runtime.
42#![cfg(any(target_os = "android", target_os = "linux"))]
43
44mod errors;
45mod memfd;
46mod sealing;
47
48pub use crate::{
49    errors::Error,
50    memfd::{HugetlbSize, Memfd, MemfdOptions},
51    sealing::{FileSeal, SealsHashSet},
52};