Merge "Implement "list_ns_pids" inside Neutron"

This commit is contained in:
Zuul 2019-09-13 05:20:58 +00:00 committed by Gerrit Code Review
commit 9fcaa8376b

View File

@ -11,6 +11,7 @@
# under the License. # under the License.
import errno import errno
import os
import socket import socket
from neutron_lib import constants from neutron_lib import constants
@ -30,6 +31,8 @@ from neutron import privileged
_IP_VERSION_FAMILY_MAP = {4: socket.AF_INET, 6: socket.AF_INET6} _IP_VERSION_FAMILY_MAP = {4: socket.AF_INET, 6: socket.AF_INET6}
NETNS_RUN_DIR = '/var/run/netns'
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
@ -189,8 +192,29 @@ def open_namespace(namespace):
@privileged.default.entrypoint @privileged.default.entrypoint
def list_ns_pids(namespace): def list_ns_pids(namespace):
"""List namespace process PIDs""" """List namespace process PIDs
return netns.ns_pids().get(namespace, [])
Based on Pyroute2.netns.ns_pids(). Remove when
https://github.com/svinota/pyroute2/issues/633 is fixed.
"""
ns_pids = []
try:
ns_path = os.path.join(NETNS_RUN_DIR, namespace)
ns_inode = os.stat(ns_path).st_ino
except OSError:
return ns_pids
for pid in os.listdir('/proc'):
if not pid.isdigit():
continue
try:
pid_path = os.path.join('/proc', pid, 'ns', 'net')
if os.stat(pid_path).st_ino == ns_inode:
ns_pids.append(int(pid))
except OSError:
continue
return ns_pids
def _translate_ip_device_exception(e, device=None, namespace=None): def _translate_ip_device_exception(e, device=None, namespace=None):