Set init_priority attribute for global variables in shared lib
The code gets unspecified initializatio order for the global/static
variables in a shared library.
If unluck, a global/static variable may be initialized in *constructor*
function, and then initialized by the default contructor.
For exmaple, if `std::unique_ptr<xxx> var{nullptr};` is initialized in
constructor function, below init order may occur:
1. It is initialized in constructor;
2. Then it is initialized as nullptr;
And eventually when the code is to use the variable, we got nullptr.
We met such issues before on openbmc/openbmc#1581, and the technical
details could be found at [this SO question][1] and [this gcc email][2]
The solution is to specify the init_priority attribute, to make the
global/staic variables inititalize earlier than the contructors.
[1]: https://stackoverflow.com/questions/43941159/global-static-variables-initialization-issue-with-attribute-constructor-i
[2]: https://gcc.gnu.org/ml/gcc-patches/2017-03/msg00863.html
Change-Id: I901a6a5cddec12aec9512fe58b16735fa2ad90d7
Signed-off-by: Lei YU <mine260309@gmail.com>
diff --git a/user_channel/channel_mgmt.cpp b/user_channel/channel_mgmt.cpp
index 585d441..9374931 100644
--- a/user_channel/channel_mgmt.cpp
+++ b/user_channel/channel_mgmt.cpp
@@ -89,7 +89,8 @@
static constexpr const bool defaultIsIpmiState = false;
static constexpr size_t smallChannelSize = 64;
-std::unique_ptr<sdbusplus::bus::match_t> chPropertiesSignal(nullptr);
+std::unique_ptr<sdbusplus::bus::match_t> chPropertiesSignal
+ __attribute__((init_priority(101)));
// String mappings use in JSON config file
static std::unordered_map<std::string, EChannelMediumType> mediumTypeMap = {
diff --git a/user_channel/user_mgmt.cpp b/user_channel/user_mgmt.cpp
index 0b86823..42a1e09 100644
--- a/user_channel/user_mgmt.cpp
+++ b/user_channel/user_mgmt.cpp
@@ -116,9 +116,12 @@
using InternalFailure =
sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
-std::unique_ptr<sdbusplus::bus::match_t> userUpdatedSignal(nullptr);
-std::unique_ptr<sdbusplus::bus::match_t> userMgrRenamedSignal(nullptr);
-std::unique_ptr<sdbusplus::bus::match_t> userPropertiesSignal(nullptr);
+std::unique_ptr<sdbusplus::bus::match_t> userUpdatedSignal
+ __attribute__((init_priority(101)));
+std::unique_ptr<sdbusplus::bus::match_t> userMgrRenamedSignal
+ __attribute__((init_priority(101)));
+std::unique_ptr<sdbusplus::bus::match_t> userPropertiesSignal
+ __attribute__((init_priority(101)));
// TODO: Below code can be removed once it is moved to common layer libmiscutil
std::string getUserService(sdbusplus::bus::bus& bus, const std::string& intf,