Blame SOURCES/kexec-tools-2.0.20-Cleanup-move-it-back-from-util_lib-elf_info.c.patch

bda30f
From a7c4cb8e998571cb3dd62e907935a1e052b15d6c Mon Sep 17 00:00:00 2001
bda30f
From: Lianbo Jiang <lijiang@redhat.com>
bda30f
Date: Fri, 23 Aug 2019 20:05:38 +0800
bda30f
Subject: [PATCH 3/5] Cleanup: move it back from util_lib/elf_info.c
bda30f
bda30f
Some code related to vmcore-dmesg.c is put into the util_lib, which
bda30f
is not very reasonable, so lets move it back and tidy up those code.
bda30f
bda30f
In addition, that will also help to limit the size of vmcore-dmesg.txt
bda30f
in vmcore-dmesg.c instead of elf_info.c.
bda30f
bda30f
Signed-off-by: Lianbo Jiang <lijiang@redhat.com>
bda30f
Signed-off-by: Simon Horman <horms@verge.net.au>
bda30f
---
bda30f
 util_lib/elf_info.c         | 48 +++++++++----------------------------
bda30f
 util_lib/include/elf_info.h |  2 +-
bda30f
 vmcore-dmesg/vmcore-dmesg.c | 30 ++++++++++++++++++++++-
bda30f
 3 files changed, 41 insertions(+), 39 deletions(-)
bda30f
bda30f
diff --git a/util_lib/elf_info.c b/util_lib/elf_info.c
bda30f
index 5d0efaafab53..2bce5cb1713c 100644
bda30f
--- a/util_lib/elf_info.c
bda30f
+++ b/util_lib/elf_info.c
bda30f
@@ -531,19 +531,7 @@ static int32_t read_file_s32(int fd, uint64_t addr)
bda30f
 	return read_file_u32(fd, addr);
bda30f
 }
bda30f
 
bda30f
-static void write_to_stdout(char *buf, unsigned int nr)
bda30f
-{
bda30f
-	ssize_t ret;
bda30f
-
bda30f
-	ret = write(STDOUT_FILENO, buf, nr);
bda30f
-	if (ret != nr) {
bda30f
-		fprintf(stderr, "Failed to write out the dmesg log buffer!:"
bda30f
-			" %s\n", strerror(errno));
bda30f
-		exit(54);
bda30f
-	}
bda30f
-}
bda30f
-
bda30f
-static void dump_dmesg_legacy(int fd)
bda30f
+static void dump_dmesg_legacy(int fd, void (*handler)(char*, unsigned int))
bda30f
 {
bda30f
 	uint64_t log_buf, log_buf_offset;
bda30f
 	unsigned log_end, logged_chars, log_end_wrapped;
bda30f
@@ -604,7 +592,8 @@ static void dump_dmesg_legacy(int fd)
bda30f
 	 */
bda30f
 	logged_chars = log_end < log_buf_len ? log_end : log_buf_len;
bda30f
 
bda30f
-	write_to_stdout(buf + (log_buf_len - logged_chars), logged_chars);
bda30f
+	if (handler)
bda30f
+		handler(buf + (log_buf_len - logged_chars), logged_chars);
bda30f
 }
bda30f
 
bda30f
 static inline uint16_t struct_val_u16(char *ptr, unsigned int offset)
bda30f
@@ -623,7 +612,7 @@ static inline uint64_t struct_val_u64(char *ptr, unsigned int offset)
bda30f
 }
bda30f
 
bda30f
 /* Read headers of log records and dump accordingly */
bda30f
-static void dump_dmesg_structured(int fd)
bda30f
+static void dump_dmesg_structured(int fd, void (*handler)(char*, unsigned int))
bda30f
 {
bda30f
 #define OUT_BUF_SIZE	4096
bda30f
 	uint64_t log_buf, log_buf_offset, ts_nsec;
bda30f
@@ -733,7 +722,8 @@ static void dump_dmesg_structured(int fd)
bda30f
 				out_buf[len++] = c;
bda30f
 
bda30f
 			if (len >= OUT_BUF_SIZE - 64) {
bda30f
-				write_to_stdout(out_buf, len);
bda30f
+				if (handler)
bda30f
+					handler(out_buf, len);
bda30f
 				len = 0;
bda30f
 			}
bda30f
 		}
bda30f
@@ -752,16 +742,16 @@ static void dump_dmesg_structured(int fd)
bda30f
 			current_idx += loglen;
bda30f
 	}
bda30f
 	free(buf);
