]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
output-lua: add SCLogPath callback
authorVictor Julien <victor@inliniac.net>
Fri, 21 Feb 2014 08:37:39 +0000 (09:37 +0100)
committerVictor Julien <victor@inliniac.net>
Fri, 15 Aug 2014 11:58:25 +0000 (13:58 +0200)
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

src/output-lua-common.c
src/output-lua.c

index 3892d64a61628c6c9e915b47f976dec473f3d6f6..d4cbb2ee632cdc13f45ef60871e40538a514226f 100644 (file)
@@ -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;
 }
 
index 06029d3a1d776e3a2f9f77c57b8ee2cacbb2a7b7..6c937c1e28cd166c8dab950042bafe5d3189dc36 100644 (file)
@@ -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: