2014-02-20 00:50:10 +00:00
|
|
|
// +build linux
|
|
|
|
|
2014-02-19 22:55:34 +00:00
|
|
|
package main
|
2014-02-19 00:56:11 +00:00
|
|
|
|
|
|
|
import (
|
2014-02-19 23:33:44 +00:00
|
|
|
"fmt"
|
2014-02-19 00:56:11 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer"
|
2014-02-19 23:33:44 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer/network"
|
2014-02-19 23:54:53 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer/utils"
|
2014-02-19 07:13:36 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/system"
|
2014-02-19 22:33:25 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/term"
|
2014-02-19 00:56:11 +00:00
|
|
|
"io"
|
2014-02-20 00:40:36 +00:00
|
|
|
"io/ioutil"
|
2014-02-19 00:56:11 +00:00
|
|
|
"os"
|
2014-02-19 01:52:06 +00:00
|
|
|
"os/exec"
|
2014-02-19 00:56:11 +00:00
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2014-02-20 00:40:36 +00:00
|
|
|
func execCommand(container *libcontainer.Container) (int, error) {
|
2014-02-19 00:56:11 +00:00
|
|
|
master, console, err := createMasterAndConsole()
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
2014-02-20 00:40:36 +00:00
|
|
|
command := exec.Command("nsinit", "init", console)
|
2014-02-19 22:33:25 +00:00
|
|
|
command.SysProcAttr = &syscall.SysProcAttr{
|
2014-02-20 00:40:36 +00:00
|
|
|
Cloneflags: uintptr(getNamespaceFlags(container.Namespaces) | syscall.CLONE_VFORK), // we need CLONE_VFORK so we can wait on the child
|
2014-02-19 22:33:25 +00:00
|
|
|
}
|
2014-02-19 00:56:11 +00:00
|
|
|
|
2014-02-19 23:33:44 +00:00
|
|
|
inPipe, err := command.StdinPipe()
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
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-20 00:40:36 +00:00
|
|
|
if err := writePidFile(command); err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2014-02-19 00:56:11 +00:00
|
|
|
|
2014-02-19 23:33:44 +00:00
|
|
|
if container.Network != nil {
|
|
|
|
name1, name2, err := createVethPair()
|
|
|
|
if err != nil {
|
2014-02-20 00:40:36 +00:00
|
|
|
return -1, err
|
2014-02-19 23:33:44 +00:00
|
|
|
}
|
|
|
|
if err := network.SetInterfaceMaster(name1, container.Network.Bridge); err != nil {
|
2014-02-20 00:40:36 +00:00
|
|
|
return -1, err
|
2014-02-19 23:33:44 +00:00
|
|
|
}
|
|
|
|
if err := network.InterfaceUp(name1); err != nil {
|
2014-02-20 00:40:36 +00:00
|
|
|
return -1, err
|
2014-02-19 23:33:44 +00:00
|
|
|
}
|
2014-02-20 00:40:36 +00:00
|
|
|
if err := network.SetInterfaceInNamespacePid(name2, command.Process.Pid); err != nil {
|
|
|
|
return -1, err
|
2014-02-19 23:33:44 +00:00
|
|
|
}
|
|
|
|
fmt.Fprint(inPipe, name2)
|
|
|
|
inPipe.Close()
|
|
|
|
}
|
|
|
|
|
2014-02-20 00:40:36 +00:00
|
|
|
go io.Copy(os.Stdout, master)
|
|
|
|
go io.Copy(master, os.Stdin)
|
2014-02-19 22:33:25 +00:00
|
|
|
|
2014-02-19 22:55:34 +00:00
|
|
|
ws, err := term.GetWinsize(os.Stdin.Fd())
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
if err := term.SetWinsize(master.Fd(), ws); err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
state, err := term.SetRawTerminal(os.Stdin.Fd())
|
|
|
|
if err != nil {
|
|
|
|
command.Process.Kill()
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
defer term.RestoreTerminal(os.Stdin.Fd(), state)
|
2014-02-19 22:33:25 +00:00
|
|
|
|
|
|
|
if err := command.Wait(); err != nil {
|
2014-02-20 00:40:36 +00:00
|
|
|
if _, ok := err.(*exec.ExitError); !ok {
|
|
|
|
return -1, err
|
|
|
|
}
|
2014-02-19 22:33:25 +00:00
|
|
|
}
|
2014-02-20 00:40:36 +00:00
|
|
|
return command.ProcessState.Sys().(syscall.WaitStatus).ExitStatus(), nil
|
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 07:13:36 +00:00
|
|
|
console, err := system.Ptsname(master)
|
2014-02-19 00:56:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
2014-02-19 07:13:36 +00:00
|
|
|
if err := system.Unlockpt(master); err != nil {
|
2014-02-19 00:56:11 +00:00
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
return master, console, nil
|
|
|
|
}
|
2014-02-19 23:33:44 +00:00
|
|
|
|
|
|
|
func createVethPair() (name1 string, name2 string, err error) {
|
2014-02-19 23:54:53 +00:00
|
|
|
name1, err = utils.GenerateRandomName("dock", 4)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
name2, err = utils.GenerateRandomName("dock", 4)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-02-19 23:33:44 +00:00
|
|
|
if err = network.CreateVethPair(name1, name2); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2014-02-20 00:40:36 +00:00
|
|
|
|
|
|
|
func writePidFile(command *exec.Cmd) error {
|
|
|
|
return ioutil.WriteFile(".nspid", []byte(fmt.Sprint(command.Process.Pid)), 0655)
|
|
|
|
}
|