Skip to content

Commit

Permalink
Limit the writes in the error log to 32-bytes writes...
Browse files Browse the repository at this point in the history
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 <pjones@redhat.com>
  • Loading branch information
vathpela committed Jul 8, 2020
1 parent 7a6e061 commit 36297ad
Showing 1 changed file with 31 additions and 14 deletions.
45 changes: 31 additions & 14 deletions src/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/param.h>
#include <sys/random.h>
#include <unistd.h>

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 36297ad

Please sign in to comment.