PageRenderTime 25ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/monitor/api/monitor-api.cpp

https://gitlab.com/github-cloud-corporation/cynara
C++ | 224 lines | 167 code | 36 blank | 21 comment | 24 complexity | 6868963886922014bf670e4ae116900e MD5 | raw file
  1. /*
  2. * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License
  15. */
  16. /**
  17. * @file src/monitor/api/monitor-api.cpp
  18. * @author Aleksander Zdyb <a.zdyb@samsung.com>
  19. * @version 1.0
  20. * @brief Implementation of external libcynara-monitor API
  21. */
  22. #include <new>
  23. #include <api/ApiInterface.h>
  24. #include <common.h>
  25. #include <configuration/MonitorConfiguration.h>
  26. #include <exceptions/TryCatch.h>
  27. #include <types/ProtocolFields.h>
  28. #include <cynara-error.h>
  29. #include <cynara-limits.h>
  30. #include <cynara-monitor.h>
  31. #include <log/log.h>
  32. #include <logic/Logic.h>
  33. #include <utils/Lists.h>
  34. struct cynara_monitor {
  35. Cynara::ApiInterface *impl;
  36. cynara_monitor(Cynara::ApiInterface *_impl) : impl(_impl) {}
  37. ~cynara_monitor() {
  38. delete impl;
  39. }
  40. };
  41. struct cynara_monitor_configuration {
  42. Cynara::MonitorConfiguration *impl;
  43. cynara_monitor_configuration(Cynara::MonitorConfiguration *_impl) : impl(_impl) {}
  44. ~cynara_monitor_configuration() {
  45. delete impl;
  46. }
  47. };
  48. struct cynara_monitor_entry {
  49. Cynara::MonitorEntry m_monitorEntry;
  50. };
  51. CYNARA_API
  52. int cynara_monitor_configuration_create(cynara_monitor_configuration **pp_conf) {
  53. if (!pp_conf)
  54. return CYNARA_API_INVALID_PARAM;
  55. return Cynara::tryCatch([&]() {
  56. Cynara::MonitorConfigurationUniquePtr ptr(new Cynara::MonitorConfiguration());
  57. *pp_conf = new cynara_monitor_configuration(ptr.get());
  58. ptr.release();
  59. LOGD("Cynara monitor configuration initialized");
  60. return CYNARA_API_SUCCESS;
  61. });
  62. }
  63. CYNARA_API
  64. void cynara_monitor_configuration_destroy(cynara_monitor_configuration *p_conf) {
  65. delete p_conf;
  66. }
  67. CYNARA_API
  68. int cynara_monitor_configuration_set_buffer_size(cynara_monitor_configuration *p_conf,
  69. size_t buffer_size) {
  70. if (!p_conf || !p_conf->impl)
  71. return CYNARA_API_INVALID_PARAM;
  72. if (buffer_size > CYNARA_MAX_MONITOR_BUFFER_SIZE) {
  73. return CYNARA_API_INVALID_PARAM;
  74. }
  75. return Cynara::tryCatch([&]() {
  76. p_conf->impl->setBufferSize(buffer_size);
  77. return CYNARA_API_SUCCESS;
  78. });
  79. }
  80. CYNARA_API
  81. int cynara_monitor_initialize(cynara_monitor **pp_cynara_monitor,
  82. const cynara_monitor_configuration *p_conf) {
  83. if (!pp_cynara_monitor)
  84. return CYNARA_API_INVALID_PARAM;
  85. init_log();
  86. return Cynara::tryCatch([&]() {
  87. Cynara::LogicUniquePtr ptr;
  88. if (p_conf && p_conf->impl) {
  89. ptr.reset(new Cynara::Logic(*(p_conf->impl)));
  90. } else {
  91. ptr.reset(new Cynara::Logic());
  92. }
  93. int ret = ptr->init();
  94. if (ret != CYNARA_API_SUCCESS) {
  95. LOGE("Couldn't initialize cynara monitor");
  96. return ret;
  97. }
  98. *pp_cynara_monitor = new cynara_monitor(ptr.get());
  99. ptr.release();
  100. LOGD("Cynara client initialized");
  101. return CYNARA_API_SUCCESS;
  102. });
  103. }
  104. CYNARA_API
  105. int cynara_monitor_finish(cynara_monitor *p_cynara_monitor) {
  106. if (p_cynara_monitor && p_cynara_monitor->impl) {
  107. p_cynara_monitor->impl->notifyFinish();
  108. }
  109. delete p_cynara_monitor;
  110. return CYNARA_API_SUCCESS;
  111. }
  112. CYNARA_API
  113. int cynara_monitor_entries_get(cynara_monitor *p_cynara_monitor,
  114. cynara_monitor_entry ***monitor_entries) {
  115. if (!p_cynara_monitor || !p_cynara_monitor->impl)
  116. return CYNARA_API_INVALID_PARAM;
  117. if (!monitor_entries)
  118. return CYNARA_API_INVALID_PARAM;
  119. return Cynara::tryCatch([&]() {
  120. std::vector<Cynara::MonitorEntry> entriesVector;
  121. auto ret = p_cynara_monitor->impl->entriesGet(entriesVector);
  122. if (ret != CYNARA_API_SUCCESS)
  123. return ret;
  124. return Cynara::createNullTerminatedArray<Cynara::MonitorEntry, cynara_monitor_entry>(
  125. entriesVector, monitor_entries,
  126. [] (const Cynara::MonitorEntry &from, cynara_monitor_entry *&to) -> int {
  127. to = new cynara_monitor_entry{from};
  128. return CYNARA_API_SUCCESS;
  129. }
  130. );
  131. });
  132. }
  133. CYNARA_API
  134. int cynara_monitor_entries_flush(cynara_monitor *p_cynara_monitor) {
  135. if (!p_cynara_monitor || !p_cynara_monitor->impl)
  136. return CYNARA_API_INVALID_PARAM;
  137. return Cynara::tryCatch([&]() {
  138. return p_cynara_monitor->impl->entriesFlush();
  139. });
  140. }
  141. CYNARA_API
  142. void cynara_monitor_entries_free(cynara_monitor_entry **monitor_entries) {
  143. Cynara::freeNullTerminatedList(monitor_entries);
  144. }
  145. CYNARA_API
  146. const char *cynara_monitor_entry_get_client(const cynara_monitor_entry *monitor_entry) {
  147. if (!monitor_entry) {
  148. LOGW("NULL passed to %s", __FUNCTION__);
  149. return nullptr;
  150. }
  151. return monitor_entry->m_monitorEntry.key().client().toString().c_str();
  152. }
  153. CYNARA_API
  154. const char *cynara_monitor_entry_get_user(const cynara_monitor_entry *monitor_entry) {
  155. if (!monitor_entry) {
  156. LOGW("NULL passed to %s", __FUNCTION__);
  157. return nullptr;
  158. }
  159. return monitor_entry->m_monitorEntry.key().user().toString().c_str();
  160. }
  161. CYNARA_API
  162. const char *cynara_monitor_entry_get_privilege(const cynara_monitor_entry *monitor_entry) {
  163. if (!monitor_entry) {
  164. LOGW("NULL passed to %s", __FUNCTION__);
  165. return nullptr;
  166. }
  167. return monitor_entry->m_monitorEntry.key().privilege().toString().c_str();
  168. }
  169. CYNARA_API
  170. int cynara_monitor_entry_get_result(const cynara_monitor_entry *monitor_entry) {
  171. if (!monitor_entry) {
  172. LOGW("NULL passed to %s", __FUNCTION__);
  173. return CYNARA_API_INVALID_PARAM;
  174. }
  175. return monitor_entry->m_monitorEntry.result();
  176. }
  177. CYNARA_API
  178. const struct timespec *cynara_monitor_entry_get_timestamp(
  179. const cynara_monitor_entry *monitor_entry) {
  180. if (!monitor_entry) {
  181. LOGW("NULL passed to %s", __FUNCTION__);
  182. return nullptr;
  183. }
  184. return &monitor_entry->m_monitorEntry.timestamp();
  185. }
  186. static void freeElem(struct cynara_monitor_entry *policyPtr) {
  187. delete policyPtr;
  188. }