From c96021c7cc69b186be05b32207898b82eaa2aaec Mon Sep 17 00:00:00 2001 From: brinkcoder Date: Thu, 14 Nov 2024 16:20:47 +0100 Subject: [PATCH] Fix: Create lock file if it does not exist in likwidMetric.go (#120) Co-authored-by: exterr2f --- collectors/likwidMetric.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/collectors/likwidMetric.go b/collectors/likwidMetric.go index 606f6a6..12757c3 100644 --- a/collectors/likwidMetric.go +++ b/collectors/likwidMetric.go @@ -374,10 +374,21 @@ func (m *LikwidCollector) takeMeasurement(evidx int, evset LikwidEventsetConfig, } defer watcher.Close() if len(m.config.LockfilePath) > 0 { + // Check if the lock file exists info, err := os.Stat(m.config.LockfilePath) + if os.IsNotExist(err) { + // Create the lock file if it does not exist + file, createErr := os.Create(m.config.LockfilePath) + if createErr != nil { + return true, fmt.Errorf("failed to create lock file: %v", createErr) + } + file.Close() + info, err = os.Stat(m.config.LockfilePath) // Recheck the file after creation + } if err != nil { return true, err } + // Check file ownership uid := info.Sys().(*syscall.Stat_t).Uid if uid != uint32(os.Getuid()) { usr, err := user.LookupId(fmt.Sprint(uid)) @@ -387,6 +398,7 @@ func (m *LikwidCollector) takeMeasurement(evidx int, evset LikwidEventsetConfig, return true, fmt.Errorf("Access to performance counters locked by %d", uid) } } + // Add the lock file to the watcher err = watcher.Add(m.config.LockfilePath) if err != nil { cclog.ComponentError(m.name, err.Error())