From nobody Sun Feb 8 23:06:11 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) client-ip=170.10.129.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1644491658; cv=none; d=zohomail.com; s=zohoarc; b=L14BQJLaOoieI/0Z29S5vrrHf2v7NgGGDHWrcMPt77yyqQYY2pmL5OB+Un3jCObumiWMpw3R15APrSnrIv/mCoGSLDvX0r4KLtW5My1AaeeN4beiQFmcfpWN8ia3WsfFMyqw74PCSlw6LMnChlh7GbeIYHTSxrcm/2XG5OVq1qQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1644491658; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=nUfxKl8VAlA1PO+6y3VdaIN02W0QR4Pn8C8GPMgZw/U=; b=FJ+8Vd8MXpT59hhR736JuWznDzlsJ375xbwENXHp/ym7YHR+VW9QE2KlSwB6J4mEksSG0i4CyI6gyf5tKOTRM1IqcgBOkgptv0m/0CZWqEjZjEfF5ULe+FBhWbH1XbTPBbRP5Gm77bIxvSZdOfPMSBHEXCbxcS7PsjYaB8PJFb0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mx.zohomail.com with SMTPS id 1644491658610157.56659697847283; Thu, 10 Feb 2022 03:14:18 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-491-ymtP-aXWNh-SpcXBc1HBiQ-1; Thu, 10 Feb 2022 06:14:15 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id BA0661091DB6; Thu, 10 Feb 2022 11:14:09 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 93D436E1E2; Thu, 10 Feb 2022 11:14:09 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 38ACA4A7C8; Thu, 10 Feb 2022 11:14:09 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 21ABDnXg018795 for ; Thu, 10 Feb 2022 06:13:49 -0500 Received: by smtp.corp.redhat.com (Postfix) id 1571910589DD; Thu, 10 Feb 2022 11:13:49 +0000 (UTC) Received: from maggie.redhat.com (unknown [10.43.2.56]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9450C10589DA for ; Thu, 10 Feb 2022 11:13:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1644491658; h=from:from:sender:sender: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: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=nUfxKl8VAlA1PO+6y3VdaIN02W0QR4Pn8C8GPMgZw/U=; b=Fd1te1qwPdv+IS5XXCwIZxn9YbfWtrv3f+q82Iu3nNbMe/Xc9a8vyJDrelI5TxGNmc8TYd xa9xmKyEnmYeAw2vh29TggFcdLFxAfDth2Q/EzslzDBMfIpVGjdoIergMTitWkjxCPsyZD NR0BUbBiX0h+hp82572iPy6mCBcJaW0= X-MC-Unique: ymtP-aXWNh-SpcXBc1HBiQ-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 5/5] qemuPrepareNVRAM: Us virFileRewrite() to write NVRAM Date: Thu, 10 Feb 2022 12:13:26 +0100 Message-Id: <5c4225b39559f3edae53dd66df0681a4de40a10a.1644491535.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1644491660533100005 Content-Type: text/plain; charset="utf-8" After previous commits there is no need for qemuPrepareNVRAM() to open code virFileRewrite(). Deduplicate the code by calling the function. Signed-off-by: Michal Privoznik --- src/qemu/qemu_process.c | 118 +++++++++++++--------------------------- 1 file changed, 39 insertions(+), 79 deletions(-) diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index 8fccf6b760..500a91f153 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -4421,6 +4421,40 @@ qemuProcessUpdateCPU(virQEMUDriver *driver, } =20 =20 +static int +qemuPrepareNVRAMHelper(int dstFD, + const void *opaque) +{ + const char *master_nvram_path =3D opaque; + VIR_AUTOCLOSE srcFD =3D -1; + ssize_t r; + + if ((srcFD =3D virFileOpenAs(master_nvram_path, O_RDONLY, + 0, -1, -1, 0)) < 0) { + virReportSystemError(-srcFD, + _("Failed to open file '%s'"), + master_nvram_path); + return -2; + } + + do { + char buf[1024]; + + if ((r =3D saferead(srcFD, buf, sizeof(buf))) < 0) { + virReportSystemError(errno, + _("Unable to read from file '%s'"), + master_nvram_path); + return -2; + } + + if (safewrite(dstFD, buf, r) < 0) + return -1; + } while (r); + + return 0; +} + + static int qemuPrepareNVRAM(virQEMUDriver *driver, virDomainObj *vm, @@ -4428,13 +4462,8 @@ qemuPrepareNVRAM(virQEMUDriver *driver, { g_autoptr(virQEMUDriverConfig) cfg =3D virQEMUDriverGetConfig(driver); int ret =3D -1; - int srcFD =3D -1; - int dstFD =3D -1; virDomainLoaderDef *loader =3D vm->def->os.loader; - bool created =3D false; const char *master_nvram_path; - ssize_t r; - g_autofree char *tmp_dst_path =3D NULL; =20 if (!loader || !loader->nvram || (virFileExists(loader->nvram) && !reset_nvram)) @@ -4458,84 +4487,15 @@ qemuPrepareNVRAM(virQEMUDriver *driver, goto cleanup; } =20 - if ((srcFD =3D virFileOpenAs(master_nvram_path, O_RDONLY, - 0, -1, -1, 0)) < 0) { - virReportSystemError(-srcFD, - _("Failed to open file '%s'"), - master_nvram_path); + if (virFileRewrite(loader->nvram, + S_IRUSR | S_IWUSR, + cfg->user, cfg->group, + qemuPrepareNVRAMHelper, + master_nvram_path) < 0) goto cleanup; - } - - tmp_dst_path =3D g_strdup_printf("%s.tmp", loader->nvram); - if ((dstFD =3D virFileOpenAs(tmp_dst_path, - O_WRONLY | O_CREAT | O_EXCL, - S_IRUSR | S_IWUSR, - cfg->user, cfg->group, - VIR_FILE_OPEN_FORCE_OWNER)) < 0) { - virReportSystemError(-dstFD, - _("Failed to create file '%s'"), - tmp_dst_path); - goto cleanup; - } - - created =3D true; - - do { - char buf[1024]; - - if ((r =3D saferead(srcFD, buf, sizeof(buf))) < 0) { - virReportSystemError(errno, - _("Unable to read from file '%s'"), - master_nvram_path); - goto cleanup; - } - - if (safewrite(dstFD, buf, r) < 0) { - virReportSystemError(errno, - _("Unable to write to file '%s'"), - tmp_dst_path); - goto cleanup; - } - } while (r); - - if (VIR_CLOSE(srcFD) < 0) { - virReportSystemError(errno, - _("Unable to close file '%s'"), - master_nvram_path); - goto cleanup; - } - - if (g_fsync(dstFD) < 0) { - virReportSystemError(errno, _("cannot sync file '%s'"), - tmp_dst_path); - goto cleanup; - } - - if (VIR_CLOSE(dstFD) < 0) { - virReportSystemError(errno, - _("Unable to close file '%s'"), - tmp_dst_path); - goto cleanup; - } - - if (rename(tmp_dst_path, loader->nvram) < 0) { - virReportSystemError(errno, - _("Unable to replace '%s'"), - loader->nvram); - goto cleanup; - } =20 ret =3D 0; cleanup: - /* We successfully generated the nvram path, but failed to - * copy the file content. Roll back. */ - if (ret < 0) { - if (created) - unlink(tmp_dst_path); - } - - VIR_FORCE_CLOSE(srcFD); - VIR_FORCE_CLOSE(dstFD); return ret; } =20 --=20 2.34.1