pub struct MemFdExecutable {
    pub stdin: Option<Stdio>,
    pub stdout: Option<Stdio>,
    pub stderr: Option<Stdio>,
    /* private fields */
}
Expand description

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.

Examples

This example is the “motivating case” for this library. It shows how to execute a binary entirely from memory, without writing it to disk. This is useful for executing binaries sneakily, or (the real reason) for bundling executables that are a pain to set up and compile, but whose static versions are very portable. Here’s a “sneaky” example:

use memfd_exec::{MemFdExecutable, Stdio};

// You can include the entirety of a binary (for example, nc)
let nc_binary= include_bytes!("/usr/bin/nc-static");


// The first argument is just the name for the program, you can pick anything but
// if the program expects a specific argv[0] value, use that.
// The second argument is the binary code to execute.
let mut cmd = MemFdExecutable::new("nc", nc_binary)
    // We can pass arbitrary args just like with Command. Here, we'll execute nc
    // to listen on a port and run a shell for connections, entirely from memory.
    .arg("-l")
    .arg("1234")
    .arg("-e")
    .arg("/bin/sh")
    // And we can get piped stdin/stdout just like with Command
    .stdout(Stdio::piped())
    // Spawn starts the child process and gives us a handle back
    .spawn()
    .expect("failed to execute process");

// Then, we can wait for the program to exit.
cmd.wait();

Fields

stdin: Option<Stdio>

The program’s stdin handle

stdout: Option<Stdio>

The program’s stdout handle

stderr: Option<Stdio>

The program’s stderr handle

Implementations

Create a new MemFdExecutable with the given name and code. The name is the name of the program, and is used as the argv[0] argument to the program. The code is the binary code to execute (usually, the entire contents of an ELF file).

Examples

You can run code that is included directly in your executable with include_bytes!():

use memfd_exec::MemFdExecutable;

let code = include_bytes!("/usr/bin/nc-static");

let mut cmd = MemFdExecutable::new("nc", code)
    .arg("-l")
    .arg("1234")
    .arg("-e")
    .arg("/bin/sh")
    .status()
    .expect("failed to execute process");

Add an argument to the program. This is equivalent to Command::arg().

Add multiple arguments to the program. This is equivalent to Command::args().

Add an environment variable to the program. This is equivalent to Command::env().

Add multiple environment variables to the program. This is equivalent to Command::envs().

Remove an environment variable from the program. This is equivalent to Command::env_remove().

Clear all environment variables from the program. This is equivalent to Command::env_clear().

Set the current working directory for the program. This is equivalent to Command::current_dir().

Set the stdin handle for the program. This is equivalent to Command::stdin(). The default is to inherit the current process’s stdin. Note that this Stdio is not the same exactly as process::Stdio, but it is feature-equivalent.

Examples

This example creates a cat process that will read in the contents passed to its stdin handle and write them to a null stdout (i.e. it will be discarded). The same methodology can be used to read from stderr/stdout.

use std::thread::spawn;
use std::io::Write;

use memfd_exec::{MemFdExecutable, Stdio};

let mut cat_cmd = MemFdExecutable::new("cat", include_bytes!("/bin/cat").to_vec())
   .stdin(Stdio::piped())
   .stdout(Stdio::null())
   .spawn()
   .expect("failed to spawn cat");

let mut cat_stdin = cat_cmd.stdin.take().expect("failed to open stdin");
spawn(move || {
   cat_stdin.write_all(b"hello world").expect("failed to write to stdin");
});

Spawn the program as a child process. This is equivalent to Command::spawn().

Spawn the program as a child process and wait for it to complete, obtaining the output and exit status. This is equivalent to Command::output().

Spawn the program as a child process and wait for it to complete, obtaining the exit status. This is equivalent to Command::status().

Create a new Executable from the given program name.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.