runc: simplify code a bit and refine doc
Simplify code in io.rs a bit, also refine doc and simplify code. Signed-off-by: Liu Jiang <gerry@linux.alibaba.com>
This commit is contained in:
parent
8c2a61a807
commit
be8046e832
|
|
@ -16,12 +16,9 @@
|
||||||
use std::fmt::{self, Debug, Formatter};
|
use std::fmt::{self, Debug, Formatter};
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::Result;
|
use std::io::Result;
|
||||||
use std::os::unix::io::FromRawFd;
|
use std::os::unix::io::{AsRawFd, FromRawFd};
|
||||||
use std::os::unix::prelude::AsRawFd;
|
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
|
|
||||||
use nix::fcntl::OFlag;
|
|
||||||
use nix::sys::stat::Mode;
|
|
||||||
use nix::unistd::{Gid, Uid};
|
use nix::unistd::{Gid, Uid};
|
||||||
|
|
||||||
use crate::Command;
|
use crate::Command;
|
||||||
|
|
@ -46,6 +43,7 @@ pub trait Io: Sync + Send {
|
||||||
/// Read side of stdin, write side of stdout and write side of stderr should be provided to command.
|
/// Read side of stdin, write side of stdout and write side of stderr should be provided to command.
|
||||||
fn set(&self, cmd: &mut Command) -> Result<()>;
|
fn set(&self, cmd: &mut Command) -> Result<()>;
|
||||||
|
|
||||||
|
/// Only close write side (should be stdout/err "from" runc process)
|
||||||
fn close_after_start(&self);
|
fn close_after_start(&self);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -72,9 +70,9 @@ impl Default for IOOption {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This struct represents pipe that can be used to transfer
|
/// Struct to represent a pipe that can be used to transfer stdio inputs and outputs.
|
||||||
/// stdio inputs and outputs
|
///
|
||||||
/// when one side of closed, this struct represent it with [`None`]
|
/// When one side of the pipe is closed, the state will be represented with [`None`].
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Pipe {
|
pub struct Pipe {
|
||||||
// Might be ugly hack: using mutex in order to take rd/wr under immutable [`Pipe`]
|
// Might be ugly hack: using mutex in order to take rd/wr under immutable [`Pipe`]
|
||||||
|
|
@ -175,31 +173,19 @@ impl PipedIo {
|
||||||
|
|
||||||
impl Io for PipedIo {
|
impl Io for PipedIo {
|
||||||
fn stdin(&self) -> Option<File> {
|
fn stdin(&self) -> Option<File> {
|
||||||
if let Some(ref stdin) = self.stdin {
|
self.stdin.as_ref().map(|v| v.take_write()).flatten()
|
||||||
stdin.take_write()
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn stdout(&self) -> Option<File> {
|
fn stdout(&self) -> Option<File> {
|
||||||
if let Some(ref stdout) = self.stdout {
|
self.stdout.as_ref().map(|v| v.take_read()).flatten()
|
||||||
stdout.take_read()
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn stderr(&self) -> Option<File> {
|
fn stderr(&self) -> Option<File> {
|
||||||
if let Some(ref stderr) = self.stderr {
|
self.stderr.as_ref().map(|v| v.take_read()).flatten()
|
||||||
stderr.take_read()
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Note that this internally use [`std::fs::File`]'s [`try_clone()`].
|
// Note that this internally use [`std::fs::File`]'s `try_clone()`.
|
||||||
/// Thus, the files passed to commands will be not closed after command exit.
|
// Thus, the files passed to commands will be not closed after command exit.
|
||||||
fn set(&self, cmd: &mut Command) -> std::io::Result<()> {
|
fn set(&self, cmd: &mut Command) -> std::io::Result<()> {
|
||||||
if let Some(ref p) = self.stdin {
|
if let Some(ref p) = self.stdin {
|
||||||
let m = p.rd.lock().unwrap();
|
let m = p.rd.lock().unwrap();
|
||||||
|
|
@ -224,10 +210,10 @@ impl Io for PipedIo {
|
||||||
cmd.stderr(f);
|
cmd.stderr(f);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// closing only write side (should be stdout/err "from" runc process)
|
|
||||||
fn close_after_start(&self) {
|
fn close_after_start(&self) {
|
||||||
if let Some(ref p) = self.stdout {
|
if let Some(ref p) = self.stdout {
|
||||||
p.close_write();
|
p.close_write();
|
||||||
|
|
@ -246,7 +232,11 @@ pub struct NullIo {
|
||||||
|
|
||||||
impl NullIo {
|
impl NullIo {
|
||||||
pub fn new() -> std::io::Result<Self> {
|
pub fn new() -> std::io::Result<Self> {
|
||||||
let fd = nix::fcntl::open("/dev/null", OFlag::O_RDONLY, Mode::empty())?;
|
let fd = nix::fcntl::open(
|
||||||
|
"/dev/null",
|
||||||
|
nix::fcntl::OFlag::O_RDONLY,
|
||||||
|
nix::sys::stat::Mode::empty(),
|
||||||
|
)?;
|
||||||
let dev_null = unsafe { Mutex::new(Some(std::fs::File::from_raw_fd(fd))) };
|
let dev_null = unsafe { Mutex::new(Some(std::fs::File::from_raw_fd(fd))) };
|
||||||
Ok(Self { dev_null })
|
Ok(Self { dev_null })
|
||||||
}
|
}
|
||||||
|
|
@ -261,7 +251,6 @@ impl Io for NullIo {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// closing only write side (should be stdout/err "from" runc process)
|
|
||||||
fn close_after_start(&self) {
|
fn close_after_start(&self) {
|
||||||
let mut m = self.dev_null.lock().unwrap();
|
let mut m = self.dev_null.lock().unwrap();
|
||||||
let _ = m.take();
|
let _ = m.take();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue