resize.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package daemon // import "github.com/docker/docker/daemon"
  2. import (
  3. "context"
  4. "errors"
  5. "strconv"
  6. "time"
  7. )
  8. // ContainerResize changes the size of the TTY of the process running
  9. // in the container with the given name to the given height and width.
  10. func (daemon *Daemon) ContainerResize(name string, height, width int) error {
  11. container, err := daemon.GetContainer(name)
  12. if err != nil {
  13. return err
  14. }
  15. container.Lock()
  16. tsk, err := container.GetRunningTask()
  17. container.Unlock()
  18. if err != nil {
  19. return err
  20. }
  21. if err = tsk.Resize(context.Background(), uint32(width), uint32(height)); err == nil {
  22. attributes := map[string]string{
  23. "height": strconv.Itoa(height),
  24. "width": strconv.Itoa(width),
  25. }
  26. daemon.LogContainerEventWithAttributes(container, "resize", attributes)
  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. return ec.Process.Resize(context.Background(), uint32(width), uint32(height))
  45. case <-timeout.C:
  46. return errors.New("timeout waiting for exec session ready")
  47. }
  48. }