f-91: netns: retour au netns du thread courant, thread détruit sinon #91

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-10-26 14:08:07 +01:00
commit 7c94e6f267
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
4 changed files with 250 additions and 59 deletions

View file

@ -0,0 +1,33 @@
//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
}