interface_backend_netcf: Use automatic mutex management

Signed-off-by: Tim Wiederhake <twiederh@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Tim Wiederhake 2022-03-24 09:18:41 +01:00
parent 29bb566a22
commit 86f048c85e

View File

@ -155,31 +155,26 @@ netcfStateCleanup(void)
static int static int
netcfStateReload(void) netcfStateReload(void)
{ {
int ret = -1;
if (!driver) if (!driver)
return 0; return 0;
virObjectLock(driver); VIR_WITH_OBJECT_LOCK_GUARD(driver) {
ncf_close(driver->netcf); ncf_close(driver->netcf);
if (ncf_init(&driver->netcf, NULL) != 0) { if (ncf_init(&driver->netcf, NULL) != 0) {
/* this isn't a good situation, because we can't shut down the /* this isn't a good situation, because we can't shut down the
* driver as there may still be connections to it. If we set * driver as there may still be connections to it. If we set
* the netcf handle to NULL, any subsequent calls to netcf * the netcf handle to NULL, any subsequent calls to netcf
* will just fail rather than causing a crash. Not ideal, but * will just fail rather than causing a crash. Not ideal, but
* livable (since this should never happen). * livable (since this should never happen).
*/ */
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("failed to re-init netcf")); _("failed to re-init netcf"));
driver->netcf = NULL; driver->netcf = NULL;
goto cleanup; return -1;
}
} }
ret = 0; return 0;
cleanup:
virObjectUnlock(driver);
return ret;
} }
@ -528,66 +523,68 @@ static int netcfConnectListInterfacesImpl(virConnectPtr conn,
static int netcfConnectNumOfInterfaces(virConnectPtr conn) static int netcfConnectNumOfInterfaces(virConnectPtr conn)
{ {
int count; int count = -1;
if (virConnectNumOfInterfacesEnsureACL(conn) < 0) if (virConnectNumOfInterfacesEnsureACL(conn) < 0)
return -1; return -1;
virObjectLock(driver); VIR_WITH_OBJECT_LOCK_GUARD(driver) {
count = netcfConnectNumOfInterfacesImpl(conn, count = netcfConnectNumOfInterfacesImpl(conn,
NETCF_IFACE_ACTIVE, NETCF_IFACE_ACTIVE,
virConnectNumOfInterfacesCheckACL); virConnectNumOfInterfacesCheckACL);
virObjectUnlock(driver); }
return count; return count;
} }
static int netcfConnectListInterfaces(virConnectPtr conn, char **const names, int nnames) static int netcfConnectListInterfaces(virConnectPtr conn, char **const names, int nnames)
{ {
int count; int count = -1;
if (virConnectListInterfacesEnsureACL(conn) < 0) if (virConnectListInterfacesEnsureACL(conn) < 0)
return -1; return -1;
virObjectLock(driver); VIR_WITH_OBJECT_LOCK_GUARD(driver) {
count = netcfConnectListInterfacesImpl(conn, count = netcfConnectListInterfacesImpl(conn,
NETCF_IFACE_ACTIVE, NETCF_IFACE_ACTIVE,
names, nnames, names, nnames,
virConnectListInterfacesCheckACL); virConnectListInterfacesCheckACL);
virObjectUnlock(driver); }
return count;
return count;
} }
static int netcfConnectNumOfDefinedInterfaces(virConnectPtr conn) static int netcfConnectNumOfDefinedInterfaces(virConnectPtr conn)
{ {
int count; int count = -1;
if (virConnectNumOfDefinedInterfacesEnsureACL(conn) < 0) if (virConnectNumOfDefinedInterfacesEnsureACL(conn) < 0)
return -1; return -1;
virObjectLock(driver); VIR_WITH_OBJECT_LOCK_GUARD(driver) {
count = netcfConnectNumOfInterfacesImpl(conn, count = netcfConnectNumOfInterfacesImpl(conn,
NETCF_IFACE_INACTIVE, NETCF_IFACE_INACTIVE,
virConnectNumOfDefinedInterfacesCheckACL); virConnectNumOfDefinedInterfacesCheckACL);
virObjectUnlock(driver); }
return count; return count;
} }
static int netcfConnectListDefinedInterfaces(virConnectPtr conn, char **const names, int nnames) static int netcfConnectListDefinedInterfaces(virConnectPtr conn, char **const names, int nnames)
{ {
int count; int count = -1;
if (virConnectListDefinedInterfacesEnsureACL(conn) < 0) if (virConnectListDefinedInterfacesEnsureACL(conn) < 0)
return -1; return -1;
virObjectLock(driver); VIR_WITH_OBJECT_LOCK_GUARD(driver) {
count = netcfConnectListInterfacesImpl(conn, count = netcfConnectListInterfacesImpl(conn,
NETCF_IFACE_INACTIVE, NETCF_IFACE_INACTIVE,
names, nnames, names, nnames,
virConnectListDefinedInterfacesCheckACL); virConnectListDefinedInterfacesCheckACL);
virObjectUnlock(driver); }
return count;
return count;
} }
#define MATCH(FLAG) (flags & (FLAG)) #define MATCH(FLAG) (flags & (FLAG))
@ -731,8 +728,8 @@ static virInterfacePtr netcfInterfaceLookupByName(virConnectPtr conn,
struct netcf_if *iface; struct netcf_if *iface;
virInterfacePtr ret = NULL; virInterfacePtr ret = NULL;
g_autoptr(virInterfaceDef) def = NULL; g_autoptr(virInterfaceDef) def = NULL;
VIR_LOCK_GUARD lock = virObjectLockGuard(driver);
virObjectLock(driver);
iface = ncf_lookup_by_name(driver->netcf, name); iface = ncf_lookup_by_name(driver->netcf, name);
if (!iface) { if (!iface) {
const char *errmsg, *details; const char *errmsg, *details;
@ -759,7 +756,6 @@ static virInterfacePtr netcfInterfaceLookupByName(virConnectPtr conn,
cleanup: cleanup:
ncf_if_free(iface); ncf_if_free(iface);
virObjectUnlock(driver);
return ret; return ret;
} }
@ -770,8 +766,8 @@ static virInterfacePtr netcfInterfaceLookupByMACString(virConnectPtr conn,
int niface; int niface;
virInterfacePtr ret = NULL; virInterfacePtr ret = NULL;
g_autoptr(virInterfaceDef) def = NULL; g_autoptr(virInterfaceDef) def = NULL;
VIR_LOCK_GUARD lock = virObjectLockGuard(driver);
virObjectLock(driver);
niface = ncf_lookup_by_mac_string(driver->netcf, macstr, 1, &iface); niface = ncf_lookup_by_mac_string(driver->netcf, macstr, 1, &iface);
if (niface < 0) { if (niface < 0) {
@ -806,7 +802,6 @@ static virInterfacePtr netcfInterfaceLookupByMACString(virConnectPtr conn,
cleanup: cleanup:
ncf_if_free(iface); ncf_if_free(iface);
virObjectUnlock(driver);
return ret; return ret;
} }
@ -818,11 +813,10 @@ static char *netcfInterfaceGetXMLDesc(virInterfacePtr ifinfo,
g_autoptr(virInterfaceDef) ifacedef = NULL; g_autoptr(virInterfaceDef) ifacedef = NULL;
char *ret = NULL; char *ret = NULL;
bool active; bool active;
VIR_LOCK_GUARD lock = virObjectLockGuard(driver);
virCheckFlags(VIR_INTERFACE_XML_INACTIVE, NULL); virCheckFlags(VIR_INTERFACE_XML_INACTIVE, NULL);
virObjectLock(driver);
iface = interfaceDriverGetNetcfIF(driver->netcf, ifinfo); iface = interfaceDriverGetNetcfIF(driver->netcf, ifinfo);
if (!iface) { if (!iface) {
/* helper already reported error */ /* helper already reported error */
@ -865,7 +859,6 @@ static char *netcfInterfaceGetXMLDesc(virInterfacePtr ifinfo,
cleanup: cleanup:
ncf_if_free(iface); ncf_if_free(iface);
VIR_FREE(xmlstr); VIR_FREE(xmlstr);
virObjectUnlock(driver);
return ret; return ret;
} }
@ -877,11 +870,10 @@ static virInterfacePtr netcfInterfaceDefineXML(virConnectPtr conn,
char *xmlstr = NULL; char *xmlstr = NULL;
g_autoptr(virInterfaceDef) ifacedef = NULL; g_autoptr(virInterfaceDef) ifacedef = NULL;
virInterfacePtr ret = NULL; virInterfacePtr ret = NULL;
VIR_LOCK_GUARD lock = virObjectLockGuard(driver);
virCheckFlags(VIR_INTERFACE_DEFINE_VALIDATE, NULL); virCheckFlags(VIR_INTERFACE_DEFINE_VALIDATE, NULL);
virObjectLock(driver);
ifacedef = virInterfaceDefParseString(xml, flags); ifacedef = virInterfaceDefParseString(xml, flags);
if (!ifacedef) { if (!ifacedef) {
/* error was already reported */ /* error was already reported */
@ -913,7 +905,6 @@ static virInterfacePtr netcfInterfaceDefineXML(virConnectPtr conn,
cleanup: cleanup:
ncf_if_free(iface); ncf_if_free(iface);
VIR_FREE(xmlstr); VIR_FREE(xmlstr);
virObjectUnlock(driver);
return ret; return ret;
} }
@ -922,8 +913,7 @@ static int netcfInterfaceUndefine(virInterfacePtr ifinfo)
struct netcf_if *iface = NULL; struct netcf_if *iface = NULL;
g_autoptr(virInterfaceDef) def = NULL; g_autoptr(virInterfaceDef) def = NULL;
int ret = -1; int ret = -1;
VIR_LOCK_GUARD lock = virObjectLockGuard(driver);
virObjectLock(driver);
iface = interfaceDriverGetNetcfIF(driver->netcf, ifinfo); iface = interfaceDriverGetNetcfIF(driver->netcf, ifinfo);
if (!iface) { if (!iface) {
@ -951,7 +941,6 @@ static int netcfInterfaceUndefine(virInterfacePtr ifinfo)
cleanup: cleanup:
ncf_if_free(iface); ncf_if_free(iface);
virObjectUnlock(driver);
return ret; return ret;
} }
@ -962,11 +951,10 @@ static int netcfInterfaceCreate(virInterfacePtr ifinfo,
g_autoptr(virInterfaceDef) def = NULL; g_autoptr(virInterfaceDef) def = NULL;
int ret = -1; int ret = -1;
bool active; bool active;
VIR_LOCK_GUARD lock = virObjectLockGuard(driver);
virCheckFlags(0, -1); virCheckFlags(0, -1);
virObjectLock(driver);
iface = interfaceDriverGetNetcfIF(driver->netcf, ifinfo); iface = interfaceDriverGetNetcfIF(driver->netcf, ifinfo);
if (!iface) { if (!iface) {
/* helper already reported error */ /* helper already reported error */
@ -1002,7 +990,6 @@ static int netcfInterfaceCreate(virInterfacePtr ifinfo,
cleanup: cleanup:
ncf_if_free(iface); ncf_if_free(iface);
virObjectUnlock(driver);
return ret; return ret;
} }
@ -1013,11 +1000,10 @@ static int netcfInterfaceDestroy(virInterfacePtr ifinfo,
g_autoptr(virInterfaceDef) def = NULL; g_autoptr(virInterfaceDef) def = NULL;
int ret = -1; int ret = -1;
bool active; bool active;
VIR_LOCK_GUARD lock = virObjectLockGuard(driver);
virCheckFlags(0, -1); virCheckFlags(0, -1);
virObjectLock(driver);
iface = interfaceDriverGetNetcfIF(driver->netcf, ifinfo); iface = interfaceDriverGetNetcfIF(driver->netcf, ifinfo);
if (!iface) { if (!iface) {
/* helper already reported error */ /* helper already reported error */
@ -1053,7 +1039,6 @@ static int netcfInterfaceDestroy(virInterfacePtr ifinfo,
cleanup: cleanup:
ncf_if_free(iface); ncf_if_free(iface);
virObjectUnlock(driver);
return ret; return ret;
} }
@ -1063,8 +1048,7 @@ static int netcfInterfaceIsActive(virInterfacePtr ifinfo)
g_autoptr(virInterfaceDef) def = NULL; g_autoptr(virInterfaceDef) def = NULL;
int ret = -1; int ret = -1;
bool active; bool active;
VIR_LOCK_GUARD lock = virObjectLockGuard(driver);
virObjectLock(driver);
iface = interfaceDriverGetNetcfIF(driver->netcf, ifinfo); iface = interfaceDriverGetNetcfIF(driver->netcf, ifinfo);
if (!iface) { if (!iface) {
@ -1085,82 +1069,78 @@ static int netcfInterfaceIsActive(virInterfacePtr ifinfo)
cleanup: cleanup:
ncf_if_free(iface); ncf_if_free(iface);
virObjectUnlock(driver);
return ret; return ret;
} }
static int netcfInterfaceChangeBegin(virConnectPtr conn, unsigned int flags) static int netcfInterfaceChangeBegin(virConnectPtr conn, unsigned int flags)
{ {
int ret; int ret = -1;
virCheckFlags(0, -1); /* currently flags must be 0 */ virCheckFlags(0, -1); /* currently flags must be 0 */
if (virInterfaceChangeBeginEnsureACL(conn) < 0) if (virInterfaceChangeBeginEnsureACL(conn) < 0)
return -1; return -1;
virObjectLock(driver); VIR_WITH_OBJECT_LOCK_GUARD(driver) {
ret = ncf_change_begin(driver->netcf, 0);
ret = ncf_change_begin(driver->netcf, 0); if (ret < 0) {
if (ret < 0) { const char *errmsg, *details;
const char *errmsg, *details; int errcode = ncf_error(driver->netcf, &errmsg, &details);
int errcode = ncf_error(driver->netcf, &errmsg, &details); virReportError(netcf_to_vir_err(errcode),
virReportError(netcf_to_vir_err(errcode), _("failed to begin transaction: %s%s%s"),
_("failed to begin transaction: %s%s%s"), errmsg, details ? " - " : "",
errmsg, details ? " - " : "", NULLSTR_EMPTY(details));
NULLSTR_EMPTY(details)); }
} }
virObjectUnlock(driver);
return ret; return ret;
} }
static int netcfInterfaceChangeCommit(virConnectPtr conn, unsigned int flags) static int netcfInterfaceChangeCommit(virConnectPtr conn, unsigned int flags)
{ {
int ret; int ret = -1;
virCheckFlags(0, -1); /* currently flags must be 0 */ virCheckFlags(0, -1); /* currently flags must be 0 */
if (virInterfaceChangeCommitEnsureACL(conn) < 0) if (virInterfaceChangeCommitEnsureACL(conn) < 0)
return -1; return -1;
virObjectLock(driver); VIR_WITH_OBJECT_LOCK_GUARD(driver) {
ret = ncf_change_commit(driver->netcf, 0);
ret = ncf_change_commit(driver->netcf, 0); if (ret < 0) {
if (ret < 0) { const char *errmsg, *details;
const char *errmsg, *details; int errcode = ncf_error(driver->netcf, &errmsg, &details);
int errcode = ncf_error(driver->netcf, &errmsg, &details); virReportError(netcf_to_vir_err(errcode),
virReportError(netcf_to_vir_err(errcode), _("failed to commit transaction: %s%s%s"),
_("failed to commit transaction: %s%s%s"), errmsg, details ? " - " : "",
errmsg, details ? " - " : "", NULLSTR_EMPTY(details));
NULLSTR_EMPTY(details)); }
} }
virObjectUnlock(driver);
return ret; return ret;
} }
static int netcfInterfaceChangeRollback(virConnectPtr conn, unsigned int flags) static int netcfInterfaceChangeRollback(virConnectPtr conn, unsigned int flags)
{ {
int ret; int ret = -1;
virCheckFlags(0, -1); /* currently flags must be 0 */ virCheckFlags(0, -1); /* currently flags must be 0 */
if (virInterfaceChangeRollbackEnsureACL(conn) < 0) if (virInterfaceChangeRollbackEnsureACL(conn) < 0)
return -1; return -1;
virObjectLock(driver); VIR_WITH_OBJECT_LOCK_GUARD(driver) {
ret = ncf_change_rollback(driver->netcf, 0);
ret = ncf_change_rollback(driver->netcf, 0); if (ret < 0) {
if (ret < 0) { const char *errmsg, *details;
const char *errmsg, *details; int errcode = ncf_error(driver->netcf, &errmsg, &details);
int errcode = ncf_error(driver->netcf, &errmsg, &details); virReportError(netcf_to_vir_err(errcode),
virReportError(netcf_to_vir_err(errcode), _("failed to rollback transaction: %s%s%s"),
_("failed to rollback transaction: %s%s%s"), errmsg, details ? " - " : "",
errmsg, details ? " - " : "", NULLSTR_EMPTY(details));
NULLSTR_EMPTY(details)); }
} }
virObjectUnlock(driver);
return ret; return ret;
} }