From 91d60d574ce1be17ba835765d29743564651f890 Mon Sep 17 00:00:00 2001 From: hengyoush Date: Tue, 17 Dec 2024 01:25:28 +0800 Subject: [PATCH] fix: fix #159 stuck when load bpf failed --- agent/agent.go | 8 ++++++++ bpf/loader/loader.go | 18 +++++------------- common/log.go | 9 +++++++++ 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/agent/agent.go b/agent/agent.go index 765033c9..3f188c09 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -82,6 +82,9 @@ func SetupAgent(options ac.AgentOptions) { if bf != nil { bf.Close() } + _bf.Err = err + options.LoadPorgressChannel <- "❌ Kyanos start failed" + options.LoadPorgressChannel <- "quit" return } _bf.Links = bf.Links @@ -118,10 +121,15 @@ func SetupAgent(options ac.AgentOptions) { }() if !options.WatchOptions.DebugOutput { loader_render.Start(ctx, options) + common.SetLogToStdout() } else { wg.Wait() common.AgentLog.Info("Waiting for events..") } + if _bf.Err != nil { + common.AgentLog.Error("Failed to load BPF: ", _bf.Err) + return + } stop := false go func() { diff --git a/bpf/loader/loader.go b/bpf/loader/loader.go index 7d760cc1..30146637 100644 --- a/bpf/loader/loader.go +++ b/bpf/loader/loader.go @@ -28,10 +28,14 @@ import ( type BPF struct { Links *list.List // close Objs *bpf.AgentObjects // close + Err error } func (b *BPF) Close() { - b.Objs.Close() + if b.Objs != nil { + b.Objs.Close() + } + if b.Links != nil { for e := b.Links.Front(); e != nil; e = e.Next() { if e.Value == nil { @@ -112,20 +116,8 @@ func LoadBPF(options *ac.AgentOptions) (*BPF, error) { } options.LoadPorgressChannel <- "🍓 Setup traffic filters" - // var links *list.List - // if options.LoadBpfProgramFunction != nil { - // links = options.LoadBpfProgramFunction() - // } else { - // links = attachBpfProgs(options.IfName, options.Kv, &options) - // } - - // if !options.DisableOpensslUprobe { - // attachOpenSslUprobes(links, options, options.Kv, objs) - // } - // attachNfFunctions(links) bpf.PullProcessExitEvents(options.Ctx, []chan *bpf.AgentProcessExitEvent{initProcExitEventChannel(options.Ctx)}) - // bf.links = links return bf, nil } diff --git a/common/log.go b/common/log.go index 9fde939a..9ee1a915 100644 --- a/common/log.go +++ b/common/log.go @@ -2,6 +2,7 @@ package common import ( "io" + "os" "time" "github.com/jefurry/logrus" @@ -58,3 +59,11 @@ func SetLogToFile() { } } } + +func SetLogToStdout() { + SetLogToFileFlag = false + for _, l := range Loggers { + // 设置为stdout + l.SetOut(os.Stdout) + } +}