mirror of
https://github.com/vishvananda/netns
synced 2025-02-19 21:16:50 +00:00
Add method for empty ns handle
This commit is contained in:
parent
b801b0b909
commit
1e4e8a6a14
@ -60,6 +60,9 @@ func (ns NsHandle) Equal(other NsHandle) bool {
|
||||
// String shows the file descriptor number and its dev and inode.
|
||||
func (ns NsHandle) String() string {
|
||||
var s syscall.Stat_t
|
||||
if ns == -1 {
|
||||
return "NS(None)"
|
||||
}
|
||||
if err := syscall.Fstat(int(ns), &s); err != nil {
|
||||
return fmt.Sprintf("NS(%d: unknown)", ns)
|
||||
}
|
||||
@ -81,6 +84,11 @@ func (ns *NsHandle) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get an empty (closed) NsHandle
|
||||
func None() NsHandle {
|
||||
return NsHandle(-1)
|
||||
}
|
||||
|
||||
// Set sets the current network namespace to the namespace represented
|
||||
// by NsHandle.
|
||||
func Set(ns NsHandle) (err error) {
|
||||
|
@ -35,3 +35,10 @@ func TestGetNewSetDelete(t *testing.T) {
|
||||
t.Fatal("Reset ns failed", origns, newns, ns)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNone(t *testing.T) {
|
||||
ns := None()
|
||||
if ns.IsOpen() {
|
||||
t.Fatal("None ns is open", ns)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user