From 014463db5a06637bb5b36d231c4303ea3e3a6aba Mon Sep 17 00:00:00 2001 From: Mark Date: Fri, 24 Jul 2020 14:35:08 +0300 Subject: [PATCH] Properly shutdown pipe writer when reader closes --- sys/char/pipe.c | 4 +--- sys/char/ring.c | 7 +++++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/sys/char/pipe.c b/sys/char/pipe.c index b35f4c8..f458f21 100644 --- a/sys/char/pipe.c +++ b/sys/char/pipe.c @@ -108,9 +108,7 @@ static ssize_t pipe_vnode_read(struct ofile *of, void *buf, size_t count) { static void pipe_vnode_close(struct ofile *of) { _assert(of && of->file.priv_data); - if (of->flags & OF_WRITABLE) { - ring_signal(of->file.priv_data, RING_SIGNAL_EOF); - } + ring_signal(of->file.priv_data, RING_SIGNAL_EOF); // TODO: this //kfree(((struct ring *) of->file.priv_data)->base); //kfree(of->file.priv_data); diff --git a/sys/char/ring.c b/sys/char/ring.c index 4db9d08..daac9c7 100644 --- a/sys/char/ring.c +++ b/sys/char/ring.c @@ -96,12 +96,18 @@ int ring_putc(struct thread *ctx, struct ring *ring, char c, int wait) { if (wait) { int res; while (!ring_writable(ring)) { + if (ring->flags & RING_SIGNAL_EOF) { + return -EPIPE; + } if ((res = thread_wait_io(ctx, &ring->writer_wait)) != 0) { _assert(res == -EINTR); return res; } } } + if (ring->flags & RING_SIGNAL_EOF) { + return -EPIPE; + } ring->base[ring->wr] = c; ring_advance_write(ring); @@ -122,6 +128,7 @@ int ring_write(struct thread *ctx, struct ring *ring, const void *buf, size_t le void ring_signal(struct ring *r, int s) { r->flags |= s; thread_notify_io(&r->wait); + thread_notify_io(&r->writer_wait); } int ring_init(struct ring *r, size_t cap) {