Skip to content

Commit

Permalink
pkg : fix Kernel config read failed, error:Config not found #117
Browse files Browse the repository at this point in the history
Signed-off-by: CFC4N <cfc4n.cs@gmail.com>
  • Loading branch information
cfc4n committed Jul 5, 2022
1 parent 302dddb commit 6de35fa
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
16 changes: 13 additions & 3 deletions pkg/util/ebpf/bpf_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bufio"
"fmt"
"os"
"strings"
)

const (
Expand Down Expand Up @@ -37,24 +38,33 @@ var (

func GetSystemConfig() (map[string]string, error) {
var KernelConfig = make(map[string]string)

var found bool
i, e := getOSUnamer()
if e != nil {
return KernelConfig, e
}

for _, system_config_path := range configPaths {
bootConf := fmt.Sprintf(system_config_path, i.Release)
var bootConf = system_config_path
if strings.Index(system_config_path, "%s") != -1 {
bootConf = fmt.Sprintf(system_config_path, i.Release)
}

KernelConfig, e = getLinuxConfig(bootConf)
if e != nil {
// 没有找到配置文件,继续找下一个
continue
}

if len(KernelConfig) > 0 {
found = true
break
}
}

if !found {
return nil, fmt.Errorf("KernelConfig not found.")
}
return KernelConfig, nil
}

Expand All @@ -69,7 +79,7 @@ func getLinuxConfig(filename string) (map[string]string, error) {
defer f.Close()

s := bufio.NewScanner(f)
if err := parse(s, KernelConfig); err != nil {
if err = parse(s, KernelConfig); err != nil {
return KernelConfig, err
}
return KernelConfig, nil
Expand Down
40 changes: 40 additions & 0 deletions pkg/util/ebpf/bpf_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package ebpf

import (
"testing"
)

func TestBpfConfig(t *testing.T) {
configPaths = []string{
"/xxxxx/proc/config.gz", // android
}
m, e := GetSystemConfig()
if e != nil {
// 正常情况 是没有找到配置文件
t.Logf("GetSystemConfig error:%s", e.Error())
}

configPaths = []string{
"/proc/config.gz", // android
"/boot/config", // linux
"/boot/config-%s", // linux
}
m, e = GetSystemConfig()
if e != nil {
t.Fatalf("GetSystemConfig error:%s", e.Error())
}
for _, item := range configCheckItems {
bc, found := m[item]
if !found {
// 没有这个配置项
t.Logf("Config not found, item:%s.", item)
}

//如果有,在判断配置项的值
if bc != "y" {
// 没有开启
t.Logf("Config disabled, item :%s.", item)
}
}
t.Logf("GetSystemConfig success")
}

0 comments on commit 6de35fa

Please sign in to comment.