69 lines
1.3 KiB
C
69 lines
1.3 KiB
C
#include <debugconsole.h>
|
|
#include <malloc.h>
|
|
#include <map.h>
|
|
#include <page_size.h>
|
|
#include <process.h>
|
|
#include <status.h>
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#include <system.h>
|
|
|
|
static int e_pid;
|
|
|
|
static int e_pgid;
|
|
|
|
static int pid;
|
|
|
|
static int pgid;
|
|
|
|
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 NULL;
|
|
|
|
struct process_data* pdata = malloc(sizeof(*pdata));
|
|
|
|
if (pdata == NULL) {
|
|
free(stack);
|
|
return NULL;
|
|
}
|
|
|
|
pdata->stack = stack;
|
|
pdata->arg_ptr = argument_ptr;
|
|
pdata->fn = func;
|
|
|
|
uintptr_t top = (uintptr_t)stack + STACK_SIZE;
|
|
int pid = clone(top, &_clone_tramp, (void*)pdata);
|
|
|
|
if (pid < 0) {
|
|
free(stack);
|
|
free(pdata);
|
|
return NULL;
|
|
}
|
|
|
|
pdata->pid = pid;
|
|
return pdata;
|
|
}
|
|
|
|
void process_data_free(struct process_data* pdata) {
|
|
free(pdata->stack);
|
|
free(pdata);
|
|
}
|
|
|
|
void process_self_init(void) {
|
|
e_pid = get_exec_pid();
|
|
e_pgid = get_procgroup(e_pid);
|
|
pid = get_self_pid();
|
|
pgid = get_procgroup(pid);
|
|
}
|
|
|
|
int process_get_exec_pid(void) { return e_pid; }
|
|
|
|
int process_get_exec_pgid(void) { return e_pgid; }
|
|
|
|
int process_get_pid(void) { return pid; }
|
|
|
|
int process_get_pgid(void) { return pgid; }
|