reworking signal system

This commit is contained in:
Axel Kittenberger 2018-05-25 10:10:38 +02:00
parent ec4940a066
commit 6bc922689f
8 changed files with 113 additions and 22 deletions

View File

@ -13,6 +13,7 @@
#include "config.h" #include "config.h"
// some older machines need this to see pselect // some older machines need this to see pselect
#define _BSD_SOURCE 1
#define _DEFAULT_SOURCE 1 #define _DEFAULT_SOURCE 1
#define _XOPEN_SOURCE 700 #define _XOPEN_SOURCE 700
#define _DARWIN_C_SOURCE 1 #define _DARWIN_C_SOURCE 1

View File

@ -8,6 +8,7 @@
*/ */
#include "feature.h" #include "feature.h"
#include <sys/select.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <signal.h> #include <signal.h>

View File

@ -271,5 +271,12 @@ signal_notify(
if( lua_pcall( L, 1, 0, -3 ) ) exit( -1 ); if( lua_pcall( L, 1, 0, -3 ) ) exit( -1 );
lua_pop( L, 1 ); lua_pop( L, 1 );
if( lua_gettop( L ) )
{
logstring( "Error", "internal, stack is dirty." );
l_stackdump( L );
exit( -1 );
}
} }

View File

@ -14,6 +14,17 @@ if not default then error( 'default not loaded' ) end
default.signal = { } default.signal = { }
--
--
--
local function onCollect
(
sync -- the user intf to the sync a child finished for
)
end
local function sighup local function sighup
( ) ( )
print( 'GOT A HUP SIGNAL' ) print( 'GOT A HUP SIGNAL' )
@ -26,12 +37,18 @@ local function sigint
( ) ( )
print( 'GOT AN INT SIGNAL' ) print( 'GOT AN INT SIGNAL' )
for _, s in ipairs( syncs ) for _, sync in ipairs( syncs )
do do
print( 'a sync' ) sync.stop( )
end
os.exit( 1 ) if( #sync.pids == 0 )
then
syncs.remove( sync )
else
sync.onCollect( onCollect )
end
end
-- os.exit( 1 )
end end
@ -55,6 +72,8 @@ init =
local int = getsignal( 'INT' ) local int = getsignal( 'INT' )
local term = getsignal( 'TERM' ) local term = getsignal( 'TERM' )
print( 'INIT', hup, int, term )
if hup ~= false then hup = sighup end if hup ~= false then hup = sighup end
if int ~= false then int = sigint end if int ~= false then int = sigint end
if term ~= false then term = sigterm end if term ~= false then term = sigterm end
@ -62,6 +81,6 @@ init =
onsignal( onsignal(
'HUP', hup, 'HUP', hup,
'INT', int, 'INT', int,
'TERM', iterm 'TERM', term
) )
end end

View File

@ -95,7 +95,7 @@ function onsignal
if n % 2 ~= 0 if n % 2 ~= 0
then then
error( 'onsignal with uneven number of arguments called', 2 ) error( 'onsignal() with uneven number of arguments called', 2 )
end end
for a = 1, n, 2 for a = 1, n, 2

View File

@ -204,6 +204,14 @@ local function collect
end end
self.processes[ pid ] = nil self.processes[ pid ] = nil
if self.onCollect
then
for _, func in ipairs( self.onCollect )
do
func( self:getUserIntf( ) )
end
end
end end
@ -469,7 +477,8 @@ local function getAlarm
( (
self self
) )
if #self.processes >= self.config.maxProcesses if self.stopped
or #self.processes >= self.config.maxProcesses
then then
return false return false
end end
@ -557,7 +566,8 @@ local function invokeActions
) )
log( 'Function', 'invokeActions( "', self.config.name, '", ', timestamp, ' )' ) log( 'Function', 'invokeActions( "', self.config.name, '", ', timestamp, ' )' )
if #self.processes >= self.config.maxProcesses if self.stopped
or #self.processes >= self.config.maxProcesses
then then
-- no new processes -- no new processes
return return
@ -731,13 +741,48 @@ local function getUserIntf
if ui then return ui end if ui then return ui end
ui = { muhkuh = true } ui = {
-- Stops the sync, meaning no more
-- processes will be spawned
stop = function( )
self.stopped = true
end,
-- Registers an additional function to be called
-- after each collect.
--
-- Used by default signal handlers to wait
-- for children to finish (or react on forwarded signal)
onCollect = function( func )
if not self.onCollect
then
self.onCollect = { func }
else
table.insert( self.onCollect, func )
end
end,
-- Returns a list of pids of children
-- processes
pids = function
( )
local pids = { }
for k, v in pairs( self.processes )
do
pids[ k ] = v
end
return pids
end,
}
self.userIntf = ui self.userIntf = ui
return ui return ui
end end
-- --
-- Creates a new Sync. -- Creates a new Sync.
-- --
@ -748,11 +793,12 @@ local function new
local s = local s =
{ {
-- fields -- fields
config = config, config = config,
delays = Queue.new( ), delays = Queue.new( ),
source = config.source, source = config.source,
processes = Counter.new( ), processes = Counter.new( ),
filters = nil, filters = nil,
stopped = false,
-- functions -- functions
addBlanketDelay = addBlanketDelay, addBlanketDelay = addBlanketDelay,
@ -765,11 +811,13 @@ local function new
getAlarm = getAlarm, getAlarm = getAlarm,
getDelays = getDelays, getDelays = getDelays,
getNextDelay = getNextDelay, getNextDelay = getNextDelay,
getUserIntf = getUserIntf,
invokeActions = invokeActions, invokeActions = invokeActions,
removeDelay = removeDelay, removeDelay = removeDelay,
rmExclude = rmExclude, rmExclude = rmExclude,
statusReport = statusReport, statusReport = statusReport,
-- use interface
getUserIntf = getUserIntf,
} }
s.inlet = InletFactory.newInlet( s ) s.inlet = InletFactory.newInlet( s )

View File

@ -77,7 +77,7 @@ end
-- --
local function get local function get
( i ) ( i )
return syncList[ i ]; return syncList[ i ]
end end
-- --

View File

@ -266,23 +266,38 @@ user.syncs =
-- iterator for user syncs -- iterator for user syncs
function iter function iter
( (
self, self, -- the syncs list copy
pos pos -- position
) )
pos = pos + 1 pos = pos + 1
if pos >= #SyncMaster then return nil end local result = self[ pos ]
local sync = SyncMaster.get( pos ) if result == nil then return nil end
return pos, sync:getUserIntf( ) return pos, self[ pos ]
end end
local mt = { } local mt = { }
--
-- This iterator will make a copy of the syncs at the moment
-- it is started, so the list can be manipulated while a loop
-- iterates through the copy.
--
mt.__ipairs = mt.__ipairs =
function( self ) function
return iter, self, -1 (
self -- is user.syncs
)
local list = { }
for k = 0, #SyncMaster - 1
do
list[ k ] = SyncMaster.get( k ):getUserIntf( )
end
return iter, list, -1
end end
-- public interface -- public interface