2014-02-19 16:50:10 -08:00
|
|
|
// +build linux
|
|
|
|
|
2014-02-20 18:27:42 -08:00
|
|
|
package nsinit
|
2014-02-18 16:56:11 -08:00
|
|
|
|
|
|
|
import (
|
2014-04-30 15:52:40 -07:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2014-02-18 16:56:11 -08:00
|
|
|
"os"
|
2014-02-18 17:52:06 -08:00
|
|
|
"os/exec"
|
2014-04-30 15:52:40 -07:00
|
|
|
"path/filepath"
|
2014-02-18 16:56:11 -08:00
|
|
|
"syscall"
|
2014-03-03 21:47:03 -08:00
|
|
|
|
2014-03-14 10:47:49 +01:00
|
|
|
"github.com/dotcloud/docker/pkg/cgroups"
|
2014-04-18 21:34:26 -07:00
|
|
|
"github.com/dotcloud/docker/pkg/cgroups/fs"
|
2014-04-18 21:30:08 -07:00
|
|
|
"github.com/dotcloud/docker/pkg/cgroups/systemd"
|
2014-03-03 21:47:03 -08:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer"
|
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer/network"
|
|
|
|
"github.com/dotcloud/docker/pkg/system"
|
2014-02-18 16:56:11 -08:00
|
|
|
)
|
|
|
|
|
2014-02-20 18: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-04-30 15:52:40 -07:00
|
|
|
func (ns *linuxNs) Exec(container *libcontainer.Container, term Terminal, pidRoot string, args []string, startCallback func()) (int, error) {
|
2014-02-20 18:05:40 -08:00
|
|
|
var (
|
2014-02-22 00:29:21 -08:00
|
|
|
master *os.File
|
2014-02-21 22:37:09 -08:00
|
|
|
console string
|
|
|
|
err error
|
2014-02-20 18:05:40 -08:00
|
|
|
)
|
|
|
|
|
2014-02-19 19: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-21 22:58:30 -08:00
|
|
|
syncPipe, err := NewSyncPipe()
|
2014-02-19 15:33:44 -08:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2014-02-20 17:58:13 -08:00
|
|
|
|
2014-02-21 22:37:09 -08:00
|
|
|
if container.Tty {
|
2014-02-24 21:11:52 -08:00
|
|
|
master, console, err = system.CreateMasterAndConsole()
|
2014-02-21 22:37:09 -08:00
|
|
|
if err != nil {
|
2014-02-20 18:05:40 -08:00
|
|
|
return -1, err
|
|
|
|
}
|
2014-02-22 00:29:21 -08:00
|
|
|
term.SetMaster(master)
|
2014-02-21 22:37:09 -08:00
|
|
|
}
|
|
|
|
|
2014-03-06 14:10:32 +01:00
|
|
|
command := ns.commandFactory.Create(container, console, syncPipe.child, args)
|
2014-02-22 00:29:21 -08:00
|
|
|
if err := term.Attach(command); err != nil {
|
|
|
|
return -1, err
|
2014-02-20 18:05:40 -08:00
|
|
|
}
|
2014-02-22 00:29:21 -08:00
|
|
|
defer term.Close()
|
2014-02-20 18:05:40 -08:00
|
|
|
|
2014-02-18 17:52:06 -08:00
|
|
|
if err := command.Start(); err != nil {
|
|
|
|
return -1, err
|
2014-02-18 16:56:11 -08:00
|
|
|
}
|
2014-03-26 06:48:16 +00:00
|
|
|
|
|
|
|
started, err := system.GetProcessStartTime(command.Process.Pid)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2014-04-30 15:52:40 -07:00
|
|
|
if err := WritePid(pidRoot, command.Process.Pid, started); err != nil {
|
2014-02-20 23:12:08 +01:00
|
|
|
command.Process.Kill()
|
2014-02-19 16:40:36 -08:00
|
|
|
return -1, err
|
|
|
|
}
|
2014-04-30 15:52:40 -07:00
|
|
|
defer DeletePid(pidRoot)
|
2014-02-18 16:56:11 -08:00
|
|
|
|
2014-02-20 23:12:08 +01:00
|
|
|
// Do this before syncing with child so that no children
|
|
|
|
// can escape the cgroup
|
2014-04-30 15:52:40 -07:00
|
|
|
cleaner, err := SetupCgroups(container, command.Process.Pid)
|
2014-03-14 10:47:49 +01:00
|
|
|
if err != nil {
|
2014-02-21 22:37:09 -08:00
|
|
|
command.Process.Kill()
|
|
|
|
return -1, err
|
2014-02-20 23:12:08 +01:00
|
|
|
}
|
2014-04-30 15:52:40 -07:00
|
|
|
if cleaner != nil {
|
|
|
|
defer cleaner.Cleanup()
|
2014-03-14 10:47:49 +01:00
|
|
|
}
|
|
|
|
|
2014-04-30 15:52:40 -07:00
|
|
|
if err := InitializeNetworking(container, command.Process.Pid, syncPipe); err != nil {
|
2014-02-21 22:20:15 -08:00
|
|
|
command.Process.Kill()
|
|
|
|
return -1, err
|
2014-02-19 15:33:44 -08:00
|
|
|
}
|
|
|
|
|
2014-02-20 23:12:08 +01:00
|
|
|
// Sync with child
|
2014-02-21 22:58:30 -08:00
|
|
|
syncPipe.Close()
|
2014-02-20 23:12:08 +01:00
|
|
|
|
2014-04-30 15:52:40 -07:00
|
|
|
if startCallback != nil {
|
|
|
|
startCallback()
|
|
|
|
}
|
|
|
|
|
2014-02-19 14:33:25 -08:00
|
|
|
if err := command.Wait(); err != nil {
|
2014-02-19 16:40:36 -08:00
|
|
|
if _, ok := err.(*exec.ExitError); !ok {
|
|
|
|
return -1, err
|
|
|
|
}
|
2014-02-19 14:33:25 -08:00
|
|
|
}
|
2014-03-13 10:35:16 -07:00
|
|
|
status := command.ProcessState.Sys().(syscall.WaitStatus).ExitStatus()
|
|
|
|
return status, err
|
2014-02-18 16:56:11 -08:00
|
|
|
}
|
|
|
|
|
2014-04-30 15:52:40 -07: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-21 22:37:09 -08:00
|
|
|
if container.Cgroups != nil {
|
2014-04-18 21:30:08 -07:00
|
|
|
c := container.Cgroups
|
|
|
|
if systemd.UseSystemd() {
|
|
|
|
return systemd.Apply(c, nspid)
|
|
|
|
}
|
2014-04-18 21:34:26 -07:00
|
|
|
return fs.Apply(c, nspid)
|
2014-02-21 22:37:09 -08:00
|
|
|
}
|
2014-03-14 10:47:49 +01:00
|
|
|
return nil, nil
|
2014-02-21 22:37:09 -08:00
|
|
|
}
|
|
|
|
|
2014-04-30 15:52:40 -07: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 14:19:39 -08:00
|
|
|
context := libcontainer.Context{}
|
|
|
|
for _, config := range container.Networks {
|
|
|
|
strategy, err := network.GetStrategy(config.Type)
|
2014-02-21 22:20:15 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-02-26 14:19:39 -08:00
|
|
|
if err := strategy.Create(config, nspid, context); err != nil {
|
2014-02-21 22:20:15 -08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2014-02-26 14:19:39 -08:00
|
|
|
return pipe.SendToChild(context)
|
2014-02-19 19:14:31 -08:00
|
|
|
}
|
2014-04-30 15:52:40 -07: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-04-30 17:18:07 -07: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
|
|
|
|
}
|