container_unix.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // +build linux solaris
  2. package libcontainerd
  3. import (
  4. "encoding/json"
  5. "io"
  6. "io/ioutil"
  7. "os"
  8. "path/filepath"
  9. "sync"
  10. "syscall"
  11. "time"
  12. "github.com/Sirupsen/logrus"
  13. containerd "github.com/docker/containerd/api/grpc/types"
  14. "github.com/docker/docker/pkg/ioutils"
  15. specs "github.com/opencontainers/runtime-spec/specs-go"
  16. "github.com/tonistiigi/fifo"
  17. "golang.org/x/net/context"
  18. )
  19. type container struct {
  20. containerCommon
  21. // Platform specific fields are below here.
  22. pauseMonitor
  23. oom bool
  24. runtime string
  25. runtimeArgs []string
  26. }
  27. type runtime struct {
  28. path string
  29. args []string
  30. }
  31. // WithRuntime sets the runtime to be used for the created container
  32. func WithRuntime(path string, args []string) CreateOption {
  33. return runtime{path, args}
  34. }
  35. func (rt runtime) Apply(p interface{}) error {
  36. if pr, ok := p.(*container); ok {
  37. pr.runtime = rt.path
  38. pr.runtimeArgs = rt.args
  39. }
  40. return nil
  41. }
  42. func (ctr *container) clean() error {
  43. if os.Getenv("LIBCONTAINERD_NOCLEAN") == "1" {
  44. return nil
  45. }
  46. if _, err := os.Lstat(ctr.dir); err != nil {
  47. if os.IsNotExist(err) {
  48. return nil
  49. }
  50. return err
  51. }
  52. if err := os.RemoveAll(ctr.dir); err != nil {
  53. return err
  54. }
  55. return nil
  56. }
  57. // cleanProcess removes the fifos used by an additional process.
  58. // Caller needs to lock container ID before calling this method.
  59. func (ctr *container) cleanProcess(id string) {
  60. if p, ok := ctr.processes[id]; ok {
  61. for _, i := range []int{syscall.Stdin, syscall.Stdout, syscall.Stderr} {
  62. if err := os.Remove(p.fifo(i)); err != nil && !os.IsNotExist(err) {
  63. logrus.Warnf("libcontainerd: failed to remove %v for process %v: %v", p.fifo(i), id, err)
  64. }
  65. }
  66. }
  67. delete(ctr.processes, id)
  68. }
  69. func (ctr *container) spec() (*specs.Spec, error) {
  70. var spec specs.Spec
  71. dt, err := ioutil.ReadFile(filepath.Join(ctr.dir, configFilename))
  72. if err != nil {
  73. return nil, err
  74. }
  75. if err := json.Unmarshal(dt, &spec); err != nil {
  76. return nil, err
  77. }
  78. return &spec, nil
  79. }
  80. func (ctr *container) start(checkpoint string, checkpointDir string, attachStdio StdioCallback) error {
  81. spec, err := ctr.spec()
  82. if err != nil {
  83. return nil
  84. }
  85. ctx, cancel := context.WithCancel(context.Background())
  86. defer cancel()
  87. ready := make(chan struct{})
  88. iopipe, err := ctr.openFifos(spec.Process.Terminal)
  89. if err != nil {
  90. return err
  91. }
  92. var stdinOnce sync.Once
  93. // we need to delay stdin closure after container start or else "stdin close"
  94. // event will be rejected by containerd.
  95. // stdin closure happens in attachStdio
  96. stdin := iopipe.Stdin
  97. iopipe.Stdin = ioutils.NewWriteCloserWrapper(stdin, func() error {
  98. var err error
  99. stdinOnce.Do(func() { // on error from attach we don't know if stdin was already closed
  100. err = stdin.Close()
  101. go func() {
  102. select {
  103. case <-ready:
  104. case <-ctx.Done():
  105. }
  106. select {
  107. case <-ready:
  108. if err := ctr.sendCloseStdin(); err != nil {
  109. logrus.Warnf("failed to close stdin: %+v", err)
  110. }
  111. default:
  112. }
  113. }()
  114. })
  115. return err
  116. })
  117. r := &containerd.CreateContainerRequest{
  118. Id: ctr.containerID,
  119. BundlePath: ctr.dir,
  120. Stdin: ctr.fifo(syscall.Stdin),
  121. Stdout: ctr.fifo(syscall.Stdout),
  122. Stderr: ctr.fifo(syscall.Stderr),
  123. Checkpoint: checkpoint,
  124. CheckpointDir: checkpointDir,
  125. // check to see if we are running in ramdisk to disable pivot root
  126. NoPivotRoot: os.Getenv("DOCKER_RAMDISK") != "",
  127. Runtime: ctr.runtime,
  128. RuntimeArgs: ctr.runtimeArgs,
  129. }
  130. ctr.client.appendContainer(ctr)
  131. if err := attachStdio(*iopipe); err != nil {
  132. ctr.closeFifos(iopipe)
  133. return err
  134. }
  135. resp, err := ctr.client.remote.apiClient.CreateContainer(context.Background(), r)
  136. if err != nil {
  137. ctr.closeFifos(iopipe)
  138. return err
  139. }
  140. ctr.systemPid = systemPid(resp.Container)
  141. close(ready)
  142. return ctr.client.backend.StateChanged(ctr.containerID, StateInfo{
  143. CommonStateInfo: CommonStateInfo{
  144. State: StateStart,
  145. Pid: ctr.systemPid,
  146. }})
  147. }
  148. func (ctr *container) newProcess(friendlyName string) *process {
  149. return &process{
  150. dir: ctr.dir,
  151. processCommon: processCommon{
  152. containerID: ctr.containerID,
  153. friendlyName: friendlyName,
  154. client: ctr.client,
  155. },
  156. }
  157. }
  158. func (ctr *container) handleEvent(e *containerd.Event) error {
  159. ctr.client.lock(ctr.containerID)
  160. defer ctr.client.unlock(ctr.containerID)
  161. switch e.Type {
  162. case StateExit, StatePause, StateResume, StateOOM:
  163. st := StateInfo{
  164. CommonStateInfo: CommonStateInfo{
  165. State: e.Type,
  166. ExitCode: e.Status,
  167. },
  168. OOMKilled: e.Type == StateExit && ctr.oom,
  169. }
  170. if e.Type == StateOOM {
  171. ctr.oom = true
  172. }
  173. if e.Type == StateExit && e.Pid != InitFriendlyName {
  174. st.ProcessID = e.Pid
  175. st.State = StateExitProcess
  176. }
  177. // Remove process from list if we have exited
  178. switch st.State {
  179. case StateExit:
  180. ctr.clean()
  181. ctr.client.deleteContainer(e.Id)
  182. case StateExitProcess:
  183. ctr.cleanProcess(st.ProcessID)
  184. }
  185. ctr.client.q.append(e.Id, func() {
  186. if err := ctr.client.backend.StateChanged(e.Id, st); err != nil {
  187. logrus.Errorf("libcontainerd: backend.StateChanged(): %v", err)
  188. }
  189. if e.Type == StatePause || e.Type == StateResume {
  190. ctr.pauseMonitor.handle(e.Type)
  191. }
  192. if e.Type == StateExit {
  193. if en := ctr.client.getExitNotifier(e.Id); en != nil {
  194. en.close()
  195. }
  196. }
  197. })
  198. default:
  199. logrus.Debugf("libcontainerd: event unhandled: %+v", e)
  200. }
  201. return nil
  202. }
  203. // discardFifos attempts to fully read the container fifos to unblock processes
  204. // that may be blocked on the writer side.
  205. func (ctr *container) discardFifos() {
  206. ctx, _ := context.WithTimeout(context.Background(), 3*time.Second)
  207. for _, i := range []int{syscall.Stdout, syscall.Stderr} {
  208. f, err := fifo.OpenFifo(ctx, ctr.fifo(i), syscall.O_RDONLY|syscall.O_NONBLOCK, 0)
  209. if err != nil {
  210. logrus.Warnf("error opening fifo %v for discarding: %+v", f, err)
  211. continue
  212. }
  213. go func() {
  214. io.Copy(ioutil.Discard, f)
  215. }()
  216. }
  217. }