2017-02-01 00:45:59 +00:00
|
|
|
// +build linux freebsd
|
|
|
|
|
|
|
|
package configs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
NEWNET NamespaceType = "NEWNET"
|
|
|
|
NEWPID NamespaceType = "NEWPID"
|
|
|
|
NEWNS NamespaceType = "NEWNS"
|
|
|
|
NEWUTS NamespaceType = "NEWUTS"
|
|
|
|
NEWIPC NamespaceType = "NEWIPC"
|
|
|
|
NEWUSER NamespaceType = "NEWUSER"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
nsLock sync.Mutex
|
|
|
|
supportedNamespaces = make(map[NamespaceType]bool)
|
|
|
|
)
|
|
|
|
|
2017-02-06 20:16:36 +00:00
|
|
|
// NsName converts the namespace type to its filename
|
|
|
|
func NsName(ns NamespaceType) string {
|
2017-02-01 00:45:59 +00:00
|
|
|
switch ns {
|
|
|
|
case NEWNET:
|
|
|
|
return "net"
|
|
|
|
case NEWNS:
|
|
|
|
return "mnt"
|
|
|
|
case NEWPID:
|
|
|
|
return "pid"
|
|
|
|
case NEWIPC:
|
|
|
|
return "ipc"
|
|
|
|
case NEWUSER:
|
|
|
|
return "user"
|
|
|
|
case NEWUTS:
|
|
|
|
return "uts"
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsNamespaceSupported returns whether a namespace is available or
|
|
|
|
// not
|
|
|
|
func IsNamespaceSupported(ns NamespaceType) bool {
|
|
|
|
nsLock.Lock()
|
|
|
|
defer nsLock.Unlock()
|
|
|
|
supported, ok := supportedNamespaces[ns]
|
|
|
|
if ok {
|
|
|
|
return supported
|
|
|
|
}
|
2017-02-06 20:16:36 +00:00
|
|
|
nsFile := NsName(ns)
|
2017-02-01 00:45:59 +00:00
|
|
|
// if the namespace type is unknown, just return false
|
|
|
|
if nsFile == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
_, err := os.Stat(fmt.Sprintf("/proc/self/ns/%s", nsFile))
|
|
|
|
// a namespace is supported if it exists and we have permissions to read it
|
|
|
|
supported = err == nil
|
|
|
|
supportedNamespaces[ns] = supported
|
|
|
|
return supported
|
|
|
|
}
|
|
|
|
|
|
|
|
func NamespaceTypes() []NamespaceType {
|
|
|
|
return []NamespaceType{
|
|
|
|
NEWNET,
|
|
|
|
NEWPID,
|
|
|
|
NEWNS,
|
|
|
|
NEWUTS,
|
|
|
|
NEWIPC,
|
|
|
|
NEWUSER,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Namespace defines configuration for each namespace. It specifies an
|
|
|
|
// alternate path that is able to be joined via setns.
|
|
|
|
type Namespace struct {
|
|
|
|
Type NamespaceType `json:"type"`
|
|
|
|
Path string `json:"path"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Namespace) GetPath(pid int) string {
|
|
|
|
if n.Path != "" {
|
|
|
|
return n.Path
|
|
|
|
}
|
2017-02-06 20:16:36 +00:00
|
|
|
return fmt.Sprintf("/proc/%d/ns/%s", pid, NsName(n.Type))
|
2017-02-01 00:45:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Namespaces) Remove(t NamespaceType) bool {
|
|
|
|
i := n.index(t)
|
|
|
|
if i == -1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
*n = append((*n)[:i], (*n)[i+1:]...)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Namespaces) Add(t NamespaceType, path string) {
|
|
|
|
i := n.index(t)
|
|
|
|
if i == -1 {
|
|
|
|
*n = append(*n, Namespace{Type: t, Path: path})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
(*n)[i].Path = path
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Namespaces) index(t NamespaceType) int {
|
|
|
|
for i, ns := range *n {
|
|
|
|
if ns.Type == t {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Namespaces) Contains(t NamespaceType) bool {
|
|
|
|
return n.index(t) != -1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Namespaces) PathOf(t NamespaceType) string {
|
|
|
|
i := n.index(t)
|
|
|
|
if i == -1 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return (*n)[i].Path
|
|
|
|
}
|