create IPC folder && create IPC_msg class

This commit is contained in:
2025-04-18 15:13:25 +05:00
parent 907322a122
commit f81b5c1b23
5 changed files with 272 additions and 51 deletions

187
src/IPC/IPC_msg.c Normal file
View File

@@ -0,0 +1,187 @@
#include "IPC_msg.h"
#include <string.h>
#include "../debug.h"
#include <sys/ksys.h>
#include <ctype.h>
static int syscalls_indexIPC_msg(lua_State* L)
{
ksys_ipc_msg* r = luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name);
const char* index = luaL_checkstring(L, 2);
if (strcmp(index, "size") == 0)
{
lua_pushinteger(L, r->datalen);
}
else if (strcmp(index, "pid") == 0)
{
lua_pushboolean(L, r->pid);
}
else
{
lua_pushnil(L);
}
return 1;
}
static int syscalls_newindexIPC_msg(lua_State* L)
{
ksys_ipc_msg* r = luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name);
lua_Integer index_i = lua_isnumber(L, 2);
uint32_t val = luaL_checkinteger(L, 3);
if (!index_i)
{
const char* index = luaL_checkstring(L, 2);
if (strcmp(index, "size") == 0)
{
r->datalen = val;
}
else if (strcmp(index, "pid") == 0)
{
r->pid = val;
}
else
{
luaL_error(L, "wrong index: %s", index);
}
}
else
{
if (index_i < r->data)
{
((lua_Integer*)r + (2 * sizeof(unsigned)))[index_i] = val;
}
else
{
luaL_error(L, "out of range. size: %d index: %d", r->datalen, index_i);
}
}
lua_pushboolean(L, true);
return 1;
}
static bool compare_ipc_msg(ksys_ipc_msg* a, ksys_ipc_msg* b)
{
if (a->datalen == b->datalen)
{
return memcmp(
a + (2 * sizeof(unsigned)),
b + (2 * sizeof(unsigned)),
a->datalen
);
}
else
{
return false;
}
}
static int syscalls_eqIPC_msg(lua_State* L)
{
lua_pushboolean(
L,
compare_ipc_msg(
luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name),
luaL_checkudata(L, 2, syscalls_IPC_msg_metatable_name)
)
);
return 1;
}
static int syscalls_IPC_msg_ReadByte(lua_State* L)
{
lua_pushinteger(
L,
((uint8_t*)luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name) + (2 * sizeof(unsigned)))[luaL_checkinteger(L, 2)]
);
return 1;
}
static int syscalls_IPC_msg_ReadWord(lua_State* L)
{
lua_pushinteger(
L,
((uint16_t*)luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name) + (2 * sizeof(unsigned)))[luaL_checkinteger(L, 2)]
);
return 1;
}
static int syscalls_IPC_msg_ReadDword(lua_State* L)
{
lua_pushinteger(
L,
((uint32_t*)luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name) + (2 * sizeof(unsigned)))[luaL_checkinteger(L, 2)]
);
return 1;
}
static int syscalls_IPC_msg_ReadInteger(lua_State* L)
{
lua_pushinteger(
L,
((lua_Integer*)luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name) + (2 * sizeof(unsigned)))[luaL_checkinteger(L, 2)]
);
return 1;
}
static const luaL_Reg syscalls_IPC_msg_m[] = {
{"__index", syscalls_indexIPC_msg},
{"__newindex", syscalls_newindexIPC_msg},
{"__eq", syscalls_eqIPC_msg},
{"ReadByte", syscalls_IPC_msg_ReadByte},
{"ReadWord", syscalls_IPC_msg_ReadWord},
{"ReadDword", syscalls_IPC_msg_ReadDword},
{"ReadInteger", syscalls_IPC_msg_ReadInteger},
{NULL, NULL}
};
struct registers* syscalls_pushIPC_msg(lua_State* L, size_t dataLen)
{
DEBUG_LINE("push IPC_msg entry");
ksys_ipc_msg* entry = lua_newuserdata(L, sizeof(ksys_ipc_msg) + dataLen);
luaL_setmetatable(L, syscalls_IPC_msg_metatable_name);
return entry;
}
static int syscalls_newIPC_msg(lua_State* L)
{
size_t dataLen = luaL_checkinteger(L, 1);
ksys_ipc_msg* r = syscalls_pushIPC_msg(L, dataLen);
r->datalen = dataLen;
return 1;
}
static const luaL_Reg syscalls_IPC_msg_lib[] = {
{"new", syscalls_newIPC_msg},
{NULL, NULL}
};
void syscalls_register_IPC_msg(lua_State* L)
{
DEBUG_LINE("register IPC_msg entry");
luaL_newlib(L, syscalls_IPC_msg_lib);
lua_setfield(L, -2, syscalls_IPC_msg_name);
luaL_newmetatable(L, syscalls_IPC_msg_metatable_name);
luaL_setfuncs(L, syscalls_IPC_msg_m, 0);
lua_pop(L, 1);
}

