From 243163d9a20ed9961f6c2eefe59b411b5c39d707 Mon Sep 17 00:00:00 2001 From: Pavel Rochnyack Date: Mon, 31 Jul 2017 12:21:24 +0700 Subject: [PATCH] Proper return value from 'swrite()' when connection has been closed According to POSIX, errno is set only if 'recv()' returns -1. When connection has been closed, 'recv()' returns 0 and errno left untouched. For functions which check errno value after 'swrite()', errno now is set to ECONNRESET, so they produce correct message 'Connection reset by peer'. --- src/daemon/common.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/daemon/common.c b/src/daemon/common.c index 3ae61d85..6c856a6b 100644 --- a/src/daemon/common.c +++ b/src/daemon/common.c @@ -269,7 +269,8 @@ ssize_t swrite(int fd, const void *buf, size_t count) { if (recv(fd, buffer, sizeof(buffer), MSG_PEEK | MSG_DONTWAIT) == 0) { /* if recv returns zero (even though poll() said there is data to be * read), that means the connection has been closed */ - return errno ? errno : -1; + errno = ECONNRESET; + return -1; } } -- 2.11.0