|
| 1 | +use crate::obj::objint::PyIntRef; |
| 2 | +use crate::pyobject::{IdProtocol, PyObjectRef, PyResult}; |
| 3 | +use crate::vm::VirtualMachine; |
| 4 | + |
| 5 | +use std::sync::atomic::{AtomicBool, Ordering}; |
| 6 | + |
| 7 | +use num_traits::cast::ToPrimitive; |
| 8 | + |
| 9 | +use arr_macro::arr; |
| 10 | + |
| 11 | +#[cfg(unix)] |
| 12 | +use nix::unistd::alarm as sig_alarm; |
| 13 | + |
| 14 | +use libc; |
| 15 | + |
| 16 | +#[cfg(not(windows))] |
| 17 | +use libc::{SIG_DFL, SIG_ERR, SIG_IGN}; |
| 18 | + |
| 19 | +#[cfg(windows)] |
| 20 | +const SIG_DFL: libc::sighandler_t = 0; |
| 21 | +#[cfg(windows)] |
| 22 | +const SIG_IGN: libc::sighandler_t = 1; |
| 23 | +#[cfg(windows)] |
| 24 | +const SIG_ERR: libc::sighandler_t = !0; |
| 25 | + |
| 26 | +const NSIG: usize = 64; |
| 27 | + |
| 28 | +// We cannot use the NSIG const in the arr macro. This will fail compilation if NSIG is different. |
| 29 | +static mut TRIGGERS: [AtomicBool; NSIG] = arr![AtomicBool::new(false); 64]; |
| 30 | + |
| 31 | +extern "C" fn run_signal(signum: i32) { |
| 32 | + unsafe { |
| 33 | + TRIGGERS[signum as usize].store(true, Ordering::Relaxed); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +fn signal( |
| 38 | + signalnum: PyIntRef, |
| 39 | + handler: PyObjectRef, |
| 40 | + vm: &VirtualMachine, |
| 41 | +) -> PyResult<Option<PyObjectRef>> { |
| 42 | + if !vm.isinstance(&handler, &vm.ctx.function_type())? |
| 43 | + && !vm.isinstance(&handler, &vm.ctx.bound_method_type())? |
| 44 | + && !vm.isinstance(&handler, &vm.ctx.builtin_function_or_method_type())? |
| 45 | + { |
| 46 | + return Err(vm.new_type_error("Hanlder must be callable".to_string())); |
| 47 | + } |
| 48 | + let signal_module = vm.import("signal", &vm.ctx.new_tuple(vec![]), 0)?; |
| 49 | + let sig_dfl = vm.get_attribute(signal_module.clone(), "SIG_DFL")?; |
| 50 | + let sig_ign = vm.get_attribute(signal_module, "SIG_IGN")?; |
| 51 | + let signalnum = signalnum.as_bigint().to_i32().unwrap(); |
| 52 | + check_signals(vm); |
| 53 | + let sig_handler = if handler.is(&sig_dfl) { |
| 54 | + SIG_DFL |
| 55 | + } else if handler.is(&sig_ign) { |
| 56 | + SIG_IGN |
| 57 | + } else { |
| 58 | + run_signal as libc::sighandler_t |
| 59 | + }; |
| 60 | + let old = unsafe { libc::signal(signalnum, sig_handler) }; |
| 61 | + if old == SIG_ERR { |
| 62 | + return Err(vm.new_os_error("Failed to set signal".to_string())); |
| 63 | + } |
| 64 | + let old_handler = vm.signal_handlers.borrow_mut().insert(signalnum, handler); |
| 65 | + Ok(old_handler) |
| 66 | +} |
| 67 | + |
| 68 | +fn getsignal(signalnum: PyIntRef, vm: &VirtualMachine) -> PyResult<Option<PyObjectRef>> { |
| 69 | + let signalnum = signalnum.as_bigint().to_i32().unwrap(); |
| 70 | + Ok(vm.signal_handlers.borrow_mut().get(&signalnum).cloned()) |
| 71 | +} |
| 72 | + |
| 73 | +#[cfg(unix)] |
| 74 | +fn alarm(time: PyIntRef, _vm: &VirtualMachine) -> u32 { |
| 75 | + let time = time.as_bigint().to_u32().unwrap(); |
| 76 | + let prev_time = if time == 0 { |
| 77 | + sig_alarm::cancel() |
| 78 | + } else { |
| 79 | + sig_alarm::set(time) |
| 80 | + }; |
| 81 | + prev_time.unwrap_or(0) |
| 82 | +} |
| 83 | + |
| 84 | +#[allow(clippy::needless_range_loop)] |
| 85 | +pub fn check_signals(vm: &VirtualMachine) { |
| 86 | + for signum in 1..NSIG { |
| 87 | + let triggerd = unsafe { TRIGGERS[signum].swap(false, Ordering::Relaxed) }; |
| 88 | + if triggerd { |
| 89 | + let handler = vm |
| 90 | + .signal_handlers |
| 91 | + .borrow() |
| 92 | + .get(&(signum as i32)) |
| 93 | + .expect("Handler should be set") |
| 94 | + .clone(); |
| 95 | + vm.invoke(handler, vec![vm.new_int(signum), vm.get_none()]) |
| 96 | + .expect("Test"); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +fn stub_func(_vm: &VirtualMachine) -> PyResult { |
| 102 | + panic!("Do not use directly"); |
| 103 | +} |
| 104 | + |
| 105 | +pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { |
| 106 | + let ctx = &vm.ctx; |
| 107 | + |
| 108 | + let sig_dfl = ctx.new_rustfunc(stub_func); |
| 109 | + let sig_ign = ctx.new_rustfunc(stub_func); |
| 110 | + |
| 111 | + let module = py_module!(vm, "signal", { |
| 112 | + "signal" => ctx.new_rustfunc(signal), |
| 113 | + "getsignal" => ctx.new_rustfunc(getsignal), |
| 114 | + "SIG_DFL" => sig_dfl, |
| 115 | + "SIG_IGN" => sig_ign, |
| 116 | + "SIGABRT" => ctx.new_int(libc::SIGABRT as u8), |
| 117 | + "SIGFPE" => ctx.new_int(libc::SIGFPE as u8), |
| 118 | + "SIGILL" => ctx.new_int(libc::SIGILL as u8), |
| 119 | + "SIGINT" => ctx.new_int(libc::SIGINT as u8), |
| 120 | + "SIGSEGV" => ctx.new_int(libc::SIGSEGV as u8), |
| 121 | + "SIGTERM" => ctx.new_int(libc::SIGTERM as u8), |
| 122 | + }); |
| 123 | + extend_module_platform_specific(vm, module) |
| 124 | +} |
| 125 | + |
| 126 | +#[cfg(unix)] |
| 127 | +fn extend_module_platform_specific(vm: &VirtualMachine, module: PyObjectRef) -> PyObjectRef { |
| 128 | + let ctx = &vm.ctx; |
| 129 | + |
| 130 | + extend_module!(vm, module, { |
| 131 | + "alarm" => ctx.new_rustfunc(alarm), |
| 132 | + "SIGHUP" => ctx.new_int(libc::SIGHUP as u8), |
| 133 | + "SIGQUIT" => ctx.new_int(libc::SIGQUIT as u8), |
| 134 | + "SIGTRAP" => ctx.new_int(libc::SIGTRAP as u8), |
| 135 | + "SIGBUS" => ctx.new_int(libc::SIGBUS as u8), |
| 136 | + "SIGKILL" => ctx.new_int(libc::SIGKILL as u8), |
| 137 | + "SIGUSR1" => ctx.new_int(libc::SIGUSR1 as u8), |
| 138 | + "SIGUSR2" => ctx.new_int(libc::SIGUSR2 as u8), |
| 139 | + "SIGPIPE" => ctx.new_int(libc::SIGPIPE as u8), |
| 140 | + "SIGALRM" => ctx.new_int(libc::SIGALRM as u8), |
| 141 | + "SIGSTKFLT" => ctx.new_int(libc::SIGSTKFLT as u8), |
| 142 | + "SIGCHLD" => ctx.new_int(libc::SIGCHLD as u8), |
| 143 | + "SIGCONT" => ctx.new_int(libc::SIGCONT as u8), |
| 144 | + "SIGSTOP" => ctx.new_int(libc::SIGSTOP as u8), |
| 145 | + "SIGTSTP" => ctx.new_int(libc::SIGTSTP as u8), |
| 146 | + "SIGTTIN" => ctx.new_int(libc::SIGTTIN as u8), |
| 147 | + "SIGTTOU" => ctx.new_int(libc::SIGTTOU as u8), |
| 148 | + "SIGURG" => ctx.new_int(libc::SIGURG as u8), |
| 149 | + "SIGXCPU" => ctx.new_int(libc::SIGXCPU as u8), |
| 150 | + "SIGXFSZ" => ctx.new_int(libc::SIGXFSZ as u8), |
| 151 | + "SIGVTALRM" => ctx.new_int(libc::SIGVTALRM as u8), |
| 152 | + "SIGPROF" => ctx.new_int(libc::SIGPROF as u8), |
| 153 | + "SIGWINCH" => ctx.new_int(libc::SIGWINCH as u8), |
| 154 | + "SIGIO" => ctx.new_int(libc::SIGIO as u8), |
| 155 | + "SIGPWR" => ctx.new_int(libc::SIGPWR as u8), |
| 156 | + "SIGSYS" => ctx.new_int(libc::SIGSYS as u8), |
| 157 | + }); |
| 158 | + |
| 159 | + module |
| 160 | +} |
| 161 | + |
| 162 | +#[cfg(not(unix))] |
| 163 | +fn extend_module_platform_specific(_vm: &VirtualMachine, module: PyObjectRef) -> PyObjectRef { |
| 164 | + module |
| 165 | +} |
0 commit comments