Change in vdsm[master]: netlink: rtnl_put_link after rtnl_link_get_kernel
phoracek at redhat.com
phoracek at redhat.com
Tue May 12 11:29:53 UTC 2015
Petr Horáček has uploaded a new change for review.
Change subject: netlink: rtnl_put_link after rtnl_link_get_kernel
......................................................................
netlink: rtnl_put_link after rtnl_link_get_kernel
According to netlink documentation [1] we have to decrement reference
counter when we don't need link (obtained from rtnl_link_get_kernel)
anymore.
[1] http://www.infradead.org/~tgr/libnl/doc/route.html#link_direct_lookup
Change-Id: Iefa70103c7c0f218c89abbe5c8371b745064a13e
Bug-Url: https://bugzilla.redhat.com/1158108
Signed-off-by: Petr Horáček <phoracek at redhat.com>
---
M lib/vdsm/netlink/link.py
1 file changed, 31 insertions(+), 21 deletions(-)
git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/12/40812/1
diff --git a/lib/vdsm/netlink/link.py b/lib/vdsm/netlink/link.py
index 1797902..8ce0ce5 100644
--- a/lib/vdsm/netlink/link.py
+++ b/lib/vdsm/netlink/link.py
@@ -16,6 +16,7 @@
#
# Refer to the README and COPYING files for full details of the license
#
+from contextlib import contextmanager
from ctypes import (CFUNCTYPE, byref, c_char, c_char_p, c_int, c_void_p,
c_size_t, sizeof)
from functools import partial
@@ -24,18 +25,19 @@
from . import _cache_manager, _nl_cache_get_first, _nl_cache_get_next
from . import _char_proto, _int_char_proto, _int_proto, _void_proto
-from . import LIBNL_ROUTE, _nl_geterror, _pool
+from . import LIBNL_ROUTE, _nl_geterror, _pool, _none_proto
from . import _addr_to_str, CHARBUFFSIZE
def get_link(name):
"""Returns the information dictionary of the name specified link."""
with _pool.socket() as sock:
- link = _get_link(name=name, sock=sock)
- if not link:
- raise IOError(errno.ENODEV, '%s is not present in the system' %
- name)
- return _link_info(link)
+ with _get_link(name=name, sock=sock) as link:
+ if not link:
+ raise IOError(errno.ENODEV, '%s is not present in the system' %
+ name)
+ link_info = _link_info(link)
+ return link_info
def iter_links():
@@ -95,11 +97,12 @@
name = (c_char * CHARBUFFSIZE)()
if cache is None:
- link = _get_link(index=link_index)
- if link is None:
- raise IOError(errno.ENODEV, 'Dev with index %s is not present in '
- 'the system' % link_index)
- return _rtnl_link_get_name(link)
+ with _get_link(index=link_index) as link:
+ if link is None:
+ raise IOError(errno.ENODEV, 'Dev with index %s is not present '
+ 'in the system' % link_index)
+ name = _rtnl_link_get_name(link)
+ return name
else:
return _rtnl_link_i2name(cache, link_index, name, sizeof(name))
@@ -143,6 +146,7 @@
return -1
+ at contextmanager
def _get_link(name=None, index=0, sock=None):
""" If defined both name and index, index is primary """
# libnl/incluede/netlink/errno.h
@@ -151,17 +155,22 @@
if name is None and index == 0:
raise ValueError('Must specify either a name or an index')
link = c_void_p()
- if sock is None:
- with _pool.socket() as sock:
- err = _rtnl_link_get_kernel(sock, index, name, byref(link))
- else:
- err = _rtnl_link_get_kernel(sock, index, name, byref(link))
- if err:
- if -err == NLE_NODEV:
- link = None
+
+ try:
+ if sock is None:
+ with _pool.socket() as sock:
+ err = _rtnl_link_get_kernel(sock, index, name, byref(link))
else:
- raise IOError(-err, _nl_geterror())
- return link
+ err = _rtnl_link_get_kernel(sock, index, name, byref(link))
+ if err:
+ if -err == NLE_NODEV:
+ link = None
+ else:
+ raise IOError(-err, _nl_geterror())
+ yield link
+ finally:
+ if link is not None:
+ _rtnl_link_put(link)
_nl_link_cache = partial(_cache_manager, _rtnl_link_alloc_cache)
@@ -180,3 +189,4 @@
'rtnl_link_i2name', LIBNL_ROUTE))
_rtnl_link_operstate2str = _int_char_proto(('rtnl_link_operstate2str',
LIBNL_ROUTE))
+_rtnl_link_put = _none_proto(('rtnl_link_put', LIBNL_ROUTE))
--
To view, visit https://gerrit.ovirt.org/40812
To unsubscribe, visit https://gerrit.ovirt.org/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Iefa70103c7c0f218c89abbe5c8371b745064a13e
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Petr Horáček <phoracek at redhat.com>
More information about the vdsm-patches
mailing list