add support to kill container process by pid

This adds support for signalling a container process by pid.

Signed-off-by: Evan Hazlett <ejhazlett@gmail.com>

make Ps more extensible

Signed-off-by: Evan Hazlett <ejhazlett@gmail.com>

ps: windows support

Signed-off-by: Evan Hazlett <ejhazlett@gmail.com>
This commit is contained in:
Evan Hazlett
2017-05-09 15:50:02 -04:00
parent fae11b6673
commit ef158f8b5e
14 changed files with 1523 additions and 145 deletions

View File

@@ -16,6 +16,11 @@ var killCommand = cli.Command{
Name: "id",
Usage: "id of the container",
},
cli.IntFlag{
Name: "pid",
Usage: "pid to kill",
Value: 0,
},
cli.BoolFlag{
Name: "all, a",
Usage: "send signal to all processes inside the container",
@@ -37,14 +42,29 @@ var killCommand = cli.Command{
return err
}
containers, err := getExecutionService(context)
if err != nil {
return err
pid := context.Int("pid")
all := context.Bool("all")
if pid > 0 && all {
return errors.New("enter a pid or all; not both")
}
killRequest := &execution.KillRequest{
ID: id,
Signal: uint32(signal),
All: context.Bool("all"),
PidOrAll: &execution.KillRequest_Pid{
Pid: uint32(pid),
},
}
if all {
killRequest.PidOrAll = &execution.KillRequest_All{
All: true,
}
}
containers, err := getExecutionService(context)
if err != nil {
return err
}
_, err = containers.Kill(gocontext.Background(), killRequest)
if err != nil {

View File

@@ -63,6 +63,7 @@ containerd client
resumeCommand,
snapshotCommand,
versionCommand,
psCommand,
}
app.Commands = append(app.Commands, extraCmds...)
app.Before = func(context *cli.Context) error {

58
cmd/ctr/ps.go Normal file
View File

@@ -0,0 +1,58 @@
package main
import (
gocontext "context"
"fmt"
"os"
"text/tabwriter"
"github.com/containerd/containerd/api/services/execution"
"github.com/pkg/errors"
"github.com/urfave/cli"
)
var psCommand = cli.Command{
Name: "ps",
Usage: "list processes for container",
Flags: []cli.Flag{
cli.StringFlag{
Name: "id",
Usage: "id of the container",
},
},
Action: func(context *cli.Context) error {
id := context.String("id")
if id == "" {
return errors.New("container id must be provided")
}
psRequest := &execution.PsRequest{
ID: id,
}
containers, err := getExecutionService(context)
if err != nil {
return err
}
resp, err := containers.Ps(gocontext.Background(), psRequest)
if err != nil {
return err
}
w := tabwriter.NewWriter(os.Stdout, 10, 1, 3, ' ', 0)
fmt.Fprintln(w, "PID")
for _, ps := range resp.Ps {
if _, err := fmt.Fprintf(w, "%d\n",
ps.Pid,
); err != nil {
return err
}
if err := w.Flush(); err != nil {
return err
}
}
return nil
},
}

View File

@@ -117,7 +117,9 @@ func forwardAllSignals(containers execution.ContainerServiceClient, id string) c
killRequest := &execution.KillRequest{
ID: id,
Signal: uint32(s.(syscall.Signal)),
All: false,
PidOrAll: &execution.KillRequest_All{
All: false,
},
}
_, err := containers.Kill(gocontext.Background(), killRequest)
if err != nil {