blob: 56a883bf94b4060cd37bf1a176aed8484ab4e2f5 [file]
#ifndef PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_INTERNAL_BOOT_COUNTER_H_
#define PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_INTERNAL_BOOT_COUNTER_H_
#include <optional>
#include <string>
#include "absl/base/thread_annotations.h"
#include "absl/status/status.h"
#include "absl/synchronization/mutex.h"
namespace safepower_agent {
class InternalBootCounter {
public:
explicit InternalBootCounter(
std::string boot_id_path = "/proc/sys/kernel/random/boot_id");
// Reads the boot ID from the configured path.
// Checks if it matches the persisted boot ID.
// If not, increments the persisted boot counter and updates the persisted
// boot ID. Updates the cached boot counter.
absl::Status CheckAndIncrement() ABSL_LOCKS_EXCLUDED(mutex_);
// Returns the cached boot counter if available.
std::optional<int> value() const ABSL_LOCKS_EXCLUDED(mutex_);
private:
const std::string boot_id_path_;
mutable absl::Mutex mutex_;
std::optional<int> boot_counter_ ABSL_GUARDED_BY(mutex_);
};
} // namespace safepower_agent
#endif // PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_INTERNAL_BOOT_COUNTER_H_