diff --git a/ce/interp.c b/ce/interp.c index bc8c0bb..a899a90 100644 --- a/ce/interp.c +++ b/ce/interp.c @@ -289,8 +289,8 @@ static void help (struct context* context) { cprintf (context, "quit\n"); } -static void cmd_cancel_proc (void) { - int pid = *(int*)argument_ptr (); +static void cmd_cancel_proc (void* arg) { + int pid = *(int*)arg; char ch = 0; for (;;) { @@ -301,8 +301,6 @@ static void cmd_cancel_proc (void) { } kill (pid); - - quit (); } static void execute_cmd (struct ast_cmd* cmd, struct context* context) { @@ -342,11 +340,12 @@ static void execute_cmd (struct ast_cmd* cmd, struct context* context) { return; } - int cancel_pid = process_spawn (&cmd_cancel_proc, &pid); + struct process_data* cancel_pdata = process_spawn (&cmd_cancel_proc, &pid); wait_for_pid (pid); - kill (cancel_pid); + if (kill (cancel_pdata->pid) < 0) + free (cancel_pdata); } } diff --git a/init/init.c b/init/init.c index deaf2bf..206734b 100644 --- a/init/init.c +++ b/init/init.c @@ -10,7 +10,9 @@ static int ce_pgid; -void receiver (void) { +void receiver (void* arg) { + (void)arg; + for (;;) { char recv[RECV_MAX]; memset (recv, 0, sizeof (recv)); diff --git a/libprocess/amd64/.gitignore b/libprocess/amd64/.gitignore new file mode 100644 index 0000000..5761abc --- /dev/null +++ b/libprocess/amd64/.gitignore @@ -0,0 +1 @@ +*.o diff --git a/libprocess/amd64/clone_tramp.S b/libprocess/amd64/clone_tramp.S new file mode 100644 index 0000000..bcc9828 --- /dev/null +++ b/libprocess/amd64/clone_tramp.S @@ -0,0 +1,7 @@ +.global _clone_tramp +_clone_tramp: + xorq %rbp, %rbp + movq %rsp, %rbp + andq $-16, %rsp + + callq _clone_tramp1 diff --git a/libprocess/amd64/clone_tramp1.c b/libprocess/amd64/clone_tramp1.c new file mode 100644 index 0000000..d7bef14 --- /dev/null +++ b/libprocess/amd64/clone_tramp1.c @@ -0,0 +1,12 @@ +#include +#include +#include + +void _clone_tramp1 (void) { + struct process_data* pdata = argument_ptr (); + + pdata->fn (pdata->arg_ptr); + + free (pdata); + quit (); +} diff --git a/libprocess/amd64/src.mk b/libprocess/amd64/src.mk new file mode 100644 index 0000000..62013f1 --- /dev/null +++ b/libprocess/amd64/src.mk @@ -0,0 +1,6 @@ +c += amd64/clone_tramp1.c + +S += amd64/clone_tramp.S + +o += amd64/clone_tramp.o \ + amd64/clone_tramp1.o diff --git a/libprocess/process.c b/libprocess/process.c index 0f08790..ab275f1 100644 --- a/libprocess/process.c +++ b/libprocess/process.c @@ -7,11 +7,32 @@ #include #include -int process_spawn (process_func_t func, void* argument_ptr) { +void _clone_tramp (void); + +struct process_data* process_spawn (process_func_t func, void* argument_ptr) { void* stack = malloc (STACK_SIZE); if (stack == NULL) - return -ST_OOM_ERROR; + return NULL; + + struct process_data* pdata = malloc (sizeof (*pdata)); + + if (pdata == NULL) { + free (stack); + return NULL; + } + + pdata->arg_ptr = argument_ptr; + pdata->fn = func; uintptr_t top = (uintptr_t)stack + STACK_SIZE; - return clone (top, func, argument_ptr); + int pid = clone (top, &_clone_tramp, (void*)pdata); + + if (pid < 0) { + free (stack); + free (pdata); + return NULL; + } + + pdata->pid = pid; + return pdata; } diff --git a/libprocess/process.h b/libprocess/process.h index 3d0c976..fc9a8dc 100644 --- a/libprocess/process.h +++ b/libprocess/process.h @@ -8,9 +8,15 @@ #define STACK_SIZE (256 * PAGE_SIZE) /* Process entry function */ -typedef void (*process_func_t) (void); +typedef void (*process_func_t) (void*); + +struct process_data { + void* arg_ptr; + process_func_t fn; + int pid; +}; /* Spawn a new process within the same procgroup with argument */ -int process_spawn (process_func_t func, void* argument_ptr); +struct process_data* process_spawn (process_func_t func, void* argument_ptr); #endif // _LIBPROCESS_PROCESS_PROCESS_H diff --git a/libprocess/src.mk b/libprocess/src.mk index bbd77b0..57d57e2 100644 --- a/libprocess/src.mk +++ b/libprocess/src.mk @@ -1,3 +1,5 @@ +include $(platform)/src.mk + c += process.c o += process.o