From 938bbf51ebc257d61f20efac9b7a76c6db286c45 Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Thu, 2 Nov 2023 16:59:57 +0900 Subject: [PATCH] tty: implement FLUSH on tcsetattr correctly? --- base/usr/include/kernel/ringbuffer.h | 1 + kernel/misc/ringbuffer.c | 6 ++++++ kernel/vfs/tty.c | 7 ++++++- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/base/usr/include/kernel/ringbuffer.h b/base/usr/include/kernel/ringbuffer.h index 95eaef91..2151c388 100644 --- a/base/usr/include/kernel/ringbuffer.h +++ b/base/usr/include/kernel/ringbuffer.h @@ -31,4 +31,5 @@ void ring_buffer_interrupt(ring_buffer_t * ring_buffer); void ring_buffer_alert_waiters(ring_buffer_t * ring_buffer); void ring_buffer_select_wait(ring_buffer_t * ring_buffer, void * process); void ring_buffer_eof(ring_buffer_t * ring_buffer); +void ring_buffer_discard(ring_buffer_t * ring_buffer); diff --git a/kernel/misc/ringbuffer.c b/kernel/misc/ringbuffer.c index 3cc05733..25c7c0c5 100644 --- a/kernel/misc/ringbuffer.c +++ b/kernel/misc/ringbuffer.c @@ -86,6 +86,12 @@ void ring_buffer_select_wait(ring_buffer_t * ring_buffer, void * process) { list_insert(((process_t *)process)->node_waits, ring_buffer); } +void ring_buffer_discard(ring_buffer_t * ring_buffer) { + spin_lock(ring_buffer->lock); + ring_buffer->read_ptr = ring_buffer->write_ptr; + spin_unlock(ring_buffer->lock); +} + size_t ring_buffer_read(ring_buffer_t * ring_buffer, size_t size, uint8_t * buffer) { size_t collected = 0; while (collected == 0) { diff --git a/kernel/vfs/tty.c b/kernel/vfs/tty.c index 5ecff71a..706d3aee 100644 --- a/kernel/vfs/tty.c +++ b/kernel/vfs/tty.c @@ -322,13 +322,18 @@ int pty_ioctl(pty_t * pty, unsigned long request, void * argp) { return 0; case TCSETS: case TCSETSW: - case TCSETSF: if (!argp) return -EINVAL; validate(argp); + /* TODO wait on output for SETSW */ if (!(((struct termios *)argp)->c_lflag & ICANON) && (pty->tios.c_lflag & ICANON)) { /* Switch out of canonical mode, the dump the input buffer */ dump_input_buffer(pty); } + goto tcset_common; + case TCSETSF: + clear_input_buffer(pty); + ring_buffer_discard(pty->in); + tcset_common: memcpy(&pty->tios, argp, sizeof(struct termios)); return 0; default: