Skip to content

Commit

Permalink
Add shimdiag flag to find shim process ID for shims
Browse files Browse the repository at this point in the history
* Adds a new -pids flag to the `shimdiag list` command that will print out the
process ID of the shim executable.

Sample output:

PS C:\> shimdiag.exe list -pids
Shim                                                                     Pid
k8s.io-3719754aab39925924b0beb27d2a195a4a6784c6ebe3690b81b2e7274a7021af  65892
k8s.io-908ea3697874891fd5814b1967515d3214257b65fc1974bdafd17276ee8ba3e5  69444

Signed-off-by: Daniel Canter <[email protected]>
  • Loading branch information
dcantah committed Dec 17, 2020
1 parent f5ee97d commit 013d310
Show file tree
Hide file tree
Showing 4 changed files with 422 additions and 34 deletions.
16 changes: 16 additions & 0 deletions cmd/containerd-shim-runhcs-v1/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"os"
"runtime"
"strings"
"sync"
Expand Down Expand Up @@ -474,3 +475,18 @@ func (s *service) DiagStacks(ctx context.Context, req *shimdiag.StacksRequest) (
}
return resp, nil
}

func (s *service) DiagPid(ctx context.Context, req *shimdiag.PidRequest) (*shimdiag.PidResponse, error) {
if s == nil {
return nil, nil
}
defer panicRecover()
ctx, span := trace.StartSpan(ctx, "DiagPid")
defer span.End()

span.AddAttributes(trace.StringAttribute("tid", s.tid))

return &shimdiag.PidResponse{
Pid: int32(os.Getpid()),
}, nil
}
48 changes: 46 additions & 2 deletions cmd/shimdiag/list.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,69 @@
package main

import (
"context"
"fmt"
"os"
"text/tabwriter"

"github.com/Microsoft/hcsshim/internal/appargs"
"github.com/Microsoft/hcsshim/internal/shimdiag"
"github.com/urfave/cli"
)

var listCommand = cli.Command{
Name: "list",
Usage: "Lists running shims",
ArgsUsage: " ",
Before: appargs.Validate(),
Flags: []cli.Flag{
cli.BoolFlag{
Name: "pids",
Usage: "Shows the process IDs of each shim",
},
},
Before: appargs.Validate(),
Action: func(ctx *cli.Context) error {
pids := ctx.Bool("pids")
shims, err := findShims("")
if err != nil {
return err
}

getPid := func(shimStr string) (int32, error) {
shim, err := getShim(shimStr)
if err != nil {
return 0, err
}
defer shim.Close()

svc := shimdiag.NewShimDiagClient(shim)
resp, err := svc.DiagPid(context.Background(), &shimdiag.PidRequest{})
if err != nil {
return 0, err
}
return resp.Pid, nil
}

w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 8, 0, '\t', 0)

if pids {
fmt.Fprintln(w, "Shim \t Pid")
}

for _, shim := range shims {
fmt.Println(shim)
output := shim
if pids {
pid, err := getPid(shim)
if err != nil {
return err
}
fmt.Fprintf(w, "%s \t %d\n", output, pid)
} else {
fmt.Println(shim)
}
}
w.Flush()
return nil
},
}
Loading

0 comments on commit 013d310

Please sign in to comment.