19
src/IPC/IPC_msg.h Normal file
View File

@@ -0,0 +1,19 @@
#ifndef _SYSCALLS_IPC_MSG_H_
#define _SYSCALLS_IPC_MSG_H_
#include "../syscalls.h"
#include <sys/ksys.h>
#define syscalls_IPC_msg_metatable_name "syscalls IPC_msg metatable"
#define syscalls_IPC_msg_name "IPC_msg"
int syscalls_newSystemColors(lua_State* L);
ksys_colors_table_t* syscalls_pushSystemColors(lua_State* L);
/**
* Register SystemColors lib
*/
void syscalls_register_SystemColors(lua_State* L);
#endif // _SYSCALLS_IPC_MSG_H_

48
src/IPC/ipc.c Normal file
View File

@@ -0,0 +1,48 @@
#include "ipc.h"
#include "IPC_msg.h"
#include <sys/ksys.h>
inline static void define_ipc(ksys_ipc_buffer* buffer, size_t bufLen)
{
asm_inline(
"int $0x40"
::"a"(60), "b"(1), "c"(buffer), "d"(bufLen)
);
}
inline static enum SendIPCErrors send_ipc(int pid, ksys_ipc_msg* msg)
{
enum SendIPCErrors ret;
asm_inline(
"int $0x40"
: "=a"(ret)
: "a"(60), "b"(1), "c"(pid), "d"(msg), "S"(sizeof(ksys_ipc_msg) + msg->datalen)
);
return ret;
}
int syscalls_DefineIPCBuffer(lua_State* L)
{
uint32_t len = luaL_checkinteger(L, 1);
ksys_ipc_buffer* buffer;
define_ipc(buffer, len);
return 1;
}
int syscalls_SendIPCMessage(lua_State* L)
{
lua_pushinteger(
L,
send_ipc(
lua_checkinteger(L, 1),
luaL_checkudata(L, 1, syscalls_IPC_msg_metatable_name)
)
);
return 1;
}

18
src/IPC/ipc.h Normal file
View File

@@ -0,0 +1,18 @@
#ifndef __IPC_H__
#define __IPC_H__
#include "../syscalls.h"
enum SendIPCErrors
{
Ok = 0,
BufferNotDefined = 1,
BufferLocked = 2,
BufferOverflow = 3,
PidNotExist = 4
};
int syscalls_DefineIPCBuffer(lua_State* L);
int syscalls_SendIPCMessage(lua_State* L);
#endif // __IPC_H__

View File

@@ -1,51 +0,0 @@
# include <sys/ksys.h>
enum SendIPCErrors
{
Ok = 0,
BufferNotDefined = 1,
BufferLocked = 2,
BufferOverflow = 3,
PidNotExist = 4
}
inline static void define_ipc(ksys_ipc_buffer *buffer, size_t bufLen)
{
asm_inline(
"int $0x40"
::"a"(60), "b"(1), "c"(buffer), "d"(bufLen)
);
}
inline static enum SendIPCErrors send_ipc(int pid, ksys_ipc_msg *msg, size_t msgLen)
{
enum SendIPCErrors ret;
asm_inline(
"int $0x40"
: "=a"(ret),
: "a"(60), "b"(1), "c"(pid), "d"(msg), "S"(msgLen)
)
return ret;
}
int syscalls_DefineIPCBuffer(lua_State *L)
{
define_ipc();
return 0;
}
int syscalls_SendIPCMessage(lua_State *L)
{
lua_pushinteger(
L,
send_ipc(
lua_checkinteger(L, 1);,
)
);
return 1;
}