From nobody Mon Apr 18 15:57:03 2022 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id AA37011C9A39; Mon, 18 Apr 2022 15:57:04 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Khs4w0YtJz4mbD; Mon, 18 Apr 2022 15:57:04 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1650297424; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=LQzRwu7BGvvSGdKOWc34UavkIv5fL6NI2FIj7WGv/N4=; b=uNdqspxnazk9Np7L3hbZhGztqBq91yOdZPkAmnJhgjaFcyOO7iI/qqlxS41dU4sYwg88Wg YrVb36kAHglk6zGkXU3QPz7M8UkmHqMEudyZSo8FSeTVK/wyuP1DzAnua4Cy4Oh97W7wLL PfpPd3ivQcot/C7Q/QKB2Gjw5v3N42tSa7J2yFRLvAynkTH/7TNhhKQ1iFGq/jDor3NOdE YO+ghhq1XQpaxxacpxHpQD74fl5sdFv9FnVVyZzlNaevem7Y1pkgco1oryXLGBX5uNL6F3 dO+7qeiOx2B55vAwvL7Igp9UWu9uSgkop4rbcNj/p0VOxSwsegNkWG/+3vqDcg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id D28CD15C81; Mon, 18 Apr 2022 15:57:03 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 23IFv3PI031485; Mon, 18 Apr 2022 15:57:03 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 23IFv3SM031484; Mon, 18 Apr 2022 15:57:03 GMT (envelope-from git) Date: Mon, 18 Apr 2022 15:57:03 GMT Message-Id: <202204181557.23IFv3SM031484@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mitchell Horne Subject: git: cf02cf8dbfe7 - main - savecore: factor out info file handling List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: mhorne X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: cf02cf8dbfe718e1f14cec0cce4663812c0e26f1 Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1650297424; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=LQzRwu7BGvvSGdKOWc34UavkIv5fL6NI2FIj7WGv/N4=; b=DzPjmU2JpMll5eFod+e8rBTry6AdvOnVRviAEYKviDnoOWQmFeBNFtMAuTQ+EAZZjhgAb6 AwQ2rhOATUrNtMNBveGmkjRkq15oBF9IWiIO1bcJTuuecaElc3TSAmQIRnFje46Yfoiub8 RfcCglAtqYTg78CAxDE8+apZRc2KU6qFIUls6AyJT0mjUI1vM9Y6FtaLWAZLCLXkjor+jA btSh3u47jl2u4I3TGQXxBgKa73XI2fLacO5Sg5WS8ZieFCBQqhrH+RKucm7/WvoViy5eh8 9sGs5gl44grfRe9XvUFN0e6zon9d/l3tSJtnFCaCJLtIRMNVxFfmsx/px5QPLA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1650297424; a=rsa-sha256; cv=none; b=PM6iWATW+bL2a3ZLlpalkBI4sny86eEwTSZNGUDygnunk533Y9/e5Pteo60o183AcEDO5D j3aSUiHdg4obpXDo2DbuPKPwdfWUKoWcGUgb7WEPEXRdUeFJ+q94dijCeHoTO7/DuT9Wwj zRS7lpLS4ZlL3wjARowsPgGcLvUjNC601DNnhNaIG07uYVnV1b2PVpN8WohXPgqVrelF0a fSMsXnkBR4RfTzbzQr3Kk2DcMumIBjVvwgZHPB6LXuH5Pvj5veXCgXy49yjL2o7GKyxuS6 dqxlx3AEGk3KNbgbGoZc2N2m6rm9EEJgm3wNAiiOGqhI8TeMMlK1YsUglLb4zA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by mhorne: URL: https://cgit.FreeBSD.org/src/commit/?id=cf02cf8dbfe718e1f14cec0cce4663812c0e26f1 commit cf02cf8dbfe718e1f14cec0cce4663812c0e26f1 Author: Mitchell Horne AuthorDate: 2022-04-18 15:21:36 +0000 Commit: Mitchell Horne CommitDate: 2022-04-18 15:56:15 +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 --- 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 84676e1c79cb..8b3a860f4086 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);