@@ -52,33 +52,30 @@ func Get() (NsHandle, error) {
52
52
return GetFromThread (os .Getpid (), syscall .Gettid ())
53
53
}
54
54
55
- // GetFromName gets a handle to a named network namespace such as one
56
- // created by `ip netns add`.
57
- func GetFromName ( name string ) (NsHandle , error ) {
58
- fd , err := syscall .Open (fmt . Sprintf ( "/var/run/netns/%s" , name ) , syscall .O_RDONLY , 0 )
55
+ // GetFromPath gets a handle to a network namespace
56
+ // identified by the path
57
+ func GetFromPath ( path string ) (NsHandle , error ) {
58
+ fd , err := syscall .Open (path , syscall .O_RDONLY , 0 )
59
59
if err != nil {
60
60
return - 1 , err
61
61
}
62
62
return NsHandle (fd ), nil
63
63
}
64
64
65
+ // GetFromName gets a handle to a named network namespace such as one
66
+ // created by `ip netns add`.
67
+ func GetFromName (name string ) (NsHandle , error ) {
68
+ return GetFromPath (fmt .Sprintf ("/var/run/netns/%s" , name ))
69
+ }
70
+
65
71
// GetFromPid gets a handle to the network namespace of a given pid.
66
72
func GetFromPid (pid int ) (NsHandle , error ) {
67
- fd , err := syscall .Open (fmt .Sprintf ("/proc/%d/ns/net" , pid ), syscall .O_RDONLY , 0 )
68
- if err != nil {
69
- return - 1 , err
70
- }
71
- return NsHandle (fd ), nil
73
+ return GetFromPath (fmt .Sprintf ("/proc/%d/ns/net" , pid ))
72
74
}
73
75
74
76
// GetFromThread gets a handle to the network namespace of a given pid and tid.
75
77
func GetFromThread (pid , tid int ) (NsHandle , error ) {
76
- name := fmt .Sprintf ("/proc/%d/task/%d/ns/net" , pid , tid )
77
- fd , err := syscall .Open (name , syscall .O_RDONLY , 0 )
78
- if err != nil {
79
- return - 1 , err
80
- }
81
- return NsHandle (fd ), nil
78
+ return GetFromPath (fmt .Sprintf ("/proc/%d/task/%d/ns/net" , pid , tid ))
82
79
}
83
80
84
81
// GetFromDocker gets a handle to the network namespace of a docker container.
0 commit comments