4

/proc/[pid]/ns/net contains a link to the inode representing the network namespace of the process with PID [pid]. Is there something similar for threads?

My use case is a multi-threaded application, where there's one main thread and a group of worker threads. The generic worker W creates a new network namespace N with a call to unshare() (which makes W enter N), pushes one end of a veth pair in N and leaves it (it uses an fd pointing to the root namespace to go back to such namespace). Since no processes are in N after W goes back to the root namespace, N is destroyed when that happens, and I do not want that.

The solution I thought about is to mount a link to N somewhere in the filesystem. This is what iproute2 netns does: mounting a link to /proc/[pid]/ns/net. The problem, in my case, is that /proc/[pid]/ns/net keeps referencing the root namespace, only W changes namespace, hence I cannot use it and I need a file/something else which points to the namespace of a thread. Is there such a thing in Linux?

2

There is a file that associates a thread to its network namespace:

/proc/[PID]/task/[TID]/ns/net

where TID is the thread ID. This solved my issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.