2014-02-25 23:19:13 +00:00
|
|
|
// +build linux
|
|
|
|
|
2014-06-04 22:47:57 +00:00
|
|
|
package namespaces
|
2014-02-20 03:53:25 +00:00
|
|
|
|
|
|
|
import (
|
2014-05-24 01:06:14 +00:00
|
|
|
"encoding/json"
|
2014-02-20 03:53:25 +00:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2014-04-30 22:24:18 +00:00
|
|
|
|
|
|
|
"github.com/dotcloud/docker/pkg/label"
|
|
|
|
"github.com/dotcloud/docker/pkg/libcontainer"
|
|
|
|
"github.com/dotcloud/docker/pkg/system"
|
2014-02-20 03:53:25 +00:00
|
|
|
)
|
|
|
|
|
2014-02-21 02:27:42 +00:00
|
|
|
// ExecIn uses an existing pid and joins the pid's namespaces with the new command.
|
2014-05-24 01:06:14 +00:00
|
|
|
func ExecIn(container *libcontainer.Container, nspid int, args []string) error {
|
|
|
|
// TODO(vmarmol): If this gets too long, send it over a pipe to the child.
|
|
|
|
// Marshall the container into JSON since it won't be available in the namespace.
|
|
|
|
containerJson, err := json.Marshal(container)
|
2014-02-20 03:53:25 +00:00
|
|
|
if err != nil {
|
2014-05-24 01:06:14 +00:00
|
|
|
return err
|
2014-02-20 03:53:25 +00:00
|
|
|
}
|
2014-05-24 01:06:14 +00:00
|
|
|
|
|
|
|
// TODO(vmarmol): Move this to the container JSON.
|
2014-03-18 20:49:16 +00:00
|
|
|
processLabel, err := label.GetPidCon(nspid)
|
|
|
|
if err != nil {
|
2014-05-24 01:06:14 +00:00
|
|
|
return err
|
2014-02-20 03:53:25 +00:00
|
|
|
}
|
|
|
|
|
2014-05-24 01:06:14 +00:00
|
|
|
// Enter the namespace and then finish setup
|
|
|
|
finalArgs := []string{os.Args[0], "nsenter", strconv.Itoa(nspid), processLabel, string(containerJson)}
|
|
|
|
finalArgs = append(finalArgs, args...)
|
2014-06-05 00:54:00 +00:00
|
|
|
if err := system.Execv(finalArgs[0], finalArgs[0:], os.Environ()); err != nil {
|
2014-05-24 01:06:14 +00:00
|
|
|
return err
|
2014-02-20 03:53:25 +00:00
|
|
|
}
|
2014-05-24 01:06:14 +00:00
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
|
|
|
|
// NsEnter is run after entering the namespace.
|
|
|
|
func NsEnter(container *libcontainer.Container, processLabel string, nspid int, args []string) error {
|
2014-06-05 00:54:00 +00:00
|
|
|
// clear the current processes env and replace it with the environment
|
|
|
|
// defined on the container
|
|
|
|
if err := LoadContainerEnvironment(container); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-05-01 00:18:07 +00:00
|
|
|
if err := FinalizeNamespace(container); err != nil {
|
2014-05-24 01:06:14 +00:00
|
|
|
return err
|
2014-02-20 03:53:25 +00:00
|
|
|
}
|
2014-05-24 01:06:14 +00:00
|
|
|
if err := label.SetProcessLabel(processLabel); err != nil {
|
|
|
|
return err
|
2014-03-18 20:49:16 +00:00
|
|
|
}
|
2014-06-05 00:54:00 +00:00
|
|
|
if err := system.Execv(args[0], args[0:], container.Env); err != nil {
|
2014-05-24 01:06:14 +00:00
|
|
|
return err
|
2014-02-20 03:53:25 +00:00
|
|
|
}
|
|
|
|
panic("unreachable")
|
|
|
|
}
|