bda30f
-	if (len)
bda30f
-		write_to_stdout(out_buf, len);
bda30f
+	if (len && handler)
bda30f
+		handler(out_buf, len);
bda30f
 }
bda30f
 
bda30f
-static void dump_dmesg(int fd)
bda30f
+void dump_dmesg(int fd, void (*handler)(char*, unsigned int))
bda30f
 {
bda30f
 	if (log_first_idx_vaddr)
bda30f
-		dump_dmesg_structured(fd);
bda30f
+		dump_dmesg_structured(fd, handler);
bda30f
 	else
bda30f
-		dump_dmesg_legacy(fd);
bda30f
+		dump_dmesg_legacy(fd, handler);
bda30f
 }
bda30f
 
bda30f
 int read_elf(int fd)
bda30f
@@ -808,22 +798,6 @@ int read_elf(int fd)
bda30f
 	return 0;
bda30f
 }
bda30f
 
bda30f
-int read_elf_vmcore(int fd)
bda30f
-{
bda30f
-	int ret;
bda30f
-
bda30f
-	ret = read_elf(fd);
bda30f
-	if (ret > 0) {
bda30f
-		fprintf(stderr, "Unable to read ELF information"
bda30f
-			" from vmcore\n");
bda30f
-		return ret;
bda30f
-	}
bda30f
-
bda30f
-	dump_dmesg(fd);
bda30f
-
bda30f
-	return 0;
bda30f
-}
bda30f
-
bda30f
 int read_phys_offset_elf_kcore(int fd, unsigned long *phys_off)
bda30f
 {
bda30f
 	int ret;
bda30f
diff --git a/util_lib/include/elf_info.h b/util_lib/include/elf_info.h
bda30f
index c328a1b0ecf2..4bc9279ba603 100644
bda30f
--- a/util_lib/include/elf_info.h
bda30f
+++ b/util_lib/include/elf_info.h
bda30f
@@ -30,6 +30,6 @@ int get_pt_load(int idx,
bda30f
 	unsigned long long *virt_end);
bda30f
 int read_phys_offset_elf_kcore(int fd, unsigned long *phys_off);
bda30f
 int read_elf(int fd);
bda30f
-int read_elf_vmcore(int fd);
bda30f
+void dump_dmesg(int fd, void (*handler)(char*, unsigned int));
bda30f
 
bda30f
 #endif /* ELF_INFO_H */
bda30f
diff --git a/vmcore-dmesg/vmcore-dmesg.c b/vmcore-dmesg/vmcore-dmesg.c
bda30f
index bebc348a657e..fe7df8ec372c 100644
bda30f
--- a/vmcore-dmesg/vmcore-dmesg.c
bda30f
+++ b/vmcore-dmesg/vmcore-dmesg.c
bda30f
@@ -5,6 +5,34 @@ typedef Elf32_Nhdr Elf_Nhdr;
bda30f
 
bda30f
 extern const char *fname;
bda30f
 
bda30f
+static void write_to_stdout(char *buf, unsigned int nr)
bda30f
+{
bda30f
+	ssize_t ret;
bda30f
+
bda30f
+	ret = write(STDOUT_FILENO, buf, nr);
bda30f
+	if (ret != nr) {
bda30f
+		fprintf(stderr, "Failed to write out the dmesg log buffer!:"
bda30f
+			" %s\n", strerror(errno));
bda30f
+		exit(54);
bda30f
+	}
bda30f
+}
bda30f
+
bda30f
+static int read_vmcore_dmesg(int fd, void (*handler)(char*, unsigned int))
bda30f
+{
bda30f
+	int ret;
bda30f
+
bda30f
+	ret = read_elf(fd);
bda30f
+	if (ret > 0) {
bda30f
+		fprintf(stderr, "Unable to read ELF information"
bda30f
+			" from vmcore\n");
bda30f
+		return ret;
bda30f
+	}
bda30f
+
bda30f
+	dump_dmesg(fd, handler);
bda30f
+
bda30f
+	return 0;
bda30f
+}
bda30f
+
bda30f
 int main(int argc, char **argv)
bda30f
 {
bda30f
 	ssize_t ret;
bda30f
@@ -23,7 +51,7 @@ int main(int argc, char **argv)
bda30f
 		return 2;
bda30f
 	}
bda30f
 
bda30f
-	ret = read_elf_vmcore(fd);
bda30f
+	ret = read_vmcore_dmesg(fd, write_to_stdout);
bda30f
 	
bda30f
 	close(fd);
bda30f
 
bda30f
-- 
bda30f
2.17.1
bda30f