Skip to content

Commit

Permalink
top: parse ps(1) args correctly
Browse files Browse the repository at this point in the history
The arguments of ps(1) should be shlexed.

Fixes: containers#12452
Signed-off-by: Valentin Rothberg <[email protected]>
  • Loading branch information
vrothberg committed Dec 1, 2021
1 parent 8de68b1 commit e2b3447
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
18 changes: 17 additions & 1 deletion libpod/container_top_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ package libpod

import (
"bufio"
"fmt"
"os"
"strconv"
"strings"

"github.com/containers/podman/v3/libpod/define"
"github.com/containers/podman/v3/pkg/rootless"
"github.com/containers/psgo"
"github.com/google/shlex"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -51,7 +53,21 @@ func (c *Container) Top(descriptors []string) ([]string, error) {
return nil, psgoErr
}

output, err = c.execPS(descriptors)
// Note that the descriptors to ps(1) must be shlexed (see #12452).
psDescriptors := []string{}
for _, d := range descriptors {
shSplit, err := shlex.Split(d)
if err != nil {
return nil, fmt.Errorf("parsing ps args: %v", err)
}
for _, s := range shSplit {
if s != "" {
psDescriptors = append(psDescriptors, s)
}
}
}

output, err = c.execPS(psDescriptors)
if err != nil {
return nil, errors.Wrapf(err, "error executing ps(1) in the container")
}
Expand Down
5 changes: 5 additions & 0 deletions test/e2e/top_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ var _ = Describe("Podman top", func() {
result.WaitWithDefaultTimeout()
Expect(result).Should(Exit(0))
Expect(len(result.OutputToStringArray())).To(BeNumerically(">", 1))

result = podmanTest.Podman([]string{"top", session.OutputToString(), "ax -o args"})
result.WaitWithDefaultTimeout()
Expect(result).Should(Exit(0))
Expect(result.OutputToStringArray()).To(Equal([]string{"COMMAND", "top -d 2"}))
})

It("podman top with comma-separated options", func() {
Expand Down

0 comments on commit e2b3447

Please sign in to comment.