From c9fbab701ff57583733c84fa93889eacf422b861 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Wed, 13 Sep 2023 00:05:06 +0200 Subject: cmd/init: remove /initctl, use intr instead --- boot | 3 ++- src/cmd/init/driver/driver.h | 1 - src/cmd/init/driver/initctl.c | 44 -------------------------------- src/cmd/init/init.c | 58 +++++++++++++++---------------------------- src/kernel/arch/amd64/time.c | 1 + src/kernel/proc.c | 7 ++++++ src/kernel/proc.h | 4 +++ sysroot/bin/sh/halt | 2 +- 8 files changed, 35 insertions(+), 85 deletions(-) delete mode 100644 src/cmd/init/driver/initctl.c diff --git a/boot b/boot index a1d3b8b..aa5f8c7 100755 --- a/boot +++ b/boot @@ -45,7 +45,6 @@ for opt; do mkfifo out/qemu.in out/qemu.out TEST_RUN=1 QTTY="-serial pipe:out/qemu" - POST="cat out/qemu.out" ;; --no-serial) QTTY="-serial none" @@ -64,6 +63,8 @@ elif [ -n "$TEST_RUN" ]; then echo "$CMD &" $CMD & + sleep 1 # dunno + # something eats the first character sent, so let's send a sacrificial newline echo > out/qemu.in # carry on with the tests diff --git a/src/cmd/init/driver/driver.h b/src/cmd/init/driver/driver.h index 98c18f1..8bef042 100644 --- a/src/cmd/init/driver/driver.h +++ b/src/cmd/init/driver/driver.h @@ -1,7 +1,6 @@ #pragma once #include -void initctl_drv(hid_t killswitch); void ps2_drv(void); void tmpfs_drv(void); diff --git a/src/cmd/init/driver/initctl.c b/src/cmd/init/driver/initctl.c deleted file mode 100644 index fed71b7..0000000 --- a/src/cmd/init/driver/initctl.c +++ /dev/null @@ -1,44 +0,0 @@ -#include "driver.h" -#include -#include -#include -#include -#include -#include - -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/cmd/init/init.c b/src/cmd/init/init.c index acb6a8f..093e281 100644 --- a/src/cmd/init/init.c +++ b/src/cmd/init/init.c @@ -1,19 +1,18 @@ #include "driver/driver.h" #include #include +#include +#include #include +#include #include #include #include #include -#include - -#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()) { + static char title[128]; snprintf(title, sizeof title, "sh >%s", out); setproctitle(title); @@ -22,9 +21,9 @@ void redirect(const char *exe, const char *out, const char *in) { exit(1); } if (!freopen(out, "a+", stdout)) - die("couldn't open %s\n", out); + err(1, "couldn't open %s", out); if (!freopen(in, "r", stdin)) - die(" couldn't open %s\n", in); + err(1, "couldn't open %s", in); for (;;) { if (!fork()) { @@ -41,9 +40,17 @@ void redirect(const char *exe, const char *out, const char *in) { } } -int main(void) { - hid_t killswitch_pipe[2]; +void shutdown(void) { + printf("[init] intr\n"); + _sys_intr(); + _sys_sleep(1000); + printf("[init] filicide\n"); + _sys_filicide(); + printf("[init] goodbye\n"); + exit(0); +} +int main(void) { freopen("/dev/com1", "a+", stdout); freopen("/dev/com1", "a+", stderr); @@ -56,7 +63,6 @@ int main(void) { "/tmp/", "/vtty", "/net/", - "/initctl", NULL }); } @@ -111,40 +117,16 @@ int main(void) { 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", "/dev/com1", "/dev/com1"); redirect("/bin/shell", "/vtty", "/keyboard"); exit(1); } - char buf[128]; + intr_set(shutdown); 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; - } + // TODO sleep(-1) to sleep forever + // TODO maybe init should collect dead children? + _sys_sleep(86400000); } - printf("[init] intr\n"); - _sys_intr(); - _sys_sleep(1000); - printf("[init] filicide\n"); - _sys_filicide(); - printf("[init] goodbye\n"); - return 0; } diff --git a/src/kernel/arch/amd64/time.c b/src/kernel/arch/amd64/time.c index aa7603c..f4fa744 100644 --- a/src/kernel/arch/amd64/time.c +++ b/src/kernel/arch/amd64/time.c @@ -48,6 +48,7 @@ void timer_deschedule(Proc *p) { assert(*slot); *slot = p->waits4timer.next; + proc_setstate(p, PS_RUNNING); update_goal(); } diff --git a/src/kernel/proc.c b/src/kernel/proc.c index 1084f48..4164874 100644 --- a/src/kernel/proc.c +++ b/src/kernel/proc.c @@ -394,8 +394,15 @@ void proc_tryreap(Proc *dead) { } } +void proc_tryintr(Proc *p) { + if (p->state == PS_WAITS4TIMER) { + timer_deschedule(p); + } +} + void proc_intr(Proc *p) { if (!p->intr_fn) return; + proc_tryintr(p); /* save old rsp,rip */ struct intr_data d; diff --git a/src/kernel/proc.h b/src/kernel/proc.h index 15da852..bfe5cfc 100644 --- a/src/kernel/proc.h +++ b/src/kernel/proc.h @@ -119,6 +119,10 @@ void proc_filicide(Proc *proc, int ret); /** Tries to reap a dead process / free a tombstone. */ void proc_tryreap(Proc *dead); +/** Try to interrupt whatever the process is doing instead of PS_RUNNING. */ +void proc_tryintr(Proc *p); + +/** Send an interupt to a process. */ void proc_intr(Proc *proc); /** Switches execution to any running process. */ diff --git a/sysroot/bin/sh/halt b/sysroot/bin/sh/halt index 563aded..7304155 100644 --- a/sysroot/bin/sh/halt +++ b/sysroot/bin/sh/halt @@ -1,2 +1,2 @@ #!/bin/shell -echo halt > /initctl \ No newline at end of file +echo "Die monster. You don't belong in this world!" > /proc/1/intr -- cgit v1.2.3