Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std: <ExitStatus as Display>::fmt name the signal it died from #95833

Merged
merged 3 commits into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions library/std/src/sys/unix/process/process_unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,17 +695,80 @@ impl From<c_int> for ExitStatus {
}
}

/// Convert a signal number to a readable, searchable name.
fn signal_string(signal: i32) -> String {
(match signal {
libc::SIGHUP => "SIGHUP",
notriddle marked this conversation as resolved.
Show resolved Hide resolved
libc::SIGINT => "SIGINT",
libc::SIGQUIT => "SIGQUIT",
libc::SIGILL => "SIGILL",
libc::SIGTRAP => "SIGTRAP",
libc::SIGABRT => "SIGABRT",
libc::SIGBUS => "SIGBUS",
libc::SIGFPE => "SIGFPE",
libc::SIGKILL => "SIGKILL",
libc::SIGUSR1 => "SIGUSR1",
libc::SIGSEGV => "SIGSEGV",
libc::SIGUSR2 => "SIGUSR2",
libc::SIGPIPE => "SIGPIPE",
libc::SIGALRM => "SIGALRM",
libc::SIGTERM => "SIGTERM",
libc::SIGCHLD => "SIGCHLD",
libc::SIGCONT => "SIGCONT",
libc::SIGSTOP => "SIGSTOP",
libc::SIGTSTP => "SIGTSTP",
libc::SIGTTIN => "SIGTTIN",
libc::SIGTTOU => "SIGTTOU",
libc::SIGURG => "SIGURG",
libc::SIGXCPU => "SIGXCPU",
libc::SIGXFSZ => "SIGXFSZ",
libc::SIGVTALRM => "SIGVTALRM",
libc::SIGPROF => "SIGPROF",
libc::SIGWINCH => "SIGWINCH",
libc::SIGIO => "SIGIO",
libc::SIGSYS => "SIGSYS",
#[cfg(target_os = "linux")]
libc::SIGSTKFLT => "SIGSTKFLT",
#[cfg(target_os = "linux")]
libc::SIGPWR => "SIGPWR",
notriddle marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "dragonfly"
))]
libc::SIGEMT => "SIGEMT",
#[cfg(any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "dragonfly"
))]
libc::SIGINFO => "SIGINFO",
_ => return format!("{signal}"),
})
.to_string()
}

impl fmt::Display for ExitStatus {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(code) = self.code() {
write!(f, "exit status: {code}")
} else if let Some(signal) = self.signal() {
let signal = signal_string(signal);
if self.core_dumped() {
write!(f, "signal: {signal} (core dumped)")
} else {
write!(f, "signal: {signal}")
}
} else if let Some(signal) = self.stopped_signal() {
let signal = signal_string(signal);
write!(f, "stopped (not terminated) by signal: {signal}")
} else if self.continued() {
write!(f, "continued (WIFCONTINUED)")
Expand Down
6 changes: 3 additions & 3 deletions library/std/src/sys/unix/process/process_unix/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ fn exitstatus_display_tests() {

let t = |v, s| assert_eq!(s, format!("{}", <ExitStatus as ExitStatusExt>::from_raw(v)));

t(0x0000f, "signal: 15");
t(0x0008b, "signal: 11 (core dumped)");
t(0x0000f, "signal: SIGTERM");
t(0x0008b, "signal: SIGSEGV (core dumped)");
t(0x00000, "exit status: 0");
t(0x0ff00, "exit status: 255");

Expand All @@ -24,7 +24,7 @@ fn exitstatus_display_tests() {
// The purpose of this test is to test our string formatting, not our understanding of the wait
// status magic numbers. So restrict these to Linux.
if cfg!(target_os = "linux") {
t(0x0137f, "stopped (not terminated) by signal: 19");
t(0x0137f, "stopped (not terminated) by signal: SIGSTOP");
t(0x0ffff, "continued (WIFCONTINUED)");
}

Expand Down