From 23082349e6ab41a4ad02bf863cfb0af30c984581 Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Mon, 6 Jan 2020 19:29:10 +0000 Subject: [PATCH] common: flush streams before closing them On loaded systems slow tty can give false positive exit failure due pending bytes. Flush the streams that hopefully makes these problems less likely. Reference: https://github.com/iputils/iputils/commit/4655ecc5105c383669ef529f21f3344f99e7372f#commitcomment-36628770 Signed-off-by: Sami Kerola --- iputils_common.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/iputils_common.c b/iputils_common.c index 3adc86e2..58eacd0e 100644 --- a/iputils_common.c +++ b/iputils_common.c @@ -33,13 +33,15 @@ void error(int status, int errnum, const char *format, ...) int close_stream(FILE *stream) { + const int flush_status = fflush(stream); #ifdef HAVE___FPENDING const int some_pending = (__fpending(stream) != 0); #endif const int prev_fail = (ferror(stream) != 0); const int fclose_fail = (fclose(stream) != 0); - if (prev_fail || (fclose_fail && ( + if (flush_status || + prev_fail || (fclose_fail && ( #ifdef HAVE___FPENDING some_pending || #endif