From 4728ce1cc2e63dd070ee295c29a6a2e70e6e54f0 Mon Sep 17 00:00:00 2001 From: Priyansh Sao Date: Wed, 6 May 2026 11:31:08 +0000 Subject: [PATCH] fix: return tty size in container_inspect_linux.go In the output of podman inspect, there is a field for the container's TTY size (ConsoleSize), which Podman presently only returns [0,0]. This PR updates the console size field for containers with terminal enabled. For cases where there is an issue with getting the size, suitable debug information is given, no update happens and default [0,0] is returned. This implementation is only for Linux systems. For FreeBSD, the console size is not implemented yet so, adds a todo for that. Fixes: #28368 Signed-off-by: Priyansh Sao --- libpod/container_inspect.go | 6 ------ libpod/container_inspect_freebsd.go | 6 ++++++ libpod/container_inspect_linux.go | 25 +++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 6 deletions(-) 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 }