lsyncd/default/signal.lua
2018-06-04 08:54:06 +02:00

96 lines
1.9 KiB
Lua

--
-- signal.lua from Lsyncd -- the Live (Mirror) Syncing Demon
--
--
-- The default signal handles for HUP, INT and TERM.
--
--
-- License: GPLv2 (see COPYING) or any later version
-- Authors: Axel Kittenberger <axkibe@gmail.com>
--
if not default then error( 'default not loaded' ) end
default.signal = { }
--
-- Returns a signal handler for 'signal'.
--
local function makeSignalHandler
(
sig, -- the signal to handle
logtext, -- text to log
finish -- function to call after all child processes have been collected
)
return function( )
log( 'Normal', 'Received an ',sig,' signal, ',logtext )
local pCount = 0
for _, sync in ipairs( syncs )
do
sync.stop( )
local pids = sync.pids( )
local pc = #pids
if( pc == 0 )
then
syncs.remove( sync )
else
pCount = pCount + pc
sync.onCollect(
function
(
sync -- the user intf to the sync a child finished for
)
if( sync.processCount( ) == 0 ) then syncs.remove( sync ) end
if #syncs == 0 then finish( ) end
end
)
for _, pid in ipairs( pids ) do signal( pid, sig ) end
end
end
if #syncs == 0 then finish( ) end
log( 'Normal', 'Waiting for ', pCount, ' child processes.' )
end
end
local function finishHup( )
os.exit( 0 )
end
local function finishInt( )
os.exit( 0 )
end
local function finishTerm( )
os.exit( 0 )
end
--
-- Sets up the default HUP/INT/TERM signal handlers.
--
-- Called after user scripts finished
--
init =
function
( )
local hup = getsignal( 'HUP' )
local int = getsignal( 'INT' )
local term = getsignal( 'TERM' )
if hup ~= false then hup = makeSignalHandler( 'HUP', 'resetting', finishHup ) end
if int ~= false then int = makeSignalHandler( 'INT', 'terminating', finishInt ) end
if term ~= false then term = makeSignalHandler( 'TERM', 'terminating', finishTerm ) end
onsignal( 'HUP', hup, 'INT', int, 'TERM', term )
end