-
Notifications
You must be signed in to change notification settings - Fork 4
/
signal.nelua
59 lines (52 loc) · 2.49 KB
/
signal.nelua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
--[[
Module with utilities to catch and raise OS application signals.
]]
local signal: type = @record{}
local signal.SIGABRT: cint <const,cimport,cinclude'<signal.h>'>
local signal.SIGFPE: cint <const,cimport,cinclude'<signal.h>'>
local signal.SIGILL: cint <const,cimport,cinclude'<signal.h>'>
local signal.SIGINT: cint <const,cimport,cinclude'<signal.h>'>
local signal.SIGSEGV: cint <const,cimport,cinclude'<signal.h>'>
local signal.SIGTERM: cint <const,cimport,cinclude'<signal.h>'>
local signal.SIG_DFL: function(cint): void <const,cimport,cinclude'<signal.h>'>
local signal.SIG_IGN: function(cint): void <const,cimport,cinclude'<signal.h>'>
local signal.SIG_ERR: function(cint): void <const,cimport,cinclude'<signal.h>'>
-- Returns last errno message plus its code.
local function geterrno(): (string, integer)
local errno: cint <cimport,cinclude'<errno.h>'>
local function strerror(errnum: cint): cstring <cimport,cinclude'<string.h>'> end
return strerror(errno), errno
end
--[[
Setup application to call `handler` when it receives signal `sig`.
Returns true when success, otherwise false plus error message and code.
]]
function signal.setsignal(sig: cint, handler: function(cint): void): (boolean, string, integer)
## cinclude '@unistd.h'
## cemit '#if defined(_POSIX_VERSION)'
local sigset_t: type <cimport,cinclude'<signal.h>',cincomplete> = @record{}
local sigaction_t: type <cimport,cinclude'<signal.h>',ctypedef'sigaction',cincomplete> = @record{
sa_handler: function(cint),
sa_mask: sigset_t,
}
local function sigemptyset(set: *sigset_t): cint <cimport,cinclude'<signal.h>'> end
local function sigaction(sig: cint, act: *sigaction_t, oact: *sigaction_t): cint <cimport, nodecl> end
local sa: sigaction_t = {sa_handler = handler}
sigemptyset(&sa.sa_mask) -- do not mask any signal
local res: cint = sigaction(sig, &sa, nilptr)
if res ~= 0 then return false, geterrno() end
## cemit '#else'
local function c_signal(sig: cint, handler: function(cint): void): function(cint): void <cimport'signal',cinclude'<signal.h>'> end
local res: function(cint): void = c_signal(sig, handler)
if res == signal.SIG_ERR then return false, geterrno() end
## cemit '#endif'
return true, (@string){}, 0
end
-- Raises signal `sig`.
function signal.raise(sig: cint): (boolean, string, integer)
local function c_raise(sig: cint): cint <cimport'raise',cinclude'<signal.h>'> end
local res: cint = c_raise(sig)
if res ~= 0 then return false, geterrno() end
return true, (@string){}, 0
end
return signal