|
1 |
| -// Package netns allows ultra-simple network namespace handling. NsHandles |
2 |
| -// can be retrieved and set. Note that the current namespace is thread |
3 |
| -// local so actions that set and reset namespaces should use LockOSThread |
4 |
| -// to make sure the namespace doesn't change due to a goroutine switch. |
5 |
| -// It is best to close NsHandles when you are done with them. This can be |
6 |
| -// accomplished via a `defer ns.Close()` on the handle. Changing namespaces |
7 |
| -// requires elevated privileges, so in most cases this code needs to be run |
8 |
| -// as root. |
9 | 1 | package netns
|
10 | 2 |
|
11 | 3 | import (
|
@@ -39,56 +31,6 @@ func Setns(ns NsHandle, nstype int) (err error) {
|
39 | 31 | return
|
40 | 32 | }
|
41 | 33 |
|
42 |
| -// NsHandle is a handle to a network namespace. It can be cast directly |
43 |
| -// to an int and used as a file descriptor. |
44 |
| -type NsHandle int |
45 |
| - |
46 |
| -// Equal determines if two network handles refer to the same network |
47 |
| -// namespace. This is done by comparing the device and inode that the |
48 |
| -// file descripors point to. |
49 |
| -func (ns NsHandle) Equal(other NsHandle) bool { |
50 |
| - var s1, s2 syscall.Stat_t |
51 |
| - if err := syscall.Fstat(int(ns), &s1); err != nil { |
52 |
| - return false |
53 |
| - } |
54 |
| - if err := syscall.Fstat(int(other), &s2); err != nil { |
55 |
| - return false |
56 |
| - } |
57 |
| - return (s1.Dev == s2.Dev) && (s1.Ino == s2.Ino) |
58 |
| -} |
59 |
| - |
60 |
| -// String shows the file descriptor number and its dev and inode. |
61 |
| -func (ns NsHandle) String() string { |
62 |
| - var s syscall.Stat_t |
63 |
| - if ns == -1 { |
64 |
| - return "NS(None)" |
65 |
| - } |
66 |
| - if err := syscall.Fstat(int(ns), &s); err != nil { |
67 |
| - return fmt.Sprintf("NS(%d: unknown)", ns) |
68 |
| - } |
69 |
| - return fmt.Sprintf("NS(%d: %d, %d)", ns, s.Dev, s.Ino) |
70 |
| -} |
71 |
| - |
72 |
| -// IsOpen returns true if Close() has not been called. |
73 |
| -func (ns NsHandle) IsOpen() bool { |
74 |
| - return ns != -1 |
75 |
| -} |
76 |
| - |
77 |
| -// Close closes the NsHandle and resets its file descriptor to -1. |
78 |
| -// It is not safe to use an NsHandle after Close() is called. |
79 |
| -func (ns *NsHandle) Close() error { |
80 |
| - if err := syscall.Close(int(*ns)); err != nil { |
81 |
| - return err |
82 |
| - } |
83 |
| - (*ns) = -1 |
84 |
| - return nil |
85 |
| -} |
86 |
| - |
87 |
| -// Get an empty (closed) NsHandle |
88 |
| -func None() NsHandle { |
89 |
| - return NsHandle(-1) |
90 |
| -} |
91 |
| - |
92 | 34 | // Set sets the current network namespace to the namespace represented
|
93 | 35 | // by NsHandle.
|
94 | 36 | func Set(ns NsHandle) (err error) {
|
|
0 commit comments