diff --git a/crates/runc/src/io.rs b/crates/runc/src/io.rs index 7ac9fc4..83ef31e 100644 --- a/crates/runc/src/io.rs +++ b/crates/runc/src/io.rs @@ -122,53 +122,38 @@ pub struct PipedIo { impl PipedIo { pub fn new(uid: u32, gid: u32, opts: IOOption) -> std::io::Result { - 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 { - stdin, - stdout, - stderr, + stdin: Self::create_pipe(uid, gid, opts.open_stdin, true)?, + stdout: Self::create_pipe(uid, gid, opts.open_stdout, false)?, + stderr: Self::create_pipe(uid, gid, opts.open_stderr, false)?, }) } + + fn create_pipe( + uid: u32, + gid: u32, + enabled: bool, + stdin: bool, + ) -> std::io::Result> { + 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 {