2012-10-07 17:48:23 +00:00
|
|
|
/*
|
|
|
|
| lsyncd.c Live (Mirror) Syncing Demon
|
|
|
|
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
| This is Lsyncd's core.
|
|
|
|
|
|
|
|
|
| It contains as minimal as possible glues to the operating system needed
|
|
|
|
| for Lsyncd's operation. All high-level logic is coded (when feasable)
|
|
|
|
| into lsyncd.lua
|
|
|
|
|
|
|
|
|
| This code assumes you have a 100 character wide display to view it (when tabstop is 4)
|
|
|
|
|
|
|
|
|
| License: GPLv2 (see COPYING) or any later version
|
|
|
|
| Authors: Axel Kittenberger <axkibe@gmail.com>
|
|
|
|
|
|
|
|
|
*/
|
2010-11-22 21:17:08 +00:00
|
|
|
|
2010-11-22 14:54:50 +00:00
|
|
|
#include "lsyncd.h"
|
2010-10-16 10:26:48 +00:00
|
|
|
|
2011-03-01 14:57:26 +00:00
|
|
|
#define SYSLOG_NAMES 1
|
|
|
|
|
2022-03-17 01:26:00 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
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 <signal.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>
|
|
|
|
|
2021-12-14 15:45:25 +00:00
|
|
|
#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
|
|
|
|
#define lua_objlen lua_rawlen
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*
|
|
|
|
| The Lua part of Lsyncd
|
|
|
|
*/
|
2012-04-04 11:15:33 +00:00
|
|
|
extern const char runner_out[];
|
|
|
|
extern size_t runner_size;
|
2010-10-18 17:09:59 +00:00
|
|
|
|
2012-02-15 19:00:28 +00:00
|
|
|
extern const char defaults_out[];
|
|
|
|
extern size_t defaults_size;
|
|
|
|
|
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
|
|
|
|
#ifndef WITH_FSEVENTS
|
|
|
|
# error "needing at least one notification system. please rerun cmake"
|
2010-11-28 08:13:05 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*
|
|
|
|
| All monitors supported by this Lsyncd.
|
|
|
|
*/
|
2010-11-28 08:13:05 +00:00
|
|
|
static 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
|
|
|
|
2014-04-29 14:11:27 +00:00
|
|
|
#ifdef WITH_FSEVENTS
|
2010-11-28 08:13:05 +00:00
|
|
|
"fsevents",
|
|
|
|
#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
|
|
|
|
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,
|
2012-10-07 17:48:23 +00:00
|
|
|
.log_level = LOG_NOTICE,
|
|
|
|
.nodaemon = false,
|
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
|
|
|
/*
|
|
|
|
| True when Lsyncd daemonized itself.
|
|
|
|
*/
|
2010-10-27 19:34:56 +00:00
|
|
|
static bool is_daemon = 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.
|
|
|
|
|
|
|
|
|
| All other resets (HUP or monitor OVERFLOW) run with 'insist'
|
|
|
|
| implictly turned on and thus Lsyncd does not failing on a non
|
|
|
|
| responding target.
|
|
|
|
*/
|
2011-08-29 09:21:40 +00:00
|
|
|
static 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
|
|
|
/*
|
|
|
|
| Set by TERM or HUP signal handler
|
|
|
|
| telling Lsyncd should end or reset ASAP.
|
|
|
|
*/
|
2010-11-22 20:09:52 +00:00
|
|
|
volatile sig_atomic_t hup = 0;
|
|
|
|
volatile sig_atomic_t term = 0;
|
2013-07-30 10:20:23 +00:00
|
|
|
volatile sig_atomic_t sigcode = 0;
|
|
|
|
int pidfile_fd = 0;
|
2010-10-16 10:26:48 +00:00
|
|
|
|
2016-05-03 08:45:22 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*
|
|
|
|
| The kernel's clock ticks per second.
|
|
|
|
*/
|
2012-02-15 11:15:32 +00:00
|
|
|
static long clocks_per_sec;
|
2010-10-19 10:12:11 +00:00
|
|
|
|
2016-05-03 08:45:22 +00:00
|
|
|
|
2022-03-23 22:41:02 +00:00
|
|
|
/*
|
|
|
|
| Dummy variable of which it's address is used as
|
|
|
|
| the cores index in the lua registry to
|
|
|
|
| the lua runners function table in the lua registry.
|
|
|
|
*/
|
|
|
|
static int runner;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
| Dummy variable of which it's address is used as
|
|
|
|
| the cores index n the lua registry to
|
|
|
|
| the lua runners error handler.
|
|
|
|
*/
|
|
|
|
static int callError;
|
|
|
|
|
|
|
|
|
2010-10-24 21:35:29 +00:00
|
|
|
/**
|
|
|
|
* signal handler
|
|
|
|
*/
|
|
|
|
void
|
2012-02-15 12:47:58 +00:00
|
|
|
sig_child(int sig) {
|
|
|
|
// nothing
|
2010-10-24 21:35:29 +00:00
|
|
|
}
|
2010-11-14 09:11:09 +00:00
|
|
|
|
2016-05-03 08:45:22 +00:00
|
|
|
|
2010-11-14 09:11:09 +00:00
|
|
|
/**
|
|
|
|
* signal handler
|
|
|
|
*/
|
|
|
|
void
|
2013-07-30 10:20:23 +00:00
|
|
|
sig_handler( int sig )
|
2010-11-14 09:11:09 +00:00
|
|
|
{
|
2013-07-30 10:20:23 +00:00
|
|
|
switch( sig )
|
|
|
|
{
|
|
|
|
case SIGTERM:
|
|
|
|
case SIGINT:
|
|
|
|
term = 1;
|
|
|
|
sigcode = sig;
|
|
|
|
return;
|
|
|
|
|
|
|
|
case SIGHUP:
|
|
|
|
hup = 1;
|
|
|
|
return;
|
2010-11-14 09:11:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 08:45:22 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*
|
|
|
|
| Non glibc builds need a real tms structure for the times( ) call
|
|
|
|
*/
|
2010-12-06 16:33:32 +00:00
|
|
|
#ifdef __GLIBC__
|
2012-10-07 17:48:23 +00:00
|
|
|
static struct tms * dummy_tms = NULL;
|
2010-12-06 16:33:32 +00:00
|
|
|
#else
|
2012-10-07 17:48:23 +00:00
|
|
|
static struct tms _dummy_tms;
|
|
|
|
static struct tms * dummy_tms = &_dummy_tms;
|
2010-12-06 16:33:32 +00:00
|
|
|
#endif
|
|
|
|
|
2016-05-03 08:45:22 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*
|
|
|
|
| Returns the absolute path of a path.
|
|
|
|
|
|
2012-02-15 13:42:24 +00:00
|
|
|
| This is a wrapper to various C-Library differences.
|
|
|
|
*/
|
|
|
|
char *
|
2012-10-07 17:48:23 +00:00
|
|
|
get_realpath( const char * rpath )
|
|
|
|
{
|
2012-02-15 13:42:24 +00:00
|
|
|
// uses c-library to get the absolute path
|
|
|
|
#ifdef __GLIBC__
|
|
|
|
// in case of GLIBC the task is easy.
|
2012-10-07 17:48:23 +00:00
|
|
|
return realpath( rpath, NULL );
|
2012-02-15 13:42:24 +00:00
|
|
|
#else
|
|
|
|
# warning having to use old style realpath()
|
2012-10-07 17:48:23 +00:00
|
|
|
// otherwise less so and requires PATH_MAX limit
|
|
|
|
char buf[ PATH_MAX] ;
|
|
|
|
char *asw = realpath( rpath, buf );
|
|
|
|
if( !asw )
|
|
|
|
{ return NULL; }
|
|
|
|
|
|
|
|
return s_strdup( asw );
|
2012-02-15 13:42:24 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-11-03 14:54:33 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
|
|
|
|
( Logging )
|
|
|
|
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
| A logging category
|
|
|
|
*/
|
|
|
|
struct logcat
|
|
|
|
{
|
2010-11-03 14:54:33 +00:00
|
|
|
char *name;
|
|
|
|
int priority;
|
|
|
|
};
|
|
|
|
|
2010-10-14 13:52:01 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*
|
|
|
|
| A table of all enabled logging categories.
|
|
|
|
| Sorted by first letter for faster access.
|
|
|
|
*/
|
|
|
|
static struct logcat *
|
|
|
|
logcats[ 26 ] = { 0, };
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
| Returns a positive priority if category is configured to be logged or -1.
|
|
|
|
*/
|
2010-11-22 20:09:52 +00:00
|
|
|
extern int
|
2012-10-07 17:48:23 +00:00
|
|
|
check_logcat( const char *name )
|
2010-10-19 20:14:55 +00:00
|
|
|
{
|
2010-11-03 14:54:33 +00:00
|
|
|
struct logcat *lc;
|
2012-02-15 12:47:58 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
if( name[ 0 ] < 'A' || name[ 0 ] > 'Z')
|
|
|
|
{ return 99; }
|
|
|
|
|
|
|
|
lc = logcats[ name[ 0 ] - 'A' ];
|
|
|
|
|
|
|
|
if( !lc )
|
|
|
|
{ return 99; }
|
|
|
|
|
|
|
|
while( lc->name )
|
|
|
|
{
|
|
|
|
if( !strcmp( lc->name, name ) )
|
|
|
|
{ return lc->priority; }
|
2012-02-15 12:47:58 +00:00
|
|
|
|
2010-11-03 14:54:33 +00:00
|
|
|
lc++;
|
|
|
|
}
|
2012-02-15 12:47:58 +00:00
|
|
|
|
2011-11-21 13:31:34 +00:00
|
|
|
return 99;
|
2010-10-19 20:14:55 +00:00
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
| Adds a logging category
|
|
|
|
|
|
|
|
|
| Returns true if OK.
|
|
|
|
*/
|
2010-11-03 21:02:14 +00:00
|
|
|
static bool
|
2012-10-07 17:48:23 +00:00
|
|
|
add_logcat( const char *name, int priority )
|
2010-10-18 17:09:59 +00:00
|
|
|
{
|
2012-02-15 11:15:32 +00:00
|
|
|
struct logcat *lc;
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
if( !strcmp( "all", name ) )
|
|
|
|
{
|
2011-11-21 13:31:34 +00:00
|
|
|
settings.log_level = 99;
|
2016-12-01 11:52:09 +00:00
|
|
|
|
2010-11-03 21:02:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
if( !strcmp( "scarce", name ) )
|
|
|
|
{
|
2010-12-10 13:17:01 +00:00
|
|
|
settings.log_level = LOG_WARNING;
|
2016-12-01 11:52:09 +00:00
|
|
|
|
2010-11-03 21:02:14 +00:00
|
|
|
return true;
|
2010-11-03 16:04:11 +00:00
|
|
|
}
|
2010-11-03 14:54:33 +00:00
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// categories must start with a capital letter.
|
2012-10-07 17:48:23 +00:00
|
|
|
if( name[ 0 ] < 'A' || name[ 0 ] > 'Z' )
|
2016-12-01 11:52:09 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2012-02-15 11:15:32 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
if( !logcats[ name[ 0 ]- 'A' ] )
|
|
|
|
{
|
2012-02-15 11:15:32 +00:00
|
|
|
// an empty capital letter
|
2010-11-03 14:54:33 +00:00
|
|
|
lc = logcats[name[0]-'A'] = s_calloc(2, sizeof(struct logcat));
|
2012-10-07 17:48:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// length of letter list
|
|
|
|
int ll = 0;
|
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// counts list length
|
2012-10-07 17:48:23 +00:00
|
|
|
for( lc = logcats[name[0]-'A']; lc->name; lc++ )
|
|
|
|
{ ll++; }
|
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// enlarges list
|
2012-10-07 17:48:23 +00:00
|
|
|
logcats[ name[ 0 ] - 'A'] =
|
|
|
|
s_realloc(
|
|
|
|
logcats[ name[ 0 ]-'A' ],
|
|
|
|
( ll + 2 ) * sizeof( struct logcat )
|
|
|
|
);
|
|
|
|
|
|
|
|
// goes to the list end
|
|
|
|
for( lc = logcats[ name[ 0 ] - 'A']; lc->name; lc++ )
|
|
|
|
{
|
|
|
|
if( !strcmp( name, lc->name ) )
|
|
|
|
{
|
2012-02-15 11:15:32 +00:00
|
|
|
// already there
|
2010-11-03 21:02:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2010-11-03 14:54:33 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
lc->name = s_strdup( name );
|
2010-11-03 14:54:33 +00:00
|
|
|
lc->priority = priority;
|
2012-10-07 17:48:23 +00:00
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// terminates the list
|
2012-10-07 17:48:23 +00:00
|
|
|
lc[ 1 ].name = NULL;
|
2010-11-03 21:02:14 +00:00
|
|
|
return true;
|
2010-10-18 17:09:59 +00:00
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
| Logs a string.
|
|
|
|
|
|
|
|
|
| Do not call this directly, but the macro logstring( )
|
|
|
|
| defined in lsyncd.h
|
|
|
|
*/
|
2012-02-15 11:15:32 +00:00
|
|
|
extern void
|
2012-10-07 17:48:23 +00:00
|
|
|
logstring0(
|
|
|
|
int priority, // the priority of the log message
|
|
|
|
const char * cat, // the category
|
|
|
|
const char * message // the log message
|
|
|
|
)
|
2010-10-20 18:33:17 +00:00
|
|
|
{
|
2012-10-07 17:48:23 +00:00
|
|
|
if( first_time )
|
|
|
|
{
|
|
|
|
// lsyncd is in it's intial configuration phase.
|
2012-02-15 11:15:32 +00:00
|
|
|
// thus just print to normal stdout/stderr.
|
2012-10-07 17:48:23 +00:00
|
|
|
if( priority >= LOG_ERR )
|
|
|
|
{
|
|
|
|
fprintf( stderr, "%s: %s\n", cat, message);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf( "%s: %s\n", cat, message );
|
2010-10-27 19:34:56 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// writes on console if not daemonized
|
2012-10-07 17:48:23 +00:00
|
|
|
if( !is_daemon )
|
|
|
|
{
|
|
|
|
char ct[ 255 ];
|
2012-02-15 11:15:32 +00:00
|
|
|
// gets current timestamp hour:minute:second
|
2010-10-20 18:33:17 +00:00
|
|
|
time_t mtime;
|
2016-05-03 08:45:22 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
time( &mtime );
|
|
|
|
|
|
|
|
strftime( ct, sizeof( ct ), "%T", localtime( &mtime ) );
|
|
|
|
|
2010-11-03 14:54:33 +00:00
|
|
|
FILE * flog = priority <= LOG_ERR ? stderr : stdout;
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
fprintf(
|
|
|
|
flog,
|
|
|
|
"%s %s: %s\n",
|
|
|
|
ct, cat, message
|
|
|
|
);
|
2010-10-20 18:33:17 +00:00
|
|
|
}
|
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// writes to file if configured so
|
2016-05-03 08:45:22 +00:00
|
|
|
if( settings.log_file )
|
2012-10-07 17:48:23 +00:00
|
|
|
{
|
|
|
|
FILE * flog = fopen( settings.log_file, "a" );
|
2016-05-03 08:45:22 +00:00
|
|
|
|
2010-10-20 18:33:17 +00:00
|
|
|
char * ct;
|
2016-05-03 08:45:22 +00:00
|
|
|
|
2010-10-20 18:33:17 +00:00
|
|
|
time_t mtime;
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
// gets current timestamp day-time-year
|
|
|
|
time( &mtime );
|
2016-05-03 08:45:22 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
ct = ctime( &mtime );
|
|
|
|
|
|
|
|
// cuts trailing linefeed
|
2016-05-03 08:45:22 +00:00
|
|
|
ct[ strlen( ct ) - 1] = 0;
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
if( flog == NULL )
|
|
|
|
{
|
|
|
|
fprintf(
|
|
|
|
stderr,
|
|
|
|
"Cannot open logfile [%s]!\n",
|
|
|
|
settings.log_file
|
|
|
|
);
|
2016-05-03 08:45:22 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
exit( -1 );
|
2010-10-20 18:33:17 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
fprintf(
|
|
|
|
flog,
|
|
|
|
"%s %s: %s\n",
|
|
|
|
ct, cat, message
|
|
|
|
);
|
|
|
|
|
|
|
|
fclose( flog );
|
2010-10-20 18:33:17 +00:00
|
|
|
}
|
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// sends to syslog if configured so
|
2012-10-07 17:48:23 +00:00
|
|
|
if( settings.log_syslog )
|
|
|
|
{
|
|
|
|
syslog( priority, "%s, %s", cat, message );
|
|
|
|
}
|
|
|
|
|
2010-10-20 18:33:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
| Lets the core print logmessages comfortably as formated string.
|
|
|
|
| This uses the lua_State for it easy string buffers only.
|
|
|
|
*/
|
2010-11-22 14:54:50 +00:00
|
|
|
extern void
|
2012-02-15 11:15:32 +00:00
|
|
|
printlogf0(lua_State *L,
|
2010-11-04 13:43:57 +00:00
|
|
|
int priority,
|
|
|
|
const char *cat,
|
|
|
|
const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
lua_pushvfstring(L, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
logstring0(priority, cat, luaL_checkstring(L, -1));
|
|
|
|
lua_pop(L, 1);
|
|
|
|
return;
|
|
|
|
}
|
2010-11-05 18:04:29 +00:00
|
|
|
|
2010-11-03 14:54:33 +00:00
|
|
|
|
2022-03-11 06:11:39 +00:00
|
|
|
/*
|
|
|
|
| Print a traceback of the error
|
|
|
|
*/
|
2022-03-17 01:29:23 +00:00
|
|
|
static int l_traceback (lua_State *L) {
|
2022-03-23 22:41:02 +00:00
|
|
|
// runner.callError
|
2022-03-11 06:11:39 +00:00
|
|
|
lua_getglobal(L, "debug");
|
|
|
|
lua_getfield(L, -1, "traceback");
|
|
|
|
lua_pushvalue(L, 1);
|
|
|
|
lua_pushinteger(L, 2);
|
|
|
|
lua_call(L, 2, 1);
|
|
|
|
printlogf( L, "traceback", "%s", lua_tostring(L, -1) );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-03-23 22:41:02 +00:00
|
|
|
/*
|
|
|
|
| Call runners terminate function and exit with given exit code
|
|
|
|
*/
|
|
|
|
static void safeexit (lua_State *L, int exitcode) {
|
|
|
|
// load_runner_func(L, "teardown");
|
|
|
|
// pushes the function
|
|
|
|
lua_pushlightuserdata( L, (void *) &runner );
|
|
|
|
lua_gettable( L, LUA_REGISTRYINDEX );
|
|
|
|
lua_pushstring( L, "teardown" );
|
|
|
|
lua_gettable( L, -2 );
|
|
|
|
lua_remove( L, -2 );
|
|
|
|
lua_pushinteger(L, exitcode);
|
|
|
|
lua_call(L, 2, 1);
|
2022-06-03 04:45:30 +00:00
|
|
|
if (lua_isnumber(L, -1)) {
|
2022-06-03 05:07:08 +00:00
|
|
|
exitcode = luaL_checkinteger(L, -1);
|
2022-03-23 22:41:02 +00:00
|
|
|
}
|
|
|
|
exit(exitcode);
|
|
|
|
}
|
|
|
|
|
2022-03-11 06:11:39 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
|
|
|
|
( Simple memory management )
|
|
|
|
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
|
|
|
|
|
|
|
|
|
|
// FIXME call the Lua garbace collector in case of out of memory
|
|
|
|
|
|
|
|
/*
|
|
|
|
| "Secured" calloc
|
|
|
|
*/
|
2010-11-22 14:54:50 +00:00
|
|
|
extern void *
|
2012-10-07 17:48:23 +00:00
|
|
|
s_calloc( size_t nmemb, size_t size )
|
2010-11-03 14:54:33 +00:00
|
|
|
{
|
2012-10-07 17:48:23 +00:00
|
|
|
void * r = calloc( nmemb, size );
|
|
|
|
|
|
|
|
if( r == NULL )
|
|
|
|
{
|
|
|
|
logstring0(
|
|
|
|
LOG_ERR,
|
|
|
|
"Error",
|
|
|
|
"Out of memory!"
|
|
|
|
);
|
|
|
|
|
|
|
|
exit( -1 );
|
2012-02-15 11:15:32 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
2010-11-03 14:54:33 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
| "Secured" malloc
|
|
|
|
*/
|
2010-11-22 14:54:50 +00:00
|
|
|
extern void *
|
2012-10-07 17:48:23 +00:00
|
|
|
s_malloc( size_t size )
|
2010-11-03 14:54:33 +00:00
|
|
|
{
|
2012-10-07 17:48:23 +00:00
|
|
|
void * r = malloc( size );
|
|
|
|
|
|
|
|
if( r == NULL )
|
|
|
|
{
|
|
|
|
logstring0(
|
|
|
|
LOG_ERR,
|
|
|
|
"Error",
|
|
|
|
"Out of memory!"
|
|
|
|
);
|
|
|
|
|
|
|
|
exit( -1 );
|
2010-11-03 14:54:33 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
2010-11-03 14:54:33 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
| "Secured" realloc
|
|
|
|
*/
|
2010-11-22 14:54:50 +00:00
|
|
|
extern void *
|
2012-10-07 17:48:23 +00:00
|
|
|
s_realloc( void * ptr, size_t size )
|
2010-11-02 21:04:01 +00:00
|
|
|
{
|
2012-10-07 17:48:23 +00:00
|
|
|
void * r = realloc( ptr, size );
|
|
|
|
|
|
|
|
if( r == NULL )
|
|
|
|
{
|
|
|
|
logstring0(
|
|
|
|
LOG_ERR,
|
|
|
|
"Error",
|
|
|
|
"Out of memory!"
|
|
|
|
);
|
|
|
|
|
|
|
|
exit( -1 );
|
2010-11-02 21:04:01 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
2010-11-03 14:54:33 +00:00
|
|
|
return r;
|
2010-11-02 21:04:01 +00:00
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
| "Secured" strdup
|
|
|
|
*/
|
2010-11-22 14:54:50 +00:00
|
|
|
extern char *
|
2012-10-07 17:48:23 +00:00
|
|
|
s_strdup( const char *src )
|
2010-11-03 14:54:33 +00:00
|
|
|
{
|
2012-10-07 17:48:23 +00:00
|
|
|
char *s = strdup( src );
|
|
|
|
|
|
|
|
if( s == NULL )
|
|
|
|
{
|
|
|
|
logstring0(
|
|
|
|
LOG_ERR,
|
|
|
|
"Error",
|
|
|
|
"Out of memory!"
|
|
|
|
);
|
|
|
|
|
|
|
|
exit( -1 );
|
2010-11-03 14:54:33 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
2010-11-03 14:54:33 +00:00
|
|
|
return s;
|
|
|
|
}
|
2010-10-18 17:09:59 +00:00
|
|
|
|
2010-11-17 11:14:36 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
|
|
|
|
( Pipes Management )
|
|
|
|
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
| A child process gets text piped through stdin
|
|
|
|
*/
|
|
|
|
struct pipemsg
|
|
|
|
{
|
|
|
|
char * text; // message to send
|
|
|
|
int tlen; // length of text
|
|
|
|
int pos; // position in message
|
2010-11-17 11:14:36 +00:00
|
|
|
};
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*
|
|
|
|
| Called by the core whenever a pipe becomes
|
|
|
|
| writeable again
|
|
|
|
*/
|
2012-02-15 11:15:32 +00:00
|
|
|
static void
|
2012-10-07 17:48:23 +00:00
|
|
|
pipe_writey(
|
|
|
|
lua_State * L,
|
|
|
|
struct observance * observance
|
|
|
|
)
|
2010-11-28 09:15:54 +00:00
|
|
|
{
|
|
|
|
int fd = observance->fd;
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
struct pipemsg *pm = (struct pipemsg * ) observance->extra;
|
|
|
|
|
|
|
|
int len = write(
|
|
|
|
fd,
|
|
|
|
pm->text + pm->pos,
|
|
|
|
pm->tlen - pm->pos
|
|
|
|
);
|
|
|
|
|
2010-11-22 20:09:52 +00:00
|
|
|
pm->pos += len;
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
if( len < 0 )
|
|
|
|
{
|
|
|
|
logstring( "Normal", "broken pipe." );
|
|
|
|
nonobserve_fd( fd );
|
|
|
|
}
|
|
|
|
else if( pm->pos >= pm->tlen )
|
|
|
|
{
|
2013-06-07 09:12:24 +00:00
|
|
|
logstring( "Exec", "finished pipe." );
|
2010-11-28 09:15:54 +00:00
|
|
|
nonobserve_fd(fd);
|
2010-11-22 20:09:52 +00:00
|
|
|
}
|
2010-11-25 22:10:24 +00:00
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
| Called when cleaning up a pipe.
|
|
|
|
*/
|
2010-11-28 09:15:54 +00:00
|
|
|
static void
|
2012-10-07 17:48:23 +00:00
|
|
|
pipe_tidy( struct observance * observance )
|
2010-11-28 09:15:54 +00:00
|
|
|
{
|
2012-10-07 17:48:23 +00:00
|
|
|
struct pipemsg *pm = ( struct pipemsg * ) observance->extra;
|
|
|
|
|
|
|
|
close( observance->fd );
|
|
|
|
free( pm->text );
|
|
|
|
free( pm );
|
2010-11-28 09:15:54 +00:00
|
|
|
}
|
2010-11-17 11:14:36 +00:00
|
|
|
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
|
|
|
|
( Helper Routines )
|
|
|
|
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
| Sets the close-on-exit flag of a file descriptor.
|
|
|
|
*/
|
2010-11-22 14:54:50 +00:00
|
|
|
extern void
|
2012-10-07 17:48:23 +00:00
|
|
|
close_exec_fd( int fd )
|
2010-11-12 15:39:43 +00:00
|
|
|
{
|
|
|
|
int flags;
|
2012-10-07 17:48:23 +00:00
|
|
|
|
2016-05-03 08:45:22 +00:00
|
|
|
flags = fcntl( fd, F_GETFD );
|
2012-10-07 17:48:23 +00:00
|
|
|
|
2016-05-03 08:45:22 +00:00
|
|
|
if( flags == -1 )
|
2012-10-07 17:48:23 +00:00
|
|
|
{
|
|
|
|
logstring( "Error", "cannot get descriptor flags!" );
|
|
|
|
exit( -1 );
|
2010-11-12 15:39:43 +00:00
|
|
|
}
|
2012-02-15 14:16:00 +00:00
|
|
|
|
2010-11-12 15:39:43 +00:00
|
|
|
flags |= FD_CLOEXEC;
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
if( fcntl( fd, F_SETFD, flags ) == -1 )
|
|
|
|
{
|
|
|
|
logstring( "Error", "cannot set descripptor flags!" );
|
|
|
|
exit( -1 );
|
2010-11-12 15:39:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
| Sets the non-blocking flag of a file descriptor.
|
|
|
|
*/
|
2010-11-22 14:54:50 +00:00
|
|
|
extern void
|
2012-10-07 17:48:23 +00:00
|
|
|
non_block_fd( int fd )
|
2010-11-12 15:39:43 +00:00
|
|
|
{
|
|
|
|
int flags;
|
2012-02-15 12:47:58 +00:00
|
|
|
|
2016-05-03 08:45:22 +00:00
|
|
|
flags = fcntl( fd, F_GETFL );
|
2012-10-07 17:48:23 +00:00
|
|
|
|
2016-05-03 08:45:22 +00:00
|
|
|
if( flags == -1 )
|
2012-10-07 17:48:23 +00:00
|
|
|
{
|
|
|
|
logstring( "Error", "cannot get status flags!" );
|
|
|
|
exit( -1 );
|
2010-11-12 15:39:43 +00:00
|
|
|
}
|
2012-02-15 12:47:58 +00:00
|
|
|
|
2010-11-28 09:15:54 +00:00
|
|
|
flags |= O_NONBLOCK;
|
2012-02-15 12:47:58 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
if( fcntl( fd, F_SETFL, flags ) == -1 )
|
|
|
|
{
|
|
|
|
logstring( "Error", "cannot set status flags!" );
|
|
|
|
exit( -1 );
|
2010-11-12 15:39:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*
|
|
|
|
| Writes a pid file.
|
|
|
|
*/
|
2010-11-22 14:54:50 +00:00
|
|
|
static void
|
2017-01-09 12:13:05 +00:00
|
|
|
write_pidfile
|
|
|
|
(
|
2013-07-30 10:20:23 +00:00
|
|
|
lua_State *L,
|
|
|
|
const char *pidfile
|
|
|
|
)
|
2012-10-07 17:48:23 +00:00
|
|
|
{
|
2015-10-14 10:57:49 +00:00
|
|
|
pidfile_fd = open( pidfile, O_CREAT | O_RDWR, 0644 );
|
|
|
|
|
|
|
|
fcntl( pidfile_fd, F_SETFD, FD_CLOEXEC );
|
2013-07-30 10:20:23 +00:00
|
|
|
|
|
|
|
char buf[ 127 ];
|
2012-10-07 17:48:23 +00:00
|
|
|
|
2013-07-30 10:20:23 +00:00
|
|
|
if( pidfile_fd < 0 )
|
2012-10-07 17:48:23 +00:00
|
|
|
{
|
|
|
|
printlogf(
|
|
|
|
L, "Error",
|
2013-07-30 10:20:23 +00:00
|
|
|
"Cannot create pidfile; '%s'",
|
2012-10-07 17:48:23 +00:00
|
|
|
pidfile
|
2013-07-30 10:20:23 +00:00
|
|
|
);
|
|
|
|
|
2022-03-23 22:41:02 +00:00
|
|
|
safeexit(L, -1 );
|
2013-07-30 10:20:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int rc = lockf( pidfile_fd, F_TLOCK, 0 );
|
|
|
|
|
|
|
|
if( rc < 0 )
|
|
|
|
{
|
|
|
|
printlogf(
|
|
|
|
L, "Error",
|
|
|
|
"Cannot lock pidfile; '%s'",
|
|
|
|
pidfile
|
|
|
|
);
|
|
|
|
|
2022-03-23 22:41:02 +00:00
|
|
|
safeexit(L, -1 );
|
2012-10-07 17:48:23 +00:00
|
|
|
}
|
|
|
|
|
2013-07-30 10:20:23 +00:00
|
|
|
snprintf( buf, sizeof( buf ), "%i\n", getpid( ) );
|
|
|
|
|
2022-06-03 03:06:33 +00:00
|
|
|
if (write( pidfile_fd, buf, strlen( buf ) ) != strlen(buf)) {
|
|
|
|
printlogf(
|
|
|
|
L, "Error",
|
|
|
|
"Cannot write pidfile; '%s'",
|
|
|
|
pidfile
|
|
|
|
);
|
|
|
|
|
|
|
|
safeexit(L, -1 );
|
|
|
|
}
|
2013-07-30 10:20:23 +00:00
|
|
|
|
2010-11-17 11:14:36 +00:00
|
|
|
}
|
2010-11-12 15:39:43 +00:00
|
|
|
|
2010-12-01 12:19:17 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
|
|
|
|
( Observances )
|
|
|
|
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
| List of file descriptor watches.
|
|
|
|
*/
|
2010-12-01 12:19:17 +00:00
|
|
|
static struct observance * observances = NULL;
|
2012-10-07 17:48:23 +00:00
|
|
|
static int observances_len = 0;
|
|
|
|
static int observances_size = 0;
|
2010-12-01 12:19:17 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
| List of file descriptors to not observe.
|
|
|
|
|
|
|
|
|
| While working for the oberver lists, it may
|
|
|
|
| not be altered, thus nonobserve stores the
|
|
|
|
| delayed removals.
|
|
|
|
*/
|
|
|
|
static int * nonobservances = NULL;
|
|
|
|
static int nonobservances_len = 0;
|
2010-12-01 12:19:17 +00:00
|
|
|
static int nonobservances_size = 0;
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*
|
|
|
|
| True while the observances list is being handled.
|
|
|
|
*/
|
2010-12-01 12:19:17 +00:00
|
|
|
static bool observance_action = false;
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
| Core watches a filedescriptor to become ready,
|
|
|
|
| one of read_ready or write_ready may be zero
|
|
|
|
*/
|
2010-12-01 12:19:17 +00:00
|
|
|
extern void
|
2012-10-07 17:48:23 +00:00
|
|
|
observe_fd(
|
|
|
|
int fd,
|
|
|
|
void ( * ready ) (lua_State *, struct observance * ),
|
|
|
|
void ( * writey ) (lua_State *, struct observance * ),
|
|
|
|
void ( * tidy ) (struct observance * ),
|
|
|
|
void *extra
|
|
|
|
)
|
2010-12-01 12:19:17 +00:00
|
|
|
{
|
|
|
|
int pos;
|
2012-10-07 17:48:23 +00:00
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// looks if the fd is already there as pos or
|
|
|
|
// stores the position to insert the new fd in pos
|
2012-10-07 17:48:23 +00:00
|
|
|
for( pos = 0; pos < observances_len; pos++)
|
|
|
|
{
|
|
|
|
if( fd <= observances[ pos ].fd )
|
|
|
|
{ break; }
|
2010-12-01 12:19:17 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
if( pos < observances_len && observances[ pos ].fd == fd )
|
|
|
|
{
|
2012-02-15 11:15:32 +00:00
|
|
|
// just updates an existing observance
|
2012-10-07 17:48:23 +00:00
|
|
|
logstring( "Masterloop", "updating fd observance" );
|
|
|
|
observances[ pos ].ready = ready;
|
|
|
|
observances[ pos ].writey = writey;
|
|
|
|
observances[ pos ].tidy = tidy;
|
|
|
|
observances[ pos ].extra = extra;
|
2010-12-01 12:19:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
if( observance_action )
|
|
|
|
{
|
|
|
|
// FIXME
|
|
|
|
logstring(
|
|
|
|
"Error",
|
|
|
|
"New observances in ready/writey handlers not yet supported"
|
|
|
|
);
|
|
|
|
|
|
|
|
exit( -1 );
|
2010-12-01 12:19:17 +00:00
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
if( !tidy )
|
|
|
|
{
|
|
|
|
logstring(
|
|
|
|
"Error",
|
|
|
|
"internal, tidy() in observe_fd() must not be NULL."
|
|
|
|
);
|
|
|
|
exit( -1 );
|
2010-12-01 12:19:17 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
if( observances_len + 1 > observances_size )
|
|
|
|
{
|
2010-12-01 12:19:17 +00:00
|
|
|
observances_size = observances_len + 1;
|
2012-10-07 17:48:23 +00:00
|
|
|
observances = s_realloc(
|
|
|
|
observances,
|
|
|
|
observances_size * sizeof( struct observance )
|
|
|
|
);
|
2010-12-01 12:19:17 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
memmove(
|
|
|
|
observances + pos + 1,
|
|
|
|
observances + pos,
|
|
|
|
(observances_len - pos) * sizeof(struct observance)
|
|
|
|
);
|
2010-12-01 12:19:17 +00:00
|
|
|
|
|
|
|
observances_len++;
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
observances[ pos ].fd = fd;
|
|
|
|
observances[ pos ].ready = ready;
|
|
|
|
observances[ pos ].writey = writey;
|
|
|
|
observances[ pos ].tidy = tidy;
|
|
|
|
observances[ pos ].extra = extra;
|
2010-12-01 12:19:17 +00:00
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
| Makes the core no longer watch a filedescriptor.
|
|
|
|
*/
|
2010-12-01 12:19:17 +00:00
|
|
|
extern void
|
2012-10-07 17:48:23 +00:00
|
|
|
nonobserve_fd( int fd )
|
2010-12-01 12:19:17 +00:00
|
|
|
{
|
|
|
|
int pos;
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
if( observance_action )
|
|
|
|
{
|
2012-02-15 11:15:32 +00:00
|
|
|
// this function is called through a ready/writey handler
|
|
|
|
// while the core works through the observance list, thus
|
|
|
|
// it does not alter the list, but stores this actions
|
|
|
|
// on a stack
|
2010-12-01 12:19:17 +00:00
|
|
|
nonobservances_len++;
|
2012-10-07 17:48:23 +00:00
|
|
|
if( nonobservances_len > nonobservances_size )
|
|
|
|
{
|
2010-12-01 12:19:17 +00:00
|
|
|
nonobservances_size = nonobservances_len;
|
2012-10-07 17:48:23 +00:00
|
|
|
nonobservances = s_realloc(
|
|
|
|
nonobservances,
|
|
|
|
nonobservances_size * sizeof( int )
|
|
|
|
);
|
2010-12-01 12:19:17 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
nonobservances[ nonobservances_len - 1 ] = fd;
|
2010-12-01 12:19:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// looks for the fd
|
2012-10-07 17:48:23 +00:00
|
|
|
for( pos = 0; pos < observances_len; pos++ )
|
|
|
|
{
|
|
|
|
if( observances[ pos ].fd == fd )
|
|
|
|
{ break; }
|
2010-12-01 12:19:17 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
if( pos >= observances_len )
|
|
|
|
{
|
|
|
|
logstring(
|
|
|
|
"Error",
|
|
|
|
"internal fail, not observance file descriptor in nonobserve"
|
|
|
|
);
|
|
|
|
|
|
|
|
exit( -1 );
|
2010-12-01 12:19:17 +00:00
|
|
|
}
|
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// tidies up the observance
|
2012-10-07 17:48:23 +00:00
|
|
|
observances[ pos ].tidy( observances + pos );
|
2012-02-15 11:15:32 +00:00
|
|
|
|
|
|
|
// and moves the list down
|
2012-10-07 17:48:23 +00:00
|
|
|
memmove(
|
|
|
|
observances + pos,
|
|
|
|
observances + pos + 1,
|
|
|
|
(observances_len - pos) * sizeof( struct observance )
|
|
|
|
);
|
|
|
|
|
2010-12-01 12:19:17 +00:00
|
|
|
observances_len--;
|
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
| A user observance became read-ready.
|
|
|
|
*/
|
2010-12-01 12:19:17 +00:00
|
|
|
static void
|
2012-10-07 17:48:23 +00:00
|
|
|
user_obs_ready(
|
|
|
|
lua_State * L,
|
|
|
|
struct observance * obs
|
|
|
|
)
|
2010-12-01 12:19:17 +00:00
|
|
|
{
|
|
|
|
int fd = obs->fd;
|
2012-10-07 17:48:23 +00:00
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// pushes the ready table on table
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_pushlightuserdata( L, ( void * ) user_obs_ready );
|
|
|
|
lua_gettable( L, LUA_REGISTRYINDEX );
|
2012-02-15 11:15:32 +00:00
|
|
|
|
|
|
|
// pushes the error handler
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_pushlightuserdata( L, (void *) &callError );
|
|
|
|
lua_gettable( L, LUA_REGISTRYINDEX );
|
2010-12-01 12:19:17 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
// pushes the user func
|
|
|
|
lua_pushnumber( L, fd );
|
|
|
|
lua_gettable( L, -3 );
|
2010-12-01 12:19:17 +00:00
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// gives the ufunc the fd
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_pushnumber( L, fd );
|
2010-12-01 12:19:17 +00:00
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// calls the user function
|
2012-10-07 17:48:23 +00:00
|
|
|
if( lua_pcall( L, 1, 0, -3 ) )
|
2013-07-30 10:20:23 +00:00
|
|
|
{
|
2022-03-23 22:41:02 +00:00
|
|
|
safeexit(L, -1 );
|
2013-07-30 10:20:23 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
lua_pop( L, 2 );
|
2010-12-01 12:19:17 +00:00
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
| A user observance became write-ready
|
|
|
|
*/
|
2010-12-01 12:19:17 +00:00
|
|
|
static void
|
2012-10-07 17:48:23 +00:00
|
|
|
user_obs_writey(
|
|
|
|
lua_State * L,
|
|
|
|
struct observance * obs
|
|
|
|
)
|
2010-12-01 12:19:17 +00:00
|
|
|
{
|
|
|
|
int fd = obs->fd;
|
2012-10-07 17:48:23 +00:00
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// pushes the writey table on table
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_pushlightuserdata( L, (void *) user_obs_writey );
|
|
|
|
lua_gettable( L, LUA_REGISTRYINDEX );
|
2010-12-01 12:19:17 +00:00
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// pushes the error handler
|
2010-12-01 12:19:17 +00:00
|
|
|
lua_pushlightuserdata(L, (void *) &callError);
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_gettable( L, LUA_REGISTRYINDEX );
|
2010-12-01 12:19:17 +00:00
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// pushes the user func
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_pushnumber( L, fd );
|
|
|
|
lua_gettable( L, -3 );
|
2010-12-01 12:19:17 +00:00
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// gives the user func the fd
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_pushnumber( L, fd );
|
2010-12-01 12:19:17 +00:00
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// calls the user function
|
2012-10-07 17:48:23 +00:00
|
|
|
if( lua_pcall( L, 1, 0, -3 ) )
|
|
|
|
{
|
2022-03-23 22:41:02 +00:00
|
|
|
safeexit(L, -1);
|
2012-10-07 17:48:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lua_pop( L, 2 );
|
2010-12-01 12:19:17 +00:00
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*
|
|
|
|
| Tidies up a user observance
|
|
|
|
| FIXME - give the user a chance to do something in that case!
|
|
|
|
*/
|
2010-12-01 12:19:17 +00:00
|
|
|
static void
|
2012-10-07 17:48:23 +00:00
|
|
|
user_obs_tidy( struct observance *obs )
|
2010-12-01 12:19:17 +00:00
|
|
|
{
|
2012-10-07 17:48:23 +00:00
|
|
|
close( obs->fd );
|
2010-12-01 12:19:17 +00:00
|
|
|
}
|
|
|
|
|
2010-11-12 15:39:43 +00:00
|
|
|
|
2017-01-09 12:13:05 +00:00
|
|
|
/******************************.
|
|
|
|
* Library calls for the runner *
|
|
|
|
'******************************/
|
2010-10-17 15:24:55 +00:00
|
|
|
|
2010-11-04 13:43:57 +00:00
|
|
|
|
2017-01-09 12:13:05 +00:00
|
|
|
static void daemonize( lua_State *L, const char *pidfile );
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
int l_stackdump( lua_State* L );
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
| Logs a message.
|
|
|
|
|
|
|
|
|
| Params on Lua stack:
|
|
|
|
|
|
|
|
|
| 1: loglevel of massage
|
|
|
|
| 2: the string to log
|
|
|
|
*/
|
2012-02-15 11:15:32 +00:00
|
|
|
static int
|
2012-10-07 17:48:23 +00:00
|
|
|
l_log( lua_State *L )
|
2010-10-20 15:34:01 +00:00
|
|
|
{
|
2012-02-15 11:15:32 +00:00
|
|
|
const char * cat; // log category
|
|
|
|
const char * message; // log message
|
|
|
|
int priority; // log priority
|
2010-11-03 14:54:33 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
cat = luaL_checkstring( L, 1 );
|
|
|
|
priority = check_logcat( cat );
|
|
|
|
|
|
|
|
// skips filtered messages
|
|
|
|
if( priority > settings.log_level )
|
2016-12-01 11:52:09 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2010-11-03 14:54:33 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
// replaces non string values
|
2010-11-04 13:43:57 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int top = lua_gettop(L);
|
2012-10-07 17:48:23 +00:00
|
|
|
for( i = 1; i <= top; i++ )
|
|
|
|
{
|
|
|
|
int t = lua_type( L, i );
|
|
|
|
|
|
|
|
switch( t )
|
|
|
|
{
|
|
|
|
case LUA_TTABLE :
|
|
|
|
lua_pushfstring(
|
|
|
|
L,
|
|
|
|
"(Table: %p)",
|
|
|
|
lua_topointer( L, i )
|
|
|
|
);
|
|
|
|
|
|
|
|
lua_replace( L, i );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LUA_TBOOLEAN :
|
|
|
|
if( lua_toboolean( L, i ) )
|
|
|
|
{ lua_pushstring( L, "(true)" ); }
|
|
|
|
else
|
|
|
|
{ lua_pushstring( L, "(false)" ); }
|
|
|
|
|
2010-11-29 10:56:39 +00:00
|
|
|
lua_replace(L, i);
|
2012-10-07 17:48:23 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LUA_TUSERDATA:
|
|
|
|
{
|
|
|
|
clock_t *c = ( clock_t * )
|
|
|
|
luaL_checkudata( L, i, "Lsyncd.jiffies" );
|
|
|
|
|
|
|
|
double d = *c;
|
|
|
|
d /= clocks_per_sec;
|
|
|
|
lua_pushfstring( L, "(Timestamp: %f)", d );
|
|
|
|
lua_replace( L, i );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LUA_TNIL:
|
|
|
|
lua_pushstring( L, "(nil)" );
|
|
|
|
lua_replace( L, i );
|
|
|
|
break;
|
2010-11-04 13:43:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// concates if there is more than one string parameter
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_concat( L, lua_gettop( L ) - 1 );
|
|
|
|
|
|
|
|
message = luaL_checkstring( L, 2 );
|
|
|
|
logstring0( priority, cat, message );
|
2010-11-02 17:07:42 +00:00
|
|
|
|
2010-10-20 15:34:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
| Returns (on Lua stack) the current kernels
|
|
|
|
| clock state (jiffies)
|
|
|
|
*/
|
2010-11-29 10:56:39 +00:00
|
|
|
extern int
|
2012-02-15 11:15:32 +00:00
|
|
|
l_now(lua_State *L)
|
2010-10-19 10:12:11 +00:00
|
|
|
{
|
2012-10-07 17:48:23 +00:00
|
|
|
clock_t * j = lua_newuserdata( L, sizeof( clock_t ) );
|
|
|
|
luaL_getmetatable( L, "Lsyncd.jiffies" );
|
|
|
|
lua_setmetatable( L, -2 );
|
|
|
|
*j = times( dummy_tms );
|
2010-10-19 10:12:11 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-03-11 06:11:39 +00:00
|
|
|
/*
|
|
|
|
| Sends a signal to proceess pid
|
|
|
|
|
|
|
|
|
| Params on Lua stack:
|
|
|
|
| 1: pid
|
|
|
|
| 2: signal
|
|
|
|
|
|
|
|
|
| Returns on Lua stack:
|
|
|
|
| return value of kill
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
l_kill( lua_State *L )
|
|
|
|
{
|
2022-06-03 05:07:08 +00:00
|
|
|
pid_t pid = luaL_checkinteger( L, 1 );
|
|
|
|
int sig = luaL_checkinteger( L, 2 );
|
2022-03-11 06:11:39 +00:00
|
|
|
|
|
|
|
int rv = kill(pid, sig );
|
|
|
|
|
|
|
|
lua_pushinteger( L, rv );
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2010-10-28 17:56:33 +00:00
|
|
|
|
2022-03-17 01:26:00 +00:00
|
|
|
/*
|
|
|
|
| Returns a free port of host
|
|
|
|
|
|
|
|
|
| Params on Lua stack:
|
|
|
|
| (not yet) 1: hostname or ip for bind
|
|
|
|
|
|
|
|
|
| Returns on Lua stack:
|
|
|
|
| return integer of free port
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
l_free_port(lua_State *L) {
|
|
|
|
int sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if(sock < 0) {
|
|
|
|
printf("error opening socket\n");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sockaddr_in serv_addr;
|
|
|
|
memset((char *) &serv_addr, 0, sizeof(serv_addr));
|
|
|
|
serv_addr.sin_family = AF_INET;
|
|
|
|
serv_addr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
serv_addr.sin_port = 0;
|
|
|
|
if (bind(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
|
|
|
|
if(errno == EADDRINUSE) {
|
|
|
|
printf("the port is not available. already to other process\n");
|
|
|
|
goto error;
|
|
|
|
} else {
|
|
|
|
printf("could not bind to process (%d) %s\n", errno, strerror(errno));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
socklen_t len = sizeof(serv_addr);
|
|
|
|
if (getsockname(sock, (struct sockaddr *)&serv_addr, &len) == -1) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_pushinteger(L, ntohs(serv_addr.sin_port));
|
|
|
|
|
|
|
|
if (close (sock) < 0 ) {
|
|
|
|
printf("did not close: %s\n", strerror(errno));
|
|
|
|
lua_pop ( L, 1 );
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
error:
|
|
|
|
lua_pushnil(L);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*
|
|
|
|
| Executes a subprocess. Does not wait for it to return.
|
|
|
|
|
|
|
|
|
| Params on Lua stack:
|
|
|
|
|
|
|
|
|
| 1: Path to binary to call
|
|
|
|
| 2: List of string as arguments
|
|
|
|
| or "<" in which case the next argument is a string
|
|
|
|
| that will be piped on stdin.
|
|
|
|
| The arguments will follow that one.
|
|
|
|
|
|
|
|
|
| Returns (Lua stack) the pid on success, 0 on failure.
|
|
|
|
*/
|
2010-10-17 17:13:53 +00:00
|
|
|
static int
|
2012-10-07 17:48:23 +00:00
|
|
|
l_exec( lua_State *L )
|
2010-10-17 17:13:53 +00:00
|
|
|
{
|
2012-10-07 17:48:23 +00:00
|
|
|
// the binary to call
|
|
|
|
const char *binary = luaL_checkstring(L, 1);
|
2022-04-14 11:55:07 +00:00
|
|
|
l_stackdump(L);
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
// number of arguments
|
|
|
|
int argc = lua_gettop( L ) - 1;
|
|
|
|
|
|
|
|
// the pid spawned
|
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
// the arguments position in the lua arguments
|
|
|
|
int li = 1;
|
|
|
|
|
|
|
|
// the pipe to text
|
|
|
|
char const * pipe_text = NULL;
|
|
|
|
|
|
|
|
// the pipes length
|
|
|
|
size_t pipe_len = 0;
|
|
|
|
|
|
|
|
// the arguments
|
|
|
|
char const ** argv;
|
|
|
|
|
|
|
|
// pipe file descriptors
|
|
|
|
int pipefd[ 2 ];
|
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
int i;
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
// expands tables
|
|
|
|
// and removes nils
|
|
|
|
for( i = 1; i <= lua_gettop( L ); i++ )
|
|
|
|
{
|
|
|
|
if( lua_isnil( L, i ) )
|
|
|
|
{
|
|
|
|
lua_remove( L, i );
|
2012-02-16 07:28:40 +00:00
|
|
|
i--;
|
|
|
|
argc--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
if( lua_istable( L, i ) )
|
|
|
|
{
|
2012-02-15 11:15:32 +00:00
|
|
|
int tlen;
|
|
|
|
int it;
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_checkstack( L, lua_gettop( L ) + lua_objlen( L, i ) + 1 );
|
|
|
|
|
|
|
|
// moves table to top of stack
|
|
|
|
lua_pushvalue( L, i );
|
|
|
|
lua_remove( L, i );
|
2012-02-15 11:15:32 +00:00
|
|
|
argc--;
|
2012-10-07 17:48:23 +00:00
|
|
|
tlen = lua_objlen( L, -1 );
|
|
|
|
|
|
|
|
for( it = 1; it <= tlen; it++ )
|
|
|
|
{
|
|
|
|
lua_pushinteger( L, it );
|
|
|
|
lua_gettable( L, -2 );
|
|
|
|
lua_insert( L, i );
|
2012-02-15 11:15:32 +00:00
|
|
|
i++;
|
|
|
|
argc++;
|
2010-12-03 19:47:33 +00:00
|
|
|
}
|
2012-02-15 11:15:32 +00:00
|
|
|
i--;
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_pop( L, 1 );
|
2010-12-03 19:47:33 +00:00
|
|
|
}
|
|
|
|
}
|
2012-02-15 11:15:32 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
// writes a log message (if needed).
|
|
|
|
if( check_logcat( "Exec" ) <= settings.log_level )
|
|
|
|
{
|
|
|
|
lua_checkstack( L, lua_gettop( L ) + argc * 3 + 2 );
|
|
|
|
lua_pushvalue( L, 1 );
|
|
|
|
|
|
|
|
for( i = 1; i <= argc; i++ )
|
|
|
|
{
|
|
|
|
lua_pushstring( L, " [" );
|
2013-06-07 09:12:24 +00:00
|
|
|
lua_pushvalue( L, i + 1 );
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_pushstring( L, "]" );
|
2010-11-05 18:04:29 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
lua_concat( L, 3 * argc + 1 );
|
|
|
|
|
2013-06-07 09:12:24 +00:00
|
|
|
// replaces midfile 0 chars by linefeed
|
|
|
|
size_t len = 0;
|
|
|
|
const char * cs = lua_tolstring( L, -1, &len );
|
|
|
|
char * s = s_calloc( len + 1, sizeof( char ) );
|
|
|
|
|
|
|
|
for( i = 0; i < len; i++ )
|
|
|
|
{
|
|
|
|
s[ i ] = cs[ i ] ? cs[ i ] : '\n';
|
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
logstring0(
|
|
|
|
LOG_DEBUG, "Exec",
|
2013-06-07 09:12:24 +00:00
|
|
|
s
|
2012-10-07 17:48:23 +00:00
|
|
|
);
|
|
|
|
|
2013-06-07 09:12:24 +00:00
|
|
|
free( s );
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_pop( L, 1 );
|
2010-11-05 18:04:29 +00:00
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
if( argc >= 2 && !strcmp( luaL_checkstring( L, 2 ), "<" ) )
|
|
|
|
{
|
2012-02-15 12:47:58 +00:00
|
|
|
// pipes something into stdin
|
2012-10-07 17:48:23 +00:00
|
|
|
if( !lua_isstring( L, 3 ) )
|
|
|
|
{
|
|
|
|
logstring(
|
|
|
|
"Error",
|
|
|
|
"in spawn(), expected a string after pipe '<'"
|
|
|
|
);
|
|
|
|
|
2022-03-23 22:41:02 +00:00
|
|
|
safeexit(L, -1 );
|
2010-11-28 20:16:56 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
pipe_text = lua_tolstring( L, 3, &pipe_len );
|
|
|
|
|
|
|
|
if( strlen( pipe_text ) > 0 )
|
|
|
|
{
|
2012-02-15 12:47:58 +00:00
|
|
|
// creates the pipe
|
2012-10-07 17:48:23 +00:00
|
|
|
if( pipe( pipefd ) == -1 )
|
|
|
|
{
|
|
|
|
logstring( "Error", "cannot create a pipe!" );
|
|
|
|
|
2022-03-23 22:41:02 +00:00
|
|
|
safeexit(L, -1 );
|
2011-01-20 18:55:46 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
2012-02-15 12:47:58 +00:00
|
|
|
// always closes the write end for child processes
|
2012-10-07 17:48:23 +00:00
|
|
|
close_exec_fd( pipefd[ 1 ] );
|
|
|
|
|
2012-02-15 12:47:58 +00:00
|
|
|
// sets the write end on non-blocking
|
2012-10-07 17:48:23 +00:00
|
|
|
non_block_fd( pipefd[ 1 ] );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-01-20 18:55:46 +00:00
|
|
|
pipe_text = NULL;
|
2010-11-12 14:05:10 +00:00
|
|
|
}
|
|
|
|
argc -= 2;
|
|
|
|
li += 2;
|
|
|
|
}
|
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// prepares the arguments
|
2012-10-07 17:48:23 +00:00
|
|
|
argv = s_calloc( argc + 2, sizeof( char * ) );
|
2016-12-01 11:52:09 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
argv[ 0 ] = binary;
|
2016-12-01 11:52:09 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
for( i = 1; i <= argc; i++ )
|
2016-12-01 11:52:09 +00:00
|
|
|
{
|
|
|
|
argv[i] = luaL_checkstring( L, i + li );
|
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
argv[ i ] = NULL;
|
|
|
|
|
|
|
|
// the fork!
|
|
|
|
pid = fork( );
|
2010-10-17 20:26:37 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
if( pid == 0 )
|
|
|
|
{
|
2012-02-15 11:15:32 +00:00
|
|
|
// replaces stdin for pipes
|
2012-10-07 17:48:23 +00:00
|
|
|
if( pipe_text )
|
2016-12-01 11:52:09 +00:00
|
|
|
{
|
|
|
|
dup2( pipefd[ 0 ], STDIN_FILENO );
|
|
|
|
}
|
2012-02-15 11:15:32 +00:00
|
|
|
|
|
|
|
// if lsyncd runs as a daemon and has a logfile it will redirect
|
|
|
|
// stdout/stderr of child processes to the logfile.
|
2012-10-07 17:48:23 +00:00
|
|
|
if( is_daemon && settings.log_file )
|
|
|
|
{
|
|
|
|
if( !freopen( settings.log_file, "a", stdout ) )
|
|
|
|
{
|
|
|
|
printlogf(
|
|
|
|
L, "Error",
|
|
|
|
"cannot redirect stdout to '%s'.",
|
|
|
|
settings.log_file
|
|
|
|
);
|
2010-10-28 17:56:33 +00:00
|
|
|
}
|
2012-02-15 14:16:00 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
if( !freopen( settings.log_file, "a", stderr ) )
|
|
|
|
{
|
|
|
|
printlogf(
|
|
|
|
L, "Error",
|
|
|
|
"cannot redirect stderr to '%s'.",
|
|
|
|
settings.log_file
|
|
|
|
);
|
2010-10-28 17:56:33 +00:00
|
|
|
}
|
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
2021-12-02 16:01:37 +00:00
|
|
|
execvp( binary, ( char ** ) argv );
|
2012-10-07 17:48:23 +00:00
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// in a sane world execv does not return!
|
2012-10-07 17:48:23 +00:00
|
|
|
printlogf(
|
|
|
|
L, "Error",
|
|
|
|
"Failed executing [ %s ]!",
|
|
|
|
binary
|
|
|
|
);
|
|
|
|
|
|
|
|
exit( -1 );
|
2010-10-17 17:13:53 +00:00
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
if( pipe_text )
|
|
|
|
{
|
2010-11-12 15:39:43 +00:00
|
|
|
int len;
|
2012-10-07 17:48:23 +00:00
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// first closes read-end of pipe, this is for child process only
|
2012-10-07 17:48:23 +00:00
|
|
|
close( pipefd[ 0 ] );
|
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// starts filling the pipe
|
2012-10-07 17:48:23 +00:00
|
|
|
len = write( pipefd[ 1 ], pipe_text, pipe_len );
|
|
|
|
|
|
|
|
if( len < 0 )
|
|
|
|
{
|
|
|
|
logstring( "Normal", "immediatly broken pipe." );
|
|
|
|
close( pipefd[ 1 ] );
|
|
|
|
}
|
|
|
|
else if( len == pipe_len )
|
|
|
|
{
|
2012-02-15 11:15:32 +00:00
|
|
|
// usual and best case, the pipe accepted all input -> close
|
2012-10-07 17:48:23 +00:00
|
|
|
close( pipefd[ 1 ] );
|
|
|
|
logstring( "Exec", "one-sweeped pipe" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-11-22 20:09:52 +00:00
|
|
|
struct pipemsg *pm;
|
2012-10-07 17:48:23 +00:00
|
|
|
logstring( "Exec", "adding pipe observance" );
|
|
|
|
pm = s_calloc( 1, sizeof( struct pipemsg ) );
|
2013-06-06 16:23:50 +00:00
|
|
|
pm->text = s_calloc( pipe_len + 1, sizeof( char ) );
|
2012-10-07 17:48:23 +00:00
|
|
|
memcpy( pm->text, pipe_text, pipe_len + 1 );
|
2010-11-28 20:16:56 +00:00
|
|
|
pm->tlen = pipe_len;
|
2010-11-22 20:09:52 +00:00
|
|
|
pm->pos = len;
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
observe_fd(
|
|
|
|
pipefd[ 1 ],
|
|
|
|
NULL,
|
|
|
|
pipe_writey,
|
|
|
|
pipe_tidy,
|
|
|
|
pm
|
|
|
|
);
|
2010-11-12 15:39:43 +00:00
|
|
|
}
|
2010-11-12 14:05:10 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
free( argv );
|
|
|
|
lua_pushnumber( L, pid );
|
|
|
|
|
2010-10-18 09:02:51 +00:00
|
|
|
return 1;
|
2010-10-17 17:13:53 +00:00
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
| Converts a relative directory path to an absolute.
|
|
|
|
|
|
|
|
|
| Params on Lua stack:
|
|
|
|
| 1: a relative path to directory
|
|
|
|
|
|
|
|
|
| Returns on Lua stack:
|
|
|
|
| The absolute path of directory
|
|
|
|
*/
|
2010-10-17 15:24:55 +00:00
|
|
|
static int
|
2012-10-07 17:48:23 +00:00
|
|
|
l_realdir( lua_State *L )
|
2010-10-17 15:24:55 +00:00
|
|
|
{
|
2010-10-16 18:21:01 +00:00
|
|
|
luaL_Buffer b;
|
|
|
|
const char *rdir = luaL_checkstring(L, 1);
|
2012-02-15 13:42:24 +00:00
|
|
|
char *adir = get_realpath(rdir);
|
2012-02-15 11:15:32 +00:00
|
|
|
|
2016-05-03 08:45:22 +00:00
|
|
|
if( !adir )
|
|
|
|
{
|
|
|
|
printlogf(
|
|
|
|
L, "Error",
|
|
|
|
"failure getting absolute path of [%s]",
|
|
|
|
rdir
|
|
|
|
);
|
|
|
|
|
2010-10-16 18:21:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-02-15 12:47:58 +00:00
|
|
|
|
2010-10-16 18:21:01 +00:00
|
|
|
{
|
2012-02-15 11:15:32 +00:00
|
|
|
// makes sure its a directory
|
2010-10-16 18:21:01 +00:00
|
|
|
struct stat st;
|
2016-05-03 08:45:22 +00:00
|
|
|
if( stat( adir, &st ) )
|
|
|
|
{
|
|
|
|
printlogf(
|
|
|
|
L, "Error",
|
|
|
|
"cannot get absolute path of dir '%s': %s",
|
|
|
|
rdir,
|
|
|
|
strerror( errno )
|
|
|
|
);
|
|
|
|
|
|
|
|
free( adir );
|
|
|
|
|
2010-10-28 17:56:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-02-15 13:42:24 +00:00
|
|
|
|
2016-05-03 08:45:22 +00:00
|
|
|
if( !S_ISDIR( st.st_mode ) )
|
|
|
|
{
|
|
|
|
printlogf(
|
|
|
|
L, "Error",
|
|
|
|
"cannot get absolute path of dir '%s': is not a directory",
|
|
|
|
rdir
|
|
|
|
);
|
|
|
|
|
|
|
|
free( adir );
|
|
|
|
|
2010-10-16 18:21:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// returns absolute path with a concated '/'
|
2016-05-03 08:45:22 +00:00
|
|
|
luaL_buffinit( L, &b );
|
|
|
|
luaL_addstring( &b, adir );
|
|
|
|
luaL_addchar( &b, '/' );
|
|
|
|
luaL_pushresult( &b );
|
|
|
|
|
|
|
|
free( adir );
|
2012-02-15 13:42:24 +00:00
|
|
|
|
2010-10-16 18:21:01 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*
|
|
|
|
| Dumps the Lua stack.
|
|
|
|
| For debugging purposes.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
l_stackdump( lua_State * L )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int top = lua_gettop( L );
|
|
|
|
|
|
|
|
printlogf(
|
|
|
|
L, "Debug",
|
|
|
|
"total in stack %d",
|
|
|
|
top
|
|
|
|
);
|
|
|
|
|
|
|
|
for( i = 1; i <= top; i++ )
|
|
|
|
{
|
|
|
|
int t = lua_type( L, i );
|
|
|
|
switch( t )
|
|
|
|
{
|
|
|
|
case LUA_TSTRING:
|
2016-05-03 08:45:22 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
printlogf(
|
|
|
|
L, "Debug",
|
|
|
|
"%d string: '%s'",
|
|
|
|
i, lua_tostring( L, i )
|
|
|
|
);
|
2016-05-03 08:45:22 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LUA_TBOOLEAN:
|
2016-05-03 08:45:22 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
printlogf(
|
|
|
|
L, "Debug",
|
|
|
|
"%d boolean %s",
|
|
|
|
i, lua_toboolean( L, i ) ? "true" : "false"
|
|
|
|
);
|
2016-05-03 08:45:22 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LUA_TNUMBER:
|
2016-05-03 08:45:22 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
printlogf(
|
|
|
|
L, "Debug",
|
|
|
|
"%d number: %g",
|
|
|
|
i, lua_tonumber( L, i )
|
|
|
|
);
|
2016-05-03 08:45:22 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2016-05-03 08:45:22 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
printlogf(
|
|
|
|
L, "Debug",
|
|
|
|
"%d %s",
|
|
|
|
i, lua_typename( L, t )
|
|
|
|
);
|
2016-05-03 08:45:22 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
break;
|
2010-10-17 15:24:55 +00:00
|
|
|
}
|
2010-10-17 20:26:37 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
2010-10-17 15:24:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*
|
|
|
|
| Reads the directories entries.
|
|
|
|
|
|
|
|
|
| Params on Lua stack:
|
|
|
|
| 1: absolute path to directory
|
|
|
|
|
|
|
|
|
| Returns on Lua stack:
|
|
|
|
| a table of directory names.
|
|
|
|
| names are keys
|
|
|
|
| values are boolean true on dirs.
|
|
|
|
*/
|
2010-10-17 15:24:55 +00:00
|
|
|
static int
|
2016-12-05 14:11:00 +00:00
|
|
|
l_readdir( lua_State *L )
|
2010-10-17 15:24:55 +00:00
|
|
|
{
|
2012-10-07 17:48:23 +00:00
|
|
|
const char * dirname = luaL_checkstring( L, 1 );
|
2016-12-05 14:11:00 +00:00
|
|
|
|
2010-10-16 18:21:01 +00:00
|
|
|
DIR *d;
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
d = opendir( dirname );
|
2016-05-03 08:45:22 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
if( d == NULL )
|
|
|
|
{
|
|
|
|
printlogf(
|
|
|
|
L, "Error", "cannot open dir [%s].",
|
|
|
|
dirname
|
|
|
|
);
|
|
|
|
|
2010-10-17 20:26:37 +00:00
|
|
|
return 0;
|
2010-10-16 18:21:01 +00:00
|
|
|
}
|
2012-02-15 11:15:32 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_newtable( L );
|
|
|
|
|
|
|
|
while( !hup && !term )
|
|
|
|
{
|
|
|
|
struct dirent *de = readdir( d );
|
2010-10-16 18:21:01 +00:00
|
|
|
bool isdir;
|
2012-10-07 17:48:23 +00:00
|
|
|
|
2016-12-05 14:11:00 +00:00
|
|
|
if( de == NULL ) // finished
|
2012-10-07 17:48:23 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2010-11-17 18:52:55 +00:00
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// ignores . and ..
|
2012-10-07 17:48:23 +00:00
|
|
|
if(
|
2016-12-05 14:11:00 +00:00
|
|
|
!strcmp( de->d_name, "." )
|
|
|
|
|| !strcmp( de->d_name, ".." )
|
2012-10-07 17:48:23 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2010-11-17 18:52:55 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
if( de->d_type == DT_UNKNOWN )
|
|
|
|
{
|
2012-02-15 11:15:32 +00:00
|
|
|
// must call stat on some systems :-/
|
2012-10-07 17:48:23 +00:00
|
|
|
// ( e.g. ReiserFS )
|
|
|
|
char *entry = s_malloc(
|
|
|
|
strlen( dirname ) +
|
|
|
|
strlen( de->d_name ) +
|
|
|
|
2
|
|
|
|
);
|
2016-05-03 08:45:22 +00:00
|
|
|
|
2010-10-16 18:21:01 +00:00
|
|
|
struct stat st;
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
strcpy( entry, dirname );
|
|
|
|
strcat( entry, "/" );
|
|
|
|
strcat( entry, de->d_name );
|
|
|
|
|
|
|
|
lstat( entry, &st );
|
|
|
|
|
|
|
|
isdir = S_ISDIR( st.st_mode );
|
|
|
|
|
|
|
|
free( entry );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// otherwise readdir can be trusted
|
2010-10-16 18:21:01 +00:00
|
|
|
isdir = de->d_type == DT_DIR;
|
|
|
|
}
|
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// adds this entry to the Lua table
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_pushstring( L, de->d_name );
|
|
|
|
lua_pushboolean( L, isdir );
|
|
|
|
lua_settable( L, -3 );
|
2010-10-16 18:21:01 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
closedir( d );
|
2016-05-03 08:45:22 +00:00
|
|
|
|
2010-10-16 18:21:01 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2010-10-16 10:26:48 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
| Terminates Lsyncd.
|
|
|
|
|
|
|
|
|
| Params on Lua stack:
|
|
|
|
| 1: exitcode of Lsyncd.
|
|
|
|
|
|
|
|
|
| Does not return.
|
|
|
|
|
|
|
|
|
*/
|
2012-02-15 11:15:32 +00:00
|
|
|
int
|
2016-12-05 14:11:00 +00:00
|
|
|
l_terminate( lua_State *L )
|
2010-10-18 12:23:46 +00:00
|
|
|
{
|
2022-06-03 05:07:08 +00:00
|
|
|
int exitcode = luaL_checkinteger( L, 1 );
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
exit( exitcode );
|
|
|
|
|
2010-10-18 12:23:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*
|
|
|
|
| Configures core parameters.
|
|
|
|
|
|
|
|
|
| Params on Lua stack:
|
|
|
|
| 1: a string, configure option
|
|
|
|
| 2: depends on Param 1
|
|
|
|
*/
|
2012-02-15 11:15:32 +00:00
|
|
|
static int
|
2012-10-07 17:48:23 +00:00
|
|
|
l_configure( lua_State *L )
|
2010-10-27 19:34:56 +00:00
|
|
|
{
|
2012-10-07 17:48:23 +00:00
|
|
|
const char * command = luaL_checkstring( L, 1 );
|
2016-12-01 11:52:09 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
if( !strcmp( command, "running" ) )
|
|
|
|
{
|
2012-02-15 11:15:32 +00:00
|
|
|
// set by runner after first initialize
|
|
|
|
// from this on log to configurated log end instead of
|
|
|
|
// stdout/stderr
|
2011-08-29 09:21:40 +00:00
|
|
|
first_time = false;
|
2012-02-15 13:42:24 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
if( !settings.nodaemon && !settings.log_file )
|
|
|
|
{
|
2012-02-15 13:42:24 +00:00
|
|
|
settings.log_syslog = true;
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
const char * log_ident =
|
2016-12-02 15:24:07 +00:00
|
|
|
settings.log_ident
|
|
|
|
? settings.log_ident
|
|
|
|
: "lsyncd";
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
openlog( log_ident, 0, settings.log_facility );
|
2011-03-01 14:57:26 +00:00
|
|
|
}
|
2012-02-15 13:42:24 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
if( !settings.nodaemon && !is_daemon )
|
|
|
|
{
|
2016-12-02 15:24:07 +00:00
|
|
|
logstring( "Normal", "--- Startup, daemonizing ---" );
|
|
|
|
|
2017-01-09 12:13:05 +00:00
|
|
|
daemonize( L, settings.pidfile );
|
2010-11-13 21:50:21 +00:00
|
|
|
}
|
2016-12-02 15:24:07 +00:00
|
|
|
else
|
2012-10-07 17:48:23 +00:00
|
|
|
{
|
2016-12-02 15:24:07 +00:00
|
|
|
logstring( "Normal", "--- Startup ---" );
|
2012-10-07 17:48:23 +00:00
|
|
|
}
|
2012-02-15 13:42:24 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
}
|
|
|
|
else if( !strcmp( command, "nodaemon" ) )
|
|
|
|
{
|
2010-11-13 21:50:21 +00:00
|
|
|
settings.nodaemon = true;
|
2012-10-07 17:48:23 +00:00
|
|
|
}
|
|
|
|
else if( !strcmp( command, "logfile" ) )
|
|
|
|
{
|
|
|
|
const char * file = luaL_checkstring( L, 2 );
|
|
|
|
|
|
|
|
if( settings.log_file )
|
2013-07-30 10:20:23 +00:00
|
|
|
{
|
|
|
|
free( settings.log_file );
|
|
|
|
}
|
2012-02-15 13:42:24 +00:00
|
|
|
|
2013-07-30 10:20:23 +00:00
|
|
|
settings.log_file =
|
|
|
|
s_strdup( file );
|
2012-10-07 17:48:23 +00:00
|
|
|
}
|
|
|
|
else if( !strcmp( command, "pidfile" ) )
|
|
|
|
{
|
|
|
|
const char * file = luaL_checkstring( L, 2 );
|
2012-02-15 13:42:24 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
if( settings.pidfile )
|
2013-07-30 10:20:23 +00:00
|
|
|
{
|
|
|
|
free( settings.pidfile );
|
|
|
|
}
|
2012-02-15 13:42:24 +00:00
|
|
|
|
2016-12-02 15:24:07 +00:00
|
|
|
settings.pidfile = s_strdup( file );
|
2012-10-07 17:48:23 +00:00
|
|
|
}
|
|
|
|
else if( !strcmp( command, "logfacility" ) )
|
|
|
|
{
|
|
|
|
if( lua_isstring( L, 2 ) )
|
|
|
|
{
|
|
|
|
const char * fname = luaL_checkstring( L, 2 );
|
2011-03-01 14:57:26 +00:00
|
|
|
int i;
|
2012-10-07 17:48:23 +00:00
|
|
|
for( i = 0; facilitynames[ i ].c_name; i++ )
|
|
|
|
{
|
|
|
|
if( !strcasecmp( fname, facilitynames[ i ].c_name ) )
|
|
|
|
{ break; }
|
2012-02-15 11:15:32 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
if( !facilitynames[ i ].c_name )
|
|
|
|
{
|
|
|
|
printlogf(
|
|
|
|
L, "Error",
|
|
|
|
"Logging facility '%s' unknown.",
|
|
|
|
fname
|
|
|
|
);
|
|
|
|
|
|
|
|
exit( -1 );
|
2011-03-01 14:57:26 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
settings.log_facility = facilitynames[ i ].c_val;
|
|
|
|
}
|
|
|
|
else if (lua_isnumber(L, 2))
|
|
|
|
{
|
2011-03-01 14:57:26 +00:00
|
|
|
settings.log_facility = luaL_checknumber(L, 2);
|
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
printlogf(
|
|
|
|
L, "Error",
|
|
|
|
"Logging facility must be a number or string"
|
|
|
|
);
|
|
|
|
|
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( !strcmp( command, "logident" ) )
|
|
|
|
{
|
|
|
|
const char * ident = luaL_checkstring( L, 2 );
|
2012-02-15 13:42:24 +00:00
|
|
|
|
2016-12-02 15:24:07 +00:00
|
|
|
if( settings.log_ident )
|
|
|
|
{
|
|
|
|
free( settings.log_ident );
|
|
|
|
}
|
2012-02-15 13:42:24 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
settings.log_ident = s_strdup( ident );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printlogf(
|
|
|
|
L, "Error",
|
|
|
|
"Internal error, unknown parameter in l_configure( %s )",
|
|
|
|
command
|
|
|
|
);
|
|
|
|
|
|
|
|
exit( -1 );
|
2010-10-27 19:34:56 +00:00
|
|
|
}
|
2012-02-15 13:42:24 +00:00
|
|
|
|
2010-10-27 19:34:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*
|
|
|
|
| Allows user scripts to observe filedescriptors
|
|
|
|
|
|
|
|
|
| Params on Lua stack:
|
|
|
|
| 1: file descriptor
|
|
|
|
| 2: function to call when read becomes ready
|
|
|
|
| 3: function to call when write becomes ready
|
|
|
|
*/
|
2012-02-15 11:15:32 +00:00
|
|
|
static int
|
2012-10-07 17:48:23 +00:00
|
|
|
l_observe_fd( lua_State *L )
|
2010-12-01 12:19:17 +00:00
|
|
|
{
|
2012-10-07 17:48:23 +00:00
|
|
|
int fd = luaL_checknumber( L, 1 );
|
2010-12-01 12:19:17 +00:00
|
|
|
bool ready = false;
|
|
|
|
bool writey = false;
|
2012-10-07 17:48:23 +00:00
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// Stores the user function in the lua registry.
|
|
|
|
// It uses the address of the cores ready/write functions
|
|
|
|
// for the user as key
|
2012-10-07 17:48:23 +00:00
|
|
|
if( !lua_isnoneornil( L, 2 ) )
|
|
|
|
{
|
|
|
|
lua_pushlightuserdata( L, (void *) user_obs_ready );
|
|
|
|
|
|
|
|
lua_gettable( L, LUA_REGISTRYINDEX );
|
|
|
|
|
|
|
|
if( lua_isnil( L, -1 ) )
|
|
|
|
{
|
|
|
|
lua_pop ( L, 1 );
|
|
|
|
lua_newtable ( L );
|
|
|
|
lua_pushlightuserdata ( L, (void *) user_obs_ready );
|
|
|
|
lua_pushvalue ( L, -2 );
|
|
|
|
lua_settable ( L, LUA_REGISTRYINDEX );
|
2010-12-01 12:19:17 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
lua_pushnumber ( L, fd );
|
|
|
|
lua_pushvalue ( L, 2 );
|
|
|
|
lua_settable ( L, -3 );
|
|
|
|
lua_pop ( L, 1 );
|
|
|
|
|
2010-12-01 12:19:17 +00:00
|
|
|
ready = true;
|
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
if( !lua_isnoneornil( L, 3 ) )
|
|
|
|
{
|
|
|
|
lua_pushlightuserdata( L, (void *) user_obs_writey );
|
|
|
|
|
|
|
|
lua_gettable (L, LUA_REGISTRYINDEX );
|
|
|
|
|
|
|
|
if( lua_isnil(L, -1) )
|
|
|
|
{
|
|
|
|
lua_pop ( L, 1 );
|
|
|
|
lua_newtable ( L );
|
|
|
|
lua_pushlightuserdata ( L, (void *) user_obs_writey );
|
|
|
|
lua_pushvalue ( L, -2 );
|
|
|
|
lua_settable ( L, LUA_REGISTRYINDEX );
|
2010-12-01 12:19:17 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
lua_pushnumber ( L, fd );
|
|
|
|
lua_pushvalue ( L, 3 );
|
|
|
|
lua_settable ( L, -3 );
|
|
|
|
lua_pop ( L, 1 );
|
|
|
|
|
2010-12-01 12:19:17 +00:00
|
|
|
writey = true;
|
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
// tells the core to watch the fd
|
|
|
|
observe_fd(
|
|
|
|
fd,
|
|
|
|
ready ? user_obs_ready : NULL,
|
|
|
|
writey ? user_obs_writey : NULL,
|
|
|
|
user_obs_tidy,
|
|
|
|
NULL
|
|
|
|
);
|
|
|
|
|
2010-12-01 12:19:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*
|
|
|
|
| Removes a user observance
|
|
|
|
|
|
|
|
|
| Params on Lua stack:
|
|
|
|
| 1: exitcode of Lsyncd.
|
|
|
|
*/
|
2010-12-01 12:19:17 +00:00
|
|
|
extern int
|
2012-10-07 17:48:23 +00:00
|
|
|
l_nonobserve_fd( lua_State *L )
|
2010-12-01 12:19:17 +00:00
|
|
|
{
|
2012-10-07 17:48:23 +00:00
|
|
|
int fd = luaL_checknumber( L, 1 );
|
2010-12-01 12:19:17 +00:00
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// removes the read function
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_pushlightuserdata( L, (void *) user_obs_ready );
|
|
|
|
lua_gettable( L, LUA_REGISTRYINDEX );
|
|
|
|
|
|
|
|
if( !lua_isnil( L, -1 ) )
|
|
|
|
{
|
|
|
|
lua_pushnumber ( L, fd );
|
|
|
|
lua_pushnil ( L );
|
|
|
|
lua_settable ( L, -2 );
|
2010-12-01 12:19:17 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_pop( L, 1 );
|
2012-02-15 11:15:32 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_pushlightuserdata( L, (void *) user_obs_writey );
|
|
|
|
lua_gettable( L, LUA_REGISTRYINDEX );
|
|
|
|
if ( !lua_isnil( L, -1 ) )
|
|
|
|
{
|
|
|
|
lua_pushnumber ( L, fd );
|
|
|
|
lua_pushnil ( L );
|
|
|
|
lua_settable ( L, -2 );
|
2010-12-01 12:19:17 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_pop( L, 1 );
|
2010-12-01 12:19:17 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
nonobserve_fd( fd );
|
2010-12-01 12:19:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:55:07 +00:00
|
|
|
/*
|
|
|
|
| Returns file size of given path or nil on error
|
|
|
|
|
|
|
|
|
| Params on Lua stack:
|
|
|
|
| 1: path of filename
|
|
|
|
*/
|
|
|
|
extern int
|
|
|
|
l_file_size( lua_State *L )
|
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
const char *filename = luaL_checkstring( L, 1 );
|
|
|
|
|
|
|
|
if (stat(filename, &sb) == -1) {
|
|
|
|
lua_pushnil(L);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
lua_pushinteger(L, (long long) sb.st_size);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-25 18:01:19 +00:00
|
|
|
static int l_jiffies_fromseconds(lua_State *L);
|
2012-10-07 17:48:23 +00:00
|
|
|
/*
|
|
|
|
| The Lsnycd's core library
|
|
|
|
*/
|
|
|
|
static const luaL_Reg lsyncdlib[] =
|
2022-03-25 18:01:19 +00:00
|
|
|
{
|
|
|
|
{ "configure", l_configure },
|
|
|
|
{ "exec", l_exec },
|
|
|
|
{ "log", l_log },
|
|
|
|
{ "now", l_now },
|
|
|
|
{ "jiffies_from_seconds", l_jiffies_fromseconds},
|
|
|
|
{ "kill", l_kill },
|
|
|
|
{ "get_free_port", l_free_port },
|
|
|
|
{ "nonobserve_fd", l_nonobserve_fd },
|
|
|
|
{ "observe_fd", l_observe_fd },
|
|
|
|
{ "readdir", l_readdir },
|
|
|
|
{ "realdir", l_realdir },
|
|
|
|
{ "stackdump", l_stackdump },
|
|
|
|
{ "terminate", l_terminate },
|
2022-04-14 11:55:07 +00:00
|
|
|
{ "get_file_size", l_file_size },
|
2022-03-25 18:01:19 +00:00
|
|
|
{ NULL, NULL }
|
2010-10-19 20:14:55 +00:00
|
|
|
};
|
|
|
|
|
2016-05-03 08:45:22 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*
|
|
|
|
| Adds a number in seconds to a jiffy timestamp.
|
|
|
|
*/
|
2012-02-15 11:15:32 +00:00
|
|
|
static int
|
2012-10-07 17:48:23 +00:00
|
|
|
l_jiffies_add( lua_State *L )
|
2010-11-29 10:56:39 +00:00
|
|
|
{
|
2012-10-07 17:48:23 +00:00
|
|
|
clock_t *p1 = ( clock_t * ) lua_touserdata( L, 1 );
|
|
|
|
clock_t *p2 = ( clock_t * ) lua_touserdata( L, 2 );
|
|
|
|
|
|
|
|
if( p1 && p2 )
|
|
|
|
{
|
|
|
|
logstring( "Error", "Cannot add two timestamps!" );
|
2022-03-23 22:41:02 +00:00
|
|
|
safeexit(L, -1 );
|
2012-10-07 17:48:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
clock_t a1 =
|
|
|
|
p1 ? *p1 : luaL_checknumber( L, 1 ) * clocks_per_sec;
|
|
|
|
|
|
|
|
clock_t a2 =
|
|
|
|
p2 ? *p2 : luaL_checknumber( L, 2 ) * clocks_per_sec;
|
|
|
|
|
|
|
|
clock_t *r =
|
|
|
|
( clock_t * ) lua_newuserdata( L, sizeof( clock_t ) );
|
|
|
|
|
|
|
|
luaL_getmetatable( L, "Lsyncd.jiffies" );
|
|
|
|
lua_setmetatable( L, -2 );
|
2012-02-15 11:15:32 +00:00
|
|
|
*r = a1 + a2;
|
2010-11-29 11:37:46 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2010-11-29 10:56:39 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 01:25:27 +00:00
|
|
|
/*
|
|
|
|
| Adds a number in seconds to a jiffy timestamp.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
l_jiffies_concat( lua_State *L )
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
clock_t *p1 = ( clock_t * ) lua_touserdata( L, 1 );
|
|
|
|
clock_t *p2 = ( clock_t * ) lua_touserdata( L, 2 );
|
|
|
|
|
|
|
|
if( p1 && p2 )
|
|
|
|
{
|
|
|
|
logstring( "Error", "Cannot add two timestamps!" );
|
2022-03-23 22:41:02 +00:00
|
|
|
safeexit(L, -1 );
|
2022-03-17 01:25:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
if (p1) {
|
|
|
|
snprintf( buf, sizeof(buf), "%Lf", (long double)(*p1));
|
|
|
|
lua_pushfstring(L, "%s%s", &buf, luaL_checkstring( L, 2));
|
|
|
|
} else {
|
|
|
|
snprintf( buf, sizeof(buf), "%Lf", (long double)(*p2));
|
|
|
|
lua_pushfstring(L, "%s%s", luaL_checkstring( L, 1), &buf);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2016-05-03 08:45:22 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*
|
|
|
|
| Subracts two jiffy timestamps resulting in a number in seconds
|
|
|
|
| or substracts a jiffy by a number in seconds resulting a jiffy timestamp.
|
|
|
|
*/
|
2012-02-15 11:15:32 +00:00
|
|
|
static int
|
2012-10-07 17:48:23 +00:00
|
|
|
l_jiffies_sub( lua_State *L )
|
2010-11-29 10:56:39 +00:00
|
|
|
{
|
2012-10-07 17:48:23 +00:00
|
|
|
clock_t *p1 = ( clock_t * ) lua_touserdata( L, 1 );
|
|
|
|
clock_t *p2 = ( clock_t * ) lua_touserdata( L, 2 );
|
|
|
|
|
|
|
|
if( p1 && p2 )
|
|
|
|
{
|
2012-02-15 11:15:32 +00:00
|
|
|
// substracting two timestamps result in a timespan in seconds
|
2010-11-29 11:37:46 +00:00
|
|
|
clock_t a1 = *p1;
|
|
|
|
clock_t a2 = *p2;
|
|
|
|
lua_pushnumber(L, ((double) (a1 -a2)) / clocks_per_sec);
|
|
|
|
return 1;
|
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// makes a timestamp earlier by NUMBER seconds
|
2012-10-07 17:48:23 +00:00
|
|
|
clock_t a1 = p1 ? *p1 : luaL_checknumber( L, 1 ) * clocks_per_sec;
|
|
|
|
clock_t a2 = p2 ? *p2 : luaL_checknumber( L, 2 ) * clocks_per_sec;
|
|
|
|
|
|
|
|
clock_t *r = (clock_t *) lua_newuserdata( L, sizeof( clock_t ) );
|
|
|
|
luaL_getmetatable( L, "Lsyncd.jiffies" );
|
|
|
|
lua_setmetatable( L, -2 );
|
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
*r = a1 - a2;
|
2012-10-07 17:48:23 +00:00
|
|
|
|
2010-11-29 10:56:39 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-05-03 08:45:22 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*
|
|
|
|
| Compares two jiffy timestamps
|
|
|
|
*/
|
2012-02-15 11:15:32 +00:00
|
|
|
static int
|
2012-10-07 17:48:23 +00:00
|
|
|
l_jiffies_eq( lua_State *L )
|
2010-11-29 10:56:39 +00:00
|
|
|
{
|
2012-10-07 17:48:23 +00:00
|
|
|
clock_t a1 = ( *( clock_t * ) luaL_checkudata( L, 1, "Lsyncd.jiffies" ) );
|
|
|
|
clock_t a2 = ( *( clock_t * ) luaL_checkudata( L, 2, "Lsyncd.jiffies" ) );
|
|
|
|
|
|
|
|
lua_pushboolean( L, a1 == a2 );
|
|
|
|
|
2010-11-29 10:56:39 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-05-03 08:45:22 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*
|
|
|
|
* True if jiffy1 timestamp is eariler than jiffy2 timestamp
|
|
|
|
*/
|
2012-02-15 11:15:32 +00:00
|
|
|
static int
|
2012-10-07 17:48:23 +00:00
|
|
|
l_jiffies_lt( lua_State *L )
|
2010-11-29 10:56:39 +00:00
|
|
|
{
|
2012-10-07 17:48:23 +00:00
|
|
|
clock_t a1 = ( *( clock_t * ) luaL_checkudata( L, 1, "Lsyncd.jiffies" ) );
|
|
|
|
clock_t a2 = ( *( clock_t * ) luaL_checkudata( L, 2, "Lsyncd.jiffies" ) );
|
|
|
|
|
|
|
|
lua_pushboolean( L, time_before( a1, a2 ) );
|
|
|
|
|
2010-11-29 10:56:39 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-05-03 08:45:22 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*
|
|
|
|
| True if jiffy1 before or equals jiffy2
|
|
|
|
*/
|
2012-02-15 11:15:32 +00:00
|
|
|
static int
|
|
|
|
l_jiffies_le(lua_State *L)
|
2010-11-29 10:56:39 +00:00
|
|
|
{
|
2012-10-07 17:48:23 +00:00
|
|
|
clock_t a1 = ( *( clock_t * ) luaL_checkudata( L, 1, "Lsyncd.jiffies" ) );
|
|
|
|
clock_t a2 = ( *( clock_t * ) luaL_checkudata( L, 2, "Lsyncd.jiffies" ) );
|
|
|
|
|
|
|
|
lua_pushboolean( L, ( a1 == a2 ) || time_before( a1, a2 ) );
|
2010-11-29 10:56:39 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-03-25 18:01:19 +00:00
|
|
|
/*
|
|
|
|
| Converts a jiffies to a number
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
l_jiffies_tonumber(lua_State *L)
|
|
|
|
{
|
|
|
|
clock_t a1 = ( *( clock_t * ) luaL_checkudata( L, 1, "Lsyncd.jiffies" ) );
|
|
|
|
|
|
|
|
lua_pushinteger( L, a1 * clocks_per_sec);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
| Converts a jiffies to a number
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
l_jiffies_fromseconds(lua_State *L)
|
|
|
|
{
|
|
|
|
lua_Integer a1 = luaL_checkinteger(L, 1);
|
|
|
|
|
|
|
|
clock_t *r = (clock_t *) lua_newuserdata( L, sizeof( clock_t ) );
|
|
|
|
luaL_getmetatable( L, "Lsyncd.jiffies" );
|
|
|
|
lua_setmetatable( L, -2 );
|
|
|
|
|
|
|
|
*r = a1 * clocks_per_sec;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
| Converts a jiffies to a number
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
l_jiffies_index(lua_State *L)
|
|
|
|
{
|
|
|
|
clock_t a1 = ( *( clock_t * ) luaL_checkudata( L, 1, "Lsyncd.jiffies" ) );
|
|
|
|
const char *a2 = luaL_checkstring(L, 2);
|
|
|
|
|
|
|
|
if (!strcmp(a2, "seconds")) {
|
|
|
|
lua_pushinteger( L, a1 / clocks_per_sec);
|
|
|
|
return 1;
|
2022-03-30 21:02:03 +00:00
|
|
|
} else if (!strcmp(a2, "string")) {
|
|
|
|
lua_pushfstring (L, "%d", a1 / clocks_per_sec);
|
|
|
|
return 1;
|
2022-03-25 18:01:19 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-11-29 10:56:39 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*
|
|
|
|
| Registers the Lsyncd's core library.
|
|
|
|
*/
|
2010-11-29 10:56:39 +00:00
|
|
|
void
|
2012-10-07 17:48:23 +00:00
|
|
|
register_lsyncd( lua_State *L )
|
2010-11-29 10:56:39 +00:00
|
|
|
{
|
2016-11-24 14:44:08 +00:00
|
|
|
lua_compat_register( L, LSYNCD_LIBNAME, lsyncdlib );
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_setglobal( L, LSYNCD_LIBNAME );
|
2010-11-29 10:56:39 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
// creates the metatable for the jiffies ( timestamps ) userdata
|
|
|
|
luaL_newmetatable( L, "Lsyncd.jiffies" );
|
|
|
|
int mt = lua_gettop( L );
|
2012-10-01 20:58:25 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_pushcfunction( L, l_jiffies_add );
|
|
|
|
lua_setfield( L, mt, "__add" );
|
2010-11-29 10:56:39 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_pushcfunction( L, l_jiffies_sub );
|
|
|
|
lua_setfield( L, mt, "__sub" );
|
2012-02-15 11:15:32 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_pushcfunction( L, l_jiffies_lt );
|
|
|
|
lua_setfield( L, mt, "__lt" );
|
2012-02-15 11:15:32 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_pushcfunction( L, l_jiffies_le );
|
|
|
|
lua_setfield( L, mt, "__le" );
|
2012-02-15 11:15:32 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_pushcfunction( L, l_jiffies_eq );
|
|
|
|
lua_setfield( L, mt, "__eq" );
|
2012-10-01 20:58:25 +00:00
|
|
|
|
2022-03-17 01:25:27 +00:00
|
|
|
lua_pushcfunction( L, l_jiffies_concat );
|
|
|
|
lua_setfield( L, mt, "__concat" );
|
|
|
|
|
2022-03-25 18:01:19 +00:00
|
|
|
lua_pushcfunction( L, l_jiffies_index );
|
|
|
|
lua_setfield( L, mt, "__index" );
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_pop( L, 1 ); // pop(mt)
|
2012-02-15 11:15:32 +00:00
|
|
|
|
2014-04-29 14:11:27 +00:00
|
|
|
#ifdef WITH_INOTIFY
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
lua_getglobal( L, LSYNCD_LIBNAME );
|
|
|
|
register_inotify( L );
|
|
|
|
lua_setfield( L, -2, LSYNCD_INOTIFYLIBNAME );
|
|
|
|
lua_pop( L, 1 );
|
|
|
|
|
2012-10-01 20:54:52 +00:00
|
|
|
#endif
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
if( lua_gettop( L ) )
|
|
|
|
{
|
|
|
|
logstring(
|
|
|
|
"Error",
|
|
|
|
"internal, stack not empty in lsyncd_register( )"
|
|
|
|
);
|
|
|
|
|
|
|
|
exit( -1 );
|
2010-11-29 10:56:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
|
|
|
|
( Lsyncd Core )
|
|
|
|
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
|
2010-11-29 10:56:39 +00:00
|
|
|
|
2010-10-19 20:14:55 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
/*
|
|
|
|
| Pushes a function from the runner on the stack.
|
|
|
|
| As well as the callError handler.
|
|
|
|
*/
|
2010-11-22 20:09:52 +00:00
|
|
|
extern void
|
2012-10-07 17:48:23 +00:00
|
|
|
load_runner_func(
|
|
|
|
lua_State * L,
|
|
|
|
const char * name
|
|
|
|
)
|
2010-11-10 15:57:37 +00:00
|
|
|
{
|
2012-10-07 17:48:23 +00:00
|
|
|
printlogf( L, "Call", "%s( )", name );
|
2012-02-15 11:15:32 +00:00
|
|
|
|
|
|
|
// pushes the error handler
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_pushlightuserdata( L, (void *) &callError );
|
|
|
|
lua_gettable( L, LUA_REGISTRYINDEX );
|
2012-02-15 11:15:32 +00:00
|
|
|
|
|
|
|
// pushes the function
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_pushlightuserdata( L, (void *) &runner );
|
|
|
|
lua_gettable( L, LUA_REGISTRYINDEX );
|
|
|
|
lua_pushstring( L, name );
|
|
|
|
lua_gettable( L, -2 );
|
|
|
|
lua_remove( L, -2 );
|
2010-11-10 15:57:37 +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
|
|
|
/*
|
|
|
|
| Daemonizes.
|
|
|
|
|
|
|
|
|
| Lsyncds own implementation over daemon(0, 0) since
|
|
|
|
| a) OSX keeps bugging about it being deprecated
|
|
|
|
| b) for a reason, since blindly closing stdin/out/err
|
|
|
|
| is unsafe, since they might not have existed and
|
|
|
|
| might actually close the monitors fd!
|
|
|
|
*/
|
2010-11-29 22:34:58 +00:00
|
|
|
static void
|
2017-01-09 12:13:05 +00:00
|
|
|
daemonize(
|
|
|
|
lua_State *L, // the lua state
|
|
|
|
const char *pidfile // if not NULL write pidfile
|
|
|
|
)
|
2010-11-29 22:34:58 +00:00
|
|
|
{
|
|
|
|
pid_t pid, sid;
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
pid = fork( );
|
|
|
|
|
|
|
|
if( pid < 0 )
|
|
|
|
{
|
|
|
|
printlogf(
|
|
|
|
L, "Error",
|
|
|
|
"Failure in daemonize at fork: %s",
|
|
|
|
strerror( errno )
|
|
|
|
);
|
|
|
|
|
|
|
|
exit( -1 );
|
|
|
|
}
|
2012-02-15 11:15:32 +00:00
|
|
|
|
2017-01-09 12:13:05 +00:00
|
|
|
if( pid > 0 )
|
2012-10-07 17:48:23 +00:00
|
|
|
{
|
|
|
|
// parent process returns to shell
|
|
|
|
exit( 0 );
|
2010-11-29 22:34:58 +00:00
|
|
|
}
|
2012-02-15 11:15:32 +00:00
|
|
|
|
2017-01-09 12:13:05 +00:00
|
|
|
if( pidfile )
|
|
|
|
{
|
|
|
|
write_pidfile( L, pidfile );
|
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
// detaches the new process from the parent process
|
|
|
|
sid = setsid( );
|
2017-01-09 12:13:05 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
if( sid < 0 )
|
|
|
|
{
|
|
|
|
printlogf(
|
|
|
|
L, "Error",
|
|
|
|
"Failure in daemonize at setsid: %s",
|
|
|
|
strerror( errno )
|
|
|
|
);
|
2012-02-15 11:15:32 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
exit( -1 );
|
2010-11-29 22:34:58 +00:00
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
// goes to root dir
|
|
|
|
if( chdir( "/" ) < 0 )
|
|
|
|
{
|
|
|
|
printlogf(
|
|
|
|
L, "Error",
|
|
|
|
"Failure in daemonize at chdir( \"/\" ): %s",
|
|
|
|
strerror( errno )
|
|
|
|
);
|
|
|
|
|
|
|
|
exit( -1 );
|
2010-11-29 22:34:58 +00:00
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
// does what clibs daemon( 0, 0 ) cannot do,
|
|
|
|
// checks if there were no stdstreams and it might close used fds
|
|
|
|
if( observances_len && observances->fd < 3 )
|
|
|
|
{
|
|
|
|
printlogf(
|
|
|
|
L, "Normal",
|
|
|
|
"daemonize not closing stdin/out/err, since there seem to none."
|
|
|
|
);
|
|
|
|
|
2010-11-29 22:34:58 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// disconnects stdstreams
|
2012-10-07 17:48:23 +00:00
|
|
|
if (
|
|
|
|
!freopen( "/dev/null", "r", stdin ) ||
|
|
|
|
!freopen( "/dev/null", "w", stdout ) ||
|
|
|
|
!freopen( "/dev/null", "w", stderr )
|
|
|
|
)
|
|
|
|
{
|
|
|
|
printlogf(
|
|
|
|
L, "Error",
|
|
|
|
"Failure in daemonize at freopen( /dev/null, std[in|out|err] )"
|
|
|
|
);
|
2010-11-29 22:34:58 +00:00
|
|
|
}
|
2012-02-15 11:15:32 +00:00
|
|
|
|
2010-11-29 22:34:58 +00:00
|
|
|
is_daemon = true;
|
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
| Normal operation happens in here.
|
|
|
|
*/
|
2010-11-10 15:57:37 +00:00
|
|
|
static void
|
2010-10-19 10:12:11 +00:00
|
|
|
masterloop(lua_State *L)
|
|
|
|
{
|
2012-10-07 17:48:23 +00:00
|
|
|
while( true )
|
|
|
|
{
|
2010-10-24 16:41:58 +00:00
|
|
|
bool have_alarm;
|
2012-02-15 11:15:32 +00:00
|
|
|
bool force_alarm = false;
|
2012-10-07 17:48:23 +00:00
|
|
|
clock_t now = times( dummy_tms );
|
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 ) );
|
|
|
|
|
|
|
|
//
|
|
|
|
// queries the runner about the soonest alarm
|
|
|
|
//
|
|
|
|
load_runner_func( L, "getAlarm" );
|
2014-02-28 09:15:48 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
if( lua_pcall( L, 0, 1, -2 ) )
|
2014-02-28 09:15:48 +00:00
|
|
|
{
|
2022-03-23 22:41:02 +00:00
|
|
|
safeexit(L, -1 );
|
2014-02-28 09:15:48 +00:00
|
|
|
}
|
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;
|
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;
|
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 ||
|
|
|
|
( have_alarm && time_before_eq( alarm_time, now ) )
|
|
|
|
)
|
|
|
|
{
|
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
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// TODO: Actually it might be smarter to handler observances
|
2010-11-29 10:56:39 +00:00
|
|
|
// eitherway. 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 )
|
|
|
|
{
|
2010-11-29 10:56:39 +00:00
|
|
|
// TODO use trunc instead of long converstions
|
2012-10-07 17:48:23 +00:00
|
|
|
double d = ( (double )( alarm_time - now ) ) / clocks_per_sec;
|
2010-11-04 13:43:57 +00:00
|
|
|
tv.tv_sec = d;
|
2012-10-07 17:48:23 +00:00
|
|
|
tv.tv_nsec = ( (d - ( long ) d) ) * 1000000000.0;
|
|
|
|
printlogf(
|
|
|
|
L, "Masterloop",
|
|
|
|
"going into select ( timeout %f seconds )",
|
|
|
|
d
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logstring(
|
|
|
|
"Masterloop",
|
|
|
|
"going into select ( no timeout )"
|
|
|
|
);
|
2010-10-19 10:12:11 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
// time for Lsyncd to try to put itself to rest into the big select( )
|
|
|
|
// this configures:
|
|
|
|
// timeouts,
|
|
|
|
// filedescriptors and
|
|
|
|
// signals
|
|
|
|
// that will wake Lsyncd
|
2010-11-12 15:39:43 +00:00
|
|
|
{
|
|
|
|
fd_set rfds;
|
|
|
|
fd_set wfds;
|
|
|
|
sigset_t sigset;
|
2010-11-22 20:09:52 +00:00
|
|
|
int pi, pr;
|
2010-11-12 15:39:43 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
sigemptyset( &sigset );
|
|
|
|
FD_ZERO( &rfds );
|
|
|
|
FD_ZERO( &wfds );
|
2010-11-22 20:09:52 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
for( pi = 0; pi < observances_len; pi++ )
|
|
|
|
{
|
2010-12-01 12:19:17 +00:00
|
|
|
struct observance *obs = observances + pi;
|
2012-10-07 17:48:23 +00:00
|
|
|
if ( obs->ready )
|
|
|
|
{
|
|
|
|
FD_SET( obs->fd, &rfds );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( obs->writey )
|
|
|
|
{
|
|
|
|
FD_SET( obs->fd, &wfds );
|
|
|
|
}
|
2010-11-12 15:39:43 +00:00
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
if( !observances_len )
|
|
|
|
{
|
|
|
|
logstring(
|
|
|
|
"Error",
|
|
|
|
"Internal fail, no observances, no monitor!"
|
|
|
|
);
|
|
|
|
|
|
|
|
exit( -1 );
|
2010-12-07 12:50:31 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
// the great select, this is the very heart beat of Lsyncd
|
|
|
|
// that puts Lsyncd to sleep until anything worth noticing
|
|
|
|
// happens
|
|
|
|
|
2010-11-22 20:09:52 +00:00
|
|
|
pr = pselect(
|
2012-10-07 17:48:23 +00:00
|
|
|
observances[ observances_len - 1 ].fd + 1,
|
|
|
|
&rfds,
|
|
|
|
&wfds,
|
|
|
|
NULL,
|
|
|
|
have_alarm ? &tv : NULL,
|
|
|
|
&sigset
|
|
|
|
);
|
|
|
|
|
|
|
|
// something happened!
|
|
|
|
|
|
|
|
if (pr >= 0)
|
|
|
|
{
|
2012-02-15 11:15:32 +00:00
|
|
|
// walks through the observances calling ready/writey
|
2010-11-28 09:15:54 +00:00
|
|
|
observance_action = true;
|
2014-02-28 09:15:48 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
for( pi = 0; pi < observances_len; pi++ )
|
|
|
|
{
|
2010-11-28 09:15:54 +00:00
|
|
|
struct observance *obs = observances + pi;
|
2012-02-16 08:09:19 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
// Checks for signals
|
|
|
|
if( hup || term )
|
2013-07-30 10:20:23 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
// a file descriptor became read-ready
|
|
|
|
if( obs->ready && FD_ISSET( obs->fd, &rfds ) )
|
|
|
|
{
|
2010-11-28 09:15:54 +00:00
|
|
|
obs->ready(L, obs);
|
2010-11-22 20:09:52 +00:00
|
|
|
}
|
2012-02-16 08:09:19 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
// Checks for signals, again, better safe than sorry
|
2013-07-30 10:20:23 +00:00
|
|
|
if ( hup || term )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2012-02-16 08:09:19 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
// FIXME breaks on multiple nonobservances in one beat
|
|
|
|
if(
|
|
|
|
nonobservances_len > 0 &&
|
|
|
|
nonobservances[ nonobservances_len - 1 ] == obs->fd
|
|
|
|
)
|
|
|
|
{
|
2010-11-22 20:09:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
// a file descriptor became write-ready
|
|
|
|
if( obs->writey && FD_ISSET( obs->fd, &wfds ) )
|
|
|
|
{
|
|
|
|
obs->writey( L, obs );
|
2010-11-22 20:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
2010-11-28 09:15:54 +00:00
|
|
|
observance_action = false;
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
// works through delayed nonobserve_fd() calls
|
|
|
|
for (pi = 0; pi < nonobservances_len; pi++)
|
|
|
|
{
|
2013-07-30 10:20:23 +00:00
|
|
|
nonobserve_fd( nonobservances[ pi ] );
|
2010-11-22 20:09:52 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
2010-11-28 09:15:54 +00:00
|
|
|
nonobservances_len = 0;
|
2010-11-12 15:39:43 +00:00
|
|
|
}
|
|
|
|
}
|
2012-02-15 11:15:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// collects 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 );
|
|
|
|
|
|
|
|
if (pid <= 0)
|
|
|
|
{
|
|
|
|
// no more zombies
|
|
|
|
break;
|
2010-11-04 13:52:34 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
// calls the runner to handle the collection
|
|
|
|
load_runner_func( L, "collectProcess" );
|
|
|
|
lua_pushinteger( L, pid );
|
|
|
|
lua_pushinteger( L, WEXITSTATUS( status ) );
|
|
|
|
|
|
|
|
if ( lua_pcall( L, 2, 0, -4 ) )
|
2022-03-23 22:41:02 +00:00
|
|
|
{
|
|
|
|
safeexit(L, -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
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
// reacts on HUP signals
|
|
|
|
if( hup )
|
|
|
|
{
|
|
|
|
load_runner_func( L, "hup" );
|
2013-07-30 10:20:23 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
if( lua_pcall( L, 0, 0, -2 ) )
|
|
|
|
{
|
2022-03-23 22:41:02 +00:00
|
|
|
safeexit( L, -1 );
|
2010-11-14 09:11:09 +00:00
|
|
|
}
|
2013-07-30 10:20:23 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_pop( L, 1 );
|
2013-07-30 10:20:23 +00:00
|
|
|
|
2010-11-14 09:11:09 +00:00
|
|
|
hup = 0;
|
|
|
|
}
|
|
|
|
|
2013-07-30 10:20:23 +00:00
|
|
|
// reacts on TERM and INT signals
|
2012-10-07 17:48:23 +00:00
|
|
|
if( term == 1 )
|
|
|
|
{
|
|
|
|
load_runner_func( L, "term" );
|
2013-07-30 10:20:23 +00:00
|
|
|
|
|
|
|
lua_pushnumber( L, sigcode );
|
|
|
|
|
|
|
|
if( lua_pcall( L, 1, 0, -3 ) )
|
2012-10-07 17:48:23 +00:00
|
|
|
{
|
2022-03-23 22:41:02 +00:00
|
|
|
safeexit(L, -1 );
|
2010-11-14 09:11:09 +00:00
|
|
|
}
|
2013-07-30 10:20:23 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_pop( L, 1 );
|
2013-07-30 10:20:23 +00:00
|
|
|
|
2010-11-14 09:11:09 +00:00
|
|
|
term = 2;
|
|
|
|
}
|
|
|
|
|
2012-02-15 11:15:32 +00:00
|
|
|
// lets the runner do stuff every cycle,
|
|
|
|
// like starting new processes, writing the statusfile etc.
|
2012-10-07 17:48:23 +00:00
|
|
|
load_runner_func( L, "cycle" );
|
2013-07-30 10:20:23 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
l_now( L );
|
2013-07-30 10:20:23 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
if( lua_pcall( L, 1, 1, -3 ) )
|
2013-07-30 10:20:23 +00:00
|
|
|
{
|
2022-03-23 22:41:02 +00:00
|
|
|
safeexit(L, -1 );
|
2013-07-30 10:20:23 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_pop( L, 2 );
|
|
|
|
|
|
|
|
if( lua_gettop( L ) )
|
|
|
|
{
|
|
|
|
logstring(
|
|
|
|
"Error",
|
|
|
|
"internal, stack is dirty."
|
|
|
|
);
|
|
|
|
l_stackdump( L );
|
2022-03-23 22:41:02 +00:00
|
|
|
safeexit(L, -1 );
|
2010-12-01 13:17:04 +00:00
|
|
|
}
|
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
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
// the runner file
|
2010-11-03 21:02:14 +00:00
|
|
|
char * lsyncd_runner_file = NULL;
|
|
|
|
|
|
|
|
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 );
|
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 );
|
|
|
|
|
|
|
|
if(
|
|
|
|
sscanf(
|
|
|
|
version,
|
|
|
|
"Lua %d.%d",
|
|
|
|
&major, &minor
|
|
|
|
) != 2
|
|
|
|
)
|
|
|
|
{
|
|
|
|
fprintf(
|
|
|
|
stderr,
|
|
|
|
"cannot parse lua library version!\n"
|
|
|
|
);
|
|
|
|
exit (-1 );
|
2010-11-11 19:52:20 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
if(
|
|
|
|
major < 5 ||
|
|
|
|
(major == 5 && minor < 1)
|
|
|
|
) {
|
|
|
|
fprintf(
|
|
|
|
stderr,
|
|
|
|
"Lua library is too old. Needs 5.1 at least"
|
|
|
|
);
|
|
|
|
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;
|
2012-10-07 17:48:23 +00:00
|
|
|
add_logcat( "Normal", LOG_NOTICE );
|
|
|
|
add_logcat( "Warn", LOG_WARNING );
|
|
|
|
add_logcat( "Error", LOG_ERR );
|
|
|
|
|
|
|
|
while( i < argc )
|
|
|
|
{
|
|
|
|
if(
|
|
|
|
strcmp( argv[ i ], "-log" ) &&
|
|
|
|
strcmp( argv[ i ], "--log" )
|
|
|
|
)
|
|
|
|
{
|
|
|
|
// arg is neither -log or --log
|
|
|
|
i++;
|
|
|
|
continue;
|
2010-11-03 16:04:11 +00:00
|
|
|
}
|
2012-02-15 12:47:58 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
if( ++i >= argc )
|
|
|
|
{
|
|
|
|
// -(-)log was last argument
|
|
|
|
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 ]
|
|
|
|
);
|
|
|
|
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
|
|
|
|
register_lsyncd( L );
|
|
|
|
|
|
|
|
if( check_logcat( "Debug" ) <= settings.log_level )
|
|
|
|
{
|
2012-02-15 12:47:58 +00:00
|
|
|
// printlogf doesnt support %ld :-(
|
2012-10-07 17:48:23 +00:00
|
|
|
printf(
|
|
|
|
"kernels clocks_per_sec=%ld\n",
|
|
|
|
clocks_per_sec
|
|
|
|
);
|
2010-11-04 13:43:57 +00:00
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
// checks if the user overrode the default runner file
|
|
|
|
if(
|
|
|
|
argp < argc &&
|
|
|
|
!strcmp( argv[ argp ], "--runner" )
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if (argp + 1 >= argc)
|
|
|
|
{
|
|
|
|
logstring(
|
|
|
|
"Error",
|
|
|
|
"Lsyncd Lua-runner file missing after --runner "
|
|
|
|
);
|
|
|
|
|
|
|
|
exit( -1 );
|
2010-10-19 21:56:00 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
lsyncd_runner_file = argv[ argp + 1 ];
|
2010-10-19 21:56:00 +00:00
|
|
|
argp += 2;
|
|
|
|
}
|
2012-04-04 11:15:33 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
if( lsyncd_runner_file )
|
|
|
|
{
|
2012-02-15 12:47:58 +00:00
|
|
|
// checks if the runner file exists
|
2010-10-19 21:56:00 +00:00
|
|
|
struct stat st;
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
if( stat( lsyncd_runner_file, &st ) )
|
|
|
|
{
|
|
|
|
printlogf(
|
|
|
|
L, "Error",
|
|
|
|
"Cannot see a runner at '%s'.",
|
|
|
|
lsyncd_runner_file
|
|
|
|
);
|
|
|
|
exit( -1 );
|
2010-10-27 19:34:56 +00:00
|
|
|
}
|
2012-02-15 19:00:28 +00:00
|
|
|
|
|
|
|
// loads the runner file
|
2012-10-07 17:48:23 +00:00
|
|
|
if( luaL_loadfile(L, lsyncd_runner_file ) )
|
|
|
|
{
|
|
|
|
printlogf(
|
|
|
|
L, "Error",
|
|
|
|
"error loading '%s': %s",
|
|
|
|
lsyncd_runner_file,
|
|
|
|
lua_tostring( L, -1 )
|
|
|
|
);
|
|
|
|
|
|
|
|
exit( -1 );
|
2010-10-19 21:56:00 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-02-15 12:47:58 +00:00
|
|
|
// loads the runner from binary
|
2012-10-07 17:48:23 +00:00
|
|
|
if( luaL_loadbuffer( L, runner_out, runner_size, "runner" ) )
|
|
|
|
{
|
|
|
|
printlogf(
|
|
|
|
L, "Error",
|
|
|
|
"error loading precompiled runner: %s",
|
|
|
|
lua_tostring( L, -1 )
|
|
|
|
);
|
|
|
|
|
|
|
|
exit( -1 );
|
2010-10-27 19:34:56 +00:00
|
|
|
}
|
2010-11-03 21:02:14 +00:00
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
// prepares the runner executing the script
|
2010-11-10 15:57:37 +00:00
|
|
|
{
|
2012-10-07 17:48:23 +00:00
|
|
|
if( lua_pcall( L, 0, LUA_MULTRET, 0 ) )
|
|
|
|
{
|
|
|
|
printlogf(
|
|
|
|
L, "Error",
|
|
|
|
"preparing runner: %s",
|
|
|
|
lua_tostring( L, -1 )
|
|
|
|
);
|
|
|
|
|
|
|
|
exit( -1 );
|
2010-11-10 15:57:37 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
lua_pushlightuserdata( L, (void *) & runner );
|
|
|
|
|
|
|
|
// switches the value ( result of preparing ) and the key &runner
|
|
|
|
lua_insert( L, 1 );
|
|
|
|
|
2012-02-15 12:47:58 +00:00
|
|
|
// saves the table of the runners functions in the lua registry
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_settable( L, LUA_REGISTRYINDEX );
|
2010-11-10 15:57:37 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
// saves the error function extras
|
|
|
|
|
|
|
|
// &callError is the key
|
|
|
|
lua_pushlightuserdata ( L, (void *) &callError );
|
|
|
|
|
|
|
|
// &runner[ callError ] the value
|
|
|
|
lua_pushlightuserdata ( L, (void *) &runner );
|
|
|
|
lua_gettable ( L, LUA_REGISTRYINDEX );
|
|
|
|
lua_pushstring ( L, "callError" );
|
|
|
|
lua_gettable ( L, -2 );
|
|
|
|
lua_remove ( L, -2 );
|
|
|
|
|
|
|
|
lua_settable ( L, LUA_REGISTRYINDEX );
|
2010-10-17 17:13:53 +00:00
|
|
|
}
|
2010-10-18 17:09:59 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
// asserts the Lsyncd's version matches
|
|
|
|
// between runner and core
|
2010-10-18 17:09:59 +00:00
|
|
|
{
|
|
|
|
const char *lversion;
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
lua_getglobal( L, "lsyncd_version" );
|
|
|
|
lversion = luaL_checkstring( L, -1 );
|
|
|
|
|
|
|
|
if( strcmp( lversion, PACKAGE_VERSION ) )
|
|
|
|
{
|
|
|
|
printlogf(
|
|
|
|
L, "Error",
|
2010-10-27 19:34:56 +00:00
|
|
|
"Version mismatch '%s' is '%s', but core is '%s'",
|
2015-10-15 06:29:47 +00:00
|
|
|
lsyncd_runner_file ? lsyncd_runner_file : "( internal runner )",
|
2012-10-07 17:48:23 +00:00
|
|
|
lversion, PACKAGE_VERSION
|
|
|
|
);
|
|
|
|
|
|
|
|
exit( -1 );
|
2010-10-18 17:09:59 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
lua_pop( L, 1 );
|
2010-10-18 17:09:59 +00:00
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
// loads the defaults from binary
|
2012-02-15 19:00:28 +00:00
|
|
|
{
|
2012-10-07 17:48:23 +00:00
|
|
|
if( luaL_loadbuffer( L, defaults_out, defaults_size, "defaults" ) )
|
|
|
|
{
|
|
|
|
printlogf(
|
|
|
|
L, "Error",
|
|
|
|
"loading defaults: %s",
|
|
|
|
lua_tostring( L, -1 )
|
|
|
|
);
|
|
|
|
|
|
|
|
exit( -1 );
|
2012-02-15 19:00:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// prepares the defaults
|
2013-07-30 10:20:23 +00:00
|
|
|
if( lua_pcall( L, 0, 0, 0 ) )
|
2012-10-07 17:48:23 +00:00
|
|
|
{
|
|
|
|
printlogf(
|
|
|
|
L, "Error",
|
|
|
|
"preparing defaults: %s",
|
|
|
|
lua_tostring( L, -1 )
|
|
|
|
);
|
|
|
|
exit( -1 );
|
2012-02-15 19:00:28 +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
|
|
|
{
|
|
|
|
int i;
|
2012-10-07 17:48:23 +00:00
|
|
|
for( i = argp; i < argc; i++ )
|
|
|
|
{
|
|
|
|
if (
|
|
|
|
!strcmp( argv[ i ], "-help" ) ||
|
|
|
|
!strcmp( argv[ i ], "--help" )
|
|
|
|
)
|
|
|
|
{
|
2013-07-30 10:20:23 +00:00
|
|
|
load_runner_func( L, "help" );
|
2012-10-07 17:48:23 +00:00
|
|
|
|
2013-07-30 10:20:23 +00:00
|
|
|
if( lua_pcall( L, 0, 0, -2 ) )
|
|
|
|
{
|
|
|
|
exit( -1 );
|
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
lua_pop( L, 1 );
|
|
|
|
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
|
2012-10-07 17:48:23 +00:00
|
|
|
load_runner_func( L, "configure" );
|
|
|
|
lua_newtable( L );
|
|
|
|
|
|
|
|
while( argp < argc )
|
|
|
|
{
|
|
|
|
lua_pushnumber ( L, idx++ );
|
|
|
|
lua_pushstring ( L, argv[ argp++ ] );
|
|
|
|
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 ] )
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2012-10-08 07:10:03 +00:00
|
|
|
if( lua_pcall( L, 2, 1, -4 ) )
|
2013-07-30 10:20:23 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
s = lua_tostring(L, -1);
|
2012-10-07 17:48:23 +00:00
|
|
|
if( s )
|
|
|
|
{
|
|
|
|
lsyncd_config_file = s_strdup( s );
|
|
|
|
}
|
2010-11-03 21:02:14 +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 )
|
|
|
|
{
|
|
|
|
printlogf(
|
|
|
|
L, "Error",
|
|
|
|
"Cannot find config file at '%s'.",
|
|
|
|
lsyncd_config_file
|
|
|
|
);
|
|
|
|
|
|
|
|
exit( -1 );
|
2012-02-15 13:42:24 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
free( lsyncd_config_file );
|
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 ) )
|
|
|
|
{
|
|
|
|
printlogf(
|
|
|
|
L, "Error",
|
|
|
|
"Cannot find config file at '%s'.",
|
|
|
|
lsyncd_config_file
|
|
|
|
);
|
|
|
|
|
|
|
|
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",
|
|
|
|
"error loading %s: %s",
|
|
|
|
lsyncd_config_file,
|
|
|
|
lua_tostring( L, -1 )
|
|
|
|
);
|
|
|
|
|
|
|
|
exit( -1 );
|
2010-11-03 21:02:14 +00:00
|
|
|
}
|
2012-02-15 13:42:24 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
if( lua_pcall( L, 0, LUA_MULTRET, 0) )
|
|
|
|
{
|
|
|
|
printlogf(
|
|
|
|
L, "Error",
|
|
|
|
"error preparing %s: %s",
|
|
|
|
lsyncd_config_file,
|
|
|
|
lua_tostring( L, -1 )
|
|
|
|
);
|
2022-03-17 01:29:23 +00:00
|
|
|
l_traceback(L);
|
2012-10-07 17:48:23 +00:00
|
|
|
|
2022-03-23 22:41:02 +00:00
|
|
|
safeexit(L, -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
|
|
|
|
2014-04-29 14:11:27 +00:00
|
|
|
#ifdef WITH_INOTIFY
|
2012-10-07 17:48:23 +00:00
|
|
|
open_inotify( L );
|
2010-11-26 16:52:24 +00:00
|
|
|
#endif
|
2012-10-07 17:48:23 +00:00
|
|
|
|
2014-04-29 14:11:27 +00:00
|
|
|
#ifdef WITH_FSEVENTS
|
2012-10-07 17:48:23 +00:00
|
|
|
open_fsevents( L );
|
2010-11-26 16:52:24 +00:00
|
|
|
#endif
|
2010-11-11 20:43:20 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
// adds signal handlers
|
|
|
|
// listens to SIGCHLD, but blocks it until pselect( )
|
|
|
|
// opens the signal handler up
|
2010-10-25 08:42:27 +00:00
|
|
|
{
|
|
|
|
sigset_t set;
|
2012-10-07 17:48:23 +00:00
|
|
|
sigemptyset( &set );
|
|
|
|
sigaddset( &set, SIGCHLD );
|
|
|
|
signal( SIGCHLD, sig_child );
|
|
|
|
sigprocmask( SIG_BLOCK, &set, NULL );
|
2012-02-15 11:15:32 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
signal( SIGHUP, sig_handler );
|
|
|
|
signal( SIGTERM, sig_handler );
|
2013-07-30 10:20:23 +00:00
|
|
|
signal( SIGINT, sig_handler );
|
2010-10-25 08:42:27 +00:00
|
|
|
}
|
2010-10-24 21:35:29 +00:00
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
// runs initializations from runner
|
|
|
|
// it will set the configuration and add watches
|
2010-10-25 21:04:28 +00:00
|
|
|
{
|
2012-10-07 17:48:23 +00:00
|
|
|
load_runner_func( L, "initialize" );
|
|
|
|
lua_pushboolean( L, first_time );
|
|
|
|
|
|
|
|
if( lua_pcall( L, 1, 0, -3 ) )
|
2013-07-30 10:20:23 +00:00
|
|
|
{
|
|
|
|
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-10-07 17:48:23 +00:00
|
|
|
//
|
2012-02-15 11:15:32 +00:00
|
|
|
// cleanup
|
2012-10-07 17:48:23 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
// tidies up all observances
|
2010-11-28 09:15:54 +00:00
|
|
|
{
|
|
|
|
int i;
|
2012-10-07 17:48:23 +00:00
|
|
|
for( i = 0; i < observances_len; i++ )
|
|
|
|
{
|
2010-11-28 09:15:54 +00:00
|
|
|
struct observance *obs = observances + i;
|
2012-10-07 17:48:23 +00:00
|
|
|
obs->tidy( obs );
|
2010-11-28 09:15:54 +00:00
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
observances_len = 0;
|
2010-11-28 09:15:54 +00:00
|
|
|
nonobservances_len = 0;
|
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
// frees logging categories
|
2010-11-14 09:11:09 +00:00
|
|
|
{
|
|
|
|
int ci;
|
|
|
|
struct logcat *lc;
|
2012-10-07 17:48:23 +00:00
|
|
|
for( ci = 'A'; ci <= 'Z'; ci++ )
|
|
|
|
{
|
|
|
|
for( lc = logcats[ ci - 'A' ]; lc && lc->name; lc++)
|
|
|
|
{
|
|
|
|
free( lc->name );
|
2010-11-14 09:11:09 +00:00
|
|
|
lc->name = NULL;
|
|
|
|
}
|
2012-10-07 17:48:23 +00:00
|
|
|
|
|
|
|
if( logcats[ci - 'A' ] )
|
|
|
|
{
|
|
|
|
free( logcats[ ci - 'A' ] );
|
|
|
|
logcats[ ci - 'A' ] = NULL;
|
2010-11-14 09:11:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
lua_close( L );
|
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 );
|
|
|
|
|
2012-10-07 17:48:23 +00:00
|
|
|
while( !term ) {
|
|
|
|
main1( argc, argv );
|
|
|
|
}
|
|
|
|
|
2013-07-30 10:20:23 +00:00
|
|
|
if( pidfile_fd > 0 )
|
|
|
|
{
|
|
|
|
close( pidfile_fd );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( settings.pidfile )
|
|
|
|
{
|
|
|
|
remove( settings.pidfile );
|
|
|
|
}
|
|
|
|
|
|
|
|
// exits with error code responding to the signal it died for
|
|
|
|
return 128 + sigcode;
|
2010-11-14 09:11:09 +00:00
|
|
|
}
|
|
|
|
|