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
|
|
|
--
|
|
|
|
--
|
|
|
|
--
|
|
|
|
local function onCollect
|
|
|
|
(
|
|
|
|
sync -- the user intf to the sync a child finished for
|
|
|
|
)
|
2018-05-27 08:17:47 +00:00
|
|
|
if( sync.processCount( ) == 0 ) then syncs.remove( sync ) end
|
2018-05-25 08:10:38 +00:00
|
|
|
|
2018-05-25 10:23:38 +00:00
|
|
|
if #syncs == 0 then os.exit( 0 ) end
|
2018-05-25 08:10:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2018-05-12 13:08:14 +00:00
|
|
|
local function sighup
|
|
|
|
( )
|
|
|
|
print( 'GOT A HUP SIGNAL' )
|
|
|
|
|
|
|
|
os.exit( 1 )
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function sigint
|
|
|
|
( )
|
2018-05-25 10:23:38 +00:00
|
|
|
log( 'Normal', 'Received an INT signal, terminating' )
|
2018-05-12 13:08:14 +00:00
|
|
|
|
2018-05-27 08:17:47 +00:00
|
|
|
local pCount = 0
|
|
|
|
|
2018-05-25 08:10:38 +00:00
|
|
|
for _, sync in ipairs( syncs )
|
2018-05-13 12:13:54 +00:00
|
|
|
do
|
2018-05-25 08:10:38 +00:00
|
|
|
sync.stop( )
|
|
|
|
|
2018-05-27 08:17:47 +00:00
|
|
|
local c = sync.processCount( )
|
|
|
|
|
|
|
|
if( c == 0 )
|
2018-05-25 08:10:38 +00:00
|
|
|
then
|
|
|
|
syncs.remove( sync )
|
|
|
|
else
|
2018-05-27 08:17:47 +00:00
|
|
|
pCount = pCount + c
|
2018-05-25 08:10:38 +00:00
|
|
|
sync.onCollect( onCollect )
|
|
|
|
end
|
2018-05-13 12:13:54 +00:00
|
|
|
end
|
2018-05-25 10:23:38 +00:00
|
|
|
|
2018-05-27 08:17:47 +00:00
|
|
|
if #syncs == 0 then os.exit( 0 ) end
|
|
|
|
|
|
|
|
log( 'Normal', 'Waiting for ', pCount, ' child processes.' )
|
2018-05-12 13:08:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function sigterm
|
|
|
|
( )
|
|
|
|
print( 'GOT A TERM SIGNAL' )
|
|
|
|
|
|
|
|
os.exit( 1 )
|
|
|
|
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 = sighup end
|
|
|
|
if int ~= false then int = sigint end
|
|
|
|
if term ~= false then term = sigterm end
|
|
|
|
|
|
|
|
onsignal(
|
|
|
|
'HUP', hup,
|
|
|
|
'INT', int,
|
2018-05-25 08:10:38 +00:00
|
|
|
'TERM', term
|
2018-05-12 13:08:14 +00:00
|
|
|
)
|
|
|
|
end
|
2018-05-27 08:17:47 +00:00
|
|
|
|