From 36297adcb266f07bb06e725a0da377bc6e6aedd0 Mon Sep 17 00:00:00 2001 From: Peter Jones Date: Thu, 30 Apr 2020 10:37:19 -0400 Subject: [PATCH] Limit the writes in the error log to 32-bytes writes... Since strace defaults to 32-bytes per string, the debug log looks like: 3054 write(16, "linux-nvme.c:84 parse_nvme(): cu"..., 87) = 87 3054 write(16, "linux.c:556 device_get(): dev_pr"..., 79) = 79 3054 write(16, "linux.c:521 device_get(): trying"..., 36) = 36 3054 write(16, "linux-ata.c:54 parse_ata(): entr"..., 33) = 33 Signed-off-by: Peter Jones --- src/error.c | 45 +++++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/src/error.c b/src/error.c index 7250b088..56756145 100644 --- a/src/error.c +++ b/src/error.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -160,19 +161,35 @@ static ssize_t dbglog_write(void *cookie, const char *buf, size_t size) { FILE *log = efi_errlog ? efi_errlog : stderr; - ssize_t ret = size; - - if (efi_get_verbose() >= log_level) { - ret = fwrite(buf, 1, size, log); - } else if (efi_dbglog_fd >= 0) { - if (efi_dbglog_memfd) - lseek(efi_dbglog_fd, 0, SEEK_SET); - if ((intptr_t)cookie != 0 && - (intptr_t)cookie == efi_dbglog_cookie && - size > 0 && - buf[size-1] == '\n') - size -= 1; - ret = write(efi_dbglog_fd, buf, size); + ssize_t ret = 0; + + while (ret < (ssize_t)size) { + /* + * This is limited to 32 characters per write because if + * the only place this is going is strace logs, that's the + * default character buffer display size. If it's going + * anywhere else, you won't really notice the difference, + * since we're not inserting newlines. + */ + ssize_t sz = MIN(size - ret, 32); + + if (efi_get_verbose() >= log_level) { + sz = fwrite(buf + ret, 1, sz, log); + if (sz < 1 && (ferror(log) || feof(log))) + break; + } else if (efi_dbglog_fd >= 0 && sz > 0) { + if (efi_dbglog_memfd) + lseek(efi_dbglog_fd, 0, SEEK_SET); + if ((intptr_t)cookie != 0 && + (intptr_t)cookie == efi_dbglog_cookie && + (ret + sz) < 0 && + buf[ret + sz - 1] == '\n') + sz -= 1; + sz = write(efi_dbglog_fd, buf + ret, sz); + if (sz < 0) + break; + } + ret += sz; } return ret; } @@ -258,7 +275,7 @@ efi_error_init(void) efi_dbglog_cookie = 0; efi_dbglog = fopencookie((void *)efi_dbglog_cookie, "a", io_funcs); - if (efi_dbglog) + if (efi_dbglog && efi_dbglog_memfd) setvbuf(efi_dbglog, efi_dbglog_buf, _IOLBF, sizeof(efi_dbglog_buf)); #endif