From fb8031ed6ef076c68d2e336dd156aff52bf622ed Mon Sep 17 00:00:00 2001 From: Marco Bonino Date: Tue, 10 Sep 2024 14:56:18 +0300 Subject: [PATCH] Fix dtc_output read when interrupted by EINTR The read() call may be interrupted by a signal, resulting in an EINTR error. In this case, read() will return -1 and set errno to EINTR. This is a common issue when dealing with I/O in a multi-process environment. Check the error code returned by errno. If it is EINTR, we can safely retry the read(). --- riscv/dts.cc | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/riscv/dts.cc b/riscv/dts.cc index 9751ffeee1..55150f8121 100644 --- a/riscv/dts.cc +++ b/riscv/dts.cc @@ -158,9 +158,11 @@ static std::string dtc_compile(const std::string& dtc_input, bool compile) int got; char buf[4096]; - while ((got = read(dtc_output_pipe[0], buf, sizeof(buf))) > 0) { - dtc_output.write(buf, got); - } + do { + while ((got = read(dtc_output_pipe[0], buf, sizeof(buf))) > 0) { + dtc_output.write(buf, got); + } + } while (got == -1 && errno == EINTR); // Retry the read() if it was interrupted by a signal if (got == -1) { std::cerr << "Failed to read dtc_output: " << strerror(errno) << std::endl; exit(1);