memfd_exec/
lib.rs

1//! This is a very simple crate that allows execution of in-memory only programs. Simply
2//! put, if you have the contents of a Linux executable in a `Vec<u8>`, you can use
3//! `memfd_exec` to execute the program without it ever touching your hard disk. Use
4//! cases for this may include:
5//!
6//! * Bundling a static executable with another program (for example, my motivation to
7//!   create this package is that I want to ship a statically built QEMU with
8//!   [cantrace](https://github.com/novafacing/cannoli))
9//! * Sending executables over the network and running them, to reduce footprint and increase
10//!   throughput
11//!
12//! # Example
13//!
14//! The following example will download and run a qemu-x86_64 executable from the internet,
15//! without ever writing the executable to disk.
16//!
17//! ```
18//! use memfd_exec::{MemFdExecutable, Stdio};
19//! use reqwest::blocking::get;
20//!
21//! const URL: &str = "https://novafacing.github.io/assets/qemu-x86_64";
22//! let resp = get(URL).unwrap();
23//!
24//! // The `MemFdExecutable` struct is at near feature-parity with `std::process::Command`,
25//! // so you can use it in the same way. The only difference is that you must provide the
26//! // executable contents as a `Vec<u8>` as well as telling it the argv[0] to use.
27//! let qemu = MemFdExecutable::new("qemu-x86_64", resp.bytes().unwrap().as_ref())
28//!     // We'll just get the version here, but you can do anything you want with the
29//!     // args.
30//!     .arg("-version")
31//!     // We'll capture the stdout of the process, so we need to set up a pipe.
32//!     .stdout(Stdio::piped())
33//!     // Spawn the process as a forked child
34//!     .spawn()
35//!     .unwrap();
36//!
37//! // Get the output and status code of the process (this will block until the process
38//! // exits)
39//! let output = qemu.wait_with_output().unwrap();
40//! assert!(output.status.into_raw() == 0);
41//! // Print out the version we got!
42//! println!("{}", String::from_utf8_lossy(&output.stdout));
43//! ```
44//!
45// #![feature(exact_size_is_empty)]
46// #![feature(exit_status_error)]
47// #![feature(raw_os_nonzero)]
48// #![feature(read_buf)]
49// #![feature(can_vector)]
50// #![feature(never_type)]
51
52mod anon_pipe;
53mod child;
54mod command_env;
55mod cvt;
56mod executable;
57mod file_desc;
58mod output;
59mod process;
60mod stdio;
61
62pub use child::{Child, ChildStderr, ChildStdin, ChildStdout};
63pub use executable::MemFdExecutable;
64pub use output::Output;
65pub use process::ExitStatus;
66pub use stdio::Stdio;