From ca38cef0fe285ac368cdd5b483ad06b672903c5e Mon Sep 17 00:00:00 2001 From: Martynas Pumputis Date: Fri, 13 Sep 2024 10:54:14 +0200 Subject: [PATCH] Do not open kcore in init() Otherwise, "pwru --help" might fail it it does not run as root. Signed-off-by: Martynas Pumputis --- internal/asm/x86/x86.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/internal/asm/x86/x86.go b/internal/asm/x86/x86.go index dfa894b4..d2604412 100644 --- a/internal/asm/x86/x86.go +++ b/internal/asm/x86/x86.go @@ -5,16 +5,18 @@ import ( "fmt" "log" "os" + "sync" "golang.org/x/arch/x86/x86asm" ) var ( - kcore *os.File - kcoreElf *elf.File + kcore *os.File + kcoreElf *elf.File + kcoreOnce sync.Once ) -func init() { +func parseKCore() { var err error if kcore, err = os.Open("/proc/kcore"); err != nil { log.Fatalf("failed to open /proc/kcore: %s", err) @@ -26,6 +28,8 @@ func init() { } func GetCallees(addr uint64, leng int) (callees []uint64, err error) { + kcoreOnce.Do(parseKCore) + if leng == 0 { leng = 100000 }