Skip to content

Commit b8ce58c

Browse files
committed
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. On Linux, using MSG_NOSIGNAL when sending could turn it to EPIPE. On others, SO_NOSIGPIPE might be used, but not tested (requiring HAS_SO_NOSIGPIPE defined)
1 parent 86198cd commit b8ce58c

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/include/reb-config.h

+1
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ These are now obsolete (as of A107) and should be removed:
227227

228228
#ifdef TO_LINUX
229229
#define HAS_POSIX_SIGNAL
230+
#define HAS_MSG_NOSIGNAL
230231
#endif
231232

232233
//* Defaults ***********************************************************

src/os/dev-net.c

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

225+
#ifdef HAS_SO_NOSIGPIPE
226+
{
227+
int val = 1;
228+
setsockopt(sock->socket, SOL_SOCKET, SO_NOSIGPIPE, &val, sizeof(val));
229+
}
230+
#endif
231+
225232
return DR_DONE;
226233
}
227234

@@ -448,8 +455,12 @@ static REBOOL Nonblocking_Mode(SOCKET sock)
448455

449456
if (mode == RSM_SEND) {
450457
// If host is no longer connected:
458+
int flags = 0;
459+
#ifdef HAS_MSG_NOSIGNAL
460+
flags |= MSG_NOSIGNAL;
461+
#endif
451462
Set_Addr(&remote_addr, sock->net.remote_ip, sock->net.remote_port);
452-
result = sendto(sock->socket, sock->data, len, 0,
463+
result = sendto(sock->socket, sock->data, len, flags,
453464
(struct sockaddr*)&remote_addr, addr_len);
454465
WATCH2("send() len: %d actual: %d\n", len, result);
455466

0 commit comments

Comments
 (0)