container_linux.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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/opencontainers/specs/specs-go"
  11. "golang.org/x/net/context"
  12. )
  13. type container struct {
  14. containerCommon
  15. // Platform specific fields are below here.
  16. pauseMonitor
  17. oom bool
  18. }
  19. func (ctr *container) clean() error {
  20. if _, err := os.Lstat(ctr.dir); err != nil {
  21. if os.IsNotExist(err) {
  22. return nil
  23. }
  24. return err
  25. }
  26. syscall.Unmount(filepath.Join(ctr.dir, "rootfs"), syscall.MNT_DETACH) // ignore error
  27. if err := os.RemoveAll(ctr.dir); err != nil {
  28. return err
  29. }
  30. return nil
  31. }
  32. func (ctr *container) spec() (*specs.Spec, error) {
  33. var spec specs.Spec
  34. dt, err := ioutil.ReadFile(filepath.Join(ctr.dir, configFilename))
  35. if err != nil {
  36. return nil, err
  37. }
  38. if err := json.Unmarshal(dt, &spec); err != nil {
  39. return nil, err
  40. }
  41. return &spec, nil
  42. }
  43. func (ctr *container) start() error {
  44. spec, err := ctr.spec()
  45. if err != nil {
  46. return nil
  47. }
  48. iopipe, err := ctr.openFifos(spec.Process.Terminal)
  49. if err != nil {
  50. return err
  51. }
  52. r := &containerd.CreateContainerRequest{
  53. Id: ctr.containerID,
  54. BundlePath: ctr.dir,
  55. Stdin: ctr.fifo(syscall.Stdin),
  56. Stdout: ctr.fifo(syscall.Stdout),
  57. Stderr: ctr.fifo(syscall.Stderr),
  58. }
  59. ctr.client.appendContainer(ctr)
  60. resp, err := ctr.client.remote.apiClient.CreateContainer(context.Background(), r)
  61. if err != nil {
  62. ctr.closeFifos(iopipe)
  63. return err
  64. }
  65. if err := ctr.client.backend.AttachStreams(ctr.containerID, *iopipe); err != nil {
  66. return err
  67. }
  68. ctr.systemPid = systemPid(resp.Container)
  69. return ctr.client.backend.StateChanged(ctr.containerID, StateInfo{
  70. State: StateStart,
  71. Pid: ctr.systemPid,
  72. })
  73. }
  74. func (ctr *container) newProcess(friendlyName string) *process {
  75. return &process{
  76. dir: ctr.dir,
  77. processCommon: processCommon{
  78. containerID: ctr.containerID,
  79. friendlyName: friendlyName,
  80. client: ctr.client,
  81. },
  82. }
  83. }
  84. func (ctr *container) handleEvent(e *containerd.Event) error {
  85. ctr.client.lock(ctr.containerID)
  86. defer ctr.client.unlock(ctr.containerID)
  87. switch e.Type {
  88. case StateExit, StatePause, StateResume, StateOOM:
  89. st := StateInfo{
  90. State: e.Type,
  91. ExitCode: e.Status,
  92. OOMKilled: e.Type == StateExit && ctr.oom,
  93. }
  94. if e.Type == StateOOM {
  95. ctr.oom = true
  96. }
  97. if e.Type == StateExit && e.Pid != InitFriendlyName {
  98. st.ProcessID = e.Pid
  99. st.State = StateExitProcess
  100. }
  101. if st.State == StateExit && ctr.restartManager != nil {
  102. restart, wait, err := ctr.restartManager.ShouldRestart(e.Status)
  103. if err != nil {
  104. logrus.Error(err)
  105. } else if restart {
  106. st.State = StateRestart
  107. ctr.restarting = true
  108. go func() {
  109. err := <-wait
  110. ctr.restarting = false
  111. if err != nil {
  112. st.State = StateExit
  113. ctr.client.q.append(e.Id, func() {
  114. if err := ctr.client.backend.StateChanged(e.Id, st); err != nil {
  115. logrus.Error(err)
  116. }
  117. })
  118. logrus.Error(err)
  119. } else {
  120. ctr.start()
  121. }
  122. }()
  123. }
  124. }
  125. // Remove process from list if we have exited
  126. // We need to do so here in case the Message Handler decides to restart it.
  127. if st.State == StateExit {
  128. if os.Getenv("LIBCONTAINERD_NOCLEAN") != "1" {
  129. ctr.clean()
  130. }
  131. ctr.client.deleteContainer(e.Id)
  132. }
  133. ctr.client.q.append(e.Id, func() {
  134. if err := ctr.client.backend.StateChanged(e.Id, st); err != nil {
  135. logrus.Error(err)
  136. }
  137. if e.Type == StatePause || e.Type == StateResume {
  138. ctr.pauseMonitor.handle(e.Type)
  139. }
  140. if e.Type == StateExit {
  141. if en := ctr.client.getExitNotifier(e.Id); en != nil {
  142. en.close()
  143. }
  144. }
  145. })
  146. default:
  147. logrus.Debugf("event unhandled: %+v", e)
  148. }
  149. return nil
  150. }