Skip to content
This repository was archived by the owner on Apr 3, 2018. It is now read-only.

Commit 911e469

Browse files
author
Julio Montes
committed
agent: implement ps command
ps command is used to list the processes running inside the container, this command is called by ```docker top``` partially fixes clearcontainers/runtime#95 Signed-off-by: Julio Montes <julio.montes@intel.com>
1 parent 6abb9c6 commit 911e469

9 files changed

Lines changed: 98 additions & 5 deletions

File tree

agent.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,7 @@ type agent interface {
146146
// container related to a Pod. If all is true, all processes in
147147
// the container will be sent the signal.
148148
killContainer(pod Pod, c Container, signal syscall.Signal, all bool) error
149+
150+
// psContainer will list the processes running inside the container
151+
psContainer(pod Pod, c Container, format string, psArgs []string) ([]byte, error)
149152
}

api.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,3 +660,34 @@ func PausePod(podID string) (*Pod, error) {
660660
func ResumePod(podID string) (*Pod, error) {
661661
return togglePausePod(podID, false)
662662
}
663+
664+
// PsContainer is the virtcontainers entry point to list
665+
// processes running inside a container
666+
func PsContainer(podID, containerID string, format string, psArgs []string) ([]byte, error) {
667+
if podID == "" {
668+
return nil, errNeedPodID
669+
}
670+
671+
if containerID == "" {
672+
return nil, errNeedContainerID
673+
}
674+
675+
lockFile, err := lockPod(podID)
676+
if err != nil {
677+
return nil, err
678+
}
679+
defer unlockPod(lockFile)
680+
681+
p, err := fetchPod(podID)
682+
if err != nil {
683+
return nil, err
684+
}
685+
686+
// Fetch the container.
687+
c, err := fetchContainer(p, containerID)
688+
if err != nil {
689+
return nil, err
690+
}
691+
692+
return c.ps(format, psArgs)
693+
}

cc_proxy.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,5 @@ func (p *ccProxy) sendCmd(cmd interface{}) (interface{}, error) {
207207
tokens = append(tokens, proxyCmd.token)
208208
}
209209

210-
if _, err := p.client.HyperWithTokens(proxyCmd.cmd, tokens, proxyCmd.message); err != nil {
211-
return nil, err
212-
}
213-
214-
return nil, nil
210+
return p.client.HyperWithTokens(proxyCmd.cmd, tokens, proxyCmd.message)
215211
}

container.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,24 @@ func (c *Container) kill(signal syscall.Signal, all bool) error {
607607
return nil
608608
}
609609

610+
func (c *Container) ps(format string, psArgs []string) ([]byte, error) {
611+
state, err := c.fetchState("ps")
612+
if err != nil {
613+
return nil, err
614+
}
615+
616+
if state.State != StateRunning {
617+
return nil, fmt.Errorf("Container not running, impossible to list processes")
618+
}
619+
620+
if _, _, err := c.pod.proxy.connect(*(c.pod), false); err != nil {
621+
return nil, err
622+
}
623+
defer c.pod.proxy.disconnect()
624+
625+
return c.pod.agent.psContainer(*(c.pod), *c, format, psArgs)
626+
}
627+
610628
func (c *Container) createShimProcess(token, url string, cmd Cmd) (*Process, error) {
611629
if c.pod.state.URL != url {
612630
return &Process{}, fmt.Errorf("Pod URL %s and URL from proxy %s MUST be identical", c.pod.state.URL, url)

hyperstart.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,3 +640,27 @@ func (h *hyper) killOneContainer(cID string, signal syscall.Signal, all bool) er
640640

641641
return nil
642642
}
643+
644+
func (h *hyper) psContainer(pod Pod, c Container, format string, psArgs []string) ([]byte, error) {
645+
return h.psOneContainer(pod.id, c.id, format, psArgs)
646+
}
647+
648+
func (h *hyper) psOneContainer(podID, cID string, format string, psArgs []string) ([]byte, error) {
649+
psCmd := hyperstart.PsCommand{
650+
Container: cID,
651+
Format: format,
652+
PsArgs: psArgs,
653+
}
654+
655+
proxyCmd := hyperstartProxyCmd{
656+
cmd: hyperstart.PsContainer,
657+
message: psCmd,
658+
}
659+
660+
response, err := h.proxy.sendCmd(proxyCmd)
661+
if msg, ok := response.([]byte); ok {
662+
return msg, err
663+
}
664+
665+
return nil, fmt.Errorf("failed to get response message")
666+
}

noop_agent.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,8 @@ func (n *noopAgent) stopContainer(pod Pod, c Container) error {
7474
func (n *noopAgent) killContainer(pod Pod, c Container, signal syscall.Signal, all bool) error {
7575
return nil
7676
}
77+
78+
// psContainer is the Noop agent Container ps implementation. It does nothing.
79+
func (n *noopAgent) psContainer(pod Pod, c Container, format string, psArgs []string) ([]byte, error) {
80+
return nil, nil
81+
}

pkg/hyperstart/hyperstart.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const (
5050
SetupInterface = "setupinterface"
5151
SetupRoute = "setuproute"
5252
RemoveContainer = "removecontainer"
53+
PsContainer = "pscontainer"
5354
)
5455

5556
// CodeList is the map making the relation between a string command
@@ -73,6 +74,7 @@ var CodeList = map[string]uint32{
7374
SetupInterface: SetupInterfaceCode,
7475
SetupRoute: SetupRouteCode,
7576
RemoveContainer: RemoveContainerCode,
77+
PsContainer: PsContainerCode,
7678
}
7779

7880
// Values related to the communication on control channel.

pkg/hyperstart/types.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const (
4545
SetupInterfaceCode
4646
SetupRouteCode
4747
RemoveContainerCode
48+
PsContainerCode
4849
ProcessAsyncEventCode
4950
)
5051

@@ -76,6 +77,14 @@ type RemoveCommand struct {
7677
Container string `json:"container"`
7778
}
7879

80+
// PsCommand is the structure corresponding to the format expected by
81+
// hyperstart to list processes of a container on the guest.
82+
type PsCommand struct {
83+
Container string `json:"container"`
84+
Format string `json:"format"`
85+
PsArgs []string `json:"psargs"`
86+
}
87+
7988
// PAECommand is the structure hyperstart can expects to
8089
// receive after a process has been started/executed on a container.
8190
type PAECommand struct {

sshd.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,8 @@ func (s *sshd) stopContainer(pod Pod, c Container) error {
203203
func (s *sshd) killContainer(pod Pod, c Container, signal syscall.Signal, all bool) error {
204204
return nil
205205
}
206+
207+
// psContainer is the agent Container ps implementation for sshd.
208+
func (s *sshd) psContainer(pod Pod, c Container, format string, psArgs []string) ([]byte, error) {
209+
return nil, nil
210+
}

0 commit comments

Comments
 (0)