aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/net.c
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2022-08-28 15:01:22 +0200
committerEgor Tensin <Egor.Tensin@gmail.com>2022-08-28 15:01:22 +0200
commit473dec9064026f3d8d676d8c5553bef12847cd97 (patch)
treeec17073aea9e844901a633e459e302e4fb60ddb3 /src/net.c
parentserver: shutting down more gracefully (diff)
downloadcimple-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?
Diffstat (limited to 'src/net.c')
-rw-r--r--src/net.c21
1 files changed, 15 insertions, 6 deletions
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;
}