ClientConnection.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "ClientConnection.h"
  7. #include <ConfigServer/ConfigClientEndpoint.h>
  8. #include <LibCore/ConfigFile.h>
  9. #include <LibCore/FileWatcher.h>
  10. #include <LibCore/Timer.h>
  11. namespace ConfigServer {
  12. static HashMap<int, RefPtr<ClientConnection>> s_connections;
  13. struct CachedDomain {
  14. String domain;
  15. NonnullRefPtr<Core::ConfigFile> config;
  16. RefPtr<Core::FileWatcher> watcher;
  17. };
  18. static HashMap<String, NonnullOwnPtr<CachedDomain>> s_cache;
  19. static constexpr int s_disk_sync_delay_ms = 5'000;
  20. static void for_each_monitoring_connection(String const& domain, ClientConnection* excluded_connection, Function<void(ClientConnection&)> callback)
  21. {
  22. for (auto& it : s_connections) {
  23. if (it.value->is_monitoring_domain(domain) && (!excluded_connection || it.value != excluded_connection))
  24. callback(*it.value);
  25. }
  26. }
  27. static Core::ConfigFile& ensure_domain_config(String const& domain)
  28. {
  29. auto it = s_cache.find(domain);
  30. if (it != s_cache.end())
  31. return *it->value->config;
  32. auto config = Core::ConfigFile::open_for_app(domain, Core::ConfigFile::AllowWriting::Yes);
  33. // FIXME: Use a single FileWatcher with multiple watches inside.
  34. auto watcher_or_error = Core::FileWatcher::create(InodeWatcherFlags::Nonblock);
  35. VERIFY(!watcher_or_error.is_error());
  36. auto result = watcher_or_error.value()->add_watch(config->filename(), Core::FileWatcherEvent::Type::ContentModified);
  37. VERIFY(!result.is_error());
  38. watcher_or_error.value()->on_change = [config, domain](auto&) {
  39. auto new_config = Core::ConfigFile::open(config->filename(), Core::ConfigFile::AllowWriting::Yes);
  40. for (auto& group : config->groups()) {
  41. for (auto& key : config->keys(group)) {
  42. if (!new_config->has_key(group, key)) {
  43. for_each_monitoring_connection(domain, nullptr, [&domain, &group, &key](ClientConnection& connection) {
  44. connection.async_notify_removed_key(domain, group, key);
  45. });
  46. }
  47. }
  48. }
  49. // FIXME: Detect type of keys.
  50. for (auto& group : new_config->groups()) {
  51. for (auto& key : new_config->keys(group)) {
  52. auto old_value = config->read_entry(group, key);
  53. auto new_value = new_config->read_entry(group, key);
  54. if (old_value != new_value) {
  55. for_each_monitoring_connection(domain, nullptr, [&domain, &group, &key, &new_value](ClientConnection& connection) {
  56. connection.async_notify_changed_string_value(domain, group, key, new_value);
  57. });
  58. }
  59. }
  60. }
  61. // FIXME: Refactor this whole thing so that we don't need a cache lookup here.
  62. s_cache.get(domain).value()->config = new_config;
  63. };
  64. auto cache_entry = make<CachedDomain>(domain, config, watcher_or_error.release_value());
  65. s_cache.set(domain, move(cache_entry));
  66. return *config;
  67. }
  68. ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> client_socket, int client_id)
  69. : IPC::ClientConnection<ConfigClientEndpoint, ConfigServerEndpoint>(*this, move(client_socket), client_id)
  70. , m_sync_timer(Core::Timer::create_single_shot(s_disk_sync_delay_ms, [this]() { sync_dirty_domains_to_disk(); }))
  71. {
  72. s_connections.set(client_id, *this);
  73. }
  74. ClientConnection::~ClientConnection()
  75. {
  76. }
  77. void ClientConnection::die()
  78. {
  79. s_connections.remove(client_id());
  80. m_sync_timer->stop();
  81. sync_dirty_domains_to_disk();
  82. }
  83. void ClientConnection::pledge_domains(Vector<String> const& domains)
  84. {
  85. if (m_has_pledged) {
  86. did_misbehave("Tried to pledge domains twice.");
  87. return;
  88. }
  89. m_has_pledged = true;
  90. for (auto& domain : domains)
  91. m_pledged_domains.set(domain);
  92. }
  93. void ClientConnection::monitor_domain(String const& domain)
  94. {
  95. if (m_has_pledged && !m_pledged_domains.contains(domain)) {
  96. did_misbehave("Attempt to monitor non-pledged domain");
  97. return;
  98. }
  99. m_monitored_domains.set(domain);
  100. }
  101. bool ClientConnection::validate_access(String const& domain, String const& group, String const& key)
  102. {
  103. if (!m_has_pledged)
  104. return true;
  105. if (m_pledged_domains.contains(domain))
  106. return true;
  107. did_misbehave(String::formatted("Blocked attempt to access domain '{}', group={}, key={}", domain, group, key).characters());
  108. return false;
  109. }
  110. void ClientConnection::sync_dirty_domains_to_disk()
  111. {
  112. if (m_dirty_domains.is_empty())
  113. return;
  114. auto dirty_domains = move(m_dirty_domains);
  115. dbgln("Syncing {} dirty domains to disk", dirty_domains.size());
  116. for (auto domain : dirty_domains) {
  117. auto& config = ensure_domain_config(domain);
  118. config.sync();
  119. }
  120. }
  121. Messages::ConfigServer::ReadStringValueResponse ClientConnection::read_string_value(String const& domain, String const& group, String const& key)
  122. {
  123. if (!validate_access(domain, group, key))
  124. return nullptr;
  125. auto& config = ensure_domain_config(domain);
  126. if (!config.has_key(group, key))
  127. return Optional<String> {};
  128. return Optional<String> { config.read_entry(group, key) };
  129. }
  130. Messages::ConfigServer::ReadI32ValueResponse ClientConnection::read_i32_value(String const& domain, String const& group, String const& key)
  131. {
  132. if (!validate_access(domain, group, key))
  133. return nullptr;
  134. auto& config = ensure_domain_config(domain);
  135. if (!config.has_key(group, key))
  136. return Optional<i32> {};
  137. return Optional<i32> { config.read_num_entry(group, key) };
  138. }
  139. Messages::ConfigServer::ReadBoolValueResponse ClientConnection::read_bool_value(String const& domain, String const& group, String const& key)
  140. {
  141. if (!validate_access(domain, group, key))
  142. return nullptr;
  143. auto& config = ensure_domain_config(domain);
  144. if (!config.has_key(group, key))
  145. return Optional<bool> {};
  146. return Optional<bool> { config.read_bool_entry(group, key) };
  147. }
  148. void ClientConnection::start_or_restart_sync_timer()
  149. {
  150. if (m_sync_timer->is_active())
  151. m_sync_timer->restart();
  152. else
  153. m_sync_timer->start();
  154. }
  155. void ClientConnection::write_string_value(String const& domain, String const& group, String const& key, String const& value)
  156. {
  157. if (!validate_access(domain, group, key))
  158. return;
  159. auto& config = ensure_domain_config(domain);
  160. if (config.has_key(group, key) && config.read_entry(group, key) == value)
  161. return;
  162. config.write_entry(group, key, value);
  163. m_dirty_domains.set(domain);
  164. start_or_restart_sync_timer();
  165. for_each_monitoring_connection(domain, this, [&domain, &group, &key, &value](ClientConnection& connection) {
  166. connection.async_notify_changed_string_value(domain, group, key, value);
  167. });
  168. }
  169. void ClientConnection::write_i32_value(String const& domain, String const& group, String const& key, i32 value)
  170. {
  171. if (!validate_access(domain, group, key))
  172. return;
  173. auto& config = ensure_domain_config(domain);
  174. if (config.has_key(group, key) && config.read_num_entry(group, key) == value)
  175. return;
  176. config.write_num_entry(group, key, value);
  177. m_dirty_domains.set(domain);
  178. start_or_restart_sync_timer();
  179. for_each_monitoring_connection(domain, this, [&domain, &group, &key, &value](ClientConnection& connection) {
  180. connection.async_notify_changed_i32_value(domain, group, key, value);
  181. });
  182. }
  183. void ClientConnection::write_bool_value(String const& domain, String const& group, String const& key, bool value)
  184. {
  185. if (!validate_access(domain, group, key))
  186. return;
  187. auto& config = ensure_domain_config(domain);
  188. if (config.has_key(group, key) && config.read_bool_entry(group, key) == value)
  189. return;
  190. config.write_bool_entry(group, key, value);
  191. m_dirty_domains.set(domain);
  192. start_or_restart_sync_timer();
  193. for_each_monitoring_connection(domain, this, [&domain, &group, &key, &value](ClientConnection& connection) {
  194. connection.async_notify_changed_bool_value(domain, group, key, value);
  195. });
  196. }
  197. void ClientConnection::remove_key(String const& domain, String const& group, String const& key)
  198. {
  199. if (!validate_access(domain, group, key))
  200. return;
  201. auto& config = ensure_domain_config(domain);
  202. if (!config.has_key(group, key))
  203. return;
  204. config.remove_entry(group, key);
  205. m_dirty_domains.set(domain);
  206. start_or_restart_sync_timer();
  207. for_each_monitoring_connection(domain, this, [&domain, &group, &key](ClientConnection& connection) {
  208. connection.async_notify_removed_key(domain, group, key);
  209. });
  210. }
  211. }