未验证 提交 e9185f46 编写于 作者: J Jim 提交者: GitHub

Merge pull request #1874 from tmatth/bugfix/avoid-sigpipe-on-closed-sock

Avoid sigpipe on writing to closed file descriptors
......@@ -50,6 +50,7 @@
#include <windows.h>
#else
#include <signal.h>
#include <pthread.h>
#endif
#include <iostream>
......@@ -2245,6 +2246,18 @@ int main(int argc, char *argv[])
sig_handler.sa_flags = 0;
sigaction(SIGINT, &sig_handler, NULL);
/* Block SIGPIPE in all threads, this can happen if a thread calls write on
a closed pipe. */
sigset_t sigpipe_mask;
sigemptyset(&sigpipe_mask);
sigaddset(&sigpipe_mask, SIGPIPE);
sigset_t saved_mask;
if (pthread_sigmask(SIG_BLOCK, &sigpipe_mask, &saved_mask) == -1) {
perror("pthread_sigmask");
exit(1);
}
#endif
#ifdef _WIN32
......
......@@ -40,6 +40,10 @@
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
#if !defined(MSG_NOSIGNAL)
#define MSG_NOSIGNAL 0
#endif
#if defined(USE_MBEDTLS)
#if defined(_WIN32)
#include <windows.h>
......@@ -930,6 +934,11 @@ RTMP_Connect0(RTMP *r, struct sockaddr * service, socklen_t addrlen)
if (r->m_sb.sb_socket != INVALID_SOCKET)
{
#ifndef _WIN32
#ifdef SO_NOSIGPIPE
setsockopt(r->m_sb.sb_socket, SOL_SOCKET, SO_NOSIGPIPE, &(int){ 1 }, sizeof(int));
#endif
#endif
if(r->m_bindIP.addrLen)
{
if (bind(r->m_sb.sb_socket, (const struct sockaddr *)&r->m_bindIP.addr, r->m_bindIP.addrLen) < 0)
......@@ -4589,7 +4598,7 @@ RTMPSockBuf_Fill(RTMPSockBuf *sb)
else
#endif
{
nBytes = recv(sb->sb_socket, sb->sb_start + sb->sb_size, nBytes, 0);
nBytes = recv(sb->sb_socket, sb->sb_start + sb->sb_size, nBytes, MSG_NOSIGNAL);
}
if (nBytes > 0)
{
......@@ -4642,7 +4651,7 @@ RTMPSockBuf_Send(RTMPSockBuf *sb, const char *buf, int len)
else
#endif
{
rc = send(sb->sb_socket, buf, len, 0);
rc = send(sb->sb_socket, buf, len, MSG_NOSIGNAL);
}
return rc;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册