execin.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // +build linux
  2. package nsinit
  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 _, nsv := range container.Namespaces {
  22. // skip the PID namespace on unshare because it it not supported
  23. if nsv.Enabled && nsv.Key != "NEWPID" {
  24. if err := system.Unshare(nsv.Value); err != nil {
  25. return -1, err
  26. }
  27. }
  28. }
  29. fds, err := getNsFds(nspid, container)
  30. closeFds := func() {
  31. for _, f := range fds {
  32. system.Closefd(f)
  33. }
  34. }
  35. if err != nil {
  36. closeFds()
  37. return -1, err
  38. }
  39. processLabel, err := label.GetPidCon(nspid)
  40. if err != nil {
  41. closeFds()
  42. return -1, err
  43. }
  44. // foreach namespace fd, use setns to join an existing container's namespaces
  45. for _, fd := range fds {
  46. if fd > 0 {
  47. if err := system.Setns(fd, 0); err != nil {
  48. closeFds()
  49. return -1, fmt.Errorf("setns %s", err)
  50. }
  51. }
  52. system.Closefd(fd)
  53. }
  54. // if the container has a new pid and mount namespace we need to
  55. // remount proc and sys to pick up the changes
  56. if container.Namespaces.Contains("NEWNS") && container.Namespaces.Contains("NEWPID") {
  57. pid, err := system.Fork()
  58. if err != nil {
  59. return -1, err
  60. }
  61. if pid == 0 {
  62. // TODO: make all raw syscalls to be fork safe
  63. if err := system.Unshare(syscall.CLONE_NEWNS); err != nil {
  64. return -1, err
  65. }
  66. if err := mount.RemountProc(); err != nil {
  67. return -1, fmt.Errorf("remount proc %s", err)
  68. }
  69. if err := mount.RemountSys(); err != nil {
  70. return -1, fmt.Errorf("remount sys %s", err)
  71. }
  72. goto dropAndExec
  73. }
  74. proc, err := os.FindProcess(pid)
  75. if err != nil {
  76. return -1, err
  77. }
  78. state, err := proc.Wait()
  79. if err != nil {
  80. return -1, err
  81. }
  82. os.Exit(state.Sys().(syscall.WaitStatus).ExitStatus())
  83. }
  84. dropAndExec:
  85. if err := FinalizeNamespace(container); err != nil {
  86. return -1, err
  87. }
  88. err = label.SetProcessLabel(processLabel)
  89. if err != nil {
  90. return -1, err
  91. }
  92. if err := system.Execv(args[0], args[0:], container.Env); err != nil {
  93. return -1, err
  94. }
  95. panic("unreachable")
  96. }
  97. func getNsFds(pid int, container *libcontainer.Container) ([]uintptr, error) {
  98. fds := make([]uintptr, len(container.Namespaces))
  99. for i, ns := range container.Namespaces {
  100. f, err := os.OpenFile(filepath.Join("/proc/", strconv.Itoa(pid), "ns", ns.File), os.O_RDONLY, 0)
  101. if err != nil {
  102. return fds, err
  103. }
  104. fds[i] = f.Fd()
  105. }
  106. return fds, nil
  107. }