execin.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // +build linux
  2. package namespaces
  3. import (
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "strconv"
  8. "syscall"
  9. "github.com/dotcloud/docker/pkg/label"
  10. "github.com/dotcloud/docker/pkg/libcontainer"
  11. "github.com/dotcloud/docker/pkg/libcontainer/mount"
  12. "github.com/dotcloud/docker/pkg/system"
  13. )
  14. // ExecIn uses an existing pid and joins the pid's namespaces with the new command.
  15. func ExecIn(container *libcontainer.Container, nspid int, args []string) (int, error) {
  16. // clear the current processes env and replace it with the environment
  17. // defined on the container
  18. if err := LoadContainerEnvironment(container); err != nil {
  19. return -1, err
  20. }
  21. for key, enabled := range container.Namespaces {
  22. // skip the PID namespace on unshare because it it not supported
  23. if enabled && key != "NEWPID" {
  24. if ns := libcontainer.GetNamespace(key); ns != nil {
  25. if err := system.Unshare(ns.Value); err != nil {
  26. return -1, err
  27. }
  28. }
  29. }
  30. }
  31. fds, err := getNsFds(nspid, container)
  32. closeFds := func() {
  33. for _, f := range fds {
  34. system.Closefd(f)
  35. }
  36. }
  37. if err != nil {
  38. closeFds()
  39. return -1, err
  40. }
  41. processLabel, err := label.GetPidCon(nspid)
  42. if err != nil {
  43. closeFds()
  44. return -1, err
  45. }
  46. // foreach namespace fd, use setns to join an existing container's namespaces
  47. for _, fd := range fds {
  48. if fd > 0 {
  49. if err := system.Setns(fd, 0); err != nil {
  50. closeFds()
  51. return -1, fmt.Errorf("setns %s", err)
  52. }
  53. }
  54. system.Closefd(fd)
  55. }
  56. // if the container has a new pid and mount namespace we need to
  57. // remount proc and sys to pick up the changes
  58. if container.Namespaces["NEWNS"] && container.Namespaces["NEWPID"] {
  59. pid, err := system.Fork()
  60. if err != nil {
  61. return -1, err
  62. }
  63. if pid == 0 {
  64. // TODO: make all raw syscalls to be fork safe
  65. if err := system.Unshare(syscall.CLONE_NEWNS); err != nil {
  66. return -1, err
  67. }
  68. if err := mount.RemountProc(); err != nil {
  69. return -1, fmt.Errorf("remount proc %s", err)
  70. }
  71. if err := mount.RemountSys(); err != nil {
  72. return -1, fmt.Errorf("remount sys %s", err)
  73. }
  74. goto dropAndExec
  75. }
  76. proc, err := os.FindProcess(pid)
  77. if err != nil {
  78. return -1, err
  79. }
  80. state, err := proc.Wait()
  81. if err != nil {
  82. return -1, err
  83. }
  84. os.Exit(state.Sys().(syscall.WaitStatus).ExitStatus())
  85. }
  86. dropAndExec:
  87. if err := FinalizeNamespace(container); err != nil {
  88. return -1, err
  89. }
  90. err = label.SetProcessLabel(processLabel)
  91. if err != nil {
  92. return -1, err
  93. }
  94. if err := system.Execv(args[0], args[0:], container.Env); err != nil {
  95. return -1, err
  96. }
  97. panic("unreachable")
  98. }
  99. func getNsFds(pid int, container *libcontainer.Container) ([]uintptr, error) {
  100. fds := []uintptr{}
  101. for key, enabled := range container.Namespaces {
  102. if enabled {
  103. if ns := libcontainer.GetNamespace(key); ns != nil {
  104. f, err := os.OpenFile(filepath.Join("/proc/", strconv.Itoa(pid), "ns", ns.File), os.O_RDONLY, 0)
  105. if err != nil {
  106. return fds, err
  107. }
  108. fds = append(fds, f.Fd())
  109. }
  110. }
  111. }
  112. return fds, nil
  113. }