43fe83
From 4424870618c6e43ca44f78842c433e9b5e6c06d4 Mon Sep 17 00:00:00 2001
43fe83
Message-Id: <4424870618c6e43ca44f78842c433e9b5e6c06d4.1381871411.git.jdenemar@redhat.com>
43fe83
From: "Daniel P. Berrange" <berrange@redhat.com>
43fe83
Date: Mon, 7 Oct 2013 16:43:12 +0100
43fe83
Subject: [PATCH] Remove use of virConnectPtr from all remaining nwfilter code
43fe83
43fe83
https://bugzilla.redhat.com/show_bug.cgi?id=1015108
43fe83
43fe83
The virConnectPtr is passed around loads of nwfilter code in
43fe83
order to provide it as a parameter to the callback registered
43fe83
by the virt drivers. None of the virt drivers use this param
43fe83
though, so it serves no purpose.
43fe83
43fe83
Avoiding the need to pass a virConnectPtr means that the
43fe83
nwfilterStateReload method no longer needs to open a bogus
43fe83
QEMU driver connection. This addresses a race condition that
43fe83
can lead to a crash on startup.
43fe83
43fe83
The nwfilter driver starts before the QEMU driver and registers
43fe83
some callbacks with DBus to detect firewalld reload. If the
43fe83
firewalld reload happens while the QEMU driver is still starting
43fe83
up though, the nwfilterStateReload method will open a connection
43fe83
to the partially initialized QEMU driver and cause a crash.
43fe83
43fe83
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
43fe83
(cherry picked from commit 999d72fbd59ea712128ae294b69b6a54039d757b)
43fe83
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
43fe83
---
43fe83
 src/conf/nwfilter_conf.c       | 49 ++++++++++++++++--------------------------
43fe83
 src/conf/nwfilter_conf.h       | 14 +++++-------
43fe83
 src/lxc/lxc_driver.c           |  3 +--
43fe83
 src/nwfilter/nwfilter_driver.c | 42 ++++++++++++++----------------------
43fe83
 src/qemu/qemu_driver.c         |  3 +--
43fe83
 src/uml/uml_driver.c           |  3 +--
43fe83
 6 files changed, 43 insertions(+), 71 deletions(-)
43fe83
43fe83
diff --git a/src/conf/nwfilter_conf.c b/src/conf/nwfilter_conf.c
43fe83
index 424c396..1b86d36 100644
43fe83
--- a/src/conf/nwfilter_conf.c
43fe83
+++ b/src/conf/nwfilter_conf.c
43fe83
@@ -2743,8 +2743,7 @@ cleanup:
43fe83
 
43fe83
 
43fe83
 static int
