forked from moby/libnetwork
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_linux.go
43 lines (35 loc) · 900 Bytes
/
init_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package ns
import (
"fmt"
"os"
"syscall"
log "github.com/Sirupsen/logrus"
"github.com/vishvananda/netns"
)
var initNs netns.NsHandle
// Init initializes a new network namespace
func Init() {
var err error
initNs, err = netns.Get()
if err != nil {
log.Errorf("could not get initial namespace: %v", err)
}
}
// SetNamespace sets the initial namespace handler
func SetNamespace() error {
if err := netns.Set(initNs); err != nil {
linkInfo, linkErr := getLink()
if linkErr != nil {
linkInfo = linkErr.Error()
}
return fmt.Errorf("failed to set to initial namespace, %v, initns fd %d: %v", linkInfo, initNs, err)
}
return nil
}
// ParseHandlerInt transforms the namespace handler into a integer
func ParseHandlerInt() int {
return int(initNs)
}
func getLink() (string, error) {
return os.Readlink(fmt.Sprintf("/proc/%d/task/%d/ns/net", os.Getpid(), syscall.Gettid()))
}