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-11-04 10:18:55 +08:00
|
|
|
"encoding/json"
|
2014-08-13 02:43:12 +08:00
|
|
|
"fmt"
|
2014-08-07 09:44:41 +08:00
|
|
|
"io"
|
2014-08-05 06:05:50 +08:00
|
|
|
"os"
|
2014-08-07 09:44:41 +08:00
|
|
|
"os/exec"
|
2014-08-09 02:11:23 +08:00
|
|
|
"path/filepath"
|
2014-08-05 06:05:50 +08:00
|
|
|
"strconv"
|
2014-08-07 09:44:41 +08:00
|
|
|
"syscall"
|
2014-08-05 06:05:50 +08:00
|
|
|
|
2014-07-17 07:52:15 +08:00
|
|
|
"github.com/docker/libcontainer"
|
2014-10-14 13:53:44 +08:00
|
|
|
"github.com/docker/libcontainer/apparmor"
|
2014-08-13 14:18:55 +08:00
|
|
|
"github.com/docker/libcontainer/cgroups"
|
2014-07-17 07:52:15 +08:00
|
|
|
"github.com/docker/libcontainer/label"
|
|
|
|
"github.com/docker/libcontainer/system"
|
2014-02-20 11:53:25 +08:00
|
|
|
)
|
|
|
|
|
2014-08-09 02:11:23 +08:00
|
|
|
// 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.
|
2014-08-13 02:43:12 +08:00
|
|
|
func ExecIn(container *libcontainer.Config, state *libcontainer.State, userArgs []string, initPath, action string,
|
2014-08-07 09:44:41 +08:00
|
|
|
stdin io.Reader, stdout, stderr io.Writer, console string, startCallback func(*exec.Cmd)) (int, error) {
|
2014-07-17 07:52:15 +08:00
|
|
|
|
2014-08-13 02:43:12 +08:00
|
|
|
args := []string{fmt.Sprintf("nsenter-%s", action), "--nspid", strconv.Itoa(state.InitPid)}
|
2014-07-03 06:11:15 +08:00
|
|
|
|
2014-08-07 09:44:41 +08:00
|
|
|
if console != "" {
|
|
|
|
args = append(args, "--console", console)
|
2014-02-20 11:53:25 +08:00
|
|
|
}
|
2014-07-17 07:52:15 +08:00
|
|
|
|
2014-08-09 02:11:23 +08:00
|
|
|
cmd := &exec.Cmd{
|
|
|
|
Path: initPath,
|
|
|
|
Args: append(args, append([]string{"--"}, userArgs...)...),
|
|
|
|
}
|
2014-05-24 09:06:14 +08:00
|
|
|
|
2014-08-09 02:11:23 +08:00
|
|
|
if filepath.Base(initPath) == initPath {
|
|
|
|
if lp, err := exec.LookPath(initPath); err == nil {
|
|
|
|
cmd.Path = lp
|
|
|
|
}
|
|
|
|
}
|
2014-08-07 09:44:41 +08:00
|
|
|
|
2014-11-04 10:18:55 +08:00
|
|
|
parent, child, err := newInitPipe()
|
2014-07-03 06:11:15 +08:00
|
|
|
if err != nil {
|
2014-08-07 09:44:41 +08:00
|
|
|
return -1, err
|
2014-07-03 06:11:15 +08:00
|
|
|
}
|
2014-11-04 10:18:55 +08:00
|
|
|
defer parent.Close()
|
2014-07-03 06:11:15 +08:00
|
|
|
|
2014-08-07 09:44:41 +08:00
|
|
|
// 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
|
2014-11-04 10:18:55 +08:00
|
|
|
cmd.ExtraFiles = []*os.File{child}
|
2014-08-07 09:44:41 +08:00
|
|
|
|
|
|
|
if err := cmd.Start(); err != nil {
|
2014-11-04 10:18:55 +08:00
|
|
|
child.Close()
|
2014-08-07 09:44:41 +08:00
|
|
|
return -1, err
|
2014-07-17 07:52:15 +08:00
|
|
|
}
|
2014-11-04 10:18:55 +08:00
|
|
|
child.Close()
|
|
|
|
|
|
|
|
terminate := func(terr error) (int, error) {
|
|
|
|
// TODO: log the errors for kill and wait
|
|
|
|
cmd.Process.Kill()
|
|
|
|
cmd.Wait()
|
|
|
|
return -1, terr
|
|
|
|
}
|
2014-07-17 07:52:15 +08:00
|
|
|
|
2014-08-06 06:34:59 +08:00
|
|
|
// Enter cgroups.
|
2014-08-13 14:18:55 +08:00
|
|
|
if err := EnterCgroups(state, cmd.Process.Pid); err != nil {
|
2014-11-04 10:18:55 +08:00
|
|
|
return terminate(err)
|
2014-08-06 06:34:59 +08:00
|
|
|
}
|
|
|
|
|
2014-11-04 10:18:55 +08:00
|
|
|
if err := json.NewEncoder(parent).Encode(container); err != nil {
|
|
|
|
return terminate(err)
|
2014-07-17 07:52:15 +08:00
|
|
|
}
|
|
|
|
|
2014-08-07 09:44:41 +08:00
|
|
|
if startCallback != nil {
|
|
|
|
startCallback(cmd)
|
2014-07-17 07:52:15 +08:00
|
|
|
}
|
|
|
|
|
2014-08-07 09:44:41 +08:00
|
|
|
if err := cmd.Wait(); err != nil {
|
|
|
|
if _, ok := err.(*exec.ExitError); !ok {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cmd.ProcessState.Sys().(syscall.WaitStatus).ExitStatus(), nil
|
2014-07-03 06:11:15 +08:00
|
|
|
}
|
|
|
|
|
2014-08-07 09:44:41 +08:00
|
|
|
// 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
|
2014-06-05 08:54:00 +08:00
|
|
|
if err := LoadContainerEnvironment(container); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-08-07 09:44:41 +08:00
|
|
|
|
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-10-14 13:53:44 +08:00
|
|
|
if err := apparmor.ApplyProfile(container.AppArmorProfile); err != nil {
|
|
|
|
return fmt.Errorf("set apparmor profile %s: %s", container.AppArmorProfile, err)
|
|
|
|
}
|
|
|
|
|
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-11-15 01:49:21 +08:00
|
|
|
if err := system.Execv(args[0], args[0:], os.Environ()); err != nil {
|
2014-05-24 09:06:14 +08:00
|
|
|
return err
|
2014-02-20 11:53:25 +08:00
|
|
|
}
|
2014-08-07 09:44:41 +08:00
|
|
|
|
2014-02-20 11:53:25 +08:00
|
|
|
panic("unreachable")
|
|
|
|
}
|
2014-08-06 06:12:28 +08:00
|
|
|
|
2014-08-13 14:18:55 +08:00
|
|
|
func EnterCgroups(state *libcontainer.State, pid int) error {
|
|
|
|
return cgroups.EnterPid(state.CgroupPaths, pid)
|
2014-08-06 06:12:28 +08:00
|
|
|
}
|