virnetlink: Use automatic memory 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-16 18:54:32 +01:00
parent 22e67e4e67
commit 20d2cf47bc

View File

@ -799,18 +799,6 @@ virNetlinkGetErrorCode(struct nlmsghdr *resp, unsigned int recvbuflen)
}
static void
virNetlinkEventServerLock(virNetlinkEventSrvPrivate *driver)
{
virMutexLock(&driver->lock);
}
static void
virNetlinkEventServerUnlock(virNetlinkEventSrvPrivate *driver)
{
virMutexUnlock(&driver->lock);
}
/**
* virNetlinkEventRemoveClientPrimitive:
*
@ -869,11 +857,10 @@ virNetlinkEventCallback(int watch,
return;
}
virNetlinkEventServerLock(srv);
VIR_DEBUG("dispatching to max %d clients, called from event watch %d",
(int)srv->handlesCount, watch);
VIR_WITH_MUTEX_LOCK_GUARD(&srv->lock) {
for (i = 0; i < srv->handlesCount; i++) {
if (srv->handles[i].deleted != VIR_NETLINK_HANDLE_VALID)
continue;
@ -883,11 +870,10 @@ virNetlinkEventCallback(int watch,
(srv->handles[i].handleCB)(msg, length, &peer, &handled,
srv->handles[i].opaque);
}
}
if (!handled)
VIR_DEBUG("event not handled.");
virNetlinkEventServerUnlock(srv);
}
/**
@ -916,7 +902,7 @@ virNetlinkEventServiceStop(unsigned int protocol)
if (!server[protocol])
return 0;
virNetlinkEventServerLock(srv);
VIR_WITH_MUTEX_LOCK_GUARD(&srv->lock) {
nl_close(srv->netlinknh);
virNetlinkFree(srv->netlinknh);
virEventRemoveHandle(srv->eventwatch);
@ -929,7 +915,7 @@ virNetlinkEventServiceStop(unsigned int protocol)
server[protocol] = NULL;
VIR_FREE(srv->handles);
virNetlinkEventServerUnlock(srv);
}
virMutexDestroy(&srv->lock);
VIR_FREE(srv);
@ -1036,29 +1022,28 @@ virNetlinkEventServiceStart(unsigned int protocol, unsigned int groups)
return -1;
}
virNetlinkEventServerLock(srv);
VIR_WITH_MUTEX_LOCK_GUARD(&srv->lock) {
/* Allocate a new socket and get fd */
if (!(srv->netlinknh = virNetlinkCreateSocket(protocol)))
goto error_locked;
goto error;
fd = nl_socket_get_fd(srv->netlinknh);
if (fd < 0) {
virReportSystemError(errno,
"%s", _("cannot get netlink socket fd"));
goto error_server;
goto error;
}
if (groups && nl_socket_add_membership(srv->netlinknh, groups) < 0) {
virReportSystemError(errno,
"%s", _("cannot add netlink membership"));
goto error_server;
goto error;
}
if (nl_socket_set_nonblocking(srv->netlinknh)) {
virReportSystemError(errno, "%s",
_("cannot set netlink socket nonblocking"));
goto error_server;
goto error;
}
if ((srv->eventwatch = virEventAddHandle(fd,
@ -1067,7 +1052,7 @@ virNetlinkEventServiceStart(unsigned int protocol, unsigned int groups)
srv, NULL)) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Failed to add netlink event handle watch"));
goto error_server;
goto error;
}
srv->netlinkfd = fd;
@ -1076,13 +1061,13 @@ virNetlinkEventServiceStart(unsigned int protocol, unsigned int groups)
ret = 0;
server[protocol] = srv;
error_server:
if (ret < 0) {
error:
if (ret < 0 && srv->netlinknh) {
nl_close(srv->netlinknh);
virNetlinkFree(srv->netlinknh);
}
error_locked:
virNetlinkEventServerUnlock(srv);
}
if (ret < 0) {
virMutexDestroy(&srv->lock);
VIR_FREE(srv);
@ -1129,8 +1114,7 @@ virNetlinkEventAddClient(virNetlinkEventHandleCallback handleCB,
return -1;
}
virNetlinkEventServerLock(srv);
VIR_WITH_MUTEX_LOCK_GUARD(&srv->lock) {
VIR_DEBUG("adding client: %d.", nextWatch);
/* first try to re-use deleted free slots */
@ -1163,11 +1147,11 @@ virNetlinkEventAddClient(virNetlinkEventHandleCallback handleCB,
virMacAddrSetRaw(&srv->handles[r].macaddr,
(unsigned char[VIR_MAC_BUFLEN]){0, 0, 0, 0, 0, 0});
VIR_DEBUG("added client to loop slot: %d. with macaddr ptr=%p", r, macaddr);
ret = nextWatch++;
virNetlinkEventServerUnlock(srv);
VIR_DEBUG("added client to loop slot: %d. with macaddr ptr=%p", r, macaddr);
}
return ret;
}
@ -1189,7 +1173,6 @@ virNetlinkEventRemoveClient(int watch, const virMacAddr *macaddr,
unsigned int protocol)
{
size_t i;
int ret = -1;
virNetlinkEventSrvPrivate *srv = NULL;
if (protocol >= MAX_LINKS)
@ -1204,8 +1187,7 @@ virNetlinkEventRemoveClient(int watch, const virMacAddr *macaddr,
return -1;
}
virNetlinkEventServerLock(srv);
VIR_WITH_MUTEX_LOCK_GUARD(&srv->lock) {
for (i = 0; i < srv->handlesCount; i++) {
if (srv->handles[i].deleted != VIR_NETLINK_HANDLE_VALID)
continue;
@ -1217,15 +1199,13 @@ virNetlinkEventRemoveClient(int watch, const virMacAddr *macaddr,
VIR_DEBUG("removed client: %d by %s.",
srv->handles[i].watch, watch ? "index" : "mac");
virNetlinkEventRemoveClientPrimitive(i, protocol);
ret = 0;
goto cleanup;
return 0;
}
}
}
VIR_DEBUG("no client found to remove.");
cleanup:
virNetlinkEventServerUnlock(srv);
return ret;
return -1;
}
#else