From nobody Sun Feb 8 18:10:48 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 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=1566577331; cv=none; d=zoho.com; s=zohoarc; b=D87fal0VDc6R6iGZ1jM8+IlDgT4R1kwgmn88CR5pEEsZs45kzI3xxFcDsxw/OubM1TqRLKzRaQU4U4xZ/r1Ph/ciES7X4B6XwXKAomM87CXGnqPC2gRhorRWL9D0Gn2+TEFdBpXi3332fczYAkE52IERhwC6Hrl8QcA/+rmsajc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1566577331; h=Content-Type:Content-Transfer-Encoding:Cc: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:ARC-Authentication-Results; bh=3dlm0jdNYplLWDJo7y+x4dhE3Qs0gY7qc7hKobfueoA=; b=OowhjAWferzccTnAcWySfi5J1GAGHty3pVMSlEyIHo9zSuLlFu1PRGtaMFl2QwraYbmASjdMTMTEe7ybvyMd7FKpjfp5J0WsP2HqvrRbJW3+bSHc7lDobD1OwgU+1yzshbOFNlbSk+75BURhD27wFRQ+PUPUWn7XOJ2yAY2PTjU= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 156657733132757.47193857024138; Fri, 23 Aug 2019 09:22:11 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 725FE10F23F6; Fri, 23 Aug 2019 16:22: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 156C81001B17; Fri, 23 Aug 2019 16:22: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 8C9E524F2F; Fri, 23 Aug 2019 16:22:05 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x7NGM41a005977 for ; Fri, 23 Aug 2019 12:22:04 -0400 Received: by smtp.corp.redhat.com (Postfix) id 4FF8B6092D; Fri, 23 Aug 2019 16:22:04 +0000 (UTC) Received: from worklaptop.redhat.com (ovpn-122-169.rdu2.redhat.com [10.10.122.169]) by smtp.corp.redhat.com (Postfix) with ESMTP id 888F260605; Fri, 23 Aug 2019 16:22:03 +0000 (UTC) From: Cole Robinson To: libvirt-list@redhat.com Date: Fri, 23 Aug 2019 12:21:45 -0400 Message-Id: <56d8740d08f75e7f722c027391921c9c2ea43d35.1566576129.git.crobinso@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Cc: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Subject: [libvirt] [PATCH v2 01/16] qemu: extract out qemuFetchConfigs from firmware 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: , Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.6.2 (mx1.redhat.com [10.5.110.66]); Fri, 23 Aug 2019 16:22:09 +0000 (UTC) From: Marc-Andr=C3=A9 Lureau The same config files disovery & priority rules are used for vhost-user backends. Signed-off-by: Marc-Andr=C3=A9 Lureau Signed-off-by: Cole Robinson --- src/qemu/Makefile.inc.am | 2 + src/qemu/qemu_configs.c | 183 +++++++++++++++++++++++++++++++++++++++ src/qemu/qemu_configs.h | 28 ++++++ src/qemu/qemu_firmware.c | 144 +----------------------------- 4 files changed, 215 insertions(+), 142 deletions(-) create mode 100644 src/qemu/qemu_configs.c create mode 100644 src/qemu/qemu_configs.h diff --git a/src/qemu/Makefile.inc.am b/src/qemu/Makefile.inc.am index 30a9751cfd..f7a0fa4a84 100644 --- a/src/qemu/Makefile.inc.am +++ b/src/qemu/Makefile.inc.am @@ -30,6 +30,8 @@ QEMU_DRIVER_SOURCES =3D \ qemu/qemu_hotplugpriv.h \ qemu/qemu_conf.c \ qemu/qemu_conf.h \ + qemu/qemu_configs.c \ + qemu/qemu_configs.h \ qemu/qemu_process.c \ qemu/qemu_process.h \ qemu/qemu_processpriv.h \ diff --git a/src/qemu/qemu_configs.c b/src/qemu/qemu_configs.c new file mode 100644 index 0000000000..39b8906be5 --- /dev/null +++ b/src/qemu/qemu_configs.c @@ -0,0 +1,183 @@ +/* + * qemu_configs.c: QEMU firmware/vhost-user etc configs + * + * Copyright (C) 2019 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + */ + +#include + +#include "qemu_configs.h" +#include "configmake.h" +#include "viralloc.h" +#include "virenum.h" +#include "virfile.h" +#include "virhash.h" +#include "virlog.h" +#include "virstring.h" + +#define VIR_FROM_THIS VIR_FROM_QEMU + +VIR_LOG_INIT("qemu.qemu_configs"); + +static int +qemuBuildFileList(virHashTablePtr files, const char *dir) +{ + DIR *dirp; + struct dirent *ent =3D NULL; + int rc; + int ret =3D -1; + + if ((rc =3D virDirOpenIfExists(&dirp, dir)) < 0) + return -1; + + if (rc =3D=3D 0) + return 0; + + while ((rc =3D virDirRead(dirp, &ent, dir)) > 0) { + VIR_AUTOFREE(char *) filename =3D NULL; + VIR_AUTOFREE(char *) path =3D NULL; + struct stat sb; + + if (STRPREFIX(ent->d_name, ".")) + continue; + + if (VIR_STRDUP(filename, ent->d_name) < 0) + goto cleanup; + + if (virAsprintf(&path, "%s/%s", dir, filename) < 0) + goto cleanup; + + if (stat(path, &sb) < 0) { + virReportSystemError(errno, _("Unable to access %s"), path); + goto cleanup; + } + + if (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode)) + continue; + + if (virHashUpdateEntry(files, filename, path) < 0) + goto cleanup; + + path =3D NULL; + } + + if (rc < 0) + goto cleanup; + + ret =3D 0; + cleanup: + virDirClose(&dirp); + return ret; +} + +static int +qemuConfigFilesSorter(const virHashKeyValuePair *a, + const virHashKeyValuePair *b) +{ + return strcmp(a->key, b->key); +} + +#define QEMU_SYSTEM_LOCATION PREFIX "/share/qemu" +#define QEMU_ETC_LOCATION SYSCONFDIR "/qemu" + +int +qemuFetchConfigs(const char *name, + char ***configs, + bool privileged) +{ + VIR_AUTOPTR(virHashTable) files =3D NULL; + VIR_AUTOFREE(char *) homeConfig =3D NULL; + VIR_AUTOFREE(char *) xdgConfig =3D NULL; + VIR_AUTOFREE(char *) sysLocation =3D virFileBuildPath(QEMU_SYSTEM_LOCA= TION, name, NULL); + VIR_AUTOFREE(char *) etcLocation =3D virFileBuildPath(QEMU_ETC_LOCATIO= N, name, NULL); + VIR_AUTOFREE(virHashKeyValuePairPtr) pairs =3D NULL; + virHashKeyValuePairPtr tmp =3D NULL; + + *configs =3D NULL; + + if (!privileged) { + /* This is a slight divergence from the specification. + * Since the system daemon runs as root, it doesn't make + * much sense to parse files in root's home directory. It + * makes sense only for session daemon which runs under + * regular user. */ + if (VIR_STRDUP(xdgConfig, getenv("XDG_CONFIG_HOME")) < 0) + return -1; + + if (!xdgConfig) { + VIR_AUTOFREE(char *) home =3D virGetUserDirectory(); + + if (!home) + return -1; + + if (virAsprintf(&xdgConfig, "%s/.config", home) < 0) + return -1; + } + + if (virAsprintf(&homeConfig, "%s/qemu/%s", xdgConfig, name) < 0) + return -1; + } + + if (!(files =3D virHashCreate(10, virHashValueFree))) + return -1; + + if (qemuBuildFileList(files, sysLocation) < 0) + return -1; + + if (qemuBuildFileList(files, etcLocation) < 0) + return -1; + + if (homeConfig && + qemuBuildFileList(files, homeConfig) < 0) + return -1; + + /* At this point, the @files hash table contains unique set of filenam= es + * where each filename (as key) has the highest priority full pathname + * associated with it. */ + + if (virHashSize(files) =3D=3D 0) + return 0; + + if (!(pairs =3D virHashGetItems(files, qemuConfigFilesSorter))) + return -1; + + for (tmp =3D pairs; tmp->key; tmp++) { + const char *path =3D tmp->value; + off_t len; + + if ((len =3D virFileLength(path, -1)) < 0) { + virReportSystemError(errno, + _("unable to get size of '%s'"), + path); + return -1; + } + + VIR_DEBUG("%s description path '%s' len=3D%jd", + name, path, (intmax_t) len); + + if (len =3D=3D 0) { + /* Empty files are used to mask less specific instances + * of the same file. */ + continue; + } + + if (virStringListAdd(configs, path) < 0) + return -1; + } + + return 0; +} diff --git a/src/qemu/qemu_configs.h b/src/qemu/qemu_configs.h new file mode 100644 index 0000000000..5c113752d0 --- /dev/null +++ b/src/qemu/qemu_configs.h @@ -0,0 +1,28 @@ +/* + * qemu_configs.h: QEMU firmware/vhost-user etc configs + * + * Copyright (C) 2019 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + */ + +#pragma once + +#include "internal.h" + +int +qemuFetchConfigs(const char *name, + char ***configs, + bool privileged); diff --git a/src/qemu/qemu_firmware.c b/src/qemu/qemu_firmware.c index 983a7c83b2..f0a6953d8b 100644 --- a/src/qemu/qemu_firmware.c +++ b/src/qemu/qemu_firmware.c @@ -23,13 +23,12 @@ #include =20 #include "qemu_firmware.h" +#include "qemu_configs.h" #include "configmake.h" #include "qemu_capabilities.h" #include "qemu_domain.h" #include "qemu_process.h" #include "virarch.h" -#include "virfile.h" -#include "virhash.h" #include "virjson.h" #include "virlog.h" #include "virstring.h" @@ -907,150 +906,11 @@ qemuFirmwareFormat(qemuFirmwarePtr fw) } =20 =20 -static int -qemuFirmwareBuildFileList(virHashTablePtr files, const char *dir) -{ - DIR *dirp; - struct dirent *ent =3D NULL; - int rc; - int ret =3D -1; - - if ((rc =3D virDirOpenIfExists(&dirp, dir)) < 0) - return -1; - - if (rc =3D=3D 0) - return 0; - - while ((rc =3D virDirRead(dirp, &ent, dir)) > 0) { - VIR_AUTOFREE(char *) filename =3D NULL; - VIR_AUTOFREE(char *) path =3D NULL; - struct stat sb; - - if (STRPREFIX(ent->d_name, ".")) - continue; - - if (VIR_STRDUP(filename, ent->d_name) < 0) - goto cleanup; - - if (virAsprintf(&path, "%s/%s", dir, filename) < 0) - goto cleanup; - - if (stat(path, &sb) < 0) { - virReportSystemError(errno, _("Unable to access %s"), path); - goto cleanup; - } - - if (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode)) - continue; - - if (virHashUpdateEntry(files, filename, path) < 0) - goto cleanup; - - path =3D NULL; - } - - if (rc < 0) - goto cleanup; - - ret =3D 0; - cleanup: - virDirClose(&dirp); - return ret; -} - -static int -qemuFirmwareFilesSorter(const virHashKeyValuePair *a, - const virHashKeyValuePair *b) -{ - return strcmp(a->key, b->key); -} - -#define QEMU_FIRMWARE_SYSTEM_LOCATION PREFIX "/share/qemu/firmware" -#define QEMU_FIRMWARE_ETC_LOCATION SYSCONFDIR "/qemu/firmware" - int qemuFirmwareFetchConfigs(char ***firmwares, bool privileged) { - VIR_AUTOPTR(virHashTable) files =3D NULL; - VIR_AUTOFREE(char *) homeConfig =3D NULL; - VIR_AUTOFREE(char *) xdgConfig =3D NULL; - VIR_AUTOFREE(virHashKeyValuePairPtr) pairs =3D NULL; - virHashKeyValuePairPtr tmp =3D NULL; - - *firmwares =3D NULL; - - if (!privileged) { - /* This is a slight divergence from the specification. - * Since the system daemon runs as root, it doesn't make - * much sense to parse files in root's home directory. It - * makes sense only for session daemon which runs under - * regular user. */ - if (VIR_STRDUP(xdgConfig, getenv("XDG_CONFIG_HOME")) < 0) - return -1; - - if (!xdgConfig) { - VIR_AUTOFREE(char *) home =3D virGetUserDirectory(); - - if (!home) - return -1; - - if (virAsprintf(&xdgConfig, "%s/.config", home) < 0) - return -1; - } - - if (virAsprintf(&homeConfig, "%s/qemu/firmware", xdgConfig) < 0) - return -1; - } - - if (!(files =3D virHashCreate(10, virHashValueFree))) - return -1; - - if (qemuFirmwareBuildFileList(files, QEMU_FIRMWARE_SYSTEM_LOCATION) < = 0) - return -1; - - if (qemuFirmwareBuildFileList(files, QEMU_FIRMWARE_ETC_LOCATION) < 0) - return -1; - - if (homeConfig && - qemuFirmwareBuildFileList(files, homeConfig) < 0) - return -1; - - /* At this point, the @files hash table contains unique set of filenam= es - * where each filename (as key) has the highest priority full pathname - * associated with it. */ - - if (virHashSize(files) =3D=3D 0) - return 0; - - if (!(pairs =3D virHashGetItems(files, qemuFirmwareFilesSorter))) - return -1; - - for (tmp =3D pairs; tmp->key; tmp++) { - const char *path =3D tmp->value; - off_t len; - - if ((len =3D virFileLength(path, -1)) < 0) { - virReportSystemError(errno, - _("unable to get size of '%s'"), - path); - return -1; - } - - VIR_DEBUG("firmware description path '%s' len=3D%jd", - path, (intmax_t) len); - - if (len =3D=3D 0) { - /* Empty files are used to mask less specific instances - * of the same file. */ - continue; - } - - if (virStringListAdd(firmwares, path) < 0) - return -1; - } - - return 0; + return qemuFetchConfigs("firmware", firmwares, privileged); } =20 =20 --=20 2.21.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list