On Thu, 7 Nov 2024 21:30:03 +0800 Xiao Liang wrote:
quoted hunk ↗ jump to hunk
+class NSEnter:
+ def __init__(self, ns_name):
+ self.ns_path = f"/run/netns/{ns_name}"
+
+ def __enter__(self):
+ self.saved = open("/proc/thread-self/ns/net")
+ with open(self.ns_path) as ns_file:
+ libc.setns(ns_file.fileno(), 0)
+
+ def __exit__(self, exc_type, exc_value, traceback):
+ libc.setns(self.saved.fileno(), 0)
+ self.saved.close()
This is quite nice, why not move it to
tools/testing/selftests/net/lib/py/netns.py
so others can use it too