Skip to content

fix: return tty size in container_inspect.go#28579

Open
priyanshsao wants to merge 1 commit intocontainers:mainfrom
priyanshsao:inspect-tty-size
Open

fix: return tty size in container_inspect.go#28579
priyanshsao wants to merge 1 commit intocontainers:mainfrom
priyanshsao:inspect-tty-size

Conversation

@priyanshsao
Copy link
Copy Markdown

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 tty size for containers which have terminal enabled and are in running state.
For cases where there is an issue with getting the size or the container is not running, suitable warning is given, no update happens and default [0,0] is returned.

Fixes: #28368

Checklist

Ensure you have completed the following checklist for your pull request to be reviewed:

  • Certify you wrote the patch or otherwise have the right to pass it on as an open-source patch by signing all
    commits. (git commit -s). (If needed, use git commit -s --amend). The author email must match
    the sign-off email address. See CONTRIBUTING.md
    for more information.
  • Referenced issues using Fixes: #00000 in commit message (if applicable)
  • Tests have been added/updated (or no tests are needed)
  • Documentation has been updated (or no documentation changes are needed)
  • All commits pass make validatepr (format/lint checks)
  • Release note entered in the section below (or None if no user-facing changes)

Does this PR introduce a user-facing change?

Fix podman inspect to return console size of a running container.

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 tty size for containers which have terminal enabled and are in running state.
For cases where there is an issue with getting the size or the container is not running, suitable warning is given, no update happens and default [0,0] is returned.

Fixes: containers#28368

Signed-off-by: Priyansh Sao <saopriyansh06@gmail.com>
@priyanshsao
Copy link
Copy Markdown
Author

A simple video to see the change quickly

podman_tty.mp4

@packit-as-a-service
Copy link
Copy Markdown

tmt tests failed for commit 8f5a4a2. @lsm5, @psss, @thrix please check.

Copy link
Copy Markdown
Member

@danishprakash danishprakash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please also add a test case for this?

} else {
defer f.Close()

height, width, err := xterm.GetSize(int(f.Fd()))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetSize returns (width, height), so the size is flipped.

Comment on lines +641 to +643
procPath := fmt.Sprintf("/proc/%d/fd/0", c.state.PID)

f, err := os.Open(procPath)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That path doesn't exist on FreeBSD, so this would always fail. We could either guard this for Linux or even split this into *_freebsd.go. Bottom line, the behavior on FreeBSD won't change (podman will still return [0,0]), but there's going to be an additional warning, which is superfluous considering we know this won't work.

f, err := os.Open(procPath)
if err != nil {
if c.state.State != define.ContainerStateRunning {
logrus.Warnf("container %q is not running, unable to get console size", c.ID())
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me, this seems to be noise.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe debug or exit with an error?

"go.podman.io/podman/v6/libpod/driver"
"go.podman.io/podman/v6/pkg/signal"
"go.podman.io/podman/v6/pkg/util"
xterm "golang.org/x/term"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
xterm "golang.org/x/term"
"golang.org/x/term"

if c.state.State != define.ContainerStateRunning {
logrus.Warnf("container %q is not running, unable to get console size", c.ID())
} else {
logrus.Warnf("unable to open %s", procPath)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lack of context here might confuse more than it would help, consider:

Suggested change
logrus.Warnf("unable to open %s", procPath)
logrus.Debugf("unable to get console size for container %s: %v", c.ID(), err)

if c.Terminal() {
procPath := fmt.Sprintf("/proc/%d/fd/0", c.state.PID)

f, err := os.Open(procPath)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the container doesn't run, and some other process gets that PID?

f, err := os.Open(procPath)
if err != nil {
if c.state.State != define.ContainerStateRunning {
logrus.Warnf("container %q is not running, unable to get console size", c.ID())
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe debug or exit with an error?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

podman inspect does not return size of container terminal

3 participants