Skip to content

Commit

Permalink
rootless: fix pod top
Browse files Browse the repository at this point in the history
we need to join the namespace of the target pod.

Closes: containers#2682

Signed-off-by: Giuseppe Scrivano <[email protected]>
  • Loading branch information
giuseppe committed Mar 18, 2019
1 parent 8aed32a commit 46b74a8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/podman/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ var cmdsNotRequiringRootless = map[*cobra.Command]bool{
_podKillCommand: true,
_podStatsCommand: true,
_podStopCommand: true,
_podTopCommand: true,
_restartCommand: true,
_rmCommand: true,
_runCommand: true,
Expand Down
26 changes: 26 additions & 0 deletions cmd/podman/pod_top.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/libpod"
"github.com/containers/libpod/pkg/rootless"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -53,6 +54,10 @@ func podTopCmd(c *cliconfig.PodTopValues) error {
)
args := c.InputArgs

if os.Geteuid() != 0 {
rootless.SetSkipStorageSetup(true)
}

if c.ListDescriptors {
descriptors, err := libpod.GetContainerPidInformationDescriptors()
if err != nil {
Expand All @@ -77,6 +82,27 @@ func podTopCmd(c *cliconfig.PodTopValues) error {
} else {
descriptors = args[1:]
}

if os.Geteuid() != 0 {
var pod *adapter.Pod
var err error
if c.Latest {
pod, err = runtime.GetLatestPod()
} else {
pod, err = runtime.LookupPod(c.InputArgs[0])
}
if err != nil {
return errors.Wrapf(err, "unable to lookup requested container")
}
became, ret, err := runtime.JoinOrCreateRootlessPod(pod)
if err != nil {
return err
}
if became {
os.Exit(ret)
}
}

w := tabwriter.NewWriter(os.Stdout, 5, 1, 3, ' ', 0)
psOutput, err := runtime.PodTop(c, descriptors)
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions test/e2e/rootless_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,21 @@ var _ = Describe("Podman rootless", func() {
cmd.WaitWithDefaultTimeout()
Expect(cmd.ExitCode()).To(Equal(0))
Expect(cmd.LineInOutputContains("hello")).To(BeTrue())

args = []string{"pod", "top", podId}
cmd = rootlessTest.PodmanAsUser(args, 1000, 1000, "", env)
cmd.WaitWithDefaultTimeout()
Expect(cmd.ExitCode()).To(Not(Equal(0)))

args = []string{"run", "--pod", podId, "-d", "--rootfs", mountPath, "sleep", "100"}
cmd = rootlessTest.PodmanAsUser(args, 1000, 1000, "", env)
cmd.WaitWithDefaultTimeout()
Expect(cmd.ExitCode()).To(Equal(0))

args = []string{"pod", "top", podId}
cmd = rootlessTest.PodmanAsUser(args, 1000, 1000, "", env)
cmd.WaitWithDefaultTimeout()
Expect(cmd.ExitCode()).To(Equal(0))
}
runInRootlessContext(f)
})
Expand Down

0 comments on commit 46b74a8

Please sign in to comment.