v / vlib / os
Raw file | 16 loc (13 sloc) | 552 bytes | Latest commit hash f6844e976
1module os
2
3#include <signal.h>
4
5fn C.signal(signal int, handlercb SignalHandler) voidptr
6
7// signal will assign `handler` callback to be called when `signum` signal is received.
8pub fn signal_opt(signum Signal, handler SignalHandler) !SignalHandler {
9 C.errno = 0
10 prev_handler := C.signal(int(signum), handler)
11 if prev_handler == C.SIG_ERR {
12 // errno isn't correctly set on Windows, but EINVAL is this only possible value it can take anyway
13 return error_with_code(posix_get_error_msg(C.EINVAL), C.EINVAL)
14 }
15 return SignalHandler(prev_handler)
16}