diff options
author | Ian Moffett <ian@osmora.org> | 2025-05-15 17:41:29 -0400 |
---|---|---|
committer | Ian Moffett <ian@osmora.org> | 2025-05-15 18:01:30 -0400 |
commit | 02ca8f9faf6b6ddd0b7a514503b223e9ecbe8fe9 (patch) | |
tree | 905c09181f3ac1026b6d800c9151af527753665e | |
parent | 7c40f732d616a7a2bd005c51ca86d609c2dc4788 (diff) |
kernel: proc: Do not introduce fork()main
Simplicity is divine, fork() may be powerful but is no longer simple. It
became a thing in the late 70s during the early days of UNIX when computing
was simple, when CPUs were only 16-bits, MMUs were not prevalent and RAM was
only 512 KB. However it isn't 1971 anymore, process management, CPUs and memory
architecture has advanced significantly since. This commit ceases work on the
fork() syscall as implementing it would only introduce unnecessary complexity,
security issues, hefty processing overhead and would perpetuate what should
now be considered legacy. The current best alternative would be providing
a form of process spawning as well as a mechanism to wait for the child
process to complete.
Signed-off-by: Ian Moffett <ian@osmora.org>
-rw-r--r-- | sys/arch/amd64/amd64/proc_machdep.c | 2 | ||||
-rw-r--r-- | sys/arch/amd64/isa/i8042.c | 2 | ||||
-rw-r--r-- | sys/include/sys/proc.h | 3 | ||||
-rw-r--r-- | sys/kern/init_main.c | 2 | ||||
-rw-r--r-- | sys/kern/kern_spawn.c (renamed from sys/kern/kern_fork.c) | 55 |
5 files changed, 37 insertions, 27 deletions
diff --git a/sys/arch/amd64/amd64/proc_machdep.c b/sys/arch/amd64/amd64/proc_machdep.c index 0be85fd..9579b7e 100644 --- a/sys/arch/amd64/amd64/proc_machdep.c +++ b/sys/arch/amd64/amd64/proc_machdep.c @@ -155,7 +155,7 @@ md_td_kick(struct proc *td) * @ip: Instruction pointer. */ int -md_fork(struct proc *p, struct proc *parent, uintptr_t ip) +md_spawn(struct proc *p, struct proc *parent, uintptr_t ip) { uintptr_t stack_base; struct trapframe *tfp; diff --git a/sys/arch/amd64/isa/i8042.c b/sys/arch/amd64/isa/i8042.c index 89bebc5..46a8d19 100644 --- a/sys/arch/amd64/isa/i8042.c +++ b/sys/arch/amd64/isa/i8042.c @@ -436,7 +436,7 @@ i8042_init(void) quirks |= I8042_HOSTILE; pr_trace("lenovo device, assuming hostile\n"); pr_trace("disabling irq 1, polling as fallback\n"); - fork1(&polltd, 0, i8042_sync_loop, NULL); + spawn(&polltd, 0, i8042_sync_loop, NULL); } if (!ISSET(quirks, I8042_HOSTILE)) { diff --git a/sys/include/sys/proc.h b/sys/include/sys/proc.h index c561e91..d252ab7 100644 --- a/sys/include/sys/proc.h +++ b/sys/include/sys/proc.h @@ -76,7 +76,8 @@ struct proc { #define PROC_EXEC BIT(1) /* Exec called (cleared by sched) */ struct proc *this_td(void); -int md_fork(struct proc *p, struct proc *parent, uintptr_t ip); +int md_spawn(struct proc *p, struct proc *parent, uintptr_t ip); +int spawn(struct proc *cur, int flags, void(*ip)(void), struct proc **newprocp); void md_td_stackinit(struct proc *td, void *stack_top, struct exec_prog *prog); __dead void md_td_kick(struct proc *td); diff --git a/sys/kern/init_main.c b/sys/kern/init_main.c index 667bb97..911785a 100644 --- a/sys/kern/init_main.c +++ b/sys/kern/init_main.c @@ -101,7 +101,7 @@ main(void) /* Startup pid 1 */ memset(&proc0, 0, sizeof(proc0.tf)); - fork1(&proc0, 0, start_init, NULL); + spawn(&proc0, 0, start_init, NULL); /* Load all drivers */ DRIVERS_INIT(); diff --git a/sys/kern/kern_fork.c b/sys/kern/kern_spawn.c index abb7707..6adf726 100644 --- a/sys/kern/kern_fork.c +++ b/sys/kern/kern_spawn.c @@ -27,49 +27,62 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include <sys/mman.h> -#include <sys/tree.h> -#include <sys/types.h> #include <sys/proc.h> +#include <sys/mman.h> #include <sys/errno.h> -#include <sys/sched.h> +#include <sys/syslog.h> +#include <sys/atomic.h> #include <sys/signal.h> +#include <sys/sched.h> #include <vm/dynalloc.h> #include <string.h> -static size_t nthreads = 0; +#define pr_trace(fmt, ...) kprintf("spawn: " fmt, ##__VA_ARGS__) +#define pr_error(...) pr_trace(__VA_ARGS__) + +static volatile size_t nthreads = 0; /* - * Fork1 - fork and direct a thread to 'ip' + * Spawn a new process * - * @cur: Current process. - * @flags: Flags to set. - * @ip: Location for new thread to start at. - * @newprocp: Will contain new thread if not NULL. + * @cur: Parent (current) process. + * @flags: Spawn flags. + * @ip: Location for process to start + * @newprocp: If not NULL, will contain the new process. */ int -fork1(struct proc *cur, int flags, void(*ip)(void), struct proc **newprocp) +spawn(struct proc *cur, int flags, void(*ip)(void), struct proc **newprocp) { struct proc *newproc; struct mmap_lgdr *mlgdr; - int status = 0; + int error; newproc = dynalloc(sizeof(*newproc)); - if (newproc == NULL) + if (newproc == NULL) { + pr_error("could not alloc proc (-ENOMEM)\n"); return -ENOMEM; + } mlgdr = dynalloc(sizeof(*mlgdr)); - if (mlgdr == NULL) + if (mlgdr == NULL) { + dynfree(newproc); + pr_error("could not alloc proc mlgdr (-ENOMEM)\n"); return -ENOMEM; + } memset(newproc, 0, sizeof(*newproc)); - status = md_fork(newproc, cur, (uintptr_t)ip); - if (status != 0) - goto done; + error = md_spawn(newproc, cur, (uintptr_t)ip); + if (error < 0) { + dynfree(newproc); + dynfree(mlgdr); + pr_error("error initializing proc\n"); + return error; + } /* Set proc output if we can */ - if (newprocp != NULL) + if (newprocp != NULL) { *newprocp = newproc; + } /* Initialize the mmap ledger */ mlgdr->nbytes = 0; @@ -79,9 +92,5 @@ fork1(struct proc *cur, int flags, void(*ip)(void), struct proc **newprocp) newproc->pid = ++nthreads; signals_init(newproc); sched_enqueue_td(newproc); -done: - if (status != 0) - dynfree(newproc); - - return status; + return 0; } |