lsyncd/mantle/lsyncd.lua

893 lines
16 KiB
Lua
Raw Normal View History

2012-02-15 19:10:50 +00:00
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2010-10-19 10:20:27 +00:00
-- lsyncd.lua Live (Mirror) Syncing Demon
--
--
2010-11-17 11:14:36 +00:00
-- This is the "runner" part of Lsyncd. It containts all its high-level logic.
-- It works closely together with the Lsyncd core in lsyncd.c. This means it
2010-10-19 10:20:27 +00:00
-- cannot be runned directly from the standard lua interpreter.
2012-02-15 19:10:50 +00:00
--
--
2012-10-03 15:37:49 +00:00
-- This code assumes your editor is at least 100 chars wide.
--
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
--
-- Safes mantle stuff wrapped away from user scripts
2010-11-05 18:20:33 +00:00
--
2018-03-14 08:57:49 +00:00
local core = core
local lockGlobals = lockGlobals
local Inotify = Inotify
local Array = Array
local Queue = Queue
local Combiner = Combiner
local Delay = Delay
local InletFactory = InletFactory
local Filter = Filter
2010-11-05 18:20:33 +00:00
--
2010-10-21 12:37:27 +00:00
-- Shortcuts (which user is supposed to be able to use them as well)
2010-10-22 12:58:57 +00:00
--
2018-03-13 11:29:43 +00:00
log = core.log
terminate = core.terminate
now = core.now
readdir = core.readdir
--
-- Global: total number of processess running.
--
2018-03-15 16:49:12 +00:00
processCount = 0
--
-- All valid entries in a settings{} call.
--
local settingsCheckgauge =
{
logfile = true,
statusFile = true,
statusInterval = true,
logfacility = true,
logident = true,
inotifyMode = true,
maxProcesses = true,
maxDelays = true,
}
2018-02-27 16:14:36 +00:00
--
-- 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-03-15 16:49:12 +00:00
for _, s in SyncMaster.iwalk( )
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( )
local ir = start
2012-10-03 07:23:18 +00:00
repeat
2018-03-15 16:49:12 +00:00
local s = SyncMaster.get( ir )
2016-12-14 13:25:20 +00:00
2012-10-03 07:23:18 +00:00
s:invokeActions( timestamp )
2016-12-14 13:25:20 +00:00
ir = ir + 1
2012-10-03 07:23:18 +00:00
2018-03-15 16:49:12 +00:00
if ir >= #SyncMaster then ir = 0 end
until ir == start
2012-10-03 07:23:18 +00:00
2018-03-15 16:49:12 +00:00
SyncMaster.nextRound( )
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:
2018-03-12 12:36:34 +00:00
lsyncd [OPTIONS] [CONFIG-FILE]
2010-11-04 13:43:57 +00:00
OPTIONS:
-delay SECS Overrides default delay times
2010-11-04 13:43:57 +00:00
-help Shows this
2010-11-13 21:50:21 +00:00
-log all Logs everything (debug)
2010-11-04 13:43:57 +00:00
-log scarce Logs errors only
2010-11-04 14:23:34 +00:00
-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, -- arguments given by user
monitors -- list of monitors the core can do
)
Monitor.initialize( monitors )
2012-10-03 07:23:18 +00:00
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 =
{
2010-11-03 21:02:14 +00:00
-- log is handled by core already.
2012-10-03 15:37:49 +00:00
2012-10-03 07:23:18 +00:00
delay =
2018-03-01 14:08:26 +00:00
{
1,
2018-03-01 14:19:30 +00:00
function
(
secs
)
2018-03-01 14:08:26 +00:00
clSettings.delay = secs + 0
end
},
2012-10-03 07:23:18 +00:00
2018-03-12 12:36:34 +00:00
log = { 1, nil },
2012-10-03 07:23:18 +00:00
2012-10-03 15:37:49 +00:00
logfile =
2018-03-01 14:08:26 +00:00
{
1,
2018-03-01 14:19:30 +00:00
function
(
file
)
2018-03-01 14:08:26 +00:00
clSettings.logfile = file
end
},
2012-10-03 07:23:18 +00:00
2012-10-03 15:37:49 +00:00
version =
2018-03-01 14:08:26 +00:00
{
0,
2018-03-01 14:19:30 +00:00
function
( )
2018-03-01 14:08:26 +00:00
io.stdout:write( 'Version: ', lsyncd_version, '\n' )
2018-03-01 14:19:30 +00:00
2018-03-01 14:08:26 +00:00
os.exit( 0 )
end
}
2010-11-03 21:02:14 +00:00
}
2012-10-03 07:23:18 +00:00
-- non-opts is filled with all args that were no part dash options
local nonopts = { }
2012-10-03 15:37:49 +00:00
local i = 1
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
2012-10-03 07:23:18 +00:00
table.insert( nonopts, args[ i ] )
2010-11-03 21:02:14 +00:00
else
2018-03-01 14:08:26 +00:00
if a:sub( 1, 2 ) == '--'
then
2012-10-03 07:23:18 +00:00
a = a:sub( 3 )
2010-11-03 21:02:14 +00:00
else
2012-10-03 07:23:18 +00:00
a = a:sub( 2 )
2010-11-03 21:02:14 +00:00
end
2012-10-03 07:23:18 +00:00
local o = options[ a ]
if not o
then
2018-03-01 14:08:26 +00:00
log( 'Error', 'unknown option command line option ', args[ i ] )
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
2018-03-12 12:36:34 +00:00
if #nonopts == 0
then
mci.help( args[ 0 ] )
2018-03-12 12:36:34 +00:00
elseif #nonopts == 1
then
return nonopts[ 1 ]
2010-11-13 20:47:45 +00:00
else
2018-03-12 12:36:34 +00:00
-- TODO make this possible
log( 'Error', 'There can only be one config file in the command line.' )
2012-10-03 15:37:49 +00:00
2018-03-12 12:36:34 +00:00
os.exit( -1 )
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
--
2012-10-03 15:37:49 +00:00
-- firstTime:
-- true when Lsyncd startups the first time,
-- false on resets, due to HUP signal or monitor queue overflow.
2012-01-27 11:08:10 +00:00
--
function mci.initialize( firstTime )
2012-10-03 15:37:49 +00:00
-- Checks if user overwrote the settings function.
-- ( was Lsyncd <2.1 style )
2018-03-15 16:49:12 +00:00
if userENV.settings ~= 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
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-13 11:29:43 +00:00
core.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-13 11:29:43 +00:00
core.configure( 'logident', uSettings.logident )
end
2012-10-03 15:37:49 +00:00
if uSettings.logfacility
then
2018-03-13 11:29:43 +00:00
core.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
2012-10-08 07:10:03 +00:00
uSettings.statusInterval = default.statusInterval
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.
2012-01-30 14:01:18 +00:00
lsyncdStatus = 'run';
2018-03-13 11:29:43 +00:00
core.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-03-15 16:49:12 +00:00
for _, s in SyncMaster.iwalk()
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-03-15 16:49:12 +00:00
for _, s in SyncMaster.iwalk( )
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
( )
log( 'Function', 'getAlarm( )' )
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-03-15 16:49:12 +00:00
for _, s in SyncMaster.iwalk( )
2016-12-14 13:25:20 +00:00
do
checkAlarm( s:getAlarm( ) )
end
else
2012-10-03 15:37:49 +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
--
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
2012-10-03 15:37:49 +00:00
--
2010-11-14 09:11:09 +00:00
-- Called by core on a hup signal.
--
function mci.hup
2018-03-01 14:08:26 +00:00
( )
log( 'Normal', '--- HUP signal, resetting ---' )
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
2012-10-03 15:37:49 +00:00
--
2010-11-14 09:11:09 +00:00
-- Called by core on a term signal.
--
function mci.term
2018-03-01 14:08:26 +00:00
(
sigcode -- signal code
)
local sigtexts =
{
[ 2 ] = 'INT',
[ 15 ] = 'TERM'
};
local sigtext = sigtexts[ sigcode ];
2018-03-01 14:08:26 +00:00
if not sigtext then sigtext = 'UNKNOWN' end
2012-10-03 15:37:49 +00:00
2018-03-01 14:08:26 +00:00
log( 'Normal', '--- ', sigtext, ' signal, fading ---' )
2012-10-03 15:37:49 +00:00
2012-01-30 14:01:18 +00:00
lsyncdStatus = 'fade'
2012-10-03 15:37:49 +00:00
2010-11-10 15:57:37 +00:00
end
2010-10-21 12:37:27 +00:00
2010-10-25 17:38:57 +00:00
--============================================================================
2012-10-03 15:37:49 +00:00
-- Lsyncd runner's user interface
2010-10-25 17:38:57 +00:00
--============================================================================
2010-10-17 15:24:55 +00:00
2012-10-03 15:37:49 +00:00
--
2010-11-05 15:18:01 +00:00
-- Main utility to create new observations.
--
2012-10-03 15:37:49 +00:00
-- Returns an Inlet to that sync.
--
2018-03-01 10:26:12 +00:00
function sync
(
opts
)
if lsyncdStatus ~= 'init'
then
error( 'Sync can only be created during initialization.', 2 )
2010-11-05 15:18:01 +00:00
end
2012-10-03 15:37:49 +00:00
2018-03-15 16:49:12 +00:00
return SyncMaster.add( opts ).inlet
2010-11-05 15:18:01 +00:00
end
2010-10-17 15:24:55 +00:00
2010-10-19 16:40:49 +00:00
2010-10-27 11:31:18 +00:00
--
2012-10-03 15:37:49 +00:00
-- Spawns a new child process.
2010-11-07 01:06:08 +00:00
--
function spawn
(
2012-10-03 15:37:49 +00:00
agent, -- the reason why a process is spawned.
-- a delay or delay list for a sync
-- it will mark the related files as blocked.
binary, -- binary to call
... -- arguments
)
2018-03-01 14:08:26 +00:00
if agent == nil
or type( agent ) ~= 'table'
2012-10-03 15:37:49 +00:00
then
2018-03-01 14:08:26 +00:00
error( 'spawning with an invalid agent', 2 )
2010-11-11 15:17:22 +00:00
end
2018-03-01 14:08:26 +00:00
if lsyncdStatus == 'fade'
then
log( 'Normal', 'ignored process spawning while fading' )
2012-01-30 14:01:18 +00:00
return
2010-11-14 09:11:09 +00:00
end
2018-03-01 14:08:26 +00:00
if type( binary ) ~= 'string'
then
error( 'calling spawn(agent, binary, ...): binary is not a string', 2 )
end
2012-10-03 15:37:49 +00:00
local dol = InletFactory.getDelayOrList( agent )
2018-03-01 14:08:26 +00:00
if not dol
then
error( 'spawning with an unknown agent', 2 )
2012-10-03 15:37:49 +00:00
end
--
-- checks if a spawn is called on an already active event
--
if dol.status
then
2012-10-03 15:37:49 +00:00
-- is an event
if dol.status ~= 'wait'
then
error( 'spawn() called on an non-waiting event', 2 )
end
2012-10-03 15:37:49 +00:00
else
-- is a list
for _, d in ipairs( dol )
do
if d.status ~= 'wait'
and d.status ~= 'block'
then
error( 'spawn() called on an non-waiting event list', 2 )
end
end
end
2012-10-03 15:37:49 +00:00
--
-- tries to spawn the process
--
2018-03-13 11:29:43 +00:00
local pid = core.exec( binary, ... )
if pid and pid > 0
then
processCount = processCount + 1
2018-03-01 14:08:26 +00:00
if uSettings.maxProcesses
and processCount > uSettings.maxProcesses
2012-10-03 15:37:49 +00:00
then
error( 'Spawned too much processes!' )
end
2012-10-03 15:37:49 +00:00
local sync = InletFactory.getSync( agent )
2010-11-30 22:56:34 +00:00
-- delay or list
if dol.status
then
2010-11-30 22:56:34 +00:00
-- is a delay
dol:setActive( )
2012-10-03 15:37:49 +00:00
sync.processes[ pid ] = dol
2012-02-15 15:47:18 +00:00
else
2010-11-30 22:56:34 +00:00
-- is a list
for _, d in ipairs( dol )
do
d:setActive( )
2010-11-12 18:52:43 +00:00
end
2012-10-03 15:37:49 +00:00
sync.processes[ pid ] = dol
2010-11-12 18:52:43 +00:00
end
2010-11-07 01:06:08 +00:00
end
2010-11-05 13:34:02 +00:00
end
2012-10-03 15:37:49 +00:00
--
2011-03-21 08:33:37 +00:00
-- Spawns a child process using the default shell.
2010-11-07 01:06:08 +00:00
--
function spawnShell
(
2012-10-03 15:37:49 +00:00
agent, -- the delay(list) to spawn the command for
command, -- the shell command
... -- additonal arguments
)
2018-03-01 14:08:26 +00:00
return spawn( agent, '/bin/sh', '-c', command, '/bin/sh', ... )
2010-10-27 11:31:18 +00:00
end
--
-- Observes a filedescriptor.
2010-12-01 12:19:17 +00:00
--
function observefd
(
2012-10-03 15:37:49 +00:00
fd, -- file descriptor
ready, -- called when fd is ready to be read
writey -- called when fd is ready to be written
)
2018-03-13 11:29:43 +00:00
return core.observe_fd( fd, ready, writey )
2010-12-01 12:19:17 +00:00
end
2010-12-01 12:19:17 +00:00
--
-- Stops observeing a filedescriptor.
2012-10-03 15:37:49 +00:00
--
function nonobservefd
(
2012-10-03 15:37:49 +00:00
fd -- file descriptor
)
2018-03-13 11:29:43 +00:00
return core.nonobserve_fd( fd )
2010-12-01 12:19:17 +00:00
end
2012-10-03 15:37:49 +00:00
--
2010-11-30 23:14:17 +00:00
-- Calls func at timestamp.
2012-10-03 15:37:49 +00:00
--
2010-11-30 23:14:17 +00:00
-- Use now() to receive current timestamp
2012-10-03 15:37:49 +00:00
-- add seconds with '+' to it
2010-11-30 23:14:17 +00:00
--
alarm = UserAlarms.alarm
2012-10-03 15:37:49 +00:00
--
2018-03-15 16:49:12 +00:00
-- Comfort routine, also for user.
2018-03-19 07:50:29 +00:00
-- Returns true if 'str' starts with 'Start'
2010-11-10 11:23:26 +00:00
--
2016-12-13 13:41:35 +00:00
function string.starts
(
2018-03-19 07:50:29 +00:00
str,
2016-12-13 13:41:35 +00:00
Start
)
2018-03-19 07:50:29 +00:00
return string.sub( str, 1, #Start ) == Start
2010-11-10 11:23:26 +00:00
end
2012-10-03 15:37:49 +00:00
--
2018-03-15 16:49:12 +00:00
-- Comfort routine, also for user.
2018-03-19 07:50:29 +00:00
-- Returns true if 'str' ends with 'End'
2010-11-10 11:23:26 +00:00
--
2016-12-13 13:41:35 +00:00
function string.ends
(
2018-03-19 07:50:29 +00:00
str,
2016-12-13 13:41:35 +00:00
End
)
2018-03-19 07:50:29 +00:00
return End == '' or string.sub( str, -#End ) == End
2010-11-10 11:23:26 +00:00
end
--
2016-12-13 13:41:35 +00:00
-- The settings call
2012-10-03 15:37:49 +00:00
--
2016-12-13 13:41:35 +00:00
function settings
(
a1 -- a string for getting a setting
-- or a table of key/value pairs to set these settings
)
-- if a1 is a string this is a get operation
if type( a1 ) == 'string'
then
return uSettings[ a1 ]
end
-- if its a table it sets all the value of the bale
for k, v in pairs( a1 )
do
if type( k ) ~= 'number'
then
if not settingsCheckgauge[ k ]
then
2018-03-01 14:08:26 +00:00
error( 'setting "'..k..'" unknown.', 2 )
end
2012-10-08 07:10:03 +00:00
uSettings[ k ] = v
else
if not settingsCheckgauge[ v ]
then
2018-03-01 14:08:26 +00:00
error( 'setting "'..v..'" unknown.', 2 )
end
2012-10-08 07:10:03 +00:00
uSettings[ v ] = true
end
end
end