From 23a8e1b78063c0276d987e835434700267b9c8f8 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Thu, 3 Jul 2025 19:59:45 -0400 Subject: kernel: Ensure mutually exclusive console access If multiple processes try to write to the console, a race condition of sorts may occur. Similarly, if multiple processes try to read from the console and contend with the console input buffer. One process may steal keys from the other. Prevent this by implementing a mutex within the console descriptor. Each time a process reads or writes the console, it attaches itself. Any other processes attempting to read or write the console while another is attached will be yielded to the scheduler until the resource is free. Signed-off-by: Ian Moffett --- sys/include/dev/cons/cons.h | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'sys/include/dev/cons/cons.h') diff --git a/sys/include/dev/cons/cons.h b/sys/include/dev/cons/cons.h index aa9d8e3..c82c3c5 100644 --- a/sys/include/dev/cons/cons.h +++ b/sys/include/dev/cons/cons.h @@ -32,6 +32,8 @@ #include #include +#include +#include #include #include #include @@ -49,6 +51,8 @@ struct cons_screen { struct fbdev fbdev; struct ansi_state ansi_s; struct console_feat feat; /* Features */ + struct proc *atproc; /* Attached proc */ + struct mutex *atproc_lock; uint32_t fg; uint32_t bg; @@ -72,6 +76,8 @@ void cons_update_color(struct cons_screen *scr, uint32_t fg, uint32_t bg); void cons_clear_scr(struct cons_screen *scr, uint32_t bg); void cons_reset_color(struct cons_screen *scr); void cons_reset_cursor(struct cons_screen *scr); +int cons_attach(void); +int cons_detach(void); int cons_putch(struct cons_screen *scr, char c); int cons_putstr(struct cons_screen *scr, const char *s, size_t len); -- cgit v1.2.3