]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
lua/util: move SCThreadInfo into suricata.util lib
authorJason Ish <jason.ish@oisf.net>
Mon, 2 Jun 2025 23:23:08 +0000 (17:23 -0600)
committerVictor Julien <victor@inliniac.net>
Wed, 4 Jun 2025 07:39:51 +0000 (09:39 +0200)
Move the SCThreadInfo global function into the suricata.util library as
thread_info().

This is the last global function to be registered, so remove the
supporting functions.

14 files changed:
doc/userguide/lua/libs/index.rst
doc/userguide/lua/libs/util.rst [new file with mode: 0644]
doc/userguide/lua/lua-functions.rst
src/Makefile.am
src/detect-lua-extensions.c
src/detect-lua-extensions.h
src/detect-lua.c
src/detect-transform-luaxform.c
src/output-lua.c
src/util-lua-builtins.c
src/util-lua-common.c
src/util-lua-common.h
src/util-lua-util.c [new file with mode: 0644]
src/util-lua-util.h [new file with mode: 0644]

index 188ca1784f330e39973c1c6596a7a35a57af069f..87e73fe09f336b9fab9a7a0f7273e4f9539e4d38 100644 (file)
@@ -25,3 +25,4 @@ environment without access to additional modules.
    ssh
    tls
    ja3
+   util
diff --git a/doc/userguide/lua/libs/util.rst b/doc/userguide/lua/libs/util.rst
new file mode 100644 (file)
index 0000000..ba3bb1c
--- /dev/null
@@ -0,0 +1,34 @@
+Util
+####
+
+The ``suricata.util`` library provides utility functions for Lua
+scripts.
+
+Setup
+*****
+
+The library must be loaded prior to use::
+
+    local util = require("suricata.util")
+
+Functions
+=========
+
+.. function:: thread_info()
+
+   Get information about the current thread.
+
+   :returns: Table containing thread information with the following fields:
+
+      - ``id`` (number): Thread ID
+      - ``name`` (string): Thread name 
+      - ``group_name`` (string): Thread group name
+            
+   Example::
+
+       local util = require("suricata.util")
+       
+       local info = util.thread_info()
+       print("Thread ID: " .. info.id)
+       print("Thread Name: " .. info.name)
+       print("Thread Group: " .. info.group_name)
index 708ee96b9aafe090402294b612651f591f93f449..b640bf79955eef131a922ba78705f8defd0b6768 100644 (file)
@@ -299,15 +299,3 @@ SCFlowintDecr
 ~~~~~~~~~~~~~
 
 Decrement Flowint at index given by the first parameter.
-
-Misc
-----
-
-SCThreadInfo
-~~~~~~~~~~~~
-
-::
-
-  tid, tname, tgroup = SCThreadInfo()
-
-It gives: tid (integer), tname (string), tgroup (string)
index 9dde1185b69fd796a4969782300d1449a864b5ae..d54c57375f42d9972c4126aa25031debc8d48408 100755 (executable)
@@ -556,6 +556,7 @@ noinst_HEADERS = \
        util-lua-smtp.h \
        util-lua-ssh.h \
        util-lua-tls.h \
+       util-lua-util.h \
        util-lua.h \
        util-macset.h \
        util-magic.h \
@@ -1136,6 +1137,7 @@ libsuricata_c_a_SOURCES = \
        util-lua-smtp.c \
        util-lua-ssh.c \
        util-lua-tls.c \
+       util-lua-util.c \
        util-lua.c \
        util-macset.c \
        util-magic.c \
index e8c810d0b6e4ccab1beb0dddf739a6aee62a2d7c..339292d76ca19d8cec7a9fb870a156e8d69892fd 100644 (file)
@@ -66,12 +66,3 @@ void LuaExtensionsMatchSetup(lua_State *lua_state, DetectLuaData *ld,
 
     LuaStateSetDirection(lua_state, (flags & STREAM_TOSERVER));
 }
