From 532be12679a85f8632877346a7d8da8f1a4546f6 Mon Sep 17 00:00:00 2001 From: rdbo Date: Tue, 5 Dec 2023 06:09:41 -0300 Subject: [PATCH] added proper index function for lm_process_t in lua --- bindings/lua/src/types.c | 29 +++++++++++++++++++++++++---- bindings/lua/tests/test.lua | 11 ++++++++++- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/bindings/lua/src/types.c b/bindings/lua/src/types.c index 0bdbc15b..89065065 100644 --- a/bindings/lua/src/types.c +++ b/bindings/lua/src/types.c @@ -26,15 +26,36 @@ #include #include "types.h" +#define STREQUAL(a, b) (!strcmp(a, b)) +#define LM_PROCESS_META "lm_process_t" + int lua_lm_process_index(lua_State *L) { - lua_pushinteger(L, 1337); + lm_process_t *udata = (lm_process_t *)luaL_checkudata(L, 1, LM_PROCESS_META); + const char *entry = luaL_checkstring(L, 2); + + if (STREQUAL(entry, "pid")) { + lua_pushinteger(L, (lua_Integer)udata->pid); + } else if (STREQUAL(entry, "ppid")) { + lua_pushinteger(L, (lua_Integer)udata->ppid); + } else if (STREQUAL(entry, "bits")) { + lua_pushinteger(L, (lua_Integer)udata->bits); + } else if (STREQUAL(entry, "start_time")) { + lua_pushinteger(L, (lua_Integer)udata->start_time); + } else if (STREQUAL(entry, "path")) { + lua_pushstring(L, udata->path); + } else if (STREQUAL(entry, "name")) { + lua_pushstring(L, udata->name); + } else { + lua_pushnil(L); + } + return 1; } int lua_lm_process_tostring(lua_State *L) { - lm_process_t *udata = (lm_process_t *)luaL_checkudata(L, 1, "lm_process_t"); + lm_process_t *udata = (lm_process_t *)luaL_checkudata(L, 1, LM_PROCESS_META); lua_pushfstring(L, "lm_process_t(pid: %d, ppid: %d, bits: %d, name: \"%s\", path: \"%s\", start_time: %p)", udata->pid, udata->ppid, udata->bits, udata->name, udata->path, udata->start_time); @@ -44,13 +65,13 @@ int lua_lm_process_tostring(lua_State *L) void lua_create_lm_process(lua_State *L) { lm_process_t *udata = lua_newuserdata(L, sizeof(lm_process_t)); - luaL_getmetatable(L, "lm_process_t"); + luaL_getmetatable(L, LM_PROCESS_META); lua_setmetatable(L, lua_gettop(L) - 1); } void lua_define_lm_process(lua_State *L) { - luaL_newmetatable(L, "lm_process_t"); + luaL_newmetatable(L, LM_PROCESS_META); lua_pushcfunction(L, lua_lm_process_index); lua_setfield(L, lua_gettop(L) - 1, "__index"); lua_pushcfunction(L, lua_lm_process_tostring); diff --git a/bindings/lua/tests/test.lua b/bindings/lua/tests/test.lua index d017163f..53752556 100644 --- a/bindings/lua/tests/test.lua +++ b/bindings/lua/tests/test.lua @@ -3,4 +3,13 @@ print("Lua Tests") local libmem = require("libmem_lua") print("libmem_lua loaded") -print(libmem.LM_FindProcess("target")) +target_proc = libmem.LM_FindProcess("target") +print("[*] Target Process") +print("PID: " .. target_proc.pid) +print("PPID: " .. target_proc.ppid) +print("Bits: " .. target_proc.bits) +print("Start Time: " .. target_proc.start_time) +print("Path: " .. target_proc.path) +print("Name: " .. target_proc.name) +print("(invalid): " .. (target_proc.invalid or "nil")) +print(target_proc)