2014-02-20 08:50:10 +08:00
|
|
|
// +build linux
|
|
|
|
|
2014-06-05 06:47:57 +08:00
|
|
|
package namespaces
|
2014-02-19 08:56:11 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2014-02-19 09:52:06 +08:00
|
|
|
"os/exec"
|
2014-02-19 08:56:11 +08:00
|
|
|
"syscall"
|
2014-03-04 13:47:03 +08:00
|
|
|
|
2014-06-10 23:14:16 +08:00
|
|
|
"github.com/docker/libcontainer"
|
|
|
|
"github.com/docker/libcontainer/cgroups"
|
|
|
|
"github.com/docker/libcontainer/cgroups/fs"
|
|
|
|
"github.com/docker/libcontainer/cgroups/systemd"
|
|
|
|
"github.com/docker/libcontainer/network"
|
2014-03-04 13:47:03 +08:00
|
|
|
"github.com/dotcloud/docker/pkg/system"
|
2014-02-19 08:56:11 +08:00
|
|
|
)
|
|
|
|
|
2014-06-20 07:36:39 +08:00
|
|
|
// TODO(vishh): This is part of the libcontainer API and it does much more than just namespaces related work.
|
|
|
|
// Move this to libcontainer package.
|
2014-02-21 10:27:42 +08: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-06-24 07:54:35 +08:00
|
|
|
func Exec(container *libcontainer.Config, term Terminal, rootfs, dataPath string, args []string, createCommand CreateCommand, startCallback func()) (int, error) {
|
2014-02-21 10:05:40 +08:00
|
|
|
var (
|
2014-02-22 16:29:21 +08:00
|
|
|
master *os.File
|
2014-02-22 14:37:09 +08:00
|
|
|
console string
|
|
|
|
err error
|
2014-02-21 10:05:40 +08:00
|
|
|
)
|
|
|
|
|
2014-02-20 11:14:31 +08:00
|
|
|
// create a pipe so that we can syncronize with the namespaced process and
|
|
|
|
// pass the veth name to the child
|
2014-02-22 14:58:30 +08:00
|
|
|
syncPipe, err := NewSyncPipe()
|
2014-02-20 07:33:44 +08:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2014-02-21 09:58:13 +08:00
|
|
|
|
2014-02-22 14:37:09 +08:00
|
|
|
if container.Tty {
|
2014-02-25 13:11:52 +08:00
|
|
|
master, console, err = system.CreateMasterAndConsole()
|
2014-02-22 14:37:09 +08:00
|
|
|
if err != nil {
|
2014-02-21 10:05:40 +08:00
|
|
|
return -1, err
|
|
|
|
}
|
2014-02-22 16:29:21 +08:00
|
|
|
term.SetMaster(master)
|
2014-02-22 14:37:09 +08:00
|
|
|
}
|
|
|
|
|
2014-05-01 09:49:24 +08:00
|
|
|
command := createCommand(container, console, rootfs, dataPath, os.Args[0], syncPipe.child, args)
|
2014-05-23 06:56:10 +08:00
|
|
|
|
2014-02-22 16:29:21 +08:00
|
|
|
if err := term.Attach(command); err != nil {
|
|
|
|
return -1, err
|
2014-02-21 10:05:40 +08:00
|
|
|
}
|
2014-02-22 16:29:21 +08:00
|
|
|
defer term.Close()
|
2014-02-21 10:05:40 +08:00
|
|
|
|
2014-02-19 09:52:06 +08:00
|
|
|
if err := command.Start(); err != nil {
|
|
|
|
return -1, err
|
2014-02-19 08:56:11 +08:00
|
|
|
}
|
2014-03-26 14:48:16 +08:00
|
|
|
|
|
|
|
started, err := system.GetProcessStartTime(command.Process.Pid)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2014-06-25 07:54:50 +08:00
|
|
|
|
|
|
|
state := &libcontainer.State{
|
2014-06-26 02:42:08 +08:00
|
|
|
InitPid: command.Process.Pid,
|
|
|
|
InitStartTime: started,
|
2014-06-25 07:54:50 +08:00
|
|
|
}
|
|
|
|
|
2014-06-25 08:17:41 +08:00
|
|
|
if err := libcontainer.SaveState(dataPath, state); err != nil {
|
2014-02-21 06:12:08 +08:00
|
|
|
command.Process.Kill()
|
2014-05-28 04:38:24 +08:00
|
|
|
command.Wait()
|
2014-02-20 08:40:36 +08:00
|
|
|
return -1, err
|
|
|
|
}
|
2014-06-25 07:54:50 +08:00
|
|
|
defer libcontainer.DeleteState(dataPath)
|
2014-02-19 08:56:11 +08:00
|
|
|
|
2014-02-21 06:12:08 +08:00
|
|
|
// Do this before syncing with child so that no children
|
|
|
|
// can escape the cgroup
|
2014-05-01 06:52:40 +08:00
|
|
|
cleaner, err := SetupCgroups(container, command.Process.Pid)
|
2014-03-14 17:47:49 +08:00
|
|
|
if err != nil {
|
2014-02-22 14:37:09 +08:00
|
|
|
command.Process.Kill()
|
2014-05-28 04:38:24 +08:00
|
|
|
command.Wait()
|
2014-02-22 14:37:09 +08:00
|
|
|
return -1, err
|
2014-02-21 06:12:08 +08:00
|
|
|
}
|
2014-05-01 06:52:40 +08:00
|
|
|
if cleaner != nil {
|
|
|
|
defer cleaner.Cleanup()
|
2014-03-14 17:47:49 +08:00
|
|
|
}
|
|
|
|
|
2014-05-01 06:52:40 +08:00
|
|
|
if err := InitializeNetworking(container, command.Process.Pid, syncPipe); err != nil {
|
2014-02-22 14:20:15 +08:00
|
|
|
command.Process.Kill()
|
2014-05-28 04:38:24 +08:00
|
|
|
command.Wait()
|
2014-02-22 14:20:15 +08:00
|
|
|
return -1, err
|
2014-02-20 07:33:44 +08:00
|
|
|
}
|
|
|
|
|
2014-02-21 06:12:08 +08:00
|
|
|
// Sync with child
|
2014-02-22 14:58:30 +08:00
|
|
|
syncPipe.Close()
|
2014-02-21 06:12:08 +08:00
|
|
|
|
2014-05-01 06:52:40 +08:00
|
|
|
if startCallback != nil {
|
|
|
|
startCallback()
|
|
|
|
}
|
|
|
|
|
2014-02-20 06:33:25 +08:00
|
|
|
if err := command.Wait(); err != nil {
|
2014-02-20 08:40:36 +08:00
|
|
|
if _, ok := err.(*exec.ExitError); !ok {
|
|
|
|
return -1, err
|
|
|
|
}
|
2014-02-20 06:33:25 +08:00
|
|
|
}
|
2014-05-01 08:55:15 +08:00
|
|
|
return command.ProcessState.Sys().(syscall.WaitStatus).ExitStatus(), nil
|
|
|
|
}
|
|
|
|
|
2014-05-01 09:49:24 +08:00
|
|
|
// DefaultCreateCommand will return an exec.Cmd with the Cloneflags set to the proper namespaces
|
2014-05-01 08:55:15 +08:00
|
|
|
// defined on the container's configuration and use the current binary as the init with the
|
|
|
|
// args provided
|
|
|
|
//
|
|
|
|
// console: the /dev/console to setup inside the container
|
|
|
|
// init: the progam executed inside the namespaces
|
|
|
|
// root: the path to the container json file and information
|
|
|
|
// pipe: sync pipe to syncronize the parent and child processes
|
|
|
|
// args: the arguemnts to pass to the container to run as the user's program
|
2014-06-24 07:54:35 +08:00
|
|
|
func DefaultCreateCommand(container *libcontainer.Config, console, rootfs, dataPath, init string, pipe *os.File, args []string) *exec.Cmd {
|
2014-05-01 08:55:15 +08:00
|
|
|
// get our binary name from arg0 so we can always reexec ourself
|
|
|
|
env := []string{
|
|
|
|
"console=" + console,
|
|
|
|
"pipe=3",
|
|
|
|
"data_path=" + dataPath,
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
TODO: move user and wd into env
|
|
|
|
if user != "" {
|
|
|
|
env = append(env, "user="+user)
|
|
|
|
}
|
|
|
|
if workingDir != "" {
|
|
|
|
env = append(env, "wd="+workingDir)
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
command := exec.Command(init, append([]string{"init"}, args...)...)
|
|
|
|
// make sure the process is executed inside the context of the rootfs
|
|
|
|
command.Dir = rootfs
|
|
|
|
command.Env = append(os.Environ(), env...)
|
|
|
|
|
|
|
|
system.SetCloneFlags(command, uintptr(GetNamespaceFlags(container.Namespaces)))
|
2014-05-14 17:50:15 +08:00
|
|
|
command.SysProcAttr.Pdeathsig = syscall.SIGKILL
|
2014-05-01 08:55:15 +08:00
|
|
|
command.ExtraFiles = []*os.File{pipe}
|
|
|
|
|
|
|
|
return command
|
2014-02-19 08:56:11 +08:00
|
|
|
}
|
|
|
|
|
2014-05-01 06:52:40 +08:00
|
|
|
// SetupCgroups applies the cgroup restrictions to the process running in the contaienr based
|
|
|
|
// on the container's configuration
|
2014-06-24 07:54:35 +08:00
|
|
|
func SetupCgroups(container *libcontainer.Config, nspid int) (cgroups.ActiveCgroup, error) {
|
2014-02-22 14:37:09 +08:00
|
|
|
if container.Cgroups != nil {
|
2014-04-19 12:30:08 +08:00
|
|
|
c := container.Cgroups
|
|
|
|
if systemd.UseSystemd() {
|
|
|
|
return systemd.Apply(c, nspid)
|
|
|
|
}
|
2014-04-19 12:34:26 +08:00
|
|
|
return fs.Apply(c, nspid)
|
2014-02-22 14:37:09 +08:00
|
|
|
}
|
2014-03-14 17:47:49 +08:00
|
|
|
return nil, nil
|
2014-02-22 14:37:09 +08:00
|
|
|
}
|
|
|
|
|
2014-05-01 06:52:40 +08:00
|
|
|
// InitializeNetworking creates the container's network stack outside of the namespace and moves
|
|
|
|
// interfaces into the container's net namespaces if necessary
|
2014-06-24 07:54:35 +08:00
|
|
|
func InitializeNetworking(container *libcontainer.Config, nspid int, pipe *SyncPipe) error {
|
2014-06-20 07:36:39 +08:00
|
|
|
context := map[string]string{}
|
2014-02-27 06:19:39 +08:00
|
|
|
for _, config := range container.Networks {
|
|
|
|
strategy, err := network.GetStrategy(config.Type)
|
2014-02-22 14:20:15 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-06-24 05:11:01 +08:00
|
|
|
if err := strategy.Create((*network.Network)(config), nspid, context); err != nil {
|
2014-02-22 14:20:15 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2014-02-27 06:19:39 +08:00
|
|
|
return pipe.SendToChild(context)
|
2014-02-20 11:14:31 +08:00
|
|
|
}
|
2014-05-01 06:52:40 +08:00
|
|
|
|
2014-05-01 08:18:07 +08:00
|
|
|
// GetNamespaceFlags parses the container's Namespaces options to set the correct
|
|
|
|
// flags on clone, unshare, and setns
|
2014-05-06 03:34:21 +08:00
|
|
|
func GetNamespaceFlags(namespaces map[string]bool) (flag int) {
|
|
|
|
for key, enabled := range namespaces {
|
|
|
|
if enabled {
|
2014-06-20 07:36:39 +08:00
|
|
|
if ns := GetNamespace(key); ns != nil {
|
2014-05-06 03:34:21 +08:00
|
|
|
flag |= ns.Value
|
|
|
|
}
|
2014-05-01 08:18:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return flag
|
|
|
|
}
|