diff options
author | FreeArtMan <dos21h@gmail.com> | 2017-11-19 12:09:13 +0000 |
---|---|---|
committer | FreeArtMan <dos21h@gmail.com> | 2017-11-19 12:09:13 +0000 |
commit | 4b86ab3af6e64c0c4bb533766c284507f0d3ee62 (patch) | |
tree | 88d02d1122a77f90878b0633c68a2002245cc052 | |
parent | d23ebff6250c7816bd3b15df06b35d4b2bb97874 (diff) | |
download | agni-4b86ab3af6e64c0c4bb533766c284507f0d3ee62.tar.gz agni-4b86ab3af6e64c0c4bb533766c284507f0d3ee62.zip |
Now lua commands supported
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | cmd/cmd_lua.c | 263 | ||||
-rw-r--r-- | cmd/cmd_lua.h | 6 | ||||
-rw-r--r-- | config_all_cmds.h | 2 | ||||
-rw-r--r-- | config_cmds.h | 3 | ||||
-rw-r--r-- | tbl_qcmd.c | 43 | ||||
-rw-r--r-- | version.h | 4 |
7 files changed, 291 insertions, 32 deletions
@@ -3,7 +3,7 @@ CC=gcc CFLAGS=-g3 -Wall SOURCES=$(wildcard *.c) OBJECTS=$(SOURCES:.c=.o) -LDFLAGS=-lrt -lm -lsqlite3 +LDFLAGS=-lrt -lm -lsqlite3 -llua INCLUDE=-I./extlibs/ -I./ all: createcmd createextlib $(OBJECTS) $(PROJECT) diff --git a/cmd/cmd_lua.c b/cmd/cmd_lua.c index a4d7573..105cb47 100644 --- a/cmd/cmd_lua.c +++ b/cmd/cmd_lua.c @@ -1,46 +1,255 @@ #include "cmd_lua.h" -#define BILLION 1000000000L +void fatal_error(lua_State *L, char *msg) +{ + fprintf(stderr, "\n FATAL ERROR:\n %s %s\n\n", msg, lua_tostring(L,-1)); +} + +int check_table_key_string(lua_State *L, const char *key, const char **value, size_t *size) +{ + const char *result; + size_t result_sz; + int t; + + lua_pushstring(L, key); + lua_gettable(L,1); + t = lua_type(L,-1); + if (t == LUA_TSTRING) + { + result = luaL_checklstring(L,-1,&result_sz); + *value = result; + *size = result_sz; + return 0; + } else + { + *value = NULL; + *size = 0; + return -1; + } +} + +int check_table_key_integer(lua_State *L, const char *key, int *value) +{ + int result; + int t; + + lua_pushstring(L, key); + lua_gettable(L,1); + if ((t = lua_type(L, -1)) == LUA_TNUMBER) + { + result = luaL_checkinteger(L,-1); + *value = result; + return 0; + } + + return -1; +} + +int verify_name(const char *lua_name) +{ + int i; + + //check if all symbols are alphanumeric + for (i=0;i<strlen(lua_name);i++) + { + if (!isdigit(lua_name[i]) && !isalpha(lua_name[i])) + { + return -1; + } + } + return 0; +} + +int lua_excute(const char *fname, rpc_call_request *req, rpc_call_response **resp) +{ + lua_State *L; + sds lua_full_path = sdsempty(); + + L = luaL_newstate(); + luaL_openlibs(L); + + if (verify_name(fname) != 0) + { + return -1; + } + + lua_full_path = sdscat(lua_full_path, "lua/"); + lua_full_path = sdscat(lua_full_path, fname); + lua_full_path = sdscat(lua_full_path, ".lua"); + + if (luaL_loadfile(L,lua_full_path)) + { + fatal_error(L, "load file failed"); + sdsfree(lua_full_path); + return -1; + } + sdsfree(lua_full_path); + + if (lua_pcall(L,0,0,0)) + { + fatal_error(L, "lua_pcall failed"); + return -1; + } + + lua_getglobal(L, "callback"); + lua_newtable(L); + + lua_pushstring(L,"method"); + lua_pushstring(L, req->method); + lua_settable(L,-3); + + lua_pushstring(L,"params"); + lua_pushstring(L, req->params); + lua_settable(L,-3); + + lua_pushstring(L,"id"); + lua_pushinteger(L, req->id); + lua_settable(L,-3); + + lua_pushstring(L,"user"); + lua_pushstring(L, req->user); + lua_settable(L,-3); + + lua_pushstring(L,"mask"); + lua_pushstring(L, req->mask); + lua_settable(L,-3); + + lua_pushstring(L,"server"); + lua_pushstring(L, req->server); + lua_settable(L,-3); + + if (lua_pcall(L,1,1,0)) + { + fatal_error(L, "cant call callback function from file"); + return -1; + } + + { + int t; + int top = lua_gettop(L); + + if (top == 1) + { + t = lua_type(L,1); + if (t == LUA_TTABLE) + { + int j; + int len = lua_rawlen(L,1); + const char *r_result; + const char *r_error; + int r_id; + size_t sz; + + check_table_key_string(L, "result", &r_result, &sz); + //resp->result = alloc_new_str(result); + printf("RESULT:%s\n", r_result); + check_table_key_integer(L, "id", &r_id); + //resp->id = result_i; + printf("ID:%d\n", r_id); + check_table_key_string(L, "error", &r_error, &sz); + //resp->error = alloc_new_str(result); + printf("ERROR:%s\n", r_error); + + *resp = rpc_call_resp_new(alloc_new_str(r_result), alloc_new_str(r_error), 1); + (*resp)->user = alloc_new_str(" "); + (*resp)->server = alloc_new_str(" "); + (*resp)->mask = alloc_new_str(" "); + + } + } else + { + fatal_error(L,"Some unexpected argumetns"); + return -1; + } + } + + lua_close(L); + return 0; +} void *cmd_lua(void *data) { char *ret = NULL; - int fret=-1; + char *req_data = NULL; + int fret; + int rc; + int i; - const int buf_size = 128; - char buf[buf_size+1]; + int count; + sds params = sdsempty(); + sds out_result = sdsempty(), out_lua = sdsempty(); + sds *tokens; - struct timespec start; - struct stat file_stat; - uint64_t proc_sec; + rpc_call_request *req = NULL; + rpc_call_response *resp = NULL; + netbyte_store *nb_req = NULL, *nb_resp=NULL; + char *nb_buf = NULL; - printf("BOTU\n"); + PRINT("LUA\n"); - stat("/proc/self",&file_stat); + req_data = (char *)data; + if (!req_data) + { + PERM(); + return NULL; + } - /* - CHECK PREDIFINED MACROSES IF SUCH FUNCTIONALITY EXCISTS OR NOT - */ - #if _POSIX_C_SOURCE < 199309L - ERROR("Dont have functionality\n"); - #endif + //---------------------------------------------------------------------------- + //prepare request + nb_req = malloc(sizeof(netbyte_store)); + nb_init(nb_req); + nb_load(nb_req, req_data); - fret = clock_gettime(CLOCK_REALTIME, &start); - if (fret<0) + if (nb_req == NULL) { - perror("clock gettime"); - ret = alloc_new_str("Can get clock thread uptime\n"); - return ret; + ERROR("implement response\n"); + return NULL; } - proc_sec = start.tv_sec - file_stat.st_ctim.tv_sec; + fret = rpc_call_req_unmarsh(nb_req, &req); + if (fret != 0) + { + ERROR("Invalid request format\n"); + return NULL; + } + + //---------------------------------------------------------------------------- + //main code + + + params = sdsnew(req->params); + tokens = sdssplitargs(params, &count); + + + + if (-1 == lua_excute(tokens[1], req, &resp)) + { + resp = rpc_call_resp_new(out_result,"None",1); + resp->user = alloc_new_str(" "); + resp->server = alloc_new_str(" "); + resp->mask = alloc_new_str(" "); + } + + sdsfree(params); + sdsfreesplitres(tokens, count); + + //---------------------------------------------------------------------------- + //prepare response + nb_resp = malloc(sizeof(netbyte_store)); + nb_init(nb_resp); + + PNL(); + rpc_call_resp_marsh(resp, &nb_resp); + + PNL(); + nb_buf = (char *)nb_create(nb_resp); + if (nb_buf) + { + ret = nb_buf; + } - snprintf(buf, buf_size, "%lud %luh %lum %lus", - (proc_sec/(3600*24)), - (proc_sec/(3600))%24, - (proc_sec/60)%60, - proc_sec%60); - ret = alloc_new_str(buf); + nb_free(nb_resp); + rpc_call_resp_free(resp); return ret; } diff --git a/cmd/cmd_lua.h b/cmd/cmd_lua.h index e2a0c4d..7050946 100644 --- a/cmd/cmd_lua.h +++ b/cmd/cmd_lua.h @@ -9,8 +9,14 @@ #include <sys/types.h> #include <sys/stat.h> +#include <lua.h> +#include <lualib.h> +#include <lauxlib.h> + #include "util.h" #include "debug.h" +#include "nbrpc_call.h" +#include "sds.h" void *cmd_lua(void *data); diff --git a/config_all_cmds.h b/config_all_cmds.h index 22abd4a..b61a5cd 100644 --- a/config_all_cmds.h +++ b/config_all_cmds.h @@ -1,6 +1,5 @@ #ifndef __CONFIG_CMD_HEADERS_H #define __CONFIG_CMD_HEADERS_H -#include "cmd/cmd_todo.h" #include "cmd/cmd_rand.h" #include "cmd/cmd_rusage.h" #include "cmd/cmd_version.h" @@ -16,6 +15,7 @@ #include "cmd/cmd_lua.h" #include "cmd/cmd_fir.h" #include "cmd/cmd_loadavg.h" +#include "cmd/cmd_todo.h" #include "cmd/cmd_ping.h" #include "cmd/cmd_sum.h" #include "cmd/cmd_help.h" diff --git a/config_cmds.h b/config_cmds.h index 192dec5..aba780e 100644 --- a/config_cmds.h +++ b/config_cmds.h @@ -4,6 +4,7 @@ #define CMD_T_SIMPLE 0 #define CMD_T_RPC 1 #define CMD_T_LUA 2 +#define CMD_T_SO 3 typedef struct single_cmd_def { @@ -43,7 +44,7 @@ single_cmd_def confgi_cmd_list[] = {CMD_T_RPC,"TODO",cmd_todo}, //command that going go to lua - {CMD_T_LUA,"LUA",cmd_lua}, + {CMD_T_LUA,"LUA",cmd_lua}, {CMD_T_LUA,"@", cmd_lua} {0,NULL,NULL} @@ -188,6 +188,49 @@ int tbl_exec_run(tbl_exec *tbl, char *cmd, char *user, char *mask, char *server, } else if (el->type == TBL_T_LUA) { ERROR("Not implemented\n"); + rpc_call_request *req = NULL; + rpc_call_response *resp = NULL; + netbyte_store *nb_req=NULL, *nb_resp=NULL; + char *buf_nb = NULL, *resp_buf=NULL; + + nb_resp = malloc(sizeof(netbyte_store)); + nb_req = malloc(sizeof(netbyte_store)); + nb_init(nb_resp); + nb_init(nb_req); + + //create request + req = rpc_call_req_new(el->name, cmd, 1); + if (!req) PERM(); + if (user != NULL) + { + PRINT("%s user\n", user); + req->user = alloc_new_str(user); + } + if (mask != NULL) + { + PRINT("%s mask\n", mask); + req->mask = alloc_new_str(mask); + } + if (server != NULL) + { + PRINT("%s server\n", server); + req->server = alloc_new_str(server); + } + rpc_call_req_marsh(req, &nb_req); + buf_nb = nb_create(nb_req); + resp_buf = el->callback(buf_nb); + if (resp_buf!=NULL) + { + nb_load(nb_resp, resp_buf); + rpc_call_resp_unmarsh(nb_resp, &resp); + ret_resp = alloc_new_str(resp->result); + rpc_call_resp_free(resp); + } else + { + ERROR("No response from RPC command\n"); + } + FREE(buf_nb); + rpc_call_req_free(req); } //return if (ret_resp != NULL) @@ -1,5 +1,5 @@ #ifndef __VERSION_H #define __VERSION_H -#define VERSION_DATE "Wed Sep 6 00:16:12 BST 2017" -#define VERSION_COMMIT "2f5d66c Removed mq_cmd shitz" +#define VERSION_DATE "Sun Nov 19 11:28:46 GMT 2017" +#define VERSION_COMMIT "d23ebff New version" #endif |