2021-05-05 15:39:02 +03:00
|
|
|
module os
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
fn C.signal(signal int, handlercb SignalHandler) voidptr
|
|
|
|
|
|
|
|
// signal will assign `handler` callback to be called when `signum` signal is received.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn signal_opt(signum Signal, handler SignalHandler) !SignalHandler {
|
2021-05-05 15:39:02 +03:00
|
|
|
C.errno = 0
|
|
|
|
prev_handler := C.signal(int(signum), handler)
|
|
|
|
if prev_handler == C.SIG_ERR {
|
|
|
|
// errno isn't correctly set on Windows, but EINVAL is this only possible value it can take anyway
|
|
|
|
return error_with_code(posix_get_error_msg(C.EINVAL), C.EINVAL)
|
|
|
|
}
|
|
|
|
return SignalHandler(prev_handler)
|
|
|
|
}
|