Why Does Calling Setns from Go Return EINVAL for the Mnt Namespace?
Calling Setns from Go Returns EINVAL for Mnt Namespace
Background
The goal is to enter the mnt namespace of a container using either C or Go code. However, the Go code consistently returns EINVAL from the setns call when attempting to enter the mnt namespace.
Working C Code
The following C code successfully enters all specified namespaces:
<code class="c">#include <sched.h> main() { // ... for (i=0; i<5; i++) { setns(fd, 0); // Join the provided namespace } }</code>
Failing Go Code
On the other hand, the equivalent Go code returns an EINVAL error for the mnt namespace:
<code class="go">import ( "syscall" ) func main() { // ... err := syscall.RawSyscall(308, fd, 0, 0) // Calling setns if err != 0 { fmt.Println("setns on", namespaces[i], "namespace failed") } }</code>
Answer
The issue lies in the multi-threaded nature of Go. When Go calls setns from a multi-threaded context, it fails for the mnt namespace because it requires a single-threaded caller. To resolve this, the setns call must be made before the Go runtime creates any additional threads.
Solution: Single-Threaded Constructor Trick
One way to achieve this is to use the CGO constructor trick, where a C function is executed before Go starts up:
<code class="c">__attribute__((constructor)) void enter_namespace(void) { setns(...); }</code>
Adding this constructor to the Go code, along with hardcoding the PID, allows successful entry into the mnt namespace:
<code class="go">/* __attribute__((constructor)) void enter_namespace(void) { ... } */ import "C"</code>
However, this approach requires hardcoding the PID, which is not ideal.
Alternative: Linux Kernel 4.16 and Above
In Linux kernel 4.16 and above, a new mode has been introduced for setns that allows it to be safely called from multi-threaded contexts. By using a modified version of the setns call that takes an additional argument (CLONE_IOCLONE_COMMON), it is possible to enter the mnt namespace from Go, even if it is multi-threaded.
The above is the detailed content of Why Does Calling Setns from Go Return EINVAL for the Mnt Namespace?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Golang is better than C in concurrency, while C is better than Golang in raw speed. 1) Golang achieves efficient concurrency through goroutine and channel, which is suitable for handling a large number of concurrent tasks. 2)C Through compiler optimization and standard library, it provides high performance close to hardware, suitable for applications that require extreme optimization.

Goimpactsdevelopmentpositivelythroughspeed,efficiency,andsimplicity.1)Speed:Gocompilesquicklyandrunsefficiently,idealforlargeprojects.2)Efficiency:Itscomprehensivestandardlibraryreducesexternaldependencies,enhancingdevelopmentefficiency.3)Simplicity:

Golang and Python each have their own advantages: Golang is suitable for high performance and concurrent programming, while Python is suitable for data science and web development. Golang is known for its concurrency model and efficient performance, while Python is known for its concise syntax and rich library ecosystem.

Golang is suitable for rapid development and concurrent scenarios, and C is suitable for scenarios where extreme performance and low-level control are required. 1) Golang improves performance through garbage collection and concurrency mechanisms, and is suitable for high-concurrency Web service development. 2) C achieves the ultimate performance through manual memory management and compiler optimization, and is suitable for embedded system development.

The performance differences between Golang and C are mainly reflected in memory management, compilation optimization and runtime efficiency. 1) Golang's garbage collection mechanism is convenient but may affect performance, 2) C's manual memory management and compiler optimization are more efficient in recursive computing.

C is more suitable for scenarios where direct control of hardware resources and high performance optimization is required, while Golang is more suitable for scenarios where rapid development and high concurrency processing are required. 1.C's advantage lies in its close to hardware characteristics and high optimization capabilities, which are suitable for high-performance needs such as game development. 2.Golang's advantage lies in its concise syntax and natural concurrency support, which is suitable for high concurrency service development.

Golang and C each have their own advantages in performance competitions: 1) Golang is suitable for high concurrency and rapid development, and 2) C provides higher performance and fine-grained control. The selection should be based on project requirements and team technology stack.
