summaryrefslogtreecommitdiff
path: root/src/user/app/init/main.c
diff options
context:
space:
mode:
authordzwdz2022-07-24 15:07:57 +0200
committerdzwdz2022-07-24 15:07:57 +0200
commit9f3fdb830f61cd8c8c1f1db9d03cba1c546c1a7e (patch)
treef5ec030c736f5201332c51cb99169b288185b84b /src/user/app/init/main.c
parent0228be3fd404cdebecf6d21b8964f6063f12dfbe (diff)
user: change the directory structure to prepare for multiple binaries
Diffstat (limited to 'src/user/app/init/main.c')
-rw-r--r--src/user/app/init/main.c89
1 files changed, 89 insertions, 0 deletions
diff --git a/src/user/app/init/main.c b/src/user/app/init/main.c
new file mode 100644
index 0000000..a2c5e46
--- /dev/null
+++ b/src/user/app/init/main.c
@@ -0,0 +1,89 @@
+#include <shared/flags.h>
+#include <shared/syscalls.h>
+#include <stdint.h>
+#include <user/app/init/shell.h>
+#include <user/app/init/driver/driver.h>
+#include <user/lib/fs/misc.h>
+#include <user/app/init/driver/tar.h>
+#include <user/lib/elfload.h>
+#include <user/lib/stdlib.h>
+#include <user/app/init/tests/main.h>
+
+__attribute__((visibility("hidden")))
+extern char _image_base[];
+
+void read_file(const char *path, size_t len);
+
+__attribute__((section(".text.startup")))
+int main(void *initrd) {
+ elf_selfreloc();
+
+ file_reopen(stdout, "/com1", 0);
+ printf("in init, loaded at 0x%x\n", &_image_base);
+
+ /* move everything provided by the kernel to /kdev */
+ MOUNT("/kdev/", fs_passthru(NULL));
+ if (!fork2_n_mount("/")) {
+ const char *l[] = {"/kdev/", NULL};
+ fs_whitelist(l);
+ }
+ if (!fork2_n_mount("/")) fs_dir_inject("/kdev/"); // TODO should be part of fs_whitelist
+
+ MOUNT("/init/", tar_driver(initrd));
+ MOUNT("/tmp/", tmpfs_drv());
+ MOUNT("/keyboard", ps2_drv());
+ MOUNT("/vga_tty", ansiterm_drv());
+
+ MOUNT("/bind/", fs_passthru(NULL));
+
+ if (fork()) {
+ /* (used to) expose a bug in the kernel
+ * the program will flow like this:
+ * 1. we launch the forked init
+ * 2. the forked init launches both shells
+ * 3. one of the shells quit
+ * 4. the forked init picks it up and quits
+ *
+ * then, in process_kill, the other shell will be deathbedded
+ *
+ * before i implement(ed) reparenting, it was a lingering running child
+ * of a dead process, which is invalid state
+ */
+ _syscall_await();
+ _syscall_exit(1);
+ }
+
+ if (!fork()) {
+ if (!file_reopen(stdout, "/kdev/com1", 0)) {
+ printf("couldn't open /kdev/com1\n"); // TODO borked
+ _syscall_exit(1);
+ }
+ if (!file_reopen(stdin, "/kdev/com1", 0)) {
+ printf("couldn't open /kdev/com1\n");
+ _syscall_exit(1);
+ }
+ termcook();
+
+ shell_loop();
+ _syscall_exit(1);
+ }
+
+ if (!fork()) {
+ if (!file_reopen(stdout, "/vga_tty", 0)) {
+ printf("couldn't open /vga_tty\n"); // TODO borked
+ _syscall_exit(1);
+ }
+ if (!file_reopen(stdin, "/keyboard", 0)) {
+ printf("couldn't open /keyboard\n");
+ _syscall_exit(1);
+ }
+ termcook();
+
+ shell_loop();
+ _syscall_exit(1);
+ }
+
+ _syscall_await();
+ printf("init: quitting\n");
+ _syscall_exit(0);
+}