container_unix.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. "time"
  11. containerd "github.com/containerd/containerd/api/grpc/types"
  12. "github.com/docker/docker/pkg/ioutils"
  13. specs "github.com/opencontainers/runtime-spec/specs-go"
  14. "github.com/sirupsen/logrus"
  15. "github.com/tonistiigi/fifo"
  16. "golang.org/x/net/context"
  17. "golang.org/x/sys/unix"
  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{unix.Stdin, unix.Stdout, unix.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(spec *specs.Spec, checkpoint, checkpointDir string, attachStdio StdioCallback) (err error) {
  81. ctx, cancel := context.WithCancel(context.Background())
  82. defer cancel()
  83. ready := make(chan struct{})
  84. fifoCtx, cancel := context.WithCancel(context.Background())
  85. defer func() {
  86. if err != nil {
  87. cancel()
  88. }
  89. }()
  90. iopipe, err := ctr.openFifos(fifoCtx, spec.Process.Terminal)
  91. if err != nil {
  92. return err
  93. }
  94. var stdinOnce sync.Once
  95. // we need to delay stdin closure after container start or else "stdin close"
  96. // event will be rejected by containerd.
  97. // stdin closure happens in attachStdio
  98. stdin := iopipe.Stdin
  99. iopipe.Stdin = ioutils.NewWriteCloserWrapper(stdin, func() error {
  100. var err error
  101. stdinOnce.Do(func() { // on error from attach we don't know if stdin was already closed
  102. err = stdin.Close()
  103. go func() {
  104. select {
  105. case <-ready:
  106. case <-ctx.Done():
  107. }
  108. select {
  109. case <-ready:
  110. if err := ctr.sendCloseStdin(); err != nil {
  111. logrus.Warnf("failed to close stdin: %+v", err)
  112. }
  113. default:
  114. }
  115. }()
  116. })
  117. return err
  118. })
  119. r := &containerd.CreateContainerRequest{
  120. Id: ctr.containerID,
  121. BundlePath: ctr.dir,
  122. Stdin: ctr.fifo(unix.Stdin),
  123. Stdout: ctr.fifo(unix.Stdout),
  124. Stderr: ctr.fifo(unix.Stderr),
  125. Checkpoint: checkpoint,
  126. CheckpointDir: checkpointDir,
  127. // check to see if we are running in ramdisk to disable pivot root
  128. NoPivotRoot: os.Getenv("DOCKER_RAMDISK") != "",
  129. Runtime: ctr.runtime,
  130. RuntimeArgs: ctr.runtimeArgs,
  131. }
  132. ctr.client.appendContainer(ctr)
  133. if err := attachStdio(*iopipe); err != nil {
  134. ctr.closeFifos(iopipe)
  135. return err
  136. }
  137. resp, err := ctr.client.remote.apiClient.CreateContainer(context.Background(), r)
  138. if err != nil {
  139. ctr.closeFifos(iopipe)
  140. return err
  141. }
  142. ctr.systemPid = systemPid(resp.Container)
  143. close(ready)
  144. return ctr.client.backend.StateChanged(ctr.containerID, StateInfo{
  145. CommonStateInfo: CommonStateInfo{
  146. State: StateStart,
  147. Pid: ctr.systemPid,
  148. }})
  149. }
  150. func (ctr *container) newProcess(friendlyName string) *process {
  151. return &process{
  152. dir: ctr.dir,
  153. processCommon: processCommon{
  154. containerID: ctr.containerID,
  155. friendlyName: friendlyName,
  156. client: ctr.client,
  157. },
  158. }
  159. }
  160. func (ctr *container) handleEvent(e *containerd.Event) error {
  161. ctr.client.lock(ctr.containerID)
  162. defer ctr.client.unlock(ctr.containerID)
  163. switch e.Type {
  164. case StateExit, StatePause, StateResume, StateOOM:
  165. st := StateInfo{
  166. CommonStateInfo: CommonStateInfo{
  167. State: e.Type,
  168. ExitCode: e.Status,
  169. },
  170. OOMKilled: e.Type == StateExit && ctr.oom,
  171. }
  172. if e.Type == StateOOM {
  173. ctr.oom = true
  174. }
  175. if e.Type == StateExit && e.Pid != InitFriendlyName {
  176. st.ProcessID = e.Pid
  177. st.State = StateExitProcess
  178. }
  179. // Remove process from list if we have exited
  180. switch st.State {
  181. case StateExit:
  182. ctr.clean()
  183. ctr.client.deleteContainer(e.Id)
  184. case StateExitProcess:
  185. ctr.cleanProcess(st.ProcessID)
  186. }
  187. ctr.client.q.append(e.Id, func() {
  188. if err := ctr.client.backend.StateChanged(e.Id, st); err != nil {
  189. logrus.Errorf("libcontainerd: backend.StateChanged(): %v", err)
  190. }
  191. if e.Type == StatePause || e.Type == StateResume {
  192. ctr.pauseMonitor.handle(e.Type)
  193. }
  194. if e.Type == StateExit {
  195. if en := ctr.client.getExitNotifier(e.Id); en != nil {
  196. en.close()
  197. }
  198. }
  199. })
  200. default:
  201. logrus.Debugf("libcontainerd: event unhandled: %+v", e)
  202. }
  203. return nil
  204. }
  205. // discardFifos attempts to fully read the container fifos to unblock processes
  206. // that may be blocked on the writer side.
  207. func (ctr *container) discardFifos() {
  208. ctx, _ := context.WithTimeout(context.Background(), 3*time.Second)
  209. for _, i := range []int{unix.Stdout, unix.Stderr} {
  210. f, err := fifo.OpenFifo(ctx, ctr.fifo(i), unix.O_RDONLY|unix.O_NONBLOCK, 0)
  211. if err != nil {
  212. logrus.Warnf("error opening fifo %v for discarding: %+v", f, err)
  213. continue
  214. }
  215. go func() {
  216. io.Copy(ioutil.Discard, f)
  217. }()
  218. }
  219. }