Skip to content

Commit

Permalink
Make the utility maybe_open_stdio cross platform
Browse files Browse the repository at this point in the history
Signed-off-by: James Sturtevant <[email protected]>
  • Loading branch information
jsturtevant committed Aug 15, 2023
1 parent 5fba436 commit e9a95b8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
7 changes: 3 additions & 4 deletions crates/containerd-shim-wasm/src/sandbox/instance_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
use crate::sandbox::error::Error;
use anyhow::{bail, Context, Result};
use std::{
fs::{self, OpenOptions},
fs::{self, File, OpenOptions},
io::ErrorKind,
os::fd::{IntoRawFd, RawFd},
path::{Path, PathBuf},
};

Expand Down Expand Up @@ -33,12 +32,12 @@ pub fn instance_exists<P: AsRef<Path>>(root_path: P, container_id: &str) -> Resu
/// containerd can send an empty path or a non-existant path
/// In both these cases we should just assume that the stdio stream was not setup (intentionally)
/// Any other error is a real error.
pub fn maybe_open_stdio(path: &str) -> Result<Option<RawFd>, Error> {
pub fn maybe_open_stdio(path: &str) -> Result<Option<File>, Error> {
if path.is_empty() {
return Ok(None);
}
match OpenOptions::new().read(true).write(true).open(path) {
Ok(f) => Ok(Some(f.into_raw_fd())),
Ok(f) => Ok(Some(f)),
Err(err) => match err.kind() {
ErrorKind::NotFound => Ok(None),
_ => Err(err.into()),
Expand Down
23 changes: 15 additions & 8 deletions crates/containerd-shim-wasmedge/src/instance.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
use std::fs::File;
use std::io::prelude::*;
use std::io::ErrorKind;
use std::sync::{Arc, Condvar, Mutex};

use anyhow::Context;
use anyhow::Result;
use containerd_shim_wasm::libcontainer_instance::LibcontainerInstance;
Expand All @@ -13,6 +8,11 @@ use containerd_shim_wasm::sandbox::instance_utils::maybe_open_stdio;
use containerd_shim_wasm::sandbox::{EngineGetter, InstanceConfig};
use nix::unistd::close;
use serde::{Deserialize, Serialize};
use std::fs::File;
use std::io::prelude::*;
use std::io::ErrorKind;
use std::os::fd::IntoRawFd;
use std::sync::{Arc, Condvar, Mutex};
use wasmedge_sdk::{
config::{CommonConfigOptions, ConfigBuilder, HostRegistrationConfigOptions},
plugin::PluginManager,
Expand Down Expand Up @@ -101,9 +101,16 @@ impl LibcontainerInstance for Wasi {

fn build_container(&self) -> std::result::Result<Container, Error> {
fs::create_dir_all(&self.rootdir)?;
let stdin = maybe_open_stdio(self.stdin.as_str()).context("could not open stdin")?;
let stdout = maybe_open_stdio(self.stdout.as_str()).context("could not open stdout")?;
let stderr = maybe_open_stdio(self.stderr.as_str()).context("could not open stderr")?;

let stdin = maybe_open_stdio(self.stdin.as_str())
.context("could not open stdin")?
.map(|f| f.into_raw_fd());
let stdout = maybe_open_stdio(self.stdout.as_str())
.context("could not open stdout")?
.map(|f| f.into_raw_fd());
let stderr = maybe_open_stdio(self.stderr.as_str())
.context("could not open stderr")?
.map(|f| f.into_raw_fd());

let syscall = create_syscall();
let err_others = |err| Error::Others(format!("failed to create container: {}", err));
Expand Down
14 changes: 11 additions & 3 deletions crates/containerd-shim-wasmtime/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ 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 libcontainer::syscall::syscall::create_syscall;
use std::os::fd::IntoRawFd;

use wasmtime::Engine;

use crate::executor::WasmtimeExecutor;

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

pub struct Wasi {
Expand Down Expand Up @@ -99,9 +101,15 @@ impl LibcontainerInstance for Wasi {
fn build_container(&self) -> std::result::Result<Container, Error> {
let engine = self.engine.clone();
let syscall = create_syscall();
let stdin = maybe_open_stdio(&self.stdin).context("could not open stdin")?;
let stdout = maybe_open_stdio(&self.stdout).context("could not open stdout")?;
let stderr = maybe_open_stdio(&self.stderr).context("could not open stderr")?;
let stdin = maybe_open_stdio(&self.stdin)
.context("could not open stdin")?
.map(|f| f.into_raw_fd());
let stdout = maybe_open_stdio(&self.stdout)
.context("could not open stdout")?
.map(|f| f.into_raw_fd());
let stderr = maybe_open_stdio(&self.stderr)
.context("could not open stderr")?
.map(|f| f.into_raw_fd());
let err_others = |err| Error::Others(format!("failed to create container: {}", err));

let wasmtime_executor = Box::new(WasmtimeExecutor::new(stdin, stdout, stderr, engine));
Expand Down
11 changes: 1 addition & 10 deletions crates/containerd-shim-wasmtime/src/oci_wasmtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ use std::fs::OpenOptions;
use std::path::Path;

use anyhow::Context;
use cap_std::fs::File as CapFile;
use containerd_shim_wasm::sandbox::{error::Error, oci};
use oci_spec::runtime::Spec;
use wasmtime_wasi::sync::file::File as WasiFile;

use wasmtime_wasi::{Dir as WasiDir, WasiCtxBuilder};

pub fn get_rootfs(spec: &Spec) -> Result<WasiDir, Error> {
Expand Down Expand Up @@ -77,11 +76,3 @@ pub fn wasi_dir(path: &str, opts: &OpenOptions) -> Result<WasiDir, std::io::Erro
let f = opts.open(path)?;
Ok(WasiDir::from_std_file(f))
}

pub fn wasi_file<P: AsRef<Path>>(
path: P,
opts: &mut OpenOptions,
) -> Result<WasiFile, std::io::Error> {
let f = opts.open(path)?;
Ok(WasiFile::from_cap_std(CapFile::from_std(f)))
}

0 comments on commit e9a95b8

Please sign in to comment.