From: Michal Privoznik <[email protected]>

Whether a network needs dnsmasq or not is decided at the
beginning of networkStartDhcpDaemon(). Move that code into a
separate function so it can be reused later.

Signed-off-by: Michal Privoznik <[email protected]>
---
 src/network/bridge_driver.c      | 36 ++++++++++++++++++++------------
 src/network/bridge_driver_priv.h |  3 +++
 2 files changed, 26 insertions(+), 13 deletions(-)

diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c
index d7004771b7..e7a4e5e1b7 100644
--- a/src/network/bridge_driver.c
+++ b/src/network/bridge_driver.c
@@ -1516,34 +1516,44 @@ networkBuildDhcpDaemonCommandLine(virNetworkDriverState 
*driver,
 }
 
 
-static int
-networkStartDhcpDaemon(virNetworkDriverState *driver,
-                       virNetworkObj *obj)
+bool
+networkNeedsDnsmasq(const virNetworkDef* def)
 {
-    g_autoptr(virNetworkDriverConfig) cfg = virNetworkDriverGetConfig(driver);
-    virNetworkDef *def = virNetworkObjGetDef(obj);
     virNetworkIPDef *ipdef;
     size_t i;
-    bool needDnsmasq = false;
-    g_autoptr(virCommand) cmd = NULL;
-    g_autofree char *pidfile = NULL;
-    pid_t dnsmasqPid;
-    g_autoptr(dnsmasqContext) dctx = NULL;
 
     /* see if there are any IP addresses that need a dhcp server */
     i = 0;
     while ((ipdef = virNetworkDefGetIPByIndex(def, AF_UNSPEC, i))) {
         i++;
         if (ipdef->nranges || ipdef->nhosts || ipdef->tftproot)
-            needDnsmasq = true;
+            return true;
     }
 
     /* no IP addresses at all, so we don't need to run */
     if (i == 0)
-        return 0;
+        return false;
 
     /* no DHCP services needed, and user disabled DNS service */
-    if (!needDnsmasq && def->dns.enable == VIR_TRISTATE_BOOL_NO)
+    if (def->dns.enable == VIR_TRISTATE_BOOL_NO)
+        return false;
+
+    return true;
+}
+
+
+static int
+networkStartDhcpDaemon(virNetworkDriverState *driver,
+                       virNetworkObj *obj)
+{
+    g_autoptr(virNetworkDriverConfig) cfg = virNetworkDriverGetConfig(driver);
+    virNetworkDef *def = virNetworkObjGetDef(obj);
+    g_autoptr(virCommand) cmd = NULL;
+    g_autofree char *pidfile = NULL;
+    pid_t dnsmasqPid;
+    g_autoptr(dnsmasqContext) dctx = NULL;
+
+    if (!networkNeedsDnsmasq(def))
         return 0;
 
     if (g_mkdir_with_parents(cfg->pidDir, 0777) < 0) {
diff --git a/src/network/bridge_driver_priv.h b/src/network/bridge_driver_priv.h
index 7f21ed7ad6..c7d1d8268d 100644
--- a/src/network/bridge_driver_priv.h
+++ b/src/network/bridge_driver_priv.h
@@ -23,3 +23,6 @@ networkDnsmasqConfContents(virNetworkObj *obj,
                            char **hostsfilestr,
                            dnsmasqContext *dctx,
                            dnsmasqCaps *caps);
+
+bool
+networkNeedsDnsmasq(const virNetworkDef* def);
-- 
2.52.0

Reply via email to