diff options
author | Freya Murphy <freya@freyacat.org> | 2025-04-30 21:07:46 -0400 |
---|---|---|
committer | Freya Murphy <freya@freyacat.org> | 2025-05-01 15:51:32 -0400 |
commit | 1a10a3725e7bea67e558715f6e9f78abcb415b3a (patch) | |
tree | 1f35cf35f61cd58a86f2a8e7ea14c565db20a211 /user/init.c | |
parent | tarfs (diff) | |
download | comus-1a10a3725e7bea67e558715f6e9f78abcb415b3a.tar.gz comus-1a10a3725e7bea67e558715f6e9f78abcb415b3a.tar.bz2 comus-1a10a3725e7bea67e558715f6e9f78abcb415b3a.zip |
finish syscall impls
Diffstat (limited to '')
-rw-r--r-- | user/init.c | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/user/init.c b/user/init.c new file mode 100644 index 0000000..18e70a2 --- /dev/null +++ b/user/init.c @@ -0,0 +1,75 @@ +#include <stdio.h> +#include <unistd.h> + +#define MAX_ARGS 4 + +struct proc { + pid_t pid; + const char *filename; + const char *args[MAX_ARGS]; +}; + +static struct proc spawn_table[] = { + // apple + { 0, "bin/apple", { NULL } }, + // end, + { 0, NULL, { NULL } }, +}; + +static int spawn(const char *filename, const char **args) +{ + int ret; + + // fork init + if ((ret = fork()) != 0) + return ret; + + // call exec + if ((ret = exec(filename, args))) + exit(ret); + + // should not happen! + exit(1); +} + +int main(void) +{ + struct proc *proc; + + // spawn our processes + for (proc = spawn_table; proc->filename != NULL; proc++) { + int pid; + pid = spawn(proc->filename, proc->args); + if (pid < 0) + fprintf(stderr, "init: cannot exec '%s': %d\n", proc->filename, + pid); + proc->pid = pid; + } + + // clean up dead on restart ours + while (1) { + int pid, status; + + pid = waitpid(0, &status); + if (pid < 0) + continue; + + printf("init: pid %d exited with %d\n", pid, status); + + // figure out if this is one of ours + for (proc = spawn_table; proc->filename != NULL; proc++) { + if (proc->pid == pid) { + proc->pid = 0; + pid = spawn(proc->filename, proc->args); + if (pid < 0) + fprintf(stderr, "init: cannot exec '%s': %d\n", + proc->filename, pid); + proc->pid = pid; + break; + } + } + } + + // very very bad! + return 1; +} |