From 473dec9064026f3d8d676d8c5553bef12847cd97 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sun, 28 Aug 2022 15:01:22 +0200 Subject: net: use MSG_NOSIGNAL Apparently, if you try to write() into a socket with the other party already gone, your process receives a SIGPIPE. Wtf? --- src/net.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/net.c b/src/net.c index 4ff7a4a..5c44c70 100644 --- a/src/net.c +++ b/src/net.c @@ -130,18 +130,27 @@ int net_connect(const char *host, const char *port) return socket_fd; } +static ssize_t net_send(int fd, const void *buf, size_t len) +{ + static const int flags = MSG_NOSIGNAL; + + ssize_t ret = send(fd, buf, len, flags); + if (ret < 0) { + print_errno("send"); + return -1; + } + + return ret; +} + int net_send_all(int fd, const void *buf, size_t len) { size_t sent_total = 0; while (sent_total < len) { - ssize_t sent_now = write(fd, (const char *)buf + sent_total, len - sent_total); - - if (sent_now < 0) { - print_errno("write"); + ssize_t sent_now = net_send(fd, (const char *)buf + sent_total, len - sent_total); + if (sent_now < 0) return -1; - } - sent_total += sent_now; } -- cgit v1.2.3