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-03-14 09:47:49 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/cgroups"
|
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 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-25 02:38:24 +00:00
|
|
|
func (ns *linuxNs) Exec(container *libcontainer.Container, term Terminal, args []string) (int, error) {
|
2014-02-21 02:05:40 +00:00
|
|
|
var (
|
2014-02-22 08:29:21 +00:00
|
|
|
master *os.File
|
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-03-13 17:43:15 +00:00
|
|
|
ns.logger.Printf("created sync pipe parent fd %d child fd %d\n", syncPipe.parent.Fd(), syncPipe.child.Fd())
|
2014-02-21 01:58:13 +00:00
|
|
|
|
2014-02-22 06:37:09 +00:00
|
|
|
if container.Tty {
|
2014-03-13 17:35:16 +00:00
|
|
|
ns.logger.Println("creating master and console")
|
2014-02-25 05:11:52 +00:00
|
|
|
master, console, err = system.CreateMasterAndConsole()
|
2014-02-22 06:37:09 +00:00
|
|
|
if err != nil {
|
2014-02-21 02:05:40 +00:00
|
|
|
return -1, err
|
|
|
|
}
|
2014-02-22 08:29:21 +00:00
|
|
|
term.SetMaster(master)
|
2014-02-22 06:37:09 +00:00
|
|
|
}
|
|
|
|
|
2014-03-06 13:10:32 +00:00
|
|
|
command := ns.commandFactory.Create(container, console, syncPipe.child, args)
|
2014-03-13 17:35:16 +00:00
|
|
|
ns.logger.Println("attach terminal to command")
|
2014-02-22 08:29:21 +00:00
|
|
|
if err := term.Attach(command); err != nil {
|
|
|
|
return -1, err
|
2014-02-21 02:05:40 +00:00
|
|
|
}
|
2014-02-22 08:29:21 +00:00
|
|
|
defer term.Close()
|
2014-02-21 02:05:40 +00:00
|
|
|
|
2014-03-13 17:35:16 +00:00
|
|
|
ns.logger.Println("starting command")
|
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-03-13 17:35:16 +00:00
|
|
|
ns.logger.Printf("writting pid %d to file\n", command.Process.Pid)
|
2014-02-25 02:38:24 +00:00
|
|
|
if err := ns.stateWriter.WritePid(command.Process.Pid); 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-03-13 17:35:16 +00:00
|
|
|
defer func() {
|
|
|
|
ns.logger.Println("removing pid file")
|
|
|
|
ns.stateWriter.DeletePid()
|
|
|
|
}()
|
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-03-13 17:35:16 +00:00
|
|
|
ns.logger.Println("setting cgroups")
|
2014-03-14 09:47:49 +00:00
|
|
|
activeCgroup, err := ns.SetupCgroups(container, command.Process.Pid)
|
|
|
|
if err != nil {
|
2014-02-22 06:37:09 +00:00
|
|
|
command.Process.Kill()
|
|
|
|
return -1, err
|
2014-02-20 22:12:08 +00:00
|
|
|
}
|
2014-03-14 09:47:49 +00:00
|
|
|
if activeCgroup != nil {
|
|
|
|
defer activeCgroup.Cleanup()
|
|
|
|
}
|
|
|
|
|
2014-03-13 17:35:16 +00:00
|
|
|
ns.logger.Println("setting up network")
|
2014-02-25 02:38:24 +00:00
|
|
|
if err := ns.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-03-13 17:35:16 +00:00
|
|
|
ns.logger.Println("closing sync pipe with child")
|
2014-02-20 22:12:08 +00:00
|
|
|
// Sync with child
|
2014-02-22 06:58:30 +00:00
|
|
|
syncPipe.Close()
|
2014-02-20 22:12:08 +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-03-13 17:35:16 +00:00
|
|
|
status := command.ProcessState.Sys().(syscall.WaitStatus).ExitStatus()
|
|
|
|
ns.logger.Printf("process exited with status %d\n", status)
|
|
|
|
return status, err
|
2014-02-19 00:56:11 +00:00
|
|
|
}
|
|
|
|
|
2014-03-14 09:47:49 +00:00
|
|
|
func (ns *linuxNs) SetupCgroups(container *libcontainer.Container, nspid int) (cgroups.ActiveCgroup, error) {
|
2014-02-22 06:37:09 +00:00
|
|
|
if container.Cgroups != nil {
|
2014-03-14 09:47:49 +00:00
|
|
|
return container.Cgroups.Apply(nspid)
|
2014-02-22 06:37:09 +00:00
|
|
|
}
|
2014-03-14 09:47:49 +00:00
|
|
|
return nil, nil
|
2014-02-22 06:37:09 +00:00
|
|
|
}
|
|
|
|
|
2014-02-25 02:38:24 +00:00
|
|
|
func (ns *linuxNs) InitializeNetworking(container *libcontainer.Container, nspid int, pipe *SyncPipe) error {
|
2014-02-26 22:19:39 +00:00
|
|
|
context := libcontainer.Context{}
|
|
|
|
for _, config := range container.Networks {
|
|
|
|
strategy, err := network.GetStrategy(config.Type)
|
2014-02-22 06:20:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-02-26 22:19:39 +00:00
|
|
|
if err := strategy.Create(config, nspid, context); err != nil {
|
2014-02-22 06:20:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2014-02-26 22:19:39 +00:00
|
|
|
return pipe.SendToChild(context)
|
2014-02-20 03:14:31 +00:00
|
|
|
}
|