Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

368
Vistas
How to enter network namespace by name?

My program needs to connect to two TAP interfaces that each live in their own network namespace. Because setns() works at the pthread level I plan to have a dedicated pthread for each interface. (My program itself runs in the "root" namespace.)

However, the setns() function requires me to pass a file descriptor to the network namespace I want to enter. Obtaining this descriptor requires the pid of a process that already exists in that namespace. Once I have that PID I can do a call to open() to get the file descriptor to the network namespace:

int fd = open("/proc/<pid>/ns/net");

But how do I obtain that PID?

One way would be to create a "dummy" process in the desired namespace:

ip netns exec tap101-ns sleep 100 & 
[1] 30645

And then use that PID in my program: int fd = open("/proc/30645/ns/net")

However, this way of working seems a bit silly...

Is there a cleaner way to enter a network namespace when all I have is the name of the namespace I want to enter?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I stumbled upon a solution shortly after posting my question.

The path /var/run/netns/ can also be used to obtain a file descriptor. According to the ip-netns documentation:

The file descriptor resulting from opening /var/run/netns/NAME refers to the specified network namespace. Holding that file descriptor open keeps the network namespace alive. The file descriptor can be used with the setns(2) system call to change the network namespace associated with a task.

So I can use this code to switch to a namespace inside my pthread:

int namespace_fd = open("/var/run/netns/tap101-ns");
setns(namespace_fd, CLONE_NEWNET);
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda