fix: close net namespace fd (#418)

Signed-off-by: Jim Ma <majinjing3@gmail.com>
This commit is contained in:
Jim Ma 2021-07-05 09:25:36 +08:00 committed by Gaius
parent 8408ab0e63
commit a8f2b818d1
No known key found for this signature in database
GPG Key ID: 8B4E5D1290FA2FFB
1 changed files with 11 additions and 1 deletions

View File

@ -31,6 +31,7 @@ func switchNetNamespace(target string) (func() error, error) {
if err != nil {
return nil, err
}
defer unix.Close(fd)
orgNS := fmt.Sprintf("/proc/%d/ns/net", os.Getpid())
// hold the original net namespace
@ -46,7 +47,16 @@ func switchNetNamespace(target string) (func() error, error) {
}
return func() error {
if err := unix.Setns(orgFD, unix.CLONE_NEWNET); err != nil {
logger.Errorf("recover net namespace, from %s to %s, error: %s", target, orgNS, err)
return err
}
logger.Infof("recover net namespace, from %s to %s", target, orgNS)
return unix.Setns(orgFD, unix.CLONE_NEWNET)
if err := unix.Close(orgFD); err != nil {
logger.Errorf("recover net namespace, close fd error: %s", err)
return err
}
logger.Infof("recover net namespace, close original fd")
return nil
}, nil
}