From nobody Sat Apr 27 17:01:32 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1518445637946901.9409353244598; Mon, 12 Feb 2018 06:27:17 -0800 (PST) Received: from localhost ([::1]:37586 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1elF4e-0006R3-W0 for importer@patchew.org; Mon, 12 Feb 2018 09:27:17 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52207) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1elF2y-0005a3-FK for qemu-devel@nongnu.org; Mon, 12 Feb 2018 09:25:35 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1elF2v-0003gM-Np for qemu-devel@nongnu.org; Mon, 12 Feb 2018 09:25:32 -0500 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:34218) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1elF2v-0003fM-Ej for qemu-devel@nongnu.org; Mon, 12 Feb 2018 09:25:29 -0500 Received: from pps.filterd (m0098409.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w1CEPRpU137154 for ; Mon, 12 Feb 2018 09:25:28 -0500 Received: from e34.co.us.ibm.com (e34.co.us.ibm.com [32.97.110.152]) by mx0a-001b2d01.pphosted.com with ESMTP id 2g3c5shfjv-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Mon, 12 Feb 2018 09:25:27 -0500 Received: from localhost by e34.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 12 Feb 2018 07:25:25 -0700 Received: from b03cxnp08028.gho.boulder.ibm.com (9.17.130.20) by e34.co.us.ibm.com (192.168.1.134) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 12 Feb 2018 07:25:22 -0700 Received: from b03ledav004.gho.boulder.ibm.com (b03ledav004.gho.boulder.ibm.com [9.17.130.235]) by b03cxnp08028.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id w1CEPLx213107648; Mon, 12 Feb 2018 07:25:21 -0700 Received: from b03ledav004.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id D9F8878038; Mon, 12 Feb 2018 07:25:21 -0700 (MST) Received: from localhost.localdomain (unknown [9.85.192.217]) by b03ledav004.gho.boulder.ibm.com (Postfix) with ESMTP id 1B65E78037; Mon, 12 Feb 2018 07:25:19 -0700 (MST) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Mon, 12 Feb 2018 12:25:06 -0200 X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180212142506.28445-1-danielhb@linux.vnet.ibm.com> References: <20180212142506.28445-1-danielhb@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 18021214-0016-0000-0000-0000083DF431 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00008522; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000251; SDB=6.00988668; UDB=6.00501947; IPR=6.00768022; BA=6.00005825; NDR=6.00000001; ZLA=6.00000005; ZF=6.00000009; ZB=6.00000000; ZP=6.00000000; ZH=6.00000000; ZU=6.00000002; MB=3.00019510; XFM=3.00000015; UTC=2018-02-12 14:25:24 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18021214-0017-0000-0000-00003D6F403F Message-Id: <20180212142506.28445-2-danielhb@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2018-02-12_07:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 impostorscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1709140000 definitions=main-1802120187 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] [fuzzy] X-Received-From: 148.163.156.1 Subject: [Qemu-devel] [PATCH v3 1/1] dump.c: allow fd_write_vmcore to return errno on failure X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Daniel Henrique Barboza , marcandre.lureau@redhat.com, Yasmin Beatriz , Jose Ricardo Ziviani Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Yasmin Beatriz fd_write_vmcore can fail to execute for a lot of reasons that can be retrieved by errno, but it only returns -1. This makes difficult for the caller to know what happened and only a generic error message is propagated back to the user. This is an example using dump-guest-memory: (qemu) dump-guest-memory /home/yasmin/mnt/test.dump dump: failed to save memory All callers of fd_write_vmcore of dump.c does error handling via error_setg(), so at first it seems feasible to add the Error pointer as an argument of fd_write_vmcore. This proved to be more complex than it first looked. fd_write_vmcore is used by write_elf64_notes and write_elf32_notes as a WriteCoreDumpFunction prototype. WriteCoreDumpFuncti= on is declared in include/qom/cpu.h and is used all around the code. This leaves us with few alternatives: - change the WriteCoreDumpFunction prototype to include an error pointer. This would require to change all functions that implements this prototype to also receive an Error pointer; - change both write_elf64_notes and write_elf32_notes to no use the WriteCoreDumpFunction. These functions use not only fd_write_vmcore but also buf_write_note, so this would require to change buf_write_note to handle an Error pointer. Considerable easier than the alternative above, but it's still a lot of code just for the benefit of the callers of fd_write_vmcore. This patch presents an easier solution that benefits all fd_write_vmcore callers: - instead of returning -1 on error, return -errno. All existing callers already checks for ret < 0 so there is no need to change the caller's logic too much. This also allows the retrieval of the errno. - all callers were updated to use error_setg_errno instead of just errno_setg. Now that fd_write_vmcore can return an errno, let's update all callers so they can benefit from a more detailed error message. This is the same dump-guest-memory example with this patch applied: (qemu) dump-guest-memory /home/yasmin/mnt/test.dump dump: failed to save memory: No space left on device (qemu) This example illustrates an error of fd_write_vmcore when called from write_data. All other callers will benefit from better error messages as well. Reported-by: yilzhang@redhat.com Cc: Jose Ricardo Ziviani Signed-off-by: Yasmin Beatriz Signed-off-by: Daniel Henrique Barboza Reviewed-by: Eric Blake Reviewed-by: Marc-Andr=C3=A9 Lureau --- dump.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/dump.c b/dump.c index 7b13baa413..171ff8a3b8 100644 --- a/dump.c +++ b/dump.c @@ -107,7 +107,7 @@ static int fd_write_vmcore(const void *buf, size_t size= , void *opaque) =20 written_size =3D qemu_write_full(s->fd, buf, size); if (written_size !=3D size) { - return -1; + return -errno; } =20 return 0; @@ -140,7 +140,7 @@ static void write_elf64_header(DumpState *s, Error **er= rp) =20 ret =3D fd_write_vmcore(&elf_header, sizeof(elf_header), s); if (ret < 0) { - error_setg(errp, "dump: failed to write elf header"); + error_setg_errno(errp, -ret, "dump: failed to write elf header"); } } =20 @@ -171,7 +171,7 @@ static void write_elf32_header(DumpState *s, Error **er= rp) =20 ret =3D fd_write_vmcore(&elf_header, sizeof(elf_header), s); if (ret < 0) { - error_setg(errp, "dump: failed to write elf header"); + error_setg_errno(errp, -ret, "dump: failed to write elf header"); } } =20 @@ -194,7 +194,8 @@ static void write_elf64_load(DumpState *s, MemoryMappin= g *memory_mapping, =20 ret =3D fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s); if (ret < 0) { - error_setg(errp, "dump: failed to write program header table"); + error_setg_errno(errp, -ret, + "dump: failed to write program header table"); } } =20 @@ -217,7 +218,8 @@ static void write_elf32_load(DumpState *s, MemoryMappin= g *memory_mapping, =20 ret =3D fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s); if (ret < 0) { - error_setg(errp, "dump: failed to write program header table"); + error_setg_errno(errp, -ret, + "dump: failed to write program header table"); } } =20 @@ -237,7 +239,8 @@ static void write_elf64_note(DumpState *s, Error **errp) =20 ret =3D fd_write_vmcore(&phdr, sizeof(Elf64_Phdr), s); if (ret < 0) { - error_setg(errp, "dump: failed to write program header table"); + error_setg_errno(errp, -ret, + "dump: failed to write program header table"); } } =20 @@ -302,7 +305,8 @@ static void write_elf32_note(DumpState *s, Error **errp) =20 ret =3D fd_write_vmcore(&phdr, sizeof(Elf32_Phdr), s); if (ret < 0) { - error_setg(errp, "dump: failed to write program header table"); + error_setg_errno(errp, -ret, + "dump: failed to write program header table"); } } =20 @@ -355,7 +359,8 @@ static void write_elf_section(DumpState *s, int type, E= rror **errp) =20 ret =3D fd_write_vmcore(&shdr, shdr_size, s); if (ret < 0) { - error_setg(errp, "dump: failed to write section header table"); + error_setg_errno(errp, -ret, + "dump: failed to write section header table"); } } =20 @@ -365,7 +370,7 @@ static void write_data(DumpState *s, void *buf, int len= gth, Error **errp) =20 ret =3D fd_write_vmcore(buf, length, s); if (ret < 0) { - error_setg(errp, "dump: failed to save memory"); + error_setg_errno(errp, -ret, "dump: failed to save memory"); } else { s->written_size +=3D length; } --=20 2.14.3