2018-04-28 12:32:56 +00:00
|
|
|
--
|
|
|
|
-- signal.lua from Lsyncd -- the Live (Mirror) Syncing Demon
|
|
|
|
--
|
|
|
|
--
|
|
|
|
-- Handles signal handlers.
|
|
|
|
--
|
|
|
|
--
|
|
|
|
-- License: GPLv2 (see COPYING) or any later version
|
|
|
|
-- Authors: Axel Kittenberger <axkibe@gmail.com>
|
|
|
|
--
|
|
|
|
if mantle
|
|
|
|
then
|
|
|
|
print( 'Error, Lsyncd mantle already loaded' )
|
|
|
|
os.exit( -1 )
|
|
|
|
end
|
|
|
|
|
|
|
|
-- "signames" is a a (mantle-)global table from signames.lua created by the
|
|
|
|
-- build script following code creates a (hash)table for the other direction.
|
|
|
|
local signums = { }
|
|
|
|
|
|
|
|
for num, name in pairs( signames )
|
|
|
|
do
|
|
|
|
signums[ name ] = num
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- a table of all registered signal handlers
|
2018-05-06 20:46:00 +00:00
|
|
|
--
|
|
|
|
-- keys are signal numbers
|
|
|
|
-- values are functions to be called
|
|
|
|
-- or 'false' in case of disabled default signals
|
2018-05-11 07:07:03 +00:00
|
|
|
local sigHandlers = { }
|
2018-04-28 12:32:56 +00:00
|
|
|
|
|
|
|
-- counter of signal handlers
|
|
|
|
-- used to tell the core to enlarge the signal queue if needed
|
|
|
|
-- (the queue must be as large as the number of different signals listened for)
|
|
|
|
local sigHandlerCount = 0
|
|
|
|
|
2018-05-06 20:46:00 +00:00
|
|
|
|
2018-04-28 12:32:56 +00:00
|
|
|
--
|
2018-05-12 13:08:14 +00:00
|
|
|
-- transforms a signal name or number to
|
|
|
|
-- a valid number. 'false' is left be 'false'
|
2018-04-28 12:32:56 +00:00
|
|
|
--
|
2018-05-12 13:08:14 +00:00
|
|
|
-- In case of a invalid signal specifie an error is raised.
|
2018-05-06 20:46:00 +00:00
|
|
|
--
|
2018-05-12 13:08:14 +00:00
|
|
|
local function toSignum
|
2018-04-28 12:32:56 +00:00
|
|
|
(
|
2018-05-12 13:08:14 +00:00
|
|
|
signal
|
2018-04-28 12:32:56 +00:00
|
|
|
)
|
|
|
|
if type( signal ) == 'number'
|
|
|
|
then
|
|
|
|
if signal < 0
|
|
|
|
or signal ~= signal
|
|
|
|
or signal - floor( signal ) ~= 0
|
|
|
|
then
|
2018-05-12 13:08:14 +00:00
|
|
|
error( 'signal ' .. signal .. ' is an invalid number.' , 3 )
|
2018-04-28 12:32:56 +00:00
|
|
|
end
|
2018-05-12 13:08:14 +00:00
|
|
|
|
|
|
|
return signal
|
2018-04-28 12:32:56 +00:00
|
|
|
elseif type( signal ) == 'string'
|
|
|
|
then
|
|
|
|
signum = signums[ signal ]
|
2018-05-12 13:08:14 +00:00
|
|
|
|
2018-04-28 12:32:56 +00:00
|
|
|
if signum == nil
|
|
|
|
then
|
2018-05-12 13:08:14 +00:00
|
|
|
error( 'signal "' .. signal .. '" unknown.' , 3 )
|
2018-04-28 12:32:56 +00:00
|
|
|
end
|
2018-05-12 13:08:14 +00:00
|
|
|
|
|
|
|
return signum
|
|
|
|
elseif signal == false
|
|
|
|
then
|
|
|
|
return false
|
2018-04-28 12:32:56 +00:00
|
|
|
else
|
2018-05-12 13:08:14 +00:00
|
|
|
error( 'signal of type ' .. type( signal ) .. ' invalid.', 3 )
|
2018-04-28 12:32:56 +00:00
|
|
|
end
|
2018-05-06 20:46:00 +00:00
|
|
|
end
|
|
|
|
|
2018-05-12 13:08:14 +00:00
|
|
|
|
2018-05-06 20:46:00 +00:00
|
|
|
--
|
|
|
|
-- The onsignal( ) function exported to userEnv.
|
|
|
|
--
|
|
|
|
function onsignal
|
|
|
|
(
|
2018-05-12 13:08:14 +00:00
|
|
|
...
|
|
|
|
--- signal1, -- signal number or name
|
|
|
|
--- handler1 -- function to call
|
|
|
|
-- -- or nil to unload the handle
|
|
|
|
-- -- or false to disable default signal handlers
|
|
|
|
-- signal2, handler2
|
|
|
|
-- signal3, handler3
|
|
|
|
-- and so on
|
2018-05-06 20:46:00 +00:00
|
|
|
)
|
2018-05-12 13:08:14 +00:00
|
|
|
local n = select( '#', ... )
|
|
|
|
local arg = {...}
|
|
|
|
|
|
|
|
if n % 2 ~= 0
|
|
|
|
then
|
2018-05-25 08:10:38 +00:00
|
|
|
error( 'onsignal() with uneven number of arguments called', 2 )
|
2018-05-12 13:08:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
for a = 1, n, 2
|
|
|
|
do
|
|
|
|
local signal = arg[ a ]
|
|
|
|
local handler = arg[ a + 1 ]
|
|
|
|
|
|
|
|
local signum = toSignum( signal )
|
|
|
|
|
|
|
|
sigHandlers[ signum ] = handler
|
|
|
|
end
|
2018-04-28 12:32:56 +00:00
|
|
|
|
2018-05-11 07:07:03 +00:00
|
|
|
core.onsignal( sigHandlers )
|
2018-04-28 12:32:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--
|
2018-05-12 13:08:14 +00:00
|
|
|
-- Returns signal handler registered for 'signum'
|
2018-04-28 12:32:56 +00:00
|
|
|
--
|
2018-05-12 13:08:14 +00:00
|
|
|
function getsignal
|
2018-04-28 12:32:56 +00:00
|
|
|
(
|
2018-05-12 13:08:14 +00:00
|
|
|
signum
|
2018-04-28 12:32:56 +00:00
|
|
|
)
|
2018-05-12 13:08:14 +00:00
|
|
|
return sigHandlers[ signum ];
|
2018-05-11 07:07:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Called by kernel on catched and queued signals
|
|
|
|
--
|
|
|
|
mci.signalEvent =
|
|
|
|
function
|
|
|
|
(
|
|
|
|
sigtable
|
|
|
|
)
|
|
|
|
for _, signum in ipairs( sigtable )
|
|
|
|
do
|
|
|
|
local handler = sigHandlers[ signum ]
|
|
|
|
|
|
|
|
if not handler
|
|
|
|
then
|
|
|
|
log( 'Error', 'Received signal '..signnum..' without a handler.' )
|
|
|
|
end
|
|
|
|
|
|
|
|
handler( )
|
|
|
|
end
|
2018-04-28 12:32:56 +00:00
|
|
|
end
|
2018-05-06 20:46:00 +00:00
|
|
|
|