diff options
Diffstat (limited to 'src/user/app/init')
-rw-r--r-- | src/user/app/init/driver/driver.h | 8 | ||||
-rw-r--r-- | src/user/app/init/driver/initctl.c | 44 | ||||
-rw-r--r-- | src/user/app/init/driver/ps2.c | 129 | ||||
-rw-r--r-- | src/user/app/init/driver/termcook.c | 103 | ||||
-rw-r--r-- | src/user/app/init/init.c | 151 |
5 files changed, 0 insertions, 435 deletions
diff --git a/src/user/app/init/driver/driver.h b/src/user/app/init/driver/driver.h deleted file mode 100644 index 98c18f1..0000000 --- a/src/user/app/init/driver/driver.h +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once -#include <camellia/types.h> - -void initctl_drv(hid_t killswitch); -void ps2_drv(void); -void tmpfs_drv(void); - -void termcook(void); diff --git a/src/user/app/init/driver/initctl.c b/src/user/app/init/driver/initctl.c deleted file mode 100644 index fed71b7..0000000 --- a/src/user/app/init/driver/initctl.c +++ /dev/null @@ -1,44 +0,0 @@ -#include "driver.h" -#include <camellia/syscalls.h> -#include <ctype.h> -#include <errno.h> -#include <stdlib.h> -#include <string.h> -#include <camellia/compat.h> - -void initctl_drv(hid_t killswitch) { - struct ufs_request res; - char buf[64]; - const size_t buflen = sizeof buf; - while (!c0_fs_wait(buf, buflen, &res)) { - switch (res.op) { - case VFSOP_OPEN: - c0_fs_respond(NULL, res.len == 0 ? 0 : -1, 0); - break; - case VFSOP_WRITE: - /* null terminate */ - if (res.len > buflen - 1) - res.len = buflen - 1; - buf[res.len] = '\0'; - /* cut at first whitespace */ - for (size_t i = 0; buf[i]; i++) { - if (isspace(buf[i])) { - buf[i] = '\0'; - break; - } - } - if (!strcmp(buf, "halt")) { - _sys_write(killswitch, "halt", 4, 0, 0); - } - if (!strcmp(buf, "intr")) { - _sys_write(killswitch, "intr", 4, 0, 0); - } - c0_fs_respond(NULL, res.len, 0); - break; - default: - c0_fs_respond(NULL, -ENOSYS, 0); - break; - } - } - exit(1); -} diff --git a/src/user/app/init/driver/ps2.c b/src/user/app/init/driver/ps2.c deleted file mode 100644 index 5470249..0000000 --- a/src/user/app/init/driver/ps2.c +++ /dev/null @@ -1,129 +0,0 @@ -#include "driver.h" -#include <assert.h> -#include <camellia/compat.h> -#include <camellia/syscalls.h> -#include <errno.h> -#include <shared/ring.h> -#include <stdbool.h> -#include <stdlib.h> -#include <thread.h> - - -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 hid_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); -} - - -/** Is a thread waiting for /kdev/ps2/kb? */ -static volatile bool blocked = false; -/* for use in read_thread */ -static hid_t rt_reqh; -static size_t rt_cap; - -static void read_thread(void *unused) { - char buf[512]; - (void)unused; - - assert(blocked); - while (ring_used((void*)&backlog) == 0) { - /* read raw input until we have something to output */ - int len = _sys_read(fd, buf, sizeof buf, 0); - if (len == 0) break; - for (int i = 0; i < len; i++) - parse_scancode(buf[i]); - } - if (ring_used((void*)&backlog) > 0) { - int ret = ring_get((void*)&backlog, buf, rt_cap); - _sys_fs_respond(rt_reqh, buf, ret, 0); - } else { - _sys_fs_respond(rt_reqh, 0, -EGENERIC, 0); - } - blocked = false; -} - -static void fs_loop(void) { - static char buf[512]; - int ret; - for (;;) { - struct ufs_request res; - hid_t reqh = _sys_fs_wait(buf, sizeof buf, &res); - if (reqh < 0) return; - - switch (res.op) { - case VFSOP_OPEN: - if (res.len == 0) { - _sys_fs_respond(reqh, NULL, 1, 0); - } else { - _sys_fs_respond(reqh, NULL, -ENOENT, 0); - } - break; - - case VFSOP_READ: - if (blocked) { - _sys_fs_respond(reqh, NULL, -EAGAIN, 0); - break; - } else if (ring_used((void*)&backlog) > 0) { - ret = ring_get((void*)&backlog, buf, res.capacity); - _sys_fs_respond(reqh, buf, ret, 0); - } else { - blocked = true; - rt_reqh = reqh; - rt_cap = res.capacity; - thread_create(0, read_thread, 0); - } - break; - - default: - _sys_fs_respond(reqh, NULL, -1, 0); - break; - } - } -} - -void ps2_drv(void) { - fd = _sys_open("/kdev/ps2/kb", 12, 0); - if (fd < 0) exit(1); - - fs_loop(); - exit(0); -} diff --git a/src/user/app/init/driver/termcook.c b/src/user/app/init/driver/termcook.c deleted file mode 100644 index a76f3a8..0000000 --- a/src/user/app/init/driver/termcook.c +++ /dev/null @@ -1,103 +0,0 @@ -#include "driver.h" -#include <camellia/syscalls.h> -#include <stdio.h> -#include <stdlib.h> -#include <unistd.h> - -enum tstate { - Normal, - Esc, - CSI, -}; - -static void w_output(hid_t output, const char *buf, size_t len) { - size_t pos = 0; - while (pos < len) { - int ret = _sys_write(output, buf + pos, len - pos, pos, 0); - if (ret < 0) break; - pos += ret; - } -} - -static void line_editor(hid_t input, hid_t output) { - char readbuf[16], linebuf[256]; - size_t linepos = 0; - enum tstate state = Normal; - for (;;) { - int readlen = _sys_read(input, readbuf, sizeof readbuf, -1); - if (readlen < 0) return; - for (int i = 0; i < readlen; i++) { - char c = readbuf[i]; - switch (state) { - case Normal: - switch (c) { - case '\b': - case 0x7f: - if (linepos != 0) { - printf("\b \b"); - linepos--; - } - break; - case 3: /* C-c */ - _sys_exit(1); - case 4: /* EOT, C-d */ - if (linepos > 0) { - w_output(output, linebuf, linepos); - linepos = 0; - } else { - _sys_write(output, NULL, 0, 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; - case '\e': - state = Esc; - break; - case '\t': - break; - default: - if (linepos < sizeof linebuf) { - linebuf[linepos++] = c; - printf("%c", c); - } - break; - } - break; - case Esc: - if (c == '[') state = CSI; - else state = Normal; - break; - case CSI: - if (0x40 <= c && c <= 0x7E) state = Normal; - break; - } - } - } -} - -void termcook(void) { - hid_t stdin_pipe[2] = {-1, -1}; - if (_sys_pipe(stdin_pipe, 0) < 0) - return; - - if (!fork()) { - /* the caller continues in a child process, - * so it can be killed when the line editor quits */ - _sys_dup(stdin_pipe[0], 0, 0); - close(stdin_pipe[0]); - close(stdin_pipe[1]); - return; - } - if (!fork()) { - close(stdin_pipe[0]); - line_editor(0, stdin_pipe[1]); - exit(0); - } - exit(_sys_await()); -} diff --git a/src/user/app/init/init.c b/src/user/app/init/init.c deleted file mode 100644 index fcebfc7..0000000 --- a/src/user/app/init/init.c +++ /dev/null @@ -1,151 +0,0 @@ -#include "driver/driver.h" -#include <camellia/flags.h> -#include <camellia/syscalls.h> -#include <stdint.h> -#include <stdio.h> -#include <string.h> -#include <unistd.h> -#include <camellia/fs/misc.h> - -#define die(fmt, ...) do { fprintf(stderr, "init: " fmt, __VA_ARGS__); exit(1); } while (0) - -static char title[128]; - -void redirect(const char *exe, const char *out, const char *in) { - if (!fork()) { - snprintf(title, sizeof title, "sh >%s", out); - setproctitle(title); - - if (!freopen(out, "a+", stderr)) { - fprintf(stdout, "couldn't open %s\n", out); - exit(1); - } - if (!freopen(out, "a+", stdout)) - die("couldn't open %s\n", out); - if (!freopen(in, "r", stdin)) - die(" couldn't open %s\n", in); - - for (;;) { - if (!fork()) { - const char *argv[] = {exe, NULL}; - termcook(); - execv(exe, (void*)argv); - fprintf(stderr, "init: couldn't start %s\n", exe); - _sys_sleep(5000); - exit(1); - } - _sys_await(); - _sys_intr(); - } - } -} - -int main(void) { - const char *teststr = "I am teststr.\n"; - hid_t killswitch_pipe[2]; - - freopen("/kdev/com1", "a+", stdout); - freopen("/kdev/com1", "a+", stderr); - printf("[init] stage 2, main at %p, teststr at %p\n", &main, teststr); - - MOUNT_AT("/") { - fs_dirinject2((const char*[]){ - "/keyboard/", - "/usr/", - "/bin/", - "/Users/", - "/tmp/", - "/vtty", - "/net/", - "/initctl", - NULL - }); - } - - MOUNT_AT("/keyboard") { - MOUNT_AT("/") { fs_whitelist((const char*[]){"/kdev/ps2/kb", NULL}); } - ps2_drv(); - } - MOUNT_AT("/usr/") { - fs_union((const char*[]){ - "/init/usr/", - NULL - }); - } - MOUNT_AT("/bin/") { - fs_union((const char*[]){ - "/init/bin/amd64/", - "/init/bin/sh/", - "/init/usr/bin/", - "/init/usr/local/bin/", - NULL - }); - } - MOUNT_AT("/Users/") { - MOUNT_AT("/tmp/") { - const char *argv[] = {"/bin/tmpfs", NULL}; - execv(argv[0], (void*)argv); - } - // TODO a simple union isn't enough here - fs_union((const char*[]){ - "/tmp/", - "/init/Users/", - NULL - }); - } - MOUNT_AT("/tmp/") { - const char *allow[] = {"/bin/tmpfs", NULL}; - const char *argv[] = {"/bin/tmpfs", NULL}; - MOUNT_AT("/") { fs_whitelist(allow); } - execv(argv[0], (void*)argv); - } - MOUNT_AT("/vtty") { - const char *allow[] = {"/bin/vterm", "/kdev/video/", "/keyboard", "/init/usr/share/fonts/", NULL}; - const char *argv[] = {"/bin/vterm", NULL}; - MOUNT_AT("/") { fs_whitelist(allow); } - execv(argv[0], (void*)argv); - } - MOUNT_AT("/net/") { - const char *allow[] = {"/bin/netstack", "/kdev/eth", NULL}; - const char *argv[] = {"/bin/netstack", "/kdev/eth", "192.168.0.11", "192.168.0.2", NULL}; - MOUNT_AT("/") { fs_whitelist(allow); } - execv(argv[0], (void*)argv); - } - - if (_sys_pipe(killswitch_pipe, 0) < 0) { - printf("couldn't create the killswitch pipe, quitting...\n"); - return 1; - } - MOUNT_AT("/initctl") { - close(killswitch_pipe[0]); - initctl_drv(killswitch_pipe[1]); - } - close(killswitch_pipe[1]); - - if (!fork()) { - // TODO close on exec - close(killswitch_pipe[0]); - redirect("/bin/shell", "/kdev/com1", "/kdev/com1"); - redirect("/bin/shell", "/vtty", "/keyboard"); - exit(1); - } - - char buf[128]; - for (;;) { - if (_sys_read(killswitch_pipe[0], buf, 128, 0) != 4) { - break; - } - if (memcmp(buf, "intr", 4) == 0) { - _sys_intr(); - } else if (memcmp(buf, "halt", 4) == 0) { - break; - } - } - printf("[init] intr\n"); - _sys_intr(); - _sys_sleep(1000); - printf("[init] filicide\n"); - _sys_filicide(); - printf("[init] goodbye\n"); - return 0; -} |