summaryrefslogtreecommitdiff
path: root/src/user/app
diff options
context:
space:
mode:
Diffstat (limited to 'src/user/app')
-rw-r--r--src/user/app/main.c89
-rw-r--r--src/user/app/shell.c171
-rw-r--r--src/user/app/shell.h3
3 files changed, 263 insertions, 0 deletions
diff --git a/src/user/app/main.c b/src/user/app/main.c
new file mode 100644
index 0000000..e59b5a6
--- /dev/null
+++ b/src/user/app/main.c
@@ -0,0 +1,89 @@
+#include <user/driver/driver.h>
+#include <user/fs/misc.h>
+#include <user/app/shell.h>
+#include <user/lib/stdlib.h>
+#include <user/fs/tar.h>
+#include <user/tests/main.h>
+#include <shared/flags.h>
+#include <shared/syscalls.h>
+#include <stdint.h>
+
+extern char _bss_start; // provided by the linker
+extern char _bss_end;
+extern char _initrd;
+
+void read_file(const char *path, size_t len);
+
+__attribute__((section(".text.startup")))
+int main(void) {
+ // allocate bss
+ _syscall_memflag(&_bss_start, &_bss_end - &_bss_start, MEMFLAG_PRESENT);
+
+ file_reopen(stdout, "/com1", 0);
+ printf("preinit\n");
+
+ /* 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 (_syscall_fork(0, NULL)) {
+ /* (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 (!_syscall_fork(0, NULL)) {
+ 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);
+ }
+
+ shell_loop();
+ _syscall_exit(1);
+ }
+
+
+ if (!_syscall_fork(0, NULL)) {
+ 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);
+ }
+
+ shell_loop();
+ _syscall_exit(1);
+ }
+
+ _syscall_await();
+ printf("init: quitting\n");
+ _syscall_exit(0);
+}
diff --git a/src/user/app/shell.c b/src/user/app/shell.c
new file mode 100644
index 0000000..0a8c44f
--- /dev/null
+++ b/src/user/app/shell.c
@@ -0,0 +1,171 @@
+#include <user/app/shell.h>
+#include <user/lib/stdlib.h>
+#include <user/tests/main.h>
+#include <shared/syscalls.h>
+#include <stdbool.h>
+
+static char *split(char *base) {
+ while (*base) {
+ if (*base == ' ' || *base == '\t') {
+ *base++ = '\0';
+ return base;
+ }
+ base++;
+ }
+ return NULL;
+}
+
+static int readline(char *buf, size_t max) {
+ char c;
+ size_t pos = 0;
+ while (file_read(stdin, &c, 1)) {
+ switch (c) {
+ case '\b':
+ case 0x7f:
+ /* for some reason backspace outputs 0x7f (DEL) */
+ if (pos != 0) {
+ printf("\b \b");
+ pos--;
+ }
+ break;
+ case '\n':
+ case '\r':
+ printf("\n");
+ buf[pos++] = '\0';
+ return pos;
+ default:
+ if (pos < max) {
+ printf("%c", c);
+ buf[pos] = c;
+ pos++;
+ }
+ }
+ }
+ return -1; // error
+}
+
+static void cmd_cat_ls(const char *args, bool ls) {
+ libc_file *file;
+ static char buf[512];
+ int len; // first used for strlen(args), then length of buffer
+
+ if (!args) args = "/";
+ len = strlen(args);
+ memcpy(buf, args, len + 1); // no overflow check - the shell is just a PoC
+
+ if (ls) { // paths to directories always have a trailing slash
+ char *p = buf;
+ while (*p) p++;
+ if (p[-1] != '/') {
+ p[0] = '/';
+ p[1] = '\0';
+ len++;
+ }
+ }
+
+ file = file_open(buf, 0);
+ if (!file) {
+ printf("couldn't open.\n");
+ return;
+ }
+
+ while (!file->eof) {
+ int len = file_read(file, buf, sizeof buf);
+ if (len <= 0) break;
+
+ if (ls) {
+ for (int i = 0; i < len; i++)
+ if (buf[i] == '\0') buf[i] = '\n';
+ }
+ file_write(stdout, buf, len);
+ }
+ file_close(file);
+}
+
+static void cmd_hexdump(const char *args) {
+ static uint8_t buf[512];
+ int fd, len;
+
+ fd = _syscall_open(args, strlen(args), 0);
+ if (fd < 0) {
+ printf("couldn't open.\n");
+ return;
+ }
+
+ len = _syscall_read(fd, buf, sizeof buf, 0);
+ for (int i = 0; i < len; i += 16) {
+ printf("%08x ", i);
+
+ for (int j = i; j < i + 8 && j < len; j++)
+ printf("%02x ", buf[j]);
+ printf(" ");
+ for (int j = i + 8; j < i + 16 && j < len; j++)
+ printf("%02x ", buf[j]);
+ printf(" |");
+
+ for (int j = i; j < i + 16 && j < len; j++) {
+ char c = '.';
+ if (0x20 <= buf[j] && buf[j] < 0x7f) c = buf[j];
+ printf("%c", c);
+ }
+ printf("|\n");
+ }
+
+ _syscall_close(fd);
+}
+
+static void cmd_touch(const char *args) {
+ int fd = _syscall_open(args, strlen(args), OPEN_CREATE);
+ if (fd < 0) {
+ printf("couldn't create file.\n");
+ return;
+ }
+ _syscall_close(fd);
+}
+
+void shell_loop(void) {
+ static char cmd[256];
+ int level = 0;
+ char *args;
+
+ for (;;) {
+ printf("%x$ ", level);
+ readline(cmd, 256);
+ args = split(cmd);
+ if (!strcmp(cmd, "echo")) {
+ printf("%s\n", args);
+ } else if (!strcmp(cmd, "cat")) {
+ cmd_cat_ls(args, false);
+ } else if (!strcmp(cmd, "ls")) {
+ cmd_cat_ls(args, true);
+ } else if (!strcmp(cmd, "hexdump")) {
+ cmd_hexdump(args);
+ } else if (!strcmp(cmd, "catall")) {
+ const char *files[] = {
+ "/init/fake.txt",
+ "/init/1.txt", "/init/2.txt",
+ "/init/dir/3.txt", NULL};
+ for (int i = 0; files[i]; i++) {
+ printf("%s:\n", files[i]);
+ cmd_cat_ls(files[i], false);
+ printf("\n");
+ }
+ } else if (!strcmp(cmd, "touch")) {
+ cmd_touch(args);
+ } else if (!strcmp(cmd, "shadow")) {
+ _syscall_mount(-1, args, strlen(args));
+ } else if (!strcmp(cmd, "exit")) {
+ _syscall_exit(0);
+ } else if (!strcmp(cmd, "fork")) {
+ if (_syscall_fork(0, NULL))
+ _syscall_await();
+ else level++;
+ } else if (!strcmp(cmd, "run_tests")) {
+ test_all();
+ } else if (!strcmp(cmd, "stress")) {
+ stress_all();
+ } else {
+ printf("unknown command :(\n");
+ }
+ }
+}
diff --git a/src/user/app/shell.h b/src/user/app/shell.h
new file mode 100644
index 0000000..fb27410
--- /dev/null
+++ b/src/user/app/shell.h
@@ -0,0 +1,3 @@
+#pragma once
+
+void shell_loop(void);