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
|
2018-06-06 07:24:13 +00:00
|
|
|
forward, -- the signal to forward to children
|
|
|
|
-- -- if nil it doesn't
|
2018-06-04 06:54:06 +00:00
|
|
|
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
|
2018-06-06 07:24:13 +00:00
|
|
|
|
2018-06-04 06:54:06 +00:00
|
|
|
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
|
|
|
|
)
|
|
|
|
|
2018-06-06 07:24:13 +00:00
|
|
|
if forward
|
|
|
|
then
|
|
|
|
for _, pid in ipairs( pids ) do signal( pid, forward ) end
|
|
|
|
end
|
2018-06-04 06:54:06 +00:00
|
|
|
end
|
2018-05-25 08:10:38 +00:00
|
|
|
end
|
2018-05-25 10:23:38 +00:00
|
|
|
|
2018-06-06 07:24:13 +00:00
|
|
|
if #syncs == 0
|
|
|
|
then
|
|
|
|
finish( )
|
|
|
|
else
|
|
|
|
log( 'Normal', 'Waiting for ', pCount, ' child processes.' )
|
|
|
|
end
|
2018-06-04 06:54:06 +00:00
|
|
|
end
|
2018-05-12 13:08:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2018-06-04 06:54:06 +00:00
|
|
|
local function finishHup( )
|
2018-06-06 07:24:13 +00:00
|
|
|
softreset( )
|
2018-06-04 06:54:06 +00:00
|
|
|
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-06 07:24:13 +00:00
|
|
|
if hup ~= false
|
|
|
|
then
|
|
|
|
hup = makeSignalHandler( 'HUP', nil, 'resetting', finishHup )
|
|
|
|
end
|
|
|
|
|
|
|
|
if int ~= false
|
|
|
|
then
|
|
|
|
int = makeSignalHandler( 'INT', 'INT', 'terminating', finishInt )
|
|
|
|
end
|
|
|
|
|
|
|
|
if term ~= false
|
|
|
|
then
|
|
|
|
term = makeSignalHandler( 'TERM', '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
|
|
|
|