Blame SOURCES/0051-debug-don-t-write-newlines-to-memfd.patch

b15ea1
From fa28009b861dc9fd67ddd1a8f5372d10f3f9c663 Mon Sep 17 00:00:00 2001
b15ea1
From: Peter Jones <pjones@redhat.com>
b15ea1
Date: Tue, 15 Oct 2019 16:27:39 -0400
b15ea1
Subject: [PATCH 51/86] debug(): don't write newlines to memfd
b15ea1
b15ea1
If we know our log will only be seen by strace, the newlines don't add
b15ea1
anything to the strings but clutter.
b15ea1
b15ea1
Signed-off-by: Peter Jones <pjones@redhat.com>
b15ea1
---
b15ea1
 src/error.c | 18 +++++++++++++++---
b15ea1
 1 file changed, 15 insertions(+), 3 deletions(-)
b15ea1
b15ea1
diff --git a/src/error.c b/src/error.c
b15ea1
index 083de15e984..b1a56ef629c 100644
b15ea1
--- a/src/error.c
b15ea1
+++ b/src/error.c
b15ea1
@@ -1,6 +1,6 @@
b15ea1
 /*
b15ea1
  * libefiboot - library for the manipulation of EFI boot variables
b15ea1
- * Copyright 2012-2015 Red Hat, Inc.
b15ea1
+ * Copyright 2012-2019 Red Hat, Inc.
b15ea1
  * Copyright (C) 2000-2001 Dell Computer Corporation <Matt_Domsch@dell.com>
b15ea1
  *
b15ea1
  * This library is free software; you can redistribute it and/or
b15ea1
@@ -27,6 +27,7 @@
b15ea1
 #include <stdio.h>
b15ea1
 #include <string.h>
b15ea1
 #include <sys/mman.h>
b15ea1
+#include <sys/random.h>
b15ea1
 #include <unistd.h>
b15ea1
 
b15ea1
 #include "efiboot.h"
b15ea1
@@ -166,6 +167,7 @@ efi_error_pop(void)
b15ea1
 static int efi_verbose;
b15ea1
 static FILE *efi_errlog, *efi_dbglog;
b15ea1
 static int efi_dbglog_fd = -1;
b15ea1
+static intptr_t efi_dbglog_cookie;
b15ea1
 static int log_level;
b15ea1
 static char efi_dbglog_buf[4096];
b15ea1
 
b15ea1
@@ -176,7 +178,7 @@ efi_set_loglevel(int level)
b15ea1
 }
b15ea1
 
b15ea1
 static ssize_t
b15ea1
-dbglog_write(void *cookie UNUSED, const char *buf, size_t size)
b15ea1
+dbglog_write(void *cookie, const char *buf, size_t size)
b15ea1
 {
b15ea1
 	FILE *log = efi_errlog ? efi_errlog : stderr;
b15ea1
 	ssize_t ret = size;
b15ea1
@@ -185,6 +187,11 @@ dbglog_write(void *cookie UNUSED, const char *buf, size_t size)
b15ea1
 		ret = fwrite(buf, 1, size, log);
b15ea1
 	} else if (efi_dbglog_fd >= 0) {
b15ea1
 		lseek(efi_dbglog_fd, 0, SEEK_SET);
b15ea1
+		if ((intptr_t)cookie != 0 &&
b15ea1
+		    (intptr_t)cookie == efi_dbglog_cookie &&
b15ea1
+		    size > 0 &&
b15ea1
+		    buf[size-1] == '\n')
b15ea1
+			size -= 1;
b15ea1
 		ret = write(efi_dbglog_fd, buf, size);
b15ea1
 	}
b15ea1
 	return ret;
b15ea1
@@ -248,6 +255,7 @@ efi_error_fini(void)
b15ea1
 static void CONSTRUCTOR
b15ea1
 efi_error_init(void)
b15ea1
 {
b15ea1
+	ssize_t bytes;
b15ea1
 	cookie_io_functions_t io_funcs = {
b15ea1
 		.write = dbglog_write,
b15ea1
 		.seek = dbglog_seek,
b15ea1
@@ -258,7 +266,11 @@ efi_error_init(void)
b15ea1
 	if (efi_dbglog_fd == -1)
b15ea1
 		return;
b15ea1
 
b15ea1
-	efi_dbglog = fopencookie(NULL, "a", io_funcs);
b15ea1
+	bytes = getrandom(&efi_dbglog_cookie, sizeof(efi_dbglog_cookie), 0);
b15ea1
+	if (bytes < (ssize_t)sizeof(efi_dbglog_cookie))
b15ea1
+		efi_dbglog_cookie = 0;
b15ea1
+
b15ea1
+	efi_dbglog = fopencookie((void *)efi_dbglog_cookie, "a", io_funcs);
b15ea1
 	if (efi_dbglog)
b15ea1
 		setvbuf(efi_dbglog, efi_dbglog_buf, _IOLBF,
b15ea1
 			sizeof(efi_dbglog_buf));
b15ea1
-- 
b15ea1
2.24.1
b15ea1