lsyncd/default/signal.lua

96 lines
1.9 KiB
Lua
Raw Normal View History

2018-05-12 13:08:14 +00:00
--
-- 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 = { }
2018-05-25 08:10:38 +00:00
--
2018-06-04 06:54:06 +00:00
-- Returns a signal handler for 'signal'.
2018-05-25 08:10:38 +00:00
--
2018-06-04 06:54:06 +00:00
local function makeSignalHandler
2018-05-25 08:10:38 +00:00
(
2018-06-04 06:54:06 +00:00
sig, -- the signal to handle
logtext, -- text to log
finish -- function to call after all child processes have been collected
2018-05-25 08:10:38 +00:00
)
2018-06-04 06:54:06 +00:00
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
2018-05-25 08:10:38 +00:00
end
2018-05-25 10:23:38 +00:00
2018-06-04 06:54:06 +00:00
if #syncs == 0 then finish( ) end
2018-05-27 08:17:47 +00:00
2018-06-04 06:54:06 +00:00
log( 'Normal', 'Waiting for ', pCount, ' child processes.' )
end
2018-05-12 13:08:14 +00:00
end
2018-06-04 06:54:06 +00:00
local function finishHup( )
os.exit( 0 )
end
2018-05-12 13:08:14 +00:00
2018-06-04 06:54:06 +00:00
local function finishInt( )
os.exit( 0 )
2018-05-12 13:08:14 +00:00
end
2018-06-04 06:54:06 +00:00
local function finishTerm( )
os.exit( 0 )
end
2018-05-12 13:08:14 +00:00
--
-- 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' )
2018-06-04 06:54:06 +00:00
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
2018-05-12 13:08:14 +00:00
2018-06-04 06:54:06 +00:00
onsignal( 'HUP', hup, 'INT', int, 'TERM', term )
2018-05-12 13:08:14 +00:00
end
2018-05-27 08:17:47 +00:00