Skip to content

Commit bc85d5b

Browse files
committed
Add disabling SIGPIPE per socket...
...where available. For now, that's only MacOS, but should be "expandable" to BSDs, we're just not sure which ones.
1 parent 50b8fd6 commit bc85d5b

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
lines changed

freertos/pubnub_internal.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
#define SOCKET_INVALID FREERTOS_INVALID_SOCKET
3838

3939

40+
/* FreeRTOS+TCP never raises SIGPIPE, so, we're good. */
41+
#define socket_disable_SIGPIPE(socket)
42+
43+
4044
typedef Socket_t pb_socket_t;
4145

4246
/** The Pubnub FreeRTOS context */

lib/sockets/pbpal_resolv_and_connect_sockets.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ enum pbpal_resolv_n_connect_result pbpal_resolv_and_connect(pubnub_t *pb)
116116
}
117117

118118
socket_set_rcv_timeout(pb->pal.socket, pb->transaction_timeout_ms);
119+
socket_disable_SIGPIPE(pb->pal.socket);
119120

120121
return error ? pbpal_connect_wouldblock : pbpal_connect_success;
121122
}

posix/pubnub_internal.h

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* -*- c-file-style:"stroustrup"; indent-tabs-mode: nil -*- */
22
#if !defined INC_PUBNUB_INTERNAL
3-
#define INC_PUBNUB_INTERNAL
3+
#define INC_PUBNUB_INTERNAL
44

55

66
#include <unistd.h>
@@ -24,12 +24,14 @@ typedef int pb_socket_t;
2424
#define socket_send(socket, buf, len) send((socket), (buf), (len), 0)
2525
#endif
2626

27-
#define socket_recv(socket, buf, len, flags) recv((socket), (buf), (len), (flags))
27+
#define socket_recv(socket, buf, len, flags) \
28+
recv((socket), (buf), (len), (flags))
2829

29-
/* Treating `EINPROGRESS` the same as `EWOULDBLOCK` isn't
30+
/* Treating `EINPROGRESS` the same as `EWOULDBLOCK` isn't
3031
the greatest solution, but it is good for now.
3132
*/
32-
#define socket_would_block() ((errno == EAGAIN) ||(errno == EWOULDBLOCK) || (errno == EINPROGRESS))
33+
#define socket_would_block() \
34+
((errno == EAGAIN) || (errno == EWOULDBLOCK) || (errno == EINPROGRESS))
3335

3436
#define socket_timed_out() (errno == ETIMEDOUT)
3537

@@ -38,16 +40,35 @@ typedef int pb_socket_t;
3840
#define SOCKET_INVALID -1
3941
#define SOCKET_ERROR -1
4042

41-
/* Maybe we could use `getsockopt(socket_fd, SOL_SOCKET, SO_ERROR, &error, &len)`,
42-
but, its utility is questionable, so probably test extensively to see if it
43-
really works for us.
43+
/* Maybe we could use `getsockopt(socket_fd, SOL_SOCKET, SO_ERROR, &error,
44+
&len)`, but, its utility is questionable, so probably test extensively to see
45+
if it really works for us.
4446
*/
4547
#define socket_is_connected(socket) true
4648

47-
#define socket_set_rcv_timeout(socket, milliseconds) do { \
48-
struct timeval M_tm = { (milliseconds)/1000, ((milliseconds)%1000) * 1000 }; \
49-
setsockopt((socket), SOL_SOCKET, SO_RCVTIMEO, (char*)&M_tm, sizeof M_tm); \
50-
} while(0)
49+
#define socket_set_rcv_timeout(socket, milliseconds) \
50+
do { \
51+
struct timeval M_tm = { (milliseconds) / 1000, \
52+
((milliseconds) % 1000) * 1000 }; \
53+
setsockopt((socket), SOL_SOCKET, SO_RCVTIMEO, (char*)&M_tm, sizeof M_tm); \
54+
} while (0)
55+
56+
57+
#if __APPLE__
58+
/* Actually, BSD in general provides SO_NOSIGPIPE, but we don't know
59+
what's the status of this support across various BSDs... So, for
60+
now, we only do this for MacOS.
61+
*/
62+
#define socket_disable_SIGPIPE(socket) \
63+
do { \
64+
int on = 1; \
65+
if (setsockopt(socket, SOL_SOCKET, SO_NOSIGPIPE, &on, sizeof(on)) == -1) { \
66+
PUBNUB_LOG_WARNING("Failed to set SO_NOSIGPIPE, errno=%d\n", errno); \
67+
} \
68+
} while (0)
69+
#else
70+
#define socket_disable_SIGPIPE(socket)
71+
#endif
5172

5273

5374
/** The Pubnub POSIX context */
@@ -66,5 +87,4 @@ struct pubnub_pal {
6687
#include "core/pubnub_internal_common.h"
6788

6889

69-
7090
#endif /* !defined INC_PUBNUB_INTERNAL */

windows/pubnub_internal.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ int socket_platform_init(void);
3737
setsockopt((socket), SOL_SOCKET, SO_RCVTIMEO, (char*)&M_tm, sizeof M_tm); \
3838
} while(0)
3939

40+
41+
/* Winsock never raises SIGPIPE, so, we're good. */
42+
#define socket_disable_SIGPIPE(socket)
43+
44+
4045
/** The Pubnub Windows context */
4146
struct pubnub_pal {
4247
pb_socket_t socket;

0 commit comments

Comments
 (0)