2014-02-19 00:56:11 +00:00
|
|
|
/*
|
|
|
|
Higher level convience functions for setting up a container
|
|
|
|
*/
|
|
|
|
|
|
|
|
package namespaces
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
2014-02-19 01:52:06 +00:00
|
|
|
"os/exec"
|
2014-02-19 02:15:41 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2014-02-19 00:56:11 +00:00
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrExistingNetworkNamespace = errors.New("specified both CLONE_NEWNET and an existing network namespace")
|
|
|
|
)
|
|
|
|
|
|
|
|
// Exec will spawn new namespaces with the specified Container configuration
|
|
|
|
// in the RootFs path and return the pid of the new containerized process.
|
|
|
|
//
|
|
|
|
// If an existing network namespace is specified the container
|
|
|
|
// will join that namespace. If an existing network namespace is not specified but CLONE_NEWNET is,
|
|
|
|
// the container will be spawned with a new network namespace with no configuration. Omiting an
|
|
|
|
// existing network namespace and the CLONE_NEWNET option in the container configuration will allow
|
|
|
|
// the container to the the host's networking options and configuration.
|
2014-02-19 01:52:06 +00:00
|
|
|
func ExecContainer(container *libcontainer.Container) (pid int, err error) {
|
2014-02-19 00:56:11 +00:00
|
|
|
// a user cannot pass CLONE_NEWNET and an existing net namespace fd to join
|
|
|
|
if container.NetNsFd > 0 && container.Namespaces.Contains(libcontainer.CLONE_NEWNET) {
|
|
|
|
return -1, ErrExistingNetworkNamespace
|
|
|
|
}
|
|
|
|
|
|
|
|
master, console, err := createMasterAndConsole()
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2014-02-19 02:15:41 +00:00
|
|
|
nsinit := filepath.Join(container.RootFs, ".nsinit")
|
2014-02-19 00:56:11 +00:00
|
|
|
|
|
|
|
// we need CLONE_VFORK so we can wait on the child
|
2014-02-19 01:52:06 +00:00
|
|
|
flag := uintptr(getNamespaceFlags(container.Namespaces) | CLONE_VFORK)
|
2014-02-19 00:56:11 +00:00
|
|
|
|
2014-02-19 02:15:41 +00:00
|
|
|
command := exec.Command(nsinit, "init", "-master", strconv.Itoa(int(master.Fd())), "-console", console)
|
2014-02-19 01:52:06 +00:00
|
|
|
command.SysProcAttr = &syscall.SysProcAttr{}
|
|
|
|
command.SysProcAttr.Cloneflags = flag
|
2014-02-19 02:15:41 +00:00
|
|
|
// command.SysProcAttr.Setctty = true
|
2014-02-19 00:56:11 +00:00
|
|
|
|
2014-02-19 01:52:06 +00:00
|
|
|
if err := command.Start(); err != nil {
|
|
|
|
return -1, err
|
2014-02-19 00:56:11 +00:00
|
|
|
}
|
2014-02-19 01:52:06 +00:00
|
|
|
pid = command.Process.Pid
|
2014-02-19 00:56:11 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
if _, err := io.Copy(os.Stdout, master); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
if _, err := io.Copy(master, os.Stdin); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}()
|
2014-02-19 02:15:41 +00:00
|
|
|
command.Wait()
|
2014-02-19 00:56:11 +00:00
|
|
|
return pid, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExecIn will spawn a new command inside an existing container's namespaces. The existing container's
|
|
|
|
// pid and namespace configuration is needed along with the specific capabilities that should
|
|
|
|
// be dropped once inside the namespace.
|
|
|
|
func ExecIn(container *libcontainer.Container, cmd *libcontainer.Command) (int, error) {
|
2014-02-19 01:52:06 +00:00
|
|
|
return -1, fmt.Errorf("not implemented")
|
|
|
|
/*
|
|
|
|
if container.NsPid <= 0 {
|
|
|
|
return -1, libcontainer.ErrInvalidPid
|
|
|
|
}
|
2014-02-19 00:56:11 +00:00
|
|
|
|
2014-02-19 01:52:06 +00:00
|
|
|
fds, err := getNsFds(container)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2014-02-19 00:56:11 +00:00
|
|
|
|
2014-02-19 01:52:06 +00:00
|
|
|
if container.NetNsFd > 0 {
|
|
|
|
fds = append(fds, container.NetNsFd)
|
2014-02-19 00:56:11 +00:00
|
|
|
}
|
|
|
|
|
2014-02-19 01:52:06 +00:00
|
|
|
pid, err := fork()
|
|
|
|
if err != nil {
|
|
|
|
for _, fd := range fds {
|
|
|
|
syscall.Close(int(fd))
|
2014-02-19 00:56:11 +00:00
|
|
|
}
|
2014-02-19 01:52:06 +00:00
|
|
|
return -1, err
|
2014-02-19 00:56:11 +00:00
|
|
|
}
|
|
|
|
|
2014-02-19 01:52:06 +00:00
|
|
|
if pid == 0 {
|
|
|
|
for _, fd := range fds {
|
|
|
|
if fd > 0 {
|
|
|
|
if err := JoinExistingNamespace(fd, ""); err != nil {
|
|
|
|
for _, fd := range fds {
|
|
|
|
syscall.Close(int(fd))
|
|
|
|
}
|
|
|
|
writeError("join existing namespace for %d %s", fd, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
syscall.Close(int(fd))
|
2014-02-19 00:56:11 +00:00
|
|
|
}
|
|
|
|
|
2014-02-19 01:52:06 +00:00
|
|
|
if container.Namespaces.Contains(libcontainer.CLONE_NEWNS) &&
|
|
|
|
container.Namespaces.Contains(libcontainer.CLONE_NEWPID) {
|
|
|
|
// important:
|
|
|
|
//
|
|
|
|
// we need to fork and unshare so that re can remount proc and sys within
|
|
|
|
// the namespace so the CLONE_NEWPID namespace will take effect
|
|
|
|
// if we don't fork we would end up unmounting proc and sys for the entire
|
|
|
|
// namespace
|
|
|
|
child, err := fork()
|
|
|
|
if err != nil {
|
|
|
|
writeError("fork child %s", err)
|
2014-02-19 00:56:11 +00:00
|
|
|
}
|
2014-02-19 01:52:06 +00:00
|
|
|
|
|
|
|
if child == 0 {
|
|
|
|
if err := unshare(CLONE_NEWNS); err != nil {
|
|
|
|
writeError("unshare newns %s", err)
|
|
|
|
}
|
|
|
|
if err := remountProc(); err != nil {
|
|
|
|
writeError("remount proc %s", err)
|
|
|
|
}
|
|
|
|
if err := remountSys(); err != nil {
|
|
|
|
writeError("remount sys %s", err)
|
|
|
|
}
|
|
|
|
if err := capabilities.DropCapabilities(container); err != nil {
|
|
|
|
writeError("drop caps %s", err)
|
|
|
|
}
|
|
|
|
if err := exec(cmd.Args[0], cmd.Args[0:], cmd.Env); err != nil {
|
|
|
|
writeError("exec %s", err)
|
|
|
|
}
|
|
|
|
panic("unreachable")
|
2014-02-19 00:56:11 +00:00
|
|
|
}
|
2014-02-19 01:52:06 +00:00
|
|
|
exit, err := utils.WaitOnPid(child)
|
|
|
|
if err != nil {
|
|
|
|
writeError("wait on child %s", err)
|
2014-02-19 00:56:11 +00:00
|
|
|
}
|
2014-02-19 01:52:06 +00:00
|
|
|
os.Exit(exit)
|
2014-02-19 00:56:11 +00:00
|
|
|
}
|
2014-02-19 01:52:06 +00:00
|
|
|
if err := exec(cmd.Args[0], cmd.Args[0:], cmd.Env); err != nil {
|
|
|
|
writeError("exec %s", err)
|
2014-02-19 00:56:11 +00:00
|
|
|
}
|
2014-02-19 01:52:06 +00:00
|
|
|
panic("unreachable")
|
2014-02-19 00:56:11 +00:00
|
|
|
}
|
2014-02-19 01:52:06 +00:00
|
|
|
return pid, err
|
|
|
|
*/
|
2014-02-19 00:56:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func createMasterAndConsole() (*os.File, string, error) {
|
|
|
|
master, err := os.OpenFile("/dev/ptmx", syscall.O_RDWR|syscall.O_NOCTTY|syscall.O_CLOEXEC, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
2014-02-19 01:52:06 +00:00
|
|
|
console, err := Ptsname(master)
|
2014-02-19 00:56:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
2014-02-19 01:52:06 +00:00
|
|
|
if err := Unlockpt(master); err != nil {
|
2014-02-19 00:56:11 +00:00
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
return master, console, nil
|
|
|
|
}
|