2014-02-20 08:50:10 +08:00
|
|
|
// +build linux
|
|
|
|
|
2014-06-05 06:47:57 +08:00
|
|
|
package namespaces
|
2014-02-19 08:56:11 +08:00
|
|
|
|
|
|
|
import (
|
2014-11-04 10:18:55 +08:00
|
|
|
"encoding/json"
|
2014-07-16 07:26:28 +08:00
|
|
|
"io"
|
2014-02-19 08:56:11 +08:00
|
|
|
"os"
|
2014-02-19 09:52:06 +08:00
|
|
|
"os/exec"
|
2014-02-19 08:56:11 +08:00
|
|
|
"syscall"
|
2014-03-04 13:47:03 +08:00
|
|
|
|
2014-06-10 23:14:16 +08:00
|
|
|
"github.com/docker/libcontainer"
|
|
|
|
"github.com/docker/libcontainer/cgroups"
|
|
|
|
"github.com/docker/libcontainer/cgroups/fs"
|
|
|
|
"github.com/docker/libcontainer/cgroups/systemd"
|
|
|
|
"github.com/docker/libcontainer/network"
|
2014-07-15 07:55:49 +08:00
|
|
|
"github.com/docker/libcontainer/system"
|
2014-02-19 08:56:11 +08:00
|
|
|
)
|
|
|
|
|
2014-06-20 07:36:39 +08:00
|
|
|
// TODO(vishh): This is part of the libcontainer API and it does much more than just namespaces related work.
|
|
|
|
// Move this to libcontainer package.
|
2014-06-24 20:30:27 +08:00
|
|
|
// Exec performs setup outside of a namespace so that a container can be
|
2014-02-21 10:27:42 +08:00
|
|
|
// executed. Exec is a high level function for working with container namespaces.
|
2014-09-24 05:04:55 +08:00
|
|
|
func Exec(container *libcontainer.Config, stdin io.Reader, stdout, stderr io.Writer, console, dataPath string, args []string, createCommand CreateCommand, startCallback func()) (int, error) {
|
2014-11-04 10:18:55 +08:00
|
|
|
var err error
|
2014-02-21 10:05:40 +08:00
|
|
|
|
2014-02-20 11:14:31 +08:00
|
|
|
// create a pipe so that we can syncronize with the namespaced process and
|
2014-11-04 10:18:55 +08:00
|
|
|
// pass the state and configuration to the child process
|
|
|
|
parent, child, err := newInitPipe()
|
2014-02-20 07:33:44 +08:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2014-11-04 10:18:55 +08:00
|
|
|
defer parent.Close()
|
2014-02-21 09:58:13 +08:00
|
|
|
|
2014-11-04 10:18:55 +08:00
|
|
|
command := createCommand(container, console, dataPath, os.Args[0], child, args)
|
2014-07-16 09:24:15 +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
|
2014-07-16 07:26:28 +08:00
|
|
|
command.Stdin = stdin
|
|
|
|
command.Stdout = stdout
|
|
|
|
command.Stderr = stderr
|
2014-02-21 10:05:40 +08:00
|
|
|
|
2014-02-19 09:52:06 +08:00
|
|
|
if err := command.Start(); err != nil {
|
2014-11-04 10:18:55 +08:00
|
|
|
child.Close()
|
2014-02-19 09:52:06 +08:00
|
|
|
return -1, err
|
2014-02-19 08:56:11 +08:00
|
|
|
}
|
2014-11-04 10:18:55 +08:00
|
|
|
child.Close()
|
2014-03-26 14:48:16 +08:00
|
|
|
|
2014-11-04 10:18:55 +08:00
|
|
|
terminate := func(terr error) (int, error) {
|
|
|
|
// TODO: log the errors for kill and wait
|
|
|
|
command.Process.Kill()
|
|
|
|
command.Wait()
|
|
|
|
return -1, terr
|
|
|
|
}
|
2014-06-16 21:30:42 +08:00
|
|
|
|
2014-03-26 14:48:16 +08:00
|
|
|
started, err := system.GetProcessStartTime(command.Process.Pid)
|
|
|
|
if err != nil {
|
2014-11-04 10:18:55 +08:00
|
|
|
return terminate(err)
|
2014-03-26 14:48:16 +08:00
|
|
|
}
|
2014-06-25 07:54:50 +08:00
|
|
|
|
2014-02-21 06:12:08 +08:00
|
|
|
// Do this before syncing with child so that no children
|
|
|
|
// can escape the cgroup
|
2014-08-13 14:18:55 +08:00
|
|
|
cgroupRef, err := SetupCgroups(container, command.Process.Pid)
|
2014-03-14 17:47:49 +08:00
|
|
|
if err != nil {
|
2014-11-04 10:18:55 +08:00
|
|
|
return terminate(err)
|
2014-02-21 06:12:08 +08:00
|
|
|
}
|
2014-08-14 09:00:15 +08:00
|
|
|
defer cgroupRef.Cleanup()
|
2014-08-13 14:18:55 +08:00
|
|
|
|
|
|
|
cgroupPaths, err := cgroupRef.Paths()
|
|
|
|
if err != nil {
|
2014-11-04 10:18:55 +08:00
|
|
|
return terminate(err)
|
2014-03-14 17:47:49 +08:00
|
|
|
}
|
|
|
|
|
2014-06-26 06:51:28 +08:00
|
|
|
var networkState network.NetworkState
|
2014-11-04 10:18:55 +08:00
|
|
|
if err := InitializeNetworking(container, command.Process.Pid, &networkState); err != nil {
|
|
|
|
return terminate(err)
|
|
|
|
}
|
|
|
|
// send the state to the container's init process then shutdown writes for the parent
|
|
|
|
if err := json.NewEncoder(parent).Encode(networkState); err != nil {
|
|
|
|
return terminate(err)
|
|
|
|
}
|
|
|
|
// shutdown writes for the parent side of the pipe
|
|
|
|
if err := syscall.Shutdown(int(parent.Fd()), syscall.SHUT_WR); err != nil {
|
|
|
|
return terminate(err)
|
2014-02-20 07:33:44 +08:00
|
|
|
}
|
|
|
|
|
2014-06-25 07:43:00 +08:00
|
|
|
state := &libcontainer.State{
|
|
|
|
InitPid: command.Process.Pid,
|
|
|
|
InitStartTime: started,
|
2014-06-26 06:51:28 +08:00
|
|
|
NetworkState: networkState,
|
2014-08-13 14:18:55 +08:00
|
|
|
CgroupPaths: cgroupPaths,
|
2014-06-25 07:43:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := libcontainer.SaveState(dataPath, state); err != nil {
|
2014-11-04 10:18:55 +08:00
|
|
|
return terminate(err)
|
2014-06-25 05:34:09 +08:00
|
|
|
}
|
2014-06-25 07:43:00 +08:00
|
|
|
defer libcontainer.DeleteState(dataPath)
|
2014-06-25 05:34:09 +08:00
|
|
|
|
2014-11-04 10:18:55 +08:00
|
|
|
// wait for the child process to fully complete and receive an error message
|
|
|
|
// if one was encoutered
|
|
|
|
var ierr *initError
|
|
|
|
if err := json.NewDecoder(parent).Decode(&ierr); err != nil && err != io.EOF {
|
|
|
|
return terminate(err)
|
|
|
|
}
|
|
|
|
if ierr != nil {
|
|
|
|
return terminate(ierr)
|
2014-06-16 21:30:42 +08:00
|
|
|
}
|
2014-02-21 06:12:08 +08:00
|
|
|
|
2014-05-01 06:52:40 +08:00
|
|
|
if startCallback != nil {
|
|
|
|
startCallback()
|
|
|
|
}
|
|
|
|
|
2014-02-20 06:33:25 +08:00
|
|
|
if err := command.Wait(); err != nil {
|
2014-02-20 08:40:36 +08:00
|
|
|
if _, ok := err.(*exec.ExitError); !ok {
|
|
|
|
return -1, err
|
|
|
|
}
|
2014-02-20 06:33:25 +08:00
|
|
|
}
|
2014-05-01 08:55:15 +08:00
|
|
|
return command.ProcessState.Sys().(syscall.WaitStatus).ExitStatus(), nil
|
|
|
|
}
|
|
|
|
|
2014-05-01 09:49:24 +08:00
|
|
|
// DefaultCreateCommand will return an exec.Cmd with the Cloneflags set to the proper namespaces
|
2014-05-01 08:55:15 +08:00
|
|
|
// defined on the container's configuration and use the current binary as the init with the
|
|
|
|
// args provided
|
|
|
|
//
|
|
|
|
// console: the /dev/console to setup inside the container
|
2014-06-24 20:30:27 +08:00
|
|
|
// init: the program executed inside the namespaces
|
2014-05-01 08:55:15 +08:00
|
|
|
// root: the path to the container json file and information
|
2014-06-24 20:30:27 +08:00
|
|
|
// pipe: sync pipe to synchronize the parent and child processes
|
|
|
|
// args: the arguments to pass to the container to run as the user's program
|
2014-09-24 05:04:55 +08:00
|
|
|
func DefaultCreateCommand(container *libcontainer.Config, console, dataPath, init string, pipe *os.File, args []string) *exec.Cmd {
|
2014-05-01 08:55:15 +08:00
|
|
|
// get our binary name from arg0 so we can always reexec ourself
|
|
|
|
env := []string{
|
|
|
|
"console=" + console,
|
|
|
|
"pipe=3",
|
|
|
|
"data_path=" + dataPath,
|
|
|
|
}
|
|
|
|
|
2014-08-01 00:49:11 +08:00
|
|
|
command := exec.Command(init, append([]string{"init", "--"}, args...)...)
|
2014-05-01 08:55:15 +08:00
|
|
|
// make sure the process is executed inside the context of the rootfs
|
2014-09-24 05:04:55 +08:00
|
|
|
command.Dir = container.RootFs
|
2014-05-01 08:55:15 +08:00
|
|
|
command.Env = append(os.Environ(), env...)
|
|
|
|
|
2014-07-15 07:46:51 +08:00
|
|
|
if command.SysProcAttr == nil {
|
|
|
|
command.SysProcAttr = &syscall.SysProcAttr{}
|
|
|
|
}
|
|
|
|
command.SysProcAttr.Cloneflags = uintptr(GetNamespaceFlags(container.Namespaces))
|
|
|
|
|
2014-05-14 17:50:15 +08:00
|
|
|
command.SysProcAttr.Pdeathsig = syscall.SIGKILL
|
2014-05-01 08:55:15 +08:00
|
|
|
command.ExtraFiles = []*os.File{pipe}
|
|
|
|
|
|
|
|
return command
|
2014-02-19 08:56:11 +08:00
|
|
|
}
|
|
|
|
|
2014-06-24 20:30:27 +08:00
|
|
|
// SetupCgroups applies the cgroup restrictions to the process running in the container based
|
2014-05-01 06:52:40 +08:00
|
|
|
// on the container's configuration
|
2014-06-24 07:54:35 +08:00
|
|
|
func SetupCgroups(container *libcontainer.Config, nspid int) (cgroups.ActiveCgroup, error) {
|
2014-02-22 14:37:09 +08:00
|
|
|
if container.Cgroups != nil {
|
2014-04-19 12:30:08 +08:00
|
|
|
c := container.Cgroups
|
2014-07-15 07:46:51 +08:00
|
|
|
|
2014-04-19 12:30:08 +08:00
|
|
|
if systemd.UseSystemd() {
|
|
|
|
return systemd.Apply(c, nspid)
|
|
|
|
}
|
2014-07-15 07:46:51 +08:00
|
|
|
|
2014-04-19 12:34:26 +08:00
|
|
|
return fs.Apply(c, nspid)
|
2014-02-22 14:37:09 +08:00
|
|
|
}
|
2014-07-15 07:46:51 +08:00
|
|
|
|
2014-03-14 17:47:49 +08:00
|
|
|
return nil, nil
|
2014-02-22 14:37:09 +08:00
|
|
|
}
|
|
|
|
|
2014-05-01 06:52:40 +08:00
|
|
|
// InitializeNetworking creates the container's network stack outside of the namespace and moves
|
|
|
|
// interfaces into the container's net namespaces if necessary
|
2014-11-04 10:18:55 +08:00
|
|
|
func InitializeNetworking(container *libcontainer.Config, nspid int, networkState *network.NetworkState) error {
|
2014-02-27 06:19:39 +08:00
|
|
|
for _, config := range container.Networks {
|
|
|
|
strategy, err := network.GetStrategy(config.Type)
|
2014-02-22 14:20:15 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-06-26 06:51:28 +08:00
|
|
|
if err := strategy.Create((*network.Network)(config), nspid, networkState); err != nil {
|
2014-02-22 14:20:15 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2014-11-04 10:18:55 +08:00
|
|
|
return nil
|
2014-05-01 08:18:07 +08:00
|
|
|
}
|