Skip to content

Commit

Permalink
Simplify signal formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdrz committed Jul 5, 2023
1 parent b56edf5 commit 43d4666
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 23 deletions.
13 changes: 8 additions & 5 deletions src/exec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,14 @@ fn handle_sigchld<T: HandleSigchld>(
}

fn signal_fmt(signal: SignalNumber) -> Cow<'static, str> {
signal_name(signal)
.or_else(|| (signal == SIGCONT_FG).then_some("SIGCONT_FG"))
.or_else(|| (signal == SIGCONT_BG).then_some("SIGCONT_BG"))
.map(|name| name.into())
.unwrap_or_else(|| format!("unknown signal #{}", signal).into())
match signal_name(signal) {
name @ Cow::Owned(_) => match signal {
SIGCONT_BG => "SIGCONT_BG".into(),
SIGCONT_FG => "SIGCONT_FG".into(),
_ => name,
},
name => name,
}
}

const fn cond_fmt<'a>(cond: bool, true_s: &'a str, false_s: &'a str) -> &'a str {
Expand Down
6 changes: 2 additions & 4 deletions src/system/signal/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl SignalHandler {
if Self::FORBIDDEN.contains(&signal) {
panic!(
"the {} signal action cannot be overriden",
signal_name(signal).unwrap()
signal_name(signal)
);
}

Expand Down Expand Up @@ -56,9 +56,7 @@ impl Drop for SignalHandler {
if let Err(err) = self.original_action.register(signal) {
dev_warn!(
"cannot restore original action for {}: {err}",
signal_name(signal)
.map(Cow::Borrowed)
.unwrap_or_else(|| Cow::Owned(format!("unknown signal {signal}"))),
signal_name(signal),
)
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/system/signal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub(crate) use handler::{SignalHandler, SignalHandlerBehavior};
pub(crate) use set::SignalSet;
pub(crate) use stream::{register_handlers, SignalStream};

use std::borrow::Cow;

pub(crate) type SignalNumber = libc::c_int;

macro_rules! define_consts {
Expand All @@ -16,10 +18,10 @@ macro_rules! define_consts {
pub(crate) use libc::{$($signal,)*};
}

pub(crate) fn signal_name(signal: SignalNumber) -> Option<&'static str> {
pub(crate) fn signal_name(signal: SignalNumber) -> Cow<'static, str> {
match signal {
$(consts::$signal => Some(stringify!($signal)),)*
_ => None,
$(consts::$signal => stringify!($signal).into(),)*
_ => format!("unknown signal ({signal})").into(),
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/system/signal/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub(crate) fn register_handlers<const N: usize>(
*handler = SignalHandler::register(*signal, SignalHandlerBehavior::Stream)
.map(MaybeUninit::new)
.map_err(|err| {
let name = signal_name(*signal).unwrap_or("unknown signal");
let name = signal_name(*signal);
dev_error!("cannot setup handler for {name}: {err}");
err
})?;
Expand Down
12 changes: 2 additions & 10 deletions src/system/wait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,9 @@ impl std::fmt::Debug for WaitStatus {
if let Some(exit_status) = self.exit_status() {
write!(f, "ExitStatus({exit_status})")
} else if let Some(signal) = self.term_signal() {
write!(
f,
"TermSignal({})",
signal_name(signal).unwrap_or("unknown")
)
write!(f, "TermSignal({})", signal_name(signal))
} else if let Some(signal) = self.stop_signal() {
write!(
f,
"StopSignal({})",
signal_name(signal).unwrap_or("unknown")
)
write!(f, "StopSignal({})", signal_name(signal))
} else if self.did_continue() {
write!(f, "Continued")
} else {
Expand Down

0 comments on commit 43d4666

Please sign in to comment.