tools/testing/selftests/net/lib/py/netns.py
Source file repositories/reference/linux-study-clean/tools/testing/selftests/net/lib/py/netns.py
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/net/lib/py/netns.py- Extension
.py- Size
- 3507 bytes
- Lines
- 123
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: tools
- Status
- atlas-only
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
Dependency Surface
- No C-style include directives detected by the generator.
Detected Declarations
- No top-level syscall, struct, function, initcall, or export declaration detected by the generator.
Annotated Snippet
# SPDX-License-Identifier: GPL-2.0
import ctypes
import os
import random
import string
import subprocess
import time
from pathlib import Path
from .utils import ip
libc = ctypes.cdll.LoadLibrary('libc.so.6')
class NetNS:
def __init__(self, name=None):
if name:
self.name = name
else:
self.name = ''.join(random.choice(string.ascii_lowercase) for _ in range(8))
ip('netns add ' + self.name)
def __del__(self):
if self.name:
ip('netns del ' + self.name)
self.name = None
def __enter__(self):
return self
def __exit__(self, ex_type, ex_value, ex_tb):
self.__del__()
def __str__(self):
return self.name
def __repr__(self):
return f"NetNS({self.name})"
class UserNetNS:
"""Network namespace owned by a non-init user namespace."""
def __init__(self):
self.name = ''.join(
random.choice(string.ascii_lowercase) for _ in range(8))
self.user_ns_path = f"/run/userns/{self.name}"
self.net_ns_path = f"/run/netns/{self.name}"
self._user_mounted = False
self._net_mounted = False
os.makedirs("/run/userns", exist_ok=True)
os.makedirs("/run/netns", exist_ok=True)
Path(self.user_ns_path).touch()
Path(self.net_ns_path).touch()
with subprocess.Popen(
["unshare", "--user", "--net", "--map-root-user",
"sleep", "infinity"]) as proc:
try:
pid = proc.pid
init_user = os.readlink("/proc/self/ns/user")
for _ in range(200):
try:
if os.readlink(f"/proc/{pid}/ns/user") != init_user:
break
except OSError:
pass
Annotation
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: atlas-only.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.