summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2022-02-12 13:00:51 -0500
committerJesse Luehrs <doy@tozt.net>2022-02-12 13:00:51 -0500
commit42e976aaed4b4a01fc55cb56e789144849807aac (patch)
tree99bec8b9b60f04188e1423ae86e59dd2be9c1cd6 /src
parentba1b599d28bf902c635662ab37958128130f85f8 (diff)
downloadnbsh-42e976aaed4b4a01fc55cb56e789144849807aac.tar.gz
nbsh-42e976aaed4b4a01fc55cb56e789144849807aac.zip
simplify
Diffstat (limited to 'src')
-rw-r--r--src/prelude.rs37
-rw-r--r--src/runner/sys.rs26
2 files changed, 45 insertions, 18 deletions
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<T, E> Result for std::result::Result<T, E>
+ 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<nix::unistd::Pid>,
) -> 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(())
}