resize.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package daemon // import "github.com/docker/docker/daemon"
  2. import (
  3. "context"
  4. "errors"
  5. "strconv"
  6. "time"
  7. "github.com/docker/docker/api/types/events"
  8. "github.com/docker/docker/errdefs"
  9. )
  10. // ContainerResize changes the size of the TTY of the process running
  11. // in the container with the given name to the given height and width.
  12. func (daemon *Daemon) ContainerResize(name string, height, width int) error {
  13. container, err := daemon.GetContainer(name)
  14. if err != nil {
  15. return err
  16. }
  17. container.Lock()
  18. tsk, err := container.GetRunningTask()
  19. container.Unlock()
  20. if err != nil {
  21. return err
  22. }
  23. if err = tsk.Resize(context.Background(), uint32(width), uint32(height)); err == nil {
  24. daemon.LogContainerEventWithAttributes(container, events.ActionResize, map[string]string{
  25. "height": strconv.Itoa(height),
  26. "width": strconv.Itoa(width),
  27. })
  28. }
  29. return err
  30. }
  31. // ContainerExecResize changes the size of the TTY of the process
  32. // running in the exec with the given name to the given height and
  33. // width.
  34. func (daemon *Daemon) ContainerExecResize(name string, height, width int) error {
  35. ec, err := daemon.getExecConfig(name)
  36. if err != nil {
  37. return err
  38. }
  39. // TODO: the timeout is hardcoded here, it would be more flexible to make it
  40. // a parameter in resize request context, which would need API changes.
  41. timeout := time.NewTimer(10 * time.Second)
  42. defer timeout.Stop()
  43. select {
  44. case <-ec.Started:
  45. // An error may have occurred, so ec.Process may be nil.
  46. if ec.Process == nil {
  47. return errdefs.InvalidParameter(errors.New("exec process is not started"))
  48. }
  49. return ec.Process.Resize(context.Background(), uint32(width), uint32(height))
  50. case <-timeout.C:
  51. return errors.New("timeout waiting for exec session ready")
  52. }
  53. }