lsyncd/lsyncd.c

1590 lines
35 KiB
C
Raw Normal View History

2010-10-19 10:20:27 +00:00
/**
* lsyncd.c Live (Mirror) Syncing Demon
*
* License: GPLv2 (see COPYING) or any later version
*
* Authors: Axel Kittenberger <axkibe@gmail.com>
*
2010-11-22 21:17:08 +00:00
* -----------------------------------------------------------------------
*
2010-10-19 10:20:27 +00:00
* This is the core. It contains as minimal as possible glues
* to the operating system needed for lsyncd operation. All high-level
* logic is coded (when feasable) into lsyncd.lua
*/
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
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>
#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>
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>
2010-10-14 13:52:01 +00:00
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
2010-10-18 17:09:59 +00:00
/**
2010-11-04 13:43:57 +00:00
* The Lua part of lsyncd if compiled into the binary.
2010-10-18 17:09:59 +00:00
*/
2010-11-03 21:02:14 +00:00
#ifndef LSYNCD_DEFAULT_RUNNER_FILE
2010-10-26 20:29:12 +00:00
extern char _binary_luac_out_start;
extern char _binary_luac_out_end;
2010-11-03 21:02:14 +00:00
#endif
2010-10-18 17:09:59 +00:00
/**
* The default notification system to use.
*/
#ifdef LSYNCD_WITH_INOTIFY
extern char *default_notify = "Inotify";
#else
# error "need at least one notifcation system. please rerun ./configure"
#endif
2010-10-27 19:34:56 +00:00
/**
* configuration parameters
*/
struct settings settings = {
2010-11-03 21:02:14 +00:00
.log_file = NULL,
.log_syslog = false,
.log_level = 0,
2010-11-13 21:50:21 +00:00
.nodaemon = false,
2010-10-27 19:34:56 +00:00
};
2010-10-20 15:34:01 +00:00
/**
* True when lsyncd daemonized itself.
*/
2010-10-27 19:34:56 +00:00
static bool is_daemon = false;
/**
* True after first configuration phase. This is to write configuration error
* messages to stdout/stderr after being first started. Then it uses whatever
* it has been configured to. This survives a reset by HUP signal or
* inotify OVERFLOW!
*/
static bool running = false;
2010-10-16 18:21:01 +00:00
/**
* Set to TERM or HUP in signal handler, when lsyncd should end or reset ASAP.
*/
volatile sig_atomic_t hup = 0;
volatile sig_atomic_t term = 0;
2010-10-16 10:26:48 +00:00
2010-10-19 10:12:11 +00:00
/**
* The kernels clock ticks per second.
*/
2010-10-20 15:34:01 +00:00
static long clocks_per_sec;
2010-10-19 10:12:11 +00:00
2010-10-24 21:35:29 +00:00
/**
* signal handler
*/
void
sig_child(int sig)
{
/* nothing */
}
2010-11-14 09:11:09 +00:00
/**
* signal handler
*/
void
sig_handler(int sig)
{
switch (sig) {
case SIGTERM:
term = 1;
return;
case SIGHUP:
hup = 1;
return;
}
}
2010-11-03 14:54:33 +00:00
/*****************************************************************************
* Logging
****************************************************************************/
2010-10-17 17:13:53 +00:00
/**
2010-11-03 14:54:33 +00:00
* A logging category
2010-10-17 17:13:53 +00:00
*/
2010-11-03 14:54:33 +00:00
struct logcat {
char *name;
int priority;
};
2010-10-17 17:13:53 +00:00
/**
2010-11-03 14:54:33 +00:00
* A table of all enabled logging categories.
* Sorted by first letter to have to do less comparisons;
2010-10-17 17:13:53 +00:00
*/
2010-11-03 14:54:33 +00:00
static struct logcat *logcats[26] = {0,};
2010-10-14 13:52:01 +00:00
2010-10-19 20:14:55 +00:00
/**
2010-11-03 14:54:33 +00:00
* Returns the positive priority if category is configured to be logged.
* or -1
2010-10-19 20:14:55 +00:00
*/
extern int
2010-11-03 14:54:33 +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;
if (name[0] < 'A' || name[0] > 'Z') {
return false;
2010-10-19 20:14:55 +00:00
}
2010-11-03 14:54:33 +00:00
lc = logcats[name[0]-'A'];
if (!lc) {
return -1;
}
while (lc->name) {
if (!strcmp(lc->name, name)) {
return lc->priority;
}
lc++;
}
return -1;
2010-10-19 20:14:55 +00:00
}
2010-10-18 17:09:59 +00:00
/**
2010-11-03 14:54:33 +00:00
* Adds a logging category
2010-11-03 21:02:14 +00:00
* @return true if OK.
2010-10-18 17:09:59 +00:00
*/
2010-11-03 21:02:14 +00:00
static bool
2010-11-03 14:54:33 +00:00
add_logcat(const char *name, int priority)
2010-10-18 17:09:59 +00:00
{
2010-11-03 14:54:33 +00:00
struct logcat *lc;
2010-11-03 16:04:11 +00:00
if (!strcmp("all", name)) {
2010-11-03 21:02:14 +00:00
settings.log_level = -1;
return true;
}
if (!strcmp("scarce", name)) {
settings.log_level = LOG_ERR;
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
/* category must start with capital letter */
if (name[0] < 'A' || name[0] > 'Z') {
2010-11-03 21:02:14 +00:00
return false;
2010-10-18 17:09:59 +00:00
}
2010-11-03 14:54:33 +00:00
if (!logcats[name[0]-'A']) {
/* en empty capital letter */
lc = logcats[name[0]-'A'] = s_calloc(2, sizeof(struct logcat));
} else {
/* length of letter list */
int ll = 0;
/* counts list length */
for(lc = logcats[name[0]-'A']; lc->name; lc++) {
ll++;
}
/* enlarge list */
logcats[name[0]-'A'] =
s_realloc(logcats[name[0]-'A'], (ll + 2) * sizeof(struct logcat));
/* go to list end */
2010-11-03 21:02:14 +00:00
for(lc = logcats[name[0]-'A']; lc->name; lc++) {
if (!strcmp(name, lc->name)) {
/* already there */
return true;
}
}
2010-11-03 14:54:33 +00:00
}
lc->name = s_strdup(name);
lc->priority = priority;
/* terminates the list */
lc[1].name = NULL;
2010-11-03 21:02:14 +00:00
return true;
2010-10-18 17:09:59 +00:00
}
2010-10-20 18:33:17 +00:00
/**
2010-11-22 14:54:50 +00:00
* Logs a string.
*
* Do not call directly, but the macro logstring() in lsyncd.h
2010-10-20 18:33:17 +00:00
*
2010-11-03 14:54:33 +00:00
* @param priorty the priority of the log message
* @param cat the category
* @param message the log message
2010-10-20 18:33:17 +00:00
*/
2010-11-22 14:54:50 +00:00
extern void
2010-11-03 14:54:33 +00:00
logstring0(int priority, const char *cat, const char *message)
2010-10-20 18:33:17 +00:00
{
2010-11-03 15:23:36 +00:00
/* in case of logall and not found category priority will be -1 */
if (priority < 0) {
priority = LOG_DEBUG;
}
2010-10-27 19:34:56 +00:00
if (!running) {
/* lsyncd is in intial configuration.
* thus just print to normal stdout/stderr. */
2010-11-03 21:02:14 +00:00
if (priority >= LOG_ERR) {
fprintf(stderr, "%s: %s\n", cat, message);
2010-11-03 14:54:33 +00:00
} else {
2010-11-03 21:02:14 +00:00
printf("%s: %s\n", cat, message);
2010-10-27 19:34:56 +00:00
}
return;
}
/* writes on console if not daemon */
2010-10-20 18:33:17 +00:00
if (!is_daemon) {
char ct[255];
/* gets current timestamp hour:minute:second */
time_t mtime;
time(&mtime);
strftime(ct, sizeof(ct), "%T", localtime(&mtime));
2010-11-03 14:54:33 +00:00
FILE * flog = priority <= LOG_ERR ? stderr : stdout;
fprintf(flog, "%s %s: %s\n", ct, cat, message);
2010-10-20 18:33:17 +00:00
}
2010-10-27 19:34:56 +00:00
/* writes to file if configured so */
2010-11-03 21:02:14 +00:00
if (settings.log_file) {
FILE * flog = fopen(settings.log_file, "a");
2010-10-20 18:33:17 +00:00
/* gets current timestamp day-time-year */
char * ct;
time_t mtime;
time(&mtime);
ct = ctime(&mtime);
/* cuts trailing linefeed */
ct[strlen(ct) - 1] = 0;
if (flog == NULL) {
2010-11-03 21:02:14 +00:00
fprintf(stderr, "Cannot open logfile [%s]!\n",
settings.log_file);
2010-10-20 18:33:17 +00:00
exit(-1); // ERRNO
}
2010-11-14 09:11:09 +00:00
fprintf(flog, "%s %s: %s\n", ct, cat, message);
2010-10-20 18:33:17 +00:00
fclose(flog);
}
2010-10-27 19:34:56 +00:00
/* sends to syslog if configured so */
2010-11-03 21:02:14 +00:00
if (settings.log_syslog) {
2010-11-03 14:54:33 +00:00
syslog(priority, "%s, %s", cat, message);
2010-10-20 18:33:17 +00:00
}
return;
}
2010-11-04 13:43:57 +00:00
/**
2010-11-22 14:54:50 +00:00
* Lets the core print logmessages comfortably as formated string.
2010-11-04 13:43:57 +00:00
* This uses the lua_State for it easy string buffers only.
*/
2010-11-22 14:54:50 +00:00
extern void
2010-11-04 13:43:57 +00:00
printlogf0(lua_State *L,
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
/*****************************************************************************
* Simple memory management
2010-11-22 14:54:50 +00:00
* TODO: call the garbace collector in case of out of memory.
2010-11-03 14:54:33 +00:00
****************************************************************************/
2010-11-02 21:04:01 +00:00
/**
2010-11-03 14:54:33 +00:00
* "secured" calloc.
2010-11-02 21:04:01 +00:00
*/
2010-11-22 14:54:50 +00:00
extern void *
2010-11-03 14:54:33 +00:00
s_calloc(size_t nmemb, size_t size)
{
void *r = calloc(nmemb, size);
if (r == NULL) {
logstring0(LOG_ERR, "Error", "Out of memory!");
exit(-1); // ERRNO
}
return r;
}
/**
* "secured" malloc. the deamon shall kill itself
* in case of out of memory.
*/
2010-11-22 14:54:50 +00:00
extern void *
2010-11-03 14:54:33 +00:00
s_malloc(size_t size)
{
void *r = malloc(size);
if (r == NULL) {
logstring0(LOG_ERR, "Error", "Out of memory!");
exit(-1); // ERRNO
}
return r;
}
/**
* "secured" realloc.
*/
2010-11-22 14:54:50 +00:00
extern void *
2010-11-03 14:54:33 +00:00
s_realloc(void *ptr, size_t size)
2010-11-02 21:04:01 +00:00
{
2010-11-03 14:54:33 +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
}
2010-11-03 14:54:33 +00:00
return r;
2010-11-02 21:04:01 +00:00
}
2010-11-03 14:54:33 +00:00
/**
* "secured" strdup.
*/
2010-11-22 14:54:50 +00:00
extern char *
2010-11-03 14:54:33 +00:00
s_strdup(const char *src)
{
char *s = strdup(src);
if (s == NULL) {
logstring0(LOG_ERR, "Error", "Out of memory!");
exit(-1); // ERRNO
}
return s;
}
2010-10-18 17:09:59 +00:00
2010-11-17 11:14:36 +00:00
/*****************************************************************************
* Pipes management
****************************************************************************/
/**
* A child process gets text piped longer than on
* write() can manage.
*/
struct pipemsg {
/* message to send */
char *text;
/* length of text */
int tlen;
/* position in message */
int pos;
};
/**
* Called by the core whenever a pipe becomes
* writeable again
2010-11-17 11:14:36 +00:00
*/
static void
pipe_writey(lua_State *L, int fd, void *extra) {
struct pipemsg *pm = (struct pipemsg *) extra;
int len = write(fd, pm->text + pm->pos, pm->tlen - pm->pos);
bool do_close = false;
pm->pos += len;
if (len < 0) {
logstring("Normal", "broken pipe.");
do_close = true;
} else if (pm->pos >= pm->tlen) {
logstring("Debug", "finished pipe.");
do_close = true;
}
if (do_close) {
close(fd);
free(pm->text);
free(extra);
unobserve_fd(fd);
}
}
2010-11-17 11:14:36 +00:00
/*****************************************************************************
* helper routines.
****************************************************************************/
2010-11-12 15:39:43 +00:00
/**
* Sets the close-on-exit flag for an fd
*/
2010-11-22 14:54:50 +00:00
extern void
2010-11-12 15:39:43 +00:00
close_exec_fd(int fd)
{
int flags;
flags = fcntl(fd, F_GETFD);
if (flags == -1) {
logstring("Error", "cannot get descriptor flags!");
exit(-1); // ERRNO
}
flags |= FD_CLOEXEC;
if (fcntl(fd, F_SETFD, flags) == -1) {
logstring("Error", "cannot set descripptor flags!");
exit(-1); // ERRNO
}
}
/**
* Sets the non-blocking flag for an fd
*/
2010-11-22 14:54:50 +00:00
extern void
2010-11-12 15:39:43 +00:00
non_block_fd(int fd)
{
int flags;
flags = fcntl(fd, F_GETFL);
if (flags == -1) {
logstring("Error", "cannot get status flags!");
exit(-1); // ERRNO
}
flags |= O_NONBLOCK;;
if (fcntl(fd, F_SETFL, flags) == -1) {
logstring("Error", "cannot set status flags!");
exit(-1); // ERRNO
}
}
/**
2010-11-17 11:14:36 +00:00
* Writes a pid file.
2010-11-12 15:39:43 +00:00
*/
2010-11-22 14:54:50 +00:00
static void
2010-11-17 11:14:36 +00:00
write_pidfile(lua_State *L, const char *pidfile) {
FILE* f = fopen(pidfile, "w");
if (!f) {
printlogf(L, "Error", "Cannot write pidfile; '%s'", pidfile);
exit(-1); // ERRNO
}
fprintf(f, "%i\n", getpid());
fclose(f);
}
2010-11-12 15:39:43 +00:00
2010-10-18 09:02:51 +00:00
/*****************************************************************************
* Library calls for lsyncd.lua
*
* These are as minimal as possible glues to the operating system needed for
* lsyncd operation.
*
****************************************************************************/
2010-10-17 15:24:55 +00:00
2010-11-04 13:43:57 +00:00
static int l_stackdump(lua_State* L);
2010-10-20 15:34:01 +00:00
/**
2010-10-20 18:33:17 +00:00
* Logs a message.
2010-10-20 15:34:01 +00:00
*
* @param loglevel (Lua stack) loglevel of massage
* @param string (Lua stack) the string to log
*/
static int
l_log(lua_State *L)
{
2010-11-03 14:54:33 +00:00
/* log category */
const char * cat;
2010-10-20 15:34:01 +00:00
/* log message */
const char * message;
2010-11-03 14:54:33 +00:00
/* log priority */
int priority;
cat = luaL_checkstring(L, 1);
priority = check_logcat(cat);
/* skips filtered messages */
2010-11-04 13:43:57 +00:00
if (priority < settings.log_level) {
2010-10-20 15:34:01 +00:00
return 0;
}
2010-11-03 14:54:33 +00:00
2010-11-04 13:43:57 +00:00
{
// replace non string values
int i;
int top = lua_gettop(L);
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)");
}
lua_replace(L, i);
break;
2010-11-20 13:22:47 +00:00
case LUA_TNIL:
lua_pushstring(L, "(nil)");
lua_replace(L, i);
break;
2010-11-04 13:43:57 +00:00
}
}
}
2010-11-02 17:07:42 +00:00
/* concates if there is more than one string parameter */
lua_concat(L, lua_gettop(L) - 1);
2010-10-20 15:34:01 +00:00
message = luaL_checkstring(L, 2);
2010-11-03 14:54:33 +00:00
logstring0(priority, cat, message);
2010-10-20 15:34:01 +00:00
return 0;
}
2010-11-06 17:40:12 +00:00
/**
* Returns (on Lua stack) true if time1 is earler than time2
* @param (on Lua Stack) time1
* @param (on Lua Stack) time2
* @return the true if time1 < time2
*/
static int
l_clockbefore(lua_State *L)
{
clock_t t1 = (clock_t) luaL_checkinteger(L, 1);
clock_t t2 = (clock_t) luaL_checkinteger(L, 2);
lua_pushboolean(L, time_before(t1, t2));
return 1;
}
2010-10-24 16:41:58 +00:00
/**
* Returns (on Lua stack) true if time1 is earler or eq to time2
* @param (on Lua Stack) time1
* @param (on Lua Stack) time2
* @return the true if time1 <= time2
*/
static int
2010-11-06 15:08:17 +00:00
l_clockbeforeq(lua_State *L)
2010-10-24 16:41:58 +00:00
{
clock_t t1 = (clock_t) luaL_checkinteger(L, 1);
clock_t t2 = (clock_t) luaL_checkinteger(L, 2);
lua_pushboolean(L, time_before_eq(t1, t2));
return 1;
}
/**
* Returns (on Lua stack) the earlier or two clock times.
*
* @param (on Lua Stack) time1
* @param (on Lua Stack) time2
* @return the earlier time
*/
static int
l_earlier(lua_State *L)
{
clock_t t1 = (clock_t) luaL_checkinteger(L, 1);
clock_t t2 = (clock_t) luaL_checkinteger(L, 2);
lua_pushinteger(L, time_before(t1, t2) ? t1 : t2);
return 1;
}
2010-10-19 10:12:11 +00:00
/**
2010-10-27 19:34:56 +00:00
* Returns (on Lua stack) the current kernels
* clock state (jiffies)
2010-10-19 10:12:11 +00:00
*/
static int
l_now(lua_State *L)
{
2010-10-23 12:36:55 +00:00
lua_pushinteger(L, times(NULL));
2010-10-19 10:12:11 +00:00
return 1;
}
2010-10-22 23:14:11 +00:00
/**
2010-10-25 08:50:10 +00:00
* Returns (on Lua stack) the addition of a clock timer by seconds.
2010-10-22 23:14:11 +00:00
*
2010-11-11 19:52:20 +00:00
* @param1 (Lua stack) the clock timer
* @param2 (Lua stack) seconds to change clock.
*
* @return (Lua stack) clock timer + seconds.
2010-10-22 23:14:11 +00:00
*/
static int
2010-11-06 15:08:17 +00:00
l_addtoclock(lua_State *L)
2010-10-22 23:14:11 +00:00
{
clock_t c1 = luaL_checkinteger(L, 1);
clock_t c2 = luaL_checkinteger(L, 2);
lua_pop(L, 2);
2010-10-25 08:50:10 +00:00
lua_pushinteger(L, c1 + c2 * clocks_per_sec);
2010-10-22 23:14:11 +00:00
return 1;
}
2010-10-28 17:56:33 +00:00
2010-10-17 17:13:53 +00:00
/**
2010-10-18 09:02:51 +00:00
* Executes a subprocess. Does not wait for it to return.
*
* @param (Lua stack) Path to binary to call
* @params (Lua stack) list of string as arguments
2010-11-12 14:05:10 +00:00
* or "<" in which case the next argument is a string that will be piped
* on stdin. the arguments will follow that one.
2010-11-11 19:52:20 +00:00
*
2010-10-18 09:02:51 +00:00
* @return (Lua stack) the pid on success, 0 on failure.
2010-10-17 17:13:53 +00:00
*/
static int
2010-10-18 09:02:51 +00:00
l_exec(lua_State *L)
2010-10-17 17:13:53 +00:00
{
2010-11-12 14:05:10 +00:00
/* the binary to call */
2010-10-17 20:26:37 +00:00
const char *binary = luaL_checkstring(L, 1);
2010-11-12 14:05:10 +00:00
/* number of arguments */
2010-10-17 17:13:53 +00:00
int argc = lua_gettop(L) - 1;
2010-11-12 14:05:10 +00:00
/* the pid spawned */
2010-10-17 20:26:37 +00:00
pid_t pid;
2010-11-12 14:05:10 +00:00
/* the arguments position in the lua arguments */
int li = 1;
/* the pipe to text */
char const *pipe_text = NULL;
/* the arguments */
char const **argv;
/* pipe file descriptors */
int pipefd[2];
/* writes a log message, prepares the message only if actually needed. */
2010-11-05 18:04:29 +00:00
if (check_logcat("Exec") >= settings.log_level) {
2010-11-12 14:05:10 +00:00
int i;
2010-11-05 18:04:29 +00:00
lua_pushvalue(L, 1);
for(i = 1; i <= argc; i++) {
lua_pushstring(L, " [");
lua_pushvalue(L, i + 1);
lua_pushstring(L, "]");
}
lua_concat(L, 3 * argc + 1);
logstring0(LOG_DEBUG, "Exec", luaL_checkstring(L, -1));
lua_pop(L, 1);
}
2010-11-12 14:05:10 +00:00
if (argc >= 2 && !strcmp(luaL_checkstring(L, 2), "<")) {
/* pipes something into stdin */
pipe_text = luaL_checkstring(L, 3);
/* creates the pipe */
if (pipe(pipefd) == -1) {
logstring("Error", "cannot create a pipe!");
2010-11-12 15:39:43 +00:00
exit(-1); // ERRNO
2010-11-12 14:05:10 +00:00
}
2010-11-12 15:39:43 +00:00
/* always close the write end for child processes */
close_exec_fd(pipefd[1]);
/* set the write end on non-blocking */
non_block_fd(pipefd[1]);
2010-11-12 14:05:10 +00:00
argc -= 2;
li += 2;
}
2010-11-12 15:39:43 +00:00
{
/* prepares the arguments */
int i;
argv = s_calloc(argc + 2, sizeof(char *));
argv[0] = binary;
for(i = 1; i <= argc; i++) {
argv[i] = luaL_checkstring(L, i + li);
}
argv[i] = NULL;
2010-10-17 20:26:37 +00:00
}
pid = fork();
if (pid == 0) {
2010-11-12 14:05:10 +00:00
/* replaces stdin for pipes */
if (pipe_text) {
dup2(pipefd[0], STDIN_FILENO);
}
2010-10-28 17:56:33 +00:00
/* if lsyncd runs as a daemon and has a logfile it will redirect
stdout/stderr of child processes to the logfile. */
2010-11-03 21:02:14 +00:00
if (is_daemon && settings.log_file) {
if (!freopen(settings.log_file, "a", stdout)) {
2010-11-03 14:54:33 +00:00
printlogf(L, "Error",
2010-11-03 21:02:14 +00:00
"cannot redirect stdout to '%s'.",
settings.log_file);
2010-10-28 17:56:33 +00:00
}
2010-11-03 21:02:14 +00:00
if (!freopen(settings.log_file, "a", stderr)) {
2010-11-03 14:54:33 +00:00
printlogf(L, "Error",
2010-11-03 21:02:14 +00:00
"cannot redirect stderr to '%s'.",
settings.log_file);
2010-10-28 17:56:33 +00:00
}
}
2010-10-17 20:26:37 +00:00
execv(binary, (char **)argv);
2010-10-28 17:56:33 +00:00
/* in a sane world execv does not return! */
2010-11-03 14:54:33 +00:00
printlogf(L, "Error", "Failed executing [%s]!", binary);
2010-10-17 20:26:37 +00:00
exit(-1); // ERRNO
2010-10-17 17:13:53 +00:00
}
2010-11-12 14:05:10 +00:00
if (pipe_text) {
2010-11-12 15:39:43 +00:00
int tlen = strlen(pipe_text);
int len;
2010-11-12 14:05:10 +00:00
/* first closes read-end of pipe, this is for child process only */
close(pipefd[0]);
2010-11-12 15:39:43 +00:00
/* start filling the pipe */
len = write(pipefd[1], pipe_text, tlen);
if (len < 0) {
2010-11-12 20:43:27 +00:00
logstring("Normal", "immediatly broken pipe.");
close(pipefd[0]);
2010-11-12 15:39:43 +00:00
}
if (len == tlen) {
/* usual and best case, the pipe accepted all input -> close */
close(pipefd[1]);
logstring("Exec", "one-sweeped pipe");
} else {
struct pipemsg *pm;
logstring("Exec", "adding pipe observer");
pm = s_calloc(1, sizeof(struct pipemsg));
pm->text = s_strdup(pipe_text);
pm->tlen = tlen;
pm->pos = len;
observe_fd(pipefd[1], NULL, pipe_writey, pm);
2010-11-12 15:39:43 +00:00
}
close(pipefd[0]);
2010-11-12 14:05:10 +00:00
}
2010-10-17 20:26:37 +00:00
free(argv);
2010-10-18 09:02:51 +00:00
lua_pushnumber(L, pid);
return 1;
2010-10-17 17:13:53 +00:00
}
2010-10-16 10:26:48 +00:00
/**
2010-10-16 18:21:01 +00:00
* Converts a relative directory path to an absolute.
*
* @param dir a relative path to directory
* @return absolute path of directory
2010-10-16 10:26:48 +00:00
*/
2010-10-17 15:24:55 +00:00
static int
2010-11-06 15:08:17 +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;
char *cbuf;
const char *rdir = luaL_checkstring(L, 1);
/* use c-library to get absolute path */
cbuf = realpath(rdir, NULL);
if (cbuf == NULL) {
2010-11-03 14:54:33 +00:00
printlogf(L, "Error", "failure getting absolute path of [%s]", rdir);
2010-10-16 18:21:01 +00:00
return 0;
}
{
/* makes sure its a directory */
struct stat st;
2010-10-28 17:56:33 +00:00
if (stat(cbuf, &st)) {
2010-11-03 14:54:33 +00:00
printlogf(L, "Error",
2010-10-28 17:56:33 +00:00
"cannot get absolute path of dir '%s': %s",
rdir, strerror(errno));
return 0;
}
2010-10-16 18:21:01 +00:00
if (!S_ISDIR(st.st_mode)) {
2010-11-03 14:54:33 +00:00
printlogf(L, "Error",
2010-10-28 17:56:33 +00:00
"cannot get absolute path of dir '%s': is not a directory",
rdir);
2010-10-16 18:21:01 +00:00
free(cbuf);
return 0;
}
}
/* returns absolute path with a concated '/' */
luaL_buffinit(L, &b);
luaL_addstring(&b, cbuf);
luaL_addchar(&b, '/');
luaL_pushresult(&b);
free(cbuf);
return 1;
}
2010-10-17 15:24:55 +00:00
/**
* Dumps the LUA stack. For debugging purposes.
*/
static int
2010-10-18 10:26:15 +00:00
l_stackdump(lua_State* L)
2010-10-17 15:24:55 +00:00
{
int i;
2010-10-18 10:26:15 +00:00
int top = lua_gettop(L);
2010-11-03 14:54:33 +00:00
printlogf(L, "Debug", "total in stack %d",top);
2010-10-17 15:24:55 +00:00
for (i = 1; i <= top; i++) {
2010-10-18 10:26:15 +00:00
int t = lua_type(L, i);
2010-10-17 15:24:55 +00:00
switch (t) {
2010-10-20 15:34:01 +00:00
case LUA_TSTRING:
2010-11-03 14:54:33 +00:00
printlogf(L, "Debug", "%d string: '%s'",
2010-10-27 19:34:56 +00:00
i, lua_tostring(L, i));
2010-10-20 15:34:01 +00:00
break;
case LUA_TBOOLEAN:
2010-11-03 14:54:33 +00:00
printlogf(L, "Debug", "%d boolean %s",
2010-10-27 19:34:56 +00:00
i, lua_toboolean(L, i) ? "true" : "false");
2010-10-20 15:34:01 +00:00
break;
case LUA_TNUMBER:
2010-11-03 14:54:33 +00:00
printlogf(L, "Debug", "%d number: %g",
2010-10-27 19:34:56 +00:00
i, lua_tonumber(L, i));
2010-10-20 15:34:01 +00:00
break;
default:
2010-11-03 14:54:33 +00:00
printlogf(L, "Debug", "%d %s",
2010-10-27 19:34:56 +00:00
i, lua_typename(L, t));
2010-10-20 15:34:01 +00:00
break;
2010-10-17 15:24:55 +00:00
}
2010-10-17 20:26:37 +00:00
}
2010-10-17 15:24:55 +00:00
return 0;
}
2010-10-16 18:21:01 +00:00
/**
2010-11-17 18:52:55 +00:00
* Reads the directories entries.
2010-11-20 14:21:55 +00:00
*
2010-10-18 09:02:51 +00:00
* @param (Lua stack) absolute path to directory.
* @return (Lua stack) a table of directory names.
2010-11-17 18:52:55 +00:00
* names are keys, values are boolean
* true on dirs.
2010-10-16 18:21:01 +00:00
*/
2010-10-17 15:24:55 +00:00
static int
2010-11-17 18:52:55 +00:00
l_readdir (lua_State *L)
2010-10-17 15:24:55 +00:00
{
2010-10-16 18:21:01 +00:00
const char * dirname = luaL_checkstring(L, 1);
DIR *d;
d = opendir(dirname);
2010-10-17 20:26:37 +00:00
if (d == NULL) {
2010-11-03 14:54:33 +00:00
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
}
lua_newtable(L);
2010-11-14 09:11:09 +00:00
while (!hup && !term) {
2010-10-16 18:21:01 +00:00
struct dirent *de = readdir(d);
bool isdir;
if (de == NULL) {
/* finished */
break;
}
2010-11-17 18:52:55 +00:00
if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
/* ignores . and .. */
continue;
}
2010-10-16 18:21:01 +00:00
if (de->d_type == DT_UNKNOWN) {
/* must call stat on some systems :-/ */
2010-11-17 18:52:55 +00:00
char *entry = s_malloc(strlen(dirname) + strlen(de->d_name) + 2);
2010-10-16 18:21:01 +00:00
struct stat st;
2010-11-17 18:52:55 +00:00
strcpy(entry, dirname);
strcat(entry, "/");
strcat(entry, de->d_name);
stat(entry, &st);
2010-10-16 18:21:01 +00:00
isdir = S_ISDIR(st.st_mode);
2010-11-17 18:52:55 +00:00
free(entry);
2010-10-16 18:21:01 +00:00
} else {
2010-10-18 17:09:59 +00:00
/* readdir can trusted */
2010-10-16 18:21:01 +00:00
isdir = de->d_type == DT_DIR;
}
2010-11-17 18:52:55 +00:00
/* adds this entry to the Lua table */
2010-10-16 18:21:01 +00:00
lua_pushstring(L, de->d_name);
2010-11-17 18:52:55 +00:00
lua_pushboolean(L, isdir);
2010-10-16 18:21:01 +00:00
lua_settable(L, -3);
}
2010-11-20 22:54:20 +00:00
closedir(d);
2010-10-16 18:21:01 +00:00
return 1;
}
2010-10-16 10:26:48 +00:00
2010-10-18 12:23:46 +00:00
/**
* Terminates lsyncd daemon.
*
* @param (Lua stack) exitcode for lsyncd.
*
* Does not return.
*/
int
l_terminate(lua_State *L)
{
int exitcode = luaL_checkinteger(L, 1);
exit(exitcode);
return 0;
}
2010-10-27 19:34:56 +00:00
/**
* Configures core parameters.
*
* @param (Lua stack) a string for a core configuratoin
* @param (Lua stack) --differes depending on string.
*/
static int
l_configure(lua_State *L)
{
const char * command = luaL_checkstring(L, 1);
2010-11-06 10:33:26 +00:00
if (!strcmp(command, "running")) {
2010-10-27 19:34:56 +00:00
/* set by runner after first initialize
* from this on log to configurated log end instead of
* stdout/stderr */
running = true;
2010-11-17 11:14:36 +00:00
if (settings.pidfile) {
write_pidfile(L, settings.pidfile);
}
2010-11-14 09:11:09 +00:00
if (!settings.nodaemon && !is_daemon) {
2010-11-13 21:50:21 +00:00
if (!settings.log_file) {
settings.log_syslog = true;
}
2010-11-20 13:22:47 +00:00
logstring("Debug", "daemonizing now.");
2010-11-13 21:50:21 +00:00
if (daemon(0, 0)) {
logstring("Error", "Failed to daemonize");
exit(-1); //ERRNO
}
is_daemon = true;
}
} else if (!strcmp(command, "nodaemon")) {
settings.nodaemon = true;
} else if (!strcmp(command, "logfile")) {
const char * file = luaL_checkstring(L, 2);
if (settings.log_file) {
free(settings.log_file);
}
settings.log_file = s_strdup(file);
2010-11-17 11:14:36 +00:00
} else if (!strcmp(command, "pidfile")) {
const char * file = luaL_checkstring(L, 2);
if (settings.pidfile) {
free(settings.pidfile);
}
settings.pidfile = s_strdup(file);
2010-10-27 19:34:56 +00:00
} else {
2010-11-03 14:54:33 +00:00
printlogf(L, "Error",
2010-10-27 19:34:56 +00:00
"Internal error, unknown parameter in l_configure(%s)",
command);
exit(-1); //ERRNO
}
return 0;
}
2010-10-19 20:14:55 +00:00
static const luaL_reg lsyncdlib[] = {
2010-11-06 15:08:17 +00:00
{"addtoclock", l_addtoclock },
2010-11-06 17:40:12 +00:00
{"clockbefore", l_clockbefore },
2010-11-06 15:08:17 +00:00
{"clockbeforeq", l_clockbeforeq },
2010-10-27 19:34:56 +00:00
{"configure", l_configure },
2010-10-24 16:41:58 +00:00
{"earlier", l_earlier },
{"exec", l_exec },
2010-10-22 23:14:11 +00:00
{"log", l_log },
{"now", l_now },
2010-11-17 18:52:55 +00:00
{"readdir", l_readdir },
2010-11-06 15:08:17 +00:00
{"realdir", l_realdir },
2010-10-22 23:14:11 +00:00
{"stackdump", l_stackdump },
{"terminate", l_terminate },
2010-10-19 20:14:55 +00:00
{NULL, NULL}
};
/*****************************************************************************
* Lsyncd Core
2010-11-03 14:54:33 +00:00
****************************************************************************/
2010-10-19 20:14:55 +00:00
2010-11-10 15:57:37 +00:00
/**
* Dummy variable whos address is used as the cores index in the lua registry
* to the lua runners function table in the lua registry.
*/
static int runner;
2010-11-10 15:57:37 +00:00
/**
* Dummy variable whos address is used as the cores index n the lua registry
* to the lua runners error handler.
*/
static int callError;
2010-11-04 13:43:57 +00:00
2010-11-10 15:57:37 +00:00
/**
* Pushes a function from the runner on the stack.
* Prior it pushed the callError handler.
*/
extern void
2010-11-11 20:43:20 +00:00
load_runner_func(lua_State *L,
const char *name)
2010-11-10 15:57:37 +00:00
{
printlogf(L, "Call", "%s()", name);
/* pushes the error handler */
lua_pushlightuserdata(L, (void *) &callError);
lua_gettable(L, LUA_REGISTRYINDEX);
/* pushes the function */
lua_pushlightuserdata(L, (void *) &runner);
lua_gettable(L, LUA_REGISTRYINDEX);
lua_pushstring(L, name);
lua_gettable(L, -2);
lua_remove(L, -2);
}
2010-10-20 15:34:01 +00:00
/**
* An observer to be called when a file descritor becomes
* read-ready or write-ready.
*/
struct observer {
/**
* The file descriptor to observe.
*/
int fd;
/**
* Function to call when read becomes ready.
*/
void (*ready)(lua_State *L, int fd, void *extra);
/**
* Function to call when write becomes ready.
*/
void (*writey)(lua_State *L, int fd, void *extra);
/**
* Extra tokens to pass to the functions-
*/
void *extra;
};
2010-10-28 17:56:33 +00:00
/**
* List of file descriptor watches.
2010-10-28 17:56:33 +00:00
*/
static struct observer * observers = NULL;
static int observers_len = 0;
static int observers_size = 0;
2010-10-28 17:56:33 +00:00
/**
* List of file descriptors to unobserve.
* While working for the oberver lists, it may
* not be altered, thus unobserve stores here the
* actions that will be delayed.
2010-10-28 17:56:33 +00:00
*/
static int *unobservers = NULL;
static int unobservers_len = 0;
static int unobservers_size = 0;
2010-10-19 20:14:55 +00:00
2010-10-19 16:40:49 +00:00
/**
* true while the observers list is being handled.
2010-10-19 16:40:49 +00:00
*/
static bool observer_action = false;
/**
* Core watches a filedescriptor to become ready,
* one of read_ready or write_ready may be zero
*/
extern void
observe_fd(int fd,
void (*ready)(lua_State *L, int fd, void *extra),
void (*writey)(lua_State *L, int fd, void *extra),
void *extra)
2010-11-11 19:52:20 +00:00
{
int pos;
if (observer_action) {
// TODO
logstring("Error",
"Adding observers in ready/writey handlers not yet supported");
exit(-1); // ERRNO
}
2010-10-20 10:25:34 +00:00
if (observers_len + 1 > observers_size) {
observers_size = observers_len + 1;
observers = s_realloc(observers,
observers_size * sizeof(struct observer));
2010-10-20 18:33:17 +00:00
}
for(pos = 0; pos < observers_len; pos++) {
if (observers[pos].fd <= fd) {
break;
2010-11-04 13:52:34 +00:00
}
2010-10-20 18:33:17 +00:00
}
if (observers[pos].fd == fd) {
logstring("Error",
"Observing already an observed file descriptor.");
exit(-1); // ERRNO
2010-11-12 09:45:22 +00:00
}
memmove(observers + pos + 1, observers + pos,
(observers_len - pos) * (sizeof(struct observer)));
observers_len++;
observers[pos].fd = fd;
observers[pos].ready = ready;
observers[pos].writey = writey;
observers[pos].extra = extra;
}
2010-10-20 13:01:26 +00:00
/**
* Makes core no longer watch fd.
*/
extern void
unobserve_fd(int fd)
{
int pos;
if (observer_action) {
/* this function is called through a ready/writey handler
* while the core works through the observer list, thus
* it does not alter the list, but stores this actions
* on a stack
*/
unobservers_len++;
if (unobservers_len > unobservers_size) {
unobservers_size = unobservers_len;
unobservers = s_realloc(unobservers,
unobservers_size * sizeof(int));
2010-10-19 16:40:49 +00:00
}
unobservers[unobservers_len - 1] = fd;
2010-10-20 13:01:26 +00:00
return;
2010-10-19 16:40:49 +00:00
}
2010-10-20 13:01:26 +00:00
/* looks for the fd */
for(pos = 0; pos < observers_len; pos++) {
if (observers[pos].fd == fd) {
break;
}
2010-11-04 13:52:34 +00:00
}
if (pos >= observers_len) {
logstring("Error",
"internal fail, not observer file descriptor in unobserve");
exit(-1); //ERRNO
2010-10-20 10:25:34 +00:00
}
/* and moves the list down */
memmove(observers + pos, observers + pos + 1,
(observers_len - pos) * (sizeof(struct observer)));
observers_len--;
2010-10-19 16:40:49 +00:00
}
2010-10-19 10:12:11 +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)
{
2010-11-14 09:11:09 +00:00
while(true) {
2010-10-24 16:41:58 +00:00
bool have_alarm;
2010-10-19 10:12:11 +00:00
clock_t now = times(NULL);
clock_t alarm_time;
2010-11-03 21:02:14 +00:00
/* queries runner about soonest alarm */
2010-11-10 22:03:02 +00:00
load_runner_func(L, "getAlarm");
2010-11-05 18:20:33 +00:00
if (lua_pcall(L, 0, 1, -2)) {
2010-11-04 13:52:34 +00:00
exit(-1); // ERRNO
}
2010-11-08 12:14:10 +00:00
if (lua_type(L, -1) == LUA_TBOOLEAN) {
have_alarm = lua_toboolean(L, -1);
} else {
have_alarm = true;
2010-11-05 18:20:33 +00:00
alarm_time = (clock_t) luaL_checkinteger(L, -1);
}
lua_pop(L, 2);
2010-10-19 10:12:11 +00:00
2010-11-04 13:43:57 +00:00
if (have_alarm && time_before_eq(alarm_time, now)) {
2010-10-27 19:34:56 +00:00
/* there is a delay that wants to be handled already thus do not
* read from inotify_fd and jump directly to its handling */
2010-11-03 15:56:23 +00:00
logstring("Masterloop", "immediately handling delays.");
2010-10-24 21:35:29 +00:00
} else {
2010-10-19 20:14:55 +00:00
/* use select() to determine what happens next
* + a new event on inotify
* + an alarm on timeout
* + 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
2010-10-24 21:35:29 +00:00
if (have_alarm) {
2010-11-04 13:43:57 +00:00
double d = ((double)(alarm_time - now)) / clocks_per_sec;
tv.tv_sec = d;
tv.tv_nsec = ((d - (long) d)) * 1000000000.0;
printlogf(L, "Masterloop",
"going into select (timeout %f seconds)", d);
2010-10-24 21:35:29 +00:00
} else {
2010-11-04 13:43:57 +00:00
logstring("Masterloop", "going into select (no timeout).");
2010-10-19 10:12:11 +00:00
}
/* time for Lsyncd to try to put itself to rest into a select(),
* configures timeouts, filedescriptors and signals
* that will wake it */
2010-11-12 15:39:43 +00:00
{
fd_set rfds;
fd_set wfds;
sigset_t sigset;
int pi, pr;
2010-11-12 15:39:43 +00:00
sigemptyset(&sigset);
2010-11-12 15:39:43 +00:00
FD_ZERO(&rfds);
FD_ZERO(&wfds);
for(pi = 0; pi < observers_len; pi++) {
int fd = observers[pi].fd;
if (observers[pi].ready) {
FD_SET(fd, &rfds);
}
if (observers[pi].writey) {
FD_SET(fd, &wfds);
}
2010-11-12 15:39:43 +00:00
}
/* the great select */
pr = pselect(
observers[observers_len - 1].fd + 1,
&rfds, &wfds, NULL,
2010-11-12 15:39:43 +00:00
have_alarm ? &tv : NULL, &sigset);
2010-10-23 12:36:55 +00:00
if (pr >= 0) {
/* walks through the observers calling ready/writey fds */
observer_action = true;
for(pi = 0; pi < observers_len; pi++) {
int fd = observers[pi].fd;
void *extra = observers[pi].extra;
if (hup || term) {
break;
}
if (observers[pi].ready && FD_ISSET(fd, &rfds)) {
observers[pi].ready(L, fd, extra);
}
if (hup || term) {
break;
}
if (unobservers_len > 0 &&
unobservers[unobservers_len - 1] == fd) {
/* ready() unobserved itself */
continue;
}
if (observers[pi].writey && FD_ISSET(fd, &wfds)) {
observers[pi].writey(L, fd, extra);
}
}
observer_action = false;
/* work through delayed unobserve_fd() calls */
for (pi = 0; pi < unobservers_len; pi++) {
unobserve_fd(unobservers[pi]);
}
unobservers_len = 0;
2010-11-12 15:39:43 +00:00
}
}
}
2010-10-28 17:56:33 +00:00
/* collects zombified child processes */
2010-10-23 19:19:46 +00:00
while(1) {
int status;
2010-10-24 13:52:28 +00:00
pid_t pid = waitpid(0, &status, WNOHANG);
2010-10-23 19:19:46 +00:00
if (pid <= 0) {
break;
}
2010-11-10 22:03:02 +00:00
load_runner_func(L, "collectProcess");
2010-10-23 19:19:46 +00:00
lua_pushinteger(L, pid);
lua_pushinteger(L, WEXITSTATUS(status));
2010-11-04 13:52:34 +00:00
if (lua_pcall(L, 2, 0, -4)) {
exit(-1); // ERRNO
}
2010-11-03 21:02:14 +00:00
lua_pop(L, 1);
2010-10-23 19:19:46 +00:00
}
/* reacts on signals */
2010-11-14 09:11:09 +00:00
if (hup) {
load_runner_func(L, "hup");
if (lua_pcall(L, 0, 0, -2)) {
exit(-1); // ERRNO
}
lua_pop(L, 1);
hup = 0;
}
/* reacts on signals */
2010-11-14 09:11:09 +00:00
if (term == 1) {
load_runner_func(L, "term");
if (lua_pcall(L, 0, 0, -2)) {
exit(-1); // ERRNO
}
lua_pop(L, 1);
term = 2;
}
2010-11-05 18:04:29 +00:00
/* lets the runner do stuff every cycle,
* like starting new processes, writing the statusfile etc. */
2010-11-10 22:03:02 +00:00
load_runner_func(L, "cycle");
2010-10-23 12:36:55 +00:00
lua_pushinteger(L, times(NULL));
2010-11-14 09:11:09 +00:00
if (lua_pcall(L, 1, 1, -3)) {
2010-11-04 13:52:34 +00:00
exit(-1); // ERRNO
}
2010-11-14 09:11:09 +00:00
if (!lua_toboolean(L, -1)) {
/* cycle told core to break mainloop */
lua_pop(L, 2);
return;
}
lua_pop(L, 2);
2010-10-19 10:12:11 +00:00
}
}
2010-10-18 10:26:15 +00:00
/**
* Main
*/
2010-10-17 15:24:55 +00:00
int
2010-11-14 09:11:09 +00:00
main1(int argc, char *argv[])
2010-10-14 13:52:01 +00:00
{
2010-10-27 19:34:56 +00:00
/* the Lua interpreter */
lua_State* L;
2010-11-03 21:02:14 +00:00
/* scripts */
char * lsyncd_runner_file = NULL;
char * lsyncd_config_file = NULL;
int argp = 1;
/* load Lua */
L = lua_open();
2010-11-11 19:52:20 +00:00
luaL_openlibs(L);
{
/* checks the lua version */
const char *version;
int major, minor;
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");
2010-11-14 09:11:09 +00:00
exit(-1); // ERRNO
2010-11-11 19:52:20 +00:00
}
if ((major < 5) || (major == 5 && minor < 1)) {
fprintf(stderr, "lua library is too old. Need 5.1 at least");
2010-11-14 09:11:09 +00:00
exit(-1); // ERRNO
2010-11-11 19:52:20 +00:00
}
lua_pop(L, 1);
}
2010-11-03 21:02:14 +00:00
2010-11-03 16:04:11 +00:00
{
2010-11-11 19:52:20 +00:00
/* prepares logging early */
2010-11-03 21:02:14 +00:00
int i = 1;
2010-11-03 16:04:11 +00:00
add_logcat("Normal", LOG_NOTICE);
add_logcat("Error", LOG_ERR);
while (i < argc) {
if (strcmp(argv[i], "-log") && strcmp(argv[i], "--log")) {
i++; continue;
}
if (++i >= argc) {
break;
}
2010-11-03 21:02:14 +00:00
if (!add_logcat(argv[i], LOG_NOTICE)) {
printlogf(L, "Error", "'%s' is not a valid logging category",
argv[i]);
2010-11-14 09:11:09 +00:00
exit(-1); // ERRNO
2010-11-03 16:04:11 +00:00
}
}
}
2010-11-03 14:54:33 +00:00
2010-11-11 19:52:20 +00:00
/* registers lsycnd core */
2010-10-17 15:24:55 +00:00
luaL_register(L, "lsyncd", lsyncdlib);
2010-10-18 10:26:15 +00:00
lua_setglobal(L, "lysncd");
lua_getglobal(L, "lysncd");
register_inotify(L);
lua_settable(L, -3);
lua_pop(L, 1);
2010-10-16 10:26:48 +00:00
2010-11-04 13:43:57 +00:00
if (check_logcat("Debug") >= settings.log_level) {
/* printlogf doesnt support %ld :-( */
printf("kernels clocks_per_sec=%ld\n", clocks_per_sec);
}
2010-10-27 09:06:13 +00:00
/* checks if the user overrode default runner file */
2010-11-03 21:02:14 +00:00
if (argp < argc && !strcmp(argv[argp], "--runner")) {
2010-11-04 14:23:34 +00:00
if (argp + 1 >= argc) {
2010-11-03 14:54:33 +00:00
logstring("Error",
"Lsyncd Lua-runner file missing after --runner.");
2010-10-27 19:34:56 +00:00
#ifdef LSYNCD_DEFAULT_RUNNER_FILE
2010-11-03 14:54:33 +00:00
printlogf(L, "Error",
2010-10-27 19:34:56 +00:00
"Using '%s' as default location for runner.",
LSYNCD_DEFAULT_RUNNER_FILE);
#else
2010-11-03 14:54:33 +00:00
logstring("Error",
2010-10-27 19:34:56 +00:00
"Using a staticly included runner as default.");
#endif
2010-11-20 13:22:47 +00:00
exit(-1); //ERRNO
2010-10-19 21:56:00 +00:00
}
lsyncd_runner_file = argv[argp + 1];
argp += 2;
} else {
2010-10-27 19:34:56 +00:00
#ifdef LSYNCD_DEFAULT_RUNNER_FILE
2010-10-19 21:56:00 +00:00
lsyncd_runner_file = LSYNCD_DEFAULT_RUNNER_FILE;
2010-10-27 19:34:56 +00:00
#endif
2010-10-19 21:56:00 +00:00
}
2010-10-27 19:34:56 +00:00
if (lsyncd_runner_file) {
/* checks if the runner file exists */
2010-10-19 21:56:00 +00:00
struct stat st;
if (stat(lsyncd_runner_file, &st)) {
2010-11-03 14:54:33 +00:00
printlogf(L, "Error",
2010-10-27 19:34:56 +00:00
"Cannot find Lsyncd Lua-runner at '%s'.", lsyncd_runner_file);
2010-11-03 14:54:33 +00:00
printlogf(L, "Error", "Maybe specify another place?");
printlogf(L, "Error",
2010-10-27 19:34:56 +00:00
"%s --runner RUNNER_FILE CONFIG_FILE", argv[0]);
2010-11-14 09:11:09 +00:00
exit(-1); // ERRNO
2010-10-27 19:34:56 +00:00
}
/* loads the runner file */
if (luaL_loadfile(L, lsyncd_runner_file)) {
2010-11-03 14:54:33 +00:00
printlogf(L, "Error",
2010-10-27 19:34:56 +00:00
"error loading '%s': %s",
lsyncd_runner_file, lua_tostring(L, -1));
2010-11-14 09:11:09 +00:00
exit(-1); // ERRNO
2010-10-19 21:56:00 +00:00
}
2010-11-03 21:02:14 +00:00
} else {
2010-10-27 19:34:56 +00:00
#ifndef LSYNCD_DEFAULT_RUNNER_FILE
/* loads the runner from binary */
if (luaL_loadbuffer(L, &_binary_luac_out_start,
&_binary_luac_out_end - &_binary_luac_out_start, "lsyncd.lua"))
{
2010-11-03 14:54:33 +00:00
printlogf(L, "Error",
2010-10-27 19:34:56 +00:00
"error loading precompiled lsyncd.lua runner: %s",
lua_tostring(L, -1));
2010-11-14 09:11:09 +00:00
exit(-1); // ERRNO
2010-10-27 19:34:56 +00:00
}
2010-10-26 20:29:12 +00:00
#else
2010-10-27 19:34:56 +00:00
/* this should never be possible, security code nevertheless */
2010-11-03 14:54:33 +00:00
logstring("Error",
2010-10-27 19:34:56 +00:00
"Internal fail: lsyncd_runner is NULL with non-static runner");
2010-11-14 09:11:09 +00:00
exit(-1); // ERRNO
2010-10-26 20:29:12 +00:00
#endif
2010-11-03 21:02:14 +00:00
}
2010-11-10 15:57:37 +00:00
{
/* place to store the lua runners functions */
/* executes the runner defining all its functions */
if (lua_pcall(L, 0, LUA_MULTRET, 0)) {
printlogf(L, "Error",
"error preparing '%s': %s",
lsyncd_runner_file ? lsyncd_runner_file : "internal runner",
lua_tostring(L, -1));
2010-11-14 09:11:09 +00:00
exit(-1); // ERRNO
2010-11-10 15:57:37 +00:00
}
lua_pushlightuserdata(L, (void *)&runner);
/* switches the value (result of preparing) and the key &runner */
lua_insert(L, 1);
/* saves the table of the runners functions in the lua registry */
lua_settable(L, LUA_REGISTRYINDEX);
/* saves the error function extra */
/* &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
{
2010-11-11 19:52:20 +00:00
/* asserts version match between runner and core */
2010-10-18 17:09:59 +00:00
const char *lversion;
lua_getglobal(L, "lsyncd_version");
lversion = luaL_checkstring(L, -1);
if (strcmp(lversion, PACKAGE_VERSION)) {
2010-11-03 14:54:33 +00:00
printlogf(L, "Error",
2010-10-27 19:34:56 +00:00
"Version mismatch '%s' is '%s', but core is '%s'",
lsyncd_runner_file ? lsyncd_runner_file : "internal runner",
lversion, PACKAGE_VERSION);
2010-11-14 09:11:09 +00:00
exit(-1); // ERRNO
2010-10-18 17:09:59 +00:00
}
2010-11-03 21:02:14 +00:00
lua_pop(L, 1);
2010-10-18 17:09:59 +00:00
}
2010-10-27 09:06:13 +00:00
{
2010-11-10 09:49:44 +00:00
/* checks if there is a "-help" or "--help" */
2010-10-27 09:06:13 +00:00
int i;
for(i = argp; i < argc; i++) {
if (!strcmp(argv[i],"-help") || !strcmp(argv[i],"--help")) {
2010-11-10 15:57:37 +00:00
load_runner_func(L, "help");
2010-11-04 13:52:34 +00:00
if (lua_pcall(L, 0, 0, -2)) {
exit(-1); // ERRNO
}
2010-11-03 21:02:14 +00:00
lua_pop(L, 1);
2010-11-14 09:11:09 +00:00
exit(-1); // ERRNO
2010-10-27 09:06:13 +00:00
}
}
}
2010-11-03 21:02:14 +00:00
{
2010-11-11 19:52:20 +00:00
/* starts the option parser in lua script */
2010-11-03 21:02:14 +00:00
int idx = 1;
const char *s;
/* creates a table with all remaining argv option arguments */
2010-11-10 15:57:37 +00:00
load_runner_func(L, "configure");
2010-11-03 21:02:14 +00:00
lua_newtable(L);
while(argp < argc) {
lua_pushnumber(L, idx++);
lua_pushstring(L, argv[argp++]);
lua_settable(L, -3);
}
2010-11-04 13:52:34 +00:00
if (lua_pcall(L, 1, 1, -3)) {
exit(-1); // ERRNO
}
2010-11-03 21:02:14 +00:00
s = lua_tostring(L, -1);
if (s) {
lsyncd_config_file = s_strdup(s);
}
2010-11-11 19:52:20 +00:00
lua_pop(L, 2);
2010-11-03 21:02:14 +00:00
}
if (lsyncd_config_file) {
/* checks existence of the config file */
2010-10-27 09:06:13 +00:00
struct stat st;
if (stat(lsyncd_config_file, &st)) {
2010-11-03 14:54:33 +00:00
printlogf(L, "Error",
"Cannot find config file at '%s'.",
2010-10-27 19:34:56 +00:00
lsyncd_config_file);
2010-11-14 09:11:09 +00:00
exit(-1); // ERRNO
2010-10-27 09:06:13 +00:00
}
2010-11-03 21:02:14 +00:00
/* loads and executes the config file */
if (luaL_loadfile(L, lsyncd_config_file)) {
printlogf(L, "Error",
"error loading %s: %s",
lsyncd_config_file, lua_tostring(L, -1));
2010-11-14 09:11:09 +00:00
exit(-1); // ERRNO
2010-11-03 21:02:14 +00:00
}
if (lua_pcall(L, 0, LUA_MULTRET, 0)) {
printlogf(L, "Error",
"error preparing %s: %s",
lsyncd_config_file, lua_tostring(L, -1));
2010-11-14 09:11:09 +00:00
exit(-1); // ERRNO
2010-11-03 21:02:14 +00:00
}
2010-10-17 17:13:53 +00:00
}
2010-10-16 10:26:48 +00:00
open_inotify(L);
2010-11-11 20:43:20 +00:00
2010-10-25 08:42:27 +00:00
{
2010-10-27 19:34:56 +00:00
/* adds signal handlers *
* listens to SIGCHLD, but blocks it until pselect()
* opens up*/
2010-10-25 08:42:27 +00:00
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGCHLD);
signal(SIGCHLD, sig_child);
2010-10-25 21:04:28 +00:00
sigprocmask(SIG_BLOCK, &set, NULL);
2010-11-14 09:11:09 +00:00
signal(SIGHUP, sig_handler);
signal(SIGTERM, sig_handler);
2010-10-25 08:42:27 +00:00
}
2010-10-24 21:35:29 +00:00
2010-10-25 21:04:28 +00:00
{
2010-11-03 21:02:14 +00:00
/* runs initialitions from runner
2010-10-27 19:34:56 +00:00
* lua code will set configuration and add watches */
2010-11-10 15:57:37 +00:00
load_runner_func(L, "initialize");
2010-11-04 13:52:34 +00:00
if (lua_pcall(L, 0, 0, -2)) {
exit(-1); // ERRNO
}
2010-11-03 21:02:14 +00:00
lua_pop(L, 1);
2010-10-25 21:04:28 +00:00
}
2010-10-18 17:09:59 +00:00
2010-10-19 10:12:11 +00:00
masterloop(L);
2010-10-18 17:09:59 +00:00
2010-10-17 15:24:55 +00:00
/* cleanup */
2010-11-14 09:11:09 +00:00
{
/* frees logging categories */
int ci;
struct logcat *lc;
for(ci = 'A'; ci <= 'Z'; ci++) {
for(lc = logcats[ci]; lc && lc->name; lc++) {
free(lc->name);
lc->name = NULL;
}
if (logcats[ci]) {
free(logcats[ci]);
}
}
}
if (lsyncd_config_file) {
free(lsyncd_config_file);
lsyncd_config_file = NULL;
}
/* resets settings to default. */
if (settings.log_file) {
free(settings.log_file);
settings.log_file = NULL;
}
settings.log_syslog = false,
settings.log_level = 0,
settings.nodaemon = false,
close_inotify();
2010-10-14 13:52:01 +00:00
lua_close(L);
return 0;
}
2010-11-14 09:11:09 +00:00
/**
* Main
*/
int
main(int argc, char *argv[])
{
/* kernel parameters */
clocks_per_sec = sysconf(_SC_CLK_TCK);
while(!term) {
main1(argc, argv);
}
return 0;
}