two/internal/netns/thread_linux.go
2026-10-26 14:08:07 +01:00

33 lines
501 B
Go

//go:build linux
package netns
import (
"os"
"runtime"
"golang.org/x/sys/unix"
)
func onThread(enter func() error, fn func() error) error {
runtime.LockOSThread()
orig, err := os.Open("/proc/thread-self/ns/net")
if err != nil {
runtime.UnlockOSThread()
return err
}
defer orig.Close()
err = enter()
if err == nil {
err = fn()
}
if restoreErr := unix.Setns(int(orig.Fd()), unix.CLONE_NEWNET); restoreErr != nil {
return restoreErr
}
runtime.UnlockOSThread()
return err
}