driver.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // +build linux
  2. package native
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "os"
  8. "os/exec"
  9. "path/filepath"
  10. "strings"
  11. "sync"
  12. "syscall"
  13. "github.com/docker/libcontainer"
  14. "github.com/docker/libcontainer/apparmor"
  15. "github.com/docker/libcontainer/cgroups/fs"
  16. "github.com/docker/libcontainer/cgroups/systemd"
  17. "github.com/docker/libcontainer/namespaces"
  18. "github.com/docker/libcontainer/syncpipe"
  19. "github.com/dotcloud/docker/daemon/execdriver"
  20. "github.com/dotcloud/docker/pkg/system"
  21. )
  22. const (
  23. DriverName = "native"
  24. Version = "0.2"
  25. )
  26. func init() {
  27. execdriver.RegisterInitFunc(DriverName, func(args *execdriver.InitArgs) error {
  28. var container *libcontainer.Config
  29. f, err := os.Open(filepath.Join(args.Root, "container.json"))
  30. if err != nil {
  31. return err
  32. }
  33. if err := json.NewDecoder(f).Decode(&container); err != nil {
  34. f.Close()
  35. return err
  36. }
  37. f.Close()
  38. rootfs, err := os.Getwd()
  39. if err != nil {
  40. return err
  41. }
  42. syncPipe, err := syncpipe.NewSyncPipeFromFd(0, uintptr(args.Pipe))
  43. if err != nil {
  44. return err
  45. }
  46. if err := namespaces.Init(container, rootfs, args.Console, syncPipe, args.Args); err != nil {
  47. return err
  48. }
  49. return nil
  50. })
  51. }
  52. type activeContainer struct {
  53. container *libcontainer.Config
  54. cmd *exec.Cmd
  55. }
  56. type driver struct {
  57. root string
  58. initPath string
  59. activeContainers map[string]*activeContainer
  60. sync.Mutex
  61. }
  62. func NewDriver(root, initPath string) (*driver, error) {
  63. if err := os.MkdirAll(root, 0700); err != nil {
  64. return nil, err
  65. }
  66. // native driver root is at docker_root/execdriver/native. Put apparmor at docker_root
  67. if err := apparmor.InstallDefaultProfile(); err != nil {
  68. return nil, err
  69. }
  70. return &driver{
  71. root: root,
  72. initPath: initPath,
  73. activeContainers: make(map[string]*activeContainer),
  74. }, nil
  75. }
  76. func (d *driver) Run(c *execdriver.Command, pipes *execdriver.Pipes, startCallback execdriver.StartCallback) (int, error) {
  77. // take the Command and populate the libcontainer.Config from it
  78. container, err := d.createContainer(c)
  79. if err != nil {
  80. return -1, err
  81. }
  82. if err := execdriver.SetTerminal(c, pipes); err != nil {
  83. return -1, err
  84. }
  85. d.Lock()
  86. d.activeContainers[c.ID] = &activeContainer{
  87. container: container,
  88. cmd: &c.Cmd,
  89. }
  90. d.Unlock()
  91. var (
  92. dataPath = filepath.Join(d.root, c.ID)
  93. args = append([]string{c.Entrypoint}, c.Arguments...)
  94. )
  95. if err := d.createContainerRoot(c.ID); err != nil {
  96. return -1, err
  97. }
  98. defer d.removeContainerRoot(c.ID)
  99. if err := d.writeContainerFile(container, c.ID); err != nil {
  100. return -1, err
  101. }
  102. return namespaces.Exec(container, c.Stdin, c.Stdout, c.Stderr, c.Console, c.Rootfs, dataPath, args, func(container *libcontainer.Config, console, rootfs, dataPath, init string, child *os.File, args []string) *exec.Cmd {
  103. // we need to join the rootfs because namespaces will setup the rootfs and chroot
  104. initPath := filepath.Join(c.Rootfs, c.InitPath)
  105. c.Path = d.initPath
  106. c.Args = append([]string{
  107. initPath,
  108. "-driver", DriverName,
  109. "-console", console,
  110. "-pipe", "3",
  111. "-root", filepath.Join(d.root, c.ID),
  112. "--",
  113. }, args...)
  114. // set this to nil so that when we set the clone flags anything else is reset
  115. c.SysProcAttr = nil
  116. system.SetCloneFlags(&c.Cmd, uintptr(namespaces.GetNamespaceFlags(container.Namespaces)))
  117. c.ExtraFiles = []*os.File{child}
  118. c.Env = container.Env
  119. c.Dir = c.Rootfs
  120. return &c.Cmd
  121. }, func() {
  122. if startCallback != nil {
  123. c.ContainerPid = c.Process.Pid
  124. startCallback(c)
  125. }
  126. })
  127. }
  128. func (d *driver) Kill(p *execdriver.Command, sig int) error {
  129. return syscall.Kill(p.Process.Pid, syscall.Signal(sig))
  130. }
  131. func (d *driver) Pause(c *execdriver.Command) error {
  132. active := d.activeContainers[c.ID]
  133. if active == nil {
  134. return fmt.Errorf("active container for %s does not exist", c.ID)
  135. }
  136. active.container.Cgroups.Freezer = "FROZEN"
  137. if systemd.UseSystemd() {
  138. return systemd.Freeze(active.container.Cgroups, active.container.Cgroups.Freezer)
  139. }
  140. return fs.Freeze(active.container.Cgroups, active.container.Cgroups.Freezer)
  141. }
  142. func (d *driver) Unpause(c *execdriver.Command) error {
  143. active := d.activeContainers[c.ID]
  144. if active == nil {
  145. return fmt.Errorf("active container for %s does not exist", c.ID)
  146. }
  147. active.container.Cgroups.Freezer = "THAWED"
  148. if systemd.UseSystemd() {
  149. return systemd.Freeze(active.container.Cgroups, active.container.Cgroups.Freezer)
  150. }
  151. return fs.Freeze(active.container.Cgroups, active.container.Cgroups.Freezer)
  152. }
  153. func (d *driver) Terminate(p *execdriver.Command) error {
  154. // lets check the start time for the process
  155. state, err := libcontainer.GetState(filepath.Join(d.root, p.ID))
  156. if err != nil {
  157. if !os.IsNotExist(err) {
  158. return err
  159. }
  160. // TODO: Remove this part for version 1.2.0
  161. // This is added only to ensure smooth upgrades from pre 1.1.0 to 1.1.0
  162. data, err := ioutil.ReadFile(filepath.Join(d.root, p.ID, "start"))
  163. if err != nil {
  164. // if we don't have the data on disk then we can assume the process is gone
  165. // because this is only removed after we know the process has stopped
  166. if os.IsNotExist(err) {
  167. return nil
  168. }
  169. return err
  170. }
  171. state = &libcontainer.State{InitStartTime: string(data)}
  172. }
  173. currentStartTime, err := system.GetProcessStartTime(p.Process.Pid)
  174. if err != nil {
  175. return err
  176. }
  177. if state.InitStartTime == currentStartTime {
  178. err = syscall.Kill(p.Process.Pid, 9)
  179. syscall.Wait4(p.Process.Pid, nil, 0, nil)
  180. }
  181. d.removeContainerRoot(p.ID)
  182. return err
  183. }
  184. func (d *driver) Info(id string) execdriver.Info {
  185. return &info{
  186. ID: id,
  187. driver: d,
  188. }
  189. }
  190. func (d *driver) Name() string {
  191. return fmt.Sprintf("%s-%s", DriverName, Version)
  192. }
  193. func (d *driver) GetPidsForContainer(id string) ([]int, error) {
  194. d.Lock()
  195. active := d.activeContainers[id]
  196. d.Unlock()
  197. if active == nil {
  198. return nil, fmt.Errorf("active container for %s does not exist", id)
  199. }
  200. c := active.container.Cgroups
  201. if systemd.UseSystemd() {
  202. return systemd.GetPids(c)
  203. }
  204. return fs.GetPids(c)
  205. }
  206. func (d *driver) writeContainerFile(container *libcontainer.Config, id string) error {
  207. data, err := json.Marshal(container)
  208. if err != nil {
  209. return err
  210. }
  211. return ioutil.WriteFile(filepath.Join(d.root, id, "container.json"), data, 0655)
  212. }
  213. func (d *driver) createContainerRoot(id string) error {
  214. return os.MkdirAll(filepath.Join(d.root, id), 0655)
  215. }
  216. func (d *driver) removeContainerRoot(id string) error {
  217. d.Lock()
  218. delete(d.activeContainers, id)
  219. d.Unlock()
  220. return os.RemoveAll(filepath.Join(d.root, id))
  221. }
  222. func getEnv(key string, env []string) string {
  223. for _, pair := range env {
  224. parts := strings.Split(pair, "=")
  225. if parts[0] == key {
  226. return parts[1]
  227. }
  228. }
  229. return ""
  230. }