lsyncd/lsyncd.c

1404 lines
32 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>
*
* 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-10-14 13:56:23 +00:00
#include "config.h"
2010-10-16 10:26:48 +00:00
#define LUA_USE_APICHECK 1
#ifdef HAVE_SYS_INOTIFY_H
# include <sys/inotify.h>
#else
2010-10-27 19:34:56 +00:00
# error Missing <sys/inotify.h>; supply kernel-headers and rerun configure.
2010-10-16 10:26:48 +00:00
#endif
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>
#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-19 10:12:11 +00:00
/**
* Macros to compare times() values
* (borrowed from linux/jiffies.h)
*
* time_after(a,b) returns true if the time a is after time b.
*/
#define time_after(a,b) ((long)(b) - (long)(a) < 0)
#define time_before(a,b) time_after(b,a)
#define time_after_eq(a,b) ((long)(a) - (long)(b) >= 0)
#define time_before_eq(a,b) time_after_eq(b,a)
2010-10-20 15:34:01 +00:00
/**
* Event types core sends to runner.
*/
enum event_type {
2010-10-22 08:37:15 +00:00
NONE = 0,
ATTRIB = 1,
MODIFY = 2,
CREATE = 3,
DELETE = 4,
MOVE = 5,
2010-10-20 10:25:34 +00:00
};
2010-10-18 17:09:59 +00:00
/**
* The Lua part of lsyncd.
*/
2010-10-26 20:29:12 +00:00
//#define LSYNCD_DEFAULT_RUNNER_FILE "lsyncd.lua"
extern char _binary_luac_out_start;
extern char _binary_luac_out_end;
2010-10-20 15:34:01 +00:00
static char * lsyncd_runner_file = NULL;
static char * lsyncd_config_file = NULL;
2010-10-18 17:09:59 +00:00
2010-10-16 10:26:48 +00:00
/**
* The inotify file descriptor.
*/
static int inotify_fd;
2010-10-17 15:24:55 +00:00
/**
* TODO allow configure.
*/
2010-10-28 17:56:33 +00:00
static const uint32_t standard_event_mask =
2010-10-17 15:24:55 +00:00
IN_ATTRIB | IN_CLOSE_WRITE | IN_CREATE |
IN_DELETE | IN_DELETE_SELF | IN_MOVED_FROM |
IN_MOVED_TO | IN_DONT_FOLLOW | IN_ONLYDIR;
2010-10-20 15:34:01 +00:00
/**
* If not null lsyncd logs in this file.
*/
static char * logfile = NULL;
2010-10-18 17:09:59 +00:00
/**
2010-10-20 15:34:01 +00:00
* If true lsyncd sends log messages to syslog
2010-10-18 17:09:59 +00:00
*/
2010-10-20 15:34:01 +00:00
bool logsyslog = false;
/**
* lsyncd log level
*/
2010-10-27 19:34:56 +00:00
enum loglevel {
2010-10-20 15:34:01 +00:00
DEBUG = 1, /* Log debug messages */
VERBOSE = 2, /* Log more */
NORMAL = 3, /* Log short summeries */
ERROR = 4, /* Log severe errors only */
CORE = 0x80 /* Indicates a core message */
2010-10-27 19:34:56 +00:00
};
/**
* configuration parameters
*/
static struct settings {
2010-10-28 17:56:33 +00:00
/**
* threshold for log messages
*/
2010-10-27 19:34:56 +00:00
enum loglevel loglevel;
2010-10-28 17:56:33 +00:00
/**
* lsyncd will periodically write its status in this
* file if configured so. (for special observing only)
*/
char * statusfile;
2010-10-27 19:34:56 +00:00
} settings = {
.loglevel = DEBUG,
2010-10-28 17:56:33 +00:00
.statusfile = NULL,
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;
/**
* loglevel before user configuration took place
* set to DEBUG for upstart debugging, Normally its NORMAL.
*/
static const enum loglevel startup_loglevel = DEBUG;
2010-10-17 15:24:55 +00:00
2010-10-16 18:21:01 +00:00
/**
* Set to TERM or HUP in signal handler, when lsyncd should end or reset ASAP.
*/
2010-10-20 15:34:01 +00:00
static volatile sig_atomic_t reset = 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-10-20 18:33:17 +00:00
/**
* predeclerations -- see belorw.
*/
2010-11-03 14:54:33 +00:00
static void * s_calloc(size_t nmemb, size_t size);
static void * s_malloc(size_t size);
static void * s_realloc(void *ptr, size_t size);
static char * s_strdup(const char *src);
/*****************************************************************************
* Logging
****************************************************************************/
2010-11-03 15:23:36 +00:00
/**
* if true logs everything
*/
2010-11-03 16:04:11 +00:00
bool logall = false;
2010-11-03 15:23:36 +00:00
2010-10-20 18:33:17 +00:00
/* core logs with CORE flag */
2010-11-03 14:54:33 +00:00
#define logstring(cat, message) \
2010-11-03 15:23:36 +00:00
{int p; if ((p = check_logcat(cat)) >= 0 || logall) \
2010-11-03 14:54:33 +00:00
{logstring0(p, cat, message); }}
2010-10-20 18:33:17 +00:00
static void
2010-11-03 11:37:25 +00:00
printlogf0(lua_State *L,
2010-11-03 14:54:33 +00:00
int priority,
const char *cat,
const char *fmt,
...)
__attribute__((format(printf, 4, 5)));
#define printlogf(L, cat, ...) \
2010-11-03 15:23:36 +00:00
{int p; if ((p = check_logcat(cat)) >= 0 || logall) \
2010-11-03 14:54:33 +00:00
{printlogf0(L, p, cat, __VA_ARGS__);}}
2010-10-20 18:33:17 +00:00
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
*/
2010-11-03 14:54:33 +00:00
static int
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-10-18 17:09:59 +00:00
*/
2010-11-03 14:54:33 +00:00
static void
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)) {
logall = true;
return;
}
2010-11-03 14:54:33 +00:00
/* category must start with capital letter */
if (name[0] < 'A' || name[0] > 'Z') {
return;
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 */
for(lc = logcats[name[0]-'A']; lc->name; lc++);
}
lc->name = s_strdup(name);
lc->priority = priority;
/* terminates the list */
lc[1].name = NULL;
2010-10-18 17:09:59 +00:00
}
2010-10-20 18:33:17 +00:00
/**
2010-11-03 14:54:33 +00:00
* Logs a string.
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
*/
static 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 14:54:33 +00:00
if (priority <= LOG_ERR) {
2010-10-27 19:34:56 +00:00
fprintf(stderr, "%s\n", message);
2010-11-03 14:54:33 +00:00
} else {
2010-10-27 19:34:56 +00:00
printf("%s\n", message);
}
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-10-20 18:33:17 +00:00
if (logfile) {
FILE * flog = fopen(logfile, "a");
/* 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 14:54:33 +00:00
fprintf(stderr, "Cannot open logfile [%s]!\n", logfile);
2010-10-20 18:33:17 +00:00
exit(-1); // ERRNO
}
2010-11-03 14:54:33 +00:00
fprintf(flog, "%s %s: %s", 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-10-20 18:33:17 +00:00
if (logsyslog) {
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-03 14:54:33 +00:00
/*****************************************************************************
* Simple memory management
****************************************************************************/
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-03 14:54:33 +00:00
void *
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.
*/
static void *
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.
*/
static void *
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.
*/
static char *
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-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-10-16 10:26:48 +00:00
/**
2010-10-17 15:24:55 +00:00
* Adds an inotify watch
*
2010-10-18 09:02:51 +00:00
* @param dir (Lua stack) path to directory
* @return (Lua stack) numeric watch descriptor
2010-10-16 10:26:48 +00:00
*/
2010-10-17 15:24:55 +00:00
static int
2010-10-18 09:02:51 +00:00
l_add_watch(lua_State *L)
2010-10-16 18:21:01 +00:00
{
2010-10-17 15:24:55 +00:00
const char *path = luaL_checkstring(L, 1);
lua_Integer wd = inotify_add_watch(inotify_fd, path, standard_event_mask);
lua_pushinteger(L, wd);
return 1;
2010-10-16 10:26:48 +00:00
}
2010-11-02 21:04:01 +00:00
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 */
if (priority < 0) {
2010-10-20 15:34:01 +00:00
return 0;
}
2010-11-03 14:54:33 +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-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
l_before_eq(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_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-10-25 08:50:10 +00:00
* @param1 the clock timer
* @param2 seconds to change clock.
2010-10-22 23:14:11 +00:00
* TODO
*/
static int
2010-10-25 08:50:10 +00:00
l_addto_clock(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
* @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-10-17 20:26:37 +00:00
const char *binary = luaL_checkstring(L, 1);
2010-10-17 17:13:53 +00:00
int argc = lua_gettop(L) - 1;
2010-10-17 20:26:37 +00:00
pid_t pid;
2010-10-17 17:13:53 +00:00
int i;
2010-10-17 20:26:37 +00:00
char const **argv = s_calloc(argc + 2, sizeof(char *));
argv[0] = binary;
2010-10-18 09:02:51 +00:00
for(i = 1; i <= argc; i++) {
2010-10-17 20:26:37 +00:00
argv[i] = luaL_checkstring(L, i + 1);
}
argv[i] = NULL;
pid = fork();
if (pid == 0) {
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. */
if (is_daemon && logfile) {
if (!freopen(logfile, "a", stdout)) {
2010-11-03 14:54:33 +00:00
printlogf(L, "Error",
2010-10-28 17:56:33 +00:00
"cannot redirect stdout to '%s'.", logfile);
}
if (!freopen(logfile, "a", stderr)) {
2010-11-03 14:54:33 +00:00
printlogf(L, "Error",
2010-10-28 17:56:33 +00:00
"cannot redirect stderr to '%s'.", logfile);
}
}
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-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 18:21:01 +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-10-18 09:02:51 +00:00
l_real_dir(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
/**
* Reads the directories sub directories.
*
2010-10-18 09:02:51 +00:00
* @param (Lua stack) absolute path to directory.
* @return (Lua stack) a table of directory names.
2010-10-16 18:21:01 +00:00
*/
2010-10-17 15:24:55 +00:00
static int
2010-10-18 09:02:51 +00:00
l_sub_dirs (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;
int idx = 1;
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);
while (!reset) {
struct dirent *de = readdir(d);
bool isdir;
if (de == NULL) {
/* finished */
break;
}
if (de->d_type == DT_UNKNOWN) {
/* must call stat on some systems :-/ */
2010-10-17 17:13:53 +00:00
char *subdir = s_malloc(strlen(dirname) + strlen(de->d_name) + 2);
2010-10-16 18:21:01 +00:00
struct stat st;
strcpy(subdir, dirname);
strcat(subdir, "/");
strcat(subdir, de->d_name);
stat(subdir, &st);
isdir = S_ISDIR(st.st_mode);
free(subdir);
} 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-10-27 19:34:56 +00:00
if (!isdir || !strcmp(de->d_name, ".") ||
!strcmp(de->d_name, ".."))
{ /* ignore non directories and . and .. */
2010-10-16 18:21:01 +00:00
continue;
}
2010-10-18 09:02:51 +00:00
/* add this to the Lua table */
2010-10-16 18:21:01 +00:00
lua_pushnumber(L, idx++);
lua_pushstring(L, de->d_name);
lua_settable(L, -3);
}
return 1;
}
2010-10-16 10:26:48 +00:00
2010-10-28 17:56:33 +00:00
/**
* Writes a string to a file descriptor
*
* @param (Lua Stack) file descriptor
* @param (Lua Stack) string.
*/
int
l_writefd(lua_State *L)
{
int fd = luaL_checkinteger(L, 1);
2010-11-02 17:07:42 +00:00
/* concates if there is more than one string parameter */
lua_concat(L, lua_gettop(L) - 1);
{
const char *s = luaL_checkstring(L, 2);
write(fd, s, strlen(s));
}
2010-10-28 17:56:33 +00:00
return 0;
}
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-18 10:26:15 +00:00
/**
2010-10-22 12:58:57 +00:00
* Suspends execution until a table of child processes returned.
2010-10-18 10:26:15 +00:00
*
2010-10-24 13:52:28 +00:00
* @param (Lua stack) a table of the process ids.
2010-10-19 20:14:55 +00:00
* @param (Lua stack) a function of a collector to be called
2010-10-24 13:52:28 +00:00
* when a process finishes.
2010-10-18 10:26:15 +00:00
*/
2010-10-19 21:56:00 +00:00
int
2010-10-19 20:14:55 +00:00
l_wait_pids(lua_State *L)
2010-10-18 10:26:15 +00:00
{
2010-10-18 12:23:46 +00:00
/* the number of pids in table */
int pidn;
/* the pid table */
int *pids;
2010-10-24 13:52:28 +00:00
/* the number of processes to be waited for */
2010-10-18 12:23:46 +00:00
int remaining = 0;
int i;
2010-10-19 20:14:55 +00:00
/* global function to call on finished processes */
const char * collector;
2010-10-18 12:23:46 +00:00
/* checks if Lua script returned a table */
2010-10-19 20:14:55 +00:00
luaL_checktype(L, 1, LUA_TTABLE);
if (lua_type(L, 2) == LUA_TNIL) {
collector = NULL;
} else {
collector = luaL_checkstring(L, 2);
2010-10-18 10:26:15 +00:00
}
2010-10-19 20:14:55 +00:00
2010-10-18 12:23:46 +00:00
/* determines size of the pid-table */
2010-10-19 20:14:55 +00:00
pidn = lua_objlen (L, 1);
2010-10-18 10:26:15 +00:00
if (pidn == 0) {
/* nothing to do on zero pids */
2010-10-19 21:56:00 +00:00
return 0;
2010-10-18 10:26:15 +00:00
}
2010-10-18 12:23:46 +00:00
/* reads the pid table from Lua stack */
2010-10-18 10:26:15 +00:00
pids = s_calloc(pidn, sizeof(int));
for(i = 0; i < pidn; i++) {
2010-10-19 20:14:55 +00:00
lua_rawgeti(L, 1, i + 1);
2010-10-18 12:23:46 +00:00
pids[i] = luaL_checkinteger(L, -1);
lua_pop(L, 1);
/* ignores zero pids */
if (pids[i]) {
remaining++;
}
2010-10-18 10:26:15 +00:00
}
2010-10-24 13:52:28 +00:00
/* starts waiting for the processes */
2010-10-18 12:23:46 +00:00
while(remaining) {
2010-10-24 13:52:28 +00:00
/* argument for waitpid, and exitcode of process */
2010-10-18 12:23:46 +00:00
int status, exitcode;
/* new process id in case of retry */
int newp;
/* process id of terminated child process */
int wp = waitpid(0, &status, 0);
/* if nothing really finished ignore */
if (wp == 0 || !WIFEXITED(status)) {
continue;
}
exitcode = WEXITSTATUS(status);
/* checks if the pid is one waited for */
for(i = 0; i < pidn; i++) {
if (pids[i] == wp) {
break;
}
}
if (i >= pidn) {
/* not a pid waited for */
continue;
}
2010-10-19 20:14:55 +00:00
/* calls the lua collector to determine further actions */
if (collector) {
lua_getglobal(L, collector);
lua_pushinteger(L, wp);
lua_pushinteger(L, exitcode);
2010-11-03 15:23:36 +00:00
printlogf(L, "Call", "startup collector");
2010-10-19 20:14:55 +00:00
lua_call(L, 2, 1);
newp = luaL_checkinteger(L, -1);
lua_pop(L, 1);
} else {
newp = 0;
}
2010-10-18 12:23:46 +00:00
/* replace the new pid in the pidtable,
or zero it on no new pid */
for(i = 0; i < pidn; i++) {
if (pids[i] == wp) {
pids[i] = newp;
if (newp == 0) {
remaining--;
}
2010-10-27 19:34:56 +00:00
/* does not break!
* in case there are duplicate pids (why-ever) */
2010-10-18 12:23:46 +00:00
}
}
}
2010-10-18 10:26:15 +00:00
free(pids);
2010-10-19 21:56:00 +00:00
return 0;
2010-10-18 10:26:15 +00:00
}
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-10-28 17:56:33 +00:00
if (!strcmp(command, "statusfile")) {
/* configures the status file lsyncd will dump its status to */
if (settings.statusfile) {
free(settings.statusfile);
2010-10-27 19:34:56 +00:00
}
2010-10-28 17:56:33 +00:00
settings.statusfile = s_strdup(luaL_checkstring(L, 2));
2010-10-27 19:34:56 +00:00
} else if (!strcmp(command, "running")) {
/* set by runner after first initialize
* from this on log to configurated log end instead of
* stdout/stderr */
running = true;
} 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 16:40:49 +00:00
2010-10-19 20:14:55 +00:00
static const luaL_reg lsyncdlib[] = {
2010-10-22 23:14:11 +00:00
{"add_watch", l_add_watch },
2010-10-25 08:50:10 +00:00
{"addto_clock", l_addto_clock },
2010-10-24 16:41:58 +00:00
{"before_eq", l_before_eq },
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-10-28 17:56:33 +00:00
{"writefd", l_writefd },
2010-10-22 23:14:11 +00:00
{"real_dir", l_real_dir },
{"stackdump", l_stackdump },
{"sub_dirs", l_sub_dirs },
{"terminate", l_terminate },
{"wait_pids", l_wait_pids },
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-10-20 15:34:01 +00:00
/**
2010-10-20 18:33:17 +00:00
* Let the core print logmessage comfortably.
2010-10-20 15:34:01 +00:00
*/
static void
2010-11-03 11:37:25 +00:00
printlogf0(lua_State *L,
2010-11-03 14:54:33 +00:00
int priority,
const char *cat,
2010-11-02 21:04:01 +00:00
const char *fmt, ...)
2010-10-20 15:34:01 +00:00
{
va_list ap;
va_start(ap, fmt);
lua_pushvfstring(L, fmt, ap);
va_end(ap);
2010-11-03 14:54:33 +00:00
logstring0(priority, cat, luaL_checkstring(L, -1));
2010-11-03 11:37:25 +00:00
lua_pop(L, 1);
2010-10-20 15:34:01 +00:00
return;
}
2010-10-19 20:14:55 +00:00
/**
* Buffer for MOVE_FROM events.
* Lsyncd buffers MOVE_FROM events to check if
*/
struct inotify_event * move_event_buf = NULL;
2010-10-28 17:56:33 +00:00
/**
* Memory allocated for move_event_buf
*/
2010-10-19 20:14:55 +00:00
size_t move_event_buf_size = 0;
2010-10-28 17:56:33 +00:00
/**
* true if the buffer is used.
*/
2010-10-20 10:25:34 +00:00
bool move_event = false;
2010-10-19 20:14:55 +00:00
2010-10-19 16:40:49 +00:00
/**
* Handles an inotify event.
*/
void handle_event(lua_State *L, struct inotify_event *event) {
2010-10-20 10:25:34 +00:00
/* TODO */
int event_type = NONE;
/* used to execute two events in case of unmatched MOVE_FROM buffer */
struct inotify_event *after_buf = NULL;
2010-11-03 14:54:33 +00:00
logstring("Inotify", "got an event");
2010-10-20 10:25:34 +00:00
2010-10-20 18:33:17 +00:00
if (reset) {
return;
}
if (event && (IN_Q_OVERFLOW & event->mask)) {
/* and overflow happened, lets runner/user decide what to do. */
lua_getglobal(L, "overflow");
2010-11-03 15:23:36 +00:00
printlogf(L, "Call", "overflow()");
2010-10-20 18:33:17 +00:00
lua_call(L, 0, 0);
return;
}
/* cancel on ignored or resetting */
if (event && (IN_IGNORED & event->mask)) {
return;
2010-10-20 13:01:26 +00:00
}
2010-10-20 10:25:34 +00:00
if (event == NULL) {
2010-10-22 08:37:15 +00:00
/* a buffered MOVE_FROM is not followed by anything,
thus it is unary */
2010-10-20 10:25:34 +00:00
event = move_event_buf;
2010-10-20 13:01:26 +00:00
event_type = DELETE;
2010-10-20 10:25:34 +00:00
move_event = false;
2010-10-22 08:37:15 +00:00
} else if (move_event &&
( !(IN_MOVED_TO & event->mask) ||
event->cookie != move_event_buf->cookie) ) {
/* there is a MOVE_FROM event in the buffer and this is not the match
* continue in this function iteration to handler the buffer instead */
2010-10-20 10:25:34 +00:00
after_buf = event;
2010-10-20 13:01:26 +00:00
event = move_event_buf;
2010-10-20 10:25:34 +00:00
event_type = DELETE;
move_event = false;
2010-10-22 08:37:15 +00:00
} else if ( move_event &&
(IN_MOVED_TO & event->mask) &&
event->cookie == move_event_buf->cookie ) {
2010-10-20 10:25:34 +00:00
/* this is indeed a matched move */
event_type = MOVE;
move_event = false;
} else if (IN_MOVED_FROM & event->mask) {
2010-10-27 19:34:56 +00:00
/* just the MOVE_FROM, buffers this event, and wait if next event is
* a matching MOVED_TO of this was an unary move out of the watched
* tree. */
2010-10-22 08:37:15 +00:00
size_t el = sizeof(struct inotify_event) + event->len;
if (move_event_buf_size < el) {
move_event_buf_size = el;
move_event_buf = s_realloc(move_event_buf, el);
2010-10-19 16:40:49 +00:00
}
2010-10-22 08:37:15 +00:00
memcpy(move_event_buf, event, el);
2010-10-20 13:01:26 +00:00
move_event = true;
2010-10-20 10:25:34 +00:00
return;
2010-10-20 13:01:26 +00:00
} else if (IN_MOVED_TO & event->mask) {
2010-10-22 08:37:15 +00:00
/* must be an unary move-to */
2010-10-20 13:01:26 +00:00
event_type = CREATE;
} else if (IN_MOVED_FROM & event->mask) {
2010-10-22 08:37:15 +00:00
/* must be an unary move-from */
2010-10-20 10:25:34 +00:00
event_type = DELETE;
} else if (IN_ATTRIB & event->mask) {
2010-10-20 13:01:26 +00:00
/* just attrib change */
2010-10-20 10:25:34 +00:00
event_type = ATTRIB;
} else if (IN_CLOSE_WRITE & event->mask) {
2010-10-20 13:01:26 +00:00
/* closed after written something */
2010-10-20 10:25:34 +00:00
event_type = MODIFY;
} else if (IN_CREATE & event->mask) {
2010-10-20 13:01:26 +00:00
/* a new file */
2010-10-20 10:25:34 +00:00
event_type = CREATE;
} else if (IN_DELETE & event->mask) {
2010-10-20 13:01:26 +00:00
/* rm'ed */
2010-10-20 10:25:34 +00:00
event_type = DELETE;
2010-10-20 13:01:26 +00:00
} else {
2010-11-03 14:54:33 +00:00
logstring("Inotify", "skipped some inotify event.");
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
2010-10-22 08:37:15 +00:00
/* and hands over to runner */
2010-11-02 14:13:45 +00:00
lua_getglobal(L, "lsyncd_inotify_event");
2010-11-01 19:57:53 +00:00
switch(event_type) {
case ATTRIB : lua_pushstring(L, "Attrib"); break;
case MODIFY : lua_pushstring(L, "Modify"); break;
case CREATE : lua_pushstring(L, "Create"); break;
case DELETE : lua_pushstring(L, "Delete"); break;
case MOVE : lua_pushstring(L, "Move"); break;
default :
2010-11-03 14:54:33 +00:00
logstring("Inotify", "Internal: unknown event in handle_event()");
2010-11-01 19:57:53 +00:00
exit(-1); // ERRNO
}
2010-10-20 13:01:26 +00:00
lua_pushnumber(L, event->wd);
2010-10-22 08:34:41 +00:00
lua_pushboolean(L, (event->mask & IN_ISDIR) != 0);
2010-10-24 16:41:58 +00:00
lua_pushinteger(L, times(NULL));
2010-10-20 13:01:26 +00:00
if (event_type == MOVE) {
lua_pushstring(L, move_event_buf->name);
lua_pushstring(L, event->name);
} else {
lua_pushstring(L, event->name);
lua_pushnil(L);
}
2010-11-03 15:23:36 +00:00
printlogf(L, "Call", "lysncd_inotify_event()");
2010-10-24 16:41:58 +00:00
lua_call(L, 6, 0);
2010-10-19 16:40:49 +00:00
2010-10-22 08:37:15 +00:00
/* if there is a buffered event executes it */
2010-10-20 10:25:34 +00:00
if (after_buf) {
handle_event(L, after_buf);
}
2010-10-19 16:40:49 +00:00
}
2010-10-19 10:12:11 +00:00
/**
* Normal operation happens in here.
*/
void
masterloop(lua_State *L)
{
2010-10-19 20:14:55 +00:00
size_t readbuf_size = 2048;
char *readbuf = s_malloc(readbuf_size);
2010-10-19 10:12:11 +00:00
while(!reset) {
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-10-24 21:35:29 +00:00
int do_read;
2010-10-19 10:12:11 +00:00
ssize_t len;
/* query runner about soonest alarm */
lua_getglobal(L, "lsyncd_get_alarm");
2010-11-03 15:23:36 +00:00
printlogf(L, "Call", "lsycnd_get_alarm()");
2010-10-24 16:41:58 +00:00
lua_call(L, 0, 2);
have_alarm = lua_toboolean(L, -2);
alarm_time = (clock_t) luaL_checkinteger(L, -1);
2010-10-19 10:12:11 +00:00
lua_pop(L, 2);
2010-10-24 16:41:58 +00:00
if (have_alarm && time_before(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-19 10:12:11 +00:00
do_read = 0;
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
sigset_t sigset;
2010-10-19 10:12:11 +00:00
fd_set readfds;
2010-10-25 08:42:27 +00:00
struct timespec tv;
sigemptyset(&sigset);
2010-10-19 10:12:11 +00:00
2010-10-24 21:35:29 +00:00
if (have_alarm) {
2010-11-03 15:56:23 +00:00
logstring("Masterloop", "going into timed select.");
2010-10-24 21:35:29 +00:00
tv.tv_sec = (alarm_time - now) / clocks_per_sec;
2010-10-27 19:34:56 +00:00
tv.tv_nsec = (alarm_time - now) *
1000000000 / clocks_per_sec % 1000000000;
2010-10-24 21:35:29 +00:00
} else {
2010-11-03 15:56:23 +00:00
logstring("Masterloop", "going into blocking select.");
2010-10-19 10:12:11 +00:00
}
2010-10-24 16:41:58 +00:00
/* if select returns a positive number there is data on inotify
* on zero the timemout occured. */
2010-10-19 10:12:11 +00:00
FD_ZERO(&readfds);
FD_SET(inotify_fd, &readfds);
2010-10-27 19:34:56 +00:00
do_read = pselect(inotify_fd + 1, &readfds, NULL, NULL,
have_alarm ? &tv : NULL, &sigset);
2010-10-19 10:12:11 +00:00
2010-11-03 15:56:23 +00:00
logstring("Masterloop", do_read > 0 ?
2010-10-27 19:34:56 +00:00
"theres data on inotify." :
"core: select() timeout or signal.");
2010-10-24 21:35:29 +00:00
}
2010-10-19 16:40:49 +00:00
/* reads possible events from inotify stream */
2010-10-24 21:35:29 +00:00
while(do_read > 0) {
2010-10-19 16:40:49 +00:00
int i = 0;
2010-10-22 08:37:15 +00:00
do {
len = read (inotify_fd, readbuf, readbuf_size);
if (len < 0 && errno == EINVAL) {
/* kernel > 2.6.21 indicates that way that way that
* the buffer was too small to fit a filename.
* double its size and try again. When using a lower
* kernel and a filename > 2KB appears lsyncd
* will fail. (but does a 2KB filename really happen?)*/
readbuf_size *= 2;
readbuf = s_realloc(readbuf, readbuf_size);
continue;
}
} while(0);
2010-10-19 16:40:49 +00:00
while (i < len && !reset) {
2010-10-27 19:34:56 +00:00
struct inotify_event *event =
(struct inotify_event *) &readbuf[i];
2010-10-19 16:40:49 +00:00
handle_event(L, event);
i += sizeof(struct inotify_event) + event->len;
}
/* check if there is more data */
2010-10-22 08:37:15 +00:00
{
2010-10-25 08:42:27 +00:00
struct timespec tv = {.tv_sec = 0, .tv_nsec = 0};
2010-10-19 16:40:49 +00:00
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(inotify_fd, &readfds);
2010-10-27 19:34:56 +00:00
do_read = pselect(inotify_fd + 1, &readfds,
NULL, NULL, &tv, NULL);
2010-10-24 21:35:29 +00:00
if (do_read > 0) {
2010-11-03 15:56:23 +00:00
logstring("Masterloop", "there is more data on inotify.");
2010-10-19 16:40:49 +00:00
}
}
2010-10-22 08:37:15 +00:00
}
/* checks if there is an unary MOVE_FROM left in the buffer */
2010-10-20 13:01:26 +00:00
if (move_event) {
handle_event(L, NULL);
}
2010-10-23 12:36:55 +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-10-24 13:52:28 +00:00
lua_getglobal(L, "lsyncd_collect_process");
2010-10-23 19:19:46 +00:00
lua_pushinteger(L, pid);
lua_pushinteger(L, WEXITSTATUS(status));
2010-11-03 15:23:36 +00:00
printlogf(L, "Call", "lsyncd_collect_process()");
2010-10-23 19:19:46 +00:00
lua_call(L, 2, 0);
}
2010-10-28 17:56:33 +00:00
/* writes status of lsyncd in a file */
/* this is not a real loop, it will only be runned once max.
* this is just using break as comfortable jump down. */
while (settings.statusfile) {
2010-10-29 05:52:38 +00:00
int fd = open(settings.statusfile,
O_WRONLY | O_CREAT | O_TRUNC, 0664);
2010-10-28 17:56:33 +00:00
if (fd < 0) {
2010-11-03 14:54:33 +00:00
printlogf(L, "Error",
2010-10-28 17:56:33 +00:00
"Cannot open statusfile '%s' for writing.",
settings.statusfile);
break;
}
/* calls the lua runner to write the status. */
2010-11-03 11:37:25 +00:00
lua_getglobal(L, "lsyncd_call_error");
2010-10-28 17:56:33 +00:00
lua_getglobal(L, "lsyncd_status_report");
lua_pushinteger(L, fd);
2010-11-03 15:23:36 +00:00
printlogf(L, "Call", "lysncd_status_report()");
2010-11-03 11:37:25 +00:00
lua_pcall(L, 1, 0, -3);
2010-10-28 17:56:33 +00:00
/* TODO */
fsync(fd);
close(fd);
break;
}
/* lets the runner spawn new processes */
2010-10-23 12:36:55 +00:00
lua_getglobal(L, "lsyncd_alarm");
lua_pushinteger(L, times(NULL));
2010-11-03 15:23:36 +00:00
printlogf(L, "Call", "lsyncd_alarm()");
2010-10-23 12:36:55 +00:00
lua_call(L, 1, 0);
2010-10-19 10:12:11 +00:00
}
}
2010-10-27 09:06:13 +00:00
/**
2010-10-28 17:56:33 +00:00
* Prints a minimal help if e.g. config_file is missing.
2010-10-27 09:06:13 +00:00
*/
2010-10-27 19:34:56 +00:00
void minihelp(char *arg0)
2010-10-27 09:06:13 +00:00
{
fprintf(stderr, "Missing config file\n");
fprintf(stderr, "Minimal Usage: %s CONFIG_FILE\n", arg0);
fprintf(stderr, " Specify -help for more help.\n");
}
2010-10-18 10:26:15 +00:00
/**
* Main
*/
2010-10-17 15:24:55 +00:00
int
main(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 16:04:11 +00:00
{
add_logcat("Normal", LOG_NOTICE);
add_logcat("Error", LOG_ERR);
/* Prepates logging early */
int i = 1;
while (i < argc) {
if (strcmp(argv[i], "-log") && strcmp(argv[i], "--log")) {
i++; continue;
}
if (++i >= argc) {
break;
}
if (argv[i][0] < 'A' || argv[i][0] > 'Z') {
XXX
}
add_logcat(argv[i], LOG_NOTICE);
}
}
2010-11-03 14:54:33 +00:00
2010-10-19 21:56:00 +00:00
/* position at cores (minimal) argument parsing *
* most arguments are parsed in the lua runner */
int argp = 1;
2010-10-27 09:06:13 +00:00
if (argc <= 1) {
2010-10-27 19:34:56 +00:00
minihelp(argv[0]);
2010-10-27 09:06:13 +00:00
return -1;
2010-10-19 21:56:00 +00:00
}
2010-10-19 10:12:11 +00:00
/* kernel parameters */
clocks_per_sec = sysconf(_SC_CLK_TCK);
2010-10-16 10:26:48 +00:00
/* load Lua */
2010-10-14 13:52:01 +00:00
L = lua_open();
2010-10-27 19:34:56 +00:00
/* TODO check lua version */
2010-10-14 13:52:01 +00:00
luaL_openlibs(L);
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");
2010-10-16 10:26:48 +00:00
2010-10-27 09:06:13 +00:00
/* checks if the user overrode default runner file */
2010-10-19 21:56:00 +00:00
if (!strcmp(argv[argp], "--runner")) {
if (argc < 3) {
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-10-19 21:56:00 +00:00
return -1; //ERRNO
}
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]);
return -1; // ERRNO
}
/* 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-10-19 21:56:00 +00:00
return -1; // ERRNO
}
}
2010-10-27 19:34:56 +00:00
#ifndef LSYNCD_DEFAULT_RUNNER_FILE
else {
/* 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));
return -1; // ERRNO
}
2010-10-16 10:26:48 +00:00
}
2010-10-26 20:29:12 +00:00
#else
2010-10-27 19:34:56 +00:00
else {
/* 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-10-26 20:29:12 +00:00
return -1; // ERRNO
}
#endif
2010-10-27 09:06:13 +00:00
/* execute the runner defining all its functions */
2010-10-16 18:21:01 +00:00
if (lua_pcall(L, 0, LUA_MULTRET, 0)) {
2010-11-03 14:54:33 +00:00
printlogf(L, "Error",
2010-10-27 19:34:56 +00:00
"error preparing '%s': %s",
lsyncd_runner_file ? lsyncd_runner_file : "internal runner",
lua_tostring(L, -1));
2010-10-17 17:13:53 +00:00
return -1; // ERRNO
}
2010-10-18 17:09:59 +00:00
{
/* checks version match between runner/core */
const char *lversion;
lua_getglobal(L, "lsyncd_version");
lversion = luaL_checkstring(L, -1);
lua_pop(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-10-18 17:09:59 +00:00
return -1; // ERRNO
}
}
2010-10-27 09:06:13 +00:00
{
2010-10-27 19:34:56 +00:00
/* checks if there is a "-help" or "--help" before anything else */
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")) {
lua_getglobal(L, "lsyncd_help");
lua_call(L, 0, 0);
return -1; // ERRNO
}
}
}
{
2010-10-27 19:34:56 +00:00
/* checks for the configuration and existence of the config file */
2010-10-27 09:06:13 +00:00
struct stat st;
2010-10-27 19:34:56 +00:00
if (argp + 1 > argc) {
minihelp(argv[0]);
return -1; // ERRNO
}
lsyncd_config_file = argv[argp++];
2010-10-27 09:06:13 +00:00
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-10-27 09:06:13 +00:00
return -1; // ERRNO
}
}
2010-10-27 19:34:56 +00:00
/* loads and executes the config file */
2010-10-19 21:56:00 +00:00
if (luaL_loadfile(L, lsyncd_config_file)) {
2010-11-03 14:54:33 +00:00
printlogf(L, "Error",
"error loading %s: %s",
2010-10-27 19:34:56 +00:00
lsyncd_config_file, lua_tostring(L, -1));
2010-10-16 18:21:01 +00:00
return -1; // ERRNO
}
2010-10-17 17:13:53 +00:00
if (lua_pcall(L, 0, LUA_MULTRET, 0)) {
2010-11-03 14:54:33 +00:00
printlogf(L, "Error",
"error preparing %s: %s",
2010-10-27 19:34:56 +00:00
lsyncd_config_file, lua_tostring(L, -1));
2010-10-17 17:13:53 +00:00
return -1; // ERRNO
}
2010-10-16 10:26:48 +00:00
2010-10-27 19:34:56 +00:00
/* opens inotify */
2010-10-17 20:26:37 +00:00
inotify_fd = inotify_init();
if (inotify_fd == -1) {
2010-11-03 14:54:33 +00:00
printlogf(L, "Error",
2010-10-27 19:34:56 +00:00
"Cannot create inotify instance! (%d:%s)",
errno, strerror(errno));
2010-10-17 20:26:37 +00:00
return -1; // ERRNO
}
2010-10-16 10:26:48 +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-10-25 08:42:27 +00:00
}
2010-10-24 21:35:29 +00:00
2010-10-25 21:04:28 +00:00
{
2010-10-27 19:34:56 +00:00
/* runs initialitions from runner *
* lua code will set configuration and add watches */
2010-10-25 21:41:45 +00:00
int idx = 1;
2010-10-27 19:34:56 +00:00
/* creates a table with all remaining argv option arguments */
2010-10-25 21:04:28 +00:00
lua_getglobal(L, "lsyncd_initialize");
2010-10-25 21:41:45 +00:00
lua_newtable(L);
while(argp < argc) {
lua_pushnumber(L, idx++);
lua_pushstring(L, argv[argp++]);
lua_settable(L, -3);
}
lua_call(L, 1, 0);
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-10-16 10:26:48 +00:00
close(inotify_fd);
2010-10-14 13:52:01 +00:00
lua_close(L);
return 0;
}