Blame SOURCES/0004-coredump-fix-unexpected-truncation-of-generated-core.patch

bd4871
From dbb542e10bfe1b2e21c7927bda9be1d301cfef65 Mon Sep 17 00:00:00 2001
bd4871
From: HATAYAMA Daisuke <d.hatayama@fujitsu.com>
bd4871
Date: Fri, 17 Jun 2022 20:38:19 +0900
bd4871
Subject: [PATCH 4/5] coredump: fix unexpected truncation of generated core
bd4871
 files
bd4871
bd4871
Core files generated by crash gcore command are sometimes unexpectedly
bd4871
truncated. Then, we can get aware of this from the following warning
bd4871
message output by gdb:
bd4871
bd4871
    BFD: warning: /root/./core.1.systemd is truncated: expected core file size >= 43606016, found: 43597824
bd4871
bd4871
From the investigation, it turned out that this truncation is
bd4871
occurring when there is no write() operation after the area skipped by
bd4871
lseek(). Holes are generated only when there is write() operation.
bd4871
bd4871
To fix this issue, use ftruncate() to allocate holes explicitly.
bd4871
bd4871
Signed-off-by: HATAYAMA Daisuke <d.hatayama@fujitsu.com>
bd4871
Signed-off-by: Lianbo Jiang <lijiang@redhat.com>
bd4871
---
bd4871
 src/libgcore/gcore_coredump.c | 15 +++++++++++++++
bd4871
 1 file changed, 15 insertions(+)
bd4871
bd4871
diff --git a/src/libgcore/gcore_coredump.c b/src/libgcore/gcore_coredump.c
bd4871
index 424b0a40a42b..27086d91468b 100644
bd4871
--- a/src/libgcore/gcore_coredump.c
bd4871
+++ b/src/libgcore/gcore_coredump.c
bd4871
@@ -331,6 +331,21 @@ void gcore_coredump(void)
bd4871
 	}
bd4871
 	progressf("done.\n");
bd4871
 
bd4871
+	/*
bd4871
+	 * Use ftruncate() to generate holes explicitly, or core file
bd4871
+	 * gets truncated if there is no write() operation after the
bd4871
+	 * area skipped by lseek().
bd4871
+	 */
bd4871
+	if (fflush(gcore->fp))
bd4871
+		error(FATAL, "%s: fflush: %s\n",
bd4871
+		      gcore->corename,
bd4871
+		      strerror(errno));
bd4871
+
bd4871
+	if (ftruncate(fileno(gcore->fp), ftell(gcore->fp)) < 0)
bd4871
+		error(FATAL, "%s: ftruncate: %s\n",
bd4871
+		      gcore->corename,
bd4871
+		      strerror(errno));
bd4871
+
bd4871
 	gcore->flags |= GCF_SUCCESS;
bd4871
 
bd4871
 }
bd4871
-- 
bd4871
2.37.1
bd4871