runc: add helper PipedIo::create_pipe()
Add helper PipedIo::create_pipe() to reduce duplicated code. Signed-off-by: Liu Jiang <gerry@linux.alibaba.com>
This commit is contained in:
parent
be8046e832
commit
a52bcd0c7e
|
|
@ -122,53 +122,38 @@ pub struct PipedIo {
|
||||||
|
|
||||||
impl PipedIo {
|
impl PipedIo {
|
||||||
pub fn new(uid: u32, gid: u32, opts: IOOption) -> std::io::Result<Self> {
|
pub fn new(uid: u32, gid: u32, opts: IOOption) -> std::io::Result<Self> {
|
||||||
let uid = Some(Uid::from_raw(uid));
|
|
||||||
let gid = Some(Gid::from_raw(gid));
|
|
||||||
let stdin = if opts.open_stdin {
|
|
||||||
let pipe = Pipe::new()?;
|
|
||||||
{
|
|
||||||
let m = pipe.rd.lock().unwrap();
|
|
||||||
if let Some(f) = m.as_ref() {
|
|
||||||
nix::unistd::fchown(f.as_raw_fd(), uid, gid)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some(pipe)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
|
|
||||||
let stdout = if opts.open_stdout {
|
|
||||||
let pipe = Pipe::new()?;
|
|
||||||
{
|
|
||||||
let m = pipe.wr.lock().unwrap();
|
|
||||||
if let Some(f) = m.as_ref() {
|
|
||||||
nix::unistd::fchown(f.as_raw_fd(), uid, gid)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some(pipe)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
|
|
||||||
let stderr = if opts.open_stderr {
|
|
||||||
let pipe = Pipe::new()?;
|
|
||||||
{
|
|
||||||
let m = pipe.wr.lock().unwrap();
|
|
||||||
if let Some(f) = m.as_ref() {
|
|
||||||
nix::unistd::fchown(f.as_raw_fd(), uid, gid)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some(pipe)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
stdin,
|
stdin: Self::create_pipe(uid, gid, opts.open_stdin, true)?,
|
||||||
stdout,
|
stdout: Self::create_pipe(uid, gid, opts.open_stdout, false)?,
|
||||||
stderr,
|
stderr: Self::create_pipe(uid, gid, opts.open_stderr, false)?,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn create_pipe(
|
||||||
|
uid: u32,
|
||||||
|
gid: u32,
|
||||||
|
enabled: bool,
|
||||||
|
stdin: bool,
|
||||||
|
) -> std::io::Result<Option<Pipe>> {
|
||||||
|
if !enabled {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
|
||||||
|
let pipe = Pipe::new()?;
|
||||||
|
let guard = if stdin {
|
||||||
|
pipe.rd.lock().unwrap()
|
||||||
|
} else {
|
||||||
|
pipe.wr.lock().unwrap()
|
||||||
|
};
|
||||||
|
if let Some(f) = guard.as_ref() {
|
||||||
|
let uid = Some(Uid::from_raw(uid));
|
||||||
|
let gid = Some(Gid::from_raw(gid));
|
||||||
|
nix::unistd::fchown(f.as_raw_fd(), uid, gid)?;
|
||||||
|
}
|
||||||
|
drop(guard);
|
||||||
|
|
||||||
|
Ok(Some(pipe))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Io for PipedIo {
|
impl Io for PipedIo {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue