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 --- src/cmd/init/driver/driver.h | 1 - src/cmd/init/driver/initctl.c | 44 -------------------------------- src/cmd/init/init.c | 58 +++++++++++++++---------------------------- 3 files changed, 20 insertions(+), 83 deletions(-) delete mode 100644 src/cmd/init/driver/initctl.c (limited to 'src/cmd') 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; } -- cgit v1.2.3