2014-11-04 10:18:55 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package namespaces
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"syscall"
|
2014-11-25 06:39:32 +08:00
|
|
|
|
|
|
|
"github.com/docker/libcontainer"
|
2014-11-04 10:18:55 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type initError struct {
|
|
|
|
Message string `json:"message,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i initError) Error() string {
|
|
|
|
return i.Message
|
|
|
|
}
|
|
|
|
|
2014-12-17 08:34:46 +08:00
|
|
|
var namespaceInfo = map[libcontainer.NamespaceType]int{
|
|
|
|
libcontainer.NEWNET: syscall.CLONE_NEWNET,
|
|
|
|
libcontainer.NEWNS: syscall.CLONE_NEWNS,
|
|
|
|
libcontainer.NEWUSER: syscall.CLONE_NEWUSER,
|
|
|
|
libcontainer.NEWIPC: syscall.CLONE_NEWIPC,
|
|
|
|
libcontainer.NEWUTS: syscall.CLONE_NEWUTS,
|
|
|
|
libcontainer.NEWPID: syscall.CLONE_NEWPID,
|
2014-12-04 07:47:26 +08:00
|
|
|
}
|
|
|
|
|
2014-11-04 10:18:55 +08:00
|
|
|
// New returns a newly initialized Pipe for communication between processes
|
|
|
|
func newInitPipe() (parent *os.File, child *os.File, err error) {
|
|
|
|
fds, err := syscall.Socketpair(syscall.AF_LOCAL, syscall.SOCK_STREAM|syscall.SOCK_CLOEXEC, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return os.NewFile(uintptr(fds[1]), "parent"), os.NewFile(uintptr(fds[0]), "child"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNamespaceFlags parses the container's Namespaces options to set the correct
|
2014-12-26 19:14:59 +08:00
|
|
|
// flags on clone, unshare. This functions returns flags only for new namespaces.
|
2014-12-17 08:34:46 +08:00
|
|
|
func GetNamespaceFlags(namespaces libcontainer.Namespaces) (flag int) {
|
2014-11-25 06:39:32 +08:00
|
|
|
for _, v := range namespaces {
|
2014-12-26 19:14:59 +08:00
|
|
|
if v.Path != "" {
|
|
|
|
continue
|
|
|
|
}
|
2014-12-17 08:34:46 +08:00
|
|
|
flag |= namespaceInfo[v.Type]
|
2014-11-04 10:18:55 +08:00
|
|
|
}
|
|
|
|
return flag
|
|
|
|
}
|