2014-02-26 07:19:13 +08:00
|
|
|
// +build linux
|
|
|
|
|
2014-06-05 06:47:57 +08:00
|
|
|
package namespaces
|
2014-02-20 11:53:25 +08:00
|
|
|
|
|
|
|
import (
|
2014-05-24 09:06:14 +08:00
|
|
|
"encoding/json"
|
2014-02-20 11:53:25 +08:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2014-05-01 06:24:18 +08:00
|
|
|
|
2014-06-10 23:14:16 +08:00
|
|
|
"github.com/docker/libcontainer"
|
|
|
|
"github.com/docker/libcontainer/label"
|
2014-05-01 06:24:18 +08:00
|
|
|
"github.com/dotcloud/docker/pkg/system"
|
2014-02-20 11:53:25 +08:00
|
|
|
)
|
|
|
|
|
2014-02-21 10:27:42 +08:00
|
|
|
// ExecIn uses an existing pid and joins the pid's namespaces with the new command.
|
2014-06-25 07:54:50 +08:00
|
|
|
func ExecIn(container *libcontainer.Config, state *libcontainer.State, args []string) error {
|
2014-05-24 09:06:14 +08:00
|
|
|
// 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 11:53:25 +08:00
|
|
|
if err != nil {
|
2014-05-24 09:06:14 +08:00
|
|
|
return err
|
2014-02-20 11:53:25 +08:00
|
|
|
}
|
2014-05-24 09:06:14 +08:00
|
|
|
|
|
|
|
// Enter the namespace and then finish setup
|
2014-06-25 07:54:50 +08:00
|
|
|
finalArgs := []string{os.Args[0], "nsenter", "--nspid", strconv.Itoa(state.Pid1), "--containerjson", string(containerJson), "--"}
|
2014-05-24 09:06:14 +08:00
|
|
|
finalArgs = append(finalArgs, args...)
|
2014-06-05 08:54:00 +08:00
|
|
|
if err := system.Execv(finalArgs[0], finalArgs[0:], os.Environ()); err != nil {
|
2014-05-24 09:06:14 +08:00
|
|
|
return err
|
2014-02-20 11:53:25 +08:00
|
|
|
}
|
2014-05-24 09:06:14 +08:00
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
|
|
|
|
// NsEnter is run after entering the namespace.
|
2014-06-24 07:54:35 +08:00
|
|
|
func NsEnter(container *libcontainer.Config, nspid int, args []string) error {
|
2014-06-05 08:54:00 +08: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 08:18:07 +08:00
|
|
|
if err := FinalizeNamespace(container); err != nil {
|
2014-05-24 09:06:14 +08:00
|
|
|
return err
|
2014-02-20 11:53:25 +08:00
|
|
|
}
|
2014-06-17 06:28:28 +08:00
|
|
|
|
2014-06-25 08:31:03 +08:00
|
|
|
if container.ProcessLabel != "" {
|
|
|
|
if err := label.SetProcessLabel(container.ProcessLabel); err != nil {
|
2014-06-17 06:28:28 +08:00
|
|
|
return err
|
|
|
|
}
|
2014-03-19 04:49:16 +08:00
|
|
|
}
|
2014-06-17 06:28:28 +08:00
|
|
|
|
2014-06-05 08:54:00 +08:00
|
|
|
if err := system.Execv(args[0], args[0:], container.Env); err != nil {
|
2014-05-24 09:06:14 +08:00
|
|
|
return err
|
2014-02-20 11:53:25 +08:00
|
|
|
}
|
|
|
|
panic("unreachable")
|
|
|
|
}
|