This commit is contained in:
Axel Kittenberger 2010-10-17 17:13:53 +00:00
parent 61932c0d0e
commit 1caf4a52ad
3 changed files with 91 additions and 19 deletions

View File

@ -5,3 +5,4 @@ settings = {
add("s", "d") add("s", "d")
-- add("s/s1", "t") -- add("s/s1", "t")

View File

@ -44,8 +44,34 @@ const uint32_t standard_event_mask =
*/ */
volatile sig_atomic_t reset = 0; volatile sig_atomic_t reset = 0;
/* the Lua interpreter */ /**
lua_State* L; * "secured" calloc.
*/
void *
s_calloc(size_t nmemb, size_t size)
{
void *r = calloc(nmemb, size);
if (r == NULL) {
printf("Out of memory!\n");
exit(-1); // ERRNO
}
return r;
}
/**
* "secured" malloc. the deamon shall kill itself
* in case of out of memory.
*/
void *
s_malloc(size_t size)
{
void *r = malloc(size);
if (r == NULL) {
printf("Out of memory!\n");
exit(-1); // ERRNO
}
return r;
}
/** /**
@ -64,6 +90,23 @@ add_watch(lua_State *L)
} }
/**
* Executes a subprocess
*/
static int
exec(lua_State *L)
{
const char *path = luaL_checkstring(L, 1);
int argc = lua_gettop(L) - 1;
int i;
const char **argv = s_calloc(argc + 1, sizeof(char *));
for(i = 0; i < argc; i++) {
argv[i] = luaL_checkstring(L, i + 2);
}
return 0;
}
/** /**
* Converts a relative directory path to an absolute. * Converts a relative directory path to an absolute.
@ -134,7 +177,6 @@ stackdump(lua_State* l)
return 0; return 0;
} }
/** /**
* Reads the directories sub directories. * Reads the directories sub directories.
* *
@ -155,7 +197,6 @@ sub_dirs (lua_State *L)
} }
lua_newtable(L); lua_newtable(L);
while (!reset) { while (!reset) {
struct dirent *de = readdir(d); struct dirent *de = readdir(d);
bool isdir; bool isdir;
@ -165,12 +206,7 @@ sub_dirs (lua_State *L)
} }
if (de->d_type == DT_UNKNOWN) { if (de->d_type == DT_UNKNOWN) {
/* must call stat on some systems :-/ */ /* must call stat on some systems :-/ */
char *subdir = malloc(strlen(dirname) + strlen(de->d_name) + 2); char *subdir = s_malloc(strlen(dirname) + strlen(de->d_name) + 2);
if (subdir == NULL) {
printf("Out of memory. Cannot determine type of %s/%s\n", dirname, de->d_name);
/* otherwise not so dramatic */
continue;
}
struct stat st; struct stat st;
strcpy(subdir, dirname); strcpy(subdir, dirname);
strcat(subdir, "/"); strcat(subdir, "/");
@ -197,6 +233,7 @@ sub_dirs (lua_State *L)
static const luaL_reg lsyncdlib[] = { static const luaL_reg lsyncdlib[] = {
{"add_watch", add_watch}, {"add_watch", add_watch},
{"exec", exec},
{"real_dir", real_dir}, {"real_dir", real_dir},
{"stackdump", stackdump}, {"stackdump", stackdump},
{"sub_dirs", sub_dirs}, {"sub_dirs", sub_dirs},
@ -206,22 +243,31 @@ static const luaL_reg lsyncdlib[] = {
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
/* the Lua interpreter */
lua_State* L;
/* load Lua */ /* load Lua */
L = lua_open(); L = lua_open();
luaL_openlibs(L); luaL_openlibs(L);
luaL_register(L, "lsyncd", lsyncdlib); luaL_register(L, "lsyncd", lsyncdlib);
luaL_loadfile(L, "lsyncd.lua"); if (luaL_loadfile(L, "lsyncd.lua")) {
if (lua_pcall(L, 0, LUA_MULTRET, 0)) {
printf("error loading lsyncd.lua: %s\n", lua_tostring(L, -1)); printf("error loading lsyncd.lua: %s\n", lua_tostring(L, -1));
return -1; // ERRNO return -1; // ERRNO
} }
luaL_loadfile(L, "lsyncd-conf.lua");
if (lua_pcall(L, 0, LUA_MULTRET, 0)) { if (lua_pcall(L, 0, LUA_MULTRET, 0)) {
printf("error running lsyncd.lua: %s\n", lua_tostring(L, -1));
return -1; // ERRNO
}
if (luaL_loadfile(L, "lsyncd-conf.lua")) {
printf("error loading lsyncd-conf.lua: %s\n", lua_tostring(L, -1)); printf("error loading lsyncd-conf.lua: %s\n", lua_tostring(L, -1));
return -1; // ERRNO return -1; // ERRNO
} }
if (lua_pcall(L, 0, LUA_MULTRET, 0)) {
printf("error running lsyncd-conf.lua: %s\n", lua_tostring(L, -1));
return -1; // ERRNO
}
/* open inotify */ /* open inotify */
inotify_fd = inotify_init(); inotify_fd = inotify_init();
@ -235,6 +281,10 @@ main(int argc, char *argv[])
lua_getglobal(L, "lsyncd_initialize"); lua_getglobal(L, "lsyncd_initialize");
lua_call(L, 0, 0); lua_call(L, 0, 0);
/* startup */
lua_getglobal(L, "startup");
lua_call(L, 0, 0);
/* cleanup */ /* cleanup */
close(inotify_fd); close(inotify_fd);
lua_close(L); lua_close(L);

View File

@ -42,7 +42,7 @@ local watches = {}
-- @param target -- @param target
-- @param ... -- @param ...
local function attend_dir(origin, path, target) local function attend_dir(origin, path, target)
print("attending dir", origin, "+", path, "->", target.dirname); print("attending dir", origin, "+", path, "->", target.path);
-- actual dir = origin + path -- actual dir = origin + path
local op = origin .. path local op = origin .. path
-- register watch and receive watch descriptor -- register watch and receive watch descriptor
@ -78,9 +78,10 @@ function lsyncd_initialize()
print("--- INIT ---") print("--- INIT ---")
local i, o local i, o
for i, o in ipairs(origin) do for i, o in ipairs(origin) do
print("Handling ", o.source, "->" , o.target) print("Handling ", o.source, "->" , o.targetpath)
local target = { dirname = o.target } local target = { path = o.targetpath }
table.insert(targets, target) table.insert(targets, target)
origin[i].target = target
attend_dir(lsyncd.real_dir(o.source), "", target) attend_dir(lsyncd.real_dir(o.source), "", target)
end end
end end
@ -92,10 +93,30 @@ end
---- ----
-- Add one directory to be watched. -- Add one directory to be watched.
function add(source, target) function add(source_dir, target_path)
local o = { source = source, target = target } local o = { source = source_dir, targetpath = target_path }
table.insert(origin, o) table.insert(origin, o)
return o return o
end end
-----
-- Called by core after initialization.
--
-- Returns a table of integers (pid of children) the core will
-- wait for before entering normal operation.
--
-- User can override this function by specifing his/her own
-- "startup". (and yet may still call default startup)
function default_startup()
print("--- STARTUP ---")
local pids = { }
for i, o in ipairs(origin) do
pid = lsyncd.exec("/usr/bin/rsyc", "-ltrs", o.source, o.targetpath)
print("started ", pid)
table.insert(pids, pid)
end
return pids
end
startup = default_startup