Merge "Move "get_cdll" to a common place in privileged.agent.linux"

This commit is contained in:
Zuul 2020-06-24 16:24:02 +00:00 committed by Gerrit Code Review
commit 382fe757ec
2 changed files with 35 additions and 19 deletions

View File

@ -0,0 +1,32 @@
# Copyright 2020 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import ctypes
from ctypes import util as ctypes_util
_CDLL = None
def get_cdll():
global _CDLL
if not _CDLL:
# NOTE(ralonsoh): from https://docs.python.org/3.6/library/
# ctypes.html#ctypes.PyDLL: "Instances of this class behave like CDLL
# instances, except that the Python GIL is not released during the
# function call, and after the function execution the Python error
# flag is checked."
# Check https://bugs.launchpad.net/neutron/+bug/1870352
_CDLL = ctypes.PyDLL(ctypes_util.find_library('c'), use_errno=True)
return _CDLL

View File

@ -10,8 +10,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import ctypes
from ctypes import util as ctypes_util
import errno import errno
import os import os
import socket import socket
@ -29,6 +27,7 @@ from pyroute2 import netns
from neutron._i18n import _ from neutron._i18n import _
from neutron import privileged from neutron import privileged
from neutron.privileged.agent import linux as priv_linux
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
@ -37,21 +36,6 @@ _IP_VERSION_FAMILY_MAP = {4: socket.AF_INET, 6: socket.AF_INET6}
NETNS_RUN_DIR = '/var/run/netns' NETNS_RUN_DIR = '/var/run/netns'
_CDLL = None
def _get_cdll():
global _CDLL
if not _CDLL:
# NOTE(ralonsoh): from https://docs.python.org/3.6/library/
# ctypes.html#ctypes.PyDLL: "Instances of this class behave like CDLL
# instances, except that the Python GIL is not released during the
# function call, and after the function execution the Python error
# flag is checked."
# Check https://bugs.launchpad.net/neutron/+bug/1870352
_CDLL = ctypes.PyDLL(ctypes_util.find_library('c'), use_errno=True)
return _CDLL
def _get_scope_name(scope): def _get_scope_name(scope):
"""Return the name of the scope (given as a number), or the scope number """Return the name of the scope (given as a number), or the scope number
@ -532,7 +516,7 @@ def create_netns(name, **kwargs):
:param name: The name of the namespace to create :param name: The name of the namespace to create
""" """
try: try:
netns.create(name, libc=_get_cdll()) netns.create(name, libc=priv_linux.get_cdll())
except OSError as e: except OSError as e:
if e.errno != errno.EEXIST: if e.errno != errno.EEXIST:
raise raise
@ -545,7 +529,7 @@ def remove_netns(name, **kwargs):
:param name: The name of the namespace to remove :param name: The name of the namespace to remove
""" """
try: try:
netns.remove(name, libc=_get_cdll()) netns.remove(name, libc=priv_linux.get_cdll())
except OSError as e: except OSError as e:
if e.errno != errno.ENOENT: if e.errno != errno.ENOENT:
raise raise