underhill_crash/
options.rs#![warn(missing_docs)]
use std::ffi::OsString;
use std::time::Duration;
pub struct Options {
pub pid: u32,
pub tid: u32,
pub sig: u32,
pub comm: String,
pub verbose: bool,
pub no_redirect: bool,
pub no_kmsg: bool,
pub timeout: Duration,
}
impl Options {
pub(crate) fn parse() -> Self {
let mut args = std::env::args_os();
args.next();
let parse_number = |arg: Option<OsString>| {
arg.and_then(|x| x.to_string_lossy().parse().ok())
.expect(Self::USAGE)
};
let pid = parse_number(args.next());
let tid = parse_number(args.next());
let sig = parse_number(args.next());
let comm = args
.next()
.expect(Self::USAGE)
.to_string_lossy()
.into_owned();
if args.next().is_some() {
panic!("{}", Self::USAGE);
}
let timeout = Duration::from_secs(
std::env::var("UNDERHILL_CRASH_TIMEOUT")
.unwrap_or_else(|_| "15".into())
.parse()
.expect(Self::USAGE),
);
let no_redirect_var = std::env::var("UNDERHILL_CRASH_NO_REDIRECT").unwrap_or_default();
let no_redirect = no_redirect_var == "1" || no_redirect_var.eq_ignore_ascii_case("true");
let no_kmsg_var = std::env::var("UNDERHILL_CRASH_NO_KMSG").unwrap_or_default();
let no_kmsg = no_kmsg_var == "1" || no_kmsg_var.eq_ignore_ascii_case("true");
let verbose_var = std::env::var("UNDERHILL_CRASH_VERBOSE").unwrap_or_default();
let verbose = verbose_var == "1" || verbose_var.eq_ignore_ascii_case("true");
Self {
pid,
tid,
sig,
comm,
verbose,
no_redirect,
no_kmsg,
timeout,
}
}
const USAGE: &'static str = "Usage: {pid} {tid} {signal} {command line}
Environment Variables:
\tUNDERHILL_CRASH_TIMEOUT - Timeout duration in seconds, default 15
\tUNDERHILL_CRASH_VERBOSE - Be verbose, default false";
}