ECPluginSharedData.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #include <kopano/platform.h>
  18. #include <kopano/ECLogger.h>
  19. #include <kopano/ECPluginSharedData.h>
  20. #include <kopano/lockhelper.hpp>
  21. namespace KC {
  22. ECPluginSharedData *ECPluginSharedData::m_lpSingleton = NULL;
  23. std::mutex ECPluginSharedData::m_SingletonLock;
  24. std::mutex ECPluginSharedData::m_CreateConfigLock;
  25. ECPluginSharedData::ECPluginSharedData(ECConfig *lpParent,
  26. ECStatsCollector *lpStatsCollector, bool bHosted, bool bDistributed) :
  27. m_lpParentConfig(lpParent), m_lpStatsCollector(lpStatsCollector),
  28. m_bHosted(bHosted), m_bDistributed(bDistributed)
  29. {
  30. }
  31. ECPluginSharedData::~ECPluginSharedData()
  32. {
  33. delete m_lpConfig;
  34. if (m_lpDefaults) {
  35. for (int n = 0; m_lpDefaults[n].szName; ++n) {
  36. free(const_cast<char *>(m_lpDefaults[n].szName));
  37. free(const_cast<char *>(m_lpDefaults[n].szValue));
  38. }
  39. delete [] m_lpDefaults;
  40. }
  41. if (m_lpszDirectives) {
  42. for (int n = 0; m_lpszDirectives[n]; ++n)
  43. free(m_lpszDirectives[n]);
  44. delete [] m_lpszDirectives;
  45. }
  46. }
  47. void ECPluginSharedData::GetSingleton(ECPluginSharedData **lppSingleton,
  48. ECConfig *lpParent, ECStatsCollector *lpStatsCollector, bool bHosted,
  49. bool bDistributed)
  50. {
  51. scoped_lock lock(m_SingletonLock);
  52. if (!m_lpSingleton)
  53. m_lpSingleton = new ECPluginSharedData(lpParent, lpStatsCollector, bHosted, bDistributed);
  54. ++m_lpSingleton->m_ulRefCount;
  55. *lppSingleton = m_lpSingleton;
  56. }
  57. void ECPluginSharedData::AddRef()
  58. {
  59. scoped_lock lock(m_SingletonLock);
  60. ++m_ulRefCount;
  61. }
  62. void ECPluginSharedData::Release()
  63. {
  64. scoped_lock lock(m_SingletonLock);
  65. if (!--m_ulRefCount) {
  66. delete m_lpSingleton;
  67. m_lpSingleton = NULL;
  68. }
  69. }
  70. ECConfig *ECPluginSharedData::CreateConfig(const configsetting_t *lpDefaults,
  71. const char *const *lpszDirectives)
  72. {
  73. scoped_lock lock(m_CreateConfigLock);
  74. if (m_lpConfig != nullptr)
  75. return m_lpConfig;
  76. int n;
  77. /*
  78. * Store all the defaults and directives in the singleton,
  79. * so it isn't removed from memory when the plugin unloads.
  80. */
  81. if (lpDefaults) {
  82. for (n = 0; lpDefaults[n].szName; ++n)
  83. ;
  84. m_lpDefaults = new configsetting_t[n+1];
  85. for (n = 0; lpDefaults[n].szName; ++n) {
  86. m_lpDefaults[n].szName = strdup(lpDefaults[n].szName);
  87. m_lpDefaults[n].szValue = strdup(lpDefaults[n].szValue);
  88. m_lpDefaults[n].ulFlags = lpDefaults[n].ulFlags;
  89. m_lpDefaults[n].ulGroup = lpDefaults[n].ulGroup;
  90. }
  91. m_lpDefaults[n].szName = NULL;
  92. m_lpDefaults[n].szValue = NULL;
  93. }
  94. if (lpszDirectives) {
  95. for (n = 0; lpszDirectives[n]; ++n)
  96. ;
  97. m_lpszDirectives = new char*[n+1];
  98. for (n = 0; lpszDirectives[n]; ++n)
  99. m_lpszDirectives[n] = strdup(lpszDirectives[n]);
  100. m_lpszDirectives[n] = NULL;
  101. }
  102. m_lpConfig = ECConfig::Create(m_lpDefaults, m_lpszDirectives);
  103. if (!m_lpConfig->LoadSettings(m_lpParentConfig->GetSetting("user_plugin_config")))
  104. ec_log_err("Failed to open plugin configuration file, using defaults.");
  105. if (m_lpConfig->HasErrors() || m_lpConfig->HasWarnings()) {
  106. LogConfigErrors(m_lpConfig);
  107. if (m_lpConfig->HasErrors()) {
  108. delete m_lpConfig;
  109. m_lpConfig = NULL;
  110. }
  111. }
  112. return m_lpConfig;
  113. }
  114. void ECPluginSharedData::Signal(int signal)
  115. {
  116. if (!m_lpConfig)
  117. return;
  118. switch (signal) {
  119. case SIGHUP:
  120. if (!m_lpConfig->ReloadSettings())
  121. ec_log_crit("Unable to reload plugin configuration file, continuing with current settings.");
  122. if (m_lpConfig->HasErrors()) {
  123. ec_log_err("Unable to reload plugin configuration file.");
  124. LogConfigErrors(m_lpConfig);
  125. }
  126. break;
  127. default:
  128. break;
  129. }
  130. }
  131. } /* namespace */