From 42e976aaed4b4a01fc55cb56e789144849807aac Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sat, 12 Feb 2022 13:00:51 -0500 Subject: simplify --- src/prelude.rs | 37 +++++++++++++++++++++++++++++++++++++ src/runner/sys.rs | 26 ++++++++------------------ 2 files changed, 45 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/prelude.rs b/src/prelude.rs index 6789a1f..c647591 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -9,3 +9,40 @@ pub use std::os::unix::ffi::{OsStrExt as _, OsStringExt as _}; pub use std::os::unix::io::{AsRawFd as _, FromRawFd as _, IntoRawFd as _}; pub use std::os::unix::process::ExitStatusExt as _; pub use users::os::unix::UserExt as _; + +pub use ext::Result as _; + +mod ext { + pub trait Result { + type T; + type E; + + fn allow(self, allow_e: Self::E) -> Self; + fn allow_with(self, allow_e: Self::E, default_t: Self::T) -> Self; + } + + impl Result for std::result::Result + where + T: std::default::Default, + E: std::cmp::PartialEq, + { + type T = T; + type E = E; + + fn allow(self, allow_e: Self::E) -> Self { + self.or_else(|e| { + if e == allow_e { + Ok(std::default::Default::default()) + } else { + Err(e) + } + }) + } + + fn allow_with(self, allow_e: Self::E, default_t: Self::T) -> Self { + self.or_else( + |e| if e == allow_e { Ok(default_t) } else { Err(e) }, + ) + } + } +} diff --git a/src/runner/sys.rs b/src/runner/sys.rs index 9cb1c2e..413882c 100644 --- a/src/runner/sys.rs +++ b/src/runner/sys.rs @@ -46,14 +46,8 @@ pub fn set_foreground_pg(pg: nix::unistd::Pid) -> anyhow::Result<()> { nix::unistd::close(pty)?; nix::sys::signal::kill(neg_pid(pg), nix::sys::signal::Signal::SIGCONT) - .or_else(|e| { - // the process group has already exited - if e == nix::errno::Errno::ESRCH { - Ok(()) - } else { - Err(e) - } - })?; + // the process group has already exited + .allow(nix::errno::Errno::ESRCH)?; Ok(()) } @@ -67,16 +61,12 @@ pub fn setpgid_parent( pid: nix::unistd::Pid, pg: Option, ) -> anyhow::Result<()> { - nix::unistd::setpgid(pid, pg.unwrap_or(PID0)).or_else(|e| { - // EACCES means that the child already called exec, but if it did, - // then it also must have already called setpgid itself, so we don't - // care. ESRCH means that the process already exited, which is similar - if e == nix::errno::Errno::EACCES || e == nix::errno::Errno::ESRCH { - Ok(()) - } else { - Err(e) - } - })?; + nix::unistd::setpgid(pid, pg.unwrap_or(PID0)) + // the child already called exec, so it must have already called + // setpgid itself + .allow(nix::errno::Errno::EACCES) + // the child already exited, so we don't care + .allow(nix::errno::Errno::ESRCH)?; Ok(()) } -- cgit v1.2.3-54-g00ecf