Skip to content

Commit

Permalink
feat process: add process support for unix
Browse files Browse the repository at this point in the history
  • Loading branch information
ffashion committed Mar 18, 2024
1 parent 71b6d2e commit 0abcfd0
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
44 changes: 44 additions & 0 deletions src/os/unix/xdbd_process.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <xdbd_process.h>
#include <stdlib.h>

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);
}
32 changes: 32 additions & 0 deletions src/os/unix/xdbd_process.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef __NGX_PROCESS__H__
#define __NGX_PROCESS__H__
#include <xdbd.h>

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__*/

0 comments on commit 0abcfd0

Please sign in to comment.