Skip to content

Commit

Permalink
posix: signal SIGPIPE where necessary
Browse files Browse the repository at this point in the history
JIRA: RTOS-759
  • Loading branch information
lukileczo committed Jan 31, 2024
1 parent a1980f4 commit ab27fad
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions include/posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions posix/posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
13 changes: 11 additions & 2 deletions posix/unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down

0 comments on commit ab27fad

Please sign in to comment.