mirror of
https://github.com/containers/podman.git
synced 2025-07-03 17:27:18 +08:00
top: parse ps(1) args correctly
The arguments of ps(1) should be shlexed. Fixes: #12452 Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
This commit is contained in:
@ -4,6 +4,7 @@ package libpod
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -11,6 +12,7 @@ import (
|
|||||||
"github.com/containers/podman/v3/libpod/define"
|
"github.com/containers/podman/v3/libpod/define"
|
||||||
"github.com/containers/podman/v3/pkg/rootless"
|
"github.com/containers/podman/v3/pkg/rootless"
|
||||||
"github.com/containers/psgo"
|
"github.com/containers/psgo"
|
||||||
|
"github.com/google/shlex"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
@ -51,7 +53,21 @@ func (c *Container) Top(descriptors []string) ([]string, error) {
|
|||||||
return nil, psgoErr
|
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 {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(err, "error executing ps(1) in the container")
|
return nil, errors.Wrapf(err, "error executing ps(1) in the container")
|
||||||
}
|
}
|
||||||
|
@ -101,6 +101,11 @@ var _ = Describe("Podman top", func() {
|
|||||||
result.WaitWithDefaultTimeout()
|
result.WaitWithDefaultTimeout()
|
||||||
Expect(result).Should(Exit(0))
|
Expect(result).Should(Exit(0))
|
||||||
Expect(len(result.OutputToStringArray())).To(BeNumerically(">", 1))
|
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() {
|
It("podman top with comma-separated options", func() {
|
||||||
|
Reference in New Issue
Block a user