From ab27fadd431103d06977d7f2eb6b3ab45a06a5df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Leczkowski?= Date: Wed, 31 Jan 2024 17:12:09 +0100 Subject: [PATCH] posix: signal SIGPIPE where necessary JIRA: RTOS-759 --- include/posix.h | 1 + posix/posix.c | 6 ++++++ posix/unix.c | 13 +++++++++++-- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/include/posix.h b/include/posix.h index d38bbfee1..b2ee9c4c3 100644 --- a/include/posix.h +++ b/include/posix.h @@ -165,6 +165,7 @@ struct stat { #define MSG_OOB 0x04 #define MSG_DONTWAIT 0x08 #define MSG_MORE 0x10 +#define MSG_NOSIGNAL 0x20 #define SCM_RIGHTS 1 diff --git a/posix/posix.c b/posix/posix.c index 6f7cb41a4..b836752a4 100644 --- a/posix/posix.c +++ b/posix/posix.c @@ -694,6 +694,7 @@ ssize_t posix_write(int fildes, void *buf, size_t nbyte) off_t offs; unsigned int status; int err; + thread_t *curr; err = posix_getOpenFile(fildes, &f); if (err < 0) { @@ -712,9 +713,14 @@ ssize_t posix_write(int fildes, void *buf, size_t nbyte) if (f->type == ftUnixSocket) { rcnt = unix_sendto(f->oid.id, buf, nbyte, 0, NULL, 0); + /* SIGPIPE handled by `send` */ } else { rcnt = proc_write(f->oid, offs, buf, nbyte, status); + if (rcnt == -EPIPE) { + curr = proc_current(); + (void)posix_tkill(process_getPid(curr->process), proc_getTid(curr), SIGPIPE); + } } if (rcnt > 0) { diff --git a/posix/unix.c b/posix/unix.c index db9c0c65e..c9fe7429c 100644 --- a/posix/unix.c +++ b/posix/unix.c @@ -698,6 +698,7 @@ static ssize_t send(unsigned socket, const void *buf, size_t len, int flags, con unixsock_t *s, *conn; int err; spinlock_ctx_t sc; + thread_t *curr; if ((s = unixsock_get(socket)) == NULL) return -ENOTSOCK; @@ -728,12 +729,20 @@ static ssize_t send(unsigned socket, const void *buf, size_t len, int flags, con break; } else if (s->state & US_PEER_CLOSED) { - posix_tkill(process_getPid(proc_current()->process), 0, SIGPIPE); + curr = proc_current(); + (void)posix_tkill(process_getPid(curr->process), proc_getTid(curr), SIGPIPE); err = -EPIPE; break; } else if ((conn = unixsock_get_connected(s)) == NULL) { - err = -ENOTCONN; + if (((s->type == SOCK_STREAM) || (s->type == SOCK_SEQPACKET)) && ((flags & MSG_NOSIGNAL) == 0)) { + curr = proc_current(); + (void)posix_tkill(process_getPid(curr->process), proc_getTid(curr), SIGPIPE); + err = -EPIPE; + } + else { + err = -ENOTCONN; + } break; } }