resize.go 1.7 KB

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