Skip to content

Commit 16ae588

Browse files
committed
FIX: ATRONIX: Prevent SIGPIPE when writing
When the other end closed the socket, send could generate a SIGPIPE, and result in process termination if it's not handled. (based on commit b8ce58c) Instead of defining HAS_SO_NOSIGPIPE and HAS_MSG_NOSIGNAL as it is done in Atronix's branch, I do check for values of these defines directly. Related SO page: https://stackoverflow.com/q/108183/494472
1 parent e977b2e commit 16ae588

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/os/dev-net.c

+14-1
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,14 @@ static REBOOL Nonblocking_Mode(SOCKET sock)
222222
return DR_ERROR;
223223
}
224224

225+
#ifdef SO_NOSIGPIPE
226+
{
227+
// prevent SIGPIPE https://stackoverflow.com/q/108183/494472
228+
int val = 1; /* Set NOSIGPIPE to ON */
229+
setsockopt(sock->socket, SOL_SOCKET, SO_NOSIGPIPE, &val, sizeof(val));
230+
}
231+
#endif
232+
225233
return DR_DONE;
226234
}
227235

@@ -448,8 +456,13 @@ static REBOOL Nonblocking_Mode(SOCKET sock)
448456

449457
if (mode == RSM_SEND) {
450458
// If host is no longer connected:
459+
int flags = 0;
460+
#ifdef MSG_NOSIGNAL
461+
// prevent SIGPIPE https://stackoverflow.com/q/108183/494472
462+
flags |= MSG_NOSIGNAL;
463+
#endif
451464
Set_Addr(&remote_addr, sock->net.remote_ip, sock->net.remote_port);
452-
result = sendto(sock->socket, sock->data, len, 0,
465+
result = sendto(sock->socket, sock->data, len, flags,
453466
(struct sockaddr*)&remote_addr, addr_len);
454467
WATCH2("send() len: %d actual: %d\n", len, result);
455468

0 commit comments

Comments
 (0)