Skip to content

Commit

Permalink
Add kill to RPCs in process module (#103)
Browse files Browse the repository at this point in the history
* Add kill to process to send signals to a given pid.

No support for wildcarding with negative pids/signals (too dangerous)

* Client side of kill

* Integration testing for kill.

shfmt also redid some lines
  • Loading branch information
sfc-gh-jchacon authored May 2, 2022
1 parent 4067a0c commit c570010
Show file tree
Hide file tree
Showing 8 changed files with 682 additions and 380 deletions.
43 changes: 43 additions & 0 deletions services/process/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func setup(f *flag.FlagSet) *subcommands.Commander {
c := client.SetupSubpackage(subPackage, f)
c.Register(&dumpCmd{}, "")
c.Register(&jstackCmd{}, "")
c.Register(&killCmd{}, "")
c.Register(&psCmd{}, "")
c.Register(&pstackCmd{}, "")
return c
Expand Down Expand Up @@ -195,6 +196,48 @@ func parseState(processState pb.ProcessState, codes []pb.ProcessStateCode) strin
return state
}

type killCmd struct {
pid uint64
signal uint
}

func (*killCmd) Name() string { return "kill" }
func (*killCmd) Synopsis() string { return "Send a signal to a process id." }
func (*killCmd) Usage() string {
return "kill: Send a signal to a process id.\n"
}

func (p *killCmd) SetFlags(f *flag.FlagSet) {
f.Uint64Var(&p.pid, "pid", 0, "Process ID to send signal")
f.UintVar(&p.signal, "signal", 0, "Signal to send process ID")
}

func (p *killCmd) Execute(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
state := args[0].(*util.ExecuteState)

c := pb.NewProcessClientProxy(state.Conn)
req := &pb.KillRequest{
Pid: p.pid,
Signal: uint32(p.signal),
}

respChan, err := c.KillOneMany(ctx, req)
if err != nil {
// Emit this to every error file as it's not specific to a given target.
for _, e := range state.Err {
fmt.Fprintf(e, "KillOneMany returned error: %v\n", err)
}
return subcommands.ExitFailure
}
for resp := range respChan {
if resp.Error != nil {
fmt.Fprintf(state.Err[resp.Index], "Got error from target %s (%d) - %v\n", resp.Target, resp.Index, resp.Error)
continue
}
}
return subcommands.ExitSuccess
}

type pstackCmd struct {
pid int64
}
Expand Down
Loading

0 comments on commit c570010

Please sign in to comment.