container_linux.go 4.7 KB

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