Crate memfd_exec
source ·Expand description
This is a very simple crate that allows execution of in-memory only programs. Simply
put, if you have the contents of a Linux executable in a Vec<u8>
, you can use
memfd_exec
to execute the program without it ever touching your hard disk. Use
cases for this may include:
- Bundling a static executable with another program (for example, my motivation to create this package is that I want to ship a statically built QEMU with cantrace)
- Sending executables over the network and running them, to reduce footprint and increase throughput
Example
The following example will download and run a qemu-x86_64 executable from the internet, without ever writing the executable to disk.
use memfd_exec::{MemFdExecutable, Stdio};
use reqwest::blocking::get;
const URL: &str = "https://novafacing.github.io/assets/qemu-x86_64";
let resp = get(URL).unwrap();
// The `MemFdExecutable` struct is at near feature-parity with `std::process::Command`,
// so you can use it in the same way. The only difference is that you must provide the
// executable contents as a `Vec<u8>` as well as telling it the argv[0] to use.
let qemu = MemFdExecutable::new("qemu-x86_64", resp.bytes().unwrap().as_ref())
// We'll just get the version here, but you can do anything you want with the
// args.
.arg("-version")
// We'll capture the stdout of the process, so we need to set up a pipe.
.stdout(Stdio::piped())
// Spawn the process as a forked child
.spawn()
.unwrap();
// Get the output and status code of the process (this will block until the process
// exits)
let output = qemu.wait_with_output().unwrap();
assert!(output.status.into_raw() == 0);
// Print out the version we got!
println!("{}", String::from_utf8_lossy(&output.stdout));
Structs
- A child process created from a
MemFdExecutable
with handles to input and output streams - A handle to a child process’s stderr.
- A handle to a child process’s standard input (stdin).
- A handle to a child process’s standard output (stdout).
- Describes the result of a process after it has terminated.
- This is the main struct used to create an in-memory only executable. Wherever possible, it is intended to be a drop-in replacement for the standard library’s
process::Command
struct. - The output of a child process, including the exit status and output streams.
Enums
- Description of a stdio stream for a child process