diff --git a/netns_linux.go b/netns_linux.go index dbd0dd0..2d845a2 100644 --- a/netns_linux.go +++ b/netns_linux.go @@ -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) { diff --git a/netns_test.go b/netns_test.go index 98ebfc6..b685b9d 100644 --- a/netns_test.go +++ b/netns_test.go @@ -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) + } +}