[PATCH v5 08/13] virdnsmasq: Lookup DNSMASQ in PATH

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



While it's true that our virCommand subsystem is happy with
non-absolute paths, the dnsmasq capability code is not. It stores
the path to dnsmasq within and makes it accessible via
dnsmasqCapsGetBinaryPath(). While strictly speaking no caller
necessarily needs canonicalized path, let's find dnsmasq once and
cache the result.

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 virFindFileInPath() means we have to mock it in
test suite because dnsmasqCaps are created in
networkxml2conftest.

Moreover, we don't need to check for dnsmasq in configure.

Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx>
Reviewed-by: Andrea Bolognani <abologna@xxxxxxxxxx>
---
 meson.build                 |  1 -
 src/util/virdnsmasq.c       | 19 ++++++++-----------
 tests/meson.build           |  1 +
 tests/networkxml2conftest.c |  3 ++-
 tests/virdnsmasqmock.c      | 19 +++++++++++++++++++
 5 files changed, 30 insertions(+), 13 deletions(-)
 create mode 100644 tests/virdnsmasqmock.c

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 <config.h>
+
+#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;
+}
-- 
2.34.1




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux