signals :: SignalHandler :: defaultinit
# Receives the callback from system when a given signal arise
interface SignalHandler
# Invoked after a call to `check_signals` if a signal has been raised
# (should be redefed by subclasses)
#
# Should be used by most signals except `sigkill` and `sigstop` since they
# cannot be caught, blocked or ignored.
#
# class MyReceiver
# super SignalHandler
#
# redef fun receive_signal(signal) do print "received safely {signal}"
# end
#
# var r = new MyReceiver
# r.handle_signal(sigint, true) # will call back when "check_signals" is called
# # ...
# check_signals # if a signal was received, it will invoke `r.receive_signal`
fun receive_signal(signal: Int) do end
# Called immediatly on receiving an unsafe signal (should be redefed by subclasses)
#
# Should be used for `sigkill` and `sigstop` since they cannot be caught,
# blocked or ignored.
#
# You should consider this methods as being fragile. It should be implemented in C
# and you should not do too much callbacks to Nit.
#
# class MyReceiver
# super SignalHandler
#
# redef fun receive_signal_unsafe(signal) do print "received unsafely {signal}"
# end
#
# var r = new MyReceiver
# r.handle_signal(sigsegv, false) # `r.receive_signal_unsafe` will be invoked on sigsegv
fun receive_signal_unsafe(signal: Int) do end
# Set the receiver as the handler of the signal
#
# If `safely`, receiver will be called when `check_signals` in invoked
# otherwise the receiver is invoked when the signal is raised, it may
# crash the Nit system but is unavoidable for unstoppable signals.
fun handle_signal(signal: Int, safely: Bool) import receive_signal `{
if (signal < 32 && signal >=0) {
struct sigaction act;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
act.sa_handler = receiver;
sigaction(signal, &act, NULL);
#ifdef SignalHandler_decr_ref
SignalHandler last_handler = (SignalHandler)nit_signals_list[signal].handler;
if (last_handler != NULL)
SignalHandler_decr_ref(last_handler);
#endif
nit_signals_list[signal].handler = self;
#ifdef SignalHandler_incr_ref
SignalHandler_incr_ref(self);
#endif
nit_signals_list[signal].safely = safely;
nit_SignalHandler_receive_signal =
(void (*)(void*, long))&SignalHandler_receive_signal;
}
`}
# Set to ignore the signal
fun ignore_signal(signal: Int) `{
if (signal < 32 && signal >=0) {
struct sigaction act;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
act.sa_handler = SIG_IGN;
sigaction(signal, &act, NULL);
#ifdef SignalHandler_decr_ref
SignalHandler last_handler = (SignalHandler)nit_signals_list[signal].handler;
if (last_handler != NULL)
SignalHandler_decr_ref(last_handler);
#endif
}
`}
# Set default action for the signal
fun default_signal(signal: Int) `{
if (signal < 32 && signal >=0) {
struct sigaction act;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
act.sa_handler = SIG_DFL;
sigaction(signal, &act, NULL);
#ifdef SignalHandler_decr_ref
SignalHandler last_handler = (SignalHandler)nit_signals_list[signal].handler;
if (last_handler != NULL)
SignalHandler_decr_ref(last_handler);
#endif
}
`}
end
lib/signals/signals.nit:101,1--205,3