diff --git a/CMakeLists.txt b/CMakeLists.txt index 4a51523..74a54af 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -65,6 +65,7 @@ set( MANTLE_CODE ${PROJECT_SOURCE_DIR}/mantle/syncmaster.lua ${PROJECT_SOURCE_DIR}/mantle/monitor.lua ${PROJECT_SOURCE_DIR}/mantle/fwriter.lua + ${PROJECT_SOURCE_DIR}/mantle/signal.lua ${PROJECT_SOURCE_DIR}/mantle/statusfile.lua ${PROJECT_SOURCE_DIR}/mantle/useralarm.lua ${PROJECT_SOURCE_DIR}/mantle/mci.lua diff --git a/mantle/mci.lua b/mantle/mci.lua index 1c5fd2d..d241615 100644 --- a/mantle/mci.lua +++ b/mantle/mci.lua @@ -386,14 +386,6 @@ function mci.initialize firstTime -- true when Lsyncd startups the first time, -- -- false on resets, due to HUP signal or monitor queue overflow. ) - print( 'HELLO' ) - do - for num, name in pairs( signames ) - do - print( 'SIG', num, name ) - end - end - -- Checks if user overwrote the settings function. -- ( was Lsyncd <2.1 style ) if userENV.settings ~= settings @@ -407,6 +399,8 @@ function mci.initialize os.exit( -1 ) end + initSignalHandlers( firstTime ) + lastReportedWaiting = false -- diff --git a/mantle/signal.lua b/mantle/signal.lua new file mode 100644 index 0000000..2b00f5b --- /dev/null +++ b/mantle/signal.lua @@ -0,0 +1,105 @@ +-- +-- signal.lua from Lsyncd -- the Live (Mirror) Syncing Demon +-- +-- +-- Handles signal handlers. +-- +-- +-- License: GPLv2 (see COPYING) or any later version +-- Authors: Axel Kittenberger +-- +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 +local sigHandlers = { } + +-- 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 + +-- +-- onsignal function exported to userEnv +-- +function onsignal +( + signal, -- signal number or name + handler -- function to call + -- -- or nil to unload the handle + -- -- or false to disable default signal handlers +) + local signum + + if type( signal ) == 'number' + then + if signal < 0 + or signal ~= signal + or signal - floor( signal ) ~= 0 + then + error( 'signal ' .. signal .. ' is an invalid number.' , 2 ) + end + + signum = signal + elseif type( signal ) == 'string' + then + signum = signums[ signal ] + + if signum == nil + then + error( 'signal "' .. signal .. '" unknown.' , 2 ) + end + else + error( 'signal of type ' .. type( signal ) .. ' invalid.', 2 ) + end + + -- FIXME store the handler in a table + -- FIXME tell core of max queue size + + core.onsignal( signum, handler ); +end + + +-- +-- Sets up the default HUP/INT/TERM signal handlers. +-- +-- Called after user scripts finished +-- +function initSignalHandlers +( + firstTime --- TODO check if needed +) + onsignal( + 'HUP', + function( ) + console.log( 'GOT A HUP SIGNAL' ); + end + ) + + onsignal( + 'INT', + function( ) + console.log( 'GOT A INT SIGNAL' ); + end + ) + + onsignal( + 'TERM', + function( ) + console.log( 'GOT A TERM SIGNAL' ); + end + ) +end diff --git a/signames.sh b/signames.sh index 1b62161..74c59d0 100755 --- a/signames.sh +++ b/signames.sh @@ -11,6 +11,9 @@ # KILL=/bin/kill +# Don't know a better way, checks only until this signal number +# To quote, this ought to be enough for anybody. +nmax=256 if [ "$#" -ne 1 ]; then @@ -28,13 +31,14 @@ echo "signames =" >> $1 echo "{" >> $1 n=1 -while name=`kill -l $n 2>/dev/null`; +while name=`$KILL --list=$n 2>/dev/null`; do if ! [ -z $name ] then echo $echoe "\t[ $n ] = '$name'," >> $1 fi n=$(( n + 1 )) + if [ $n -gt $nmax ]; then break; fi done echo "}" >> $1