Address - prevent sigpipe on client connections.
We erroneously did not set SO_NOSIGPIPE on connections aquired with
net/connect, only those quired thorugh net/server. This meant that
failed writes by a client could send sigpipe.
@@ -497,6 +497,18 @@ static struct addrinfo *janet_get_addrinfo(Janet *argv, int32_t offset) {
* C Funs
*/
+static void nosigpipe(JSock s) {
+#ifdef SO_NOSIGPIPE
+ int enable = 1;
+ if (setsockopt(s, SOL_SOCKET, SO_NOSIGPIPE, &enable, sizeof(int)) < 0) {
+ JSOCKCLOSE(s);
+ janet_panic("setsockopt(SO_NOSIGPIPE) failed");
+ }
+#else
+ (void) s;
+#endif
+}
+
static Janet cfun_net_connect(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
@@ -517,6 +529,8 @@ static Janet cfun_net_connect(int32_t argc, Janet *argv) {
janet_panic("could not connect to socket");
}
+ nosigpipe(sock);
+
/* Wrap socket in abstract type JanetStream */
JanetStream *stream = make_stream(sock, JANET_STREAM_READABLE | JANET_STREAM_WRITABLE);
return janet_wrap_abstract(stream);
@@ -542,12 +556,7 @@ static Janet cfun_net_server(int32_t argc, Janet *argv) {
JSOCKCLOSE(sfd);
janet_panic("setsockopt(SO_REUSEADDR) failed");
}
-#ifdef SO_NOSIGPIPE
- if (setsockopt(sfd, SOL_SOCKET, SO_NOSIGPIPE, &enable, sizeof(int)) < 0) {
- JSOCKCLOSE(sfd);
- janet_panic("setsockopt(SO_NOSIGPIPE) failed");
- }
-#endif
+ nosigpipe(sfd);
#ifdef SO_REUSEPORT
if (setsockopt(sfd, SOL_SOCKET, SO_REUSEPORT, &enable, sizeof(int)) < 0) {
JSOCKCLOSE(sfd);
