Skip to content

Commit

Permalink
Merge pull request containerd#239 from jsturtevant/move-test-dps
Browse files Browse the repository at this point in the history
  • Loading branch information
cpuguy83 authored Aug 15, 2023
2 parents 74248dd + ab80487 commit 5fba436
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 36 deletions.
41 changes: 20 additions & 21 deletions crates/containerd-shim-wasmedge/src/instance.rs
Original file line number Diff line number Diff line change
@@ -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 anyhow::Context;
Expand All @@ -12,7 +11,6 @@ use containerd_shim_wasm::sandbox::error::Error;
use containerd_shim_wasm::sandbox::instance::ExitCode;
use containerd_shim_wasm::sandbox::instance_utils::maybe_open_stdio;
use containerd_shim_wasm::sandbox::{EngineGetter, InstanceConfig};
use libc::{dup2, STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO};
use nix::unistd::close;
use serde::{Deserialize, Serialize};
use wasmedge_sdk::{
Expand All @@ -32,10 +30,6 @@ use libcontainer::syscall::syscall::create_syscall;

use crate::executor::WasmEdgeExecutor;

static mut STDIN_FD: Option<RawFd> = None;
static mut STDOUT_FD: Option<RawFd> = None;
static mut STDERR_FD: Option<RawFd> = None;

static DEFAULT_CONTAINER_ROOT_DIR: &str = "/run/containerd/wasmedge";

pub struct Wasi {
Expand All @@ -51,20 +45,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<PathBuf>,
Expand Down Expand Up @@ -176,6 +156,7 @@ 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;
Expand All @@ -185,7 +166,7 @@ mod wasitest {
use containerd_shim_wasm::sandbox::instance::Wait;
use containerd_shim_wasm::sandbox::testutil::{has_cap_sys_admin, run_test_with_sudo};
use containerd_shim_wasm::sandbox::Instance;
use libc::SIGKILL;
use libc::{dup2, SIGKILL, STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO};
use oci_spec::runtime::{ProcessBuilder, RootBuilder, SpecBuilder};
use tempfile::{tempdir, TempDir};

Expand All @@ -198,6 +179,24 @@ mod wasitest {
wat2wasm,
};

static mut STDIN_FD: Option<RawFd> = None;
static mut STDOUT_FD: Option<RawFd> = None;
static mut STDERR_FD: Option<RawFd> = None;

fn reset_stdio() {
unsafe {
if let Some(stdin) = STDIN_FD {
let _ = dup2(stdin, STDIN_FILENO);
}
if let Some(stdout) = STDOUT_FD {
let _ = dup2(stdout, STDOUT_FILENO);
}
if let Some(stderr) = STDERR_FD {
let _ = dup2(stderr, 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
Expand Down
30 changes: 15 additions & 15 deletions crates/containerd-shim-wasmtime/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,25 @@ mod wasitest {
use oci_spec::runtime::{ProcessBuilder, RootBuilder, SpecBuilder};
use tempfile::{tempdir, TempDir};

use super::*;

static mut STDIN_FD: Option<RawFd> = None;
static mut STDOUT_FD: Option<RawFd> = None;
static mut STDERR_FD: Option<RawFd> = None;

use super::*;
fn reset_stdio() {
unsafe {
if let Some(stdin) = STDIN_FD {
let _ = dup2(stdin, STDIN_FILENO);
}
if let Some(stdout) = STDOUT_FD {
let _ = dup2(stdout, STDOUT_FILENO);
}
if let Some(stderr) = STDERR_FD {
let _ = dup2(stderr, STDERR_FILENO);
}
}
}

// This is taken from https://github.com/bytecodealliance/wasmtime/blob/6a60e8363f50b936e4c4fc958cb9742314ff09f3/docs/WASI-tutorial.md?plain=1#L270-L298
fn hello_world_module(start_fn: Option<&str>) -> Vec<u8> {
Expand Down Expand Up @@ -337,18 +351,4 @@ mod wasitest {
wasi.delete()?;
res
}

fn reset_stdio() {
unsafe {
if STDIN_FD.is_some() {
let _ = dup2(STDIN_FD.unwrap(), STDIN_FILENO);
}
if STDOUT_FD.is_some() {
let _ = dup2(STDOUT_FD.unwrap(), STDOUT_FILENO);
}
if STDERR_FD.is_some() {
let _ = dup2(STDERR_FD.unwrap(), STDERR_FILENO);
}
}
}
}

0 comments on commit 5fba436

Please sign in to comment.