From nobody Sun Feb 8 20:35:23 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.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.133.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=1641977318; cv=none; d=zohomail.com; s=zohoarc; b=CerowrLRonRQ5Ji+722bdgW30R8aVOEMMqH871DRZqzPBVIQRVn+7lJqIIjd2GnTprGsQ+4UY1lFy1nPtUuUKzVc49cOeBD3yuqzp5ArdwnWA9fML9uKL44EPw61BmOTexh0pJPXIlQrv1sOfyg6t1ttF88ygscSJ8nLfZTOWEc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1641977318; 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=J3oVLblUJHV1xlICwi7rvVIA6OS/dWDwBieqwpSZ/NE=; b=VKg4V4Kwfg3O8oWIgnRxNptJeHp/ADYHxRKKLVajVFOtGgXeFGkovGhnw7ZQxlGngJUr5acMV/qw7TPYvrFTHbjHcVgt6X2LLRe6x3FVNLJWKdac0/J5xge9i+G03A9wKVvPiDHtw4zxXH+WwTMkWKqXh5UymYnC9BbfHHGkQNI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.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.133.124]) by mx.zohomail.com with SMTPS id 1641977318551914.6166144138509; Wed, 12 Jan 2022 00:48:38 -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-36-9xDbZrk2OHaS8V4xt6pUhQ-1; Wed, 12 Jan 2022 03:48:34 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 3B8F583DD23; Wed, 12 Jan 2022 08:48:29 +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 07FEE7CAD8; Wed, 12 Jan 2022 08:48:29 +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 BF5C41809CBF; Wed, 12 Jan 2022 08:48:28 +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 20C8mRne017400 for ; Wed, 12 Jan 2022 03:48:27 -0500 Received: by smtp.corp.redhat.com (Postfix) id D1BE767C9D; Wed, 12 Jan 2022 08:48:27 +0000 (UTC) Received: from maggie.redhat.com (unknown [10.43.2.56]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5C95B752A3 for ; Wed, 12 Jan 2022 08:48:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1641977317; 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=J3oVLblUJHV1xlICwi7rvVIA6OS/dWDwBieqwpSZ/NE=; b=TIZ1sbFsvhwTuhnO2s+HmVRZa8oiCS26AC42tEpvghA5P6RyMGkREHKInIuUr3UikC9+ww CpYBu4An/2G/lmcOZy3yjRXG71QVGxT2vkiuDZASCHnDmWCrRlspma81Me6zSJxNGscqqx PoGVT5sTUmpxZnujprPtHDqStMAZNFE= X-MC-Unique: 9xDbZrk2OHaS8V4xt6pUhQ-1 From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH v3 3/7] virdnsmasq: Lookup DNSMASQ in PATH Date: Wed, 12 Jan 2022 09:47:54 +0100 Message-Id: <2da99211b2743e3f6cf1112dc64bbb3a01b7dd25.1641976729.git.mprivozn@redhat.com> In-Reply-To: References: 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.12 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: 1641977319316100001 Content-Type: text/plain; charset="utf-8" While it's true that our virCommand subsystem is happy with non-absolute paths, the dnsmasq capability code is not. For instance, it does call stat() over the binary to learn its mtime (and thus decide whether capabilities need to be fetched again or not). Therefore, when constructing the capabilities structure look up the binary path. If DNSMASQ already contains an absolute path then virFindFileInPath() will simply return a copy. With this code in place, the virFileIsExecutable() check can be removed from dnsmasqCapsRefreshInternal() because virFindFileInPath() already made sure the binary is executable. But introducing virFileIsExecutable() means we have to mock it in test suite because dnsmasqCaps are created in networkxml2conftest. Signed-off-by: Michal Privoznik --- src/util/virdnsmasq.c | 22 +++++++++------------- tests/meson.build | 1 + tests/networkmock.c | 30 ++++++++++++++++++++++++++++++ tests/networkxml2conftest.c | 3 ++- 4 files changed, 42 insertions(+), 14 deletions(-) create mode 100644 tests/networkmock.c diff --git a/src/util/virdnsmasq.c b/src/util/virdnsmasq.c index f4bdab116e..79060e2021 100644 --- a/src/util/virdnsmasq.c +++ b/src/util/virdnsmasq.c @@ -670,16 +670,6 @@ dnsmasqCapsRefreshInternal(dnsmasqCaps *caps, bool for= ce) return 0; caps->mtime =3D sb.st_mtime; =20 - /* Make sure the binary we are about to try exec'ing exists. - * Technically we could catch the exec() failure, but that's - * in a sub-process so it's hard to feed back a useful error. - */ - if (!virFileIsExecutable(caps->binaryPath)) { - virReportSystemError(errno, _("dnsmasq binary %s is not executable= "), - caps->binaryPath); - return -1; - } - vercmd =3D virCommandNewArgList(caps->binaryPath, "--version", NULL); virCommandSetOutputBuffer(vercmd, &version); virCommandAddEnvPassCommon(vercmd); @@ -702,14 +692,20 @@ dnsmasqCapsRefreshInternal(dnsmasqCaps *caps, bool fo= rce) static dnsmasqCaps * dnsmasqCapsNewEmpty(void) { - dnsmasqCaps *caps; + g_autoptr(dnsmasqCaps) caps =3D NULL; =20 if (dnsmasqCapsInitialize() < 0) return NULL; if (!(caps =3D virObjectNew(dnsmasqCapsClass))) return NULL; - caps->binaryPath =3D g_strdup(DNSMASQ); - return caps; + + if (!(caps->binaryPath =3D virFindFileInPath(DNSMASQ))) { + virReportSystemError(ENOENT, "%s", + _("Unable to find 'dnsmasq' binary in $PATH")= ); + return NULL; + } + + return g_steal_pointer(&caps); } =20 dnsmasqCaps * diff --git a/tests/meson.build b/tests/meson.build index 4792220b48..e8f5d4c1f7 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -57,6 +57,7 @@ endif =20 mock_libs =3D [ { 'name': 'domaincapsmock' }, + { 'name': 'networkmock' }, { 'name': 'shunload', 'sources': [ 'shunloadhelper.c' ] }, { 'name': 'vircgroupmock' }, { 'name': 'virfilecachemock' }, diff --git a/tests/networkmock.c b/tests/networkmock.c new file mode 100644 index 0000000000..a9c13311a6 --- /dev/null +++ b/tests/networkmock.c @@ -0,0 +1,30 @@ +/* + * 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 "internal.h" +#include "virfile.h" + +char * +virFindFileInPath(const char *file) +{ + if (file && g_strrstr(file, "dnsmasq")) + return g_strdup(file); + + /* We should not need any other binaries so return NULL. */ + return NULL; +} diff --git a/tests/networkxml2conftest.c b/tests/networkxml2conftest.c index d79c2b4783..8a6657654a 100644 --- a/tests/networkxml2conftest.c +++ b/tests/networkxml2conftest.c @@ -158,4 +158,5 @@ mymain(void) return ret =3D=3D 0 ? EXIT_SUCCESS : EXIT_FAILURE; } =20 -VIR_TEST_MAIN(mymain) +VIR_TEST_MAIN_PRELOAD(mymain, + VIR_TEST_MOCK("network")) --=20 2.34.1