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
1ea44dfaf9
commit
7c94e6f267
4 changed files with 250 additions and 59 deletions
33
internal/netns/thread_linux.go
Normal file
33
internal/netns/thread_linux.go
Normal 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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue