container_linux.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package libcontainerd
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "syscall"
  8. "github.com/Sirupsen/logrus"
  9. containerd "github.com/docker/containerd/api/grpc/types"
  10. "github.com/docker/docker/restartmanager"
  11. "github.com/opencontainers/specs/specs-go"
  12. "golang.org/x/net/context"
  13. )
  14. type container struct {
  15. containerCommon
  16. // Platform specific fields are below here.
  17. pauseMonitor
  18. oom bool
  19. }
  20. func (ctr *container) clean() error {
  21. if _, err := os.Lstat(ctr.dir); err != nil {
  22. if os.IsNotExist(err) {
  23. return nil
  24. }
  25. return err
  26. }
  27. syscall.Unmount(filepath.Join(ctr.dir, "rootfs"), syscall.MNT_DETACH) // ignore error
  28. if err := os.RemoveAll(ctr.dir); err != nil {
  29. return err
  30. }
  31. return nil
  32. }
  33. // cleanProcess removes the fifos used by an additional process.
  34. // Caller needs to lock container ID before calling this method.
  35. func (ctr *container) cleanProcess(id string) {
  36. if p, ok := ctr.processes[id]; ok {
  37. for _, i := range []int{syscall.Stdin, syscall.Stdout, syscall.Stderr} {
  38. if err := os.Remove(p.fifo(i)); err != nil {
  39. logrus.Warnf("failed to remove %v for process %v: %v", p.fifo(i), id, err)
  40. }
  41. }
  42. }
  43. delete(ctr.processes, id)
  44. }
  45. func (ctr *container) spec() (*specs.Spec, error) {
  46. var spec specs.Spec
  47. dt, err := ioutil.ReadFile(filepath.Join(ctr.dir, configFilename))
  48. if err != nil {
  49. return nil, err
  50. }
  51. if err := json.Unmarshal(dt, &spec); err != nil {
  52. return nil, err
  53. }
  54. return &spec, nil
  55. }
  56. func (ctr *container) start() error {
  57. spec, err := ctr.spec()
  58. if err != nil {
  59. return nil
  60. }
  61. iopipe, err := ctr.openFifos(spec.Process.Terminal)
  62. if err != nil {
  63. return err
  64. }
  65. r := &containerd.CreateContainerRequest{
  66. Id: ctr.containerID,
  67. BundlePath: ctr.dir,
  68. Stdin: ctr.fifo(syscall.Stdin),
  69. Stdout: ctr.fifo(syscall.Stdout),
  70. Stderr: ctr.fifo(syscall.Stderr),
  71. // check to see if we are running in ramdisk to disable pivot root
  72. NoPivotRoot: os.Getenv("DOCKER_RAMDISK") != "",
  73. }
  74. ctr.client.appendContainer(ctr)
  75. resp, err := ctr.client.remote.apiClient.CreateContainer(context.Background(), r)
  76. if err != nil {
  77. ctr.closeFifos(iopipe)
  78. return err
  79. }
  80. if err := ctr.client.backend.AttachStreams(ctr.containerID, *iopipe); err != nil {
  81. return err
  82. }
  83. ctr.systemPid = systemPid(resp.Container)
  84. return ctr.client.backend.StateChanged(ctr.containerID, StateInfo{
  85. CommonStateInfo: CommonStateInfo{
  86. State: StateStart,
  87. Pid: ctr.systemPid,
  88. }})
  89. }
  90. func (ctr *container) newProcess(friendlyName string) *process {
  91. return &process{
  92. dir: ctr.dir,
  93. processCommon: processCommon{
  94. containerID: ctr.containerID,
  95. friendlyName: friendlyName,
  96. client: ctr.client,
  97. },
  98. }
  99. }
  100. func (ctr *container) handleEvent(e *containerd.Event) error {
  101. ctr.client.lock(ctr.containerID)
  102. defer ctr.client.unlock(ctr.containerID)
  103. switch e.Type {
  104. case StateExit, StatePause, StateResume, StateOOM:
  105. st := StateInfo{
  106. CommonStateInfo: CommonStateInfo{
  107. State: e.Type,
  108. ExitCode: e.Status,
  109. },
  110. OOMKilled: e.Type == StateExit && ctr.oom,
  111. }
  112. if e.Type == StateOOM {
  113. ctr.oom = true
  114. }
  115. if e.Type == StateExit && e.Pid != InitFriendlyName {
  116. st.ProcessID = e.Pid
  117. st.State = StateExitProcess
  118. }
  119. if st.State == StateExit && ctr.restartManager != nil {
  120. restart, wait, err := ctr.restartManager.ShouldRestart(e.Status, false)
  121. if err != nil {
  122. logrus.Warnf("container %s %v", ctr.containerID, err)
  123. } else if restart {
  124. st.State = StateRestart
  125. ctr.restarting = true
  126. ctr.client.deleteContainer(e.Id)
  127. go func() {
  128. err := <-wait
  129. ctr.restarting = false
  130. if err != nil {
  131. st.State = StateExit
  132. ctr.client.q.append(e.Id, func() {
  133. if err := ctr.client.backend.StateChanged(e.Id, st); err != nil {
  134. logrus.Error(err)
  135. }
  136. })
  137. if err != restartmanager.ErrRestartCanceled {
  138. logrus.Error(err)
  139. }
  140. } else {
  141. ctr.start()
  142. }
  143. }()
  144. }
  145. }
  146. // Remove process from list if we have exited
  147. // We need to do so here in case the Message Handler decides to restart it.
  148. switch st.State {
  149. case StateExit:
  150. if os.Getenv("LIBCONTAINERD_NOCLEAN") != "1" {
  151. ctr.clean()
  152. }
  153. ctr.client.deleteContainer(e.Id)
  154. case StateExitProcess:
  155. ctr.cleanProcess(st.ProcessID)
  156. }
  157. ctr.client.q.append(e.Id, func() {
  158. if err := ctr.client.backend.StateChanged(e.Id, st); err != nil {
  159. logrus.Error(err)
  160. }
  161. if e.Type == StatePause || e.Type == StateResume {
  162. ctr.pauseMonitor.handle(e.Type)
  163. }
  164. if e.Type == StateExit {
  165. if en := ctr.client.getExitNotifier(e.Id); en != nil {
  166. en.close()
  167. }
  168. }
  169. })
  170. default:
  171. logrus.Debugf("event unhandled: %+v", e)
  172. }
  173. return nil
  174. }