lua/util: move SCThreadInfo into suricata.util lib

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.
pull/13361/head
Jason Ish 1 year ago committed by Victor Julien
parent 778a699622
commit e5faedf7e6

@ -25,3 +25,4 @@ environment without access to additional modules.
ssh
tls
ja3
util

@ -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)

@ -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)

@ -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 \

@ -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;
}

@ -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);

@ -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 */

@ -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));

@ -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;

@ -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 },
};

@ -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;

@ -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*

@ -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;
}

@ -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 */
Loading…
Cancel
Save