43fe83
-_virNWFilterDefLoopDetect(virConnectPtr conn,
43fe83
-                          virNWFilterObjListPtr nwfilters,
43fe83
+_virNWFilterDefLoopDetect(virNWFilterObjListPtr nwfilters,
43fe83
                           virNWFilterDefPtr def,
43fe83
                           const char *filtername)
43fe83
 {
43fe83
@@ -2768,7 +2767,7 @@ _virNWFilterDefLoopDetect(virConnectPtr conn,
43fe83
             obj = virNWFilterObjFindByName(nwfilters,
43fe83
                                            entry->include->filterref);
43fe83
             if (obj) {
43fe83
-                rc = _virNWFilterDefLoopDetect(conn, nwfilters,
43fe83
+                rc = _virNWFilterDefLoopDetect(nwfilters,
43fe83
                                                obj->def, filtername);
43fe83
 
43fe83
                 virNWFilterObjUnlock(obj);
43fe83
@@ -2784,7 +2783,6 @@ _virNWFilterDefLoopDetect(virConnectPtr conn,
43fe83
 
43fe83
 /*
43fe83
  * virNWFilterDefLoopDetect:
43fe83
- * @conn: pointer to virConnect object
43fe83
  * @nwfilters : the nwfilters to search
43fe83
  * @def : the filter definition that may add a loop and is to be tested
43fe83
  *
43fe83
@@ -2794,11 +2792,10 @@ _virNWFilterDefLoopDetect(virConnectPtr conn,
43fe83
  * Returns 0 in case no loop was detected, -1 otherwise.
43fe83
  */
43fe83
 static int
43fe83
-virNWFilterDefLoopDetect(virConnectPtr conn,
43fe83
-                         virNWFilterObjListPtr nwfilters,
43fe83
+virNWFilterDefLoopDetect(virNWFilterObjListPtr nwfilters,
43fe83
                          virNWFilterDefPtr def)
43fe83
 {
43fe83
-    return _virNWFilterDefLoopDetect(conn, nwfilters, def, def->name);
43fe83
+    return _virNWFilterDefLoopDetect(nwfilters, def, def->name);
43fe83
 }
43fe83
 
43fe83
 int nCallbackDriver;
43fe83
@@ -2857,7 +2854,7 @@ static void *virNWFilterDomainFWUpdateOpaque;
43fe83
  * error. This should be called upon reloading of the driver.
43fe83
  */
43fe83
 int
43fe83
-virNWFilterInstFiltersOnAllVMs(virConnectPtr conn)
43fe83
+virNWFilterInstFiltersOnAllVMs(void)
43fe83
 {
43fe83
     size_t i;
43fe83
     struct domUpdateCBStruct cb = {
43fe83
@@ -2867,15 +2864,14 @@ virNWFilterInstFiltersOnAllVMs(virConnectPtr conn)
43fe83
     };
43fe83
 
43fe83
     for (i = 0; i < nCallbackDriver; i++)
43fe83
-        callbackDrvArray[i]->vmFilterRebuild(conn,
43fe83
-                                             virNWFilterDomainFWUpdateCB,
43fe83
+        callbackDrvArray[i]->vmFilterRebuild(virNWFilterDomainFWUpdateCB,
43fe83
                                              &cb;;
43fe83
 
43fe83
     return 0;
43fe83
 }
43fe83
 
43fe83
 static int
43fe83
-virNWFilterTriggerVMFilterRebuild(virConnectPtr conn)
43fe83
+virNWFilterTriggerVMFilterRebuild(void)
43fe83
 {
43fe83
     size_t i;
43fe83
     int ret = 0;
43fe83
@@ -2889,8 +2885,7 @@ virNWFilterTriggerVMFilterRebuild(virConnectPtr conn)
43fe83
         return -1;
43fe83
 
43fe83
     for (i = 0; i < nCallbackDriver; i++) {
43fe83
-        if (callbackDrvArray[i]->vmFilterRebuild(conn,
43fe83
-                                                 virNWFilterDomainFWUpdateCB,
43fe83
+        if (callbackDrvArray[i]->vmFilterRebuild(virNWFilterDomainFWUpdateCB,
43fe83
                                                  &cb) < 0)
43fe83
             ret = -1;
43fe83
     }
43fe83
@@ -2899,15 +2894,13 @@ virNWFilterTriggerVMFilterRebuild(virConnectPtr conn)
43fe83
         cb.step = STEP_TEAR_NEW; /* rollback */
43fe83
 
43fe83
         for (i = 0; i < nCallbackDriver; i++)
43fe83
-            callbackDrvArray[i]->vmFilterRebuild(conn,
43fe83
-                                                 virNWFilterDomainFWUpdateCB,
43fe83
+            callbackDrvArray[i]->vmFilterRebuild(virNWFilterDomainFWUpdateCB,
43fe83
                                                  &cb;;
43fe83
     } else {
43fe83
         cb.step = STEP_TEAR_OLD; /* switch over */
43fe83
 
43fe83
         for (i = 0; i < nCallbackDriver; i++)
43fe83
-            callbackDrvArray[i]->vmFilterRebuild(conn,
43fe83
-                                                 virNWFilterDomainFWUpdateCB,
43fe83
+            callbackDrvArray[i]->vmFilterRebuild(virNWFilterDomainFWUpdateCB,
43fe83
                                                  &cb;;
43fe83
     }
43fe83
 
43fe83
@@ -2918,14 +2911,13 @@ virNWFilterTriggerVMFilterRebuild(virConnectPtr conn)
43fe83
 
43fe83
 
43fe83
 int
43fe83
-virNWFilterTestUnassignDef(virConnectPtr conn,
43fe83
-                           virNWFilterObjPtr nwfilter)
43fe83
+virNWFilterTestUnassignDef(virNWFilterObjPtr nwfilter)
43fe83
 {
43fe83
     int rc = 0;
43fe83
 
43fe83
     nwfilter->wantRemoved = 1;
43fe83
     /* trigger the update on VMs referencing the filter */
43fe83
-    if (virNWFilterTriggerVMFilterRebuild(conn))
43fe83
+    if (virNWFilterTriggerVMFilterRebuild())
43fe83
         rc = -1;
43fe83
 
43fe83
     nwfilter->wantRemoved = 0;
43fe83
@@ -2964,8 +2956,7 @@ cleanup:
43fe83
 }
43fe83
 
43fe83
 virNWFilterObjPtr
43fe83
-virNWFilterObjAssignDef(virConnectPtr conn,
43fe83
-                        virNWFilterObjListPtr nwfilters,
43fe83
+virNWFilterObjAssignDef(virNWFilterObjListPtr nwfilters,
43fe83
                         virNWFilterDefPtr def)
43fe83
 {
43fe83
     virNWFilterObjPtr nwfilter;
43fe83
@@ -2984,7 +2975,7 @@ virNWFilterObjAssignDef(virConnectPtr conn,
43fe83
         virNWFilterObjUnlock(nwfilter);
43fe83
     }
43fe83
 
43fe83
-    if (virNWFilterDefLoopDetect(conn, nwfilters, def) < 0) {
43fe83
+    if (virNWFilterDefLoopDetect(nwfilters, def) < 0) {
43fe83
         virReportError(VIR_ERR_OPERATION_FAILED,
43fe83
                        "%s", _("filter would introduce a loop"));
43fe83
         return NULL;
43fe83
@@ -3003,7 +2994,7 @@ virNWFilterObjAssignDef(virConnectPtr conn,
43fe83
 
43fe83
         nwfilter->newDef = def;
43fe83
         /* trigger the update on VMs referencing the filter */
43fe83
-        if (virNWFilterTriggerVMFilterRebuild(conn)) {
43fe83
+        if (virNWFilterTriggerVMFilterRebuild()) {
43fe83
             nwfilter->newDef = NULL;
43fe83
             virNWFilterUnlockFilterUpdates();
43fe83
             virNWFilterObjUnlock(nwfilter);
43fe83
@@ -3045,8 +3036,7 @@ virNWFilterObjAssignDef(virConnectPtr conn,
43fe83
 
43fe83
 
43fe83
 static virNWFilterObjPtr
43fe83
-virNWFilterObjLoad(virConnectPtr conn,
43fe83
-                   virNWFilterObjListPtr nwfilters,
43fe83
+virNWFilterObjLoad(virNWFilterObjListPtr nwfilters,
43fe83
                    const char *file,
43fe83
                    const char *path)
43fe83
 {
43fe83
@@ -3065,7 +3055,7 @@ virNWFilterObjLoad(virConnectPtr conn,
43fe83
         return NULL;
43fe83
     }
43fe83
 
43fe83
-    if (!(nwfilter = virNWFilterObjAssignDef(conn, nwfilters, def))) {
43fe83
+    if (!(nwfilter = virNWFilterObjAssignDef(nwfilters, def))) {
43fe83
         virNWFilterDefFree(def);
43fe83
         return NULL;
43fe83
     }
43fe83
@@ -3081,8 +3071,7 @@ virNWFilterObjLoad(virConnectPtr conn,
43fe83
 
43fe83
 
43fe83
 int
43fe83
-virNWFilterLoadAllConfigs(virConnectPtr conn,
43fe83
-                          virNWFilterObjListPtr nwfilters,
43fe83
+virNWFilterLoadAllConfigs(virNWFilterObjListPtr nwfilters,
43fe83
                           const char *configDir)
43fe83
 {
43fe83
     DIR *dir;
43fe83
@@ -3110,7 +3099,7 @@ virNWFilterLoadAllConfigs(virConnectPtr conn,
43fe83
         if (!(path = virFileBuildPath(configDir, entry->d_name, NULL)))
43fe83
             continue;
43fe83
 
43fe83
-        nwfilter = virNWFilterObjLoad(conn, nwfilters, entry->d_name, path);
43fe83
+        nwfilter = virNWFilterObjLoad(nwfilters, entry->d_name, path);
43fe83
         if (nwfilter)
43fe83
             virNWFilterObjUnlock(nwfilter);
43fe83
 
43fe83
diff --git a/src/conf/nwfilter_conf.h b/src/conf/nwfilter_conf.h
43fe83
index e470615..29906f1 100644
43fe83
--- a/src/conf/nwfilter_conf.h
43fe83
+++ b/src/conf/nwfilter_conf.h
43fe83
@@ -687,12 +687,10 @@ int virNWFilterObjSaveDef(virNWFilterDriverStatePtr driver,
43fe83
 
43fe83
 int virNWFilterObjDeleteDef(virNWFilterObjPtr nwfilter);
43fe83
 
43fe83
-virNWFilterObjPtr virNWFilterObjAssignDef(virConnectPtr conn,
43fe83
-                                          virNWFilterObjListPtr nwfilters,
43fe83
+virNWFilterObjPtr virNWFilterObjAssignDef(virNWFilterObjListPtr nwfilters,
43fe83
                                           virNWFilterDefPtr def);
43fe83
 
43fe83
-int virNWFilterTestUnassignDef(virConnectPtr conn,
43fe83
-                               virNWFilterObjPtr nwfilter);
43fe83
+int virNWFilterTestUnassignDef(virNWFilterObjPtr nwfilter);
43fe83
 
43fe83
 virNWFilterDefPtr virNWFilterDefParseNode(xmlDocPtr xml,
43fe83
                                           xmlNodePtr root);
43fe83
@@ -706,8 +704,7 @@ int virNWFilterSaveXML(const char *configDir,
43fe83
 int virNWFilterSaveConfig(const char *configDir,
43fe83
                           virNWFilterDefPtr def);
43fe83
 
43fe83
-int virNWFilterLoadAllConfigs(virConnectPtr conn,
43fe83
-                              virNWFilterObjListPtr nwfilters,
43fe83
+int virNWFilterLoadAllConfigs(virNWFilterObjListPtr nwfilters,
43fe83
                               const char *configDir);
43fe83
 
43fe83
 char *virNWFilterConfigFile(const char *dir,
43fe83
@@ -725,11 +722,10 @@ void virNWFilterUnlockFilterUpdates(void);
43fe83
 int virNWFilterConfLayerInit(virDomainObjListIterator domUpdateCB, void *opaque);
43fe83
 void virNWFilterConfLayerShutdown(void);
43fe83
 
43fe83
-int virNWFilterInstFiltersOnAllVMs(virConnectPtr conn);
43fe83
+int virNWFilterInstFiltersOnAllVMs(void);
43fe83
 
43fe83
 
43fe83
-typedef int (*virNWFilterRebuild)(virConnectPtr conn,
43fe83
-                                  virDomainObjListIterator domUpdateCB,
43fe83
+typedef int (*virNWFilterRebuild)(virDomainObjListIterator domUpdateCB,
43fe83
                                   void *data);
43fe83
 typedef void (*virNWFilterVoidCall)(void);
43fe83
 
43fe83
diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c
43fe83
index bafbe93..7999b8c 100644
43fe83
--- a/src/lxc/lxc_driver.c
43fe83
+++ b/src/lxc/lxc_driver.c
43fe83
@@ -84,8 +84,7 @@ virLXCDriverPtr lxc_driver = NULL;
43fe83
 
43fe83
 /* callbacks for nwfilter */
43fe83
 static int
43fe83
-lxcVMFilterRebuild(virConnectPtr conn ATTRIBUTE_UNUSED,
43fe83
-                   virDomainObjListIterator iter, void *data)
43fe83
+lxcVMFilterRebuild(virDomainObjListIterator iter, void *data)
43fe83
 {
43fe83
     return virDomainObjListForEach(lxc_driver->domains, iter, data);
43fe83
 }
43fe83
diff --git a/src/nwfilter/nwfilter_driver.c b/src/nwfilter/nwfilter_driver.c
43fe83
index 0a515e7..ce75c82 100644
43fe83
--- a/src/nwfilter/nwfilter_driver.c
43fe83
+++ b/src/nwfilter/nwfilter_driver.c
43fe83
@@ -230,8 +230,7 @@ nwfilterStateInitialize(bool privileged,
43fe83
 
43fe83
     VIR_FREE(base);
43fe83
 
43fe83
-    if (virNWFilterLoadAllConfigs(NULL,
43fe83
-                                  &driverState->nwfilters,
43fe83
+    if (virNWFilterLoadAllConfigs(&driverState->nwfilters,
43fe83
                                   driverState->configDir) < 0)
43fe83
         goto error;
43fe83
 
43fe83
@@ -267,37 +266,28 @@ err_free_driverstate:
43fe83
  * files and update its state
43fe83
  */
43fe83
 static int
43fe83
-nwfilterStateReload(void) {
43fe83
-    virConnectPtr conn;
43fe83
-
43fe83
-    if (!driverState) {
43fe83
+nwfilterStateReload(void)
43fe83
+{
43fe83
+    if (!driverState)
43fe83
         return -1;
43fe83
-    }
43fe83
 
43fe83
     if (!driverState->privileged)
43fe83
         return 0;
43fe83
 
43fe83
-    conn = virConnectOpen("qemu:///system");
43fe83
-
43fe83
-    if (conn) {
43fe83
-        virNWFilterDHCPSnoopEnd(NULL);
43fe83
-        /* shut down all threads -- they will be restarted if necessary */
43fe83
-        virNWFilterLearnThreadsTerminate(true);
43fe83
-
43fe83
-        nwfilterDriverLock(driverState);
43fe83
-        virNWFilterCallbackDriversLock();
43fe83
+    virNWFilterDHCPSnoopEnd(NULL);
43fe83
+    /* shut down all threads -- they will be restarted if necessary */
43fe83
+    virNWFilterLearnThreadsTerminate(true);
43fe83
 
43fe83
-        virNWFilterLoadAllConfigs(conn,
43fe83
-                                  &driverState->nwfilters,
43fe83
-                                  driverState->configDir);
43fe83
+    nwfilterDriverLock(driverState);
43fe83
+    virNWFilterCallbackDriversLock();
43fe83
 
43fe83
-        virNWFilterCallbackDriversUnlock();
43fe83
-        nwfilterDriverUnlock(driverState);
43fe83
+    virNWFilterLoadAllConfigs(&driverState->nwfilters,
43fe83
+                              driverState->configDir);
43fe83
 
43fe83
-        virNWFilterInstFiltersOnAllVMs(conn);
43fe83
+    virNWFilterCallbackDriversUnlock();
43fe83
+    nwfilterDriverUnlock(driverState);
43fe83
 
43fe83
-        virConnectClose(conn);
43fe83
-    }
43fe83
+    virNWFilterInstFiltersOnAllVMs();
43fe83
 
43fe83
     return 0;
43fe83
 }
43fe83
@@ -568,7 +558,7 @@ nwfilterDefineXML(virConnectPtr conn,
43fe83
     if (virNWFilterDefineXMLEnsureACL(conn, def) < 0)
43fe83
         goto cleanup;
43fe83
 
43fe83
-    if (!(nwfilter = virNWFilterObjAssignDef(conn, &driver->nwfilters, def)))
43fe83
+    if (!(nwfilter = virNWFilterObjAssignDef(&driver->nwfilters, def)))
43fe83
         goto cleanup;
43fe83
 
43fe83
     if (virNWFilterObjSaveDef(driver, nwfilter, def) < 0) {
43fe83
@@ -612,7 +602,7 @@ nwfilterUndefine(virNWFilterPtr obj) {
43fe83
     if (virNWFilterUndefineEnsureACL(obj->conn, nwfilter->def) < 0)
43fe83
         goto cleanup;
43fe83
 
43fe83
-    if (virNWFilterTestUnassignDef(obj->conn, nwfilter) < 0) {
43fe83
+    if (virNWFilterTestUnassignDef(nwfilter) < 0) {
43fe83
         virReportError(VIR_ERR_OPERATION_INVALID,
43fe83
                        "%s",
43fe83
                        _("nwfilter is in use"));
43fe83
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
43fe83
index fd548b2..3820303 100644
43fe83
--- a/src/qemu/qemu_driver.c
43fe83
+++ b/src/qemu/qemu_driver.c
43fe83
@@ -177,8 +177,7 @@ static void
43fe83
 qemuVMDriverUnlock(void) {}
43fe83
 
43fe83
 static int
43fe83
-qemuVMFilterRebuild(virConnectPtr conn ATTRIBUTE_UNUSED,
43fe83
-                    virDomainObjListIterator iter, void *data)
43fe83
+qemuVMFilterRebuild(virDomainObjListIterator iter, void *data)
43fe83
 {
43fe83
     return virDomainObjListForEach(qemu_driver->domains, iter, data);
43fe83
 }
43fe83
diff --git a/src/uml/uml_driver.c b/src/uml/uml_driver.c
43fe83
index 9ca352f..eb02542 100644
43fe83
--- a/src/uml/uml_driver.c
43fe83
+++ b/src/uml/uml_driver.c
43fe83
@@ -148,8 +148,7 @@ static int umlMonitorCommand(const struct uml_driver *driver,
43fe83
 static struct uml_driver *uml_driver = NULL;
43fe83
 
43fe83
 static int
43fe83
-umlVMFilterRebuild(virConnectPtr conn ATTRIBUTE_UNUSED,
43fe83
-                   virDomainObjListIterator iter, void *data)
43fe83
+umlVMFilterRebuild(virDomainObjListIterator iter, void *data)
43fe83
 {
43fe83
     return virDomainObjListForEach(uml_driver->domains, iter, data);
43fe83
 }
43fe83
-- 
43fe83
1.8.3.2
43fe83