diff --git a/meson.build b/meson.build index c2e9619f1f..70843afcd5 100644 --- a/meson.build +++ b/meson.build @@ -813,7 +813,6 @@ endforeach optional_programs = [ 'augparse', 'dmidecode', - 'dnsmasq', 'ebtables', 'flake8', 'ip', diff --git a/src/util/virdnsmasq.c b/src/util/virdnsmasq.c index 5bed8817e5..579c67d86a 100644 --- a/src/util/virdnsmasq.c +++ b/src/util/virdnsmasq.c @@ -46,6 +46,7 @@ VIR_LOG_INIT("util.dnsmasq"); +#define DNSMASQ "dnsmasq" #define DNSMASQ_HOSTSFILE_SUFFIX "hostsfile" #define DNSMASQ_ADDNHOSTSFILE_SUFFIX "addnhosts" @@ -650,16 +651,6 @@ dnsmasqCapsRefreshInternal(dnsmasqCaps *caps) g_autoptr(virCommand) vercmd = NULL; g_autofree char *version = NULL; - /* 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 = virCommandNewArgList(caps->binaryPath, "--version", NULL); virCommandSetOutputBuffer(vercmd, &version); virCommandAddEnvPassCommon(vercmd); @@ -679,7 +670,13 @@ dnsmasqCapsNewEmpty(void) return NULL; if (!(caps = virObjectNew(dnsmasqCapsClass))) return NULL; - caps->binaryPath = g_strdup(DNSMASQ); + + if (!(caps->binaryPath = virFindFileInPath(DNSMASQ))) { + virReportSystemError(ENOENT, "%s", + _("Unable to find 'dnsmasq' binary in $PATH")); + return NULL; + } + return g_steal_pointer(&caps); } diff --git a/tests/meson.build b/tests/meson.build index 4792220b48..8f92f2033f 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -59,6 +59,7 @@ mock_libs = [ { 'name': 'domaincapsmock' }, { 'name': 'shunload', 'sources': [ 'shunloadhelper.c' ] }, { 'name': 'vircgroupmock' }, + { 'name': 'virdnsmasqmock' }, { 'name': 'virfilecachemock' }, { 'name': 'virfirewallmock' }, { 'name': 'virgdbusmock' }, diff --git a/tests/networkxml2conftest.c b/tests/networkxml2conftest.c index 6a2c70ead1..13257c749b 100644 --- a/tests/networkxml2conftest.c +++ b/tests/networkxml2conftest.c @@ -158,4 +158,5 @@ mymain(void) return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; } -VIR_TEST_MAIN(mymain) +VIR_TEST_MAIN_PRELOAD(mymain, + VIR_TEST_MOCK("virdnsmasq")) diff --git a/tests/virdnsmasqmock.c b/tests/virdnsmasqmock.c new file mode 100644 index 0000000000..448332ec40 --- /dev/null +++ b/tests/virdnsmasqmock.c @@ -0,0 +1,19 @@ +/* + * Copyright (C) 2022 Red Hat, Inc. + * SPDX-License-Identifier: LGPL-2.1-or-later + */ + +#include + +#include "internal.h" +#include "virfile.h" + +char * +virFindFileInPath(const char *file) +{ + if (STREQ_NULLABLE(file, "dnsmasq")) + return g_strdup("/usr/sbin/dnsmasq"); + + /* We should not need any other binaries so return NULL. */ + return NULL; +}