Signed-off-by: Tim Wiederhake <twiederh@xxxxxxxxxx> --- src/nwfilter/nwfilter_driver.c | 83 +++++++++++++--------------------- 1 file changed, 32 insertions(+), 51 deletions(-) diff --git a/src/nwfilter/nwfilter_driver.c b/src/nwfilter/nwfilter_driver.c index 8eea9e5805..12bbbc661f 100644 --- a/src/nwfilter/nwfilter_driver.c +++ b/src/nwfilter/nwfilter_driver.c @@ -57,15 +57,6 @@ static int nwfilterStateReload(void); static virMutex mutex = VIR_MUTEX_INITIALIZER; -static void nwfilterDriverLock(void) -{ - virMutexLock(&mutex); -} -static void nwfilterDriverUnlock(void) -{ - virMutexUnlock(&mutex); -} - #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(&mutex); 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(&mutex) { + 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(&mutex) { + 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(&mutex) { + obj = virNWFilterObjListFindByName(driver->nwfilters, name); + } if (!obj) { virReportError(VIR_ERR_NO_NWFILTER, @@ -491,17 +477,16 @@ nwfilterConnectListNWFilters(virConnectPtr conn, char **const names, int maxnames) { - int nnames; + VIR_LOCK_GUARD lock = { NULL }; if (virConnectListNWFiltersEnsureACL(conn) < 0) return -1; - nwfilterDriverLock(); - nnames = virNWFilterObjListGetNames(driver->nwfilters, conn, - virConnectListNWFiltersCheckACL, - names, maxnames); - nwfilterDriverUnlock(); - return nnames; + lock = virLockGuardLock(&mutex); + + return virNWFilterObjListGetNames(driver->nwfilters, conn, + virConnectListNWFiltersCheckACL, + names, maxnames); } @@ -510,19 +495,17 @@ nwfilterConnectListAllNWFilters(virConnectPtr conn, virNWFilterPtr **nwfilters, unsigned int flags) { - int ret; + VIR_LOCK_GUARD lock = { NULL }; virCheckFlags(0, -1); if (virConnectListAllNWFiltersEnsureACL(conn) < 0) return -1; - nwfilterDriverLock(); - ret = virNWFilterObjListExport(conn, driver->nwfilters, nwfilters, - virConnectListAllNWFiltersCheckACL); - nwfilterDriverUnlock(); + lock = virLockGuardLock(&mutex); - return ret; + return virNWFilterObjListExport(conn, driver->nwfilters, nwfilters, + virConnectListAllNWFiltersCheckACL); } @@ -531,6 +514,7 @@ nwfilterDefineXMLFlags(virConnectPtr conn, const char *xml, unsigned int flags) { + VIR_LOCK_GUARD lock = virLockGuardLock(&mutex); virNWFilterDef *def; virNWFilterObj *obj = NULL; virNWFilterDef *objdef; @@ -545,7 +529,6 @@ nwfilterDefineXMLFlags(virConnectPtr conn, return NULL; } - nwfilterDriverLock(); virNWFilterWriteLockFilterUpdates(); if (!(def = virNWFilterDefParseString(xml, flags))) @@ -572,7 +555,6 @@ nwfilterDefineXMLFlags(virConnectPtr conn, virNWFilterObjUnlock(obj); virNWFilterUnlockFilterUpdates(); - nwfilterDriverUnlock(); return nwfilter; } @@ -588,11 +570,11 @@ nwfilterDefineXML(virConnectPtr conn, static int nwfilterUndefine(virNWFilterPtr nwfilter) { + VIR_LOCK_GUARD lock = virLockGuardLock(&mutex); virNWFilterObj *obj; virNWFilterDef *def; int ret = -1; - nwfilterDriverLock(); virNWFilterWriteLockFilterUpdates(); if (!(obj = nwfilterObjFromNWFilter(nwfilter->uuid))) @@ -621,7 +603,6 @@ nwfilterUndefine(virNWFilterPtr nwfilter) virNWFilterObjUnlock(obj); virNWFilterUnlockFilterUpdates(); - nwfilterDriverUnlock(); return ret; } @@ -630,15 +611,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(&mutex) { + obj = nwfilterObjFromNWFilter(nwfilter->uuid); + } if (!obj) return NULL; -- 2.31.1