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

cli/cmd : clean up all probe while process exit. (#150) #151

Merged
merged 2 commits into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
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
17 changes: 17 additions & 0 deletions cli/cmd/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"log"
"os"
"os/signal"
"sync"
"syscall"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -70,6 +71,9 @@ func openSSLCommandFunc(command *cobra.Command, args []string) {
modNames := []string{user.MODULE_NAME_OPENSSL, user.MODULE_NAME_GNUTLS, user.MODULE_NAME_NSPR, user.MODULE_NAME_GOSSL}

var runMods uint8
var runModules = make(map[string]user.IModule)
var wg sync.WaitGroup

for _, modName := range modNames {
mod := user.GetModuleByName(modName)
if mod == nil {
Expand Down Expand Up @@ -122,7 +126,9 @@ func openSSLCommandFunc(command *cobra.Command, args []string) {
return
}
}(mod)
runModules[mod.Name()] = mod
logger.Printf("%s\tmodule started successfully.", mod.Name())
wg.Add(1)
runMods++
}

Expand All @@ -131,5 +137,16 @@ func openSSLCommandFunc(command *cobra.Command, args []string) {
<-stopper
}
cancelFun()

// clean up
for _, mod := range runModules {
e = mod.Close()
if e != nil {
logger.Fatalf("%s\tmodule close failed. error:%+v", mod.Name(), e)
}
wg.Done()
}

wg.Wait()
os.Exit(0)
}
24 changes: 12 additions & 12 deletions user/imodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (this *Module) run() {
case _ = <-this.ctx.Done():
err := this.child.Stop()
if err != nil {
this.logger.Fatalf("stop Module:%s error:%v.", this.child.Name(), err)
this.logger.Fatalf("%s\t stop Module error:%v.", this.child.Name(), err)
}
return
}
Expand All @@ -136,7 +136,7 @@ func (this *Module) readEvents() error {
case event.Type() == ebpf.PerfEventArray:
go this.perfEventReader(errChan, event)
default:
errChan <- fmt.Errorf("Not support mapType:%s , mapinfo:%s", event.Type().String(), event.String())
errChan <- fmt.Errorf("%s\tNot support mapType:%s , mapinfo:%s", this.child.Name(), event.Type().String(), event.String())
}
}

Expand All @@ -159,7 +159,7 @@ func (this *Module) perfEventReader(errChan chan error, em *ebpf.Map) {
//判断ctx是不是结束
select {
case _ = <-this.ctx.Done():
log.Printf("readEvent received close signal from context.Done.")
this.logger.Printf("%s\tperfEventReader received close signal from context.Done().", this.child.Name())
return
default:
}
Expand All @@ -169,19 +169,19 @@ func (this *Module) perfEventReader(errChan chan error, em *ebpf.Map) {
if errors.Is(err, perf.ErrClosed) {
return
}
errChan <- fmt.Errorf("reading from perf event reader: %s", err)
errChan <- fmt.Errorf("%s\treading from perf event reader: %s", this.child.Name(), err)
return
}

if record.LostSamples != 0 {
log.Printf("perf event ring buffer full, dropped %d samples", record.LostSamples)
this.logger.Printf("%s\tperf event ring buffer full, dropped %d samples", this.child.Name(), record.LostSamples)
continue
}

var event event_processor.IEventStruct
event, err = this.child.Decode(em, record.RawSample)
if err != nil {
log.Printf("this.child.decode error:%v", err)
this.logger.Printf("%s\tthis.child.decode error:%v", this.child.Name(), err)
continue
}

Expand All @@ -193,33 +193,33 @@ func (this *Module) perfEventReader(errChan chan error, em *ebpf.Map) {
func (this *Module) ringbufEventReader(errChan chan error, em *ebpf.Map) {
rd, err := ringbuf.NewReader(em)
if err != nil {
errChan <- fmt.Errorf("creating %s reader dns: %s", em.String(), err)
errChan <- fmt.Errorf("%s\tcreating %s reader dns: %s", this.child.Name(), em.String(), err)
return
}
defer rd.Close()
for {
//判断ctx是不是结束
select {
case _ = <-this.ctx.Done():
this.logger.Printf("readEvent received close signal from context.Done.")
this.logger.Printf("%s\tringbufEventReader received close signal from context.Done().", this.child.Name())
return
default:
}

record, err := rd.Read()
if err != nil {
if errors.Is(err, ringbuf.ErrClosed) {
this.logger.Println("Received signal, exiting..")
this.logger.Printf("%s\tReceived signal, exiting..", this.child.Name())
return
}
errChan <- fmt.Errorf("reading from ringbuf reader: %s", err)
errChan <- fmt.Errorf("%s\treading from ringbuf reader: %s", this.child.Name(), err)
return
}

var event event_processor.IEventStruct
event, err = this.child.Decode(em, record.RawSample)
if err != nil {
log.Printf("this.child.decode error:%v", err)
this.logger.Printf("%s\tthis.child.decode error:%v", this.child.Name(), err)
continue
}

Expand All @@ -231,7 +231,7 @@ func (this *Module) ringbufEventReader(errChan chan error, em *ebpf.Map) {
func (this *Module) Decode(em *ebpf.Map, b []byte) (event event_processor.IEventStruct, err error) {
es, found := this.child.DecodeFun(em)
if !found {
err = fmt.Errorf("can't found decode function :%s, address:%p", em.String(), em)
err = fmt.Errorf("%s\tcan't found decode function :%s, address:%p", this.child.Name(), em.String(), em)
return
}

Expand Down
3 changes: 2 additions & 1 deletion user/probe_openssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,11 @@ func (this *MOpenSSLProbe) start() error {
}

func (this *MOpenSSLProbe) Close() error {
this.logger.Printf("%s\tclose. \n", this.Name())
if err := this.bpfManager.Stop(manager.CleanAll); err != nil {
return fmt.Errorf("couldn't stop manager %v .", err)
}
return nil
return this.Module.Close()
}

// 通过elf的常量替换方式传递数据
Expand Down