From: Tobias Stoeckmann Date: Thu, 19 Feb 2026 17:14:23 +0000 (+0100) Subject: dmesg: Gracefully handle EPIPE errors X-Git-Tag: v2.43-devel~53^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=01745e8593420f864d0904b5a4913796e513f97d;p=thirdparty%2Futil-linux.git dmesg: Gracefully handle EPIPE errors Just calling exit with a success return value in case of an EPIPE error in safe_fwrite is not precise enough. If dmesg was called with "-c", i.e. to print and then clear the ring buffer, a successful return value should mean that the ring buffer is cleared as well. Instead, continue operation on EPIPE error but stop any further regular output. Signed-off-by: Tobias Stoeckmann --- diff --git a/sys-utils/dmesg.c b/sys-utils/dmesg.c index b6f506034..cd042f549 100644 --- a/sys-utils/dmesg.c +++ b/sys-utils/dmesg.c @@ -821,11 +821,8 @@ doprint: } else rc = fwrite(p, 1, len, out) != len; - if (rc != 0) { - if (errno != EPIPE) - err(EXIT_FAILURE, _("write failed")); - exit(EXIT_SUCCESS); - } + if (rc != 0 && errno != EPIPE) + err(EXIT_FAILURE, _("write failed")); } } @@ -1012,7 +1009,7 @@ static void raw_print(struct dmesg_control *ctl, const char *buf, size_t size) /* * Print file in small chunks to save memory */ - while (size) { + while (size && !ferror(stdout)) { size_t sz = size > ctl->pagesize ? ctl->pagesize : size; char *x = ctl->mmap_buff; @@ -1380,7 +1377,7 @@ static void print_buffer(struct dmesg_control *ctl, return; } - while (get_next_syslog_record(ctl, &rec) == 0) + while (get_next_syslog_record(ctl, &rec) == 0 && !ferror(stdout)) print_record(ctl, &rec); } @@ -1549,7 +1546,7 @@ static int print_kmsg(struct dmesg_control *ctl) */ sz = ctl->kmsg_first_read; - while (sz > 0) { + while (sz > 0 && !ferror(stdout)) { *(ctl->kmsg_buf + sz) = '\0'; /* for debug messages */ if (parse_kmsg_record(ctl, &rec, @@ -1571,7 +1568,7 @@ static int print_kmsg_file(struct dmesg_control *ctl, size_t sz) if (ctl->method != DMESG_METHOD_KMSG || !ctl->filename) return -1; - while (sz > 0) { + while (sz > 0 && !ferror(stdout)) { len = strnlen(ctl->mmap_buff, sz); if (len > sizeof(str)) errx(EXIT_FAILURE, _("record too large"));