client.go 24 KB

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