f-8: code: implemete netns first code #8
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
c2664e94fa
commit
348eb4aee3
5 changed files with 45 additions and 2 deletions
5
internal/netns/enter.go
Normal file
5
internal/netns/enter.go
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
package netns
|
||||
|
||||
func Enter(name string) error {
|
||||
return enter(name)
|
||||
}
|
||||
26
internal/netns/enter_linux.go
Normal file
26
internal/netns/enter_linux.go
Normal 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)
|
||||
}
|
||||
8
internal/netns/enter_other.go
Normal file
8
internal/netns/enter_other.go
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
//go:build !linux
|
||||
|
||||
package netns
|
||||
|
||||
func enter(name string) error {
|
||||
// Ignoré hors Linux
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue