command.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package nsinit
  2. import (
  3. "fmt"
  4. "github.com/dotcloud/docker/pkg/libcontainer"
  5. "github.com/dotcloud/docker/pkg/system"
  6. "os"
  7. "os/exec"
  8. )
  9. // CommandFactory takes the container's configuration and options passed by the
  10. // parent processes and creates an *exec.Cmd that will be used to fork/exec the
  11. // namespaced init process
  12. type CommandFactory interface {
  13. Create(container *libcontainer.Container, console string, syncFd uintptr, args []string) *exec.Cmd
  14. }
  15. type DefaultCommandFactory struct {
  16. Root string
  17. }
  18. // Create will return an exec.Cmd with the Cloneflags set to the proper namespaces
  19. // defined on the container's configuration and use the current binary as the init with the
  20. // args provided
  21. func (c *DefaultCommandFactory) Create(container *libcontainer.Container, console string, pipe uintptr, args []string) *exec.Cmd {
  22. // get our binary name from arg0 so we can always reexec ourself
  23. command := exec.Command(os.Args[0], append([]string{
  24. "-console", console,
  25. "-pipe", fmt.Sprint(pipe),
  26. "-root", c.Root,
  27. "init"}, args...)...)
  28. system.SetCloneFlags(command, uintptr(GetNamespaceFlags(container.Namespaces)))
  29. command.Env = container.Env
  30. return command
  31. }
  32. // GetNamespaceFlags parses the container's Namespaces options to set the correct
  33. // flags on clone, unshare, and setns
  34. func GetNamespaceFlags(namespaces libcontainer.Namespaces) (flag int) {
  35. for _, ns := range namespaces {
  36. flag |= ns.Value
  37. }
  38. return flag
  39. }