Skip to content
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

ksniff graceful shutdown on Ctrl+C + Wireshark doesn't close on exit #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
47 changes: 33 additions & 14 deletions pkg/cmd/sniff.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"ksniff/pkg/config"
"ksniff/pkg/service/sniffer"
"os"
"os/signal"
"os/exec"
"syscall"
"path/filepath"
"strings"
"time"
Expand Down Expand Up @@ -292,6 +294,18 @@ func findLocalTcpdumpBinaryPath() (string, error) {
return "", errors.Errorf("couldn't find static tcpdump binary on any of: '%v'", tcpdumpLocalBinaryPathLookupList)
}

func (o *Ksniff) Cleanup() {
log.Info("starting sniffer cleanup")

err := o.snifferService.Cleanup()
if err != nil {
log.WithError(err).Error("failed to teardown sniffer, a manual teardown is required.")
return
}

log.Info("sniffer cleanup completed successfully")
}

func (o *Ksniff) Run() error {
log.Infof("sniffing on pod: '%s' [namespace: '%s', container: '%s', filter: '%s', interface: '%s']",
o.settings.UserSpecifiedPodName, o.settings.UserSpecifiedNamespace, o.settings.UserSpecifiedContainer, o.settings.UserSpecifiedFilter, o.settings.UserSpecifiedInterface)
Expand All @@ -301,16 +315,15 @@ func (o *Ksniff) Run() error {
return err
}

defer func() {
log.Info("starting sniffer cleanup")

err := o.snifferService.Cleanup()
if err != nil {
log.WithError(err).Error("failed to teardown sniffer, a manual teardown is required.")
return
}
defer o.Cleanup()

log.Info("sniffer cleanup completed successfully")
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
o.Cleanup()
log.Info("cleaned up, exiting.")
os.Exit(0)
aviramha marked this conversation as resolved.
Show resolved Hide resolved
}()

if o.settings.UserSpecifiedOutputFile != "" {
Expand Down Expand Up @@ -339,23 +352,29 @@ func (o *Ksniff) Run() error {
title := fmt.Sprintf("gui.window_title:%s/%s/%s", o.settings.UserSpecifiedNamespace, o.settings.UserSpecifiedPodName, o.settings.UserSpecifiedContainer)
cmd := exec.Command("wireshark", "-k", "-i", "-", "-o", title)

cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
Pgid: 0,
}

stdinWriter, err := cmd.StdinPipe()
if err != nil {
return err
}

err = cmd.Start()
if err != nil {
return err
}

go func() {
err := o.snifferService.Start(stdinWriter)
if err != nil {
log.WithError(err).Errorf("failed to start remote sniffing, stopping wireshark")
_ = cmd.Process.Kill()
}
}()

err = cmd.Run()
if err != nil {
return err
}
cmd.Wait()
}

return nil
Expand Down