|
@@ -3,70 +3,88 @@
|
|
|
package namespaces
|
|
|
|
|
|
import (
|
|
|
- "encoding/json"
|
|
|
+ "io"
|
|
|
"os"
|
|
|
+ "os/exec"
|
|
|
+ "path/filepath"
|
|
|
"strconv"
|
|
|
+ "syscall"
|
|
|
|
|
|
"github.com/docker/libcontainer"
|
|
|
"github.com/docker/libcontainer/label"
|
|
|
+ "github.com/docker/libcontainer/syncpipe"
|
|
|
"github.com/docker/libcontainer/system"
|
|
|
)
|
|
|
|
|
|
-// ExecIn uses an existing pid and joins the pid's namespaces with the new command.
|
|
|
-func ExecIn(container *libcontainer.Config, state *libcontainer.State, args []string) error {
|
|
|
- // Enter the namespace and then finish setup
|
|
|
- args, err := GetNsEnterCommand(strconv.Itoa(state.InitPid), container, "", args)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
+// ExecIn reexec's the initPath with the argv 0 rewrite to "nsenter" so that it is able to run the
|
|
|
+// setns code in a single threaded environment joining the existing containers' namespaces.
|
|
|
+func ExecIn(container *libcontainer.Config, state *libcontainer.State, userArgs []string, initPath string,
|
|
|
+ stdin io.Reader, stdout, stderr io.Writer, console string, startCallback func(*exec.Cmd)) (int, error) {
|
|
|
|
|
|
- finalArgs := append([]string{os.Args[0]}, args...)
|
|
|
+ args := []string{"nsenter", "--nspid", strconv.Itoa(state.InitPid)}
|
|
|
|
|
|
- if err := system.Execv(finalArgs[0], finalArgs[0:], os.Environ()); err != nil {
|
|
|
- return err
|
|
|
+ if console != "" {
|
|
|
+ args = append(args, "--console", console)
|
|
|
}
|
|
|
|
|
|
- panic("unreachable")
|
|
|
-}
|
|
|
+ cmd := &exec.Cmd{
|
|
|
+ Path: initPath,
|
|
|
+ Args: append(args, append([]string{"--"}, userArgs...)...),
|
|
|
+ }
|
|
|
|
|
|
-func getContainerJson(container *libcontainer.Config) (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)
|
|
|
- if err != nil {
|
|
|
- return "", err
|
|
|
+ if filepath.Base(initPath) == initPath {
|
|
|
+ if lp, err := exec.LookPath(initPath); err == nil {
|
|
|
+ cmd.Path = lp
|
|
|
+ }
|
|
|
}
|
|
|
- return string(containerJson), nil
|
|
|
-}
|
|
|
|
|
|
-func GetNsEnterCommand(initPid string, container *libcontainer.Config, console string, args []string) ([]string, error) {
|
|
|
- containerJson, err := getContainerJson(container)
|
|
|
+ pipe, err := syncpipe.NewSyncPipe()
|
|
|
if err != nil {
|
|
|
- return nil, err
|
|
|
+ return -1, err
|
|
|
}
|
|
|
+ defer pipe.Close()
|
|
|
|
|
|
- out := []string{
|
|
|
- "--nspid", initPid,
|
|
|
- "--containerjson", containerJson,
|
|
|
+ // Note: these are only used in non-tty mode
|
|
|
+ // if there is a tty for the container it will be opened within the namespace and the
|
|
|
+ // fds will be duped to stdin, stdiout, and stderr
|
|
|
+ cmd.Stdin = stdin
|
|
|
+ cmd.Stdout = stdout
|
|
|
+ cmd.Stderr = stderr
|
|
|
+
|
|
|
+ cmd.ExtraFiles = []*os.File{pipe.Child()}
|
|
|
+
|
|
|
+ if err := cmd.Start(); err != nil {
|
|
|
+ return -1, err
|
|
|
}
|
|
|
+ pipe.CloseChild()
|
|
|
|
|
|
- if console != "" {
|
|
|
- out = append(out, "--console", console)
|
|
|
+ if err := pipe.SendToChild(container); err != nil {
|
|
|
+ cmd.Process.Kill()
|
|
|
+ cmd.Wait()
|
|
|
+ return -1, err
|
|
|
+ }
|
|
|
+
|
|
|
+ if startCallback != nil {
|
|
|
+ startCallback(cmd)
|
|
|
}
|
|
|
- out = append(out, "nsenter")
|
|
|
- out = append(out, "--")
|
|
|
- out = append(out, args...)
|
|
|
|
|
|
- return out, nil
|
|
|
+ if err := cmd.Wait(); err != nil {
|
|
|
+ if _, ok := err.(*exec.ExitError); !ok {
|
|
|
+ return -1, err
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return cmd.ProcessState.Sys().(syscall.WaitStatus).ExitStatus(), nil
|
|
|
}
|
|
|
|
|
|
-// Run a command in a container after entering the namespace.
|
|
|
-func NsEnter(container *libcontainer.Config, args []string) error {
|
|
|
- // clear the current processes env and replace it with the environment
|
|
|
- // defined on the container
|
|
|
+// Finalize expects that the setns calls have been setup and that is has joined an
|
|
|
+// existing namespace
|
|
|
+func FinalizeSetns(container *libcontainer.Config, args []string) error {
|
|
|
+ // clear the current processes env and replace it with the environment defined on the container
|
|
|
if err := LoadContainerEnvironment(container); err != nil {
|
|
|
return err
|
|
|
}
|
|
|
+
|
|
|
if err := FinalizeNamespace(container); err != nil {
|
|
|
return err
|
|
|
}
|
|
@@ -80,5 +98,6 @@ func NsEnter(container *libcontainer.Config, args []string) error {
|
|
|
if err := system.Execv(args[0], args[0:], container.Env); err != nil {
|
|
|
return err
|
|
|
}
|
|
|
+
|
|
|
panic("unreachable")
|
|
|
}
|