dbus-name-cache.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. *
  3. * Embedded Linux library
  4. *
  5. * Copyright (C) 2016 Intel Corporation. All rights reserved.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25. #define _GNU_SOURCE
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include "util.h"
  29. #include "hashmap.h"
  30. #include "idle.h"
  31. #include "dbus.h"
  32. #include "dbus-private.h"
  33. struct _dbus_name_cache {
  34. struct l_dbus *bus;
  35. struct l_hashmap *names;
  36. const struct _dbus_name_ops *driver;
  37. unsigned int last_watch_id;
  38. struct l_idle *watch_remove_work;
  39. };
  40. struct service_watch {
  41. l_dbus_watch_func_t connect_func;
  42. l_dbus_watch_func_t disconnect_func;
  43. l_dbus_destroy_func_t destroy;
  44. void *user_data;
  45. unsigned int id;
  46. struct service_watch *next;
  47. };
  48. struct name_cache_entry {
  49. int ref_count;
  50. char *unique_name;
  51. struct service_watch *watches;
  52. };
  53. struct _dbus_name_cache *_dbus_name_cache_new(struct l_dbus *bus,
  54. const struct _dbus_name_ops *driver)
  55. {
  56. struct _dbus_name_cache *cache;
  57. cache = l_new(struct _dbus_name_cache, 1);
  58. cache->bus = bus;
  59. cache->driver = driver;
  60. return cache;
  61. }
  62. static void service_watch_destroy(void *data)
  63. {
  64. struct service_watch *watch = data;
  65. if (watch->destroy)
  66. watch->destroy(watch->user_data);
  67. l_free(watch);
  68. }
  69. static void name_cache_entry_destroy(void *data)
  70. {
  71. struct name_cache_entry *entry = data;
  72. struct service_watch *watch;
  73. while (entry->watches) {
  74. watch = entry->watches;
  75. entry->watches = watch->next;
  76. service_watch_destroy(watch);
  77. }
  78. l_free(entry->unique_name);
  79. l_free(entry);
  80. }
  81. void _dbus_name_cache_free(struct _dbus_name_cache *cache)
  82. {
  83. if (!cache)
  84. return;
  85. if (cache->watch_remove_work)
  86. l_idle_remove(cache->watch_remove_work);
  87. l_hashmap_destroy(cache->names, name_cache_entry_destroy);
  88. l_free(cache);
  89. }
  90. bool _dbus_name_cache_add(struct _dbus_name_cache *cache, const char *name)
  91. {
  92. struct name_cache_entry *entry;
  93. if (!_dbus_valid_bus_name(name))
  94. return false;
  95. if (!cache->names)
  96. cache->names = l_hashmap_string_new();
  97. entry = l_hashmap_lookup(cache->names, name);
  98. if (!entry) {
  99. entry = l_new(struct name_cache_entry, 1);
  100. l_hashmap_insert(cache->names, name, entry);
  101. cache->driver->get_name_owner(cache->bus, name);
  102. }
  103. entry->ref_count++;
  104. return true;
  105. }
  106. bool _dbus_name_cache_remove(struct _dbus_name_cache *cache, const char *name)
  107. {
  108. struct name_cache_entry *entry;
  109. entry = l_hashmap_lookup(cache->names, name);
  110. if (!entry)
  111. return false;
  112. if (--entry->ref_count)
  113. return true;
  114. l_hashmap_remove(cache->names, name);
  115. name_cache_entry_destroy(entry);
  116. return true;
  117. }
  118. const char *_dbus_name_cache_lookup(struct _dbus_name_cache *cache,
  119. const char *name)
  120. {
  121. struct name_cache_entry *entry;
  122. entry = l_hashmap_lookup(cache->names, name);
  123. if (!entry)
  124. return NULL;
  125. return entry->unique_name;
  126. }
  127. void _dbus_name_cache_notify(struct _dbus_name_cache *cache,
  128. const char *name, const char *owner)
  129. {
  130. struct name_cache_entry *entry;
  131. struct service_watch *watch;
  132. bool prev_connected, connected;
  133. if (!cache)
  134. return;
  135. entry = l_hashmap_lookup(cache->names, name);
  136. if (!entry)
  137. return;
  138. prev_connected = !!entry->unique_name;
  139. connected = owner && *owner != '\0';
  140. l_free(entry->unique_name);
  141. entry->unique_name = connected ? l_strdup(owner) : NULL;
  142. /*
  143. * This check also means we notify all watchers who have a connected
  144. * callback when we first learn that the service is in fact connected.
  145. */
  146. if (connected == prev_connected)
  147. return;
  148. for (watch = entry->watches; watch; watch = watch->next)
  149. if (connected && watch->connect_func)
  150. watch->connect_func(cache->bus, watch->user_data);
  151. else if (!connected && watch->disconnect_func)
  152. watch->disconnect_func(cache->bus, watch->user_data);
  153. }
  154. unsigned int _dbus_name_cache_add_watch(struct _dbus_name_cache *cache,
  155. const char *name,
  156. l_dbus_watch_func_t connect_func,
  157. l_dbus_watch_func_t disconnect_func,
  158. void *user_data,
  159. l_dbus_destroy_func_t destroy)
  160. {
  161. struct name_cache_entry *entry;
  162. struct service_watch *watch;
  163. if (!_dbus_name_cache_add(cache, name))
  164. return 0;
  165. watch = l_new(struct service_watch, 1);
  166. watch->id = ++cache->last_watch_id;
  167. watch->connect_func = connect_func;
  168. watch->disconnect_func = disconnect_func;
  169. watch->user_data = user_data;
  170. watch->destroy = destroy;
  171. entry = l_hashmap_lookup(cache->names, name);
  172. watch->next = entry->watches;
  173. entry->watches = watch;
  174. if (entry->unique_name && connect_func)
  175. watch->connect_func(cache->bus, watch->user_data);
  176. return watch->id;
  177. }
  178. static bool service_watch_remove(const void *key, void *value, void *user_data)
  179. {
  180. struct name_cache_entry *entry = value;
  181. struct service_watch **watch, *tmp;
  182. for (watch = &entry->watches; *watch;) {
  183. if ((*watch)->id) {
  184. watch = &(*watch)->next;
  185. continue;
  186. }
  187. tmp = *watch;
  188. *watch = tmp->next;
  189. service_watch_destroy(tmp);
  190. entry->ref_count--;
  191. }
  192. if (entry->ref_count)
  193. return false;
  194. name_cache_entry_destroy(entry);
  195. return true;
  196. }
  197. static void service_watch_remove_all(struct l_idle *idle, void *user_data)
  198. {
  199. struct _dbus_name_cache *cache = user_data;
  200. l_idle_remove(cache->watch_remove_work);
  201. cache->watch_remove_work = NULL;
  202. l_hashmap_foreach_remove(cache->names, service_watch_remove, cache);
  203. }
  204. static void service_watch_mark(const void *key, void *value, void *user_data)
  205. {
  206. struct name_cache_entry *entry = value;
  207. struct service_watch *watch;
  208. unsigned int *id = user_data;
  209. if (!*id)
  210. return;
  211. for (watch = entry->watches; watch; watch = watch->next)
  212. if (watch->id == *id) {
  213. watch->id = 0;
  214. watch->connect_func = NULL;
  215. watch->disconnect_func = NULL;
  216. if (watch->destroy) {
  217. watch->destroy(watch->user_data);
  218. watch->destroy = NULL;
  219. }
  220. *id = 0;
  221. break;
  222. }
  223. }
  224. bool _dbus_name_cache_remove_watch(struct _dbus_name_cache *cache,
  225. unsigned int id)
  226. {
  227. l_hashmap_foreach(cache->names, service_watch_mark, &id);
  228. if (id)
  229. return false;
  230. if (!cache->watch_remove_work)
  231. cache->watch_remove_work = l_idle_create(
  232. service_watch_remove_all,
  233. cache, NULL);
  234. return true;
  235. }