diff --git a/libpod/container_inspect.go b/libpod/container_inspect.go index d0730eacf37..628233e173a 100644 --- a/libpod/container_inspect.go +++ b/libpod/container_inspect.go @@ -630,12 +630,6 @@ func (c *Container) generateInspectContainerHostConfig(ctrSpec *spec.Spec, named } } - // Terminal size - // We can't actually get this for now... - // So default to something sane. - // TODO: Populate this. - hostConfig.ConsoleSize = []uint{0, 0} - return hostConfig, nil } diff --git a/libpod/container_inspect_freebsd.go b/libpod/container_inspect_freebsd.go index 2d42e5d6ba8..e9d86a6e1c1 100644 --- a/libpod/container_inspect_freebsd.go +++ b/libpod/container_inspect_freebsd.go @@ -24,5 +24,11 @@ func (c *Container) platformInspectContainerHostConfig(ctrSpec *spec.Spec, hostC return err } + // Console size + // We can't actually get this for now... + // So default to something sane. + // TODO: Populate this. + hostConfig.ConsoleSize = []uint{0, 0} + return nil } diff --git a/libpod/container_inspect_linux.go b/libpod/container_inspect_linux.go index 12bca74f876..de34e314e0e 100644 --- a/libpod/container_inspect_linux.go +++ b/libpod/container_inspect_linux.go @@ -4,6 +4,7 @@ package libpod import ( "fmt" + "os" "sort" "strings" @@ -14,6 +15,7 @@ import ( "go.podman.io/podman/v6/libpod/define" "go.podman.io/podman/v6/pkg/util" "go.podman.io/storage/types" + "golang.org/x/term" ) func (c *Container) platformInspectContainerHostConfig(ctrSpec *spec.Spec, hostConfig *define.InspectContainerHostConfig) error { @@ -306,6 +308,29 @@ func (c *Container) platformInspectContainerHostConfig(ctrSpec *spec.Spec, hostC return err } + // ConsoleSize. + // Default to [0,0] if we can't get it for some reason, + // or if the container doesn't have a TTY. + hostConfig.ConsoleSize = []uint{0, 0} + + if c.Terminal() { + procPath := fmt.Sprintf("/proc/%d/fd/0", c.state.PID) + + f, err := os.Open(procPath) + if err != nil { + logrus.Debugf("unable to get console size for container %s: %v", c.ID(), err) + } else { + defer f.Close() + + width, height, err := term.GetSize(int(f.Fd())) + if err != nil { + logrus.Debugf("could not get terminal size: %v", err) + } else { + hostConfig.ConsoleSize = []uint{uint(width), uint(height)} + } + } + } + return nil }