Skip to content

Commit

Permalink
fix(cmd/tracerunner): fix signal handling in trace attach
Browse files Browse the repository at this point in the history
Signed-off-by: Lorenzo Fontana <[email protected]>
  • Loading branch information
fntlnz committed Jan 14, 2019
1 parent 3392e93 commit b23e1ac
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion pkg/cmd/tracerunner.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package cmd

import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"os/signal"
"path"
"strings"
"syscall"

"github.com/fntlnz/mountinfo"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -87,7 +90,29 @@ func (o *TraceRunnerOptions) Run() error {
}
}

c := exec.Command(o.bpftraceBinaryPath, programPath)
fmt.Println("if you have maps to print, send a SIGINT using Ctrl-C, if you want to interrupt the execution send SIGINT two times")
ctx, cancel := context.WithCancel(context.Background())
sigCh := make(chan os.Signal, 1)

signal.Notify(sigCh, os.Signal(syscall.SIGINT))

go func() {
killable := false
defer cancel()
M:
select {
case <-ctx.Done():
return
case <-sigCh:
if !killable {
killable = true
goto M
}
return
}
}()

c := exec.CommandContext(ctx, o.bpftraceBinaryPath, programPath)
c.Stdout = os.Stdout
c.Stdin = os.Stdin
c.Stderr = os.Stderr
Expand Down

0 comments on commit b23e1ac

Please sign in to comment.