2012-10-07 17:48:23 +00:00
|
|
|
/*
|
2018-04-13 20:55:04 +00:00
|
|
|
| main.c from Lsyncd -- the Live (Mirror) Syncing Demon
|
|
|
|
|
|
|
|
|
| Entry and main loop
|
2018-03-27 06:54:14 +00:00
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
| License: GPLv2 (see COPYING) or any later version
|
|
|
|
| Authors: Axel Kittenberger <axkibe@gmail.com>
|
|
|
|
*/
|
2018-04-13 20:55:04 +00:00
|
|
|
#include "feature.h"
|
2010-11-22 21:17:08 +00:00
|
|
|
|
2018-04-13 20:55:04 +00:00
|
|
|
// FIXME remove unneeded headers
|
2010-10-16 10:26:48 +00:00
|
|
|
|
2011-03-30 14:08:01 +00:00
|
|
|
#include <sys/select.h>
|
2010-10-16 18:21:01 +00:00
|
|
|
#include <sys/stat.h>
|
2010-10-19 10:12:11 +00:00
|
|
|
#include <sys/times.h>
|
2010-10-18 12:23:46 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2010-10-16 18:21:01 +00:00
|
|
|
#include <dirent.h>
|
2010-10-16 10:26:48 +00:00
|
|
|
#include <errno.h>
|
2010-10-28 17:56:33 +00:00
|
|
|
#include <fcntl.h>
|
2010-10-16 18:21:01 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdbool.h>
|
2011-08-16 14:25:30 +00:00
|
|
|
#include <stddef.h>
|
2010-10-16 18:21:01 +00:00
|
|
|
#include <stdlib.h>
|
2010-10-14 13:52:01 +00:00
|
|
|
#include <stdio.h>
|
2010-10-16 10:26:48 +00:00
|
|
|
#include <string.h>
|
2011-07-19 13:29:19 +00:00
|
|
|
#include <strings.h>
|
2010-10-20 15:34:01 +00:00
|
|
|
#include <syslog.h>
|
2010-11-04 13:43:57 +00:00
|
|
|
#include <math.h>
|
2010-10-20 15:34:01 +00:00
|
|
|
#include <time.h>
|
2010-10-16 10:26:48 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2011-02-25 07:57:11 +00:00
|
|
|
#define LUA_USE_APICHECK 1
|
2010-10-14 13:52:01 +00:00
|
|
|
#include <lua.h>
|
|
|
|
#include <lualib.h>
|
|
|
|
#include <lauxlib.h>
|
|
|
|
|
2018-03-23 08:34:33 +00:00
|
|
|
#include "log.h"
|
2018-04-11 07:03:53 +00:00
|
|
|
#include "mci.h"
|
2018-03-27 07:09:07 +00:00
|
|
|
#include "mem.h"
|
|
|
|
#include "util.h"
|
2018-03-26 17:05:00 +00:00
|
|
|
#include "pipe.h"
|
2018-03-28 07:17:49 +00:00
|
|
|
#include "observe.h"
|
2018-03-30 07:40:09 +00:00
|
|
|
#include "time.h"
|
2018-04-01 17:35:35 +00:00
|
|
|
#include "signal.h"
|
2018-04-03 06:54:07 +00:00
|
|
|
#include "userobs.h"
|
2018-03-22 08:14:13 +00:00
|
|
|
|
2018-03-27 06:54:14 +00:00
|
|
|
#ifdef WITH_INOTIFY
|
|
|
|
#include "inotify.h"
|
|
|
|
#endif
|
|
|
|
|
2012-02-15 19:00:28 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*
|
|
|
|
| Makes sure there is one file system monitor.
|
|
|
|
*/
|
2014-04-29 14:11:27 +00:00
|
|
|
#ifndef WITH_INOTIFY
|
|
|
|
# error "needing at least one notification system. please rerun cmake"
|
2010-11-28 08:13:05 +00:00
|
|
|
#endif
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*
|
|
|
|
| All monitors supported by this Lsyncd.
|
|
|
|
*/
|
2018-04-11 07:03:53 +00:00
|
|
|
char *monitors[] = {
|
2012-02-15 19:00:28 +00:00
|
|
|
|
2014-04-29 14:11:27 +00:00
|
|
|
#ifdef WITH_INOTIFY
|
2010-11-28 08:13:05 +00:00
|
|
|
"inotify",
|
|
|
|
#endif
|
2012-02-15 19:00:28 +00:00
|
|
|
|
2010-11-28 08:13:05 +00:00
|
|
|
NULL,
|
|
|
|
};
|
2010-11-26 16:17:36 +00:00
|
|
|
|
2016-05-03 08:45:22 +00:00
|
|
|
|
2018-06-06 07:24:13 +00:00
|
|
|
/**
|
|
|
|
| Set to true to soft reset at earliest possilibity.
|
|
|
|
*/
|
|
|
|
bool softreset = false;
|
|
|
|
|
|
|
|
|
2010-10-27 19:34:56 +00:00
|
|
|
/**
|
2012-10-07 17:48:23 +00:00
|
|
|
| Configuration parameters that matter to the core
|
|
|
|
*/
|
2010-11-22 20:09:52 +00:00
|
|
|
struct settings settings = {
|
2012-10-07 17:48:23 +00:00
|
|
|
.log_file = NULL,
|
|
|
|
.log_syslog = false,
|
|
|
|
.log_ident = NULL,
|
2011-03-01 14:57:26 +00:00
|
|
|
.log_facility = LOG_USER,
|
2018-03-12 11:51:52 +00:00
|
|
|
.log_level = LOG_NOTICE
|
2010-10-27 19:34:56 +00:00
|
|
|
};
|
2010-10-20 15:34:01 +00:00
|
|
|
|
2016-05-03 08:45:22 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*
|
2018-03-12 11:51:52 +00:00
|
|
|
| True if stdout and stderr are detected to
|
|
|
|
| be directed to /dev/null.
|
2012-10-07 17:48:23 +00:00
|
|
|
*/
|
2018-03-23 08:34:33 +00:00
|
|
|
bool no_output = false;
|
2012-02-15 19:00:28 +00:00
|
|
|
|
2016-05-03 08:45:22 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*
|
|
|
|
| The config file loaded by Lsyncd.
|
|
|
|
*/
|
2012-02-15 13:42:24 +00:00
|
|
|
char * lsyncd_config_file = NULL;
|
2010-10-27 19:34:56 +00:00
|
|
|
|
2016-05-03 08:45:22 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*
|
|
|
|
| False after first time Lsyncd started up.
|
|
|
|
|
|
|
|
|
| Configuration error messages are thus written to
|
|
|
|
| stdout/stderr only on first start.
|
|
|
|
*/
|
2018-03-23 08:34:33 +00:00
|
|
|
bool first_time = true;
|
2010-10-27 19:34:56 +00:00
|
|
|
|
2016-05-03 08:45:22 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*
|
|
|
|
| Normal operation happens in here.
|
|
|
|
*/
|
2010-11-10 15:57:37 +00:00
|
|
|
static void
|
2018-04-11 07:03:53 +00:00
|
|
|
masterloop(
|
|
|
|
lua_State *L
|
|
|
|
)
|
2010-10-19 10:12:11 +00:00
|
|
|
{
|
2018-06-06 07:24:13 +00:00
|
|
|
while( !softreset )
|
2012-10-07 17:48:23 +00:00
|
|
|
{
|
2010-10-24 16:41:58 +00:00
|
|
|
bool have_alarm;
|
2012-02-15 11:15:32 +00:00
|
|
|
bool force_alarm = false;
|
2018-03-30 07:36:26 +00:00
|
|
|
clock_t cnow = now( );
|
2012-02-15 11:15:32 +00:00
|
|
|
clock_t alarm_time = 0;
|
2010-10-19 10:12:11 +00:00
|
|
|
|
2012-03-16 15:05:16 +00:00
|
|
|
// memory usage debugging
|
2012-10-07 17:48:23 +00:00
|
|
|
// lua_gc( L, LUA_GCCOLLECT, 0 );
|
|
|
|
// printf(
|
|
|
|
// "gccount: %d\n",
|
|
|
|
// lua_gc( L, LUA_GCCOUNT, 0 ) * 1024 + lua_gc( L, LUA_GCCOUNTB, 0 ) );
|
|
|
|
|
|
|
|
//
|
2018-03-14 08:57:49 +00:00
|
|
|
// queries the mantle about the soonest alarm
|
2012-10-07 17:48:23 +00:00
|
|
|
//
|
2018-03-15 08:27:14 +00:00
|
|
|
load_mci( L, "getAlarm" );
|
2014-02-28 09:15:48 +00:00
|
|
|
|
2018-03-13 11:02:34 +00:00
|
|
|
if( lua_pcall( L, 0, 1, -2 ) ) exit( -1 );
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
if( lua_type( L, -1 ) == LUA_TBOOLEAN)
|
|
|
|
{
|
2014-02-28 09:15:48 +00:00
|
|
|
have_alarm = false;
|
2018-03-27 15:43:26 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
force_alarm = lua_toboolean( L, -1 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-11-08 12:14:10 +00:00
|
|
|
have_alarm = true;
|
2018-03-27 15:43:26 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
alarm_time = *( ( clock_t * ) luaL_checkudata( L, -1, "Lsyncd.jiffies" ) );
|
2010-11-05 18:20:33 +00:00
|
|
|
}
|
2014-02-28 09:15:48 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_pop( L, 2 );
|
2010-10-19 10:12:11 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
if(
|
|
|
|
force_alarm ||
|
2018-03-30 07:36:26 +00:00
|
|
|
( have_alarm && time_before_eq( alarm_time, cnow ) )
|
2012-10-07 17:48:23 +00:00
|
|
|
)
|
|
|
|
{
|
2012-02-15 11:15:32 +00:00
|
|
|
// there is a delay that wants to be handled already thus instead
|
|
|
|
// of reading/writing from observances it jumps directly to
|
|
|
|
// handling
|
2010-11-26 16:52:24 +00:00
|
|
|
|
2018-03-26 16:09:22 +00:00
|
|
|
// TODO: Actually it might be smarter to handle observances
|
|
|
|
// anyway. since event queues might overflow.
|
2012-10-07 17:48:23 +00:00
|
|
|
logstring( "Masterloop", "immediately handling delays." );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// uses select( ) to determine what happens next:
|
|
|
|
// a) a new event on an observance
|
|
|
|
// b) an alarm on timeout
|
|
|
|
// c) the return of a child process
|
2010-10-25 08:42:27 +00:00
|
|
|
struct timespec tv;
|
2010-10-19 10:12:11 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
if( have_alarm )
|
|
|
|
{
|
2018-03-26 16:09:22 +00:00
|
|
|
// TODO use trunc instead of long conversions
|
2018-03-30 07:36:26 +00:00
|
|
|
double d = ( ( double )( alarm_time - cnow ) ) / clocks_per_sec;
|
2010-11-04 13:43:57 +00:00
|
|
|
tv.tv_sec = d;
|
2018-03-27 15:43:26 +00:00
|
|
|
tv.tv_nsec = ( ( d - ( long ) d ) ) * 1000000000.0;
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
printlogf(
|
|
|
|
L, "Masterloop",
|
|
|
|
"going into select ( timeout %f seconds )",
|
|
|
|
d
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-03-13 09:01:36 +00:00
|
|
|
logstring( "Masterloop", "going into select ( no timeout )" );
|
2010-10-19 10:12:11 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
2018-03-28 06:59:10 +00:00
|
|
|
observe_select( L, have_alarm ? &tv : NULL );
|
2012-02-15 11:15:32 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 06:59:10 +00:00
|
|
|
// collects possibly zombified child processes
|
2012-10-07 17:48:23 +00:00
|
|
|
while( 1 )
|
|
|
|
{
|
2010-10-23 19:19:46 +00:00
|
|
|
int status;
|
2012-10-07 17:48:23 +00:00
|
|
|
pid_t pid = waitpid( 0, &status, WNOHANG );
|
|
|
|
|
2018-03-13 09:01:36 +00:00
|
|
|
// no more zombies
|
2018-03-13 11:02:34 +00:00
|
|
|
if( pid <= 0 ) break;
|
2012-10-07 17:48:23 +00:00
|
|
|
|
2018-03-14 08:57:49 +00:00
|
|
|
// calls the mantle to handle the collection
|
2018-03-15 08:27:14 +00:00
|
|
|
load_mci( L, "collectProcess" );
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_pushinteger( L, pid );
|
|
|
|
lua_pushinteger( L, WEXITSTATUS( status ) );
|
|
|
|
|
2018-03-13 11:02:34 +00:00
|
|
|
if( lua_pcall( L, 2, 0, -4 ) ) exit(-1);
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
lua_pop( L, 1 );
|
2012-02-15 11:15:32 +00:00
|
|
|
}
|
2010-10-23 19:19:46 +00:00
|
|
|
|
2018-03-14 08:57:49 +00:00
|
|
|
// lets the mantle do stuff every cycle,
|
2012-02-15 11:15:32 +00:00
|
|
|
// like starting new processes, writing the statusfile etc.
|
2018-03-15 08:27:14 +00:00
|
|
|
load_mci( L, "cycle" );
|
2018-03-30 07:36:26 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
l_now( L );
|
2018-03-30 07:36:26 +00:00
|
|
|
|
2018-03-13 09:01:36 +00:00
|
|
|
if( lua_pcall( L, 1, 1, -3 ) ) exit( -1 );
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
if( !lua_toboolean( L, -1 ) )
|
|
|
|
{
|
2012-02-15 11:15:32 +00:00
|
|
|
// cycle told core to break mainloop
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_pop( L, 2 );
|
2010-11-14 09:11:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-03-13 11:02:34 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_pop( L, 2 );
|
|
|
|
|
|
|
|
if( lua_gettop( L ) )
|
|
|
|
{
|
2018-03-13 11:02:34 +00:00
|
|
|
logstring( "Error", "internal, stack is dirty." );
|
2012-10-07 17:48:23 +00:00
|
|
|
l_stackdump( L );
|
|
|
|
exit( -1 );
|
2010-12-01 13:17:04 +00:00
|
|
|
}
|
2010-10-19 10:12:11 +00:00
|
|
|
}
|
2018-06-06 07:24:13 +00:00
|
|
|
|
|
|
|
logstring( "Normal", "--- Soft Reset ---" );
|
|
|
|
softreset = false;
|
2010-10-19 10:12:11 +00:00
|
|
|
}
|
|
|
|
|
2016-05-03 08:45:22 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*
|
|
|
|
| The effective main for one run.
|
2014-02-28 09:15:48 +00:00
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
| HUP signals may cause several runs of the one main.
|
|
|
|
*/
|
2010-10-17 15:24:55 +00:00
|
|
|
int
|
2012-10-07 17:48:23 +00:00
|
|
|
main1( int argc, char *argv[] )
|
2010-10-14 13:52:01 +00:00
|
|
|
{
|
2012-02-15 11:15:32 +00:00
|
|
|
// the Lua interpreter
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_State * L;
|
2010-10-27 19:34:56 +00:00
|
|
|
|
2010-11-03 21:02:14 +00:00
|
|
|
int argp = 1;
|
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// load Lua
|
2012-10-07 17:48:23 +00:00
|
|
|
L = luaL_newstate( );
|
2016-12-02 15:24:07 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
luaL_openlibs( L );
|
2018-03-14 08:57:49 +00:00
|
|
|
|
2010-11-11 19:52:20 +00:00
|
|
|
{
|
2012-02-15 11:15:32 +00:00
|
|
|
// checks the lua version
|
2012-10-07 17:48:23 +00:00
|
|
|
const char * version;
|
2010-11-11 19:52:20 +00:00
|
|
|
int major, minor;
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_getglobal( L, "_VERSION" );
|
|
|
|
version = luaL_checkstring( L, -1 );
|
|
|
|
|
2018-03-13 09:01:36 +00:00
|
|
|
if( sscanf( version, "Lua %d.%d", &major, &minor ) != 2 )
|
2012-10-07 17:48:23 +00:00
|
|
|
{
|
2018-03-13 09:01:36 +00:00
|
|
|
fprintf( stderr, "cannot parse lua library version!\n" );
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
exit (-1 );
|
2010-11-11 19:52:20 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
2018-03-13 09:01:36 +00:00
|
|
|
if( major < 5 || ( major == 5 && minor < 2 ) )
|
|
|
|
{
|
|
|
|
fprintf( stderr, "Lua library is too old. Needs 5.2 at least" );
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
exit( -1 );
|
2010-11-11 19:52:20 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
lua_pop( L, 1 );
|
2010-11-11 19:52:20 +00:00
|
|
|
}
|
2010-11-03 21:02:14 +00:00
|
|
|
|
2010-11-03 16:04:11 +00:00
|
|
|
{
|
2012-10-07 17:48:23 +00:00
|
|
|
// logging is prepared quite early
|
2010-11-03 21:02:14 +00:00
|
|
|
int i = 1;
|
2018-03-13 11:02:34 +00:00
|
|
|
add_logcat( "Normal", LOG_NOTICE );
|
|
|
|
add_logcat( "Warn", LOG_WARNING );
|
|
|
|
add_logcat( "Error", LOG_ERR );
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
while( i < argc )
|
|
|
|
{
|
2018-03-13 09:01:36 +00:00
|
|
|
if( strcmp( argv[ i ], "-log" ) && strcmp( argv[ i ], "--log" ) )
|
2012-10-07 17:48:23 +00:00
|
|
|
{
|
|
|
|
// arg is neither -log or --log
|
|
|
|
i++;
|
|
|
|
continue;
|
2010-11-03 16:04:11 +00:00
|
|
|
}
|
2012-02-15 12:47:58 +00:00
|
|
|
|
2018-03-13 09:01:36 +00:00
|
|
|
// -(-)log was last argument
|
|
|
|
if( ++i >= argc ) break;
|
2012-02-15 12:47:58 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
if( !add_logcat( argv[ i ], LOG_NOTICE ) )
|
|
|
|
{
|
|
|
|
printlogf(
|
|
|
|
L, "Error",
|
|
|
|
"'%s' is not a valid logging category",
|
|
|
|
argv[ i ]
|
|
|
|
);
|
2018-03-13 09:01:36 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
exit( -1 );
|
2010-11-03 16:04:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-11-03 14:54:33 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
// registers Lsycnd's core library
|
2018-03-13 09:01:36 +00:00
|
|
|
register_core( L );
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
if( check_logcat( "Debug" ) <= settings.log_level )
|
|
|
|
{
|
2012-02-15 12:47:58 +00:00
|
|
|
// printlogf doesnt support %ld :-(
|
2018-03-12 16:03:56 +00:00
|
|
|
printf( "kernels clocks_per_sec=%ld\n", clocks_per_sec );
|
2010-11-04 13:43:57 +00:00
|
|
|
}
|
|
|
|
|
2018-04-22 15:52:35 +00:00
|
|
|
signal_init( );
|
|
|
|
|
|
|
|
#ifdef WITH_INOTIFY
|
|
|
|
open_inotify( L );
|
|
|
|
#endif
|
|
|
|
|
2018-04-11 07:03:53 +00:00
|
|
|
mci_load_mantle( L );
|
2018-04-22 15:52:35 +00:00
|
|
|
|
2018-04-11 07:03:53 +00:00
|
|
|
mci_load_default( L );
|
2018-03-15 12:43:02 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
// checks if there is a "-help" or "--help"
|
2010-10-27 09:06:13 +00:00
|
|
|
{
|
2018-04-22 15:52:35 +00:00
|
|
|
// FIXME this should be done in mantle
|
2010-10-27 09:06:13 +00:00
|
|
|
int i;
|
2012-10-07 17:48:23 +00:00
|
|
|
for( i = argp; i < argc; i++ )
|
|
|
|
{
|
2018-03-12 12:20:26 +00:00
|
|
|
if ( !strcmp( argv[ i ], "-help" ) || !strcmp( argv[ i ], "--help" ) )
|
2012-10-07 17:48:23 +00:00
|
|
|
{
|
2018-03-15 08:27:14 +00:00
|
|
|
load_mci( L, "help" );
|
2012-10-07 17:48:23 +00:00
|
|
|
|
2018-03-12 12:20:26 +00:00
|
|
|
if( lua_pcall( L, 0, 0, -2 ) ) exit( -1 );
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
lua_pop( L, 1 );
|
2018-03-12 12:20:26 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
exit( 0 );
|
2010-10-27 09:06:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
// starts the option parser in Lua script
|
2010-11-03 21:02:14 +00:00
|
|
|
{
|
|
|
|
int idx = 1;
|
|
|
|
const char *s;
|
2012-10-07 17:48:23 +00:00
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// creates a table with all remaining argv option arguments
|
2018-03-15 08:27:14 +00:00
|
|
|
load_mci( L, "configure" );
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_newtable( L );
|
|
|
|
|
|
|
|
while( argp < argc )
|
|
|
|
{
|
2018-04-21 12:23:21 +00:00
|
|
|
lua_pushnumber( L, idx++ );
|
2018-04-22 15:52:35 +00:00
|
|
|
lua_pushstring( L, argv[ argp++ ] );
|
2018-03-12 12:20:26 +00:00
|
|
|
lua_settable( L, -3 );
|
2010-11-03 21:02:14 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// creates a table with the cores event monitor interfaces
|
2010-11-28 09:37:43 +00:00
|
|
|
idx = 0;
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_newtable( L );
|
|
|
|
|
|
|
|
while( monitors[ idx ] )
|
|
|
|
{
|
2018-03-12 12:20:26 +00:00
|
|
|
lua_pushnumber( L, idx + 1 );
|
|
|
|
lua_pushstring( L, monitors[ idx++ ] );
|
|
|
|
lua_settable( L, -3 );
|
2010-11-04 13:52:34 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
2018-03-12 12:20:26 +00:00
|
|
|
if( lua_pcall( L, 2, 1, -4 ) ) exit( -1 );
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
if( first_time )
|
|
|
|
{
|
2012-02-15 13:42:24 +00:00
|
|
|
// If not first time, simply retains the config file given
|
2018-04-22 15:52:35 +00:00
|
|
|
s = lua_tostring( L, -1 );
|
2018-03-13 11:02:34 +00:00
|
|
|
|
|
|
|
if( s ) lsyncd_config_file = s_strdup( s );
|
2010-11-03 21:02:14 +00:00
|
|
|
}
|
2018-03-12 12:20:26 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_pop( L, 2 );
|
2010-11-03 21:02:14 +00:00
|
|
|
}
|
|
|
|
|
2012-02-15 13:42:24 +00:00
|
|
|
// checks existence of the config file
|
2012-10-07 17:48:23 +00:00
|
|
|
if( lsyncd_config_file )
|
|
|
|
{
|
2010-10-27 09:06:13 +00:00
|
|
|
struct stat st;
|
2012-02-15 13:42:24 +00:00
|
|
|
|
|
|
|
// gets the absolute path to the config file
|
|
|
|
// so in case of HUPing the daemon, it finds it again
|
2012-10-07 17:48:23 +00:00
|
|
|
char * apath = get_realpath( lsyncd_config_file );
|
|
|
|
if( !apath )
|
|
|
|
{
|
2018-03-12 12:20:26 +00:00
|
|
|
printlogf( L, "Error", "Cannot find config file at '%s'.", lsyncd_config_file );
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
exit( -1 );
|
2012-02-15 13:42:24 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
free( lsyncd_config_file );
|
2018-03-12 12:20:26 +00:00
|
|
|
|
2012-02-15 13:42:24 +00:00
|
|
|
lsyncd_config_file = apath;
|
2012-02-15 19:00:28 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
if( stat( lsyncd_config_file, &st ) )
|
|
|
|
{
|
2018-03-12 12:20:26 +00:00
|
|
|
printlogf( L, "Error", "Cannot find config file at '%s'.", lsyncd_config_file );
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
exit( -1 );
|
2010-10-27 09:06:13 +00:00
|
|
|
}
|
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// loads and executes the config file
|
2012-10-07 17:48:23 +00:00
|
|
|
if( luaL_loadfile( L, lsyncd_config_file ) )
|
|
|
|
{
|
|
|
|
printlogf(
|
|
|
|
L, "Error",
|
2018-03-13 11:02:34 +00:00
|
|
|
"error loading %s: %s", lsyncd_config_file, lua_tostring( L, -1 )
|
2012-10-07 17:48:23 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
exit( -1 );
|
2010-11-03 21:02:14 +00:00
|
|
|
}
|
2012-02-15 13:42:24 +00:00
|
|
|
|
2018-03-15 12:43:02 +00:00
|
|
|
// loads the user enivornment
|
2018-05-13 12:13:54 +00:00
|
|
|
lua_getglobal( L, "userenv" );
|
2018-03-15 12:43:02 +00:00
|
|
|
lua_setupvalue( L, -2, 1 );
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
if( lua_pcall( L, 0, LUA_MULTRET, 0) )
|
|
|
|
{
|
|
|
|
printlogf(
|
|
|
|
L, "Error",
|
2018-03-13 11:02:34 +00:00
|
|
|
"error preparing %s: %s", lsyncd_config_file, lua_tostring( L, -1 )
|
2012-10-07 17:48:23 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
exit( -1 );
|
2010-11-03 21:02:14 +00:00
|
|
|
}
|
2010-10-17 17:13:53 +00:00
|
|
|
}
|
2010-10-16 10:26:48 +00:00
|
|
|
|
2018-03-14 08:57:49 +00:00
|
|
|
// runs initializations from mantle
|
2012-10-07 17:48:23 +00:00
|
|
|
// it will set the configuration and add watches
|
2010-10-25 21:04:28 +00:00
|
|
|
{
|
2018-03-15 08:27:14 +00:00
|
|
|
load_mci( L, "initialize" );
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_pushboolean( L, first_time );
|
|
|
|
|
2018-03-12 12:20:26 +00:00
|
|
|
if( lua_pcall( L, 1, 0, -3 ) ) exit( -1 );
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
lua_pop( L, 1 );
|
2010-10-25 21:04:28 +00:00
|
|
|
}
|
2010-10-18 17:09:59 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
//
|
|
|
|
// enters the master loop
|
|
|
|
//
|
|
|
|
masterloop( L );
|
2010-10-18 17:09:59 +00:00
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// cleanup
|
2018-03-28 06:59:10 +00:00
|
|
|
observe_tidy_all( );
|
2018-06-06 07:24:13 +00:00
|
|
|
mci_tidy( );
|
|
|
|
signal_tidy( );
|
2010-11-28 09:15:54 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
// frees logging categories
|
2018-03-23 08:34:33 +00:00
|
|
|
log_free( );
|
2010-11-14 09:11:09 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_close( L );
|
2018-03-12 12:20:26 +00:00
|
|
|
|
2010-10-14 13:52:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2010-11-14 09:11:09 +00:00
|
|
|
|
2016-05-03 08:45:22 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*
|
|
|
|
| Main
|
|
|
|
*/
|
2010-11-14 09:11:09 +00:00
|
|
|
int
|
2012-10-07 17:48:23 +00:00
|
|
|
main( int argc, char * argv[ ] )
|
2010-11-14 09:11:09 +00:00
|
|
|
{
|
2012-02-15 11:15:32 +00:00
|
|
|
// gets a kernel parameter
|
2012-10-07 17:48:23 +00:00
|
|
|
clocks_per_sec = sysconf( _SC_CLK_TCK );
|
2010-11-14 09:11:09 +00:00
|
|
|
|
2018-03-09 09:03:08 +00:00
|
|
|
setlinebuf( stdout );
|
|
|
|
setlinebuf( stderr );
|
|
|
|
|
2018-06-04 06:54:06 +00:00
|
|
|
while( true ) main1( argc, argv );
|
2013-07-30 10:20:23 +00:00
|
|
|
|
2018-06-04 06:54:06 +00:00
|
|
|
// return -1;
|
2010-11-14 09:11:09 +00:00
|
|
|
}
|
|
|
|
|