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-04-30 22:52:40 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2014-02-19 00:56:11 +00:00
|
|
|
"os"
|
2014-02-19 01:52:06 +00:00
|
|
|
"os/exec"
|
2014-04-30 22:52:40 +00:00
|
|
|
"path/filepath"
|
2014-02-19 00:56:11 +00:00
|
|
|
"syscall"
|
2014-03-04 05:47:03 +00:00
|
|
|
|
2014-03-14 09:47:49 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/cgroups"
|
2014-04-19 04:34:26 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/cgroups/fs"
|
2014-04-19 04:30:08 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/cgroups/systemd"
|
2014-03-04 05:47:03 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer"
|
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer/network"
|
|
|
|
"github.com/dotcloud/docker/pkg/system"
|
2014-02-19 00:56:11 +00:00
|
|
|
)
|
|
|
|
|
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-05-01 00:55:15 +00:00
|
|
|
func Exec(container *libcontainer.Container, term Terminal, rootfs, dataPath string, args []string, startCallback func()) (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-02-21 01:58:13 +00:00
|
|
|
|
2014-02-22 06:37:09 +00:00
|
|
|
if container.Tty {
|
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-05-01 00:55:15 +00:00
|
|
|
command := CreateCommand(container, console, rootfs, dataPath, os.Args[0], syncPipe.child, args)
|
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-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-26 06:48:16 +00:00
|
|
|
|
|
|
|
started, err := system.GetProcessStartTime(command.Process.Pid)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2014-05-01 00:55:15 +00:00
|
|
|
if err := WritePid(dataPath, command.Process.Pid, started); 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-05-01 00:55:15 +00:00
|
|
|
defer DeletePid(dataPath)
|
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-04-30 22:52:40 +00:00
|
|
|
cleaner, err := SetupCgroups(container, command.Process.Pid)
|
2014-03-14 09:47:49 +00:00
|
|
|
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-04-30 22:52:40 +00:00
|
|
|
if cleaner != nil {
|
|
|
|
defer cleaner.Cleanup()
|
2014-03-14 09:47:49 +00:00
|
|
|
}
|
|
|
|
|
2014-04-30 22:52:40 +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-22 06:58:30 +00:00
|
|
|
syncPipe.Close()
|
2014-02-20 22:12:08 +00:00
|
|
|
|
2014-04-30 22:52:40 +00:00
|
|
|
if startCallback != nil {
|
|
|
|
startCallback()
|
|
|
|
}
|
|
|
|
|
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-05-01 00:55:15 +00:00
|
|
|
return command.ProcessState.Sys().(syscall.WaitStatus).ExitStatus(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
func CreateCommand(container *libcontainer.Container, console, rootfs, dataPath, init string, pipe *os.File, args []string) *exec.Cmd {
|
|
|
|
// 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)))
|
|
|
|
command.ExtraFiles = []*os.File{pipe}
|
|
|
|
|
|
|
|
return command
|
2014-02-19 00:56:11 +00:00
|
|
|
}
|
|
|
|
|
2014-04-30 22:52:40 +00:00
|
|
|
// SetupCgroups applies the cgroup restrictions to the process running in the contaienr based
|
|
|
|
// on the container's configuration
|
|
|
|
func SetupCgroups(container *libcontainer.Container, nspid int) (cgroups.ActiveCgroup, error) {
|
2014-02-22 06:37:09 +00:00
|
|
|
if container.Cgroups != nil {
|
2014-04-19 04:30:08 +00:00
|
|
|
c := container.Cgroups
|
|
|
|
if systemd.UseSystemd() {
|
|
|
|
return systemd.Apply(c, nspid)
|
|
|
|
}
|
2014-04-19 04:34:26 +00:00
|
|
|
return fs.Apply(c, 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-04-30 22:52:40 +00:00
|
|
|
// InitializeNetworking creates the container's network stack outside of the namespace and moves
|
|
|
|
// interfaces into the container's net namespaces if necessary
|
|
|
|
func 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
|
|
|
}
|
2014-04-30 22:52:40 +00:00
|
|
|
|
|
|
|
// WritePid writes the namespaced processes pid to pid and it's start time
|
|
|
|
// to the path specified
|
|
|
|
func WritePid(path string, pid int, startTime string) error {
|
|
|
|
err := ioutil.WriteFile(filepath.Join(path, "pid"), []byte(fmt.Sprint(pid)), 0655)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ioutil.WriteFile(filepath.Join(path, "start"), []byte(startTime), 0655)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeletePid removes the pid and started file from disk when the container's process
|
|
|
|
// dies and the container is cleanly removed
|
|
|
|
func DeletePid(path string) error {
|
|
|
|
err := os.Remove(filepath.Join(path, "pid"))
|
|
|
|
if serr := os.Remove(filepath.Join(path, "start")); err == nil {
|
|
|
|
err = serr
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2014-05-01 00:18:07 +00:00
|
|
|
|
|
|
|
// GetNamespaceFlags parses the container's Namespaces options to set the correct
|
|
|
|
// flags on clone, unshare, and setns
|
|
|
|
func GetNamespaceFlags(namespaces libcontainer.Namespaces) (flag int) {
|
|
|
|
for _, ns := range namespaces {
|
|
|
|
if ns.Enabled {
|
|
|
|
flag |= ns.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return flag
|
|
|
|
}
|