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 7b6b454b60
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
4 changed files with 250 additions and 59 deletions

View file

@ -5,22 +5,11 @@ package netns
import (
"fmt"
"os"
"runtime"
"golang.org/x/sys/unix"
)
func call(name string, fn func() error) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
// sauvegarde du netns courant
orig, err := os.Open("/proc/self/ns/net")
if err != nil {
return err
}
defer orig.Close()
// entrer dans le netns cible
f, err := os.Open(fmt.Sprintf("/var/run/netns/%s", name))
if err != nil {
@ -28,17 +17,7 @@ func call(name string, fn func() error) error {
}
defer f.Close()
if err := unix.Setns(int(f.Fd()), unix.CLONE_NEWNET); err != nil {
return err
}
// exécuter la fonction dans le netns
err = fn()
// toujours revenir au netns d'origine
if restoreErr := unix.Setns(int(orig.Fd()), unix.CLONE_NEWNET); restoreErr != nil {
return restoreErr
}
return err
return onThread(func() error {
return unix.Setns(int(f.Fd()), unix.CLONE_NEWNET)
}, fn)
}