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-04-30 22:52:40 +00:00
|
|
|
func (ns *linuxNs) Exec(container *libcontainer.Container, term Terminal, pidRoot 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-03-06 13:10:32 +00:00
|
|
|
command := ns.commandFactory.Create(container, console, 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-04-30 22:52:40 +00:00
|
|
|
if err := WritePid(pidRoot, 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-04-30 22:52:40 +00:00
|
|
|
defer DeletePid(pidRoot)
|
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-03-13 17:35:16 +00:00
|
|
|
status := command.ProcessState.Sys().(syscall.WaitStatus).ExitStatus()
|
|
|
|
return status, err
|
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
|
|
|
|
}
|