Create lockfile if it does not exist

This commit is contained in:
Thomas Röhl 2024-04-25 17:25:53 +02:00
parent 16c796a2b8
commit 0186dce521

View File

@ -374,6 +374,14 @@ 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 {
if _, err := os.Stat(m.config.LockfilePath); os.IsNotExist(err) {
file, err := os.Create(m.config.LockfilePath)
if err != nil {
cclog.ComponentError(m.name, "Cannot create lockfile", m.config.LockfilePath, ":", err.Error())
return true, err
}
file.Close()
}
info, err := os.Stat(m.config.LockfilePath) info, err := os.Stat(m.config.LockfilePath)
if err != nil { if err != nil {
return true, err return true, err
@ -382,9 +390,9 @@ func (m *LikwidCollector) takeMeasurement(evidx int, evset LikwidEventsetConfig,
if uid != uint32(os.Getuid()) { if uid != uint32(os.Getuid()) {
usr, err := user.LookupId(fmt.Sprint(uid)) usr, err := user.LookupId(fmt.Sprint(uid))
if err == nil { if err == nil {
return true, fmt.Errorf("Access to performance counters locked by %s", usr.Username) return true, fmt.Errorf("access to performance counters locked by %s", usr.Username)
} else { } else {
return true, fmt.Errorf("Access to performance counters locked by %d", uid) return true, fmt.Errorf("access to performance counters locked by %d", uid)
} }
} }
err = watcher.Add(m.config.LockfilePath) err = watcher.Add(m.config.LockfilePath)