From 9f3fdb830f61cd8c8c1f1db9d03cba1c546c1a7e Mon Sep 17 00:00:00 2001 From: dzwdz Date: Sun, 24 Jul 2022 15:07:57 +0200 Subject: user: change the directory structure to prepare for multiple binaries --- src/user/app/init/driver/ansiterm.c | 99 ++++++++++++++ src/user/app/init/driver/driver.h | 7 + src/user/app/init/driver/ps2.c | 86 ++++++++++++ src/user/app/init/driver/tar.c | 158 ++++++++++++++++++++++ src/user/app/init/driver/tar.h | 4 + src/user/app/init/driver/termcook.c | 71 ++++++++++ src/user/app/init/driver/tmpfs.c | 132 +++++++++++++++++++ src/user/app/init/main.c | 89 +++++++++++++ src/user/app/init/shell.c | 200 ++++++++++++++++++++++++++++ src/user/app/init/shell.h | 3 + src/user/app/init/tests/main.c | 256 ++++++++++++++++++++++++++++++++++++ src/user/app/init/tests/main.h | 18 +++ src/user/app/init/tests/pipe.c | 116 ++++++++++++++++ src/user/app/init/tests/semaphore.c | 89 +++++++++++++ src/user/app/init/tests/stress.c | 28 ++++ src/user/app/main.c | 89 ------------- src/user/app/shell.c | 200 ---------------------------- src/user/app/shell.h | 3 - src/user/driver/ansiterm.c | 99 -------------- src/user/driver/driver.h | 7 - src/user/driver/ps2.c | 86 ------------ src/user/driver/termcook.c | 71 ---------- src/user/driver/tmpfs.c | 132 ------------------- src/user/fs/misc.c | 163 ----------------------- src/user/fs/misc.h | 16 --- src/user/fs/tar.c | 158 ---------------------- src/user/fs/tar.h | 4 - src/user/lib/fs/misc.c | 163 +++++++++++++++++++++++ src/user/lib/fs/misc.h | 16 +++ src/user/tests/main.c | 256 ------------------------------------ src/user/tests/main.h | 18 --- src/user/tests/pipe.c | 116 ---------------- src/user/tests/semaphore.c | 89 ------------- src/user/tests/stress.c | 28 ---- 34 files changed, 1535 insertions(+), 1535 deletions(-) create mode 100644 src/user/app/init/driver/ansiterm.c create mode 100644 src/user/app/init/driver/driver.h create mode 100644 src/user/app/init/driver/ps2.c create mode 100644 src/user/app/init/driver/tar.c create mode 100644 src/user/app/init/driver/tar.h create mode 100644 src/user/app/init/driver/termcook.c create mode 100644 src/user/app/init/driver/tmpfs.c create mode 100644 src/user/app/init/main.c create mode 100644 src/user/app/init/shell.c create mode 100644 src/user/app/init/shell.h create mode 100644 src/user/app/init/tests/main.c create mode 100644 src/user/app/init/tests/main.h create mode 100644 src/user/app/init/tests/pipe.c create mode 100644 src/user/app/init/tests/semaphore.c create mode 100644 src/user/app/init/tests/stress.c delete mode 100644 src/user/app/main.c delete mode 100644 src/user/app/shell.c delete mode 100644 src/user/app/shell.h delete mode 100644 src/user/driver/ansiterm.c delete mode 100644 src/user/driver/driver.h delete mode 100644 src/user/driver/ps2.c delete mode 100644 src/user/driver/termcook.c delete mode 100644 src/user/driver/tmpfs.c delete mode 100644 src/user/fs/misc.c delete mode 100644 src/user/fs/misc.h delete mode 100644 src/user/fs/tar.c delete mode 100644 src/user/fs/tar.h create mode 100644 src/user/lib/fs/misc.c create mode 100644 src/user/lib/fs/misc.h delete mode 100644 src/user/tests/main.c delete mode 100644 src/user/tests/main.h delete mode 100644 src/user/tests/pipe.c delete mode 100644 src/user/tests/semaphore.c delete mode 100644 src/user/tests/stress.c diff --git a/src/user/app/init/driver/ansiterm.c b/src/user/app/init/driver/ansiterm.c new file mode 100644 index 0000000..df7fb23 --- /dev/null +++ b/src/user/app/init/driver/ansiterm.c @@ -0,0 +1,99 @@ +#include +#include +#include + +struct vga_cell { + unsigned char c; + unsigned char style; +} __attribute__((__packed__)); +static struct vga_cell vga[80 * 25]; + +static handle_t vga_fd; + +static struct {int x, y;} cursor = {0}; +static bool dirty = false; +static bool pendingFlush = false; + +static void flush(void) { + size_t off = 0; + /* we have to do multiple write() calls if we're behind a shitty passthrough fs + * i don't like this either */ + while (off < sizeof(vga)) + off += _syscall_write(vga_fd, (void*)vga + off, sizeof(vga) - off, off); + dirty = false; + pendingFlush = false; +} + +static void scroll(void) { + for (size_t i = 0; i < 80 * 24; i++) + vga[i] = vga[i + 80]; + for (size_t i = 80 * 24; i < 80 * 25; i++) + vga[i].c = ' '; + cursor.y--; + pendingFlush = true; +} + +static void in_char(char c) { + switch (c) { + case '\n': + cursor.x = 0; + cursor.y++; + pendingFlush = true; + break; + case '\b': + if (--cursor.x < 0) cursor.x = 0; + break; + default: + vga[cursor.y * 80 + cursor.x++].c = c; + } + + if (cursor.x >= 80) { + cursor.x = 0; + cursor.y++; + } + while (cursor.y >= 25) scroll(); + dirty = true; +} + +void ansiterm_drv(void) { + vga_fd = _syscall_open("/kdev/vga", 9, 0); + _syscall_read(vga_fd, vga, sizeof vga, 0); + + // find first empty line + for (cursor.y = 0; cursor.y < 25; cursor.y++) { + for (cursor.x = 0; cursor.x < 80; cursor.x++) { + char c = vga[cursor.y * 80 + cursor.x].c; + if (c != ' ' && c != '\0') break; + } + if (cursor.x == 80) break; + } + cursor.x = 0; + + for (int i = 0; i < 80 * 25; i++) + vga[i].style = 0x70; + flush(); + + static char buf[512]; + struct fs_wait_response res; + while (!_syscall_fs_wait(buf, sizeof buf, &res)) { + switch (res.op) { + case VFSOP_OPEN: + // TODO check path + _syscall_fs_respond(NULL, 0, 0); + break; + + case VFSOP_WRITE: + for (size_t i = 0; i < res.len; i++) + in_char(buf[i]); + /* if (pendingFlush) */ flush(); + _syscall_fs_respond(NULL, res.len, 0); + break; + + default: + _syscall_fs_respond(NULL, -1, 0); + break; + } + } + + _syscall_exit(1); +} diff --git a/src/user/app/init/driver/driver.h b/src/user/app/init/driver/driver.h new file mode 100644 index 0000000..d6e7343 --- /dev/null +++ b/src/user/app/init/driver/driver.h @@ -0,0 +1,7 @@ +#pragma once + +void ansiterm_drv(void); +void ps2_drv(void); +void tmpfs_drv(void); + +void termcook(void); diff --git a/src/user/app/init/driver/ps2.c b/src/user/app/init/driver/ps2.c new file mode 100644 index 0000000..bc7f4f0 --- /dev/null +++ b/src/user/app/init/driver/ps2.c @@ -0,0 +1,86 @@ +#include +#include +#include +#include + + +static const char keymap_lower[] = { + '\0', '\0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b', '\t', + 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\r', '\0', 'a', 's', + 'd', 'f', 'g', 'h', 'j', 'k', 'l', '\0', '\'', '`', '\0', '\\', 'z', 'x', 'c', 'v', + 'b', 'n', 'm', ',', '.', '/', '\0', '*', '\0', ' ', '\0', '\0', '\0', '\0', '\0', '\0', + '\0', '\0', '\0', '\0', '\0', '\0', '\0', '7', '8', '9', '-', '4', '5', '6', '+', '1', + '2', '3', '0', '.', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', + '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', + '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', +}; + +static const char keymap_upper[] = { + '\0', '\0', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '\b', '\t', + 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\r', '\0', 'A', 'S', + 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~', '\0', '|', 'Z', 'X', 'C', 'V', + 'B', 'N', 'M', '<', '>', '?', '\0', '*', '\0', ' ', '\0', '\0', '\0', '\0', '\0', '\0', + '\0', '\0', '\0', '\0', '\0', '\0', '\0', '7', '8', '9', '-', '4', '5', '6', '+', '1', + '2', '3', '0', '.', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', + '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', + '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', +}; + + +static volatile uint8_t backlog_buf[16]; +static volatile ring_t backlog = {(void*)backlog_buf, sizeof backlog_buf, 0, 0}; + +static handle_t fd; + +static bool keys[0x80] = {0}; + +static void parse_scancode(uint8_t s) { + bool ctrl = keys[0x1D]; + bool shift = keys[0x2A] || keys[0x36]; + bool down = !(s & 0x80); + char c; + s &= 0x7f; + keys[s] = down; + + c = shift ? keymap_upper[s] : keymap_lower[s]; + if (ctrl && keymap_upper[s] >= 'A' && keymap_upper[s] <= 'Z') + c = keymap_upper[s] - 'A' + 1; + if (down && c) ring_put1b((void*)&backlog, c); +} + +static void main_loop(void) { + static char buf[512]; + struct fs_wait_response res; + int ret; + while (!_syscall_fs_wait(buf, sizeof buf, &res)) { + switch (res.op) { + case VFSOP_OPEN: + _syscall_fs_respond(NULL, 1, 0); + break; + + case VFSOP_READ: + while (ring_size((void*)&backlog) == 0) { + /* read raw input until we have something to output */ + int len = _syscall_read(fd, buf, sizeof buf, 0); + if (len == 0) break; + for (int i = 0; i < len; i++) + parse_scancode(buf[i]); + } + ret = ring_get((void*)&backlog, buf, res.capacity); + _syscall_fs_respond(buf, ret, 0); + break; + + default: + _syscall_fs_respond(NULL, -1, 0); + break; + } + } +} + +void ps2_drv(void) { + fd = _syscall_open("/kdev/ps2", 9, 0); + if (fd < 0) _syscall_exit(1); + + main_loop(); + _syscall_exit(0); +} diff --git a/src/user/app/init/driver/tar.c b/src/user/app/init/driver/tar.c new file mode 100644 index 0000000..60deccd --- /dev/null +++ b/src/user/app/init/driver/tar.c @@ -0,0 +1,158 @@ +#include +#include +#include +#include + +#define BUF_SIZE 64 + +static void *tar_open(const char *path, int len, void *base, size_t base_len); +static void tar_read(struct fs_wait_response *res, void *base, size_t base_len); +static int tar_size(void *sector); +static void *tar_find(const char *path, size_t path_len, void *base, size_t base_len); +static int oct_parse(char *str, size_t len); + + +static const char *root_fakemeta = ""; /* see comment in tar_open */ + + +void tar_driver(void *base) { + static char buf[BUF_SIZE]; + struct fs_wait_response res; + void *ptr; + while (!_syscall_fs_wait(buf, BUF_SIZE, &res)) { + switch (res.op) { + case VFSOP_OPEN: + ptr = tar_open(buf, res.len, base, ~0); + _syscall_fs_respond(ptr, ptr ? 0 : -1, 0); + break; + + case VFSOP_READ: + tar_read(&res, base, ~0); + break; + + default: + _syscall_fs_respond(NULL, -1, 0); // unsupported + break; + } + } + _syscall_exit(0); +} + +static void *tar_open(const char *path, int len, void *base, size_t base_len) { + if (len <= 0) return NULL; + path += 1; // skip the leading slash + len -= 1; + + /* TAR archives don't (seem to) contain an entry for the root dir, so i'm + * returning a fake one. this isn't a full entry because i'm currently too + * lazy to create a full one - thus, it has to be special cased in tar_read */ + if (len == 0) + return (void*)root_fakemeta; + + return tar_find(path, len, base, base_len); +} + +static void tar_read(struct fs_wait_response *res, void *base, size_t base_len) { + void *meta = (void*)res->id; + char type = *(char*)(meta + 156); + size_t meta_len; + int size; + + static char buf[BUF_SIZE]; // TODO reuse a single buffer + size_t buf_pos = 0; + + if (meta == root_fakemeta) type = '5'; /* see comment in tar_open() */ + + switch (type) { + case '\0': + case '0': /* normal files */ + size = tar_size(meta); + if (res->offset < 0 || res->offset > size) { + // TODO support negative offsets + _syscall_fs_respond(NULL, -1, 0); + } else { + _syscall_fs_respond(meta + 512 + res->offset, size - res->offset, 0); + } + break; + + case '5': /* directory */ + meta_len = strlen(meta); + size_t to_skip = res->offset; + + /* find files in dir */ + for (size_t off = 0; off < base_len;) { + if (0 != memcmp(base + off + 257, "ustar", 5)) + break; // not a metadata sector + // TODO more meaningful variable names and clean code up + + /* check if prefix matches */ + if (0 == memcmp(base + off, meta, meta_len) && + *(char*)(base + off + meta_len) != '\0') { + char *suffix = base + off + meta_len; + size_t suffix_len = strlen(suffix); + + /* check if the path contains any non-trailing slashes */ + char *next = suffix; + while (*next && *next != '/') next++; + if (*next == '/') next++; + if (*next == '\0') { + if (to_skip > suffix_len) { + to_skip -= suffix_len; + } else { + suffix += to_skip; + suffix_len -= to_skip; + to_skip = 0; + + /* it doesn't - so let's add it to the result */ + memcpy(buf + buf_pos, suffix, suffix_len); + buf[buf_pos + suffix_len] = '\0'; + buf_pos += suffix_len + 1; + // TODO no buffer overrun check + } + } + } + + size = tar_size(base + off); + off += 512; // skip this metadata sector + off += (size + 511) & ~511; // skip the data sectors + } + + _syscall_fs_respond(buf, buf_pos, 0); + break; + + default: + _syscall_fs_respond(NULL, -1, 0); + break; + } +} + +static int tar_size(void *sector) { + return oct_parse(sector + 124, 11); +} + +static void *tar_find(const char *path, size_t path_len, void *base, size_t base_len) { + int size; + if (path_len > 100) return NULL; // illegal path + + for (size_t off = 0; off < base_len;) { + if (0 != memcmp(base + off + 257, "ustar", 5)) + break; // not a metadata sector + if (0 == memcmp(base + off, path, path_len) && + *(char*)(base + off + path_len) == '\0') + return base + off; // file found, quit + + size = tar_size(base + off); + off += 512; // skip this metadata sector + off += (size + 511) & ~511; // skip the data sectors + } + return NULL; +} + +static int oct_parse(char *str, size_t len) { + int res = 0; + for (size_t i = 0; i < len; i++) { + res *= 8; + res += str[i] - '0'; // no format checking + } + return res; +} diff --git a/src/user/app/init/driver/tar.h b/src/user/app/init/driver/tar.h new file mode 100644 index 0000000..c1dee78 --- /dev/null +++ b/src/user/app/init/driver/tar.h @@ -0,0 +1,4 @@ +#pragma once +#include + +_Noreturn void tar_driver(void *base); diff --git a/src/user/app/init/driver/termcook.c b/src/user/app/init/driver/termcook.c new file mode 100644 index 0000000..4142322 --- /dev/null +++ b/src/user/app/init/driver/termcook.c @@ -0,0 +1,71 @@ +#include +#include +#include + +static void w_output(handle_t output, const char *buf, size_t len) { + size_t pos = 0; + while (pos < len) { + int ret = _syscall_write(output, buf + pos, len - pos, pos); + if (ret < 0) break; + pos += ret; + } +} + +static void line_editor(handle_t input, handle_t output) { + char readbuf[16], linebuf[256]; + size_t linepos = 0; + for (;;) { + int readlen = _syscall_read(input, readbuf, sizeof readbuf, -1); + if (readlen < 0) return; + for (int i = 0; i < readlen; i++) { + char c = readbuf[i]; + switch (c) { + case '\b': + case 0x7f: + if (linepos != 0) { + printf("\b \b"); + linepos--; + } + break; + case 4: /* EOT, C-d */ + if (linepos > 0) { + w_output(output, linebuf, linepos); + linepos = 0; + } else { + _syscall_write(output, NULL, 0, 0); // eof + } + break; + case '\n': + case '\r': + printf("\n"); + if (linepos < sizeof linebuf) + linebuf[linepos++] = '\n'; + w_output(output, linebuf, linepos); + linepos = 0; + break; + default: + if (linepos < sizeof linebuf) { + linebuf[linepos++] = c; + printf("%c", c); + } + break; + } + } + } +} + +void termcook(void) { + handle_t stdin_pipe[2] = {-1, -1}; + if (_syscall_pipe(stdin_pipe, 0) < 0) + return; + + if (!fork()) { + close(stdin_pipe[0]); + line_editor(0, stdin_pipe[1]); + _syscall_exit(0); + } + /* 0 is stdin, like in unix */ + _syscall_dup(stdin_pipe[0], 0, 0); + close(stdin_pipe[0]); + close(stdin_pipe[1]); +} diff --git a/src/user/app/init/driver/tmpfs.c b/src/user/app/init/driver/tmpfs.c new file mode 100644 index 0000000..6e5b45e --- /dev/null +++ b/src/user/app/init/driver/tmpfs.c @@ -0,0 +1,132 @@ +#include +#include +#include +#include + +struct node { + const char *name; + size_t namelen; + struct node *next; + char *buf; + size_t size, capacity; +}; + +struct node *root = NULL; +static struct node special_root; + +static struct node *lookup(const char *path, size_t len) { + for (struct node *iter = root; iter; iter = iter->next) { + if (iter->namelen == len && !memcmp(path, iter->name, len)) + return iter; + } + return NULL; +} + +static struct node *tmpfs_open(const char *path, struct fs_wait_response *res) { + struct node *node; + if (res->len == 0) return NULL; + path++; + res->len--; + + if (res->len == 0) return &special_root; + + // no directory support (yet) + if (memchr(path, '/', res->len)) return NULL; + + node = lookup(path, res->len); + if (!node && (res->flags & OPEN_CREATE)) { + node = malloc(sizeof *node); + memset(node, 0, sizeof *node); + + char *namebuf = malloc(res->len); + memcpy(namebuf, path, res->len); + node->name = namebuf; + node->namelen = res->len; + node->next = root; + root = node; + } + return node; +} + +void tmpfs_drv(void) { + // TODO replace all the static allocations in drivers with mallocs + static char buf[512]; + struct fs_wait_response res; + struct node *ptr; + while (!_syscall_fs_wait(buf, sizeof buf, &res)) { + switch (res.op) { + case VFSOP_OPEN: + ptr = tmpfs_open(buf, &res); + _syscall_fs_respond(ptr, ptr ? 0 : -1, 0); + break; + + case VFSOP_READ: + ptr = (void*)res.id; + if (ptr == &special_root) { + size_t buf_pos = 0; + size_t to_skip = res.offset; + + for (struct node *iter = root; iter; iter = iter->next) { + if (iter->namelen <= to_skip) { + to_skip -= iter->namelen; + continue; + } + + if (iter->namelen + buf_pos - to_skip >= sizeof(buf)) { + memcpy(buf + buf_pos, iter->name + to_skip, sizeof(buf) - buf_pos - to_skip); + buf_pos = sizeof(buf); + break; + } + memcpy(buf + buf_pos, iter->name + to_skip, iter->namelen - to_skip); + buf_pos += iter->namelen - to_skip; + buf[buf_pos++] = '\0'; + to_skip = 0; + } + _syscall_fs_respond(buf, buf_pos, 0); + } else { + // TODO offset + if (res.offset) + _syscall_fs_respond(NULL, 0, 0); + else + _syscall_fs_respond(ptr->buf, ptr->size, 0); + break; + } + break; + + case VFSOP_WRITE: + ptr = (void*)res.id; + if (ptr == &special_root) { + _syscall_fs_respond(NULL, -1, 0); + break; + } + if (res.len == 0) { + _syscall_fs_respond(NULL, 0, 0); + break; + } + if (!ptr->buf) { + ptr->buf = malloc(256); + if (!ptr->buf) { + _syscall_fs_respond(NULL, -1, 0); + break; + } + memset(ptr->buf, 0, 256); + ptr->capacity = 256; + } + + size_t len = res.len; + if (len > ptr->capacity - res.offset) + len = ptr->capacity - res.offset; + memcpy(ptr->buf + res.offset, buf, len); + if (ptr->size < res.offset + len) + ptr->size = res.offset + len; + _syscall_fs_respond(NULL, len, 0); + break; + + default: + _syscall_fs_respond(NULL, -1, 0); + break; + } + } + + _syscall_exit(1); +} 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include + +__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); +} diff --git a/src/user/app/init/shell.c b/src/user/app/init/shell.c new file mode 100644 index 0000000..cda8a65 --- /dev/null +++ b/src/user/app/init/shell.c @@ -0,0 +1,200 @@ +#include +#include +#include +#include +#include +#include + +static bool isspace(char c) { + return c == ' ' || c == '\t' || c == '\n'; +} + +static char *strsplit(char *base, char delim) { + if (!base) return NULL; + while (*base) { + if (delim ? *base == delim : isspace(*base)) { + *base++ = '\0'; + return base; + } + base++; + } + return NULL; +} + +static char *strtrim(char *s) { + char *end; + if (!s) return NULL; + while (isspace(*s)) s++; + end = s + strlen(s); + while (end > s && isspace(end[-1])) end--; + *end = '\0'; + return s; +} + + +// TODO fgets +static int readline(char *buf, size_t max) { + char c = '\0'; + size_t pos = 0; + while (pos < (max-1) && c != '\n' && file_read(stdin, &c, 1)) + buf[pos++] = c; + buf[pos++] = '\0'; + return pos; +} + +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) { + 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 + if (buf[len-1] != '/') { + buf[len] = '/'; + buf[len+1] = '\0'; + } + } + + file = file_open(buf, 0); + } else if (ls) { /* ls default argument */ + file = file_open("/", 0); + } else { /* cat default argument */ + file = file_clone(stdin); + } + + 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"); + } + + 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; + } + close(fd); +} + +void shell_loop(void) { + static char buf[256]; + int level = 0; + char *cmd, *args, *redir; + + for (;;) { + printf("%x$ ", level); + + readline(buf, 256); + redir = strtrim(strsplit(buf, '>')); + cmd = strtrim(buf); + args = strtrim(strsplit(cmd, 0)); + + /* "special" commands that can't be handled in a subprocess */ + if (!strcmp(cmd, "shadow")) { + _syscall_mount(-1, args, strlen(args)); + continue; + } else if (!strcmp(cmd, "exit")) { + _syscall_exit(0); + continue; + } else if (!strcmp(cmd, "fork")) { + if (!fork()) level++; + else _syscall_await(); + continue; + } + + if (!fork()) { + if (redir && !file_reopen(stdout, redir, OPEN_CREATE)) { + // TODO stderr + _syscall_exit(0); + } + + if (!strcmp(cmd, "echo")) { + printf("%s\n", args); + } else if (!strcmp(cmd, "exec")) { + libc_file *file = file_open(args, 0); + if (!file) { + printf("couldn't open file\n"); + } else { + elf_execf(file); + file_close(file); + printf("elf_execf failed\n"); + } + } 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, "run_tests")) { + test_all(); + } else if (!strcmp(cmd, "stress")) { + stress_all(); + } else { + printf("unknown command :(\n"); + } + _syscall_exit(0); + } else { + _syscall_await(); + } + } +} diff --git a/src/user/app/init/shell.h b/src/user/app/init/shell.h new file mode 100644 index 0000000..fb27410 --- /dev/null +++ b/src/user/app/init/shell.h @@ -0,0 +1,3 @@ +#pragma once + +void shell_loop(void); diff --git a/src/user/app/init/tests/main.c b/src/user/app/init/tests/main.c new file mode 100644 index 0000000..5db6324 --- /dev/null +++ b/src/user/app/init/tests/main.c @@ -0,0 +1,256 @@ +#define TEST_MACROS +#include +#include +#include +#include +#include +#include + +static void run_forked(void (*fn)()) { + if (!fork()) { + fn(); + _syscall_exit(0); + } else { + /* successful tests must return 0 + * TODO add a better fail msg */ + if (_syscall_await() != 0) test_fail(); + } +} + + +const char *tmpfilepath = "/tmp/.test_internal"; + +static void test_await(void) { + /* creates 16 child processes, each returning a different value. then checks + * if await() returns every value exactly once */ + int ret; + int counts[16] = {0}; + + for (int i = 0; i < 16; i++) + if (!fork()) + _syscall_exit(i); + + while ((ret = _syscall_await()) != ~0) { + assert(0 <= ret && ret < 16); + counts[ret]++; + } + + for (int i = 0; i < 16; i++) + assert(counts[i] == 1); +} + +static void test_faults(void) { + /* tests what happens when child processes fault. + * expected behavior: parent processes still manages to finish, and it can + * reap all its children */ + int await_cnt = 0; + + if (!fork()) { // invalid memory access + asm volatile("movb $69, 0" ::: "memory"); + printf("this shouldn't happen"); + _syscall_exit(-1); + } + if (!fork()) { // #GP + asm volatile("hlt" ::: "memory"); + printf("this shouldn't happen"); + _syscall_exit(-1); + } + + while (_syscall_await() != ~0) await_cnt++; + assert(await_cnt == 2); +} + +static void test_interrupted_fs(void) { + handle_t h; + if (_syscall_fork(FORK_NEWFS, &h)) { /* child */ + // TODO make a similar test with all 0s passed to fs_wait + struct fs_wait_response res; + _syscall_fs_wait(NULL, 0, &res); + _syscall_exit(0); + } else { /* parent */ + _syscall_mount(h, "/", 1); + int ret = _syscall_open("/", 1, 0); + // the handler quits while handling that call - but this syscall should return anyways + _syscall_exit(ret < 0 ? 0 : -1); + } +} + +static void test_orphaned_fs(void) { + handle_t h; + if (_syscall_fork(FORK_NEWFS, &h)) { /* child */ + _syscall_exit(0); + } else { /* parent */ + _syscall_mount(h, "/", 1); + int ret = _syscall_open("/", 1, 0); + // no handler will ever be available to handle this call - the syscall should instantly return + _syscall_exit(ret < 0 ? 0 : -1); + } +} + +static void test_memflag(void) { + void *page = (void*)0x77777000; + _syscall_memflag(page, 4096, MEMFLAG_PRESENT); // allocate page + memset(page, 77, 4096); // write to it + _syscall_memflag(page, 4096, 0); // free it + + if (!fork()) { + memset(page, 11, 4096); // should segfault + _syscall_exit(0); + } else { + assert(_syscall_await() != 0); // test if the process crashed + } + + char *pages[4]; + for (size_t i = 0; i < 4; i++) { + pages[i] = _syscall_memflag(NULL, 4096, MEMFLAG_FINDFREE); + printf("[test_memflag] findfree: 0x%x\n", pages[i]); + + assert(pages[i] == _syscall_memflag(NULL, 4096, MEMFLAG_FINDFREE | MEMFLAG_PRESENT)); + if (i > 0) assert(pages[i] != pages[i-1]); + *pages[i] = 0x77; + } + + for (size_t i = 0; i < 4; i++) + _syscall_memflag(pages, 4096, MEMFLAG_PRESENT); + + // TODO check if reclaims +} + +static void test_dup(void) { + handle_t pipe[2]; + handle_t h1, h2; + assert(_syscall_pipe(pipe, 0) >= 0); + + if (!fork()) { + close(pipe[0]); + + h1 = _syscall_dup(pipe[1], -1, 0); + assert(h1 >= 0); + assert(h1 != pipe[1]); + h2 = _syscall_dup(h1, -1, 0); + assert(h2 >= 0); + assert(h2 != pipe[1] && h2 != h1); + + _syscall_write(pipe[1], "og", 2, 0); + _syscall_write(h1, "h1", 2, 0); + _syscall_write(h2, "h2", 2, 0); + + close(pipe[1]); + _syscall_write(h1, "h1", 2, 0); + _syscall_write(h2, "h2", 2, 0); + + assert(_syscall_dup(h1, pipe[1], 0) == pipe[1]); + assert(_syscall_dup(h2, pipe[1], 0) == pipe[1]); + assert(_syscall_dup(h1, pipe[1], 0) == pipe[1]); + assert(_syscall_dup(h2, pipe[1], 0) == pipe[1]); + close(h1); + close(h2); + + assert(_syscall_dup(pipe[1], h2, 0) == h2); + _syscall_write(h2, "h2", 2, 0); + close(h2); + + assert(_syscall_dup(pipe[1], h1, 0) == h1); + _syscall_write(h1, "h1", 2, 0); + close(h1); + + _syscall_exit(0); + } else { + char buf[16]; + size_t count = 0; + close(pipe[1]); + while (_syscall_read(pipe[0], buf, sizeof buf, 0) >= 0) + count++; + assert(count == 7); + _syscall_await(); + } + + + close(pipe[0]); +} + +static void test_malloc(void) { + // not really a test + void *p1, *p2; + + p1 = malloc(420); + printf("p1 = 0x%x\n", p1); + + p2 = malloc(1024); + printf("p2 = 0x%x\n", p2); + free(p2); + p2 = malloc(256); + printf("p2 = 0x%x\n", p2); + free(p2); + p2 = malloc(4096); + printf("p2 = 0x%x\n", p2); + free(p2); + + free(p1); +} + +static void test_efault(void) { + const char *str = "o, 16 characters"; + char str2[16]; + char *invalid = (void*)0x1000; + handle_t h; + + memcpy(str2, str, 16); + + assert((h = _syscall_open(tmpfilepath, strlen(tmpfilepath), OPEN_CREATE))); + assert(_syscall_write(h, str, 16, 0) == 16); + assert(_syscall_write(h, str2, 16, 0) == 16); + + assert(_syscall_write(h, invalid, 16, 0) == -EFAULT); + + /* x64 canonical pointers */ + assert(_syscall_write(h, (void*)((uintptr_t)str ^ 0x8000000000000000), 16, 0) == -EFAULT); + assert(_syscall_write(h, (void*)((uintptr_t)str ^ 0x1000000000000000), 16, 0) == -EFAULT); + assert(_syscall_write(h, (void*)((uintptr_t)str ^ 0x0100000000000000), 16, 0) == -EFAULT); + assert(_syscall_write(h, (void*)((uintptr_t)str ^ 0x0010000000000000), 16, 0) == -EFAULT); + assert(_syscall_write(h, (void*)((uintptr_t)str ^ 0x0001000000000000), 16, 0) == -EFAULT); + assert(_syscall_write(h, (void*)((uintptr_t)str ^ 0x0000800000000000), 16, 0) == -EFAULT); + + assert(_syscall_write(h, (void*)((uintptr_t)str2 ^ 0x8000000000000000), 16, 0) == -EFAULT); + assert(_syscall_write(h, (void*)((uintptr_t)str2 ^ 0x1000000000000000), 16, 0) == -EFAULT); + assert(_syscall_write(h, (void*)((uintptr_t)str2 ^ 0x0100000000000000), 16, 0) == -EFAULT); + assert(_syscall_write(h, (void*)((uintptr_t)str2 ^ 0x0010000000000000), 16, 0) == -EFAULT); + assert(_syscall_write(h, (void*)((uintptr_t)str2 ^ 0x0001000000000000), 16, 0) == -EFAULT); + assert(_syscall_write(h, (void*)((uintptr_t)str2 ^ 0x0000800000000000), 16, 0) == -EFAULT); + + assert(_syscall_write(h, str, 16, 0) == 16); + close(h); +} + +static void test_execbuf(void) { + // not really a test, TODO + const char str1[] = "test_execbuf: string 1\n"; + const char str2[] = "test_execbuf: and 2\n"; + uint64_t buf[] = { + EXECBUF_SYSCALL, _SYSCALL_WRITE, 1, (uintptr_t)str1, sizeof(str1) - 1, -1, + EXECBUF_SYSCALL, _SYSCALL_WRITE, 1, (uintptr_t)str2, sizeof(str2) - 1, -1, + EXECBUF_SYSCALL, _SYSCALL_EXIT, 0, 0, 0, 0, + }; + _syscall_execbuf(buf, sizeof buf); + test_fail(); +} + +static void test_misc(void) { + assert(_syscall(~0, 0, 0, 0, 0) < 0); /* try making an invalid syscall */ +} + + +void test_all(void) { + run_forked(test_await); + run_forked(test_faults); + run_forked(test_interrupted_fs); + run_forked(test_orphaned_fs); + run_forked(test_memflag); + run_forked(test_dup); + run_forked(test_malloc); + run_forked(test_pipe); + run_forked(test_semaphore); + run_forked(test_efault); + run_forked(test_execbuf); + run_forked(test_misc); +} diff --git a/src/user/app/init/tests/main.h b/src/user/app/init/tests/main.h new file mode 100644 index 0000000..ed11c5e --- /dev/null +++ b/src/user/app/init/tests/main.h @@ -0,0 +1,18 @@ +#pragma once + +void stress_all(void); +void test_all(void); + +void test_pipe(void); +void test_semaphore(void); + +#ifdef TEST_MACROS + +#define argify(str) str, sizeof(str) - 1 +#define test_fail() do { \ + printf("\033[31m" "TEST FAILED: %s:%xh\n" "\033[0m", __func__, __LINE__); \ + _syscall_exit(0); \ +} while (0) +#define assert(cond) if (!(cond)) test_fail(); + +#endif diff --git a/src/user/app/init/tests/pipe.c b/src/user/app/init/tests/pipe.c new file mode 100644 index 0000000..e7724e9 --- /dev/null +++ b/src/user/app/init/tests/pipe.c @@ -0,0 +1,116 @@ +#define TEST_MACROS +#include +#include +#include +#include + +static const char *pipe_msgs[2] = {"hello", "world"}; + +void test_pipe(void) { + handle_t ends[2]; + char buf[16]; + int ret; + + /* test regular reads / writes */ + assert(_syscall_pipe(ends, 0) >= 0); + if (!fork()) { + /* those repeated asserts ensure that you can't read/write to the wrong ends */ + assert(_syscall_read(ends[1], buf, 16, 0) < 0); + assert(_syscall_write(ends[0], buf, 16, 0) < 0); + + ret = _syscall_write(ends[1], pipe_msgs[0], 5, -1); + assert(ret == 5); + + assert(_syscall_read(ends[1], buf, 16, 0) < 0); + assert(_syscall_write(ends[0], buf, 16, 0) < 0); + + ret = _syscall_write(ends[1], pipe_msgs[1], 5, -1); + assert(ret == 5); + + _syscall_exit(0); + } else { + assert(_syscall_read(ends[1], buf, 16, 0) < 0); + assert(_syscall_write(ends[0], buf, 16, 0) < 0); + + ret = _syscall_read(ends[0], buf, 16, 0); + assert(ret == 5); + assert(!memcmp(buf, pipe_msgs[0], 5)); + + assert(_syscall_read(ends[1], buf, 16, 0) < 0); + assert(_syscall_write(ends[0], buf, 16, 0) < 0); + + _syscall_read(ends[0], buf, 16, 0); + assert(ret == 5); + assert(!memcmp(buf, pipe_msgs[1], 5)); + + _syscall_await(); + } + close(ends[0]); + close(ends[1]); + + + /* writing to pipes with one end closed */ + assert(_syscall_pipe(ends, 0) >= 0); + for (int i = 0; i < 16; i++) { + if (!fork()) { + close(ends[1]); + assert(_syscall_read(ends[0], buf, 16, 0) < 0); + _syscall_exit(0); + } + } + close(ends[1]); + for (int i = 0; i < 16; i++) + _syscall_await(); + close(ends[0]); + + assert(_syscall_pipe(ends, 0) >= 0); + for (int i = 0; i < 16; i++) { + if (!fork()) { + close(ends[0]); + assert(_syscall_write(ends[1], buf, 16, 0) < 0); + _syscall_exit(0); + } + } + close(ends[0]); + for (int i = 0; i < 16; i++) + _syscall_await(); + close(ends[1]); + + + /* queueing */ + assert(_syscall_pipe(ends, 0) >= 0); + for (int i = 0; i < 16; i++) { + if (!fork()) { + assert(_syscall_write(ends[1], pipe_msgs[0], 5, -1) == 5); + _syscall_exit(0); + } + } + close(ends[1]); + for (int i = 0; i < 16; i++) { + assert(_syscall_read(ends[0], buf, sizeof buf, 0) == 5); + _syscall_await(); + } + assert(_syscall_read(ends[0], buf, sizeof buf, 0) < 0); + close(ends[0]); + + assert(_syscall_pipe(ends, 0) >= 0); + for (int i = 0; i < 16; i++) { + if (!fork()) { + memset(buf, 0, sizeof buf); + assert(_syscall_read(ends[0], buf, 5, -1) == 5); + assert(!memcmp(buf, pipe_msgs[1], 5)); + _syscall_exit(0); + } + } + close(ends[0]); + for (int i = 0; i < 16; i++) { + assert(_syscall_write(ends[1], pipe_msgs[1], 5, -1) == 5); + _syscall_await(); + } + assert(_syscall_write(ends[1], pipe_msgs[1], 5, -1) < 0); + close(ends[1]); + + + // not a to.do detect when all processes that can read are stuck on writing to the pipe and vice versa + // it seems like linux just lets the process hang endlessly. +} diff --git a/src/user/app/init/tests/semaphore.c b/src/user/app/init/tests/semaphore.c new file mode 100644 index 0000000..0bfa979 --- /dev/null +++ b/src/user/app/init/tests/semaphore.c @@ -0,0 +1,89 @@ +#define TEST_MACROS +#include +#include +#include +#include +#include + +static void odd(handle_t out, struct evil_sem *sem1, struct evil_sem *sem2) { + _syscall_write(out, "1", 1, -1); + esem_signal(sem1); + + esem_wait(sem2); + _syscall_write(out, "3", 1, -1); + esem_signal(sem1); + + esem_wait(sem2); + _syscall_write(out, "5", 1, -1); + esem_signal(sem1); +} + +static void even(handle_t out, struct evil_sem *sem1, struct evil_sem *sem2) { + esem_wait(sem1); + _syscall_write(out, "2", 1, -1); + esem_signal(sem2); + + esem_wait(sem1); + _syscall_write(out, "4", 1, -1); + esem_signal(sem2); + + esem_wait(sem1); + _syscall_write(out, "6", 1, -1); + esem_signal(sem2); +} + +void test_semaphore(void) { + struct evil_sem *sem1, *sem2; + handle_t pipe[2]; + assert(_syscall_pipe(pipe, 0) >= 0); + + if (!fork()) { + sem1 = esem_new(0); + sem2 = esem_new(0); + assert(sem1 && sem2); + if (!fork()) { + odd(pipe[1], sem1, sem2); + _syscall_exit(69); + } else { + even(pipe[1], sem1, sem2); + assert(_syscall_await() == 69); + } + esem_free(sem1); + esem_free(sem2); + + _syscall_write(pipe[1], "|", 1, -1); + + sem1 = esem_new(0); + sem2 = esem_new(0); + assert(sem1 && sem2); + if (!fork()) { + even(pipe[1], sem1, sem2); + _syscall_exit(69); + } else { + odd(pipe[1], sem1, sem2); + assert(_syscall_await() == 69); + _syscall_await(); + } + esem_free(sem1); + esem_free(sem2); + + _syscall_exit(0); + } else { + close(pipe[1]); + + char buf[16]; + size_t pos = 0; + for (;;) { + int ret = _syscall_read(pipe[0], buf + pos, sizeof(buf) - pos, 0); + if (ret < 0) break; + pos += ret; + } + buf[pos] = '\0'; // idc about the "potential" overflow + if (strcmp(buf, "123456|123456")) { + printf("%s\n", buf); + test_fail(); + } + + _syscall_await(); + } +} diff --git a/src/user/app/init/tests/stress.c b/src/user/app/init/tests/stress.c new file mode 100644 index 0000000..9223d01 --- /dev/null +++ b/src/user/app/init/tests/stress.c @@ -0,0 +1,28 @@ +#define TEST_MACROS +#include +#include +#include +#include + +static void run_forked(void (*fn)()) { + if (!fork()) { + fn(); + _syscall_exit(0); + } else { + /* successful tests must return 0 + * TODO add a better fail msg */ + if (_syscall_await() != 0) test_fail(); + } +} + + +static void stress_fork(void) { + for (size_t i = 0; i < 2048; i++) { + if (!fork()) _syscall_exit(0); + _syscall_await(); + } +} + +void stress_all(void) { + run_forked(stress_fork); +} diff --git a/src/user/app/main.c b/src/user/app/main.c deleted file mode 100644 index 9f4d50c..0000000 --- a/src/user/app/main.c +++ /dev/null @@ -1,89 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -__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); -} diff --git a/src/user/app/shell.c b/src/user/app/shell.c deleted file mode 100644 index 9085aed..0000000 --- a/src/user/app/shell.c +++ /dev/null @@ -1,200 +0,0 @@ -#include -#include -#include -#include -#include -#include - -static bool isspace(char c) { - return c == ' ' || c == '\t' || c == '\n'; -} - -static char *strsplit(char *base, char delim) { - if (!base) return NULL; - while (*base) { - if (delim ? *base == delim : isspace(*base)) { - *base++ = '\0'; - return base; - } - base++; - } - return NULL; -} - -static char *strtrim(char *s) { - char *end; - if (!s) return NULL; - while (isspace(*s)) s++; - end = s + strlen(s); - while (end > s && isspace(end[-1])) end--; - *end = '\0'; - return s; -} - - -// TODO fgets -static int readline(char *buf, size_t max) { - char c = '\0'; - size_t pos = 0; - while (pos < (max-1) && c != '\n' && file_read(stdin, &c, 1)) - buf[pos++] = c; - buf[pos++] = '\0'; - return pos; -} - -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) { - 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 - if (buf[len-1] != '/') { - buf[len] = '/'; - buf[len+1] = '\0'; - } - } - - file = file_open(buf, 0); - } else if (ls) { /* ls default argument */ - file = file_open("/", 0); - } else { /* cat default argument */ - file = file_clone(stdin); - } - - 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"); - } - - 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; - } - close(fd); -} - -void shell_loop(void) { - static char buf[256]; - int level = 0; - char *cmd, *args, *redir; - - for (;;) { - printf("%x$ ", level); - - readline(buf, 256); - redir = strtrim(strsplit(buf, '>')); - cmd = strtrim(buf); - args = strtrim(strsplit(cmd, 0)); - - /* "special" commands that can't be handled in a subprocess */ - if (!strcmp(cmd, "shadow")) { - _syscall_mount(-1, args, strlen(args)); - continue; - } else if (!strcmp(cmd, "exit")) { - _syscall_exit(0); - continue; - } else if (!strcmp(cmd, "fork")) { - if (!fork()) level++; - else _syscall_await(); - continue; - } - - if (!fork()) { - if (redir && !file_reopen(stdout, redir, OPEN_CREATE)) { - // TODO stderr - _syscall_exit(0); - } - - if (!strcmp(cmd, "echo")) { - printf("%s\n", args); - } else if (!strcmp(cmd, "exec")) { - libc_file *file = file_open(args, 0); - if (!file) { - printf("couldn't open file\n"); - } else { - elf_execf(file); - file_close(file); - printf("elf_execf failed\n"); - } - } 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, "run_tests")) { - test_all(); - } else if (!strcmp(cmd, "stress")) { - stress_all(); - } else { - printf("unknown command :(\n"); - } - _syscall_exit(0); - } else { - _syscall_await(); - } - } -} diff --git a/src/user/app/shell.h b/src/user/app/shell.h deleted file mode 100644 index fb27410..0000000 --- a/src/user/app/shell.h +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once - -void shell_loop(void); diff --git a/src/user/driver/ansiterm.c b/src/user/driver/ansiterm.c deleted file mode 100644 index 4b9f0d0..0000000 --- a/src/user/driver/ansiterm.c +++ /dev/null @@ -1,99 +0,0 @@ -#include -#include -#include - -struct vga_cell { - unsigned char c; - unsigned char style; -} __attribute__((__packed__)); -static struct vga_cell vga[80 * 25]; - -static handle_t vga_fd; - -static struct {int x, y;} cursor = {0}; -static bool dirty = false; -static bool pendingFlush = false; - -static void flush(void) { - size_t off = 0; - /* we have to do multiple write() calls if we're behind a shitty passthrough fs - * i don't like this either */ - while (off < sizeof(vga)) - off += _syscall_write(vga_fd, (void*)vga + off, sizeof(vga) - off, off); - dirty = false; - pendingFlush = false; -} - -static void scroll(void) { - for (size_t i = 0; i < 80 * 24; i++) - vga[i] = vga[i + 80]; - for (size_t i = 80 * 24; i < 80 * 25; i++) - vga[i].c = ' '; - cursor.y--; - pendingFlush = true; -} - -static void in_char(char c) { - switch (c) { - case '\n': - cursor.x = 0; - cursor.y++; - pendingFlush = true; - break; - case '\b': - if (--cursor.x < 0) cursor.x = 0; - break; - default: - vga[cursor.y * 80 + cursor.x++].c = c; - } - - if (cursor.x >= 80) { - cursor.x = 0; - cursor.y++; - } - while (cursor.y >= 25) scroll(); - dirty = true; -} - -void ansiterm_drv(void) { - vga_fd = _syscall_open("/kdev/vga", 9, 0); - _syscall_read(vga_fd, vga, sizeof vga, 0); - - // find first empty line - for (cursor.y = 0; cursor.y < 25; cursor.y++) { - for (cursor.x = 0; cursor.x < 80; cursor.x++) { - char c = vga[cursor.y * 80 + cursor.x].c; - if (c != ' ' && c != '\0') break; - } - if (cursor.x == 80) break; - } - cursor.x = 0; - - for (int i = 0; i < 80 * 25; i++) - vga[i].style = 0x70; - flush(); - - static char buf[512]; - struct fs_wait_response res; - while (!_syscall_fs_wait(buf, sizeof buf, &res)) { - switch (res.op) { - case VFSOP_OPEN: - // TODO check path - _syscall_fs_respond(NULL, 0, 0); - break; - - case VFSOP_WRITE: - for (size_t i = 0; i < res.len; i++) - in_char(buf[i]); - /* if (pendingFlush) */ flush(); - _syscall_fs_respond(NULL, res.len, 0); - break; - - default: - _syscall_fs_respond(NULL, -1, 0); - break; - } - } - - _syscall_exit(1); -} diff --git a/src/user/driver/driver.h b/src/user/driver/driver.h deleted file mode 100644 index d6e7343..0000000 --- a/src/user/driver/driver.h +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -void ansiterm_drv(void); -void ps2_drv(void); -void tmpfs_drv(void); - -void termcook(void); diff --git a/src/user/driver/ps2.c b/src/user/driver/ps2.c deleted file mode 100644 index fd81882..0000000 --- a/src/user/driver/ps2.c +++ /dev/null @@ -1,86 +0,0 @@ -#include -#include -#include -#include - - -static const char keymap_lower[] = { - '\0', '\0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b', '\t', - 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\r', '\0', 'a', 's', - 'd', 'f', 'g', 'h', 'j', 'k', 'l', '\0', '\'', '`', '\0', '\\', 'z', 'x', 'c', 'v', - 'b', 'n', 'm', ',', '.', '/', '\0', '*', '\0', ' ', '\0', '\0', '\0', '\0', '\0', '\0', - '\0', '\0', '\0', '\0', '\0', '\0', '\0', '7', '8', '9', '-', '4', '5', '6', '+', '1', - '2', '3', '0', '.', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', - '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', - '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', -}; - -static const char keymap_upper[] = { - '\0', '\0', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '\b', '\t', - 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\r', '\0', 'A', 'S', - 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~', '\0', '|', 'Z', 'X', 'C', 'V', - 'B', 'N', 'M', '<', '>', '?', '\0', '*', '\0', ' ', '\0', '\0', '\0', '\0', '\0', '\0', - '\0', '\0', '\0', '\0', '\0', '\0', '\0', '7', '8', '9', '-', '4', '5', '6', '+', '1', - '2', '3', '0', '.', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', - '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', - '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', -}; - - -static volatile uint8_t backlog_buf[16]; -static volatile ring_t backlog = {(void*)backlog_buf, sizeof backlog_buf, 0, 0}; - -static handle_t fd; - -static bool keys[0x80] = {0}; - -static void parse_scancode(uint8_t s) { - bool ctrl = keys[0x1D]; - bool shift = keys[0x2A] || keys[0x36]; - bool down = !(s & 0x80); - char c; - s &= 0x7f; - keys[s] = down; - - c = shift ? keymap_upper[s] : keymap_lower[s]; - if (ctrl && keymap_upper[s] >= 'A' && keymap_upper[s] <= 'Z') - c = keymap_upper[s] - 'A' + 1; - if (down && c) ring_put1b((void*)&backlog, c); -} - -static void main_loop(void) { - static char buf[512]; - struct fs_wait_response res; - int ret; - while (!_syscall_fs_wait(buf, sizeof buf, &res)) { - switch (res.op) { - case VFSOP_OPEN: - _syscall_fs_respond(NULL, 1, 0); - break; - - case VFSOP_READ: - while (ring_size((void*)&backlog) == 0) { - /* read raw input until we have something to output */ - int len = _syscall_read(fd, buf, sizeof buf, 0); - if (len == 0) break; - for (int i = 0; i < len; i++) - parse_scancode(buf[i]); - } - ret = ring_get((void*)&backlog, buf, res.capacity); - _syscall_fs_respond(buf, ret, 0); - break; - - default: - _syscall_fs_respond(NULL, -1, 0); - break; - } - } -} - -void ps2_drv(void) { - fd = _syscall_open("/kdev/ps2", 9, 0); - if (fd < 0) _syscall_exit(1); - - main_loop(); - _syscall_exit(0); -} diff --git a/src/user/driver/termcook.c b/src/user/driver/termcook.c deleted file mode 100644 index 56ac77c..0000000 --- a/src/user/driver/termcook.c +++ /dev/null @@ -1,71 +0,0 @@ -#include -#include -#include - -static void w_output(handle_t output, const char *buf, size_t len) { - size_t pos = 0; - while (pos < len) { - int ret = _syscall_write(output, buf + pos, len - pos, pos); - if (ret < 0) break; - pos += ret; - } -} - -static void line_editor(handle_t input, handle_t output) { - char readbuf[16], linebuf[256]; - size_t linepos = 0; - for (;;) { - int readlen = _syscall_read(input, readbuf, sizeof readbuf, -1); - if (readlen < 0) return; - for (int i = 0; i < readlen; i++) { - char c = readbuf[i]; - switch (c) { - case '\b': - case 0x7f: - if (linepos != 0) { - printf("\b \b"); - linepos--; - } - break; - case 4: /* EOT, C-d */ - if (linepos > 0) { - w_output(output, linebuf, linepos); - linepos = 0; - } else { - _syscall_write(output, NULL, 0, 0); // eof - } - break; - case '\n': - case '\r': - printf("\n"); - if (linepos < sizeof linebuf) - linebuf[linepos++] = '\n'; - w_output(output, linebuf, linepos); - linepos = 0; - break; - default: - if (linepos < sizeof linebuf) { - linebuf[linepos++] = c; - printf("%c", c); - } - break; - } - } - } -} - -void termcook(void) { - handle_t stdin_pipe[2] = {-1, -1}; - if (_syscall_pipe(stdin_pipe, 0) < 0) - return; - - if (!fork()) { - close(stdin_pipe[0]); - line_editor(0, stdin_pipe[1]); - _syscall_exit(0); - } - /* 0 is stdin, like in unix */ - _syscall_dup(stdin_pipe[0], 0, 0); - close(stdin_pipe[0]); - close(stdin_pipe[1]); -} diff --git a/src/user/driver/tmpfs.c b/src/user/driver/tmpfs.c deleted file mode 100644 index 6e5b45e..0000000 --- a/src/user/driver/tmpfs.c +++ /dev/null @@ -1,132 +0,0 @@ -#include -#include -#include -#include - -struct node { - const char *name; - size_t namelen; - struct node *next; - char *buf; - size_t size, capacity; -}; - -struct node *root = NULL; -static struct node special_root; - -static struct node *lookup(const char *path, size_t len) { - for (struct node *iter = root; iter; iter = iter->next) { - if (iter->namelen == len && !memcmp(path, iter->name, len)) - return iter; - } - return NULL; -} - -static struct node *tmpfs_open(const char *path, struct fs_wait_response *res) { - struct node *node; - if (res->len == 0) return NULL; - path++; - res->len--; - - if (res->len == 0) return &special_root; - - // no directory support (yet) - if (memchr(path, '/', res->len)) return NULL; - - node = lookup(path, res->len); - if (!node && (res->flags & OPEN_CREATE)) { - node = malloc(sizeof *node); - memset(node, 0, sizeof *node); - - char *namebuf = malloc(res->len); - memcpy(namebuf, path, res->len); - node->name = namebuf; - node->namelen = res->len; - node->next = root; - root = node; - } - return node; -} - -void tmpfs_drv(void) { - // TODO replace all the static allocations in drivers with mallocs - static char buf[512]; - struct fs_wait_response res; - struct node *ptr; - while (!_syscall_fs_wait(buf, sizeof buf, &res)) { - switch (res.op) { - case VFSOP_OPEN: - ptr = tmpfs_open(buf, &res); - _syscall_fs_respond(ptr, ptr ? 0 : -1, 0); - break; - - case VFSOP_READ: - ptr = (void*)res.id; - if (ptr == &special_root) { - size_t buf_pos = 0; - size_t to_skip = res.offset; - - for (struct node *iter = root; iter; iter = iter->next) { - if (iter->namelen <= to_skip) { - to_skip -= iter->namelen; - continue; - } - - if (iter->namelen + buf_pos - to_skip >= sizeof(buf)) { - memcpy(buf + buf_pos, iter->name + to_skip, sizeof(buf) - buf_pos - to_skip); - buf_pos = sizeof(buf); - break; - } - memcpy(buf + buf_pos, iter->name + to_skip, iter->namelen - to_skip); - buf_pos += iter->namelen - to_skip; - buf[buf_pos++] = '\0'; - to_skip = 0; - } - _syscall_fs_respond(buf, buf_pos, 0); - } else { - // TODO offset - if (res.offset) - _syscall_fs_respond(NULL, 0, 0); - else - _syscall_fs_respond(ptr->buf, ptr->size, 0); - break; - } - break; - - case VFSOP_WRITE: - ptr = (void*)res.id; - if (ptr == &special_root) { - _syscall_fs_respond(NULL, -1, 0); - break; - } - if (res.len == 0) { - _syscall_fs_respond(NULL, 0, 0); - break; - } - if (!ptr->buf) { - ptr->buf = malloc(256); - if (!ptr->buf) { - _syscall_fs_respond(NULL, -1, 0); - break; - } - memset(ptr->buf, 0, 256); - ptr->capacity = 256; - } - - size_t len = res.len; - if (len > ptr->capacity - res.offset) - len = ptr->capacity - res.offset; - memcpy(ptr->buf + res.offset, buf, len); - if (ptr->size < res.offset + len) - ptr->size = res.offset + len; - _syscall_fs_respond(NULL, len, 0); - break; - - default: - _syscall_fs_respond(NULL, -1, 0); - break; - } - } - - _syscall_exit(1); -} diff --git a/src/user/fs/misc.c b/src/user/fs/misc.c deleted file mode 100644 index fa1115d..0000000 --- a/src/user/fs/misc.c +++ /dev/null @@ -1,163 +0,0 @@ -#include -#include -#include -#include -#include -#include - -bool fork2_n_mount(const char *path) { - handle_t h; - if (_syscall_fork(FORK_NEWFS, &h) > 0) { /* parent */ - _syscall_mount(h, path, strlen(path)); - close(h); - return true; - } - return false; -} - -void fs_passthru(const char *prefix) { - struct fs_wait_response res; - const size_t buf_len = 1024; - char *buf = malloc(buf_len); - int prefix_len = prefix ? strlen(prefix) : 0; - if (!buf) _syscall_exit(1); - - while (!_syscall_fs_wait(buf, buf_len, &res)) { - switch (res.op) { - case VFSOP_OPEN: - if (prefix) { - if (prefix_len + res.len > buf_len) { - _syscall_fs_respond(NULL, -1, 0); - break; - } - - // TODO memmove - char tmp[64]; - memcpy(tmp, buf, res.len); - memcpy(buf + prefix_len, tmp, res.len); - memcpy(buf, prefix, prefix_len); - res.len += prefix_len; - } - _syscall_fs_respond(NULL, _syscall_open(buf, res.len, res.flags), FSR_DELEGATE); - break; - - default: - _syscall_fs_respond(NULL, -1, 0); - break; - } - } - _syscall_exit(0); -} - -void fs_whitelist(const char **list) { - struct fs_wait_response res; - const size_t buf_len = 1024; - char *buf = malloc(buf_len); - bool allow; - if (!buf) _syscall_exit(1); - - while (!_syscall_fs_wait(buf, buf_len, &res)) { - switch (res.op) { - case VFSOP_OPEN: - allow = false; - // TODO reverse dir_inject - for (const char **iter = list; *iter; iter++) { - size_t len = strlen(*iter); // inefficient, whatever - if (len <= res.len && !memcmp(buf, *iter, len)) { - allow = true; - break; - } - } - _syscall_fs_respond(NULL, allow ? _syscall_open(buf, res.len, res.flags) : -1, FSR_DELEGATE); - break; - - default: - _syscall_fs_respond(NULL, -1, 0); - break; - } - } - _syscall_exit(0); -} - - -void fs_dir_inject(const char *path) { - struct fs_dir_handle { - const char *inject; - int delegate, inject_len; - }; - - const size_t path_len = strlen(path); - struct fs_wait_response res; - struct fs_dir_handle *data; - const size_t buf_len = 1024; - char *buf = malloc(buf_len); - int ret, inject_len; - - if (!buf) _syscall_exit(1); - - while (!_syscall_fs_wait(buf, buf_len, &res)) { - data = res.id; - switch (res.op) { - case VFSOP_OPEN: - if (buf[res.len - 1] == '/' && - res.len < path_len && !memcmp(path, buf, res.len)) - { - /* opening a directory that we're injecting into */ - - data = malloc(sizeof *data); - data->delegate = _syscall_open(buf, res.len, res.flags); - data->inject = path + res.len; - - /* inject up to the next slash */ - inject_len = 0; - while (data->inject[inject_len] && data->inject[inject_len] != '/') - inject_len++; - if (data->inject[inject_len] == '/') - inject_len++; - data->inject_len = inject_len; - - _syscall_fs_respond(data, 0, 0); - } else { - _syscall_fs_respond(NULL, _syscall_open(buf, res.len, res.flags), FSR_DELEGATE); - } - break; - - case VFSOP_CLOSE: - if (data->delegate >= 0) - close(data->delegate); - _syscall_fs_respond(NULL, 0, 0); - break; - - case VFSOP_READ: - if (res.offset > 0) _syscall_fs_respond(NULL, 0, 0); // TODO working offsets - - int out_len = data->inject_len; - memcpy(buf, data->inject, out_len); - buf[out_len++] = '\0'; - - if (data->delegate >= 0) { - int to_read = res.capacity < buf_len ? res.capacity : buf_len; - to_read -= out_len; - ret = _syscall_read(data->delegate, buf + out_len, to_read, 0); - if (ret > 0) out_len += ret; - // TODO deduplicate entries - } - - _syscall_fs_respond(buf, out_len, 0); - break; - - case VFSOP_WRITE: - if (data->delegate >= 0) - ret = _syscall_write(data->delegate, buf, res.len, res.offset); - else - ret = -1; - _syscall_fs_respond(NULL, ret, 0); - break; - - default: - _syscall_fs_respond(NULL, -1, 0); - break; - } - } - _syscall_exit(0); -} diff --git a/src/user/fs/misc.h b/src/user/fs/misc.h deleted file mode 100644 index 3a8b071..0000000 --- a/src/user/fs/misc.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once -#include -#include - -bool fork2_n_mount(const char *path); - -void fs_passthru(const char *prefix); -void fs_whitelist(const char **list); - -void fs_dir_inject(const char *path); - -/** Mounts something and injects its path into the fs */ -// TODO path needs to have a trailing slash -#define MOUNT(path, impl) \ - if (!fork2_n_mount(path)) {_klogf("impl %s", path); impl;} \ - if (!fork2_n_mount("/")) {_klogf("dir for %s", path); fs_dir_inject(path);} diff --git a/src/user/fs/tar.c b/src/user/fs/tar.c deleted file mode 100644 index 60deccd..0000000 --- a/src/user/fs/tar.c +++ /dev/null @@ -1,158 +0,0 @@ -#include -#include -#include -#include - -#define BUF_SIZE 64 - -static void *tar_open(const char *path, int len, void *base, size_t base_len); -static void tar_read(struct fs_wait_response *res, void *base, size_t base_len); -static int tar_size(void *sector); -static void *tar_find(const char *path, size_t path_len, void *base, size_t base_len); -static int oct_parse(char *str, size_t len); - - -static const char *root_fakemeta = ""; /* see comment in tar_open */ - - -void tar_driver(void *base) { - static char buf[BUF_SIZE]; - struct fs_wait_response res; - void *ptr; - while (!_syscall_fs_wait(buf, BUF_SIZE, &res)) { - switch (res.op) { - case VFSOP_OPEN: - ptr = tar_open(buf, res.len, base, ~0); - _syscall_fs_respond(ptr, ptr ? 0 : -1, 0); - break; - - case VFSOP_READ: - tar_read(&res, base, ~0); - break; - - default: - _syscall_fs_respond(NULL, -1, 0); // unsupported - break; - } - } - _syscall_exit(0); -} - -static void *tar_open(const char *path, int len, void *base, size_t base_len) { - if (len <= 0) return NULL; - path += 1; // skip the leading slash - len -= 1; - - /* TAR archives don't (seem to) contain an entry for the root dir, so i'm - * returning a fake one. this isn't a full entry because i'm currently too - * lazy to create a full one - thus, it has to be special cased in tar_read */ - if (len == 0) - return (void*)root_fakemeta; - - return tar_find(path, len, base, base_len); -} - -static void tar_read(struct fs_wait_response *res, void *base, size_t base_len) { - void *meta = (void*)res->id; - char type = *(char*)(meta + 156); - size_t meta_len; - int size; - - static char buf[BUF_SIZE]; // TODO reuse a single buffer - size_t buf_pos = 0; - - if (meta == root_fakemeta) type = '5'; /* see comment in tar_open() */ - - switch (type) { - case '\0': - case '0': /* normal files */ - size = tar_size(meta); - if (res->offset < 0 || res->offset > size) { - // TODO support negative offsets - _syscall_fs_respond(NULL, -1, 0); - } else { - _syscall_fs_respond(meta + 512 + res->offset, size - res->offset, 0); - } - break; - - case '5': /* directory */ - meta_len = strlen(meta); - size_t to_skip = res->offset; - - /* find files in dir */ - for (size_t off = 0; off < base_len;) { - if (0 != memcmp(base + off + 257, "ustar", 5)) - break; // not a metadata sector - // TODO more meaningful variable names and clean code up - - /* check if prefix matches */ - if (0 == memcmp(base + off, meta, meta_len) && - *(char*)(base + off + meta_len) != '\0') { - char *suffix = base + off + meta_len; - size_t suffix_len = strlen(suffix); - - /* check if the path contains any non-trailing slashes */ - char *next = suffix; - while (*next && *next != '/') next++; - if (*next == '/') next++; - if (*next == '\0') { - if (to_skip > suffix_len) { - to_skip -= suffix_len; - } else { - suffix += to_skip; - suffix_len -= to_skip; - to_skip = 0; - - /* it doesn't - so let's add it to the result */ - memcpy(buf + buf_pos, suffix, suffix_len); - buf[buf_pos + suffix_len] = '\0'; - buf_pos += suffix_len + 1; - // TODO no buffer overrun check - } - } - } - - size = tar_size(base + off); - off += 512; // skip this metadata sector - off += (size + 511) & ~511; // skip the data sectors - } - - _syscall_fs_respond(buf, buf_pos, 0); - break; - - default: - _syscall_fs_respond(NULL, -1, 0); - break; - } -} - -static int tar_size(void *sector) { - return oct_parse(sector + 124, 11); -} - -static void *tar_find(const char *path, size_t path_len, void *base, size_t base_len) { - int size; - if (path_len > 100) return NULL; // illegal path - - for (size_t off = 0; off < base_len;) { - if (0 != memcmp(base + off + 257, "ustar", 5)) - break; // not a metadata sector - if (0 == memcmp(base + off, path, path_len) && - *(char*)(base + off + path_len) == '\0') - return base + off; // file found, quit - - size = tar_size(base + off); - off += 512; // skip this metadata sector - off += (size + 511) & ~511; // skip the data sectors - } - return NULL; -} - -static int oct_parse(char *str, size_t len) { - int res = 0; - for (size_t i = 0; i < len; i++) { - res *= 8; - res += str[i] - '0'; // no format checking - } - return res; -} diff --git a/src/user/fs/tar.h b/src/user/fs/tar.h deleted file mode 100644 index c1dee78..0000000 --- a/src/user/fs/tar.h +++ /dev/null @@ -1,4 +0,0 @@ -#pragma once -#include - -_Noreturn void tar_driver(void *base); diff --git a/src/user/lib/fs/misc.c b/src/user/lib/fs/misc.c new file mode 100644 index 0000000..9522cfa --- /dev/null +++ b/src/user/lib/fs/misc.c @@ -0,0 +1,163 @@ +#include +#include +#include +#include +#include +#include + +bool fork2_n_mount(const char *path) { + handle_t h; + if (_syscall_fork(FORK_NEWFS, &h) > 0) { /* parent */ + _syscall_mount(h, path, strlen(path)); + close(h); + return true; + } + return false; +} + +void fs_passthru(const char *prefix) { + struct fs_wait_response res; + const size_t buf_len = 1024; + char *buf = malloc(buf_len); + int prefix_len = prefix ? strlen(prefix) : 0; + if (!buf) _syscall_exit(1); + + while (!_syscall_fs_wait(buf, buf_len, &res)) { + switch (res.op) { + case VFSOP_OPEN: + if (prefix) { + if (prefix_len + res.len > buf_len) { + _syscall_fs_respond(NULL, -1, 0); + break; + } + + // TODO memmove + char tmp[64]; + memcpy(tmp, buf, res.len); + memcpy(buf + prefix_len, tmp, res.len); + memcpy(buf, prefix, prefix_len); + res.len += prefix_len; + } + _syscall_fs_respond(NULL, _syscall_open(buf, res.len, res.flags), FSR_DELEGATE); + break; + + default: + _syscall_fs_respond(NULL, -1, 0); + break; + } + } + _syscall_exit(0); +} + +void fs_whitelist(const char **list) { + struct fs_wait_response res; + const size_t buf_len = 1024; + char *buf = malloc(buf_len); + bool allow; + if (!buf) _syscall_exit(1); + + while (!_syscall_fs_wait(buf, buf_len, &res)) { + switch (res.op) { + case VFSOP_OPEN: + allow = false; + // TODO reverse dir_inject + for (const char **iter = list; *iter; iter++) { + size_t len = strlen(*iter); // inefficient, whatever + if (len <= res.len && !memcmp(buf, *iter, len)) { + allow = true; + break; + } + } + _syscall_fs_respond(NULL, allow ? _syscall_open(buf, res.len, res.flags) : -1, FSR_DELEGATE); + break; + + default: + _syscall_fs_respond(NULL, -1, 0); + break; + } + } + _syscall_exit(0); +} + + +void fs_dir_inject(const char *path) { + struct fs_dir_handle { + const char *inject; + int delegate, inject_len; + }; + + const size_t path_len = strlen(path); + struct fs_wait_response res; + struct fs_dir_handle *data; + const size_t buf_len = 1024; + char *buf = malloc(buf_len); + int ret, inject_len; + + if (!buf) _syscall_exit(1); + + while (!_syscall_fs_wait(buf, buf_len, &res)) { + data = res.id; + switch (res.op) { + case VFSOP_OPEN: + if (buf[res.len - 1] == '/' && + res.len < path_len && !memcmp(path, buf, res.len)) + { + /* opening a directory that we're injecting into */ + + data = malloc(sizeof *data); + data->delegate = _syscall_open(buf, res.len, res.flags); + data->inject = path + res.len; + + /* inject up to the next slash */ + inject_len = 0; + while (data->inject[inject_len] && data->inject[inject_len] != '/') + inject_len++; + if (data->inject[inject_len] == '/') + inject_len++; + data->inject_len = inject_len; + + _syscall_fs_respond(data, 0, 0); + } else { + _syscall_fs_respond(NULL, _syscall_open(buf, res.len, res.flags), FSR_DELEGATE); + } + break; + + case VFSOP_CLOSE: + if (data->delegate >= 0) + close(data->delegate); + _syscall_fs_respond(NULL, 0, 0); + break; + + case VFSOP_READ: + if (res.offset > 0) _syscall_fs_respond(NULL, 0, 0); // TODO working offsets + + int out_len = data->inject_len; + memcpy(buf, data->inject, out_len); + buf[out_len++] = '\0'; + + if (data->delegate >= 0) { + int to_read = res.capacity < buf_len ? res.capacity : buf_len; + to_read -= out_len; + ret = _syscall_read(data->delegate, buf + out_len, to_read, 0); + if (ret > 0) out_len += ret; + // TODO deduplicate entries + } + + _syscall_fs_respond(buf, out_len, 0); + break; + + case VFSOP_WRITE: + if (data->delegate >= 0) + ret = _syscall_write(data->delegate, buf, res.len, res.offset); + else + ret = -1; + _syscall_fs_respond(NULL, ret, 0); + break; + + default: + _syscall_fs_respond(NULL, -1, 0); + break; + } + } + _syscall_exit(0); +} diff --git a/src/user/lib/fs/misc.h b/src/user/lib/fs/misc.h new file mode 100644 index 0000000..3a8b071 --- /dev/null +++ b/src/user/lib/fs/misc.h @@ -0,0 +1,16 @@ +#pragma once +#include +#include + +bool fork2_n_mount(const char *path); + +void fs_passthru(const char *prefix); +void fs_whitelist(const char **list); + +void fs_dir_inject(const char *path); + +/** Mounts something and injects its path into the fs */ +// TODO path needs to have a trailing slash +#define MOUNT(path, impl) \ + if (!fork2_n_mount(path)) {_klogf("impl %s", path); impl;} \ + if (!fork2_n_mount("/")) {_klogf("dir for %s", path); fs_dir_inject(path);} diff --git a/src/user/tests/main.c b/src/user/tests/main.c deleted file mode 100644 index 97714e7..0000000 --- a/src/user/tests/main.c +++ /dev/null @@ -1,256 +0,0 @@ -#define TEST_MACROS -#include -#include -#include -#include -#include -#include - -static void run_forked(void (*fn)()) { - if (!fork()) { - fn(); - _syscall_exit(0); - } else { - /* successful tests must return 0 - * TODO add a better fail msg */ - if (_syscall_await() != 0) test_fail(); - } -} - - -const char *tmpfilepath = "/tmp/.test_internal"; - -static void test_await(void) { - /* creates 16 child processes, each returning a different value. then checks - * if await() returns every value exactly once */ - int ret; - int counts[16] = {0}; - - for (int i = 0; i < 16; i++) - if (!fork()) - _syscall_exit(i); - - while ((ret = _syscall_await()) != ~0) { - assert(0 <= ret && ret < 16); - counts[ret]++; - } - - for (int i = 0; i < 16; i++) - assert(counts[i] == 1); -} - -static void test_faults(void) { - /* tests what happens when child processes fault. - * expected behavior: parent processes still manages to finish, and it can - * reap all its children */ - int await_cnt = 0; - - if (!fork()) { // invalid memory access - asm volatile("movb $69, 0" ::: "memory"); - printf("this shouldn't happen"); - _syscall_exit(-1); - } - if (!fork()) { // #GP - asm volatile("hlt" ::: "memory"); - printf("this shouldn't happen"); - _syscall_exit(-1); - } - - while (_syscall_await() != ~0) await_cnt++; - assert(await_cnt == 2); -} - -static void test_interrupted_fs(void) { - handle_t h; - if (_syscall_fork(FORK_NEWFS, &h)) { /* child */ - // TODO make a similar test with all 0s passed to fs_wait - struct fs_wait_response res; - _syscall_fs_wait(NULL, 0, &res); - _syscall_exit(0); - } else { /* parent */ - _syscall_mount(h, "/", 1); - int ret = _syscall_open("/", 1, 0); - // the handler quits while handling that call - but this syscall should return anyways - _syscall_exit(ret < 0 ? 0 : -1); - } -} - -static void test_orphaned_fs(void) { - handle_t h; - if (_syscall_fork(FORK_NEWFS, &h)) { /* child */ - _syscall_exit(0); - } else { /* parent */ - _syscall_mount(h, "/", 1); - int ret = _syscall_open("/", 1, 0); - // no handler will ever be available to handle this call - the syscall should instantly return - _syscall_exit(ret < 0 ? 0 : -1); - } -} - -static void test_memflag(void) { - void *page = (void*)0x77777000; - _syscall_memflag(page, 4096, MEMFLAG_PRESENT); // allocate page - memset(page, 77, 4096); // write to it - _syscall_memflag(page, 4096, 0); // free it - - if (!fork()) { - memset(page, 11, 4096); // should segfault - _syscall_exit(0); - } else { - assert(_syscall_await() != 0); // test if the process crashed - } - - char *pages[4]; - for (size_t i = 0; i < 4; i++) { - pages[i] = _syscall_memflag(NULL, 4096, MEMFLAG_FINDFREE); - printf("[test_memflag] findfree: 0x%x\n", pages[i]); - - assert(pages[i] == _syscall_memflag(NULL, 4096, MEMFLAG_FINDFREE | MEMFLAG_PRESENT)); - if (i > 0) assert(pages[i] != pages[i-1]); - *pages[i] = 0x77; - } - - for (size_t i = 0; i < 4; i++) - _syscall_memflag(pages, 4096, MEMFLAG_PRESENT); - - // TODO check if reclaims -} - -static void test_dup(void) { - handle_t pipe[2]; - handle_t h1, h2; - assert(_syscall_pipe(pipe, 0) >= 0); - - if (!fork()) { - close(pipe[0]); - - h1 = _syscall_dup(pipe[1], -1, 0); - assert(h1 >= 0); - assert(h1 != pipe[1]); - h2 = _syscall_dup(h1, -1, 0); - assert(h2 >= 0); - assert(h2 != pipe[1] && h2 != h1); - - _syscall_write(pipe[1], "og", 2, 0); - _syscall_write(h1, "h1", 2, 0); - _syscall_write(h2, "h2", 2, 0); - - close(pipe[1]); - _syscall_write(h1, "h1", 2, 0); - _syscall_write(h2, "h2", 2, 0); - - assert(_syscall_dup(h1, pipe[1], 0) == pipe[1]); - assert(_syscall_dup(h2, pipe[1], 0) == pipe[1]); - assert(_syscall_dup(h1, pipe[1], 0) == pipe[1]); - assert(_syscall_dup(h2, pipe[1], 0) == pipe[1]); - close(h1); - close(h2); - - assert(_syscall_dup(pipe[1], h2, 0) == h2); - _syscall_write(h2, "h2", 2, 0); - close(h2); - - assert(_syscall_dup(pipe[1], h1, 0) == h1); - _syscall_write(h1, "h1", 2, 0); - close(h1); - - _syscall_exit(0); - } else { - char buf[16]; - size_t count = 0; - close(pipe[1]); - while (_syscall_read(pipe[0], buf, sizeof buf, 0) >= 0) - count++; - assert(count == 7); - _syscall_await(); - } - - - close(pipe[0]); -} - -static void test_malloc(void) { - // not really a test - void *p1, *p2; - - p1 = malloc(420); - printf("p1 = 0x%x\n", p1); - - p2 = malloc(1024); - printf("p2 = 0x%x\n", p2); - free(p2); - p2 = malloc(256); - printf("p2 = 0x%x\n", p2); - free(p2); - p2 = malloc(4096); - printf("p2 = 0x%x\n", p2); - free(p2); - - free(p1); -} - -static void test_efault(void) { - const char *str = "o, 16 characters"; - char str2[16]; - char *invalid = (void*)0x1000; - handle_t h; - - memcpy(str2, str, 16); - - assert((h = _syscall_open(tmpfilepath, strlen(tmpfilepath), OPEN_CREATE))); - assert(_syscall_write(h, str, 16, 0) == 16); - assert(_syscall_write(h, str2, 16, 0) == 16); - - assert(_syscall_write(h, invalid, 16, 0) == -EFAULT); - - /* x64 canonical pointers */ - assert(_syscall_write(h, (void*)((uintptr_t)str ^ 0x8000000000000000), 16, 0) == -EFAULT); - assert(_syscall_write(h, (void*)((uintptr_t)str ^ 0x1000000000000000), 16, 0) == -EFAULT); - assert(_syscall_write(h, (void*)((uintptr_t)str ^ 0x0100000000000000), 16, 0) == -EFAULT); - assert(_syscall_write(h, (void*)((uintptr_t)str ^ 0x0010000000000000), 16, 0) == -EFAULT); - assert(_syscall_write(h, (void*)((uintptr_t)str ^ 0x0001000000000000), 16, 0) == -EFAULT); - assert(_syscall_write(h, (void*)((uintptr_t)str ^ 0x0000800000000000), 16, 0) == -EFAULT); - - assert(_syscall_write(h, (void*)((uintptr_t)str2 ^ 0x8000000000000000), 16, 0) == -EFAULT); - assert(_syscall_write(h, (void*)((uintptr_t)str2 ^ 0x1000000000000000), 16, 0) == -EFAULT); - assert(_syscall_write(h, (void*)((uintptr_t)str2 ^ 0x0100000000000000), 16, 0) == -EFAULT); - assert(_syscall_write(h, (void*)((uintptr_t)str2 ^ 0x0010000000000000), 16, 0) == -EFAULT); - assert(_syscall_write(h, (void*)((uintptr_t)str2 ^ 0x0001000000000000), 16, 0) == -EFAULT); - assert(_syscall_write(h, (void*)((uintptr_t)str2 ^ 0x0000800000000000), 16, 0) == -EFAULT); - - assert(_syscall_write(h, str, 16, 0) == 16); - close(h); -} - -static void test_execbuf(void) { - // not really a test, TODO - const char str1[] = "test_execbuf: string 1\n"; - const char str2[] = "test_execbuf: and 2\n"; - uint64_t buf[] = { - EXECBUF_SYSCALL, _SYSCALL_WRITE, 1, (uintptr_t)str1, sizeof(str1) - 1, -1, - EXECBUF_SYSCALL, _SYSCALL_WRITE, 1, (uintptr_t)str2, sizeof(str2) - 1, -1, - EXECBUF_SYSCALL, _SYSCALL_EXIT, 0, 0, 0, 0, - }; - _syscall_execbuf(buf, sizeof buf); - test_fail(); -} - -static void test_misc(void) { - assert(_syscall(~0, 0, 0, 0, 0) < 0); /* try making an invalid syscall */ -} - - -void test_all(void) { - run_forked(test_await); - run_forked(test_faults); - run_forked(test_interrupted_fs); - run_forked(test_orphaned_fs); - run_forked(test_memflag); - run_forked(test_dup); - run_forked(test_malloc); - run_forked(test_pipe); - run_forked(test_semaphore); - run_forked(test_efault); - run_forked(test_execbuf); - run_forked(test_misc); -} diff --git a/src/user/tests/main.h b/src/user/tests/main.h deleted file mode 100644 index ed11c5e..0000000 --- a/src/user/tests/main.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - -void stress_all(void); -void test_all(void); - -void test_pipe(void); -void test_semaphore(void); - -#ifdef TEST_MACROS - -#define argify(str) str, sizeof(str) - 1 -#define test_fail() do { \ - printf("\033[31m" "TEST FAILED: %s:%xh\n" "\033[0m", __func__, __LINE__); \ - _syscall_exit(0); \ -} while (0) -#define assert(cond) if (!(cond)) test_fail(); - -#endif diff --git a/src/user/tests/pipe.c b/src/user/tests/pipe.c deleted file mode 100644 index 2e964f9..0000000 --- a/src/user/tests/pipe.c +++ /dev/null @@ -1,116 +0,0 @@ -#define TEST_MACROS -#include -#include -#include -#include - -static const char *pipe_msgs[2] = {"hello", "world"}; - -void test_pipe(void) { - handle_t ends[2]; - char buf[16]; - int ret; - - /* test regular reads / writes */ - assert(_syscall_pipe(ends, 0) >= 0); - if (!fork()) { - /* those repeated asserts ensure that you can't read/write to the wrong ends */ - assert(_syscall_read(ends[1], buf, 16, 0) < 0); - assert(_syscall_write(ends[0], buf, 16, 0) < 0); - - ret = _syscall_write(ends[1], pipe_msgs[0], 5, -1); - assert(ret == 5); - - assert(_syscall_read(ends[1], buf, 16, 0) < 0); - assert(_syscall_write(ends[0], buf, 16, 0) < 0); - - ret = _syscall_write(ends[1], pipe_msgs[1], 5, -1); - assert(ret == 5); - - _syscall_exit(0); - } else { - assert(_syscall_read(ends[1], buf, 16, 0) < 0); - assert(_syscall_write(ends[0], buf, 16, 0) < 0); - - ret = _syscall_read(ends[0], buf, 16, 0); - assert(ret == 5); - assert(!memcmp(buf, pipe_msgs[0], 5)); - - assert(_syscall_read(ends[1], buf, 16, 0) < 0); - assert(_syscall_write(ends[0], buf, 16, 0) < 0); - - _syscall_read(ends[0], buf, 16, 0); - assert(ret == 5); - assert(!memcmp(buf, pipe_msgs[1], 5)); - - _syscall_await(); - } - close(ends[0]); - close(ends[1]); - - - /* writing to pipes with one end closed */ - assert(_syscall_pipe(ends, 0) >= 0); - for (int i = 0; i < 16; i++) { - if (!fork()) { - close(ends[1]); - assert(_syscall_read(ends[0], buf, 16, 0) < 0); - _syscall_exit(0); - } - } - close(ends[1]); - for (int i = 0; i < 16; i++) - _syscall_await(); - close(ends[0]); - - assert(_syscall_pipe(ends, 0) >= 0); - for (int i = 0; i < 16; i++) { - if (!fork()) { - close(ends[0]); - assert(_syscall_write(ends[1], buf, 16, 0) < 0); - _syscall_exit(0); - } - } - close(ends[0]); - for (int i = 0; i < 16; i++) - _syscall_await(); - close(ends[1]); - - - /* queueing */ - assert(_syscall_pipe(ends, 0) >= 0); - for (int i = 0; i < 16; i++) { - if (!fork()) { - assert(_syscall_write(ends[1], pipe_msgs[0], 5, -1) == 5); - _syscall_exit(0); - } - } - close(ends[1]); - for (int i = 0; i < 16; i++) { - assert(_syscall_read(ends[0], buf, sizeof buf, 0) == 5); - _syscall_await(); - } - assert(_syscall_read(ends[0], buf, sizeof buf, 0) < 0); - close(ends[0]); - - assert(_syscall_pipe(ends, 0) >= 0); - for (int i = 0; i < 16; i++) { - if (!fork()) { - memset(buf, 0, sizeof buf); - assert(_syscall_read(ends[0], buf, 5, -1) == 5); - assert(!memcmp(buf, pipe_msgs[1], 5)); - _syscall_exit(0); - } - } - close(ends[0]); - for (int i = 0; i < 16; i++) { - assert(_syscall_write(ends[1], pipe_msgs[1], 5, -1) == 5); - _syscall_await(); - } - assert(_syscall_write(ends[1], pipe_msgs[1], 5, -1) < 0); - close(ends[1]); - - - // not a to.do detect when all processes that can read are stuck on writing to the pipe and vice versa - // it seems like linux just lets the process hang endlessly. -} diff --git a/src/user/tests/semaphore.c b/src/user/tests/semaphore.c deleted file mode 100644 index 836edf5..0000000 --- a/src/user/tests/semaphore.c +++ /dev/null @@ -1,89 +0,0 @@ -#define TEST_MACROS -#include -#include -#include -#include -#include - -static void odd(handle_t out, struct evil_sem *sem1, struct evil_sem *sem2) { - _syscall_write(out, "1", 1, -1); - esem_signal(sem1); - - esem_wait(sem2); - _syscall_write(out, "3", 1, -1); - esem_signal(sem1); - - esem_wait(sem2); - _syscall_write(out, "5", 1, -1); - esem_signal(sem1); -} - -static void even(handle_t out, struct evil_sem *sem1, struct evil_sem *sem2) { - esem_wait(sem1); - _syscall_write(out, "2", 1, -1); - esem_signal(sem2); - - esem_wait(sem1); - _syscall_write(out, "4", 1, -1); - esem_signal(sem2); - - esem_wait(sem1); - _syscall_write(out, "6", 1, -1); - esem_signal(sem2); -} - -void test_semaphore(void) { - struct evil_sem *sem1, *sem2; - handle_t pipe[2]; - assert(_syscall_pipe(pipe, 0) >= 0); - - if (!fork()) { - sem1 = esem_new(0); - sem2 = esem_new(0); - assert(sem1 && sem2); - if (!fork()) { - odd(pipe[1], sem1, sem2); - _syscall_exit(69); - } else { - even(pipe[1], sem1, sem2); - assert(_syscall_await() == 69); - } - esem_free(sem1); - esem_free(sem2); - - _syscall_write(pipe[1], "|", 1, -1); - - sem1 = esem_new(0); - sem2 = esem_new(0); - assert(sem1 && sem2); - if (!fork()) { - even(pipe[1], sem1, sem2); - _syscall_exit(69); - } else { - odd(pipe[1], sem1, sem2); - assert(_syscall_await() == 69); - _syscall_await(); - } - esem_free(sem1); - esem_free(sem2); - - _syscall_exit(0); - } else { - close(pipe[1]); - - char buf[16]; - size_t pos = 0; - for (;;) { - int ret = _syscall_read(pipe[0], buf + pos, sizeof(buf) - pos, 0); - if (ret < 0) break; - pos += ret; - } - buf[pos] = '\0'; // idc about the "potential" overflow - if (strcmp(buf, "123456|123456")) { - printf("%s\n", buf); - test_fail(); - } - - _syscall_await(); - } -} diff --git a/src/user/tests/stress.c b/src/user/tests/stress.c deleted file mode 100644 index 5d29e87..0000000 --- a/src/user/tests/stress.c +++ /dev/null @@ -1,28 +0,0 @@ -#define TEST_MACROS -#include -#include -#include -#include - -static void run_forked(void (*fn)()) { - if (!fork()) { - fn(); - _syscall_exit(0); - } else { - /* successful tests must return 0 - * TODO add a better fail msg */ - if (_syscall_await() != 0) test_fail(); - } -} - - -static void stress_fork(void) { - for (size_t i = 0; i < 2048; i++) { - if (!fork()) _syscall_exit(0); - _syscall_await(); - } -} - -void stress_all(void) { - run_forked(stress_fork); -} -- cgit v1.2.3