fix: persist netns before restoring origin network namespace

This commit is contained in:
RouxAntoine 2024-01-03 20:33:29 +01:00
parent 500d6ad57c
commit 3422b93159
Signed by: antoine
GPG Key ID: 098FB66FC0475E70
2 changed files with 8 additions and 9 deletions

View File

@ -27,17 +27,17 @@ func New() (handle NsHandle, error error) {
return None(), err return None(), err
} }
err = Set(origins)
if err != nil {
return NsHandle{Fd: 0}, err
}
err = newNs.persist() err = newNs.persist()
if err != nil { if err != nil {
defer deferClose(&newNs, &error) defer deferClose(&newNs, &error)
return newNs, err return newNs, err
} }
err = Set(origins)
if err != nil {
return NsHandle{Fd: 0}, err
}
return newNs, nil return newNs, nil
} }

View File

@ -38,8 +38,7 @@ func (ns *NsHandle) persist() error {
return err return err
} }
nsPath := GetPath() err = unix.Mount(GetPath(), namedPath, "bind", unix.MS_BIND, "")
err = unix.Mount(nsPath, namedPath, "bind", unix.MS_BIND, "")
if err != nil { if err != nil {
return err return err
} }
@ -71,9 +70,9 @@ func (ns *NsHandle) String() string {
} }
var s unix.Stat_t var s unix.Stat_t
if err := unix.Fstat(ns.Fd, &s); err != nil { if err := unix.Fstat(ns.Fd, &s); err != nil {
return fmt.Sprintf("NS(%d: unknown)", ns) return fmt.Sprintf("NS(%d: unknown)", ns.Fd)
} }
return fmt.Sprintf("NS(%d: %d, %d)", ns, s.Dev, s.Ino) return fmt.Sprintf("NS(%d: %d, %d)", ns.Fd, s.Dev, s.Ino)
} }
// IsOpen returns true if Close() has not been called. // IsOpen returns true if Close() has not been called.