Make Signals compatible with nix, implement TryFrom<&str> (#1599)

* Make our signals compatible to nix Signals

* no-default nix
This commit is contained in:
Dominik Maier 2023-10-03 17:33:45 +09:00 committed by GitHub
parent 20f1119bab
commit 25409119ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 1 deletions

View File

@ -107,7 +107,7 @@ serde_json = { version = "1.0", optional = true, default-features = false, featu
miniz_oxide = { version = "0.7.1", optional = true}
hostname = { version = "^0.3", optional = true } # Is there really no gethostname in the stdlib?
rand_core = { version = "0.6", optional = true }
nix = { version = "0.26", optional = true , features = ["socket", "poll"] }
nix = { version = "0.26", default-features = false, optional = true, features = ["signal", "socket", "poll"] }
uuid = { version = "1.4", optional = true, features = ["serde", "v4"] }
clap = {version = "4.0", features = ["derive", "wrap_help"], optional = true} # CLI parsing, for libafl_bolts::cli / the `cli` feature
log = "0.4.20"

View File

@ -299,6 +299,38 @@ pub enum Signal {
SigTrap = SIGTRAP,
}
impl TryFrom<&str> for Signal {
type Error = Error;
fn try_from(value: &str) -> Result<Self, Self::Error> {
Ok(match value {
"SIGABRT" => Signal::SigAbort,
"SIGBUS" => Signal::SigBus,
"SIGFPE" => Signal::SigFloatingPointException,
"SIGILL" => Signal::SigIllegalInstruction,
"SIGPIPE" => Signal::SigPipe,
"SIGSEGV" => Signal::SigSegmentationFault,
"SIGUSR2" => Signal::SigUser2,
"SIGALRM" => Signal::SigAlarm,
"SIGHUP" => Signal::SigHangUp,
"SIGKILL" => Signal::SigKill,
"SIGQUIT" => Signal::SigQuit,
"SIGTERM" => Signal::SigTerm,
"SIGINT" => Signal::SigInterrupt,
"SIGTRAP" => Signal::SigTrap,
_ => return Err(Error::illegal_argument(format!("No signal named {value}"))),
})
}
}
#[cfg(feature = "std")]
impl From<Signal> for nix::sys::signal::Signal {
fn from(value: Signal) -> Self {
// we can be semi-certain that all signals exist in nix.
i32::from(value).try_into().unwrap()
}
}
/// A list of crashing signals
pub static CRASH_SIGNALS: &[Signal] = &[
Signal::SigAbort,