execin.go 2.6 KB

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