use crate::host::{PyHost, PyObj};
use fusevm::Value;
use std::cell::RefCell;
use std::collections::HashMap;
thread_local! {
static HANDLERS: RefCell<HashMap<i64, Value>> = RefCell::new(HashMap::new());
}
const SIG_DFL: i64 = 0;
const SIG_IGN: i64 = 1;
pub fn call(h: &mut PyHost, name: &str, args: &[Value]) -> Option<Result<Value, String>> {
Some(match name {
"signal" => {
let num = args.first().and_then(|v| h.as_int(v)).unwrap_or(0);
let new = args.get(1).cloned().unwrap_or(Value::Int(SIG_DFL));
let old = HANDLERS.with(|m| m.borrow_mut().insert(num, new));
Ok(old.unwrap_or(Value::Int(SIG_DFL)))
}
"getsignal" => {
let num = args.first().and_then(|v| h.as_int(v)).unwrap_or(0);
if let Some(v) = HANDLERS.with(|m| m.borrow().get(&num).cloned()) {
return Some(Ok(v));
}
Ok(if num == libc::SIGINT as i64 {
h.alloc(PyObj::Builtin("_signal.default_int_handler".into()))
} else {
Value::Int(SIG_DFL)
})
}
"default_int_handler" => Err("KeyboardInterrupt".to_string()),
"alarm" | "setitimer" | "getitimer" => Ok(Value::Int(0)),
"pause" => Ok(Value::Undef),
"raise_signal" => Ok(Value::Undef),
"strsignal" => {
let num = args.first().and_then(|v| h.as_int(v)).unwrap_or(0);
Ok(h.new_str(format!("Signal {num}")))
}
"valid_signals" => {
let mut items: indexmap::IndexMap<crate::host::PKey, Value> = indexmap::IndexMap::new();
for (_, n) in SIGNALS {
items.insert(crate::host::PKey::Int(*n as i64), Value::Int(*n as i64));
}
Ok(h.new_set(items))
}
"pthread_sigmask" => Ok(h.new_set(indexmap::IndexMap::new())),
"set_wakeup_fd" => Ok(Value::Int(-1)),
"siginterrupt" => Ok(Value::Undef),
_ => return None,
})
}
const SIGNALS: &[(&str, i32)] = &[
("SIGHUP", libc::SIGHUP),
("SIGINT", libc::SIGINT),
("SIGQUIT", libc::SIGQUIT),
("SIGILL", libc::SIGILL),
("SIGTRAP", libc::SIGTRAP),
("SIGABRT", libc::SIGABRT),
("SIGFPE", libc::SIGFPE),
("SIGKILL", libc::SIGKILL),
("SIGBUS", libc::SIGBUS),
("SIGSEGV", libc::SIGSEGV),
("SIGSYS", libc::SIGSYS),
("SIGPIPE", libc::SIGPIPE),
("SIGALRM", libc::SIGALRM),
("SIGTERM", libc::SIGTERM),
("SIGURG", libc::SIGURG),
("SIGSTOP", libc::SIGSTOP),
("SIGTSTP", libc::SIGTSTP),
("SIGCONT", libc::SIGCONT),
("SIGCHLD", libc::SIGCHLD),
("SIGTTIN", libc::SIGTTIN),
("SIGTTOU", libc::SIGTTOU),
("SIGIO", libc::SIGIO),
("SIGXCPU", libc::SIGXCPU),
("SIGXFSZ", libc::SIGXFSZ),
("SIGVTALRM", libc::SIGVTALRM),
("SIGPROF", libc::SIGPROF),
("SIGWINCH", libc::SIGWINCH),
("SIGUSR1", libc::SIGUSR1),
("SIGUSR2", libc::SIGUSR2),
];
pub fn entries(h: &mut PyHost) -> Vec<(String, Value)> {
const FNS: &[&str] = &[
"signal",
"getsignal",
"default_int_handler",
"alarm",
"pause",
"raise_signal",
"strsignal",
"valid_signals",
"setitimer",
"getitimer",
"pthread_sigmask",
"set_wakeup_fd",
"siginterrupt",
];
let mut out: Vec<(String, Value)> = FNS
.iter()
.map(|f| {
(
(*f).to_string(),
h.alloc(PyObj::Builtin(format!("_signal.{f}"))),
)
})
.collect();
for (name, num) in SIGNALS {
out.push(((*name).to_string(), Value::Int(*num as i64)));
}
for (name, v) in [
("SIG_DFL", SIG_DFL),
("SIG_IGN", SIG_IGN),
("SIG_BLOCK", libc::SIG_BLOCK as i64),
("SIG_UNBLOCK", libc::SIG_UNBLOCK as i64),
("SIG_SETMASK", libc::SIG_SETMASK as i64),
("ITIMER_REAL", 0),
("ITIMER_VIRTUAL", 1),
("ITIMER_PROF", 2),
("NSIG", 32),
] {
out.push((name.to_string(), Value::Int(v)));
}
let err = h.alloc(PyObj::Builtin("OSError".into()));
out.push(("ItimerError".to_string(), err));
out
}