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  */
33 static 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 
58 static 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_::name
char name[16]
Definition: threadvars.h:65
util-lua-common.h
util-lua.h
LuaCallbackError
int LuaCallbackError(lua_State *luastate, const char *msg)
Definition: util-lua-common.c:39
threads.h
SCLuaLoadUtilLib
int SCLuaLoadUtilLib(lua_State *L)
Definition: util-lua-util.c:63
lua_State
struct lua_State lua_State
Definition: suricata-common.h:523
LuaStateGetThreadVars
ThreadVars * LuaStateGetThreadVars(lua_State *luastate)
get tv pointer from the lua state
Definition: util-lua.c:102
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
ThreadVars_::thread_group_name
char * thread_group_name
Definition: threadvars.h:67
SCGetThreadIdLong
#define SCGetThreadIdLong(...)
Definition: threads.h:255
suricata-common.h
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
threadvars.h
util-lua-util.h