From nobody Mon Feb 9 02:08:25 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.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 216.205.24.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=1624391716; cv=none; d=zohomail.com; s=zohoarc; b=gtH3kQuRRi1EFqKGbpsXPDIApU4+Gksg5wjZM+pDbqWvCckmrKIAynqo/YyIAqL5o5no6TkWMkKbOhDbS8AAM6TkdbUzWvHG++I7AoUztnIiPs1efngWjrLzw753ZLkR/1geZq8Yiy5p/tYMv+aNNrGbuxLcHJWoNUxvYLOxEr4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1624391716; 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=SuyMJrQznKgiALczAUR7Ic+FV9D7QF4NGhkuTY3s3PU=; b=hfKvf0rWhUvac5QkREMDwPeu1m7xNIiblSZMWwBu/jhUQKbTebqDAgVbWg7NpueAOL6GSp3u8dHVvk9syoZiVO8mgyUzB2lGaaUss2JkPWGaJw/AuTHFKGcT89qGuy8C0ibE/MTPenwIwjvCuuadsJx1QNZ74cDrZUauw+fzCJE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.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 [216.205.24.124]) by mx.zohomail.com with SMTPS id 1624391716254688.7086033562869; Tue, 22 Jun 2021 12:55:16 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-21-Esx1olO3OZ-Jr6wJooiceA-1; Tue, 22 Jun 2021 15:55:13 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C524D801596; Tue, 22 Jun 2021 19:55:07 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9B77F5D705; Tue, 22 Jun 2021 19:55:07 +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 263001809C99; Tue, 22 Jun 2021 19:55:07 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 15MJrgqU016883 for ; Tue, 22 Jun 2021 15:53:42 -0400 Received: by smtp.corp.redhat.com (Postfix) id 7AB935D9F0; Tue, 22 Jun 2021 19:53:42 +0000 (UTC) Received: from himantopus.redhat.com (ovpn-112-18.phx2.redhat.com [10.3.112.18]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3E9245D9DE for ; Tue, 22 Jun 2021 19:53:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1624391715; 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=SuyMJrQznKgiALczAUR7Ic+FV9D7QF4NGhkuTY3s3PU=; b=OHBaiwHUNcB7dp7Ips4NHBxWYgBugIamjhIVZ9wKMiZ5YK+JWZ/IyvcdGd45TV2HWIQH6t uShmZZ0xr46cm2mLq7IvqhBAq7ZvRwRAlMZySpD3a9UwNgsJB1BtJAyxeP7+wqFBO/UwG3 NKcbmZ3gLdQY7NQdS0M8vOpckLMIctM= X-MC-Unique: Esx1olO3OZ-Jr6wJooiceA-1 From: Jonathon Jongsma To: libvir-list@redhat.com Subject: [libvirt PATCH v2 4/5] nodedev: handle mdevctl errors consistently Date: Tue, 22 Jun 2021 14:53:35 -0500 Message-Id: <20210622195336.814205-5-jjongsma@redhat.com> In-Reply-To: <20210622195336.814205-1-jjongsma@redhat.com> References: <20210622195336.814205-1-jjongsma@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 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.15 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) Content-Type: text/plain; charset="utf-8" Currently, we have three different types of mdevctl errors: 1. the command cannot be constructed ecause of unsatisfied preconditions 2. the command cannot be executed due to some error 3. the command is executed, but returns an error status These different failures are handled differently. Some cases set an error and return and error status, and some return a error message but do not set an error. This means that the caller has to check both whether the return value is negative and whether the errmsg parameter is non-NULL before deciding whether to report the error or not. The situation is further complicated by the fact that there are occasional instances where mdevctl exits with an error status but does not print an error message. This results in errmsg being an empty string "" (i.e. non-NULL). Simplify the situation by ensuring that virReportError() is called for all error conditions rather than returning an error message back to the calling function. Signed-off-by: Jonathon Jongsma Reviewed-by: Boris Fiuczynski --- src/node_device/node_device_driver.c | 114 +++++++++++++++------------ 1 file changed, 65 insertions(+), 49 deletions(-) diff --git a/src/node_device/node_device_driver.c b/src/node_device/node_de= vice_driver.c index eb85cc0439..497db0006a 100644 --- a/src/node_device/node_device_driver.c +++ b/src/node_device/node_device_driver.c @@ -746,6 +746,8 @@ nodeDeviceGetMdevctlCommand(virNodeDeviceDef *def, case MDEVCTL_CMD_LAST: default: /* SHOULD NEVER HAPPEN */ + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unknown Command '%i'"), cmd_type); return NULL; } =20 @@ -795,48 +797,62 @@ nodeDeviceGetMdevctlCommand(virNodeDeviceDef *def, =20 =20 static int -virMdevctlCreate(virNodeDeviceDef *def, char **uuid, char **errmsg) +virMdevctlCreate(virNodeDeviceDef *def, char **uuid) { int status; + g_autofree char *errmsg =3D NULL; g_autoptr(virCommand) cmd =3D nodeDeviceGetMdevctlCommand(def, MDEVCTL_CMD_CR= EATE, uuid, - errmsg); + &errmsg); =20 if (!cmd) return -1; =20 /* an auto-generated uuid is returned via stdout if no uuid is specifi= ed in * the mdevctl args */ - if (virCommandRun(cmd, &status) < 0 || status !=3D 0) + if (virCommandRun(cmd, &status) < 0) + return -1; + + if (status !=3D 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to start mediated device: %s"), + MDEVCTL_ERROR(errmsg)); return -1; + } =20 /* remove newline */ *uuid =3D g_strstrip(*uuid); - return 0; } =20 =20 static int -virMdevctlDefine(virNodeDeviceDef *def, char **uuid, char **errmsg) +virMdevctlDefine(virNodeDeviceDef *def, char **uuid) { int status; + g_autofree char *errmsg =3D NULL; g_autoptr(virCommand) cmd =3D nodeDeviceGetMdevctlCommand(def, MDEVCTL_CMD_DE= FINE, - uuid, errmsg); + uuid, &errmsg); =20 if (!cmd) return -1; =20 /* an auto-generated uuid is returned via stdout if no uuid is specifi= ed in * the mdevctl args */ - if (virCommandRun(cmd, &status) < 0 || status !=3D 0) + if (virCommandRun(cmd, &status) < 0) return -1; =20 + if (status !=3D 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to define mediated device: %s"), + MDEVCTL_ERROR(errmsg)); + return -1; + } + /* remove newline */ *uuid =3D g_strstrip(*uuid); - return 0; } =20 @@ -846,7 +862,6 @@ nodeDeviceCreateXMLMdev(virConnectPtr conn, virNodeDeviceDef *def) { g_autofree char *uuid =3D NULL; - g_autofree char *errmsg =3D NULL; =20 if (!def->parent) { virReportError(VIR_ERR_XML_ERROR, "%s", @@ -854,11 +869,7 @@ nodeDeviceCreateXMLMdev(virConnectPtr conn, return NULL; } =20 - if (virMdevctlCreate(def, &uuid, &errmsg) < 0) { - if (errmsg) - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Unable to start mediated device: %s"), - errmsg); + if (virMdevctlCreate(def, &uuid) < 0) { return NULL; } =20 @@ -928,55 +939,79 @@ nodeDeviceCreateXML(virConnectPtr conn, =20 =20 static int -virMdevctlStop(virNodeDeviceDef *def, char **errmsg) +virMdevctlStop(virNodeDeviceDef *def) { int status; g_autoptr(virCommand) cmd =3D NULL; + g_autofree char *errmsg =3D NULL; =20 - cmd =3D nodeDeviceGetMdevctlCommand(def, MDEVCTL_CMD_STOP, NULL, errms= g); + cmd =3D nodeDeviceGetMdevctlCommand(def, MDEVCTL_CMD_STOP, NULL, &errm= sg); =20 if (!cmd) return -1; =20 - if (virCommandRun(cmd, &status) < 0 || status !=3D 0) + if (virCommandRun(cmd, &status) < 0) return -1; =20 + if (status !=3D 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to destroy '%s': %s"), def->name, + MDEVCTL_ERROR(errmsg)); + return -1; + } + return 0; } =20 =20 static int -virMdevctlUndefine(virNodeDeviceDef *def, char **errmsg) +virMdevctlUndefine(virNodeDeviceDef *def) { int status; g_autoptr(virCommand) cmd =3D NULL; + g_autofree char *errmsg =3D NULL; =20 - cmd =3D nodeDeviceGetMdevctlCommand(def, MDEVCTL_CMD_UNDEFINE, NULL, e= rrmsg); + cmd =3D nodeDeviceGetMdevctlCommand(def, MDEVCTL_CMD_UNDEFINE, NULL, &= errmsg); =20 if (!cmd) return -1; =20 - if (virCommandRun(cmd, &status) < 0 || status !=3D 0) + if (virCommandRun(cmd, &status) < 0) return -1; =20 + if (status !=3D 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to undefine mediated device: %s"), + MDEVCTL_ERROR(errmsg)); + return -1; + } + return 0; } =20 =20 static int -virMdevctlStart(virNodeDeviceDef *def, char **errmsg) +virMdevctlStart(virNodeDeviceDef *def) { int status; g_autoptr(virCommand) cmd =3D NULL; + g_autofree char *errmsg =3D NULL; =20 - cmd =3D nodeDeviceGetMdevctlCommand(def, MDEVCTL_CMD_START, NULL, errm= sg); + cmd =3D nodeDeviceGetMdevctlCommand(def, MDEVCTL_CMD_START, NULL, &err= msg); =20 if (!cmd) return -1; =20 - if (virCommandRun(cmd, &status) < 0 || status !=3D 0) + if (virCommandRun(cmd, &status) < 0) return -1; =20 + if (status !=3D 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unable to create mediated device: %s"), + MDEVCTL_ERROR(errmsg)); + return -1; + } + return 0; } =20 @@ -1204,7 +1239,6 @@ nodeDeviceDestroy(virNodeDevicePtr device) g_autofree char *vfiogroup =3D virMediatedDeviceGetIOMMUGroupDev(def->caps->data.mdev.uuid); VIR_AUTOCLOSE fd =3D -1; - g_autofree char *errmsg =3D NULL; =20 if (!vfiogroup) goto cleanup; @@ -1218,13 +1252,9 @@ nodeDeviceDestroy(virNodeDevicePtr device) goto cleanup; } =20 - if (virMdevctlStop(def, &errmsg) < 0) { - if (errmsg) - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Unable to destroy '%s': %s"), def->name, - errmsg); + if (virMdevctlStop(def) < 0) goto cleanup; - } + ret =3D 0; } else { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", @@ -1299,7 +1329,6 @@ nodeDeviceDefineXML(virConnect *conn, g_autoptr(virNodeDeviceDef) def =3D NULL; const char *virt_type =3D NULL; g_autofree char *uuid =3D NULL; - g_autofree char *errmsg =3D NULL; g_autofree char *name =3D NULL; =20 virCheckFlags(0, NULL); @@ -1327,10 +1356,7 @@ nodeDeviceDefineXML(virConnect *conn, return NULL; } =20 - if (virMdevctlDefine(def, &uuid, &errmsg) < 0) { - if (errmsg) - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Unable to define mediated device: %s"), errm= sg); + if (virMdevctlDefine(def, &uuid) < 0) { return NULL; } =20 @@ -1385,14 +1411,9 @@ nodeDeviceUndefine(virNodeDevice *device, } =20 if (nodeDeviceHasCapability(def, VIR_NODE_DEV_CAP_MDEV)) { - g_autofree char *errmsg =3D NULL; - - if (virMdevctlUndefine(def, &errmsg) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Unable to undefine mediated device: %s"), - MDEVCTL_ERROR(errmsg)); + if (virMdevctlUndefine(def) < 0) goto cleanup; - } + ret =3D 0; } else { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", @@ -1432,14 +1453,9 @@ nodeDeviceCreate(virNodeDevice *device, goto cleanup; =20 if (nodeDeviceHasCapability(def, VIR_NODE_DEV_CAP_MDEV)) { - g_autofree char *errmsg =3D NULL; - - if (virMdevctlStart(def, &errmsg) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Unable to create mediated device: %s"), - MDEVCTL_ERROR(errmsg)); + if (virMdevctlStart(def) < 0) goto cleanup; - } + ret =3D 0; } else { virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", --=20 2.31.1