From 0abcfd00d5d5e4f67b03d86d6c7f20ea81284ef8 Mon Sep 17 00:00:00 2001 From: ffashion Date: Mon, 18 Mar 2024 21:24:33 +0800 Subject: [PATCH] feat process: add process support for unix --- CMakeLists.txt | 2 ++ src/os/unix/xdbd_process.c | 44 ++++++++++++++++++++++++++++++++++++++ src/os/unix/xdbd_process.h | 32 +++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 src/os/unix/xdbd_process.c create mode 100644 src/os/unix/xdbd_process.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 180438c..c0dd1a5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,6 +33,8 @@ set( ${PROJECT_SOURCE_DIR}/src/event/ ${PROJECT_SOURCE_DIR}/src/connection/ ${PROJECT_SOURCE_DIR}/src/core/ + #FIXME: check platform + ${PROJECT_SOURCE_DIR}/src/os/unix/ ) include_directories(${XADB_INCLUDES}) diff --git a/src/os/unix/xdbd_process.c b/src/os/unix/xdbd_process.c new file mode 100644 index 0000000..a689e1e --- /dev/null +++ b/src/os/unix/xdbd_process.c @@ -0,0 +1,44 @@ +#include +#include + +static void xdbd_execute_proc(xdbd_t *cycle, void *data); + +xdbd_pid_t xdbd_spawn_process(xdbd_t *xdbd, + xdbd_spawn_proc_pt proc, void *data, char *name) { + xdbd_pid_t pid; + + pid = fork(); + + switch (pid) { + + case -1: + bfdev_log_err("fork() failed while spawning \"%s\"", name); + // xdbd_close_channel(xdbd_processes[s].channel, cycle->log); + return XDBD_INVALID_PID; + + case 0: + proc(xdbd, data); + break; + + default: + break; + } + + return pid; +} + +xdbd_pid_t xdbd_execute(xdbd_t *cycle, xdbd_exec_ctx_t *ctx) { + return xdbd_spawn_process(cycle, xdbd_execute_proc, ctx, ctx->name); +} + + +static void xdbd_execute_proc(xdbd_t *cycle, void *data) { + xdbd_exec_ctx_t *ctx = data; + + if (execve(ctx->path, ctx->argv, ctx->envp) == -1) { + bfdev_log_err("execve() failed while executing %s \"%s\"", + ctx->name, ctx->path); + } + + exit(1); +} diff --git a/src/os/unix/xdbd_process.h b/src/os/unix/xdbd_process.h new file mode 100644 index 0000000..1ea3e18 --- /dev/null +++ b/src/os/unix/xdbd_process.h @@ -0,0 +1,32 @@ +#ifndef __NGX_PROCESS__H__ +#define __NGX_PROCESS__H__ +#include + +typedef pid_t xdbd_pid_t; + +#define XDBD_INVALID_PID -1 + +typedef void (*xdbd_spawn_proc_pt) (xdbd_t *xdbd, void *data); + +typedef struct { + xdbd_pid_t pid; + xdbd_spawn_proc_pt proc; + +} xdbd_process_t; + +typedef struct { + char *path; + char *name; + char *const *argv; + char *const *envp; +} xdbd_exec_ctx_t; + +xdbd_pid_t xdbd_spawn_process(xdbd_t *xdbd, + xdbd_spawn_proc_pt proc, void *data, char *name); + +xdbd_pid_t xdbd_execute(xdbd_t *cycle, xdbd_exec_ctx_t *ctx); + +#define ngx_getpid getpid +#define ngx_getppid getppid + +#endif /*__NGX_PROCESS__H__*/