Skip to content

Commit b23e1ac

Browse files
committed
fix(cmd/tracerunner): fix signal handling in trace attach
Signed-off-by: Lorenzo Fontana <[email protected]>
1 parent 3392e93 commit b23e1ac

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

pkg/cmd/tracerunner.go

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package cmd
22

33
import (
4+
"context"
45
"fmt"
56
"io"
67
"io/ioutil"
78
"os"
89
"os/exec"
10+
"os/signal"
911
"path"
1012
"strings"
13+
"syscall"
1114

1215
"github.com/fntlnz/mountinfo"
1316
"github.com/spf13/cobra"
@@ -87,7 +90,29 @@ func (o *TraceRunnerOptions) Run() error {
8790
}
8891
}
8992

90-
c := exec.Command(o.bpftraceBinaryPath, programPath)
93+
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")
94+
ctx, cancel := context.WithCancel(context.Background())
95+
sigCh := make(chan os.Signal, 1)
96+
97+
signal.Notify(sigCh, os.Signal(syscall.SIGINT))
98+
99+
go func() {
100+
killable := false
101+
defer cancel()
102+
M:
103+
select {
104+
case <-ctx.Done():
105+
return
106+
case <-sigCh:
107+
if !killable {
108+
killable = true
109+
goto M
110+
}
111+
return
112+
}
113+
}()
114+
115+
c := exec.CommandContext(ctx, o.bpftraceBinaryPath, programPath)
91116
c.Stdout = os.Stdout
92117
c.Stdin = os.Stdin
93118
c.Stderr = os.Stderr

0 commit comments

Comments
 (0)