-
Notifications
You must be signed in to change notification settings - Fork 0
/
netns.go
43 lines (35 loc) · 975 Bytes
/
netns.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 neslink
import (
"os"
)
// Namespace is a path to a file associated with a network namespace.
type Namespace string
// NsFd is a file descriptor for an open Namespace file.
type NsFd int
const (
NsFdNone NsFd = NsFd(-1)
DefaultMountPath string = "/run/netns"
)
// String returns the Namespace file path as a string.
func (n Namespace) String() string {
return string(n)
}
// Int returns the Namespace file descriptor as an int.
func (n NsFd) Int() int {
return int(n)
}
// Valid determines if the file descriptor is valid. This can be used to
// determine if a returned NsFd is ok regardless of the error.
func (n NsFd) Valid() bool {
return n.Int() > 0
}
// Exists determines if the path used for the namespace exists. Whilst not an
// exhaustive check, this can help debug namespace providers.
func (ns Namespace) Exists() bool {
if info, err := os.Stat(ns.String()); err != nil {
if !info.IsDir() {
return true
}
}
return false
}