From f75ae738c4ed7c81314256b4d22dd7e8cadf544b Mon Sep 17 00:00:00 2001 From: James Sturtevant Date: Wed, 9 Aug 2023 20:31:09 +0000 Subject: [PATCH] move code that is only used in the tests Signed-off-by: James Sturtevant --- .../containerd-shim-wasmedge/src/instance.rs | 41 ++++++++++--------- .../containerd-shim-wasmtime/src/instance.rs | 41 ++++++++++--------- 2 files changed, 42 insertions(+), 40 deletions(-) diff --git a/crates/containerd-shim-wasmedge/src/instance.rs b/crates/containerd-shim-wasmedge/src/instance.rs index 52ec2da82..777740232 100644 --- a/crates/containerd-shim-wasmedge/src/instance.rs +++ b/crates/containerd-shim-wasmedge/src/instance.rs @@ -1,7 +1,6 @@ use std::fs::File; use std::io::prelude::*; use std::io::ErrorKind; -use std::os::unix::io::RawFd; use std::sync::{Arc, Condvar, Mutex}; use std::thread; @@ -14,7 +13,7 @@ use containerd_shim_wasm::sandbox::instance_utils::{ get_instance_root, instance_exists, maybe_open_stdio, }; use containerd_shim_wasm::sandbox::{EngineGetter, Instance, InstanceConfig}; -use libc::{dup2, SIGINT, SIGKILL, STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO}; +use libc::{SIGINT, SIGKILL}; use log::{debug, error}; use nix::errno::Errno; use nix::sys::signal::Signal as NixSignal; @@ -39,10 +38,6 @@ use libcontainer::syscall::syscall::create_syscall; use crate::executor::WasmEdgeExecutor; -static mut STDIN_FD: Option = None; -static mut STDOUT_FD: Option = None; -static mut STDERR_FD: Option = None; - static DEFAULT_CONTAINER_ROOT_DIR: &str = "/run/containerd/wasmedge"; type ExitCode = (Mutex)>>, Condvar); @@ -59,20 +54,6 @@ pub struct Wasi { rootdir: PathBuf, } -pub fn reset_stdio() { - unsafe { - if STDIN_FD.is_some() { - dup2(STDIN_FD.unwrap(), STDIN_FILENO); - } - if STDOUT_FD.is_some() { - dup2(STDOUT_FD.unwrap(), STDOUT_FILENO); - } - if STDERR_FD.is_some() { - dup2(STDERR_FD.unwrap(), STDERR_FILENO); - } - } -} - #[derive(Serialize, Deserialize)] struct Options { root: Option, @@ -275,12 +256,14 @@ impl EngineGetter for Wasi { mod wasitest { use std::borrow::Cow; use std::fs::{create_dir, read_to_string, File, OpenOptions}; + use std::os::unix::io::RawFd; use std::os::unix::prelude::OpenOptionsExt; use std::sync::mpsc::channel; use std::time::Duration; use containerd_shim_wasm::function; use containerd_shim_wasm::sandbox::testutil::{has_cap_sys_admin, run_test_with_sudo}; + use libc::{dup2, SIGKILL, STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO}; use oci_spec::runtime::{ProcessBuilder, RootBuilder, SpecBuilder}; use tempfile::{tempdir, TempDir}; @@ -293,6 +276,24 @@ mod wasitest { wat2wasm, }; + static mut STDIN_FD: Option = None; + static mut STDOUT_FD: Option = None; + static mut STDERR_FD: Option = None; + + fn reset_stdio() { + unsafe { + if STDIN_FD.is_some() { + dup2(STDIN_FD.unwrap(), STDIN_FILENO); + } + if STDOUT_FD.is_some() { + dup2(STDOUT_FD.unwrap(), STDOUT_FILENO); + } + if STDERR_FD.is_some() { + dup2(STDERR_FD.unwrap(), STDERR_FILENO); + } + } + } + // This is taken from https://github.com/bytecodealliance/wasmtime/blob/6a60e8363f50b936e4c4fc958cb9742314ff09f3/docs/WASI-tutorial.md?plain=1#L270-L298 const WASI_HELLO_WAT: &[u8]= r#"(module ;; Import the required fd_write WASI function which will write the given io vectors to stdout diff --git a/crates/containerd-shim-wasmtime/src/instance.rs b/crates/containerd-shim-wasmtime/src/instance.rs index e7d12d1b3..40d9c0e6c 100644 --- a/crates/containerd-shim-wasmtime/src/instance.rs +++ b/crates/containerd-shim-wasmtime/src/instance.rs @@ -9,7 +9,6 @@ use nix::sys::wait::waitid; use serde::{Deserialize, Serialize}; use std::fs::File; use std::io::{ErrorKind, Read}; -use std::os::fd::RawFd; use std::path::{Path, PathBuf}; use std::sync::{Arc, Condvar, Mutex}; use std::thread; @@ -19,7 +18,7 @@ use chrono::{DateTime, Utc}; use containerd_shim_wasm::sandbox::error::Error; use containerd_shim_wasm::sandbox::instance::Wait; use containerd_shim_wasm::sandbox::{EngineGetter, Instance, InstanceConfig}; -use libc::{dup2, STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO}; + use libc::{SIGINT, SIGKILL}; use libcontainer::syscall::syscall::create_syscall; use log::error; @@ -33,10 +32,6 @@ use libcontainer::signal::Signal; static DEFAULT_CONTAINER_ROOT_DIR: &str = "/run/containerd/wasmtime"; type ExitCode = Arc<(Mutex)>>, Condvar)>; -static mut STDIN_FD: Option = None; -static mut STDOUT_FD: Option = None; -static mut STDERR_FD: Option = None; - pub struct Wasi { exit_code: ExitCode, engine: wasmtime::Engine, @@ -48,20 +43,6 @@ pub struct Wasi { id: String, } -pub fn reset_stdio() { - unsafe { - if STDIN_FD.is_some() { - dup2(STDIN_FD.unwrap(), STDIN_FILENO); - } - if STDOUT_FD.is_some() { - dup2(STDOUT_FD.unwrap(), STDOUT_FILENO); - } - if STDERR_FD.is_some() { - dup2(STDERR_FD.unwrap(), STDERR_FILENO); - } - } -} - #[derive(Serialize, Deserialize)] struct Options { root: Option, @@ -266,6 +247,7 @@ impl EngineGetter for Wasi { mod wasitest { use std::fs::{create_dir, read_to_string, File, OpenOptions}; use std::io::prelude::*; + use std::os::fd::RawFd; use std::os::unix::prelude::OpenOptionsExt; use std::sync::mpsc::channel; use std::time::Duration; @@ -273,11 +255,30 @@ mod wasitest { use containerd_shim_wasm::function; use containerd_shim_wasm::sandbox::instance::Wait; use containerd_shim_wasm::sandbox::testutil::{has_cap_sys_admin, run_test_with_sudo}; + use libc::{dup2, STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO}; use oci_spec::runtime::{ProcessBuilder, RootBuilder, SpecBuilder}; use tempfile::{tempdir, TempDir}; use super::*; + static mut STDIN_FD: Option = None; + static mut STDOUT_FD: Option = None; + static mut STDERR_FD: Option = None; + + fn reset_stdio() { + unsafe { + if STDIN_FD.is_some() { + dup2(STDIN_FD.unwrap(), STDIN_FILENO); + } + if STDOUT_FD.is_some() { + dup2(STDOUT_FD.unwrap(), STDOUT_FILENO); + } + if STDERR_FD.is_some() { + dup2(STDERR_FD.unwrap(), STDERR_FILENO); + } + } + } + // This is taken from https://github.com/bytecodealliance/wasmtime/blob/6a60e8363f50b936e4c4fc958cb9742314ff09f3/docs/WASI-tutorial.md?plain=1#L270-L298 const WASI_HELLO_WAT: &[u8]= r#"(module ;; Import the required fd_write WASI function which will write the given io vectors to stdout