From 4469bd7b53acea642915e2bf0e3cb5dfa378a47e Mon Sep 17 00:00:00 2001
From: Willy Tarreau <w@1wt.eu>
Date: Tue, 3 Jul 2018 12:10:31 -0400
Subject: [PATCH] tools: kwboot: properly quit when read() returns 0

When kwboot is attached to a terminal which disappears such as one
connected via an unplugged USB cable, read() returns 0, making kwboot
loop until a key is pressed in the terminal. The only case where read()
may return 0 here is when the terminal is closed anyway, so let's
properly handle this one and report is similar to other errors.

Signed-off-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Stefan Roese <sr@denx.de>
---
 tools/kwboot.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tools/kwboot.c b/tools/kwboot.c
index 0a77060288..50ae2b4b77 100644
--- a/tools/kwboot.c
+++ b/tools/kwboot.c
@@ -182,7 +182,7 @@ kwboot_tty_recv(int fd, void *buf, size_t len, int timeo)
 		}
 
 		n = read(fd, buf, len);
-		if (n < 0)
+		if (n <= 0)
 			goto out;
 
 		buf = (char *)buf + n;
@@ -466,7 +466,7 @@ kwboot_term_pipe(int in, int out, char *quit, int *s)
 	char _buf[128], *buf = _buf;
 
 	nin = read(in, buf, sizeof(buf));
-	if (nin < 0)
+	if (nin <= 0)
 		return -1;
 
 	if (quit) {
-- 
2.39.5