2014-02-25 18:19:13 -05:00
|
|
|
// +build linux
|
|
|
|
|
2014-02-20 21:27:42 -05:00
|
|
|
package nsinit
|
2014-02-19 22:53:25 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-03-18 16:49:16 -04:00
|
|
|
"github.com/dotcloud/docker/pkg/label"
|
2014-02-19 22:53:25 -05:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer"
|
2014-04-11 11:06:56 -04:00
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer/mount"
|
2014-02-19 22:53:25 -05:00
|
|
|
"github.com/dotcloud/docker/pkg/system"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2014-02-20 21:27:42 -05:00
|
|
|
// ExecIn uses an existing pid and joins the pid's namespaces with the new command.
|
2014-02-24 21:38:24 -05:00
|
|
|
func (ns *linuxNs) ExecIn(container *libcontainer.Container, nspid int, args []string) (int, error) {
|
2014-03-17 20:42:16 -04:00
|
|
|
for _, nsv := range container.Namespaces {
|
|
|
|
// skip the PID namespace on unshare because it it not supported
|
|
|
|
if nsv.Key != "NEWPID" {
|
|
|
|
if err := system.Unshare(nsv.Value); err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2014-02-19 22:53:25 -05:00
|
|
|
}
|
|
|
|
}
|
2014-02-25 00:11:52 -05:00
|
|
|
fds, err := ns.getNsFds(nspid, container)
|
2014-02-19 22:53:25 -05:00
|
|
|
closeFds := func() {
|
|
|
|
for _, f := range fds {
|
|
|
|
system.Closefd(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
closeFds()
|
|
|
|
return -1, err
|
|
|
|
}
|
2014-03-18 16:49:16 -04:00
|
|
|
processLabel, err := label.GetPidCon(nspid)
|
|
|
|
if err != nil {
|
|
|
|
closeFds()
|
|
|
|
return -1, err
|
|
|
|
}
|
2014-02-20 01:43:40 -05:00
|
|
|
// foreach namespace fd, use setns to join an existing container's namespaces
|
2014-02-19 22:53:25 -05:00
|
|
|
for _, fd := range fds {
|
|
|
|
if fd > 0 {
|
2014-03-13 13:35:16 -04:00
|
|
|
ns.logger.Printf("setns on %d\n", fd)
|
2014-02-19 22:53:25 -05:00
|
|
|
if err := system.Setns(fd, 0); err != nil {
|
|
|
|
closeFds()
|
|
|
|
return -1, fmt.Errorf("setns %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
system.Closefd(fd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the container has a new pid and mount namespace we need to
|
|
|
|
// remount proc and sys to pick up the changes
|
2014-02-25 15:41:31 -05:00
|
|
|
if container.Namespaces.Contains("NEWNS") && container.Namespaces.Contains("NEWPID") {
|
2014-03-13 13:35:16 -04:00
|
|
|
ns.logger.Println("forking to remount /proc and /sys")
|
2014-02-19 22:53:25 -05:00
|
|
|
pid, err := system.Fork()
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
if pid == 0 {
|
|
|
|
// TODO: make all raw syscalls to be fork safe
|
|
|
|
if err := system.Unshare(syscall.CLONE_NEWNS); err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2014-04-11 11:06:56 -04:00
|
|
|
if err := mount.RemountProc(); err != nil {
|
2014-02-19 22:53:25 -05:00
|
|
|
return -1, fmt.Errorf("remount proc %s", err)
|
|
|
|
}
|
2014-04-11 11:06:56 -04:00
|
|
|
if err := mount.RemountSys(); err != nil {
|
2014-02-19 22:53:25 -05:00
|
|
|
return -1, fmt.Errorf("remount sys %s", err)
|
|
|
|
}
|
2014-02-20 01:43:40 -05:00
|
|
|
goto dropAndExec
|
2014-02-19 22:53:25 -05:00
|
|
|
}
|
|
|
|
proc, err := os.FindProcess(pid)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
state, err := proc.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
os.Exit(state.Sys().(syscall.WaitStatus).ExitStatus())
|
|
|
|
}
|
2014-02-20 01:43:40 -05:00
|
|
|
dropAndExec:
|
2014-03-03 15:15:47 -05:00
|
|
|
if err := finalizeNamespace(container); err != nil {
|
|
|
|
return -1, err
|
2014-02-19 22:53:25 -05:00
|
|
|
}
|
2014-03-18 16:49:16 -04:00
|
|
|
err = label.SetProcessLabel(processLabel)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2014-02-25 00:11:52 -05:00
|
|
|
if err := system.Execv(args[0], args[0:], container.Env); err != nil {
|
2014-02-19 22:53:25 -05:00
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
|
2014-02-25 00:11:52 -05:00
|
|
|
func (ns *linuxNs) getNsFds(pid int, container *libcontainer.Container) ([]uintptr, error) {
|
2014-02-19 22:53:25 -05:00
|
|
|
fds := make([]uintptr, len(container.Namespaces))
|
|
|
|
for i, ns := range container.Namespaces {
|
2014-02-25 00:52:29 -05:00
|
|
|
f, err := os.OpenFile(filepath.Join("/proc/", strconv.Itoa(pid), "ns", ns.File), os.O_RDONLY, 0)
|
2014-02-19 22:53:25 -05:00
|
|
|
if err != nil {
|
|
|
|
return fds, err
|
|
|
|
}
|
|
|
|
fds[i] = f.Fd()
|
|
|
|
}
|
|
|
|
return fds, nil
|
|
|
|
}
|