2014-02-20 00:50:10 +00:00
|
|
|
// +build linux
|
|
|
|
|
2014-02-21 02:27:42 +00:00
|
|
|
package nsinit
|
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-21 02:27:42 +00:00
|
|
|
// Exec performes setup outside of a namespace so that a container can be
|
|
|
|
// executed. Exec is a high level function for working with container namespaces.
|
|
|
|
func Exec(container *libcontainer.Container, tty bool, args []string) (int, error) {
|
2014-02-21 02:05:40 +00:00
|
|
|
var (
|
|
|
|
master *os.File
|
|
|
|
console string
|
|
|
|
err error
|
|
|
|
|
|
|
|
inPipe io.WriteCloser
|
|
|
|
outPipe, errPipe io.ReadCloser
|
|
|
|
)
|
|
|
|
|
|
|
|
if tty {
|
|
|
|
master, console, err = createMasterAndConsole()
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2014-02-19 00:56:11 +00:00
|
|
|
}
|
|
|
|
|
2014-02-20 03:14:31 +00:00
|
|
|
// create a pipe so that we can syncronize with the namespaced process and
|
|
|
|
// pass the veth name to the child
|
2014-02-21 01:58:13 +00:00
|
|
|
r, w, err := os.Pipe()
|
2014-02-19 23:33:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2014-02-21 01:58:13 +00:00
|
|
|
system.UsetCloseOnExec(r.Fd())
|
|
|
|
|
|
|
|
command := createCommand(container, console, r.Fd(), args)
|
|
|
|
|
2014-02-21 02:05:40 +00:00
|
|
|
if !tty {
|
|
|
|
inPipe, err = command.StdinPipe()
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
outPipe, err = command.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
errPipe, err = command.StderrPipe()
|
|
|
|
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-21 01:58:13 +00:00
|
|
|
|
2014-02-20 00:40:36 +00:00
|
|
|
if err := writePidFile(command); err != nil {
|
2014-02-20 22:12:08 +00:00
|
|
|
command.Process.Kill()
|
2014-02-20 00:40:36 +00:00
|
|
|
return -1, err
|
|
|
|
}
|
2014-02-20 04:35:04 +00:00
|
|
|
defer deletePidFile()
|
2014-02-19 00:56:11 +00:00
|
|
|
|
2014-02-20 22:12:08 +00:00
|
|
|
// Do this before syncing with child so that no children
|
|
|
|
// can escape the cgroup
|
2014-02-21 00:11:22 +00:00
|
|
|
if container.Cgroups != nil {
|
|
|
|
if err := container.Cgroups.Apply(command.Process.Pid); err != nil {
|
|
|
|
command.Process.Kill()
|
|
|
|
return -1, err
|
|
|
|
}
|
2014-02-20 22:12:08 +00:00
|
|
|
}
|
|
|
|
|
2014-02-19 23:33:44 +00:00
|
|
|
if container.Network != nil {
|
2014-02-20 06:43:40 +00:00
|
|
|
vethPair, err := initializeContainerVeth(container.Network.Bridge, command.Process.Pid)
|
2014-02-19 23:33:44 +00:00
|
|
|
if err != nil {
|
2014-02-20 00:40:36 +00:00
|
|
|
return -1, err
|
2014-02-19 23:33:44 +00:00
|
|
|
}
|
2014-02-21 01:59:08 +00:00
|
|
|
sendVethName(w, vethPair)
|
2014-02-19 23:33:44 +00:00
|
|
|
}
|
|
|
|
|
2014-02-20 22:12:08 +00:00
|
|
|
// Sync with child
|
2014-02-21 01:58:13 +00:00
|
|
|
w.Close()
|
2014-02-21 01:59:08 +00:00
|
|
|
r.Close()
|
2014-02-20 22:12:08 +00:00
|
|
|
|
2014-02-21 02:05:40 +00:00
|
|
|
if tty {
|
|
|
|
go io.Copy(os.Stdout, master)
|
|
|
|
go io.Copy(master, os.Stdin)
|
|
|
|
state, err := setupWindow(master)
|
|
|
|
if err != nil {
|
|
|
|
command.Process.Kill()
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
defer term.RestoreTerminal(os.Stdin.Fd(), state)
|
|
|
|
} else {
|
2014-02-21 02:10:30 +00:00
|
|
|
go func() {
|
|
|
|
defer inPipe.Close()
|
|
|
|
io.Copy(inPipe, os.Stdin)
|
|
|
|
}()
|
2014-02-21 02:05:40 +00:00
|
|
|
go io.Copy(os.Stdout, outPipe)
|
|
|
|
go io.Copy(os.Stderr, errPipe)
|
2014-02-19 22:55:34 +00:00
|
|
|
}
|
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-21 02:10:30 +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
|
|
|
}
|
|
|
|
|
2014-02-20 06:43:40 +00:00
|
|
|
// sendVethName writes the veth pair name to the child's stdin then closes the
|
|
|
|
// pipe so that the child stops waiting for more data
|
2014-02-21 01:59:08 +00:00
|
|
|
func sendVethName(pipe io.Writer, name string) {
|
2014-02-20 03:14:31 +00:00
|
|
|
fmt.Fprint(pipe, name)
|
|
|
|
}
|
|
|
|
|
2014-02-20 06:43:40 +00:00
|
|
|
// initializeContainerVeth will create a veth pair and setup the host's
|
|
|
|
// side of the pair by setting the specified bridge as the master and bringing
|
|
|
|
// up the interface.
|
|
|
|
//
|
|
|
|
// Then will with set the other side of the veth pair into the container's namespaced
|
|
|
|
// using the pid and returns the veth's interface name to provide to the container to
|
|
|
|
// finish setting up the interface inside the namespace
|
|
|
|
func initializeContainerVeth(bridge string, nspid int) (string, error) {
|
2014-02-20 03:14:31 +00:00
|
|
|
name1, name2, err := createVethPair()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if err := network.SetInterfaceMaster(name1, bridge); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if err := network.InterfaceUp(name1); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if err := network.SetInterfaceInNamespacePid(name2, nspid); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return name2, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupWindow(master *os.File) (*term.State, error) {
|
|
|
|
ws, err := term.GetWinsize(os.Stdin.Fd())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := term.SetWinsize(master.Fd(), ws); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return term.SetRawTerminal(os.Stdin.Fd())
|
|
|
|
}
|
|
|
|
|
2014-02-20 06:43:40 +00:00
|
|
|
// createMasterAndConsole will open /dev/ptmx on the host and retreive the
|
|
|
|
// pts name for use as the pty slave inside the container
|
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
|
|
|
|
2014-02-20 06:43:40 +00:00
|
|
|
// createVethPair will automatically generage two random names for
|
|
|
|
// the veth pair and ensure that they have been created
|
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
|
|
|
|
2014-02-20 06:43:40 +00:00
|
|
|
// writePidFile writes the namespaced processes pid to .nspid in the rootfs for the container
|
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)
|
|
|
|
}
|
2014-02-20 04:35:04 +00:00
|
|
|
|
|
|
|
func deletePidFile() error {
|
|
|
|
return os.Remove(".nspid")
|
|
|
|
}
|
|
|
|
|
2014-02-20 06:43:40 +00:00
|
|
|
// createCommand will return an exec.Cmd with the Cloneflags set to the proper namespaces
|
|
|
|
// defined on the container's configuration and use the current binary as the init with the
|
|
|
|
// args provided
|
2014-02-21 01:58:13 +00:00
|
|
|
func createCommand(container *libcontainer.Container, console string, pipe uintptr, args []string) *exec.Cmd {
|
|
|
|
command := exec.Command("nsinit", append([]string{"-console", console, "-pipe", fmt.Sprint(pipe), "init"}, args...)...)
|
2014-02-20 04:35:04 +00:00
|
|
|
command.SysProcAttr = &syscall.SysProcAttr{
|
2014-02-20 22:40:36 +00:00
|
|
|
Cloneflags: uintptr(getNamespaceFlags(container.Namespaces)),
|
2014-02-20 04:35:04 +00:00
|
|
|
}
|
|
|
|
return command
|
|
|
|
}
|