execin.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // +build linux
  2. package nsinit
  3. import (
  4. "fmt"
  5. "github.com/dotcloud/docker/pkg/libcontainer"
  6. "github.com/dotcloud/docker/pkg/libcontainer/capabilities"
  7. "github.com/dotcloud/docker/pkg/system"
  8. "os"
  9. "path/filepath"
  10. "strconv"
  11. "syscall"
  12. )
  13. // ExecIn uses an existing pid and joins the pid's namespaces with the new command.
  14. func (ns *linuxNs) ExecIn(container *libcontainer.Container, nspid int, args []string) (int, error) {
  15. for _, ns := range container.Namespaces {
  16. if err := system.Unshare(ns.Value); err != nil {
  17. return -1, err
  18. }
  19. }
  20. fds, err := ns.getNsFds(nspid, container)
  21. closeFds := func() {
  22. for _, f := range fds {
  23. system.Closefd(f)
  24. }
  25. }
  26. if err != nil {
  27. closeFds()
  28. return -1, err
  29. }
  30. // foreach namespace fd, use setns to join an existing container's namespaces
  31. for _, fd := range fds {
  32. if fd > 0 {
  33. if err := system.Setns(fd, 0); err != nil {
  34. closeFds()
  35. return -1, fmt.Errorf("setns %s", err)
  36. }
  37. }
  38. system.Closefd(fd)
  39. }
  40. // if the container has a new pid and mount namespace we need to
  41. // remount proc and sys to pick up the changes
  42. if container.Namespaces.Contains("NEWNS") && container.Namespaces.Contains("NEWPID") {
  43. pid, err := system.Fork()
  44. if err != nil {
  45. return -1, err
  46. }
  47. if pid == 0 {
  48. // TODO: make all raw syscalls to be fork safe
  49. if err := system.Unshare(syscall.CLONE_NEWNS); err != nil {
  50. return -1, err
  51. }
  52. if err := remountProc(); err != nil {
  53. return -1, fmt.Errorf("remount proc %s", err)
  54. }
  55. if err := remountSys(); err != nil {
  56. return -1, fmt.Errorf("remount sys %s", err)
  57. }
  58. goto dropAndExec
  59. }
  60. proc, err := os.FindProcess(pid)
  61. if err != nil {
  62. return -1, err
  63. }
  64. state, err := proc.Wait()
  65. if err != nil {
  66. return -1, err
  67. }
  68. os.Exit(state.Sys().(syscall.WaitStatus).ExitStatus())
  69. }
  70. dropAndExec:
  71. if err := capabilities.DropCapabilities(container); err != nil {
  72. return -1, fmt.Errorf("drop capabilities %s", err)
  73. }
  74. if err := system.Execv(args[0], args[0:], container.Env); err != nil {
  75. return -1, err
  76. }
  77. panic("unreachable")
  78. }
  79. func (ns *linuxNs) getNsFds(pid int, container *libcontainer.Container) ([]uintptr, error) {
  80. fds := make([]uintptr, len(container.Namespaces))
  81. for i, ns := range container.Namespaces {
  82. f, err := os.OpenFile(filepath.Join("/proc/", strconv.Itoa(pid), "ns", ns.File), os.O_RDONLY, 0)
  83. if err != nil {
  84. return fds, err
  85. }
  86. fds[i] = f.Fd()
  87. }
  88. return fds, nil
  89. }