2012-02-15 19:10:50 +00:00
|
|
|
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2010-10-19 10:20:27 +00:00
|
|
|
-- lsyncd.lua Live (Mirror) Syncing Demon
|
|
|
|
--
|
2018-03-14 16:50:51 +00:00
|
|
|
--
|
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
|
|
|
--
|
2018-03-14 16:50:51 +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>
|
|
|
|
--
|
|
|
|
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2016-12-12 18:53:44 +00:00
|
|
|
|
2018-03-13 15:42:31 +00:00
|
|
|
if mantle
|
2016-12-01 12:25:49 +00:00
|
|
|
then
|
2018-03-14 16:50:51 +00:00
|
|
|
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
|
|
|
|
2016-12-12 18:53:44 +00:00
|
|
|
|
2012-10-02 05:53:13 +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
|
2012-10-02 05:53:13 +00:00
|
|
|
|
2016-12-12 18:53:44 +00:00
|
|
|
|
2012-10-02 05:53:13 +00:00
|
|
|
--
|
2016-12-12 18:53:44 +00:00
|
|
|
-- Global: total number of processess running.
|
2012-10-02 05:53:13 +00:00
|
|
|
--
|
2018-03-15 16:49:12 +00:00
|
|
|
processCount = 0
|
2011-08-18 13:29:18 +00:00
|
|
|
|
2016-12-12 18:53:44 +00:00
|
|
|
|
2018-03-14 16:50:51 +00:00
|
|
|
--
|
|
|
|
-- Settings specified by command line.
|
|
|
|
--
|
2018-03-15 16:49:12 +00:00
|
|
|
clSettings = { }
|
2018-02-27 16:14:36 +00:00
|
|
|
|
|
|
|
|
2018-03-14 16:50:51 +00:00
|
|
|
--
|
|
|
|
-- Settings specified by config scripts.
|
|
|
|
--
|
|
|
|
uSettings = { }
|
2018-02-27 16:14:36 +00:00
|
|
|
|
|
|
|
|
2018-03-14 16:50:51 +00:00
|
|
|
|
2010-11-10 15:57:37 +00:00
|
|
|
--============================================================================
|
2018-03-15 08:27:14 +00:00
|
|
|
-- Mantle core interface. These functions are called from core.
|
2010-11-10 15:57:37 +00:00
|
|
|
--============================================================================
|
|
|
|
|
2018-03-12 12:20:26 +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
|
|
|
--
|
2018-03-15 08:27:14 +00:00
|
|
|
-- The mantle cores interface
|
2012-10-03 07:23:18 +00:00
|
|
|
--
|
2018-03-15 08:27:14 +00:00
|
|
|
mci = { }
|
2018-03-13 11:29:43 +00:00
|
|
|
|
2010-11-10 15:57:37 +00:00
|
|
|
|
2012-10-23 12:31:54 +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
|
|
|
|
--
|
2018-03-15 08:27:14 +00:00
|
|
|
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' )
|
|
|
|
|
2018-03-13 11:02:34 +00:00
|
|
|
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
|
2018-03-15 08:27:14 +00:00
|
|
|
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
|
|
|
--
|
2018-03-15 08:27:14 +00:00
|
|
|
function mci.collectProcess
|
2018-03-01 14:08:26 +00:00
|
|
|
(
|
|
|
|
pid, -- process id
|
|
|
|
exitcode -- exitcode
|
|
|
|
)
|
2011-08-18 13:29:18 +00:00
|
|
|
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
|
2011-08-18 13:29:18 +00:00
|
|
|
|
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.
|
2011-08-18 13:29:18 +00:00
|
|
|
-- * received filesystem events.
|
2013-07-30 10:20:23 +00:00
|
|
|
-- * 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
|
2012-10-23 12:31:54 +00:00
|
|
|
then
|
|
|
|
lastReportedWaiting = timestamp
|
|
|
|
|
2018-03-14 08:57:49 +00:00
|
|
|
log( 'Normal', 'waiting for ', processCount, ' more child processes.' )
|
2012-10-23 12:31:54 +00:00
|
|
|
end
|
2012-10-05 07:41:46 +00:00
|
|
|
|
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
|
2018-03-15 08:27:14 +00:00
|
|
|
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( )
|
2012-10-05 07:41:46 +00:00
|
|
|
|
2011-08-18 13:29:18 +00:00
|
|
|
local ir = start
|
2012-10-03 07:23:18 +00:00
|
|
|
|
2011-08-18 13:29: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
|
|
|
|
2011-08-18 13:29:18 +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
|
2011-08-18 13:29:18 +00:00
|
|
|
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.
|
|
|
|
--
|
2018-03-15 08:27:14 +00:00
|
|
|
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:
|
2010-11-28 20:16:56 +00:00
|
|
|
-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 ]
|
|
|
|
|
2016-12-01 11:52:09 +00:00
|
|
|
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
|
|
|
|
2016-12-01 11:52:09 +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' )
|
2016-12-01 11:52:09 +00:00
|
|
|
|
2012-10-03 15:37:49 +00:00
|
|
|
os.exit( -1 )
|
2016-12-01 11:52:09 +00:00
|
|
|
elseif o[1] < 0
|
|
|
|
then
|
2012-10-03 15:37:49 +00:00
|
|
|
o[ 1 ] = -o[ 1 ]
|
|
|
|
end
|
|
|
|
|
2016-12-01 11:52:09 +00:00
|
|
|
if o[ 2 ]
|
|
|
|
then
|
|
|
|
if o[ 1 ] == 0
|
|
|
|
then
|
2012-10-03 15:37:49 +00:00
|
|
|
o[ 2 ]( )
|
2016-12-01 11:52:09 +00:00
|
|
|
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] )
|
2010-11-28 20:16:56 +00:00
|
|
|
end
|
|
|
|
end
|
2016-12-12 18:53:44 +00:00
|
|
|
|
2010-11-28 20:16:56 +00:00
|
|
|
i = i + o[1]
|
2010-11-03 21:02:14 +00:00
|
|
|
end
|
2012-10-03 15:37:49 +00:00
|
|
|
|
2016-12-12 18:53:44 +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
|
2016-12-01 11:52:09 +00:00
|
|
|
then
|
2018-03-15 08:27:14 +00:00
|
|
|
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
|
|
|
--
|
2018-03-15 08:27:14 +00:00
|
|
|
function mci.initialize( firstTime )
|
2012-10-03 15:37:49 +00:00
|
|
|
|
2016-12-01 11:52:09 +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
|
2016-12-01 11:52:09 +00:00
|
|
|
then
|
2012-10-08 07:10:03 +00:00
|
|
|
log(
|
2016-12-01 11:52:09 +00:00
|
|
|
'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
|
|
|
)
|
|
|
|
|
2016-12-01 11:52:09 +00:00
|
|
|
os.exit( -1 )
|
2012-10-08 07:10:03 +00:00
|
|
|
end
|
2012-01-27 11:08:10 +00:00
|
|
|
|
2012-10-23 12:31:54 +00:00
|
|
|
lastReportedWaiting = false
|
|
|
|
|
2012-10-05 07:41:46 +00:00
|
|
|
--
|
2010-10-25 14:55:40 +00:00
|
|
|
-- From this point on, no globals may be created anymore
|
2012-10-05 07:41:46 +00:00
|
|
|
--
|
|
|
|
lockGlobals( )
|
2010-10-25 14:55:40 +00:00
|
|
|
|
2012-10-06 11:43:55 +00:00
|
|
|
--
|
2010-11-17 11:14:36 +00:00
|
|
|
-- all command line settings overwrite config file settings
|
2012-10-06 11:43:55 +00:00
|
|
|
--
|
2016-12-01 11:52:09 +00:00
|
|
|
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
|
|
|
|
2016-12-01 11:52:09 +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
|
|
|
|
2016-12-01 11:52:09 +00:00
|
|
|
if uSettings.logident
|
|
|
|
then
|
2018-03-19 08:02:30 +00:00
|
|
|
configure( 'logident', uSettings.logident )
|
2011-03-01 14:57:26 +00:00
|
|
|
end
|
2012-10-03 15:37:49 +00:00
|
|
|
|
2016-12-01 11:52:09 +00:00
|
|
|
if uSettings.logfacility
|
|
|
|
then
|
2018-03-19 08:02:30 +00:00
|
|
|
configure( 'logfacility', uSettings.logfacility )
|
2011-03-01 14:57:26 +00:00
|
|
|
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
|
|
|
--
|
2016-12-01 11:52:09 +00:00
|
|
|
if uSettings.statusInterval == nil
|
|
|
|
then
|
2018-03-23 16:36:51 +00:00
|
|
|
uSettings.statusInterval = userENV.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
|
2016-12-01 11:52:09 +00:00
|
|
|
then
|
2018-03-12 12:20:26 +00:00
|
|
|
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';
|
2012-10-05 07:41:46 +00:00
|
|
|
|
2018-03-19 08:02:30 +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-03-15 16:49:12 +00:00
|
|
|
for _, s in SyncMaster.iwalk()
|
2016-12-12 18:53:44 +00:00
|
|
|
do
|
2010-11-13 13:44:51 +00:00
|
|
|
-- checks if any user functions is a layer 3 string.
|
|
|
|
local config = s.config
|
2012-10-05 07:41:46 +00:00
|
|
|
|
2016-12-12 18:53:44 +00:00
|
|
|
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 ] )
|
2012-10-05 07:41:46 +00:00
|
|
|
|
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( )
|
2016-12-01 12:25:49 +00:00
|
|
|
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
|
2018-03-14 16:50:51 +00:00
|
|
|
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.
|
2016-12-01 12:25:49 +00:00
|
|
|
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)
|
|
|
|
--
|
2018-03-15 08:27:14 +00:00
|
|
|
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,
|
2011-08-18 13:29:18 +00:00
|
|
|
-- 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( ) )
|
2011-08-18 13:29:18 +00:00
|
|
|
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
|
2011-08-18 13:29:18 +00:00
|
|
|
|
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( ) )
|
|
|
|
|
2018-03-15 08:27:14 +00:00
|
|
|
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
|
|
|
--
|
2018-03-15 08:27:14 +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
|
|
|
--
|
2018-03-15 08:27:14 +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
|
|
|
|
)
|
2016-12-01 12:25:49 +00:00
|
|
|
if exitcode ~= 0
|
|
|
|
then
|
2018-03-01 14:08:26 +00:00
|
|
|
log( 'Error', 'Startup process', pid, ' failed' )
|
2016-12-01 12:25:49 +00:00
|
|
|
|
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.
|
|
|
|
--
|
2018-03-15 08:27:14 +00:00
|
|
|
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.
|
|
|
|
--
|
2018-03-15 08:27:14 +00:00
|
|
|
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.
|
|
|
|
--
|
2018-03-15 08:27:14 +00:00
|
|
|
function mci.term
|
2018-03-01 14:08:26 +00:00
|
|
|
(
|
|
|
|
sigcode -- signal code
|
|
|
|
)
|
|
|
|
local sigtexts =
|
|
|
|
{
|
|
|
|
[ 2 ] = 'INT',
|
|
|
|
[ 15 ] = 'TERM'
|
2013-07-30 10:20:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
|