lsyncd/mantle/lsyncd.lua

1422 lines
24 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
--============================================================================
-- Lsyncd Prototypes
--============================================================================
2018-02-27 16:14:36 +00:00
2012-10-03 07:23:18 +00:00
--
2010-11-28 09:37:43 +00:00
-- Holds information about the event monitor capabilities
-- of the core.
--
2016-12-13 13:41:35 +00:00
Monitors = ( function
( )
2012-10-03 07:23:18 +00:00
--
2010-11-28 09:37:43 +00:00
-- The cores monitor list
2010-11-28 10:47:57 +00:00
--
2012-10-03 07:23:18 +00:00
local list = { }
2010-11-28 09:37:43 +00:00
2012-10-03 07:23:18 +00:00
--
2010-11-28 10:47:57 +00:00
-- The default event monitor.
--
2018-03-01 14:08:26 +00:00
local function default
( )
2012-10-03 07:23:18 +00:00
return list[ 1 ]
2010-11-28 10:47:57 +00:00
end
2012-10-03 07:23:18 +00:00
2010-11-28 09:37:43 +00:00
--
2012-10-03 07:23:18 +00:00
-- Initializes with info received from core
--
local function initialize( clist )
2016-12-13 13:41:35 +00:00
for k, v in ipairs( clist )
do
2012-10-03 07:23:18 +00:00
list[ k ] = v
2010-11-28 09:37:43 +00:00
end
end
2012-10-03 07:23:18 +00:00
--
-- Public interface
--
return {
default = default,
list = list,
initialize = initialize
2010-11-28 09:37:43 +00:00
}
2012-10-03 07:23:18 +00:00
end)( )
2010-11-13 13:13:51 +00:00
--
2012-10-03 07:23:18 +00:00
-- Writes functions for the user for layer 3 configurations.
--
local functionWriter = ( function( )
2010-11-13 13:13:51 +00:00
2012-10-03 07:23:18 +00:00
--
-- All variables known to layer 3 configs.
--
2010-11-13 13:13:51 +00:00
transVars = {
2012-01-27 14:19:17 +00:00
{ '%^pathname', 'event.pathname', 1 },
{ '%^pathdir', 'event.pathdir', 1 },
{ '%^path', 'event.path', 1 },
{ '%^sourcePathname', 'event.sourcePathname', 1 },
{ '%^sourcePathdir', 'event.sourcePathdir', 1 },
{ '%^sourcePath', 'event.sourcePath', 1 },
{ '%^source', 'event.source', 1 },
{ '%^targetPathname', 'event.targetPathname', 1 },
{ '%^targetPathdir', 'event.targetPathdir', 1 },
{ '%^targetPath', 'event.targetPath', 1 },
{ '%^target', 'event.target', 1 },
{ '%^o%.pathname', 'event.pathname', 1 },
{ '%^o%.path', 'event.path', 1 },
{ '%^o%.sourcePathname', 'event.sourcePathname', 1 },
{ '%^o%.sourcePathdir', 'event.sourcePathdir', 1 },
{ '%^o%.sourcePath', 'event.sourcePath', 1 },
{ '%^o%.targetPathname', 'event.targetPathname', 1 },
{ '%^o%.targetPathdir', 'event.targetPathdir', 1 },
{ '%^o%.targetPath', 'event.targetPath', 1 },
{ '%^d%.pathname', 'event2.pathname', 2 },
{ '%^d%.path', 'event2.path', 2 },
{ '%^d%.sourcePathname', 'event2.sourcePathname', 2 },
{ '%^d%.sourcePathdir', 'event2.sourcePathdir', 2 },
{ '%^d%.sourcePath', 'event2.sourcePath', 2 },
{ '%^d%.targetPathname', 'event2.targetPathname', 2 },
{ '%^d%.targetPathdir', 'event2.targetPathdir', 2 },
{ '%^d%.targetPath', 'event2.targetPath', 2 },
2010-11-13 13:13:51 +00:00
}
2012-01-27 11:08:10 +00:00
--
2012-10-03 07:23:18 +00:00
-- Splits a user string into its arguments.
-- Returns a table of arguments
2010-11-13 13:13:51 +00:00
--
2012-10-03 07:23:18 +00:00
local function splitStr(
str -- a string where parameters are seperated by spaces.
)
local args = { }
2016-12-13 13:41:35 +00:00
while str ~= ''
do
2010-11-13 13:13:51 +00:00
-- break where argument stops
local bp = #str
2012-10-03 07:23:18 +00:00
2010-11-13 13:13:51 +00:00
-- in a quote
local inQuote = false
2012-10-03 07:23:18 +00:00
2010-11-13 13:13:51 +00:00
-- tests characters to be space and not within quotes
2016-12-13 13:41:35 +00:00
for i = 1, #str
do
2012-10-03 07:23:18 +00:00
local c = string.sub( str, i, i )
2016-12-13 13:41:35 +00:00
if c == '"'
then
2010-11-13 13:13:51 +00:00
inQuote = not inQuote
2016-12-13 13:41:35 +00:00
elseif c == ' ' and not inQuote
then
2010-11-13 13:13:51 +00:00
bp = i - 1
2016-12-13 13:41:35 +00:00
2010-11-13 13:13:51 +00:00
break
end
end
2012-10-03 07:23:18 +00:00
local arg = string.sub( str, 1, bp )
arg = string.gsub( arg, '"', '\\"' )
table.insert( args, arg )
str = string.sub( str, bp + 1, -1 )
str = string.match( str, '^%s*(.-)%s*$' )
2010-11-13 13:13:51 +00:00
end
2012-10-03 07:23:18 +00:00
2010-11-13 13:13:51 +00:00
return args
end
2012-10-03 07:23:18 +00:00
2010-11-13 13:13:51 +00:00
--
2012-10-03 07:23:18 +00:00
-- Translates a call to a binary to a lua function.
-- TODO this has a little too blocking.
2010-11-13 13:13:51 +00:00
--
2016-12-13 13:41:35 +00:00
local function translateBinary
(
str
)
2010-11-13 13:13:51 +00:00
-- splits the string
2012-10-03 07:23:18 +00:00
local args = splitStr( str )
2012-01-27 11:08:10 +00:00
2010-11-13 13:13:51 +00:00
-- true if there is a second event
local haveEvent2 = false
2012-01-27 11:08:10 +00:00
2018-03-01 14:08:26 +00:00
for ia, iv in ipairs( args )
do
-- a list of arguments this arg is being split into
2012-10-03 07:23:18 +00:00
local a = { { true, iv } }
2010-11-13 13:13:51 +00:00
-- goes through all translates
2018-03-01 14:08:26 +00:00
for _, v in ipairs( transVars )
do
2012-01-27 11:08:10 +00:00
local ai = 1
2018-03-01 14:08:26 +00:00
while ai <= #a
do
if a[ ai ][ 1 ]
then
2012-01-27 11:08:10 +00:00
local pre, post =
string.match( a[ ai ][ 2 ], '(.*)'..v[1]..'(.*)' )
2012-10-03 07:23:18 +00:00
2018-03-01 14:08:26 +00:00
if pre
then
if v[3] > 1
then
haveEvent2 = true
end
2012-10-03 07:23:18 +00:00
2018-03-01 14:08:26 +00:00
if pre ~= ''
then
2012-10-03 07:23:18 +00:00
table.insert( a, ai, { true, pre } )
2010-11-13 13:13:51 +00:00
ai = ai + 1
end
2012-10-03 07:23:18 +00:00
a[ ai ] = { false, v[ 2 ] }
2018-03-01 14:08:26 +00:00
if post ~= ''
then
2012-10-03 07:23:18 +00:00
table.insert( a, ai + 1, { true, post } )
end
2010-11-13 13:13:51 +00:00
end
end
ai = ai + 1
end
end
-- concats the argument pieces into a string.
2012-01-27 14:19:17 +00:00
local as = ''
2010-11-13 13:13:51 +00:00
local first = true
2012-10-03 07:23:18 +00:00
2018-02-27 16:14:36 +00:00
for _, v in ipairs( a )
do
if not first then as = as..' .. ' end
2012-10-03 07:23:18 +00:00
2018-02-27 16:14:36 +00:00
if v[ 1 ]
then
2012-10-03 07:23:18 +00:00
as = as .. '"' .. v[ 2 ] .. '"'
2012-01-27 11:08:10 +00:00
else
2012-10-03 07:23:18 +00:00
as = as .. v[ 2 ]
2010-11-13 13:13:51 +00:00
end
2012-10-03 07:23:18 +00:00
2010-11-13 13:13:51 +00:00
first = false
end
2012-10-03 07:23:18 +00:00
args[ ia ] = as
2010-11-13 13:13:51 +00:00
end
local ft
2018-02-27 16:14:36 +00:00
2016-12-13 13:41:35 +00:00
if not haveEvent2
then
ft = 'function( event )\n'
2010-11-13 13:13:51 +00:00
else
2016-12-13 13:41:35 +00:00
ft = 'function( event, event2 )\n'
2010-11-13 13:13:51 +00:00
end
2012-10-03 07:23:18 +00:00
ft = ft ..
" log('Normal', 'Event ', event.etype, \n" ..
" ' spawns action \"".. str.."\"')\n" ..
2016-12-13 13:41:35 +00:00
" spawn( event"
2012-10-03 07:23:18 +00:00
2016-12-13 13:41:35 +00:00
for _, v in ipairs( args )
do
2012-10-03 07:23:18 +00:00
ft = ft .. ',\n ' .. v
2010-11-13 13:13:51 +00:00
end
2012-10-03 07:23:18 +00:00
ft = ft .. ')\nend'
2010-11-13 13:13:51 +00:00
return ft
2012-10-03 07:23:18 +00:00
2010-11-13 13:13:51 +00:00
end
2012-10-03 07:23:18 +00:00
--
2010-11-13 13:13:51 +00:00
-- Translates a call using a shell to a lua function
--
2016-12-13 13:41:35 +00:00
local function translateShell
(
str
)
2010-11-13 13:13:51 +00:00
local argn = 1
2016-12-13 13:41:35 +00:00
2012-10-03 07:23:18 +00:00
local args = { }
2016-12-13 13:41:35 +00:00
2010-11-13 13:13:51 +00:00
local cmd = str
2016-12-13 13:41:35 +00:00
2010-11-13 13:44:51 +00:00
local lc = str
2012-10-03 07:23:18 +00:00
2010-11-13 13:13:51 +00:00
-- true if there is a second event
local haveEvent2 = false
2016-12-13 13:41:35 +00:00
for _, v in ipairs( transVars )
do
2010-11-13 13:13:51 +00:00
local occur = false
2012-10-03 07:23:18 +00:00
cmd = string.gsub(
cmd,
v[ 1 ],
2018-03-01 14:08:26 +00:00
function
( )
2010-11-13 13:44:51 +00:00
occur = true
2012-10-03 07:23:18 +00:00
return '"$' .. argn .. '"'
end
)
2018-03-01 14:08:26 +00:00
lc = string.gsub( lc, v[1], ']]..' .. v[2] .. '..[[' )
2012-10-03 07:23:18 +00:00
2016-12-13 13:41:35 +00:00
if occur
then
2010-11-13 13:13:51 +00:00
argn = argn + 1
2016-12-13 13:41:35 +00:00
2012-10-03 07:23:18 +00:00
table.insert( args, v[ 2 ] )
2016-12-13 13:41:35 +00:00
if v[ 3 ] > 1
then
2010-11-13 13:13:51 +00:00
haveEvent2 = true
end
end
2012-10-03 07:23:18 +00:00
2010-11-13 13:13:51 +00:00
end
2012-10-03 07:23:18 +00:00
2010-11-13 13:13:51 +00:00
local ft
2016-12-13 13:41:35 +00:00
if not haveEvent2
then
ft = 'function( event )\n'
2010-11-13 13:13:51 +00:00
else
2016-12-13 13:41:35 +00:00
ft = 'function( event, event2 )\n'
2010-11-13 13:13:51 +00:00
end
2012-10-03 07:23:18 +00:00
2012-01-27 11:08:10 +00:00
-- TODO do array joining instead
2012-01-27 14:19:17 +00:00
ft = ft..
2012-01-27 14:52:42 +00:00
" log('Normal', 'Event ',event.etype,\n"..
2012-03-22 18:52:14 +00:00
" [[ spawns shell \""..lc.."\"]])\n"..
" spawnShell(event, [["..cmd.."]]"
2012-10-03 07:23:18 +00:00
2016-12-13 13:41:35 +00:00
for _, v in ipairs( args )
do
2012-01-27 14:19:17 +00:00
ft = ft..',\n '..v
2010-11-13 13:13:51 +00:00
end
2012-10-03 07:23:18 +00:00
ft = ft .. ')\nend'
2010-11-13 13:13:51 +00:00
return ft
2012-10-03 07:23:18 +00:00
2010-11-13 13:13:51 +00:00
end
2012-10-03 07:23:18 +00:00
--
-- Writes a lua function for a layer 3 user script.
--
2016-12-13 13:41:35 +00:00
local function translate
(
str
)
-- trims spaces
2012-10-03 07:23:18 +00:00
str = string.match( str, '^%s*(.-)%s*$' )
2010-11-13 13:13:51 +00:00
local ft
2018-03-01 14:08:26 +00:00
2016-12-13 13:41:35 +00:00
if string.byte( str, 1, 1 ) == 47
then
-- starts with /
2012-10-03 07:23:18 +00:00
ft = translateBinary( str )
2016-12-13 13:41:35 +00:00
elseif string.byte( str, 1, 1 ) == 94
then
-- starts with ^
2012-10-03 07:23:18 +00:00
ft = translateShell( str:sub( 2, -1 ) )
2010-11-13 13:13:51 +00:00
else
2012-10-03 07:23:18 +00:00
ft = translateShell( str )
2010-11-13 13:13:51 +00:00
end
2012-10-03 07:23:18 +00:00
2018-03-01 14:08:26 +00:00
log( 'FWrite', 'translated "', str, '" to \n', ft )
2012-10-03 07:23:18 +00:00
2010-11-13 13:13:51 +00:00
return ft
end
2012-10-03 07:23:18 +00:00
2010-11-13 13:13:51 +00:00
--
2012-10-03 07:23:18 +00:00
-- Public interface.
--
return { translate = translate }
end )( )
2010-11-13 13:13:51 +00:00
2010-10-28 17:56:33 +00:00
--
2016-12-13 13:41:35 +00:00
-- Writes a status report file at most every 'statusintervall' seconds.
2010-11-06 10:10:57 +00:00
--
2016-12-13 13:41:35 +00:00
local StatusFile = ( function
( )
2012-10-03 07:23:18 +00:00
--
2010-11-06 10:10:57 +00:00
-- Timestamp when the status file has been written.
2012-10-03 07:23:18 +00:00
--
2010-11-06 10:10:57 +00:00
local lastWritten = false
2012-10-03 07:23:18 +00:00
--
-- Timestamp when a status file should be written.
--
2010-11-06 10:10:57 +00:00
local alarm = false
2012-10-03 07:23:18 +00:00
--
-- Returns the alarm when the status file should be written-
2010-11-06 10:10:57 +00:00
--
2016-12-13 13:41:35 +00:00
local function getAlarm
( )
2010-11-06 10:10:57 +00:00
return alarm
end
2012-10-03 07:23:18 +00:00
--
2010-11-06 10:10:57 +00:00
-- Called to check if to write a status file.
--
2016-12-13 13:41:35 +00:00
local function write
(
timestamp
)
2016-12-14 13:25:20 +00:00
log( 'Function', 'write( ', timestamp, ' )' )
2012-10-03 07:23:18 +00:00
2012-10-08 07:10:03 +00:00
--
-- takes care not to write too often
2012-10-08 07:10:03 +00:00
--
if uSettings.statusInterval > 0
then
2012-10-03 07:23:18 +00:00
-- already waiting?
if alarm and timestamp < alarm
then
2018-03-01 14:08:26 +00:00
log( 'Statusfile', 'waiting(', timestamp, ' < ', alarm, ')' )
2010-11-06 10:10:57 +00:00
return
end
2012-10-03 07:23:18 +00:00
2010-11-06 10:33:26 +00:00
-- determines when a next write will be possible
if not alarm
then
2018-03-01 14:08:26 +00:00
local nextWrite = lastWritten and timestamp + uSettings.statusInterval
2012-10-03 07:23:18 +00:00
if nextWrite and timestamp < nextWrite
then
2018-03-01 14:08:26 +00:00
log( 'Statusfile', 'setting alarm: ', nextWrite )
2010-11-06 10:10:57 +00:00
alarm = nextWrite
2012-10-03 07:23:18 +00:00
2010-11-06 10:10:57 +00:00
return
end
end
2012-10-03 07:23:18 +00:00
2010-11-29 20:32:54 +00:00
lastWritten = timestamp
2010-11-06 10:10:57 +00:00
alarm = false
end
2012-10-03 07:23:18 +00:00
log( 'Statusfile', 'writing now' )
2012-10-08 07:10:03 +00:00
local f, err = io.open( uSettings.statusFile, 'w' )
2012-10-03 07:23:18 +00:00
2016-12-13 13:41:35 +00:00
if not f
then
2012-10-03 07:23:18 +00:00
log(
'Error',
'Cannot open status file "' ..
2012-10-08 07:10:03 +00:00
uSettings.statusFile ..
2012-10-03 07:23:18 +00:00
'" :' ..
err
)
2010-11-06 10:10:57 +00:00
return
end
2012-10-03 07:23:18 +00:00
f:write( 'Lsyncd status report at ', os.date( ), '\n\n' )
2018-03-15 16:49:12 +00:00
for i, s in SyncMaster.iwalk( )
2016-12-13 13:41:35 +00:00
do
2012-10-03 07:23:18 +00:00
s:statusReport( f )
2016-12-13 13:41:35 +00:00
2012-10-03 07:23:18 +00:00
f:write( '\n' )
2010-11-12 10:07:58 +00:00
end
2012-01-27 11:08:10 +00:00
2012-10-03 07:23:18 +00:00
Inotify.statusReport( f )
2016-12-13 13:41:35 +00:00
2012-10-03 07:23:18 +00:00
f:close( )
2010-11-05 18:04:29 +00:00
end
2010-11-06 10:10:57 +00:00
2012-10-03 07:23:18 +00:00
--
-- Public interface
--
return { write = write, getAlarm = getAlarm }
2012-10-03 07:23:18 +00:00
end )( )
2010-11-30 23:14:17 +00:00
--
2012-10-03 07:23:18 +00:00
-- Lets userscripts make their own alarms.
--
2016-12-13 13:41:35 +00:00
local UserAlarms = ( function
( )
2012-10-03 07:23:18 +00:00
local alarms = { }
--
-- Calls the user function at timestamp.
2010-11-30 23:14:17 +00:00
--
2016-12-13 13:41:35 +00:00
local function alarm
(
timestamp,
func,
extra
)
2012-01-27 11:08:10 +00:00
local idx
2016-11-25 13:55:59 +00:00
for k, v in ipairs( alarms )
do
if timestamp < v.timestamp
then
2010-11-30 23:14:17 +00:00
idx = k
2016-11-25 13:55:59 +00:00
2010-11-30 23:14:17 +00:00
break
end
end
2012-10-03 07:23:18 +00:00
2018-03-01 14:08:26 +00:00
local a =
{
2012-10-03 07:23:18 +00:00
timestamp = timestamp,
func = func,
extra = extra
}
2016-11-25 13:55:59 +00:00
if idx
then
2012-10-03 07:23:18 +00:00
table.insert( alarms, idx, a )
2010-11-30 23:14:17 +00:00
else
2012-10-03 07:23:18 +00:00
table.insert( alarms, a )
2010-11-30 23:14:17 +00:00
end
end
2012-10-03 07:23:18 +00:00
2010-11-30 23:14:17 +00:00
--
2012-10-03 07:23:18 +00:00
-- Retrieves the soonest alarm.
--
2016-12-13 13:41:35 +00:00
local function getAlarm
( )
2016-11-25 13:55:59 +00:00
if #alarms == 0
then
2012-01-27 11:08:10 +00:00
return false
2010-11-30 23:14:17 +00:00
else
return alarms[1].timestamp
end
end
2012-10-03 07:23:18 +00:00
--
-- Calls user alarms.
2010-11-30 23:14:17 +00:00
--
2018-03-01 14:08:26 +00:00
local function invoke
(
timestamp
)
while #alarms > 0
and alarms[ 1 ].timestamp <= timestamp
2012-10-03 07:23:18 +00:00
do
alarms[ 1 ].func( alarms[ 1 ].timestamp, alarms[ 1 ].extra )
table.remove( alarms, 1 )
2010-11-30 23:14:17 +00:00
end
end
2012-10-03 07:23:18 +00:00
--
-- Public interface
--
return {
alarm = alarm,
getAlarm = getAlarm,
invoke = invoke
}
end )( )
2010-11-30 23:14:17 +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
--
2012-01-30 14:01:18 +00:00
local 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-15 12:43:02 +00:00
core.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(
'Error',
'Backtrace ',
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
--
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.
--
function mci.configure( args, monitors )
2010-11-28 09:37:43 +00:00
Monitors.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
local ft = functionWriter.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.
2010-11-10 11:23:26 +00:00
-- Returns true if 'String' starts with 'Start'
--
2016-12-13 13:41:35 +00:00
function string.starts
(
String,
Start
)
2018-03-15 16:49:12 +00:00
return string.sub( String, 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.
2010-11-10 11:23:26 +00:00
-- Returns true if 'String' ends with 'End'
--
2016-12-13 13:41:35 +00:00
function string.ends
(
String,
End
)
2012-10-03 15:37:49 +00:00
return End == '' or string.sub( String, -#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