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 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-21 22:49:55 +00:00
|
|
|
"log"
|
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.
|
2014-02-22 06:20:15 +00:00
|
|
|
func Exec(container *libcontainer.Container, stdin io.Reader, stdout, stderr io.Writer,
|
|
|
|
master *os.File, logFile string, args []string) (int, error) {
|
2014-02-21 02:05:40 +00:00
|
|
|
var (
|
2014-02-22 06:37:09 +00:00
|
|
|
console string
|
|
|
|
err error
|
2014-02-21 02:05:40 +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-22 06:58:30 +00:00
|
|
|
syncPipe, err := NewSyncPipe()
|
2014-02-19 23:33:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2014-02-21 01:58:13 +00:00
|
|
|
|
2014-02-22 06:37:09 +00:00
|
|
|
if container.Tty {
|
|
|
|
log.Printf("setting up master and console")
|
|
|
|
master, console, err = CreateMasterAndConsole()
|
|
|
|
if err != nil {
|
2014-02-21 02:05:40 +00:00
|
|
|
return -1, err
|
|
|
|
}
|
2014-02-22 06:37:09 +00:00
|
|
|
}
|
|
|
|
|
2014-02-22 06:58:30 +00:00
|
|
|
command := CreateCommand(container, console, logFile, syncPipe.child.Fd(), args)
|
2014-02-22 06:37:09 +00:00
|
|
|
if container.Tty {
|
|
|
|
log.Printf("starting copy for tty")
|
|
|
|
go io.Copy(stdout, master)
|
|
|
|
go io.Copy(master, stdin)
|
|
|
|
|
|
|
|
state, err := SetupWindow(master, os.Stdin)
|
|
|
|
if err != nil {
|
|
|
|
command.Process.Kill()
|
2014-02-21 02:05:40 +00:00
|
|
|
return -1, err
|
|
|
|
}
|
2014-02-22 06:37:09 +00:00
|
|
|
defer term.RestoreTerminal(os.Stdin.Fd(), state)
|
|
|
|
} else {
|
|
|
|
if err := startStdCopy(command, stdin, stdout, stderr); err != nil {
|
|
|
|
command.Process.Kill()
|
2014-02-21 02:05:40 +00:00
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-21 22:49:55 +00:00
|
|
|
log.Printf("staring init")
|
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-22 06:37:09 +00:00
|
|
|
log.Printf("writing state file")
|
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-22 06:37:09 +00:00
|
|
|
if err := SetupCgroups(container, command.Process.Pid); err != nil {
|
|
|
|
command.Process.Kill()
|
|
|
|
return -1, err
|
2014-02-20 22:12:08 +00:00
|
|
|
}
|
2014-02-22 06:58:30 +00:00
|
|
|
if err := InitializeNetworking(container, command.Process.Pid, syncPipe); err != nil {
|
2014-02-22 06:20:15 +00:00
|
|
|
command.Process.Kill()
|
|
|
|
return -1, err
|
2014-02-19 23:33:44 +00:00
|
|
|
}
|
|
|
|
|
2014-02-20 22:12:08 +00:00
|
|
|
// Sync with child
|
2014-02-21 22:49:55 +00:00
|
|
|
log.Printf("closing sync pipes")
|
2014-02-22 06:58:30 +00:00
|
|
|
syncPipe.Close()
|
2014-02-20 22:12:08 +00:00
|
|
|
|
2014-02-21 22:49:55 +00:00
|
|
|
log.Printf("waiting on process")
|
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 22:49:55 +00:00
|
|
|
log.Printf("process ended")
|
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-22 06:37:09 +00:00
|
|
|
func SetupCgroups(container *libcontainer.Container, nspid int) error {
|
|
|
|
if container.Cgroups != nil {
|
|
|
|
log.Printf("setting up cgroups")
|
|
|
|
if err := container.Cgroups.Apply(nspid); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-02-22 06:58:30 +00:00
|
|
|
func InitializeNetworking(container *libcontainer.Container, nspid int, pipe *SyncPipe) error {
|
2014-02-22 06:20:15 +00:00
|
|
|
if container.Network != nil {
|
|
|
|
log.Printf("creating host network configuration type %s", container.Network.Type)
|
|
|
|
strategy, err := network.GetStrategy(container.Network.Type)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
networkContext, err := strategy.Create(container.Network, nspid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Printf("sending %v as network context", networkContext)
|
2014-02-22 06:58:30 +00:00
|
|
|
if err := pipe.SendToChild(networkContext); err != nil {
|
2014-02-22 06:20:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2014-02-20 03:14:31 +00:00
|
|
|
}
|
|
|
|
|
2014-02-22 05:14:21 +00:00
|
|
|
// SetupWindow gets the parent window size and sets the master
|
|
|
|
// pty to the current size and set the parents mode to RAW
|
|
|
|
func SetupWindow(master, parent *os.File) (*term.State, error) {
|
|
|
|
ws, err := term.GetWinsize(parent.Fd())
|
2014-02-20 03:14:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := term.SetWinsize(master.Fd(), ws); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-02-22 05:14:21 +00:00
|
|
|
return term.SetRawTerminal(parent.Fd())
|
2014-02-20 03:14:31 +00:00
|
|
|
}
|
|
|
|
|
2014-02-22 05:14:21 +00:00
|
|
|
// CreateMasterAndConsole will open /dev/ptmx on the host and retreive the
|
2014-02-20 06:43:40 +00:00
|
|
|
// pts name for use as the pty slave inside the container
|
2014-02-22 05:14:21 +00:00
|
|
|
func CreateMasterAndConsole() (*os.File, string, error) {
|
2014-02-19 00:56:11 +00:00
|
|
|
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
|
|
|
// 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 {
|
2014-02-22 06:20:15 +00:00
|
|
|
log.Printf("removing .nspid file")
|
2014-02-20 04:35:04 +00:00
|
|
|
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-22 05:14:21 +00:00
|
|
|
func CreateCommand(container *libcontainer.Container, console, logFile string, pipe uintptr, args []string) *exec.Cmd {
|
2014-02-21 23:32:50 +00:00
|
|
|
// get our binary name so we can always reexec ourself
|
|
|
|
name := os.Args[0]
|
|
|
|
command := exec.Command(name, append([]string{
|
2014-02-21 22:49:55 +00:00
|
|
|
"-console", console,
|
|
|
|
"-pipe", fmt.Sprint(pipe),
|
|
|
|
"-log", logFile,
|
|
|
|
"init"}, args...)...)
|
|
|
|
|
2014-02-20 04:35:04 +00:00
|
|
|
command.SysProcAttr = &syscall.SysProcAttr{
|
2014-02-22 01:11:57 +00:00
|
|
|
Cloneflags: uintptr(GetNamespaceFlags(container.Namespaces)),
|
2014-02-20 04:35:04 +00:00
|
|
|
}
|
2014-02-22 00:17:18 +00:00
|
|
|
command.Env = container.Env
|
2014-02-20 04:35:04 +00:00
|
|
|
return command
|
|
|
|
}
|
2014-02-22 06:37:09 +00:00
|
|
|
|
|
|
|
func startStdCopy(command *exec.Cmd, stdin io.Reader, stdout, stderr io.Writer) error {
|
|
|
|
log.Printf("opening std pipes")
|
|
|
|
inPipe, err := command.StdinPipe()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
outPipe, err := command.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
errPipe, err := command.StderrPipe()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("starting copy for std pipes")
|
|
|
|
go func() {
|
|
|
|
defer inPipe.Close()
|
|
|
|
io.Copy(inPipe, stdin)
|
|
|
|
}()
|
|
|
|
go io.Copy(stdout, outPipe)
|
|
|
|
go io.Copy(stderr, errPipe)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|