git: 0decce4326ec - stable/13 - savecore: factor out info file handling

From: Mitchell Horne <mhorne_at_FreeBSD.org>
Date: Mon, 16 May 2022 13:51:20 UTC
The branch stable/13 has been updated by mhorne:

URL: https://cgit.FreeBSD.org/src/commit/?id=0decce4326ecec59372ecfa64642d8e1235ca815

commit 0decce4326ecec59372ecfa64642d8e1235ca815
Author:     Mitchell Horne <mhorne@FreeBSD.org>
AuthorDate: 2022-04-18 15:21:36 +0000
Commit:     Mitchell Horne <mhorne@FreeBSD.org>
CommitDate: 2022-05-16 13:34:03 +0000

    savecore: factor out info file handling
    
    Move it to a separate function, allowing its reuse.
    
    Reviewed by:    markj
    MFC after:      3 days
    Sponsored by:   Juniper Networks, Inc.
    Sponsored by:   Klara, Inc.
    Differential Revision:  https://reviews.freebsd.org/D34822
    
    (cherry picked from commit cf02cf8dbfe718e1f14cec0cce4663812c0e26f1)
---
 sbin/savecore/savecore.c | 75 +++++++++++++++++++++++++++---------------------
 1 file changed, 43 insertions(+), 32 deletions(-)

diff --git a/sbin/savecore/savecore.c b/sbin/savecore/savecore.c
index 5e7a7115b0ad..4444b845babb 100644
--- a/sbin/savecore/savecore.c
+++ b/sbin/savecore/savecore.c
@@ -304,6 +304,43 @@ writekey(int savedirfd, const char *keyname, uint8_t *dumpkey,
 	return (true);
 }
 
+static int
+write_header_info(xo_handle_t *xostdout, const struct kerneldumpheader *kdh,
+    int savedirfd, const char *infoname, const char *device, int bounds,
+    int status)
+{
+	xo_handle_t *xoinfo;
+	FILE *info;
+
+	/*
+	 * Create or overwrite any existing dump header files.
+	 */
+	if ((info = xfopenat(savedirfd, infoname,
+	    O_WRONLY | O_CREAT | O_TRUNC, "w", 0600)) == NULL) {
+		logmsg(LOG_ERR, "open(%s): %m", infoname);
+		return (-1);
+	}
+
+	xoinfo = xo_create_to_file(info, xo_get_style(NULL), 0);
+	if (xoinfo == NULL) {
+		logmsg(LOG_ERR, "%s: %m", infoname);
+		fclose(info);
+		return (-1);
+	}
+	xo_open_container_h(xoinfo, "crashdump");
+
+	if (verbose)
+		printheader(xostdout, kdh, device, bounds, status);
+
+	printheader(xoinfo, kdh, device, bounds, status);
+	xo_close_container_h(xoinfo, "crashdump");
+	xo_flush_h(xoinfo);
+	xo_finish_h(xoinfo);
+	fclose(info);
+
+	return (0);
+}
+
 static off_t
 file_size(int savedirfd, const char *path)
 {
@@ -698,16 +735,16 @@ static void
 DoFile(const char *savedir, int savedirfd, const char *device)
 {
 	static char *buf = NULL;
-	xo_handle_t *xostdout, *xoinfo;
+	xo_handle_t *xostdout;
 	char infoname[32], corename[32], linkname[32], keyname[32];
 	char *temp = NULL;
 	struct kerneldumpheader kdhf, kdhl;
 	uint8_t *dumpkey;
 	off_t mediasize, dumpextent, dumplength, firsthd, lasthd;
-	FILE *core, *info;
+	FILE *core;
 	int fdcore, fddev, error;
 	int bounds, status;
-	u_int sectorsize, xostyle;
+	u_int sectorsize;
 	uint32_t dumpkeysize;
 	bool iscompressed, isencrypted, istextdump, ret;
 
@@ -915,18 +952,6 @@ DoFile(const char *savedir, int savedirfd, const char *device)
 
 	saved_dump_remove(savedirfd, bounds);
 
-	snprintf(infoname, sizeof(infoname), "info.%d", bounds);
-
-	/*
-	 * Create or overwrite any existing dump header files.
-	 */
-	if ((info = xfopenat(savedirfd, infoname,
-	    O_WRONLY | O_CREAT | O_TRUNC, "w", 0600)) == NULL) {
-		logmsg(LOG_ERR, "open(%s): %m", infoname);
-		nerr++;
-		goto closefd;
-	}
-
 	isencrypted = (dumpkeysize > 0);
 	if (compress)
 		snprintf(corename, sizeof(corename), "%s.%d.gz",
@@ -943,7 +968,6 @@ DoFile(const char *savedir, int savedirfd, const char *device)
 	    0600);
 	if (fdcore < 0) {
 		logmsg(LOG_ERR, "open(%s): %m", corename);
-		fclose(info);
 		nerr++;
 		goto closefd;
 	}
@@ -955,30 +979,17 @@ DoFile(const char *savedir, int savedirfd, const char *device)
 	if (core == NULL) {
 		logmsg(LOG_ERR, "%s: %m", corename);
 		(void)close(fdcore);
-		(void)fclose(info);
 		nerr++;
 		goto closefd;
 	}
 	fdcore = -1;
 
-	xostyle = xo_get_style(NULL);
-	xoinfo = xo_create_to_file(info, xostyle, 0);
-	if (xoinfo == NULL) {
-		logmsg(LOG_ERR, "%s: %m", infoname);
-		fclose(info);
+	snprintf(infoname, sizeof(infoname), "info.%d", bounds);
+	if (write_header_info(xostdout, &kdhl, savedirfd, infoname, device,
+	    bounds, status) != 0) {
 		nerr++;
 		goto closeall;
 	}
-	xo_open_container_h(xoinfo, "crashdump");
-
-	if (verbose)
-		printheader(xostdout, &kdhl, device, bounds, status);
-
-	printheader(xoinfo, &kdhl, device, bounds, status);
-	xo_close_container_h(xoinfo, "crashdump");
-	xo_flush_h(xoinfo);
-	xo_finish_h(xoinfo);
-	fclose(info);
 
 	if (isencrypted) {
 		dumpkey = calloc(1, dumpkeysize);