summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Pointner <equinox@anylike.org>2009-12-25 17:24:48 +0000
committerChristian Pointner <equinox@anylike.org>2009-12-25 17:24:48 +0000
commit45009626de760b1f97be4d41beae610621c65ae2 (patch)
treee1e4e7bcdab28455323c1d01f18a6c2e6e1496a3
parenthandling return value of main_loop (diff)
options get passed to main_loop lua function
-rw-r--r--src/anylike.c108
-rw-r--r--src/main_loop.lua14
2 files changed, 91 insertions, 31 deletions
diff --git a/src/anylike.c b/src/anylike.c
index d42dc4e..3eb91cc 100644
--- a/src/anylike.c
+++ b/src/anylike.c
@@ -55,6 +55,7 @@ int init_libgcrypt()
}
gcry_error_t err = gcry_control(GCRYCTL_DISABLE_SECMEM, 0);
+
if(err) {
log_printf(ERROR, "failed to disable secure memory: %s", gcry_strerror(err));
return -1;
@@ -72,7 +73,8 @@ int init_libgcrypt()
#endif
-#define LUA_MAIN_LOOP "main_loop.lua"
+#define LUA_MAIN_LOOP_FILE "main_loop.lua"
+#define LUA_MAIN_LOOP_FUNC "main_loop"
static const luaL_Reg anylike_lualibs[] = {
{"", luaopen_base},
@@ -84,7 +86,7 @@ static const luaL_Reg anylike_lualibs[] = {
{NULL, NULL}
};
-void anylike_openlibs(lua_State *L)
+int init_main_loop(lua_State *L)
{
const luaL_Reg *lib = anylike_lualibs;
for (; lib->func; lib++) {
@@ -92,33 +94,20 @@ void anylike_openlibs(lua_State *L)
lua_pushstring(L, lib->name);
lua_call(L, 1, 0);
}
-}
-int main_loop(options_t* opt)
-{
- lua_State *L;
- L = luaL_newstate();
- if(!L) {
- log_printf(ERROR, "error creating lua state");
- return -1;
- }
-
- anylike_openlibs(L);
-
- int ret = luaL_loadfile(L, LUA_MAIN_LOOP);
+ int ret = luaL_loadfile(L, LUA_MAIN_LOOP_FILE);
if(ret) {
const char* err_str = luaL_checkstring(L, -1);
switch(ret) {
- case LUA_ERRSYNTAX: log_printf(ERROR, "luaL_loadfile(%s) syntax error: %s", LUA_MAIN_LOOP, err_str); break;
- case LUA_ERRMEM: log_printf(ERROR, "luaL_loadfile(%s) malloc error: %s", LUA_MAIN_LOOP, err_str); break;
- case LUA_ERRFILE: log_printf(ERROR, "luaL_loadfile(%s) file access error: %s", LUA_MAIN_LOOP, err_str); break;
- default: log_printf(ERROR, "luaL_loadfile(%s) unknown error: %s", LUA_MAIN_LOOP, err_str); break;
+ case LUA_ERRSYNTAX: log_printf(ERROR, "luaL_loadfile(%s) syntax error: %s", LUA_MAIN_LOOP_FILE, err_str); break;
+ case LUA_ERRMEM: log_printf(ERROR, "luaL_loadfile(%s) malloc error: %s", LUA_MAIN_LOOP_FILE, err_str); break;
+ case LUA_ERRFILE: log_printf(ERROR, "luaL_loadfile(%s) file access error: %s", LUA_MAIN_LOOP_FILE, err_str); break;
+ default: log_printf(ERROR, "luaL_loadfile(%s) unknown error: %s", LUA_MAIN_LOOP_FILE, err_str); break;
}
- lua_close(L);
return -1;
}
- ret = lua_pcall(L, 0, LUA_MULTRET, 0);
+ ret = lua_pcall(L, 0, 0, 0);
if(ret) {
const char* err_str = luaL_checkstring(L, -1);
switch(ret) {
@@ -126,17 +115,90 @@ int main_loop(options_t* opt)
case LUA_ERRMEM: log_printf(ERROR, "lua_pcall() malloc error: %s", err_str); break;
case LUA_ERRERR: log_printf(ERROR, "lua_pcall() error at error handler function: %s", err_str); break;
}
- lua_close(L);
+ return -1;
+ }
+
+ return 0;
+}
+
+void push_opt_value_string(lua_State* L, const int tidx, const char* key, const char* value)
+{
+ lua_pushstring(L, key);
+ lua_pushstring(L, value);
+ lua_settable(L, tidx);
+}
+
+void push_opt_value_int(lua_State* L, const int tidx, const char* key, const u_int32_t value)
+{
+ lua_pushstring(L, key);
+ lua_pushinteger(L, value);
+ lua_settable(L, tidx);
+}
+
+void push_opt_value_boolean(lua_State* L, const int tidx, const char* key, const u_int32_t value)
+{
+ lua_pushstring(L, key);
+ lua_pushboolean(L, value);
+ lua_settable(L, tidx);
+}
+
+void push_opt_table(lua_State* L, options_t* opt)
+{
+ lua_newtable(L);
+
+ push_opt_value_string(L, -3, "progname", opt->progname_);
+ push_opt_value_boolean(L, -3, "daemonize", opt->daemonize_);
+ push_opt_value_string(L, -3, "username", opt->username_);
+ push_opt_value_string(L, -3, "groupname", opt->groupname_);
+ push_opt_value_string(L, -3, "chroot_dir", opt->chroot_dir_);
+ push_opt_value_string(L, -3, "pid_file", opt->pid_file_);
+
+// string_list_t log_targets_;
+}
+
+int call_main_loop(lua_State* L, options_t* opt)
+{
+ lua_getglobal(L, LUA_MAIN_LOOP_FUNC);
+ if(!lua_isfunction(L, -1)) {
+ log_printf(ERROR, "there is no function '%s' in '%s'", LUA_MAIN_LOOP_FUNC, LUA_MAIN_LOOP_FILE);
+ return -1;
+ };
+ push_opt_table(L, opt);
+
+ int ret = lua_pcall(L, 1, LUA_MULTRET, 0);
+ if(ret) {
+ const char* err_str = luaL_checkstring(L, -1);
+ switch(ret) {
+ case LUA_ERRRUN: log_printf(ERROR, "lua_pcall(%s) runtime error: %s", LUA_MAIN_LOOP_FUNC, err_str); break;
+ case LUA_ERRMEM: log_printf(ERROR, "lua_pcall(%s) malloc error: %s", LUA_MAIN_LOOP_FUNC, err_str); break;
+ case LUA_ERRERR: log_printf(ERROR, "lua_pcall(%s) error at error handler function: %s", LUA_MAIN_LOOP_FUNC, err_str); break;
+ }
return -1;
}
int n = lua_gettop(L);
- log_printf(DEBUG, "%s returned %d values", LUA_MAIN_LOOP, n);
+ log_printf(DEBUG, "%s returned %d values", LUA_MAIN_LOOP_FUNC, n);
int i;
for (i = 1; i <= n; i++)
log_printf(DEBUG, "return value [%d] = '%s'", i, luaL_checkstring(L, i));
ret = lua_tointeger(L, 1);
+ return ret;
+}
+
+int main_loop(options_t* opt)
+{
+ lua_State *L;
+ L = luaL_newstate();
+ if(!L) {
+ log_printf(ERROR, "error creating lua state");
+ return -1;
+ }
+
+ int ret = init_main_loop(L);
+ if(!ret)
+ ret = call_main_loop(L, opt);
+
lua_close(L);
return ret;
}
diff --git a/src/main_loop.lua b/src/main_loop.lua
index b3a0c28..82e3148 100644
--- a/src/main_loop.lua
+++ b/src/main_loop.lua
@@ -23,12 +23,10 @@
-- along with anylike. If not, see <http://www.gnu.org/licenses/>.
--
-log.print("NOTICE", "main_loop started");
+function main_loop(opt)
+ log.print("NOTICE", "main_loop started");
-log.print("DEBUG", "yet another message");
-
-log.print("ERROR", "this is an error");
-
-log.print("NOTICE", crypt.test());
-
-return 0, "hello", "world";
+ -- add main loop
+
+ return 0; -- =0 -> OK, <0 -> error, >0 -> signal
+end