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/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. attributes := map[string]string{
  24. "height": strconv.Itoa(height),
  25. "width": strconv.Itoa(width),
  26. }
  27. daemon.LogContainerEventWithAttributes(container, "resize", attributes)
  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. }