summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordzwdz2023-09-13 00:05:06 +0200
committerdzwdz2023-09-13 00:05:06 +0200
commitc9fbab701ff57583733c84fa93889eacf422b861 (patch)
tree7a15a77a97d7965782fb81c80f9536f5d1911f3b /src
parent1e54db260811d0d759e76be725d50f4b59d0fc41 (diff)
cmd/init: remove /initctl, use intr instead
Diffstat (limited to 'src')
-rw-r--r--src/cmd/init/driver/driver.h1
-rw-r--r--src/cmd/init/driver/initctl.c44
-rw-r--r--src/cmd/init/init.c58
-rw-r--r--src/kernel/arch/amd64/time.c1
-rw-r--r--src/kernel/proc.c7
-rw-r--r--src/kernel/proc.h4
6 files changed, 32 insertions, 83 deletions
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 <camellia/types.h>
-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 <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/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 <camellia/compat.h>
#include <camellia/flags.h>
+#include <camellia/fs/misc.h>
+#include <camellia/intr.h>
#include <camellia/syscalls.h>
+#include <err.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()) {
+ 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. */