suricata
util-lua-util.c
Go to the documentation of this file.
1/* Copyright (C) 2025 Open Information Security Foundation
2 *
3 * You can copy, redistribute or modify this Program under the terms of
4 * the GNU General Public License version 2 as published by the Free
5 * Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * version 2 along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 */
17
18#include "suricata-common.h"
19#include "threads.h"
20#include "threadvars.h"
21
22#include "util-lua.h"
23#include "util-lua-common.h"
24#include "util-lua-util.h"
25
26#include "lua.h"
27#include "lauxlib.h"
28
29/**
30 * \brief Get thread information and return as a table
31 * \retval 1 table with thread info fields: id, name, thread_group_name
32 */
33static int LuaUtilThreadInfo(lua_State *luastate)
34{
35 const ThreadVars *tv = LuaStateGetThreadVars(luastate);
36 if (tv == NULL)
37 return LuaCallbackError(luastate, "internal error: no tv");
38
39 unsigned long tid = SCGetThreadIdLong();
40
41 lua_newtable(luastate);
42
43 lua_pushstring(luastate, "id");
44 lua_pushinteger(luastate, (lua_Integer)tid);
45 lua_settable(luastate, -3);
46
47 lua_pushstring(luastate, "name");
48 lua_pushstring(luastate, tv->name);
49 lua_settable(luastate, -3);
50
51 lua_pushstring(luastate, "group_name");
52 lua_pushstring(luastate, tv->thread_group_name);
53 lua_settable(luastate, -3);
54
55 return 1;
56}
57
58static const struct luaL_Reg utillib[] = {
59 { "thread_info", LuaUtilThreadInfo },
60 { NULL, NULL },
61};
62
64{
65 luaL_newlib(L, utillib);
66 return 1;
67}
ThreadVars * tv
Per thread variable structure.
Definition threadvars.h:58
char * thread_group_name
Definition threadvars.h:67
char name[16]
Definition threadvars.h:65
struct lua_State lua_State
#define SCGetThreadIdLong(...)
Definition threads.h:255
int LuaCallbackError(lua_State *luastate, const char *msg)
int SCLuaLoadUtilLib(lua_State *L)
ThreadVars * LuaStateGetThreadVars(lua_State *luastate)
get tv pointer from the lua state
Definition util-lua.c:102