diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2022-08-28 15:01:22 +0200 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2022-08-28 15:01:22 +0200 |
commit | 473dec9064026f3d8d676d8c5553bef12847cd97 (patch) | |
tree | ec17073aea9e844901a633e459e302e4fb60ddb3 | |
parent | server: shutting down more gracefully (diff) | |
download | cimple-473dec9064026f3d8d676d8c5553bef12847cd97.tar.gz cimple-473dec9064026f3d8d676d8c5553bef12847cd97.zip |
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?
-rw-r--r-- | src/net.c | 21 |
1 files changed, 15 insertions, 6 deletions
@@ -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; } |