Skip to content

Commit de01baa

Browse files
committed
prevent SIGPIPE from being generated
1 parent 95b09bf commit de01baa

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

src/lib/ares__socket.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,14 @@ static int configure_socket(ares_socket_t s, struct server_state *server)
184184
}
185185
#endif
186186

187+
/* No need to emit SIGPIPE on socket errors */
188+
#if defined(SO_NOSIGPIPE)
189+
{
190+
int opt = 1;
191+
setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, (void *)&opt, sizeof(opt));
192+
}
193+
#endif
194+
187195
/* Set the socket's send and receive buffer sizes. */
188196
if ((channel->socket_send_buffer_size > 0) &&
189197
setsockopt(s, SOL_SOCKET, SO_SNDBUF,

test/ares-test.cc

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,10 +423,16 @@ MockServer::MockServer(int family, unsigned short port)
423423
// Send TCP data right away.
424424
setsockopt(tcpfd_, IPPROTO_TCP, TCP_NODELAY,
425425
BYTE_CAST &optval , sizeof(int));
426+
#if defined(SO_NOSIGPIPE)
427+
setsockopt(tcpfd_, SOL_SOCKET, SO_NOSIGPIPE, (void *)&optval, sizeof(optval));
428+
#endif
426429

427430
// Create a UDP socket to receive data on.
428431
udpfd_ = socket(family, SOCK_DGRAM, 0);
429432
EXPECT_NE(ARES_SOCKET_BAD, udpfd_);
433+
#if defined(SO_NOSIGPIPE)
434+
setsockopt(udpfd_, SOL_SOCKET, SO_NOSIGPIPE, (void *)&optval, sizeof(optval));
435+
#endif
430436

431437
// Bind the sockets to the given port.
432438
if (family == AF_INET) {
@@ -655,6 +661,7 @@ void MockServer::ProcessRequest(ares_socket_t fd, struct sockaddr_storage* addr,
655661

656662
/* DNS 0x20 will mix case, do case-insensitive matching of name in request */
657663
char lower_name[256];
664+
int flags = 0;
658665
arestest_strtolower(lower_name, name, sizeof(lower_name));
659666

660667
// Before processing, let gMock know the request is happening.
@@ -701,7 +708,11 @@ void MockServer::ProcessRequest(ares_socket_t fd, struct sockaddr_storage* addr,
701708
addrlen = 0;
702709
}
703710

704-
ares_ssize_t rc = (ares_ssize_t)sendto(fd, BYTE_CAST reply.data(), (SEND_TYPE_ARG3)reply.size(), 0,
711+
#ifdef MSG_NOSIGNAL
712+
flags |= MSG_NOSIGNAL;
713+
#endif
714+
715+
ares_ssize_t rc = (ares_ssize_t)sendto(fd, BYTE_CAST reply.data(), (SEND_TYPE_ARG3)reply.size(), flags,
705716
(struct sockaddr *)addr, addrlen);
706717
if (rc < static_cast<ares_ssize_t>(reply.size())) {
707718
std::cerr << "Failed to send full reply, rc=" << rc << std::endl;

0 commit comments

Comments
 (0)