#ifndef IPC_H
#define IPC_H

#include <stdint.h>
#include <stddef.h>
#include <sys/types.h>
#include <sys/utsname.h>

// IPC commands
// TODO: Add other harmful functions
typedef enum : uint64_t {
    CMD_SYSTEM,
    CMD_EXECVE,
    CMD_OPEN,
    CMD_POPEN,
    CMD_CHMOD,
    CMD_UNAME,
    CMD_GETRANDOM,
    CMD_UNLINK,
} ipc_command_t;

#define SHM_MSG_SIZE 1024
#define SHM_MSG_BUF_SIZE (SHM_MSG_SIZE - sizeof(ipc_command_t))

typedef struct {
    ipc_command_t command;
    union {
        char raw[SHM_MSG_BUF_SIZE];
        struct {
            char path[SHM_MSG_BUF_SIZE];
        } system; // CMD_SYSTEM
        struct {
            char path[SHM_MSG_BUF_SIZE];
        } execve; // CMD_EXECVE
        struct {
            char path[SHM_MSG_BUF_SIZE - sizeof(int)];
            int flags;
        } open; // CMD_OPEN
        struct {
            char path[SHM_MSG_BUF_SIZE];
        } popen; // CMD_POPEN
        struct {
            char path[SHM_MSG_BUF_SIZE - sizeof(mode_t)];
            mode_t mode;
        } chmod; // CMD_CHMOD
        struct {
            struct utsname buf;
        } uname; // CMD_UNAME
        struct {
            size_t buflen;
            char buf[SHM_MSG_BUF_SIZE - sizeof(size_t)];
        } getrandom; // CMD_GETRANDOM
        struct {
            char path[SHM_MSG_BUF_SIZE];
        } unlink; // CMD_UNLINK
    } data;
} ipc_message_t;

typedef struct {
    int is_ready;           // 0 = Mailbox empty, 1 = Message pending
    ipc_message_t message;  // Message payload
} ipc_mailbox_t;

// Supervisor functions
void setup_ipc(void);
void handle_ipc_command(ipc_message_t *msg);

// Sandboxed process functions
void sandbox_send(ipc_message_t *msg, size_t msg_len);

// Common globals

extern ipc_mailbox_t *g_shm_mailbox;
extern int g_ipc_notify_pipe;

#endif // IPC_H
