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:
parent
da6295baf1
commit
7b6b454b60
4 changed files with 250 additions and 59 deletions
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue