-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jorge Prendes <[email protected]>
- Loading branch information
Showing
9 changed files
with
164 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#[cfg_attr(unix, path = "sys/unix/stdio.rs")] | ||
#[cfg_attr(windows, path = "sys/windows/stdio.rs")] | ||
mod stdio_impl; | ||
|
||
use std::fs::{File, OpenOptions}; | ||
use std::io::ErrorKind::NotFound; | ||
use std::io::{Error, Result}; | ||
use std::path::Path; | ||
use std::sync::{Arc, Mutex}; | ||
|
||
use stdio_impl::*; | ||
|
||
#[derive(Default, Clone)] | ||
pub struct Stdio { | ||
pub stdin: Stdin, | ||
pub stdout: Stdout, | ||
pub stderr: Stderr, | ||
} | ||
|
||
impl Stdio { | ||
pub fn redirect(&self) -> Result<()> { | ||
self.stdin.redirect()?; | ||
self.stdout.redirect()?; | ||
self.stderr.redirect()?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
macro_rules! stdio_impl { | ||
( $stdio_type:ident, $fd:expr ) => { | ||
#[derive(Default, Clone)] | ||
pub struct $stdio_type(Arc<Mutex<Option<File>>>); | ||
|
||
impl<P: AsRef<Path>> TryFrom<Option<P>> for $stdio_type { | ||
type Error = std::io::Error; | ||
fn try_from(path: Option<P>) -> Result<Self> { | ||
path.and_then(|path| match path.as_ref() { | ||
path if path.as_os_str().is_empty() => None, | ||
path => Some(path.to_owned()), | ||
}) | ||
.map( | ||
|path| match OpenOptions::new().read(true).write(true).open(path) { | ||
Err(err) if err.kind() == NotFound => Ok(None), | ||
Ok(f) => Ok(Some(f)), | ||
Err(err) => Err(err), | ||
}, | ||
) | ||
.transpose() | ||
.map(Option::flatten) | ||
.map(Mutex::new) | ||
.map(Arc::new) | ||
.map(Self) | ||
} | ||
} | ||
|
||
impl $stdio_type { | ||
pub fn redirect(&self) -> Result<()> { | ||
if let Some(f) = self.0.try_lock().ok().and_then(|mut f| f.take()) { | ||
let f = try_into_fd(f)?; | ||
let _ = unsafe { libc::dup($fd) }; | ||
if unsafe { libc::dup2(f.as_raw_fd(), $fd) } == -1 { | ||
return Err(Error::last_os_error()); | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
stdio_impl!(Stdin, STDIN_FILENO); | ||
stdio_impl!(Stdout, STDOUT_FILENO); | ||
stdio_impl!(Stderr, STDERR_FILENO); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use std::io::Result; | ||
pub use std::os::fd::AsRawFd; | ||
use std::os::fd::OwnedFd; | ||
|
||
pub use libc::{STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO}; | ||
|
||
pub fn try_into_fd(f: impl Into<OwnedFd>) -> Result<impl AsRawFd> { | ||
Ok(f.into()) | ||
} |
42 changes: 42 additions & 0 deletions
42
crates/containerd-shim-wasm/src/sandbox/sys/windows/stdio.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use std::io::{Error, ErrorKind, Result}; | ||
use std::os::windows::prelude::{AsRawHandle, IntoRawHandle, OwnedHandle}; | ||
|
||
type RawFd = libc::c_int; | ||
|
||
pub static STDIN_FILENO: RawFd = 0; | ||
pub static STDOUT_FILENO: RawFd = 1; | ||
pub static STDERR_FILENO: RawFd = 2; | ||
|
||
struct OwnedFd { | ||
fd: RawFd, | ||
} | ||
|
||
pub fn try_into_fd(f: impl Into<OwnedHandle>) -> Result<impl AsRawFd> { | ||
let handle = f.into(); | ||
let raw = handle.as_raw_handle() as libc::intptr_t; | ||
let fd = unsafe { libc::open_osfhandle(raw, libc::O_APPEND) }; | ||
if fd == -1 { | ||
return Err(Error::new( | ||
ErrorKind::Other, | ||
"Failed to open file descriptor", | ||
)); | ||
} | ||
let _ = handle.into_raw_handle(); // drop ownership of the handle, it's managed by fd now | ||
Ok(OwnedFd { fd }) | ||
} | ||
|
||
pub trait AsRawFd { | ||
fn as_raw_fd(&self) -> RawFd; | ||
} | ||
|
||
impl AsRawFd for OwnedFd { | ||
fn as_raw_fd(&self) -> RawFd { | ||
self.fd | ||
} | ||
} | ||
|
||
impl Drop for OwnedFd { | ||
fn drop(&mut self) { | ||
unsafe { libc::close(self.fd) }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.