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