diff options
author | Anton Kling <anton@kling.gg> | 2024-03-26 11:40:39 +0100 |
---|---|---|
committer | Anton Kling <anton@kling.gg> | 2024-03-26 11:40:39 +0100 |
commit | 297231bb3602d868d3891d357026c53f9fcc2402 (patch) | |
tree | 6c4f21db84912750f6f464b26bf1e4503d1fe479 /userland | |
parent | 3deb2df8e62a5f0a5535ee734a5aa13b0959f53f (diff) |
Increase support for signals
Diffstat (limited to 'userland')
-rw-r--r-- | userland/libc/include/signal.h | 1 | ||||
-rw-r--r-- | userland/minibox/Makefile | 2 | ||||
-rw-r--r-- | userland/minibox/minibox.c | 2 | ||||
-rw-r--r-- | userland/minibox/utilities/include.h | 1 | ||||
-rw-r--r-- | userland/sh/sh.c | 54 | ||||
-rw-r--r-- | userland/terminal/term.c | 16 |
6 files changed, 68 insertions, 8 deletions
diff --git a/userland/libc/include/signal.h b/userland/libc/include/signal.h index 621710b..2be081c 100644 --- a/userland/libc/include/signal.h +++ b/userland/libc/include/signal.h @@ -12,6 +12,7 @@ #define SIGBUS 8 #define SIGKILL 9 #define SIGFPE 10 +#define SIGTERM 15 typedef int pid_t; typedef int sigset_t; diff --git a/userland/minibox/Makefile b/userland/minibox/Makefile index b2894c3..5d1e359 100644 --- a/userland/minibox/Makefile +++ b/userland/minibox/Makefile @@ -1,6 +1,6 @@ CC="i686-sb-gcc" CFLAGS=-Wall -Wextra -pedantic -Wimplicit-fallthrough -g -O0 -OBJ=minibox.o utilities/cat.o utilities/echo.o utilities/yes.o utilities/minibox.o utilities/ascii.o utilities/wc.o utilities/init.o utilities/ls.o utilities/touch.o utilities/ed.o utilities/sh/sh.o utilities/sh/lexer.o utilities/sh/ast.o +OBJ=minibox.o utilities/cat.o utilities/echo.o utilities/yes.o utilities/minibox.o utilities/ascii.o utilities/wc.o utilities/init.o utilities/ls.o utilities/touch.o utilities/ed.o utilities/sh/sh.o utilities/sh/lexer.o utilities/sh/ast.o utilities/kill.o %.o: %.c $(CC) $(CFLAGS) $(INCLUDE) $(LIBS) -c $< -o $@ diff --git a/userland/minibox/minibox.c b/userland/minibox/minibox.c index f3950df..e5a7f79 100644 --- a/userland/minibox/minibox.c +++ b/userland/minibox/minibox.c @@ -22,7 +22,7 @@ typedef struct Command { Command utilities[] = {COMMAND(minibox), COMMAND(ascii), COMMAND(echo), COMMAND(cat), COMMAND(yes), COMMAND(wc), COMMAND(init), COMMAND(ls), COMMAND(touch), - COMMAND(ed), COMMAND(sh)}; + COMMAND(ed), COMMAND(sh), COMMAND(kill)}; char *parse_filename(char *str) { char *tmp = NULL, *is = str; diff --git a/userland/minibox/utilities/include.h b/userland/minibox/utilities/include.h index b01d976..a3bd561 100644 --- a/userland/minibox/utilities/include.h +++ b/userland/minibox/utilities/include.h @@ -38,5 +38,6 @@ int ls_main(int argc, char **argv); int touch_main(int argc, char **argv); int ed_main(int argc, char **argv); int sh_main(int argc, char **argv); +int kill_main(int argc, char **argv); int init_main(void); diff --git a/userland/sh/sh.c b/userland/sh/sh.c index 5fe7f63..e7741aa 100644 --- a/userland/sh/sh.c +++ b/userland/sh/sh.c @@ -2,6 +2,7 @@ #include <fcntl.h> #include <limits.h> #include <poll.h> +#include <signal.h> #include <stddef.h> #include <stdio.h> #include <stdlib.h> @@ -11,6 +12,47 @@ #define IS_SPECIAL(_c) (';' == _c || '\n' == _c || '&' == _c || '|' == _c) +struct child_process { + int pid; + struct child_process *next; +}; + +struct child_process *children = NULL; + +void add_child_process(int pid) { + struct child_process *new = malloc(sizeof(struct child_process)); + new->next = NULL; + new->pid = pid; + struct child_process **entry = &children; + for (; *entry; entry = &(*entry)->next) + ; + *entry = new; +} + +void remove_child_process(int pid) { + struct child_process *prev = NULL; + struct child_process *child = children; + for (; child; child = child->next) { + if (pid == child->pid) { + if (prev) { + prev->next = child->next; + } else { + children = child->next; + } + free(child); + return; + } + prev = child; + } +} + +void slaugther_children(void) { + struct child_process *child = children; + for (; child; child = child->next) { + kill(child->pid, SIGTERM); + } +} + char *PATH = "/:/bin"; int can_open(char *f) { @@ -112,6 +154,7 @@ int execute_program(char *s, size_t l, int ignore_rc, int f_stdout, exit(1); } } + add_child_process(pid); close(fd[1]); if (new_out) @@ -213,7 +256,18 @@ char *get_line(void) { return str; } +void term_handler() { + slaugther_children(); + exit(0); +} + int main(void) { + struct sigaction act = {0}; + act.sa_handler = term_handler; + if (-1 == sigaction(SIGTERM, &act, NULL)) { + perror("sigaction"); + return 1; + } for (;;) { printf("/ : "); char *l = get_line(); diff --git a/userland/terminal/term.c b/userland/terminal/term.c index 4824a59..cd75a90 100644 --- a/userland/terminal/term.c +++ b/userland/terminal/term.c @@ -4,6 +4,7 @@ #include <libgui.h> #include <poll.h> #include <pty.h> +#include <signal.h> #include <socket.h> #include <stddef.h> #include <stdint.h> @@ -19,6 +20,8 @@ uint32_t screen_pos_x = 0; uint32_t screen_pos_y = 0; int serial_fd; +int shell_pid; + int raw_mode = 0; void execsh(void) { @@ -98,8 +101,8 @@ void newtty(void) { int m; int s; openpty(&m, &s, NULL, NULL, NULL); - int pid = fork(); - if (0 == pid) { + shell_pid = fork(); + if (0 == shell_pid) { close(global_w->ws_socket); close(global_w->bitmap_fd); dup2(s, 0); @@ -246,13 +249,14 @@ void run() { } if (WINDOWSERVER_EVENT_WINDOW_EXIT == e.type) { close(global_w->ws_socket); + kill(shell_pid, SIGTERM); exit(0); return; } -// if (WINDOWSERVER_EVENT_WINDOW_RESIZE == e.type) { -// GUI_Resize(global_w, e.vector[0], e.vector[1]); -// continue; -// } + // if (WINDOWSERVER_EVENT_WINDOW_RESIZE == e.type) { + // GUI_Resize(global_w, e.vector[0], e.vector[1]); + // continue; + // } if (WINDOWSERVER_EVENT_KEYPRESS != e.type) { continue; } |