diff --git a/lib/remote/CMakeLists.txt b/lib/remote/CMakeLists.txt index 2c5a0326a..740b112b4 100644 --- a/lib/remote/CMakeLists.txt +++ b/lib/remote/CMakeLists.txt @@ -14,6 +14,7 @@ set(remote_SOURCES apilistener-authority.cpp apiuser.cpp apiuser.hpp apiuser-ti.hpp configfileshandler.cpp configfileshandler.hpp + configobjectslock.cpp configobjectslock.hpp configobjectutility.cpp configobjectutility.hpp configpackageshandler.cpp configpackageshandler.hpp configpackageutility.cpp configpackageutility.hpp diff --git a/lib/remote/configobjectslock.cpp b/lib/remote/configobjectslock.cpp new file mode 100644 index 000000000..e529c832b --- /dev/null +++ b/lib/remote/configobjectslock.cpp @@ -0,0 +1,24 @@ +/* Icinga 2 | (c) 2022 Icinga GmbH | GPLv2+ */ + +#ifndef _WIN32 + +#include "base/shared-memory.hpp" +#include "remote/configobjectslock.hpp" +#include + +using namespace icinga; + +// On *nix one process may write config objects while another is loading the config, so this uses IPC. +static SharedMemory l_ConfigObjectsMutex; + +ConfigObjectsExclusiveLock::ConfigObjectsExclusiveLock() + : m_Lock(l_ConfigObjectsMutex.Get()) +{ +} + +ConfigObjectsSharedLock::ConfigObjectsSharedLock(std::try_to_lock_t) + : m_Lock(l_ConfigObjectsMutex.Get(), boost::interprocess::try_to_lock) +{ +} + +#endif /* _WIN32 */ diff --git a/lib/remote/configobjectslock.hpp b/lib/remote/configobjectslock.hpp new file mode 100644 index 000000000..ee909815f --- /dev/null +++ b/lib/remote/configobjectslock.hpp @@ -0,0 +1,72 @@ +/* Icinga 2 | (c) 2023 Icinga GmbH | GPLv2+ */ + +#pragma once + +#include + +#ifndef _WIN32 +#include +#include +#include +#endif /* _WIN32 */ + +namespace icinga +{ + +#ifdef _WIN32 + +class ConfigObjectsSharedLock +{ +public: + inline ConfigObjectsSharedLock(std::try_to_lock_t) + { + } + + constexpr explicit operator bool() const + { + return true; + } +}; + +#else /* _WIN32 */ + +/** + * Waits until all ConfigObjects*Lock-s have vanished. For its lifetime disallows such. + * Keep an instance alive during reload to forbid runtime config changes! + * This way Icinga reads a consistent config which doesn't suddenly get runtime-changed. + * + * @ingroup remote + */ +class ConfigObjectsExclusiveLock +{ +public: + ConfigObjectsExclusiveLock(); + +private: + boost::interprocess::scoped_lock m_Lock; +}; + +/** + * Waits until the only ConfigObjectsExclusiveLock has vanished (if any). For its lifetime disallows such. + * Keep an instance alive during runtime config changes to delay a reload (if any)! + * This way Icinga reads a consistent config which doesn't suddenly get runtime-changed. + * + * @ingroup remote + */ +class ConfigObjectsSharedLock +{ +public: + ConfigObjectsSharedLock(std::try_to_lock_t); + + inline explicit operator bool() const + { + return m_Lock.owns(); + } + +private: + boost::interprocess::sharable_lock m_Lock; +}; + +#endif /* _WIN32 */ + +}