RunOptions

Struct RunOptions 

Source
pub struct RunOptions<'a> { /* private fields */ }
Expand description

Options which can be used to customize arguments, environment and how the ELF is executed.

Implementations§

Source§

impl<'a> RunOptions<'a>

Source

pub fn new() -> Self

Creates a blank new set of options ready for configuration.

All options are initially empty / set to false.

Source

pub fn with_replace(self, replace: bool) -> Self

Toggles the replace mode. If set to true, the current process will be replaced by the executed binary. Otherwise, fork() will be called and the current process will be able to wait for the child.

Source

pub fn with_args(self, args: &'a [&'a str]) -> Self

Set command line arguments for the executed binary.

These are the actual arguments passed to the program (argv[1], argv[2], etc.). The program name (argv[0]) is automatically set to the memfd path.

§Example
use memfd_runner::{run_with_options, RunOptions};

let elf_bytes = std::fs::read("/usr/bin/echo").unwrap();
let options = RunOptions::new().with_args(&["Hello", "World!"]);
let exit_code = run_with_options(&elf_bytes, options).unwrap();
// This executes: /proc/self/fd/X "Hello" "World!"
Source

pub fn with_env(self, env: &'a [&'a str]) -> Self

Set environment variables for the executed binary. Environment variables should be in “KEY=value” format.

§Example
use memfd_runner::{run_with_options, RunOptions};

let elf_bytes = std::fs::read("/usr/bin/env").unwrap();
let options = RunOptions::new().with_env(&["PATH=/usr/bin", "HOME=/tmp"]);
let exit_code = run_with_options(&elf_bytes, options).unwrap();
Source

pub fn with_argv0(self, argv0: &'a str) -> Self

Set a custom argv[0] for the executed binary.

By default, argv[0] is set to the memfd path (/proc/self/fd/N). This method allows you to customize what the executed program sees as its program name.

§Example
use memfd_runner::{run_with_options, RunOptions};

let elf_bytes = std::fs::read("/usr/bin/echo").unwrap();
let options = RunOptions::new()
    .with_argv0("my-custom-program")
    .with_args(&["Hello", "World!"]);
let exit_code = run_with_options(&elf_bytes, options).unwrap();
// The program sees argv[0] as "my-custom-program"

Trait Implementations§

Source§

impl<'a> Clone for RunOptions<'a>

Source§

fn clone(&self) -> RunOptions<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Default for RunOptions<'a>

Source§

fn default() -> RunOptions<'a>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for RunOptions<'a>

§

impl<'a> RefUnwindSafe for RunOptions<'a>

§

impl<'a> Send for RunOptions<'a>

§

impl<'a> Sync for RunOptions<'a>

§

impl<'a> Unpin for RunOptions<'a>

§

impl<'a> UnwindSafe for RunOptions<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.