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 (
|
2014-07-16 07:26:28 +08:00
|
|
|
"io"
|
2014-02-19 08:56:11 +08:00
|
|
|
"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-07-12 02:02:55 +08:00
|
|
|
"github.com/docker/libcontainer/syncpipe"
|
2014-07-15 07:55:49 +08:00
|
|
|
"github.com/docker/libcontainer/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-06-24 20:30:27 +08:00
|
|
|
// Exec performs setup outside of a namespace so that a container can be
|
2014-02-21 10:27:42 +08:00
|
|
|
// executed. Exec is a high level function for working with container namespaces.
|
2014-07-16 09:24:15 +08:00
|
|
|
func Exec(container *libcontainer.Config, stdin io.Reader, stdout, stderr io.Writer, console string, rootfs, dataPath string, args []string, createCommand CreateCommand, startCallback func()) (int, error) {
|
2014-02-21 10:05:40 +08:00
|
|
|
var (
|
2014-07-16 09:24:15 +08:00
|
|
|
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-07-12 02:02:55 +08:00
|
|
|
syncPipe, err := syncpipe.NewSyncPipe()
|
2014-02-20 07:33:44 +08:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2014-06-16 21:30:42 +08:00
|
|
|
defer syncPipe.Close()
|
2014-02-21 09:58:13 +08:00
|
|
|
|
2014-07-12 02:02:55 +08:00
|
|
|
command := createCommand(container, console, rootfs, dataPath, os.Args[0], syncPipe.Child(), args)
|
2014-07-16 09:24:15 +08:00
|
|
|
// Note: these are only used in non-tty mode
|
|
|
|
// if there is a tty for the container it will be opened within the namespace and the
|
|
|
|
// fds will be duped to stdin, stdiout, and stderr
|
2014-07-16 07:26:28 +08:00
|
|
|
command.Stdin = stdin
|
|
|
|
command.Stdout = stdout
|
|
|
|
command.Stderr = stderr
|
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
|
|
|
|
2014-06-16 21:30:42 +08:00
|
|
|
// Now we passed the pipe to the child, close our side
|
|
|
|
syncPipe.CloseChild()
|
|
|
|
|
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
|
|
|
|
2014-02-21 06:12:08 +08:00
|
|
|
// Do this before syncing with child so that no children
|
|
|
|
// can escape the cgroup
|
2014-08-13 14:18:55 +08:00
|
|
|
cgroupRef, 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-08-13 14:18:55 +08:00
|
|
|
if cgroupRef != nil {
|
|
|
|
defer cgroupRef.Cleanup()
|
|
|
|
}
|
|
|
|
|
|
|
|
cgroupPaths, err := cgroupRef.Paths()
|
|
|
|
if err != nil {
|
|
|
|
command.Process.Kill()
|
|
|
|
command.Wait()
|
|
|
|
return -1, err
|
2014-03-14 17:47:49 +08:00
|
|
|
}
|
|
|
|
|
2014-06-26 06:51:28 +08:00
|
|
|
var networkState network.NetworkState
|
|
|
|
if err := InitializeNetworking(container, command.Process.Pid, syncPipe, &networkState); 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-06-25 07:43:00 +08:00
|
|
|
state := &libcontainer.State{
|
|
|
|
InitPid: command.Process.Pid,
|
|
|
|
InitStartTime: started,
|
2014-06-26 06:51:28 +08:00
|
|
|
NetworkState: networkState,
|
2014-08-13 14:18:55 +08:00
|
|
|
CgroupPaths: cgroupPaths,
|
2014-06-25 07:43:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := libcontainer.SaveState(dataPath, state); err != nil {
|
2014-06-25 05:34:09 +08:00
|
|
|
command.Process.Kill()
|
|
|
|
command.Wait()
|
|
|
|
return -1, err
|
|
|
|
}
|
2014-06-25 07:43:00 +08:00
|
|
|
defer libcontainer.DeleteState(dataPath)
|
2014-06-25 05:34:09 +08:00
|
|
|
|
2014-02-21 06:12:08 +08:00
|
|
|
// Sync with child
|
2014-06-27 02:38:53 +08:00
|
|
|
if err := syncPipe.ReadFromChild(); err != nil {
|
|
|
|
command.Process.Kill()
|
|
|
|
command.Wait()
|
2014-06-16 21:30:42 +08:00
|
|
|
return -1, err
|
|
|
|
}
|
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-07-15 07:46:51 +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
|
2014-06-24 20:30:27 +08:00
|
|
|
// init: the program executed inside the namespaces
|
2014-05-01 08:55:15 +08:00
|
|
|
// root: the path to the container json file and information
|
2014-06-24 20:30:27 +08:00
|
|
|
// pipe: sync pipe to synchronize the parent and child processes
|
|
|
|
// args: the arguments 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)
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2014-08-01 00:49:11 +08:00
|
|
|
command := exec.Command(init, append([]string{"init", "--"}, args...)...)
|
2014-05-01 08:55:15 +08:00
|
|
|
// make sure the process is executed inside the context of the rootfs
|
|
|
|
command.Dir = rootfs
|
|
|
|
command.Env = append(os.Environ(), env...)
|
|
|
|
|
2014-07-15 07:46:51 +08:00
|
|
|
if command.SysProcAttr == nil {
|
|
|
|
command.SysProcAttr = &syscall.SysProcAttr{}
|
|
|
|
}
|
|
|
|
command.SysProcAttr.Cloneflags = 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-06-24 20:30:27 +08:00
|
|
|
// SetupCgroups applies the cgroup restrictions to the process running in the container based
|
2014-05-01 06:52:40 +08:00
|
|
|
// 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
|
2014-07-15 07:46:51 +08:00
|
|
|
|
2014-04-19 12:30:08 +08:00
|
|
|
if systemd.UseSystemd() {
|
|
|
|
return systemd.Apply(c, nspid)
|
|
|
|
}
|
2014-07-15 07:46:51 +08:00
|
|
|
|
2014-04-19 12:34:26 +08:00
|
|
|
return fs.Apply(c, nspid)
|
2014-02-22 14:37:09 +08:00
|
|
|
}
|
2014-07-15 07:46:51 +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-07-12 02:02:55 +08:00
|
|
|
func InitializeNetworking(container *libcontainer.Config, nspid int, pipe *syncpipe.SyncPipe, networkState *network.NetworkState) error {
|
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-26 06:51:28 +08:00
|
|
|
if err := strategy.Create((*network.Network)(config), nspid, networkState); err != nil {
|
2014-02-22 14:20:15 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2014-06-26 06:51:28 +08:00
|
|
|
return pipe.SendToChild(networkState)
|
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
|
|
|
|
}
|