summaryrefslogtreecommitdiff
path: root/luaclient.c
diff options
context:
space:
mode:
Diffstat (limited to 'luaclient.c')
-rw-r--r--luaclient.c268
1 files changed, 0 insertions, 268 deletions
diff --git a/luaclient.c b/luaclient.c
deleted file mode 100644
index 901d509..0000000
--- a/luaclient.c
+++ /dev/null
@@ -1,268 +0,0 @@
-/*
- * rhctl
- *
- * Copyright (C) 2009-2014 Christian Pointner <equinox@helsinki.at>
- *
- * This file is part of rhctl.
- *
- * rhctl is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * any later version.
- *
- * rhctl is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with rhctl. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#include "datatypes.h"
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <errno.h>
-
-#include <lua.h>
-#include <lualib.h>
-#include <lauxlib.h>
-
-#include "l_log.h"
-#include "l_sig_handler.h"
-#include "l_cmd.h"
-
-#include "log.h"
-#include "options.h"
-
-#include "daemon.h"
-#include "utils.h"
-
-
-#define LUA_MAIN_LOOP_FUNC "main_loop"
-
-static const luaL_Reg anylike_lualibs[] = {
- {"", luaopen_base},
- {LUA_LOADLIBNAME, luaopen_package},
- {LUA_TABLIBNAME, luaopen_table},
- {LUA_STRLIBNAME, luaopen_string},
- {LUA_MATHLIBNAME, luaopen_math},
- {LUA_IOLIBNAME, luaopen_io},
- {LUA_OSLIBNAME, luaopen_os},
- {LUA_LOGLIBNAME, luaopen_log},
- {LUA_SIGNALLIBNAME, luaopen_signal},
- {LUA_CMDLIBNAME, luaopen_cmd},
- {NULL, NULL}
-};
-
-int init_main_loop(lua_State *L, const char* filename)
-{
- const luaL_Reg *lib = anylike_lualibs;
- for (; lib->func; lib++) {
- lua_pushcfunction(L, lib->func);
- lua_pushstring(L, lib->name);
- lua_call(L, 1, 0);
- }
-
- int ret = luaL_loadfile(L, filename);
- if(ret) {
- const char* err_str = luaL_checkstring(L, -1);
- switch(ret) {
- case LUA_ERRSYNTAX: log_printf(ERROR, "luaL_loadfile(%s) syntax error: %s", filename, err_str); break;
- case LUA_ERRMEM: log_printf(ERROR, "luaL_loadfile(%s) malloc error: %s", filename, err_str); break;
- case LUA_ERRFILE: log_printf(ERROR, "luaL_loadfile(%s) file access error: %s", filename, err_str); break;
- default: log_printf(ERROR, "luaL_loadfile(%s) unknown error: %s", filename, err_str); break;
- }
- return -1;
- }
-
- ret = lua_pcall(L, 0, 0, 0);
- if(ret) {
- const char* err_str = luaL_checkstring(L, -1);
- switch(ret) {
- case LUA_ERRRUN: log_printf(ERROR, "lua_pcall() runtime error: %s", err_str); break;
- 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;
- }
- return -1;
- }
-
- return 0;
-}
-
-int call_main_loop(lua_State* L, int cmd_fd, options_t* opt)
-{
- lua_getglobal(L, LUA_MAIN_LOOP_FUNC);
- if(!lua_isfunction(L, -1)) {
- log_printf(ERROR, "there is no function '%s' inside the loaded file", LUA_MAIN_LOOP_FUNC);
- return -1;
- };
-
- options_lua_push(opt, L);
-
- 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_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(int cmd_fd, 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, opt->lua_file_);
- if(!ret)
- ret = call_main_loop(L, cmd_fd, opt);
-
- lua_close(L);
- return ret;
-}
-
-int main(int argc, char* argv[])
-{
- log_init();
-
- options_t opt;
- int ret = options_parse(&opt, argc, argv);
- if(ret) {
- if(ret > 0) {
- fprintf(stderr, "syntax error near: %s\n\n", argv[ret]);
- }
- if(ret == -2) {
- fprintf(stderr, "memory error on options_parse, exiting\n");
- }
-
- if(ret != -2)
- options_print_usage();
-
- options_clear(&opt);
- log_close();
- exit(ret);
- }
- string_list_element_t* tmp = opt.log_targets_.first_;
- if(!tmp) {
- log_add_target("syslog:3,luaclient,daemon");
- }
- else {
- while(tmp) {
- ret = log_add_target(tmp->string_);
- if(ret) {
- switch(ret) {
- case -2: fprintf(stderr, "memory error on log_add_target, exitting\n"); break;
- case -3: fprintf(stderr, "unknown log target: '%s', exitting\n", tmp->string_); break;
- case -4: fprintf(stderr, "this log target is only allowed once: '%s', exitting\n", tmp->string_); break;
- default: fprintf(stderr, "syntax error near: '%s', exitting\n", tmp->string_); break;
- }
-
- options_clear(&opt);
- log_close();
- exit(ret);
- }
- tmp = tmp->next_;
- }
- }
- log_printf(NOTICE, "just started...");
- if(options_parse_post(&opt)) {
- options_clear(&opt);
- log_close();
- exit(-1);
- }
-
- priv_info_t priv;
- if(opt.username_)
- if(priv_init(&priv, opt.username_, opt.groupname_)) {
- options_clear(&opt);
- log_close();
- exit(-1);
- }
-
- FILE* pid_file = NULL;
- if(opt.pid_file_) {
- pid_file = fopen(opt.pid_file_, "w");
- if(!pid_file) {
- log_printf(WARNING, "unable to open pid file: %s", strerror(errno));
- }
- }
-
- if(opt.chroot_dir_)
- if(do_chroot(opt.chroot_dir_)) {
- options_clear(&opt);
- log_close();
- exit(-1);
- }
- if(opt.username_)
- if(priv_drop(&priv)) {
- options_clear(&opt);
- log_close();
- exit(-1);
- }
-
- if(opt.daemonize_) {
- pid_t oldpid = getpid();
- daemonize();
- log_printf(INFO, "running in background now (old pid: %d)", oldpid);
- }
-
- if(pid_file) {
- pid_t pid = getpid();
- fprintf(pid_file, "%d", pid);
- fclose(pid_file);
- }
-
- for(;;) {
- // extern global variable defined in l_cmd.c
- cmd_fd = connect_command_socket(opt.command_sock_);
- if(cmd_fd < 0)
- ret = 2;
- else {
- ret = main_loop(cmd_fd, &opt);
- }
-
- if(ret == 2) {
- log_printf(ERROR, "socket error, trying to reconnect in 5 seconds..");
-
- if(cmd_fd > 0)
- close(cmd_fd);
- sleep(5);
- }
- else
- break;
- }
-
- if(cmd_fd > 0)
- close(cmd_fd);
-
- if(!ret)
- log_printf(NOTICE, "normal shutdown");
- else if(ret < 0)
- log_printf(NOTICE, "shutdown after error (code %d)", ret);
- else
- log_printf(NOTICE, "shutdown after signal");
-
- options_clear(&opt);
- log_close();
-
- return ret;
-}