Fix: Create lock file if it does not exist in likwidMetric.go (#120)

Co-authored-by: exterr2f <Robert.Externbrink@rub.de>
This commit is contained in:
brinkcoder 2024-11-14 16:20:47 +01:00 committed by GitHub
parent 8f336c1bb7
commit c96021c7cc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -374,10 +374,21 @@ func (m *LikwidCollector) takeMeasurement(evidx int, evset LikwidEventsetConfig,
} }
defer watcher.Close() defer watcher.Close()
if len(m.config.LockfilePath) > 0 { if len(m.config.LockfilePath) > 0 {
// Check if the lock file exists
info, err := os.Stat(m.config.LockfilePath) 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 { if err != nil {
return true, err return true, err
} }
// Check file ownership
uid := info.Sys().(*syscall.Stat_t).Uid uid := info.Sys().(*syscall.Stat_t).Uid
if uid != uint32(os.Getuid()) { if uid != uint32(os.Getuid()) {
usr, err := user.LookupId(fmt.Sprint(uid)) 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) 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) err = watcher.Add(m.config.LockfilePath)
if err != nil { if err != nil {
cclog.ComponentError(m.name, err.Error()) cclog.ComponentError(m.name, err.Error())