client.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. package remote // import "github.com/docker/docker/libcontainerd/remote"
  2. import (
  3. "context"
  4. "encoding/json"
  5. "io"
  6. "os"
  7. "path/filepath"
  8. "reflect"
  9. "runtime"
  10. "strings"
  11. "sync"
  12. "syscall"
  13. "time"
  14. "github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/options"
  15. "github.com/containerd/containerd"
  16. apievents "github.com/containerd/containerd/api/events"
  17. "github.com/containerd/containerd/api/types"
  18. "github.com/containerd/containerd/archive"
  19. "github.com/containerd/containerd/cio"
  20. "github.com/containerd/containerd/content"
  21. containerderrors "github.com/containerd/containerd/errdefs"
  22. "github.com/containerd/containerd/events"
  23. "github.com/containerd/containerd/images"
  24. "github.com/containerd/containerd/runtime/linux/runctypes"
  25. "github.com/containerd/typeurl"
  26. "github.com/docker/docker/errdefs"
  27. "github.com/docker/docker/libcontainerd/queue"
  28. libcontainerdtypes "github.com/docker/docker/libcontainerd/types"
  29. "github.com/docker/docker/pkg/ioutils"
  30. v1 "github.com/opencontainers/image-spec/specs-go/v1"
  31. specs "github.com/opencontainers/runtime-spec/specs-go"
  32. "github.com/pkg/errors"
  33. "github.com/sirupsen/logrus"
  34. "google.golang.org/grpc/codes"
  35. "google.golang.org/grpc/status"
  36. )
  37. type container struct {
  38. mu sync.Mutex
  39. bundleDir string
  40. ctr containerd.Container
  41. task containerd.Task
  42. execs map[string]containerd.Process
  43. oomKilled bool
  44. }
  45. func (c *container) setTask(t containerd.Task) {
  46. c.mu.Lock()
  47. c.task = t
  48. c.mu.Unlock()
  49. }
  50. func (c *container) getTask() containerd.Task {
  51. c.mu.Lock()
  52. t := c.task
  53. c.mu.Unlock()
  54. return t
  55. }
  56. func (c *container) addProcess(id string, p containerd.Process) {
  57. c.mu.Lock()
  58. if c.execs == nil {
  59. c.execs = make(map[string]containerd.Process)
  60. }
  61. c.execs[id] = p
  62. c.mu.Unlock()
  63. }
  64. func (c *container) deleteProcess(id string) {
  65. c.mu.Lock()
  66. delete(c.execs, id)
  67. c.mu.Unlock()
  68. }
  69. func (c *container) getProcess(id string) containerd.Process {
  70. c.mu.Lock()
  71. p := c.execs[id]
  72. c.mu.Unlock()
  73. return p
  74. }
  75. func (c *container) setOOMKilled(killed bool) {
  76. c.mu.Lock()
  77. c.oomKilled = killed
  78. c.mu.Unlock()
  79. }
  80. func (c *container) getOOMKilled() bool {
  81. c.mu.Lock()
  82. killed := c.oomKilled
  83. c.mu.Unlock()
  84. return killed
  85. }
  86. type client struct {
  87. sync.RWMutex // protects containers map
  88. client *containerd.Client
  89. stateDir string
  90. logger *logrus.Entry
  91. ns string
  92. backend libcontainerdtypes.Backend
  93. eventQ queue.Queue
  94. containers map[string]*container
  95. }
  96. // NewClient creates a new libcontainerd client from a containerd client
  97. func NewClient(ctx context.Context, cli *containerd.Client, stateDir, ns string, b libcontainerdtypes.Backend) (libcontainerdtypes.Client, error) {
  98. c := &client{
  99. client: cli,
  100. stateDir: stateDir,
  101. logger: logrus.WithField("module", "libcontainerd").WithField("namespace", ns),
  102. ns: ns,
  103. backend: b,
  104. containers: make(map[string]*container),
  105. }
  106. go c.processEventStream(ctx, ns)
  107. return c, nil
  108. }
  109. func (c *client) Version(ctx context.Context) (containerd.Version, error) {
  110. return c.client.Version(ctx)
  111. }
  112. // Restore loads the containerd container.
  113. // It should not be called concurrently with any other operation for the given ID.
  114. func (c *client) Restore(ctx context.Context, id string, attachStdio libcontainerdtypes.StdioCallback) (alive bool, pid int, err error) {
  115. c.Lock()
  116. _, ok := c.containers[id]
  117. if ok {
  118. c.Unlock()
  119. return false, 0, errors.WithStack(errdefs.Conflict(errors.New("id already in use")))
  120. }
  121. cntr := &container{}
  122. c.containers[id] = cntr
  123. cntr.mu.Lock()
  124. defer cntr.mu.Unlock()
  125. c.Unlock()
  126. defer func() {
  127. if err != nil {
  128. c.Lock()
  129. delete(c.containers, id)
  130. c.Unlock()
  131. }
  132. }()
  133. var dio *cio.DirectIO
  134. defer func() {
  135. if err != nil && dio != nil {
  136. dio.Cancel()
  137. dio.Close()
  138. }
  139. err = wrapError(err)
  140. }()
  141. ctr, err := c.client.LoadContainer(ctx, id)
  142. if err != nil {
  143. return false, -1, errors.WithStack(wrapError(err))
  144. }
  145. attachIO := func(fifos *cio.FIFOSet) (cio.IO, error) {
  146. // dio must be assigned to the previously defined dio for the defer above
  147. // to handle cleanup
  148. dio, err = c.newDirectIO(ctx, fifos)
  149. if err != nil {
  150. return nil, err
  151. }
  152. return attachStdio(dio)
  153. }
  154. t, err := ctr.Task(ctx, attachIO)
  155. if err != nil && !containerderrors.IsNotFound(err) {
  156. return false, -1, errors.Wrap(wrapError(err), "error getting containerd task for container")
  157. }
  158. if t != nil {
  159. s, err := t.Status(ctx)
  160. if err != nil {
  161. return false, -1, errors.Wrap(wrapError(err), "error getting task status")
  162. }
  163. alive = s.Status != containerd.Stopped
  164. pid = int(t.Pid())
  165. }
  166. cntr.bundleDir = filepath.Join(c.stateDir, id)
  167. cntr.ctr = ctr
  168. cntr.task = t
  169. // TODO(mlaventure): load execs
  170. c.logger.WithFields(logrus.Fields{
  171. "container": id,
  172. "alive": alive,
  173. "pid": pid,
  174. }).Debug("restored container")
  175. return alive, pid, nil
  176. }
  177. func (c *client) Create(ctx context.Context, id string, ociSpec *specs.Spec, runtimeOptions interface{}) error {
  178. if ctr := c.getContainer(id); ctr != nil {
  179. return errors.WithStack(errdefs.Conflict(errors.New("id already in use")))
  180. }
  181. bdir, err := prepareBundleDir(filepath.Join(c.stateDir, id), ociSpec)
  182. if err != nil {
  183. return errdefs.System(errors.Wrap(err, "prepare bundle dir failed"))
  184. }
  185. c.logger.WithField("bundle", bdir).WithField("root", ociSpec.Root.Path).Debug("bundle dir created")
  186. cdCtr, err := c.client.NewContainer(ctx, id,
  187. containerd.WithSpec(ociSpec),
  188. containerd.WithRuntime(runtimeName, runtimeOptions))
  189. if err != nil {
  190. return wrapError(err)
  191. }
  192. c.Lock()
  193. c.containers[id] = &container{
  194. bundleDir: bdir,
  195. ctr: cdCtr,
  196. }
  197. c.Unlock()
  198. return nil
  199. }
  200. // Start create and start a task for the specified containerd id
  201. func (c *client) Start(ctx context.Context, id, checkpointDir string, withStdin bool, attachStdio libcontainerdtypes.StdioCallback) (int, error) {
  202. ctr := c.getContainer(id)
  203. if ctr == nil {
  204. return -1, errors.WithStack(errdefs.NotFound(errors.New("no such container")))
  205. }
  206. if t := ctr.getTask(); t != nil {
  207. return -1, errors.WithStack(errdefs.Conflict(errors.New("container already started")))
  208. }
  209. var (
  210. cp *types.Descriptor
  211. t containerd.Task
  212. rio cio.IO
  213. err error
  214. stdinCloseSync = make(chan struct{})
  215. )
  216. if checkpointDir != "" {
  217. // write checkpoint to the content store
  218. tar := archive.Diff(ctx, "", checkpointDir)
  219. cp, err = c.writeContent(ctx, images.MediaTypeContainerd1Checkpoint, checkpointDir, tar)
  220. // remove the checkpoint when we're done
  221. defer func() {
  222. if cp != nil {
  223. err := c.client.ContentStore().Delete(context.Background(), cp.Digest)
  224. if err != nil {
  225. c.logger.WithError(err).WithFields(logrus.Fields{
  226. "ref": checkpointDir,
  227. "digest": cp.Digest,
  228. }).Warnf("failed to delete temporary checkpoint entry")
  229. }
  230. }
  231. }()
  232. if err := tar.Close(); err != nil {
  233. return -1, errors.Wrap(err, "failed to close checkpoint tar stream")
  234. }
  235. if err != nil {
  236. return -1, errors.Wrapf(err, "failed to upload checkpoint to containerd")
  237. }
  238. }
  239. spec, err := ctr.ctr.Spec(ctx)
  240. if err != nil {
  241. return -1, errors.Wrap(err, "failed to retrieve spec")
  242. }
  243. uid, gid := getSpecUser(spec)
  244. t, err = ctr.ctr.NewTask(ctx,
  245. func(id string) (cio.IO, error) {
  246. fifos := newFIFOSet(ctr.bundleDir, libcontainerdtypes.InitProcessName, withStdin, spec.Process.Terminal)
  247. rio, err = c.createIO(fifos, id, libcontainerdtypes.InitProcessName, stdinCloseSync, attachStdio)
  248. return rio, err
  249. },
  250. func(_ context.Context, _ *containerd.Client, info *containerd.TaskInfo) error {
  251. info.Checkpoint = cp
  252. if runtime.GOOS != "windows" {
  253. info.Options = &runctypes.CreateOptions{
  254. IoUid: uint32(uid),
  255. IoGid: uint32(gid),
  256. NoPivotRoot: os.Getenv("DOCKER_RAMDISK") != "",
  257. }
  258. } else {
  259. // Make sure we set the runhcs options to debug if we are at debug level.
  260. if c.logger.Level == logrus.DebugLevel {
  261. info.Options = &options.Options{Debug: true}
  262. }
  263. }
  264. return nil
  265. })
  266. if err != nil {
  267. close(stdinCloseSync)
  268. if rio != nil {
  269. rio.Cancel()
  270. rio.Close()
  271. }
  272. return -1, wrapError(err)
  273. }
  274. ctr.setTask(t)
  275. // Signal c.createIO that it can call CloseIO
  276. close(stdinCloseSync)
  277. if err := t.Start(ctx); err != nil {
  278. if _, err := t.Delete(ctx); err != nil {
  279. c.logger.WithError(err).WithField("container", id).
  280. Error("failed to delete task after fail start")
  281. }
  282. ctr.setTask(nil)
  283. return -1, wrapError(err)
  284. }
  285. return int(t.Pid()), nil
  286. }
  287. // Exec creates exec process.
  288. //
  289. // The containerd client calls Exec to register the exec config in the shim side.
  290. // When the client calls Start, the shim will create stdin fifo if needs. But
  291. // for the container main process, the stdin fifo will be created in Create not
  292. // the Start call. stdinCloseSync channel should be closed after Start exec
  293. // process.
  294. func (c *client) Exec(ctx context.Context, containerID, processID string, spec *specs.Process, withStdin bool, attachStdio libcontainerdtypes.StdioCallback) (int, error) {
  295. ctr := c.getContainer(containerID)
  296. if ctr == nil {
  297. return -1, errors.WithStack(errdefs.NotFound(errors.New("no such container")))
  298. }
  299. t := ctr.getTask()
  300. if t == nil {
  301. return -1, errors.WithStack(errdefs.InvalidParameter(errors.New("container is not running")))
  302. }
  303. if p := ctr.getProcess(processID); p != nil {
  304. return -1, errors.WithStack(errdefs.Conflict(errors.New("id already in use")))
  305. }
  306. var (
  307. p containerd.Process
  308. rio cio.IO
  309. err error
  310. stdinCloseSync = make(chan struct{})
  311. )
  312. fifos := newFIFOSet(ctr.bundleDir, processID, withStdin, spec.Terminal)
  313. defer func() {
  314. if err != nil {
  315. if rio != nil {
  316. rio.Cancel()
  317. rio.Close()
  318. }
  319. }
  320. }()
  321. p, err = t.Exec(ctx, processID, spec, func(id string) (cio.IO, error) {
  322. rio, err = c.createIO(fifos, containerID, processID, stdinCloseSync, attachStdio)
  323. return rio, err
  324. })
  325. if err != nil {
  326. close(stdinCloseSync)
  327. return -1, wrapError(err)
  328. }
  329. ctr.addProcess(processID, p)
  330. // Signal c.createIO that it can call CloseIO
  331. //
  332. // the stdin of exec process will be created after p.Start in containerd
  333. defer close(stdinCloseSync)
  334. if err = p.Start(ctx); err != nil {
  335. // use new context for cleanup because old one may be cancelled by user, but leave a timeout to make sure
  336. // we are not waiting forever if containerd is unresponsive or to work around fifo cancelling issues in
  337. // older containerd-shim
  338. ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second)
  339. defer cancel()
  340. p.Delete(ctx)
  341. ctr.deleteProcess(processID)
  342. return -1, wrapError(err)
  343. }
  344. return int(p.Pid()), nil
  345. }
  346. func (c *client) SignalProcess(ctx context.Context, containerID, processID string, signal int) error {
  347. p, err := c.getProcess(containerID, processID)
  348. if err != nil {
  349. return err
  350. }
  351. return wrapError(p.Kill(ctx, syscall.Signal(signal)))
  352. }
  353. func (c *client) ResizeTerminal(ctx context.Context, containerID, processID string, width, height int) error {
  354. p, err := c.getProcess(containerID, processID)
  355. if err != nil {
  356. return err
  357. }
  358. return p.Resize(ctx, uint32(width), uint32(height))
  359. }
  360. func (c *client) CloseStdin(ctx context.Context, containerID, processID string) error {
  361. p, err := c.getProcess(containerID, processID)
  362. if err != nil {
  363. return err
  364. }
  365. return p.CloseIO(ctx, containerd.WithStdinCloser)
  366. }
  367. func (c *client) Pause(ctx context.Context, containerID string) error {
  368. p, err := c.getProcess(containerID, libcontainerdtypes.InitProcessName)
  369. if err != nil {
  370. return err
  371. }
  372. return wrapError(p.(containerd.Task).Pause(ctx))
  373. }
  374. func (c *client) Resume(ctx context.Context, containerID string) error {
  375. p, err := c.getProcess(containerID, libcontainerdtypes.InitProcessName)
  376. if err != nil {
  377. return err
  378. }
  379. return p.(containerd.Task).Resume(ctx)
  380. }
  381. func (c *client) Stats(ctx context.Context, containerID string) (*libcontainerdtypes.Stats, error) {
  382. p, err := c.getProcess(containerID, libcontainerdtypes.InitProcessName)
  383. if err != nil {
  384. return nil, err
  385. }
  386. m, err := p.(containerd.Task).Metrics(ctx)
  387. if err != nil {
  388. return nil, err
  389. }
  390. v, err := typeurl.UnmarshalAny(m.Data)
  391. if err != nil {
  392. return nil, err
  393. }
  394. return libcontainerdtypes.InterfaceToStats(m.Timestamp, v), nil
  395. }
  396. func (c *client) ListPids(ctx context.Context, containerID string) ([]uint32, error) {
  397. p, err := c.getProcess(containerID, libcontainerdtypes.InitProcessName)
  398. if err != nil {
  399. return nil, err
  400. }
  401. pis, err := p.(containerd.Task).Pids(ctx)
  402. if err != nil {
  403. return nil, err
  404. }
  405. var pids []uint32
  406. for _, i := range pis {
  407. pids = append(pids, i.Pid)
  408. }
  409. return pids, nil
  410. }
  411. func (c *client) Summary(ctx context.Context, containerID string) ([]libcontainerdtypes.Summary, error) {
  412. p, err := c.getProcess(containerID, libcontainerdtypes.InitProcessName)
  413. if err != nil {
  414. return nil, err
  415. }
  416. pis, err := p.(containerd.Task).Pids(ctx)
  417. if err != nil {
  418. return nil, err
  419. }
  420. var infos []libcontainerdtypes.Summary
  421. for _, pi := range pis {
  422. i, err := typeurl.UnmarshalAny(pi.Info)
  423. if err != nil {
  424. return nil, errors.Wrap(err, "unable to decode process details")
  425. }
  426. s, err := summaryFromInterface(i)
  427. if err != nil {
  428. return nil, err
  429. }
  430. infos = append(infos, *s)
  431. }
  432. return infos, nil
  433. }
  434. func (c *client) DeleteTask(ctx context.Context, containerID string) (uint32, time.Time, error) {
  435. p, err := c.getProcess(containerID, libcontainerdtypes.InitProcessName)
  436. if err != nil {
  437. return 255, time.Now(), nil
  438. }
  439. status, err := p.(containerd.Task).Delete(ctx)
  440. if err != nil {
  441. return 255, time.Now(), nil
  442. }
  443. if ctr := c.getContainer(containerID); ctr != nil {
  444. ctr.setTask(nil)
  445. }
  446. return status.ExitCode(), status.ExitTime(), nil
  447. }
  448. func (c *client) Delete(ctx context.Context, containerID string) error {
  449. ctr := c.getContainer(containerID)
  450. if ctr == nil {
  451. return errors.WithStack(errdefs.NotFound(errors.New("no such container")))
  452. }
  453. if err := ctr.ctr.Delete(ctx); err != nil {
  454. return wrapError(err)
  455. }
  456. if os.Getenv("LIBCONTAINERD_NOCLEAN") != "1" {
  457. if err := os.RemoveAll(ctr.bundleDir); err != nil {
  458. c.logger.WithError(err).WithFields(logrus.Fields{
  459. "container": containerID,
  460. "bundle": ctr.bundleDir,
  461. }).Error("failed to remove state dir")
  462. }
  463. }
  464. c.removeContainer(containerID)
  465. return nil
  466. }
  467. func (c *client) Status(ctx context.Context, containerID string) (libcontainerdtypes.Status, error) {
  468. ctr := c.getContainer(containerID)
  469. if ctr == nil {
  470. return libcontainerdtypes.StatusUnknown, errors.WithStack(errdefs.NotFound(errors.New("no such container")))
  471. }
  472. t := ctr.getTask()
  473. if t == nil {
  474. return libcontainerdtypes.StatusUnknown, errors.WithStack(errdefs.NotFound(errors.New("no such task")))
  475. }
  476. s, err := t.Status(ctx)
  477. if err != nil {
  478. return libcontainerdtypes.StatusUnknown, wrapError(err)
  479. }
  480. return libcontainerdtypes.Status(s.Status), nil
  481. }
  482. func (c *client) CreateCheckpoint(ctx context.Context, containerID, checkpointDir string, exit bool) error {
  483. p, err := c.getProcess(containerID, libcontainerdtypes.InitProcessName)
  484. if err != nil {
  485. return err
  486. }
  487. opts := []containerd.CheckpointTaskOpts{}
  488. if exit {
  489. opts = append(opts, func(r *containerd.CheckpointTaskInfo) error {
  490. if r.Options == nil {
  491. r.Options = &runctypes.CheckpointOptions{
  492. Exit: true,
  493. }
  494. } else {
  495. opts, _ := r.Options.(*runctypes.CheckpointOptions)
  496. opts.Exit = true
  497. }
  498. return nil
  499. })
  500. }
  501. img, err := p.(containerd.Task).Checkpoint(ctx, opts...)
  502. if err != nil {
  503. return wrapError(err)
  504. }
  505. // Whatever happens, delete the checkpoint from containerd
  506. defer func() {
  507. err := c.client.ImageService().Delete(context.Background(), img.Name())
  508. if err != nil {
  509. c.logger.WithError(err).WithField("digest", img.Target().Digest).
  510. Warnf("failed to delete checkpoint image")
  511. }
  512. }()
  513. b, err := content.ReadBlob(ctx, c.client.ContentStore(), img.Target())
  514. if err != nil {
  515. return errdefs.System(errors.Wrapf(err, "failed to retrieve checkpoint data"))
  516. }
  517. var index v1.Index
  518. if err := json.Unmarshal(b, &index); err != nil {
  519. return errdefs.System(errors.Wrapf(err, "failed to decode checkpoint data"))
  520. }
  521. var cpDesc *v1.Descriptor
  522. for _, m := range index.Manifests {
  523. if m.MediaType == images.MediaTypeContainerd1Checkpoint {
  524. cpDesc = &m
  525. break
  526. }
  527. }
  528. if cpDesc == nil {
  529. return errdefs.System(errors.Wrapf(err, "invalid checkpoint"))
  530. }
  531. rat, err := c.client.ContentStore().ReaderAt(ctx, *cpDesc)
  532. if err != nil {
  533. return errdefs.System(errors.Wrapf(err, "failed to get checkpoint reader"))
  534. }
  535. defer rat.Close()
  536. _, err = archive.Apply(ctx, checkpointDir, content.NewReader(rat))
  537. if err != nil {
  538. return errdefs.System(errors.Wrapf(err, "failed to read checkpoint reader"))
  539. }
  540. return err
  541. }
  542. func (c *client) getContainer(id string) *container {
  543. c.RLock()
  544. ctr := c.containers[id]
  545. c.RUnlock()
  546. return ctr
  547. }
  548. func (c *client) removeContainer(id string) {
  549. c.Lock()
  550. delete(c.containers, id)
  551. c.Unlock()
  552. }
  553. func (c *client) getProcess(containerID, processID string) (containerd.Process, error) {
  554. ctr := c.getContainer(containerID)
  555. if ctr == nil {
  556. return nil, errors.WithStack(errdefs.NotFound(errors.New("no such container")))
  557. }
  558. t := ctr.getTask()
  559. if t == nil {
  560. return nil, errors.WithStack(errdefs.NotFound(errors.New("container is not running")))
  561. }
  562. if processID == libcontainerdtypes.InitProcessName {
  563. return t, nil
  564. }
  565. p := ctr.getProcess(processID)
  566. if p == nil {
  567. return nil, errors.WithStack(errdefs.NotFound(errors.New("no such exec")))
  568. }
  569. return p, nil
  570. }
  571. // createIO creates the io to be used by a process
  572. // This needs to get a pointer to interface as upon closure the process may not have yet been registered
  573. func (c *client) createIO(fifos *cio.FIFOSet, containerID, processID string, stdinCloseSync chan struct{}, attachStdio libcontainerdtypes.StdioCallback) (cio.IO, error) {
  574. var (
  575. io *cio.DirectIO
  576. err error
  577. )
  578. io, err = c.newDirectIO(context.Background(), fifos)
  579. if err != nil {
  580. return nil, err
  581. }
  582. if io.Stdin != nil {
  583. var (
  584. err error
  585. stdinOnce sync.Once
  586. )
  587. pipe := io.Stdin
  588. io.Stdin = ioutils.NewWriteCloserWrapper(pipe, func() error {
  589. stdinOnce.Do(func() {
  590. err = pipe.Close()
  591. // Do the rest in a new routine to avoid a deadlock if the
  592. // Exec/Start call failed.
  593. go func() {
  594. <-stdinCloseSync
  595. p, err := c.getProcess(containerID, processID)
  596. if err == nil {
  597. err = p.CloseIO(context.Background(), containerd.WithStdinCloser)
  598. if err != nil && strings.Contains(err.Error(), "transport is closing") {
  599. err = nil
  600. }
  601. }
  602. }()
  603. })
  604. return err
  605. })
  606. }
  607. rio, err := attachStdio(io)
  608. if err != nil {
  609. io.Cancel()
  610. io.Close()
  611. }
  612. return rio, err
  613. }
  614. func (c *client) processEvent(ctr *container, et libcontainerdtypes.EventType, ei libcontainerdtypes.EventInfo) {
  615. c.eventQ.Append(ei.ContainerID, func() {
  616. err := c.backend.ProcessEvent(ei.ContainerID, et, ei)
  617. if err != nil {
  618. c.logger.WithError(err).WithFields(logrus.Fields{
  619. "container": ei.ContainerID,
  620. "event": et,
  621. "event-info": ei,
  622. }).Error("failed to process event")
  623. }
  624. if et == libcontainerdtypes.EventExit && ei.ProcessID != ei.ContainerID {
  625. p := ctr.getProcess(ei.ProcessID)
  626. if p == nil {
  627. c.logger.WithError(errors.New("no such process")).
  628. WithFields(logrus.Fields{
  629. "container": ei.ContainerID,
  630. "process": ei.ProcessID,
  631. }).Error("exit event")
  632. return
  633. }
  634. _, err = p.Delete(context.Background())
  635. if err != nil {
  636. c.logger.WithError(err).WithFields(logrus.Fields{
  637. "container": ei.ContainerID,
  638. "process": ei.ProcessID,
  639. }).Warn("failed to delete process")
  640. }
  641. ctr.deleteProcess(ei.ProcessID)
  642. ctr := c.getContainer(ei.ContainerID)
  643. if ctr == nil {
  644. c.logger.WithFields(logrus.Fields{
  645. "container": ei.ContainerID,
  646. }).Error("failed to find container")
  647. } else {
  648. newFIFOSet(ctr.bundleDir, ei.ProcessID, true, false).Close()
  649. }
  650. }
  651. })
  652. }
  653. func (c *client) processEventStream(ctx context.Context, ns string) {
  654. var (
  655. err error
  656. ev *events.Envelope
  657. et libcontainerdtypes.EventType
  658. ei libcontainerdtypes.EventInfo
  659. ctr *container
  660. )
  661. // Filter on both namespace *and* topic. To create an "and" filter,
  662. // this must be a single, comma-separated string
  663. eventStream, errC := c.client.EventService().Subscribe(ctx, "namespace=="+ns+",topic~=|^/tasks/|")
  664. c.logger.Debug("processing event stream")
  665. var oomKilled bool
  666. for {
  667. select {
  668. case err = <-errC:
  669. if err != nil {
  670. errStatus, ok := status.FromError(err)
  671. if !ok || errStatus.Code() != codes.Canceled {
  672. c.logger.WithError(err).Error("failed to get event")
  673. go c.processEventStream(ctx, ns)
  674. } else {
  675. c.logger.WithError(ctx.Err()).Info("stopping event stream following graceful shutdown")
  676. }
  677. }
  678. return
  679. case ev = <-eventStream:
  680. if ev.Event == nil {
  681. c.logger.WithField("event", ev).Warn("invalid event")
  682. continue
  683. }
  684. v, err := typeurl.UnmarshalAny(ev.Event)
  685. if err != nil {
  686. c.logger.WithError(err).WithField("event", ev).Warn("failed to unmarshal event")
  687. continue
  688. }
  689. c.logger.WithField("topic", ev.Topic).Debug("event")
  690. switch t := v.(type) {
  691. case *apievents.TaskCreate:
  692. et = libcontainerdtypes.EventCreate
  693. ei = libcontainerdtypes.EventInfo{
  694. ContainerID: t.ContainerID,
  695. ProcessID: t.ContainerID,
  696. Pid: t.Pid,
  697. }
  698. case *apievents.TaskStart:
  699. et = libcontainerdtypes.EventStart
  700. ei = libcontainerdtypes.EventInfo{
  701. ContainerID: t.ContainerID,
  702. ProcessID: t.ContainerID,
  703. Pid: t.Pid,
  704. }
  705. case *apievents.TaskExit:
  706. et = libcontainerdtypes.EventExit
  707. ei = libcontainerdtypes.EventInfo{
  708. ContainerID: t.ContainerID,
  709. ProcessID: t.ID,
  710. Pid: t.Pid,
  711. ExitCode: t.ExitStatus,
  712. ExitedAt: t.ExitedAt,
  713. }
  714. case *apievents.TaskOOM:
  715. et = libcontainerdtypes.EventOOM
  716. ei = libcontainerdtypes.EventInfo{
  717. ContainerID: t.ContainerID,
  718. OOMKilled: true,
  719. }
  720. oomKilled = true
  721. case *apievents.TaskExecAdded:
  722. et = libcontainerdtypes.EventExecAdded
  723. ei = libcontainerdtypes.EventInfo{
  724. ContainerID: t.ContainerID,
  725. ProcessID: t.ExecID,
  726. }
  727. case *apievents.TaskExecStarted:
  728. et = libcontainerdtypes.EventExecStarted
  729. ei = libcontainerdtypes.EventInfo{
  730. ContainerID: t.ContainerID,
  731. ProcessID: t.ExecID,
  732. Pid: t.Pid,
  733. }
  734. case *apievents.TaskPaused:
  735. et = libcontainerdtypes.EventPaused
  736. ei = libcontainerdtypes.EventInfo{
  737. ContainerID: t.ContainerID,
  738. }
  739. case *apievents.TaskResumed:
  740. et = libcontainerdtypes.EventResumed
  741. ei = libcontainerdtypes.EventInfo{
  742. ContainerID: t.ContainerID,
  743. }
  744. default:
  745. c.logger.WithFields(logrus.Fields{
  746. "topic": ev.Topic,
  747. "type": reflect.TypeOf(t)},
  748. ).Info("ignoring event")
  749. continue
  750. }
  751. ctr = c.getContainer(ei.ContainerID)
  752. if ctr == nil {
  753. c.logger.WithField("container", ei.ContainerID).Warn("unknown container")
  754. continue
  755. }
  756. if oomKilled {
  757. ctr.setOOMKilled(true)
  758. oomKilled = false
  759. }
  760. ei.OOMKilled = ctr.getOOMKilled()
  761. c.processEvent(ctr, et, ei)
  762. }
  763. }
  764. }
  765. func (c *client) writeContent(ctx context.Context, mediaType, ref string, r io.Reader) (*types.Descriptor, error) {
  766. writer, err := c.client.ContentStore().Writer(ctx, content.WithRef(ref))
  767. if err != nil {
  768. return nil, err
  769. }
  770. defer writer.Close()
  771. size, err := io.Copy(writer, r)
  772. if err != nil {
  773. return nil, err
  774. }
  775. labels := map[string]string{
  776. "containerd.io/gc.root": time.Now().UTC().Format(time.RFC3339),
  777. }
  778. if err := writer.Commit(ctx, 0, "", content.WithLabels(labels)); err != nil {
  779. return nil, err
  780. }
  781. return &types.Descriptor{
  782. MediaType: mediaType,
  783. Digest: writer.Digest(),
  784. Size_: size,
  785. }, nil
  786. }
  787. func wrapError(err error) error {
  788. switch {
  789. case err == nil:
  790. return nil
  791. case containerderrors.IsNotFound(err):
  792. return errdefs.NotFound(err)
  793. }
  794. msg := err.Error()
  795. for _, s := range []string{"container does not exist", "not found", "no such container"} {
  796. if strings.Contains(msg, s) {
  797. return errdefs.NotFound(err)
  798. }
  799. }
  800. return err
  801. }