Skip to content

fix(cmd/tracerunner): fix signal handling in trace attach #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 14, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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