Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

posix: signal SIGPIPE where necessary #514

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading