From 4e09bca9e34c226b6d7e34b4fa11248405fd988e Mon Sep 17 00:00:00 2001 From: Anton Kling Date: Sun, 22 Oct 2023 19:50:38 +0200 Subject: Move everything into a new repo. --- userland/minibox/utilities/init.c | 72 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 userland/minibox/utilities/init.c (limited to 'userland/minibox/utilities/init.c') diff --git a/userland/minibox/utilities/init.c b/userland/minibox/utilities/init.c new file mode 100644 index 0000000..35bedf2 --- /dev/null +++ b/userland/minibox/utilities/init.c @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int cmdfd; + +void execsh(void) { + char *argv[] = {NULL}; + execv("/sh", argv); +} + +void newtty(void) { + int m; + int s; + openpty(&m, &s, NULL, NULL, NULL); + int pid = fork(); + if (0 == pid) { + dup2(s, 0); + dup2(s, 1); + dup2(s, 2); + execsh(); + return; + } + close(s); + cmdfd = m; +} + +void shell() { + struct pollfd fds[2]; + fds[0].fd = cmdfd; + fds[0].events = POLLIN; + fds[0].revents = 0; + fds[1].fd = open("/dev/keyboard", O_RDONLY, 0); + fds[1].events = POLLIN; + fds[1].revents = 0; + for (;; fds[0].revents = fds[1].revents = 0) { + poll(fds, 2, 0); + if (fds[0].revents & POLLIN) { + char c[4096]; + int rc = read(fds[0].fd, c, 4096); + assert(rc > 0); + for (int i = 0; i < rc; i++) + putchar(c[i]); + } + if (fds[1].revents & POLLIN) { + char c; + int rc = read(fds[1].fd, &c, 1); + assert(rc > 0); + write(cmdfd, &c, 1); + // putchar(c); + } + } +} + +int init_main(void) { + if (1 != getpid()) { + printf("minibox init must be launched as pid1.\n"); + return 1; + } + if (fork()) + for (;;) + wait(NULL); + char *a[] = {NULL}; + execv("/term", a); + return 1; +} -- cgit v1.2.3