driver.go 6.6 KB

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