f-8: code: implemete netns first code #8

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2025-12-28 14:57:36 +01:00
commit 348eb4aee3
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
5 changed files with 45 additions and 2 deletions

5
internal/netns/enter.go Normal file
View file

@ -0,0 +1,5 @@
package netns
func Enter(name string) error {
return enter(name)
}

View file

@ -0,0 +1,26 @@
//go:build linux
package netns
import (
"fmt"
"os"
"runtime"
"golang.org/x/sys/unix"
)
func enter(name string) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
path := fmt.Sprintf("/var/run/netns/%s", name)
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
return unix.Setns(int(f.Fd()), unix.CLONE_NEWNET)
}

View file

@ -0,0 +1,8 @@
//go:build !linux
package netns
func enter(name string) error {
// Ignoré hors Linux
return nil
}