lsyncd/mantle/mci.lua

609 lines
11 KiB
Lua
Raw Permalink Normal View History

2010-10-19 10:20:27 +00:00
--
2018-03-30 13:15:49 +00:00
-- mci.lua from Lsyncd -- the Live (Mirror) Syncing Demon
--
2012-02-15 19:10:50 +00:00
--
2018-03-30 13:15:49 +00:00
-- The mantle part of the inteface between mantle and core.
--
2012-10-03 15:37:49 +00:00
--
2012-02-15 19:10:50 +00:00
-- License: GPLv2 (see COPYING) or any later version
-- Authors: Axel Kittenberger <axkibe@gmail.com>
--
if mantle
then
print( 'Error, Lsyncd mantle already loaded' )
os.exit( -1 )
2010-10-19 21:56:00 +00:00
end
2012-10-03 15:37:49 +00:00
--
2018-03-19 08:02:30 +00:00
-- Shortcuts
2010-10-22 12:58:57 +00:00
--
2018-03-19 08:02:30 +00:00
configure = core.configure
2018-03-13 11:29:43 +00:00
log = core.log
terminate = core.terminate
now = core.now
readdir = core.readdir
--
-- Minimum seconds between two writes of the status file.
--
local defaultStatusInterval = 10
--
-- Global: total number of processess running.
--
2018-03-15 16:49:12 +00:00
processCount = 0
--
-- Settings specified by command line.
--
2018-03-15 16:49:12 +00:00
clSettings = { }
2018-02-27 16:14:36 +00:00
--
-- Settings specified by config scripts.
--
uSettings = { }
2018-02-27 16:14:36 +00:00
2010-11-10 15:57:37 +00:00
--============================================================================
-- Mantle core interface. These functions are called from core.
2010-11-10 15:57:37 +00:00
--============================================================================
2012-10-03 07:23:18 +00:00
--
-- Current status of Lsyncd.
2010-11-10 15:57:37 +00:00
--
2012-01-30 14:01:18 +00:00
-- 'init' ... on (re)init
-- 'run' ... normal operation
-- 'fade' ... waits for remaining processes
2010-11-11 19:52:20 +00:00
--
2018-03-19 07:50:29 +00:00
lsyncdStatus = 'init'
2010-11-10 15:57:37 +00:00
2010-11-29 20:32:54 +00:00
--
-- The mantle cores interface
2012-10-03 07:23:18 +00:00
--
mci = { }
2018-03-13 11:29:43 +00:00
2010-11-10 15:57:37 +00:00
--
-- Last time said to be waiting for more child processes
--
local lastReportedWaiting = false
2012-10-03 07:23:18 +00:00
--
-- Called from core whenever Lua code failed.
--
2010-11-10 15:57:37 +00:00
-- Logs a backtrace
--
function mci.callError
2016-12-14 13:25:20 +00:00
(
message
)
2018-03-19 07:50:29 +00:00
log( 'Error', 'in Lua: ', message )
2012-10-03 07:23:18 +00:00
2010-11-10 15:57:37 +00:00
-- prints backtrace
local level = 2
2012-10-03 07:23:18 +00:00
2016-11-25 13:55:59 +00:00
while true
do
2012-10-03 07:23:18 +00:00
local info = debug.getinfo( level, 'Sl' )
if not info then terminate( -1 ) end
2012-10-03 07:23:18 +00:00
log(
2018-03-19 07:50:29 +00:00
'Error', 'Backtrace ',
2012-10-03 07:23:18 +00:00
level - 1, ' :',
info.short_src, ':',
info.currentline
)
2010-11-10 15:57:37 +00:00
level = level + 1
end
end
2012-10-03 07:23:18 +00:00
2018-03-14 08:57:49 +00:00
-- Registers the mantle with the core
core.mci( mci )
2018-03-14 08:57:49 +00:00
2012-10-03 07:23:18 +00:00
--
-- Called from core whenever a child process has finished and
-- the zombie process was collected by core.
2010-11-10 15:57:37 +00:00
--
function mci.collectProcess
2018-03-01 14:08:26 +00:00
(
pid, -- process id
exitcode -- exitcode
)
processCount = processCount - 1
2012-10-03 07:23:18 +00:00
2018-03-01 14:08:26 +00:00
if processCount < 0
then
2012-10-03 07:23:18 +00:00
error( 'negative number of processes!' )
end
2018-05-25 10:23:38 +00:00
for _, s in ipairs( SyncMaster.syncList( ) )
2018-03-01 14:08:26 +00:00
do
if s:collect( pid, exitcode ) then return end
2010-11-10 15:57:37 +00:00
end
end
2012-10-03 07:23:18 +00:00
--
2010-11-05 18:04:29 +00:00
-- Called from core everytime a masterloop cycle runs through.
2012-10-03 07:23:18 +00:00
--
2012-01-27 11:08:10 +00:00
-- This happens in case of
2010-11-05 18:04:29 +00:00
-- * an expired alarm.
-- * a returned child process.
-- * received filesystem events.
-- * received a HUP, TERM or INT signal.
2010-10-23 12:36:55 +00:00
--
2018-03-19 07:50:29 +00:00
function mci.cycle
(
2012-10-03 07:23:18 +00:00
timestamp -- the current kernel time (in jiffies)
)
2016-12-14 13:25:20 +00:00
log( 'Function', 'cycle( ', timestamp, ' )' )
2016-11-25 13:55:59 +00:00
if lsyncdStatus == 'fade'
then
if processCount > 0
then
2018-03-14 08:57:49 +00:00
if lastReportedWaiting == false
or timestamp >= lastReportedWaiting + 60
then
lastReportedWaiting = timestamp
2018-03-14 08:57:49 +00:00
log( 'Normal', 'waiting for ', processCount, ' more child processes.' )
end
2010-11-14 09:11:09 +00:00
return true
else
return false
end
end
2012-10-03 07:23:18 +00:00
2016-12-14 13:25:20 +00:00
if lsyncdStatus ~= 'run'
then
error( 'mci.cycle() called while not running!' )
2010-11-14 09:11:09 +00:00
end
2012-10-03 07:23:18 +00:00
--
2018-03-15 16:49:12 +00:00
-- Goes through all syncs and spawns more actions
-- if possibly. But only lets SyncMaster invoke actions if
-- not at global limit.
2012-10-03 07:23:18 +00:00
--
2016-12-14 13:25:20 +00:00
if not uSettings.maxProcesses
or processCount < uSettings.maxProcesses
2012-10-03 07:23:18 +00:00
then
2018-03-15 16:49:12 +00:00
local start = SyncMaster.getRound( )
2018-05-25 10:23:38 +00:00
if #SyncMaster > 0
then
local ir = start
local slist = SyncMaster.syncList( )
local scount = #SyncMaster
2012-10-03 07:23:18 +00:00
2018-05-25 10:23:38 +00:00
repeat
local s = slist[ ir ]
2016-12-14 13:25:20 +00:00
2018-05-25 10:23:38 +00:00
s:invokeActions( timestamp )
2016-12-14 13:25:20 +00:00
2018-05-25 10:23:38 +00:00
ir = ir + 1
2012-10-03 07:23:18 +00:00
2018-05-25 10:23:38 +00:00
if ir >= scount then ir = 0 end
until ir == start
2012-10-03 07:23:18 +00:00
2018-05-25 10:23:38 +00:00
SyncMaster.nextRound( )
end
2010-11-08 12:14:10 +00:00
end
2010-11-30 23:14:17 +00:00
2012-10-03 07:23:18 +00:00
UserAlarms.invoke( timestamp )
2010-11-30 23:14:17 +00:00
2016-11-25 13:55:59 +00:00
if uSettings.statusFile
then
2012-10-03 07:23:18 +00:00
StatusFile.write( timestamp )
2010-11-05 18:04:29 +00:00
end
2010-11-14 09:11:09 +00:00
return true
2010-10-22 23:14:11 +00:00
end
2012-10-03 07:23:18 +00:00
--
-- Called by core if '-help' or '--help' is in
2010-10-27 09:06:13 +00:00
-- the arguments.
--
function mci.help( )
2010-10-27 19:34:56 +00:00
io.stdout:write(
2010-11-04 13:43:57 +00:00
[[
2010-11-13 21:50:21 +00:00
2012-01-27 11:08:10 +00:00
USAGE:
lsyncd [OPTIONS] [CONFIG-FILE(S)]
2010-11-04 13:43:57 +00:00
OPTIONS:
-c STRING Executes STRING as Lua config
-delay SECS Overrides default delay times
2010-11-04 13:43:57 +00:00
-help Shows this
-log all Logs everything (debug)
-log scarce Logs errors only
-log CATEGORY Turns on logging for a debug category
2010-11-13 21:50:21 +00:00
-logfile FILE Writes log to FILE (DEFAULT: uses syslog)
-version Prints versions and exits
2010-11-04 13:43:57 +00:00
LICENSE:
GPLv2 or any later version.
SEE:
2018-03-12 12:36:34 +00:00
`man lsyncd` or visit https://axkibe.github.io/lsyncd/ for further information.
2010-10-27 09:06:13 +00:00
]])
2010-11-28 10:47:57 +00:00
2012-10-03 15:37:49 +00:00
os.exit( -1 )
2010-10-27 09:06:13 +00:00
end
2012-10-03 07:23:18 +00:00
--
2010-11-03 21:02:14 +00:00
-- Called from core to parse the command line arguments
--
2012-10-03 07:23:18 +00:00
-- returns a string as user script to load.
-- or simply 'true' if running with rsync bevaiour
--
-- terminates on invalid arguments.
--
2018-03-16 08:36:32 +00:00
function mci.configure(
args, -- command line arguments
2018-03-16 08:36:32 +00:00
monitors -- list of monitors the core can do
)
Monitor.initialize( monitors )
2012-10-03 07:23:18 +00:00
-- confs is filled with
-- all config file
-- stdin read requests
-- inline configs
local confs = { }
local i = 1
2012-10-08 07:10:03 +00:00
--
2012-10-03 07:23:18 +00:00
-- a list of all valid options
--
-- first paramter is the number of parameters an option takes
2012-10-03 15:37:49 +00:00
-- if < 0 the called function has to check the presence of
-- optional arguments.
2012-10-03 07:23:18 +00:00
--
-- second paramter is the function to call
2010-11-28 09:37:43 +00:00
--
2018-03-01 14:08:26 +00:00
local options =
{
c =
{ 1, function( string ) table.insert( confs, { command = string, n = i } ) end },
2012-10-03 15:37:49 +00:00
2012-10-03 07:23:18 +00:00
delay =
{ 1, function( secs ) clSettings.delay = secs + 0 end },
2012-10-03 07:23:18 +00:00
-- log is handled by core already.
log =
{ 1, nil },
2012-10-03 07:23:18 +00:00
2012-10-03 15:37:49 +00:00
logfile =
{ 1, function( file ) clSettings.logfile = file end },
2012-10-03 07:23:18 +00:00
2012-10-03 15:37:49 +00:00
version =
{ 0, function( ) io.stdout:write( 'Version: ', lsyncd_version, '\n' ) os.exit( 0 ) end }
2010-11-03 21:02:14 +00:00
}
2012-10-03 07:23:18 +00:00
2018-03-01 14:08:26 +00:00
while i <= #args
do
2012-10-03 15:37:49 +00:00
local a = args[ i ]
2012-10-03 07:23:18 +00:00
2018-03-01 14:08:26 +00:00
if a:sub( 1, 1 ) ~= '-'
then
table.insert( confs, { file = args[ i ] } )
elseif a == '-'
then
table.insert( confs, { stdin = true } )
2010-11-03 21:02:14 +00:00
else
if a:sub( 1, 2 ) == '--' then a = a:sub( 3 ) else a = a:sub( 2 ) end
2012-10-03 07:23:18 +00:00
local o = options[ a ]
if not o
then
log( 'Error', 'unknown command line option ', args[ i ] )
2018-03-01 14:08:26 +00:00
2012-10-03 15:37:49 +00:00
os.exit( -1 )
2010-11-03 21:02:14 +00:00
end
2012-10-03 07:23:18 +00:00
if o[ 1 ] >= 0 and i + o[ 1 ] > #args
then
2012-10-03 15:37:49 +00:00
log( 'Error', a ,' needs ', o[ 1 ],' arguments' )
2012-10-03 15:37:49 +00:00
os.exit( -1 )
elseif o[1] < 0
then
2012-10-03 15:37:49 +00:00
o[ 1 ] = -o[ 1 ]
end
if o[ 2 ]
then
if o[ 1 ] == 0
then
2012-10-03 15:37:49 +00:00
o[ 2 ]( )
elseif o[ 1 ] == 1
then
o[ 2 ]( args[ i + 1 ] )
elseif o[ 1 ] == 2
then
o[ 2 ]( args[ i + 1 ], args[ i + 2 ] )
elseif o[ 1 ] == 3
then
o[ 2 ]( args[ i + 1 ], args[ i + 2 ], args[ i + 3 ] )
end
end
i = i + o[ 1 ]
2010-11-03 21:02:14 +00:00
end
2012-10-03 15:37:49 +00:00
i = i + 1
2010-11-03 21:02:14 +00:00
end
if #confs == 0 then mci.help( args[ 0 ] ) end
for _, conf in ipairs( confs )
do
local f, err, status
if conf.stdin
then
f, err = load( core.stdin( ), 'stdin', 't', userenv )
elseif conf.command
then
f, err = load( conf.command, 'arg: '..conf.n, 't', userenv )
else
f, err = loadfile( conf.file, 't', userenv )
end
2012-10-03 15:37:49 +00:00
if not f
then
log( 'Error', err )
os.exit( -1 )
end
status, err = pcall( f )
if not status
then
log( 'Error', err )
os.exit( -1 )
end
2010-11-03 21:02:14 +00:00
end
end
2012-10-03 15:37:49 +00:00
--
2010-10-17 15:24:55 +00:00
-- Called from core on init or restart after user configuration.
2011-08-29 09:21:40 +00:00
--
2018-04-21 12:23:21 +00:00
function mci.initialize
(
firstTime -- true when Lsyncd startups the first time,
-- -- false on resets, due to HUP signal or monitor queue overflow.
)
-- Checks if user overwrote the settings function.
-- ( was Lsyncd <2.1 style )
2018-05-13 12:13:54 +00:00
if userenv.settings ~= user.settings
then
2012-10-08 07:10:03 +00:00
log(
'Error',
'Do not use settings = { ... }\n'..
2018-03-01 14:08:26 +00:00
' please use settings{ ... } ( without the equal sign )'
2012-10-08 07:10:03 +00:00
)
os.exit( -1 )
2012-10-08 07:10:03 +00:00
end
2012-01-27 11:08:10 +00:00
2018-05-13 12:13:54 +00:00
if userenv.init then userenv.init( ) end
2018-04-28 12:32:56 +00:00
lastReportedWaiting = false
--
2010-10-25 14:55:40 +00:00
-- From this point on, no globals may be created anymore
--
lockGlobals( )
2010-10-25 14:55:40 +00:00
--
2010-11-17 11:14:36 +00:00
-- all command line settings overwrite config file settings
--
for k, v in pairs( clSettings )
do
if k ~= 'syncs'
then
2012-10-08 07:10:03 +00:00
uSettings[ k ] = v
2010-11-13 21:50:21 +00:00
end
end
2012-02-15 15:47:18 +00:00
if uSettings.logfile
then
2018-03-19 08:02:30 +00:00
configure( 'logfile', uSettings.logfile )
2010-11-13 21:50:21 +00:00
end
2012-10-03 15:37:49 +00:00
if uSettings.logident
then
2018-03-19 08:02:30 +00:00
configure( 'logident', uSettings.logident )
end
2012-10-03 15:37:49 +00:00
if uSettings.logfacility
then
2018-03-19 08:02:30 +00:00
configure( 'logfacility', uSettings.logfacility )
end
2012-10-03 15:37:49 +00:00
--
2012-10-08 07:10:03 +00:00
-- Transfers some defaults to uSettings
2012-10-03 15:37:49 +00:00
--
if uSettings.statusInterval == nil
then
uSettings.statusInterval = defaultStatusInterval
2010-10-27 09:06:13 +00:00
end
2010-10-25 21:41:45 +00:00
2012-01-27 11:08:10 +00:00
-- makes sure the user gave Lsyncd anything to do
2018-03-15 16:49:12 +00:00
if #SyncMaster == 0
then
log( 'Error', 'Nothing to watch!' )
2012-10-03 15:37:49 +00:00
os.exit( -1 )
2010-10-21 12:37:27 +00:00
end
2010-11-03 11:37:25 +00:00
-- from now on use logging as configured instead of stdout/err.
2018-05-11 07:07:03 +00:00
lsyncdStatus = 'run'
2018-05-11 07:07:03 +00:00
configure( 'running' )
2012-01-27 11:08:10 +00:00
2018-03-01 14:08:26 +00:00
local ufuncs =
{
2012-10-03 15:37:49 +00:00
'onAttrib',
'onCreate',
'onDelete',
'onModify',
'onMove',
'onStartup',
2010-11-13 13:44:51 +00:00
}
2012-01-27 11:08:10 +00:00
2010-11-13 13:44:51 +00:00
-- translates layer 3 scripts
2018-05-14 06:56:42 +00:00
for _, s in ipairs( SyncMaster )
do
2010-11-13 13:44:51 +00:00
-- checks if any user functions is a layer 3 string.
local config = s.config
for _, fn in ipairs( ufuncs )
do
if type(config[fn]) == 'string'
then
2018-03-16 08:36:32 +00:00
local ft = FWriter.translate( config[ fn ] )
2018-03-09 09:42:10 +00:00
config[ fn ] = assert( load( 'return '..ft ) )( )
2010-11-13 13:44:51 +00:00
end
end
end
2010-11-07 09:53:39 +00:00
2010-11-17 18:52:55 +00:00
-- runs through the Syncs created by users
2018-05-14 06:56:42 +00:00
for _, s in ipairs( SyncMaster )
do
if s.config.monitor == 'inotify'
then
2012-10-03 15:37:49 +00:00
Inotify.addSync( s, s.source )
2010-11-28 10:47:57 +00:00
else
error( 'sync '.. s.config.name..' has unknown event monitor interface.' )
2010-11-28 10:47:57 +00:00
end
2012-10-03 15:37:49 +00:00
-- if the sync has an init function, the init delay
-- is stacked which causes the init function to be called.
if s.config.init
then
2012-10-03 15:37:49 +00:00
s:addInitDelay( )
2010-10-22 10:35:26 +00:00
end
end
2010-10-17 15:24:55 +00:00
end
2012-10-03 15:37:49 +00:00
--
-- Called by core to query the soonest alarm.
2010-10-19 10:12:11 +00:00
--
2016-11-25 13:55:59 +00:00
-- @return false ... no alarm, core can go in untimed sleep
2010-11-08 12:14:10 +00:00
-- true ... immediate action
2010-11-05 18:20:33 +00:00
-- times ... the alarm time (only read if number is 1)
--
function mci.getAlarm
2016-12-14 13:25:20 +00:00
( )
2018-05-12 13:08:14 +00:00
log( 'Function', 'getAlarm( )', lsyncdStatus )
2012-10-03 15:37:49 +00:00
2018-03-01 14:08:26 +00:00
if lsyncdStatus ~= 'run' then return false end
2012-10-03 15:37:49 +00:00
2010-11-05 18:20:33 +00:00
local alarm = false
2012-10-03 15:37:49 +00:00
--
-- Checks if 'a' is sooner than the 'alarm' up-value.
2010-11-06 21:29:22 +00:00
--
2016-12-14 13:25:20 +00:00
local function checkAlarm
(
2018-03-01 14:08:26 +00:00
a -- alarm time
2016-12-14 13:25:20 +00:00
)
2018-03-01 14:08:26 +00:00
if a == nil then error( 'got nil alarm' ) end
2012-10-03 15:37:49 +00:00
2016-11-25 13:55:59 +00:00
if alarm == true or not a
then
2012-10-03 15:37:49 +00:00
-- 'alarm' is already immediate or
-- a not a new alarm
2010-12-01 13:25:05 +00:00
return
2010-11-06 21:29:22 +00:00
end
2012-10-03 15:37:49 +00:00
-- sets 'alarm' to a if a is sooner
2016-11-25 13:55:59 +00:00
if not alarm or a < alarm
then
2010-12-01 13:25:05 +00:00
alarm = a
2010-10-24 16:41:58 +00:00
end
end
2010-11-06 21:29:22 +00:00
2012-10-03 15:37:49 +00:00
--
-- checks all syncs for their earliest alarm,
-- but only if the global process limit is not yet reached.
2012-10-03 15:37:49 +00:00
--
2016-12-14 13:25:20 +00:00
if not uSettings.maxProcesses
or processCount < uSettings.maxProcesses
2012-10-03 15:37:49 +00:00
then
2018-05-14 06:56:42 +00:00
for _, s in ipairs( SyncMaster )
2016-12-14 13:25:20 +00:00
do
checkAlarm( s:getAlarm( ) )
end
else
2018-05-12 13:08:14 +00:00
log( 'Alarm', 'at global process limit.' )
2010-11-06 10:10:57 +00:00
end
2010-11-06 21:29:22 +00:00
-- checks if a statusfile write has been delayed
2012-10-03 15:37:49 +00:00
checkAlarm( StatusFile.getAlarm( ) )
2010-11-30 23:14:17 +00:00
-- checks for an userAlarm
2012-10-03 15:37:49 +00:00
checkAlarm( UserAlarms.getAlarm( ) )
log( 'Alarm', 'mci.getAlarm returns: ', alarm )
2010-11-06 10:10:57 +00:00
2010-11-05 18:20:33 +00:00
return alarm
2010-10-19 10:12:11 +00:00
end
2010-10-17 15:24:55 +00:00
2010-11-10 15:57:37 +00:00
2012-10-03 15:37:49 +00:00
--
-- Called when an file system monitor events arrive
2010-11-26 16:19:56 +00:00
--
mci.inotifyEvent = Inotify.event
2010-10-21 12:37:27 +00:00
2018-06-04 06:54:06 +00:00
2010-10-21 12:37:27 +00:00
--
2012-10-03 15:37:49 +00:00
-- Collector for every child process that finished in startup phase
2010-10-21 12:37:27 +00:00
--
function mci.collector
2017-01-09 12:13:05 +00:00
(
2012-10-03 15:37:49 +00:00
pid, -- pid of the child process
exitcode -- exitcode of the child process
)
if exitcode ~= 0
then
2018-03-01 14:08:26 +00:00
log( 'Error', 'Startup process', pid, ' failed' )
2012-10-03 15:37:49 +00:00
terminate( -1 )
2010-10-20 13:01:26 +00:00
end
2012-10-03 15:37:49 +00:00
2010-10-21 12:37:27 +00:00
return 0
2010-10-20 10:25:34 +00:00
end
2012-10-03 15:37:49 +00:00
--
2010-11-10 15:57:37 +00:00
-- Called by core when an overflow happened.
--
function mci.overflow
2018-03-01 14:08:26 +00:00
( )
log( 'Normal', '--- OVERFLOW in event queue ---' )
2012-10-03 15:37:49 +00:00
2012-01-30 14:01:18 +00:00
lsyncdStatus = 'fade'
2010-11-14 09:11:09 +00:00
end