Signed-off-by: Tim Wiederhake <twiederh@xxxxxxxxxx> --- src/nwfilter/nwfilter_driver.c | 83 ++++++++++++++-------------------- 1 file changed, 34 insertions(+), 49 deletions(-) diff --git a/src/nwfilter/nwfilter_driver.c b/src/nwfilter/nwfilter_driver.c index eefb2b0fff..bfda96b7ed 100644 --- a/src/nwfilter/nwfilter_driver.c +++ b/src/nwfilter/nwfilter_driver.c @@ -57,15 +57,6 @@ static int nwfilterStateReload(void); static virMutex driverMutex = VIR_MUTEX_INITIALIZER; -static void nwfilterDriverLock(void) -{ - virMutexLock(&driverMutex); -} -static void nwfilterDriverUnlock(void) -{ - virMutexUnlock(&driverMutex); -} - #ifdef WITH_FIREWALLD static void @@ -204,6 +195,7 @@ nwfilterStateInitialize(bool privileged, virStateInhibitCallback callback G_GNUC_UNUSED, void *opaque G_GNUC_UNUSED) { + VIR_LOCK_GUARD lock = virLockGuardLock(&driverMutex); GDBusConnection *sysbus = NULL; if (root != NULL) { @@ -230,8 +222,6 @@ nwfilterStateInitialize(bool privileged, if (!privileged) return VIR_DRV_STATE_INIT_SKIPPED; - nwfilterDriverLock(); - driver->stateDir = g_strdup(RUNSTATEDIR "/libvirt/nwfilter"); if (g_mkdir_with_parents(driver->stateDir, S_IRWXU) < 0) { @@ -290,13 +280,10 @@ nwfilterStateInitialize(bool privileged, if (virNWFilterBuildAll(driver, false) < 0) goto error; - nwfilterDriverUnlock(); - return VIR_DRV_STATE_INIT_COMPLETE; error: - nwfilterDriverUnlock(); - nwfilterStateCleanup(); + nwfilterStateCleanupLocked(); return VIR_DRV_STATE_INIT_ERROR; @@ -335,16 +322,15 @@ nwfilterStateReload(void) /* shut down all threads -- they will be restarted if necessary */ virNWFilterLearnThreadsTerminate(true); - nwfilterDriverLock(); - virNWFilterWriteLockFilterUpdates(); - - virNWFilterObjListLoadAllConfigs(driver->nwfilters, driver->configDir); + VIR_WITH_MUTEX_LOCK_GUARD(&driverMutex) { + virNWFilterWriteLockFilterUpdates(); - virNWFilterUnlockFilterUpdates(); + virNWFilterObjListLoadAllConfigs(driver->nwfilters, driver->configDir); - virNWFilterBuildAll(driver, false); + virNWFilterUnlockFilterUpdates(); - nwfilterDriverUnlock(); + virNWFilterBuildAll(driver, false); + } return 0; } @@ -422,13 +408,13 @@ static virNWFilterPtr nwfilterLookupByUUID(virConnectPtr conn, const unsigned char *uuid) { - virNWFilterObj *obj; + virNWFilterObj *obj = NULL; virNWFilterDef *def; virNWFilterPtr nwfilter = NULL; - nwfilterDriverLock(); - obj = nwfilterObjFromNWFilter(uuid); - nwfilterDriverUnlock(); + VIR_WITH_MUTEX_LOCK_GUARD(&driverMutex) { + obj = nwfilterObjFromNWFilter(uuid); + } if (!obj) return NULL; @@ -449,13 +435,13 @@ static virNWFilterPtr nwfilterLookupByName(virConnectPtr conn, const char *name) { - virNWFilterObj *obj; + virNWFilterObj *obj = NULL; virNWFilterDef *def; virNWFilterPtr nwfilter = NULL; - nwfilterDriverLock(); - obj = virNWFilterObjListFindByName(driver->nwfilters, name); - nwfilterDriverUnlock(); + VIR_WITH_MUTEX_LOCK_GUARD(&driverMutex) { + obj = virNWFilterObjListFindByName(driver->nwfilters, name); + } if (!obj) { virReportError(VIR_ERR_NO_NWFILTER, @@ -491,16 +477,17 @@ nwfilterConnectListNWFilters(virConnectPtr conn, char **const names, int maxnames) { - int nnames; + int nnames = -1; if (virConnectListNWFiltersEnsureACL(conn) < 0) return -1; - nwfilterDriverLock(); - nnames = virNWFilterObjListGetNames(driver->nwfilters, conn, - virConnectListNWFiltersCheckACL, - names, maxnames); - nwfilterDriverUnlock(); + VIR_WITH_MUTEX_LOCK_GUARD(&driverMutex) { + nnames = virNWFilterObjListGetNames(driver->nwfilters, conn, + virConnectListNWFiltersCheckACL, + names, maxnames); + } + return nnames; } @@ -510,17 +497,17 @@ nwfilterConnectListAllNWFilters(virConnectPtr conn, virNWFilterPtr **nwfilters, unsigned int flags) { - int ret; + int ret = -1; virCheckFlags(0, -1); if (virConnectListAllNWFiltersEnsureACL(conn) < 0) return -1; - nwfilterDriverLock(); - ret = virNWFilterObjListExport(conn, driver->nwfilters, nwfilters, - virConnectListAllNWFiltersCheckACL); - nwfilterDriverUnlock(); + VIR_WITH_MUTEX_LOCK_GUARD(&driverMutex) { + ret = virNWFilterObjListExport(conn, driver->nwfilters, nwfilters, + virConnectListAllNWFiltersCheckACL); + } return ret; } @@ -531,6 +518,7 @@ nwfilterDefineXMLFlags(virConnectPtr conn, const char *xml, unsigned int flags) { + VIR_LOCK_GUARD lock = virLockGuardLock(&driverMutex); virNWFilterDef *def; virNWFilterObj *obj = NULL; virNWFilterDef *objdef; @@ -545,7 +533,6 @@ nwfilterDefineXMLFlags(virConnectPtr conn, return NULL; } - nwfilterDriverLock(); virNWFilterWriteLockFilterUpdates(); if (!(def = virNWFilterDefParseString(xml, flags))) @@ -572,7 +559,6 @@ nwfilterDefineXMLFlags(virConnectPtr conn, virNWFilterObjUnlock(obj); virNWFilterUnlockFilterUpdates(); - nwfilterDriverUnlock(); return nwfilter; } @@ -588,11 +574,11 @@ nwfilterDefineXML(virConnectPtr conn, static int nwfilterUndefine(virNWFilterPtr nwfilter) { + VIR_LOCK_GUARD lock = virLockGuardLock(&driverMutex); virNWFilterObj *obj; virNWFilterDef *def; int ret = -1; - nwfilterDriverLock(); virNWFilterWriteLockFilterUpdates(); if (!(obj = nwfilterObjFromNWFilter(nwfilter->uuid))) @@ -621,7 +607,6 @@ nwfilterUndefine(virNWFilterPtr nwfilter) virNWFilterObjUnlock(obj); virNWFilterUnlockFilterUpdates(); - nwfilterDriverUnlock(); return ret; } @@ -630,15 +615,15 @@ static char * nwfilterGetXMLDesc(virNWFilterPtr nwfilter, unsigned int flags) { - virNWFilterObj *obj; + virNWFilterObj *obj = NULL; virNWFilterDef *def; char *ret = NULL; virCheckFlags(0, NULL); - nwfilterDriverLock(); - obj = nwfilterObjFromNWFilter(nwfilter->uuid); - nwfilterDriverUnlock(); + VIR_WITH_MUTEX_LOCK_GUARD(&driverMutex) { + obj = nwfilterObjFromNWFilter(nwfilter->uuid); + } if (!obj) return NULL; -- 2.31.1