1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-01 04:19:55 +00:00

Be aggressive with setting SO_NOSIGPIPE on BSD/Apple.

This commit is contained in:
Calvin Rose 2020-08-10 18:59:53 -05:00
parent ca75f8dc20
commit 9b36e2b145

View File

@ -139,6 +139,19 @@ static int janet_stream_close(void *p, size_t s) {
return 0; return 0;
} }
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
}
/* /*
* Event loop * Event loop
*/ */
@ -308,6 +321,7 @@ static size_t janet_loop_event(size_t index) {
JSock connfd = accept(fd, NULL, NULL); JSock connfd = accept(fd, NULL, NULL);
if (JSOCKVALID(connfd)) { if (JSOCKVALID(connfd)) {
/* Made a new connection socket */ /* Made a new connection socket */
nosigpipe(connfd);
JanetStream *stream = make_stream(connfd, JANET_STREAM_READABLE | JANET_STREAM_WRITABLE); JanetStream *stream = make_stream(connfd, JANET_STREAM_READABLE | JANET_STREAM_WRITABLE);
Janet streamv = janet_wrap_abstract(stream); Janet streamv = janet_wrap_abstract(stream);
JanetFunction *handler = jlfd->data.read_accept.handler; JanetFunction *handler = jlfd->data.read_accept.handler;
@ -497,18 +511,6 @@ static struct addrinfo *janet_get_addrinfo(Janet *argv, int32_t offset) {
* C Funs * 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) { static Janet cfun_net_connect(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2); janet_fixarity(argc, 2);