-
-/**
- *  \brief Register Suricata Lua functions
- */
-int LuaRegisterExtensions(lua_State *lua_state)
-{
-    LuaRegisterFunctions(lua_state);
-    return 0;
-}
index 3620fd084993b4fd9fd2c9bcc92448a15b35242b..a452483b843b6a84864084ea1ccd3805a62ca48f 100644 (file)
@@ -26,8 +26,6 @@
 
 extern const char luaext_key_ld[];
 
-int LuaRegisterExtensions(lua_State *);
-
 void LuaExtensionsMatchSetup(lua_State *lua_state, DetectLuaData *, DetectEngineThreadCtx *det_ctx,
         Flow *f, Packet *p, const Signature *s, uint8_t flags);
 
index 92e1e80eecb4f62840db76ab2d4e92cecfb164cb..7829b2e9c7947f85c085ef77375ad77c13e0aa41 100644 (file)
@@ -424,7 +424,6 @@ static void *DetectLuaThreadInit(void *data)
         SCLuaSbLoadLibs(t->luastate);
     }
 
-    LuaRegisterExtensions(t->luastate);
     LuaStateSetDetectLuaData(t->luastate, lua);
 
     /* hackish, needed to allow unittests to pass buffers as scripts instead of files */
index 87fb8403e314e274b055ecf3451c1495d1846883..157f5872543f7401f32f291fe82cafe0b48f7eef 100644 (file)
@@ -202,8 +202,6 @@ static void *DetectLuaxformThreadInit(void *data)
         SCLuaSbLoadLibs(t->luastate);
     }
 
