From: Victor Julien Date: Fri, 21 Feb 2014 08:37:39 +0000 (+0100) Subject: output-lua: add SCLogPath callback X-Git-Tag: suricata-2.1beta2~149 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1e836be3d8e21e62486f09a91aebafdf1bfdc7c3;p=thirdparty%2Fsuricata.git output-lua: add SCLogPath callback Add a lua callback for getting Suricata's log path, so that lua scripts can easily get the logging directory Suricata uses. Update the Setup logic to register callbacks before the scripts 'setup' is called. Example: name = "fast_lua.log" function setup (args) filename = SCLogPath() .. "/" .. name file = assert(io.open(filename, "a")) end --- diff --git a/src/output-lua-common.c b/src/output-lua-common.c index 3892d64a61..d4cbb2ee63 100644 --- a/src/output-lua-common.c +++ b/src/output-lua-common.c @@ -232,11 +232,22 @@ static int LuaCallbackTuple(lua_State *luastate) return LuaCallbackTuplePushToStackFromPacket(luastate, p); } +static int LuaCallbackLogPath(lua_State *luastate) +{ + const char *ld = ConfigGetLogDirectory(); + if (ld == NULL) + return LuaCallbackError(luastate, "internal error: no log dir"); + + return LuaReturnStringBuffer(luastate, (const uint8_t *)ld, strlen(ld)); +} + int LogLuaRegisterFunctions(lua_State *luastate) { /* registration of the callbacks */ lua_pushcfunction(luastate, LuaCallbackTuple); lua_setglobal(luastate, "SCPacketTuple"); + lua_pushcfunction(luastate, LuaCallbackLogPath); + lua_setglobal(luastate, "SCLogPath"); return 0; } diff --git a/src/output-lua.c b/src/output-lua.c index 06029d3a1d..6c937c1e28 100644 --- a/src/output-lua.c +++ b/src/output-lua.c @@ -433,17 +433,17 @@ static lua_State *LuaScriptSetup(const char *filename) lua_getglobal(luastate, "setup"); - if (lua_pcall(luastate, 0, 0, 0) != 0) { - SCLogError(SC_ERR_LUAJIT_ERROR, "couldn't run script 'setup' function: %s", lua_tostring(luastate, -1)); - goto error; - } - /* register functions common to all */ LogLuaRegisterFunctions(luastate); /* unconditionally register http function. They will only work * if the tx is registered in the state at runtime though. */ LogLuaRegisterHttpFunctions(luastate); + if (lua_pcall(luastate, 0, 0, 0) != 0) { + SCLogError(SC_ERR_LUAJIT_ERROR, "couldn't run script 'setup' function: %s", lua_tostring(luastate, -1)); + goto error; + } + SCLogDebug("lua_State %p is set up", luastate); return luastate; error: