From 175c6dde834e73c460876257359c82a1a49b96fb Mon Sep 17 00:00:00 2001 From: Will Woods Date: Thu, 30 Sep 2021 20:54:09 -0400 Subject: [PATCH] wasmldr: read module from fd 3 if no path given This commit makes wasmldr try to read the wasm module from fd3 if no file path was given. (Since keepldr doesn't currently pass arguments to wasmldr, this will be the default behavior.) This means you should be able to run a .wasm module in a keep by doing: cargo run -- exec 3< hello.wasm This behavior will likely change soon, but hey.. Hello World! Signed-off-by: Will Woods --- internal/wasmldr/src/cli.rs | 4 ++-- internal/wasmldr/src/main.rs | 11 ++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/internal/wasmldr/src/cli.rs b/internal/wasmldr/src/cli.rs index f41c825c1..7f70aef69 100644 --- a/internal/wasmldr/src/cli.rs +++ b/internal/wasmldr/src/cli.rs @@ -24,8 +24,8 @@ pub struct RunOptions { // TODO: --inherit-env // TODO: --stdin, --stdout, --stderr /// Path of the WebAssembly module to run - #[structopt(index = 1, required = true, value_name = "MODULE", parse(from_os_str))] - pub module: PathBuf, + #[structopt(index = 1, value_name = "MODULE", parse(from_os_str))] + pub module: Option, // NOTE: this has to come last for TrailingVarArg /// Arguments to pass to the WebAssembly module diff --git a/internal/wasmldr/src/main.rs b/internal/wasmldr/src/main.rs index 0c542085b..8160d117b 100644 --- a/internal/wasmldr/src/main.rs +++ b/internal/wasmldr/src/main.rs @@ -36,6 +36,7 @@ use structopt::StructOpt; use std::fs::File; use std::io::Read; +use std::os::unix::io::{FromRawFd, RawFd}; fn main() { // Initialize the logger, taking settings from the default env vars @@ -47,9 +48,13 @@ fn main() { let opts = cli::RunOptions::from_args(); info!("opts: {:#?}", opts); - info!("reading {:?}", opts.module); - // TODO: don't just panic here... - let mut reader = File::open(&opts.module).expect("Unable to open file"); + let mut reader = if let Some(module) = opts.module { + info!("reading module from {:?}", &module); + File::open(&module).expect("Unable to open file") + } else { + info!("reading module from fd 3"); + unsafe { File::from_raw_fd(RawFd::from(3)) } + }; let mut bytes = Vec::new(); reader