-    LuaRegisterExtensions(t->luastate);
-
     int status = luaL_loadfile(t->luastate, lua->filename);
     if (status) {
         SCLogError("couldn't load file: %s", lua_tostring(t->luastate, -1));
index 15b1d440cafd6853ee6d5f9f05bb340c83e3c243..3e1edaef3291f97a905783f0eb7b8ce04ae81c7c 100644 (file)
@@ -608,9 +608,6 @@ static lua_State *LuaScriptSetup(const char *filename, LogLuaMasterCtx *ctx)
 
     lua_getglobal(luastate, "setup");
 
-    /* register functions common to all */
-    LuaRegisterFunctions(luastate);
-
     if (lua_pcall(luastate, 0, 0, 0) != 0) {
         SCLogError("couldn't run script 'setup' function: %s", lua_tostring(luastate, -1));
         goto error;
index 6682917204acb267e671bc16b6a4634488b7b796..7146ef648f49150aeff464a5249d0997d69f3706 100644 (file)
@@ -36,6 +36,7 @@
 #include "util-lua-ja3.h"
 #include "util-lua-filelib.h"
 #include "util-lua-log.h"
+#include "util-lua-util.h"
 
 #include "lauxlib.h"
 
@@ -59,6 +60,7 @@ static const luaL_Reg builtins[] = {
     { "suricata.smtp", SCLuaLoadSmtpLib },
     { "suricata.ssh", SCLuaLoadSshLib },
     { "suricata.tls", SCLuaLoadTlsLib },
+    { "suricata.util", SCLuaLoadUtilLib },
     { NULL, NULL },
 };
 
index 9498b9b9f2df1756bf648cab91b8f335952b4d9d..8540d2757584a87ed3c91dd6bceab22c1c1e5586 100644 (file)
@@ -99,43 +99,6 @@ void LuaPushTableKeyValueArray(
     lua_settable(luastate, -3);
 }
 
-/** \internal
- *  \brief fill lua stack with thread info
- *  \param luastate the lua state
- *  \param pa pointer to packet alert struct
- *  \retval cnt number of data items placed on the stack
- *
- *  Places: thread id (number), thread name (string, thread group name (string)
- */
-static int LuaCallbackThreadInfoPushToStackFromThreadVars(lua_State *luastate, const ThreadVars *tv)
-{
-    unsigned long tid = SCGetThreadIdLong();
-    lua_pushinteger (luastate, (lua_Integer)tid);
-    lua_pushstring (luastate, tv->name);
-    lua_pushstring (luastate, tv->thread_group_name);
-    return 3;
-}
-
-/** \internal
- *  \brief Wrapper for getting tuple info into a lua script
- *  \retval cnt number of items placed on the stack
- */
-static int LuaCallbackThreadInfo(lua_State *luastate)
-{
-    const ThreadVars *tv = LuaStateGetThreadVars(luastate);
-    if (tv == NULL)
-        return LuaCallbackError(luastate, "internal error: no tv");
-
-    return LuaCallbackThreadInfoPushToStackFromThreadVars(luastate, tv);
-}
-
-int LuaRegisterFunctions(lua_State *luastate)
-{
-    lua_pushcfunction(luastate, LuaCallbackThreadInfo);
-    lua_setglobal(luastate, "SCThreadInfo");
-    return 0;
-}
-
 int LuaStateNeedProto(lua_State *luastate, AppProto alproto)
 {
     AppProto flow_alproto = 0;
index ddeca01b48409d307fbe9c7b26166997d7ac3a69..e50c17afe94d5688c7c4448082bedcff7e064013 100644 (file)
@@ -37,8 +37,6 @@ void LuaPushTableKeyValueLString(
         lua_State *luastate, const char *key, const char *value, size_t len);
 void LuaPushTableKeyValueArray(lua_State *luastate, const char *key, const uint8_t *value, size_t len);
 
-int LuaRegisterFunctions(lua_State *luastate);
-
 int LuaStateNeedProto(lua_State *luastate, AppProto alproto);
 
 /* hack to please scan-build. Even though LuaCallbackError *always*
diff --git a/src/util-lua-util.c b/src/util-lua-util.c
new file mode 100644 (file)
index 0000000..aa6a279
--- /dev/null
@@ -0,0 +1,67 @@
+/* Copyright (C) 2025 Open Information Security Foundation
+ *
+ * You can copy, redistribute or modify this Program under the terms of
+ * the GNU General Public License version 2 as published by the Free
+ * Software Foundation.
+ *
+ * This program 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
+ * version 2 along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#include "suricata-common.h"
+#include "threads.h"
+#include "threadvars.h"
+
+#include "util-lua.h"
+#include "util-lua-common.h"
+#include "util-lua-util.h"
+
+#include "lua.h"
+#include "lauxlib.h"
+
+/**
+ * \brief Get thread information and return as a table
+ * \retval 1 table with thread info fields: id, name, thread_group_name
+ */
+static int LuaUtilThreadInfo(lua_State *luastate)
+{
+    const ThreadVars *tv = LuaStateGetThreadVars(luastate);
+    if (tv == NULL)
+        return LuaCallbackError(luastate, "internal error: no tv");
+
+    unsigned long tid = SCGetThreadIdLong();
+
+    lua_newtable(luastate);
+
+    lua_pushstring(luastate, "id");
+    lua_pushinteger(luastate, (lua_Integer)tid);
+    lua_settable(luastate, -3);
+
+    lua_pushstring(luastate, "name");
+    lua_pushstring(luastate, tv->name);
+    lua_settable(luastate, -3);
+
+    lua_pushstring(luastate, "group_name");
+    lua_pushstring(luastate, tv->thread_group_name);
+    lua_settable(luastate, -3);
+
+    return 1;
+}
+
+static const struct luaL_Reg utillib[] = {
+    { "thread_info", LuaUtilThreadInfo },
+    { NULL, NULL },
+};
+
+int SCLuaLoadUtilLib(lua_State *L)
+{
+    luaL_newlib(L, utillib);
+    return 1;
+}
diff --git a/src/util-lua-util.h b/src/util-lua-util.h
new file mode 100644 (file)
index 0000000..e0f45b8
--- /dev/null
@@ -0,0 +1,25 @@
+/* Copyright (C) 2025 Open Information Security Foundation
+ *
+ * You can copy, redistribute or modify this Program under the terms of
+ * the GNU General Public License version 2 as published by the Free
+ * Software Foundation.
+ *
+ * This program 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
+ * version 2 along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#ifndef SURICATA_UTIL_LUA_UTIL_H
+#define SURICATA_UTIL_LUA_UTIL_H
+
+#include "lua.h"
+
+int SCLuaLoadUtilLib(lua_State *L);
+
+#endif /* SURICATA_UTIL_LUA_UTIL_H */
\ No newline at end of file