ClientConnection.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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).release_value_but_fixme_should_propagate_errors();
  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).release_value_but_fixme_should_propagate_errors();
  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(NonnullOwnPtr<Core::Stream::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. if (auto result = config.sync(); result.is_error()) {
  119. dbgln("Failed to write config '{}' to disk: {}", domain, result.error());
  120. // Put it back in the list since it's still dirty.
  121. m_dirty_domains.set(domain);
  122. }
  123. }
  124. }
  125. Messages::ConfigServer::ListConfigKeysResponse ClientConnection::list_config_keys(String const& domain, String const& group)
  126. {
  127. if (!validate_access(domain, group, ""))
  128. return Vector<String> {};
  129. auto& config = ensure_domain_config(domain);
  130. return { config.keys(group) };
  131. }
  132. Messages::ConfigServer::ListConfigGroupsResponse ClientConnection::list_config_groups(String const& domain)
  133. {
  134. if (!validate_access(domain, "", ""))
  135. return Vector<String> {};
  136. auto& config = ensure_domain_config(domain);
  137. return { config.groups() };
  138. }
  139. Messages::ConfigServer::ReadStringValueResponse ClientConnection::read_string_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<String> {};
  146. return Optional<String> { config.read_entry(group, key) };
  147. }
  148. Messages::ConfigServer::ReadI32ValueResponse ClientConnection::read_i32_value(String const& domain, String const& group, String const& key)
  149. {
  150. if (!validate_access(domain, group, key))
  151. return nullptr;
  152. auto& config = ensure_domain_config(domain);
  153. if (!config.has_key(group, key))
  154. return Optional<i32> {};
  155. return Optional<i32> { config.read_num_entry(group, key) };
  156. }
  157. Messages::ConfigServer::ReadBoolValueResponse ClientConnection::read_bool_value(String const& domain, String const& group, String const& key)
  158. {
  159. if (!validate_access(domain, group, key))
  160. return nullptr;
  161. auto& config = ensure_domain_config(domain);
  162. if (!config.has_key(group, key))
  163. return Optional<bool> {};
  164. return Optional<bool> { config.read_bool_entry(group, key) };
  165. }
  166. void ClientConnection::start_or_restart_sync_timer()
  167. {
  168. if (m_sync_timer->is_active())
  169. m_sync_timer->restart();
  170. else
  171. m_sync_timer->start();
  172. }
  173. void ClientConnection::write_string_value(String const& domain, String const& group, String const& key, String const& value)
  174. {
  175. if (!validate_access(domain, group, key))
  176. return;
  177. auto& config = ensure_domain_config(domain);
  178. if (config.has_key(group, key) && config.read_entry(group, key) == value)
  179. return;
  180. config.write_entry(group, key, value);
  181. m_dirty_domains.set(domain);
  182. start_or_restart_sync_timer();
  183. for_each_monitoring_connection(domain, this, [&domain, &group, &key, &value](ClientConnection& connection) {
  184. connection.async_notify_changed_string_value(domain, group, key, value);
  185. });
  186. }
  187. void ClientConnection::write_i32_value(String const& domain, String const& group, String const& key, i32 value)
  188. {
  189. if (!validate_access(domain, group, key))
  190. return;
  191. auto& config = ensure_domain_config(domain);
  192. if (config.has_key(group, key) && config.read_num_entry(group, key) == value)
  193. return;
  194. config.write_num_entry(group, key, value);
  195. m_dirty_domains.set(domain);
  196. start_or_restart_sync_timer();
  197. for_each_monitoring_connection(domain, this, [&domain, &group, &key, &value](ClientConnection& connection) {
  198. connection.async_notify_changed_i32_value(domain, group, key, value);
  199. });
  200. }
  201. void ClientConnection::write_bool_value(String const& domain, String const& group, String const& key, bool value)
  202. {
  203. if (!validate_access(domain, group, key))
  204. return;
  205. auto& config = ensure_domain_config(domain);
  206. if (config.has_key(group, key) && config.read_bool_entry(group, key) == value)
  207. return;
  208. config.write_bool_entry(group, key, value);
  209. m_dirty_domains.set(domain);
  210. start_or_restart_sync_timer();
  211. for_each_monitoring_connection(domain, this, [&domain, &group, &key, &value](ClientConnection& connection) {
  212. connection.async_notify_changed_bool_value(domain, group, key, value);
  213. });
  214. }
  215. void ClientConnection::remove_key(String const& domain, String const& group, String const& key)
  216. {
  217. if (!validate_access(domain, group, key))
  218. return;
  219. auto& config = ensure_domain_config(domain);
  220. if (!config.has_key(group, key))
  221. return;
  222. config.remove_entry(group, key);
  223. m_dirty_domains.set(domain);
  224. start_or_restart_sync_timer();
  225. for_each_monitoring_connection(domain, this, [&domain, &group, &key](ClientConnection& connection) {
  226. connection.async_notify_removed_key(domain, group, key);
  227. });
  228. }
  229. }