Skip to content

Commit 532be12

Browse files
committed
added proper index function for lm_process_t in lua
1 parent e48e701 commit 532be12

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

bindings/lua/src/types.c

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,36 @@
2626
#include <lualib.h>
2727
#include "types.h"
2828

29+
#define STREQUAL(a, b) (!strcmp(a, b))
30+
#define LM_PROCESS_META "lm_process_t"
31+
2932
int lua_lm_process_index(lua_State *L)
3033
{
31-
lua_pushinteger(L, 1337);
34+
lm_process_t *udata = (lm_process_t *)luaL_checkudata(L, 1, LM_PROCESS_META);
35+
const char *entry = luaL_checkstring(L, 2);
36+
37+
if (STREQUAL(entry, "pid")) {
38+
lua_pushinteger(L, (lua_Integer)udata->pid);
39+
} else if (STREQUAL(entry, "ppid")) {
40+
lua_pushinteger(L, (lua_Integer)udata->ppid);
41+
} else if (STREQUAL(entry, "bits")) {
42+
lua_pushinteger(L, (lua_Integer)udata->bits);
43+
} else if (STREQUAL(entry, "start_time")) {
44+
lua_pushinteger(L, (lua_Integer)udata->start_time);
45+
} else if (STREQUAL(entry, "path")) {
46+
lua_pushstring(L, udata->path);
47+
} else if (STREQUAL(entry, "name")) {
48+
lua_pushstring(L, udata->name);
49+
} else {
50+
lua_pushnil(L);
51+
}
52+
3253
return 1;
3354
}
3455

3556
int lua_lm_process_tostring(lua_State *L)
3657
{
37-
lm_process_t *udata = (lm_process_t *)luaL_checkudata(L, 1, "lm_process_t");
58+
lm_process_t *udata = (lm_process_t *)luaL_checkudata(L, 1, LM_PROCESS_META);
3859

3960
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);
4061

@@ -44,13 +65,13 @@ int lua_lm_process_tostring(lua_State *L)
4465
void lua_create_lm_process(lua_State *L)
4566
{
4667
lm_process_t *udata = lua_newuserdata(L, sizeof(lm_process_t));
47-
luaL_getmetatable(L, "lm_process_t");
68+
luaL_getmetatable(L, LM_PROCESS_META);
4869
lua_setmetatable(L, lua_gettop(L) - 1);
4970
}
5071

5172
void lua_define_lm_process(lua_State *L)
5273
{
53-
luaL_newmetatable(L, "lm_process_t");
74+
luaL_newmetatable(L, LM_PROCESS_META);
5475
lua_pushcfunction(L, lua_lm_process_index);
5576
lua_setfield(L, lua_gettop(L) - 1, "__index");
5677
lua_pushcfunction(L, lua_lm_process_tostring);

bindings/lua/tests/test.lua

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,13 @@ print("Lua Tests")
33
local libmem = require("libmem_lua")
44
print("libmem_lua loaded")
55

6-
print(libmem.LM_FindProcess("target"))
6+
target_proc = libmem.LM_FindProcess("target")
7+
print("[*] Target Process")
8+
print("PID: " .. target_proc.pid)
9+
print("PPID: " .. target_proc.ppid)
10+
print("Bits: " .. target_proc.bits)
11+
print("Start Time: " .. target_proc.start_time)
12+
print("Path: " .. target_proc.path)
13+
print("Name: " .. target_proc.name)
14+
print("(invalid): " .. (target_proc.invalid or "nil"))
15+
print(target_proc)

0 commit comments

Comments
 (0)