container_unix.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. if err := ctr.sendCloseStdin(); err != nil {
  105. logrus.Warnf("failed to close stdin: %+v", err)
  106. }
  107. case <-ctx.Done():
  108. }
  109. }()
  110. })
  111. return err
  112. })
  113. r := &containerd.CreateContainerRequest{
  114. Id: ctr.containerID,
  115. BundlePath: ctr.dir,
  116. Stdin: ctr.fifo(syscall.Stdin),
  117. Stdout: ctr.fifo(syscall.Stdout),
  118. Stderr: ctr.fifo(syscall.Stderr),
  119. Checkpoint: checkpoint,
  120. CheckpointDir: checkpointDir,
  121. // check to see if we are running in ramdisk to disable pivot root
  122. NoPivotRoot: os.Getenv("DOCKER_RAMDISK") != "",
  123. Runtime: ctr.runtime,
  124. RuntimeArgs: ctr.runtimeArgs,
  125. }
  126. ctr.client.appendContainer(ctr)
  127. if err := attachStdio(*iopipe); err != nil {
  128. ctr.closeFifos(iopipe)
  129. return err
  130. }
  131. resp, err := ctr.client.remote.apiClient.CreateContainer(context.Background(), r)
  132. if err != nil {
  133. ctr.closeFifos(iopipe)
  134. return err
  135. }
  136. ctr.systemPid = systemPid(resp.Container)
  137. close(ready)
  138. return ctr.client.backend.StateChanged(ctr.containerID, StateInfo{
  139. CommonStateInfo: CommonStateInfo{
  140. State: StateStart,
  141. Pid: ctr.systemPid,
  142. }})
  143. }
  144. func (ctr *container) newProcess(friendlyName string) *process {
  145. return &process{
  146. dir: ctr.dir,
  147. processCommon: processCommon{
  148. containerID: ctr.containerID,
  149. friendlyName: friendlyName,
  150. client: ctr.client,
  151. },
  152. }
  153. }
  154. func (ctr *container) handleEvent(e *containerd.Event) error {
  155. ctr.client.lock(ctr.containerID)
  156. defer ctr.client.unlock(ctr.containerID)
  157. switch e.Type {
  158. case StateExit, StatePause, StateResume, StateOOM:
  159. st := StateInfo{
  160. CommonStateInfo: CommonStateInfo{
  161. State: e.Type,
  162. ExitCode: e.Status,
  163. },
  164. OOMKilled: e.Type == StateExit && ctr.oom,
  165. }
  166. if e.Type == StateOOM {
  167. ctr.oom = true
  168. }
  169. if e.Type == StateExit && e.Pid != InitFriendlyName {
  170. st.ProcessID = e.Pid
  171. st.State = StateExitProcess
  172. }
  173. // Remove process from list if we have exited
  174. switch st.State {
  175. case StateExit:
  176. ctr.clean()
  177. ctr.client.deleteContainer(e.Id)
  178. case StateExitProcess:
  179. ctr.cleanProcess(st.ProcessID)
  180. }
  181. ctr.client.q.append(e.Id, func() {
  182. if err := ctr.client.backend.StateChanged(e.Id, st); err != nil {
  183. logrus.Errorf("libcontainerd: backend.StateChanged(): %v", err)
  184. }
  185. if e.Type == StatePause || e.Type == StateResume {
  186. ctr.pauseMonitor.handle(e.Type)
  187. }
  188. if e.Type == StateExit {
  189. if en := ctr.client.getExitNotifier(e.Id); en != nil {
  190. en.close()
  191. }
  192. }
  193. })
  194. default:
  195. logrus.Debugf("libcontainerd: event unhandled: %+v", e)
  196. }
  197. return nil
  198. }
  199. // discardFifos attempts to fully read the container fifos to unblock processes
  200. // that may be blocked on the writer side.
  201. func (ctr *container) discardFifos() {
  202. ctx, _ := context.WithTimeout(context.Background(), 3*time.Second)
  203. for _, i := range []int{syscall.Stdout, syscall.Stderr} {
  204. f, err := fifo.OpenFifo(ctx, ctr.fifo(i), syscall.O_RDONLY|syscall.O_NONBLOCK, 0)
  205. if err != nil {
  206. logrus.Warnf("error opening fifo %v for discarding: %+v", f, err)
  207. continue
  208. }
  209. go func() {
  210. io.Copy(ioutil.Discard, f)
  211. }()
  212. }
  213. }