Skip to content

Commit 4728ce1

Browse files
committed
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 <saopriyansh06@gmail.com>
1 parent 6c49daf commit 4728ce1

3 files changed

Lines changed: 31 additions & 6 deletions

File tree

libpod/container_inspect.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -630,12 +630,6 @@ func (c *Container) generateInspectContainerHostConfig(ctrSpec *spec.Spec, named
630630
}
631631
}
632632

633-
// Terminal size
634-
// We can't actually get this for now...
635-
// So default to something sane.
636-
// TODO: Populate this.
637-
hostConfig.ConsoleSize = []uint{0, 0}
638-
639633
return hostConfig, nil
640634
}
641635

libpod/container_inspect_freebsd.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,11 @@ func (c *Container) platformInspectContainerHostConfig(ctrSpec *spec.Spec, hostC
2424
return err
2525
}
2626

27+
// Console size
28+
// We can't actually get this for now...
29+
// So default to something sane.
30+
// TODO: Populate this.
31+
hostConfig.ConsoleSize = []uint{0, 0}
32+
2733
return nil
2834
}

libpod/container_inspect_linux.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package libpod
44

55
import (
66
"fmt"
7+
"os"
78
"sort"
89
"strings"
910

@@ -14,6 +15,7 @@ import (
1415
"go.podman.io/podman/v6/libpod/define"
1516
"go.podman.io/podman/v6/pkg/util"
1617
"go.podman.io/storage/types"
18+
"golang.org/x/term"
1719
)
1820

1921
func (c *Container) platformInspectContainerHostConfig(ctrSpec *spec.Spec, hostConfig *define.InspectContainerHostConfig) error {
@@ -306,6 +308,29 @@ func (c *Container) platformInspectContainerHostConfig(ctrSpec *spec.Spec, hostC
306308
return err
307309
}
308310

311+
// ConsoleSize.
312+
// Default to [0,0] if we can't get it for some reason,
313+
// or if the container doesn't have a TTY.
314+
hostConfig.ConsoleSize = []uint{0, 0}
315+
316+
if c.Terminal() {
317+
procPath := fmt.Sprintf("/proc/%d/fd/0", c.state.PID)
318+
319+
f, err := os.Open(procPath)
320+
if err != nil {
321+
logrus.Debugf("unable to get console size for container %s: %v", c.ID(), err)
322+
} else {
323+
defer f.Close()
324+
325+
width, height, err := term.GetSize(int(f.Fd()))
326+
if err != nil {
327+
logrus.Debugf("could not get terminal size: %v", err)
328+
} else {
329+
hostConfig.ConsoleSize = []uint{uint(width), uint(height)}
330+
}
331+
}
332+
}
333+
309334
return nil
310335
}
311336

0 commit comments

Comments
 (0)