pubsub_check.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /* Copyright (c) 2001, Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * @file pubsub_check.c
  8. * @brief Enforce various requirements on a pubsub_builder.
  9. **/
  10. #define PUBSUB_PRIVATE
  11. #include "lib/dispatch/dispatch_naming.h"
  12. #include "lib/dispatch/msgtypes.h"
  13. #include "lib/pubsub/pubsub_flags.h"
  14. #include "lib/pubsub/pubsub_builder_st.h"
  15. #include "lib/pubsub/pubsub_build.h"
  16. #include "lib/container/bitarray.h"
  17. #include "lib/container/smartlist.h"
  18. #include "lib/log/util_bug.h"
  19. #include "lib/malloc/malloc.h"
  20. #include "lib/string/compat_string.h"
  21. #include <string.h>
  22. static void pubsub_adjmap_add(pubsub_adjmap_t *map,
  23. const pubsub_cfg_t *item);
  24. /**
  25. * Helper: contruct and return a new pubsub_adjacency_map from <b>cfg</b>.
  26. * Return NULL on error.
  27. **/
  28. static pubsub_adjmap_t *
  29. pubsub_build_adjacency_map(const pubsub_items_t *cfg)
  30. {
  31. pubsub_adjmap_t *map = tor_malloc_zero(sizeof(*map));
  32. const size_t n_subsystems = get_num_subsys_ids();
  33. const size_t n_msgs = get_num_message_ids();
  34. map->n_subsystems = n_subsystems;
  35. map->n_msgs = n_msgs;
  36. map->pub_by_subsys = tor_calloc(n_subsystems, sizeof(smartlist_t*));
  37. map->sub_by_subsys = tor_calloc(n_subsystems, sizeof(smartlist_t*));
  38. map->pub_by_msg = tor_calloc(n_msgs, sizeof(smartlist_t*));
  39. map->sub_by_msg = tor_calloc(n_msgs, sizeof(smartlist_t*));
  40. SMARTLIST_FOREACH_BEGIN(cfg->items, const pubsub_cfg_t *, item) {
  41. pubsub_adjmap_add(map, item);
  42. } SMARTLIST_FOREACH_END(item);
  43. return map;
  44. }
  45. /**
  46. * Helper: add a single pubsub_cfg_t to an adjacency map.
  47. **/
  48. static void
  49. pubsub_adjmap_add(pubsub_adjmap_t *map,
  50. const pubsub_cfg_t *item)
  51. {
  52. smartlist_t **by_subsys;
  53. smartlist_t **by_msg;
  54. tor_assert(item->subsys < map->n_subsystems);
  55. tor_assert(item->msg < map->n_msgs);
  56. if (item->is_publish) {
  57. by_subsys = &map->pub_by_subsys[item->subsys];
  58. by_msg = &map->pub_by_msg[item->msg];
  59. } else {
  60. by_subsys = &map->sub_by_subsys[item->subsys];
  61. by_msg = &map->sub_by_msg[item->msg];
  62. }
  63. if (! *by_subsys)
  64. *by_subsys = smartlist_new();
  65. if (! *by_msg)
  66. *by_msg = smartlist_new();
  67. smartlist_add(*by_subsys, (void*) item);
  68. smartlist_add(*by_msg, (void *) item);
  69. }
  70. /**
  71. * Release all storage held by m and set m to NULL.
  72. **/
  73. #define pubsub_adjmap_free(m) \
  74. FREE_AND_NULL(pubsub_adjmap_t, pubsub_adjmap_free_, m)
  75. /**
  76. * Free every element of an <b>n</b>-element array of smartlists, then
  77. * free the array itself.
  78. **/
  79. static void
  80. pubsub_adjmap_free_helper(smartlist_t **lsts, size_t n)
  81. {
  82. if (!lsts)
  83. return;
  84. for (unsigned i = 0; i < n; ++i) {
  85. smartlist_free(lsts[i]);
  86. }
  87. tor_free(lsts);
  88. }
  89. /**
  90. * Release all storage held by <b>map</b>.
  91. **/
  92. static void
  93. pubsub_adjmap_free_(pubsub_adjmap_t *map)
  94. {
  95. if (!map)
  96. return;
  97. pubsub_adjmap_free_helper(map->pub_by_subsys, map->n_subsystems);
  98. pubsub_adjmap_free_helper(map->sub_by_subsys, map->n_subsystems);
  99. pubsub_adjmap_free_helper(map->pub_by_msg, map->n_msgs);
  100. pubsub_adjmap_free_helper(map->sub_by_msg, map->n_msgs);
  101. tor_free(map);
  102. }
  103. /**
  104. * Helper: return the length of <b>sl</b>, or 0 if sl is NULL.
  105. **/
  106. static int
  107. smartlist_len_opt(const smartlist_t *sl)
  108. {
  109. if (sl)
  110. return smartlist_len(sl);
  111. else
  112. return 0;
  113. }
  114. /** Return a pointer to a statically allocated string encoding the
  115. * dispatcher flags in <b>flags</b>. */
  116. static const char *
  117. format_flags(unsigned flags)
  118. {
  119. static char buf[32];
  120. buf[0] = 0;
  121. if (flags & DISP_FLAG_EXCL) {
  122. strlcat(buf, " EXCL", sizeof(buf));
  123. }
  124. if (flags & DISP_FLAG_STUB) {
  125. strlcat(buf, " STUB", sizeof(buf));
  126. }
  127. return buf[0] ? buf+1 : buf;
  128. }
  129. /**
  130. * Log a message containing a description of <b>cfg</b> at severity, prefixed
  131. * by the string <b>prefix</b>.
  132. */
  133. static void
  134. pubsub_cfg_dump(const pubsub_cfg_t *cfg, int severity, const char *prefix)
  135. {
  136. if (!prefix)
  137. prefix = 0;
  138. tor_log(severity, LD_MESG,
  139. "%s%s %s: %s{%s} on %s (%s) <%u %u %u %u %x> [%s:%d]",
  140. prefix,
  141. get_subsys_id_name(cfg->subsys),
  142. cfg->is_publish ? "PUB" : "SUB",
  143. get_message_id_name(cfg->msg),
  144. get_msg_type_id_name(cfg->type),
  145. get_channel_id_name(cfg->channel),
  146. format_flags(cfg->flags),
  147. cfg->subsys, cfg->msg, cfg->type, cfg->channel, cfg->flags,
  148. cfg->added_by_file, cfg->added_by_line);
  149. }
  150. /**
  151. * Check whether there are any errors or inconsistencies for the message
  152. * described by <b>msg</b> in <b>map</b>. If there are problems, log about
  153. * them, and return -1. Otherwise return 0.
  154. **/
  155. static int
  156. lint_message(const pubsub_adjmap_t *map, message_id_t msg)
  157. {
  158. /* NOTE: Some of the checks in this function are maybe over-zealous, and we
  159. * might not want to have them forever. I've marked them with [?] below.
  160. */
  161. if (BUG(msg >= map->n_msgs))
  162. return 0; // LCOV_EXCL_LINE
  163. const smartlist_t *pub = map->pub_by_msg[msg];
  164. const smartlist_t *sub = map->sub_by_msg[msg];
  165. const size_t n_pub = smartlist_len_opt(pub);
  166. const size_t n_sub = smartlist_len_opt(sub);
  167. if (n_pub == 0 && n_sub == 0) {
  168. log_info(LD_MESG, "Nobody is publishing or subscribing to message %u "
  169. "(%s).",
  170. msg, get_message_id_name(msg));
  171. return 0; // No publishers or subscribers: nothing to do.
  172. }
  173. /* We'll set this to false if there are any problems. */
  174. bool ok = true;
  175. /* First make sure that if there are publishers, there are subscribers. */
  176. if (n_pub == 0) {
  177. log_warn(LD_MESG|LD_BUG,
  178. "Message %u (%s) has subscribers, but no publishers.",
  179. msg, get_message_id_name(msg));
  180. ok = false;
  181. } else if (n_sub == 0) {
  182. log_warn(LD_MESG|LD_BUG,
  183. "Message %u (%s) has publishers, but no subscribers.",
  184. msg, get_message_id_name(msg));
  185. ok = false;
  186. }
  187. /* The 'all' list has the publishers and the subscribers. */
  188. smartlist_t *all = smartlist_new();
  189. if (pub)
  190. smartlist_add_all(all, pub);
  191. if (sub)
  192. smartlist_add_all(all, sub);
  193. const pubsub_cfg_t *item0 = smartlist_get(all, 0);
  194. /* Indicates which subsystems we've found publishing/subscribing here. */
  195. bitarray_t *published_by = bitarray_init_zero((unsigned)map->n_subsystems);
  196. bitarray_t *subscribed_by = bitarray_init_zero((unsigned)map->n_subsystems);
  197. bool pub_excl = false, sub_excl = false, chan_same = true, type_same = true;
  198. /* Make sure that the messages all have the same channel and type;
  199. * check whether the DISP_FLAG_EXCL flag is used;
  200. * and if any subsystem is publishing or subscribing to it twice [??].
  201. */
  202. SMARTLIST_FOREACH_BEGIN(all, const pubsub_cfg_t *, cfg) {
  203. if (cfg->channel != item0->channel) {
  204. chan_same = false;
  205. }
  206. if (cfg->type != item0->type) {
  207. type_same = false;
  208. }
  209. if (cfg->flags & DISP_FLAG_EXCL) {
  210. if (cfg->is_publish)
  211. pub_excl = true;
  212. else
  213. sub_excl = true;
  214. }
  215. if (cfg->is_publish) {
  216. if (bitarray_is_set(published_by, cfg->subsys)) {
  217. log_warn(LD_MESG|LD_BUG,
  218. "Message %u (%s) is configured to be published by subsystem "
  219. "%u (%s) more than once.",
  220. msg, get_message_id_name(msg),
  221. cfg->subsys, get_subsys_id_name(cfg->subsys));
  222. ok = false;
  223. }
  224. bitarray_set(published_by, cfg->subsys);
  225. } else {
  226. if (bitarray_is_set(subscribed_by, cfg->subsys)) {
  227. log_warn(LD_MESG|LD_BUG,
  228. "Message %u (%s) is configured to be subscribed by subsystem "
  229. "%u (%s) more than once.",
  230. msg, get_message_id_name(msg),
  231. cfg->subsys, get_subsys_id_name(cfg->subsys));
  232. ok = false;
  233. }
  234. bitarray_set(subscribed_by, cfg->subsys);
  235. }
  236. } SMARTLIST_FOREACH_END(cfg);
  237. /* Check whether any subsystem is publishing and subscribing the same
  238. * message. [??]
  239. */
  240. for (unsigned i = 0; i < map->n_subsystems; ++i) {
  241. if (bitarray_is_set(published_by, i) &&
  242. bitarray_is_set(subscribed_by, i)) {
  243. log_warn(LD_MESG|LD_BUG,
  244. "Message %u (%s) is published and subscribed by the same "
  245. "subsystem %u (%s)",
  246. msg, get_message_id_name(msg),
  247. i, get_subsys_id_name(i));
  248. ok = false;
  249. }
  250. }
  251. if (! chan_same) {
  252. log_warn(LD_MESG|LD_BUG,
  253. "Message %u (%s) is associated with multiple inconsistent "
  254. "channels.",
  255. msg, get_message_id_name(msg));
  256. ok = false;
  257. }
  258. if (! type_same) {
  259. log_warn(LD_MESG|LD_BUG,
  260. "Message %u (%s) is associated with multiple inconsistent "
  261. "message types.",
  262. msg, get_message_id_name(msg));
  263. ok = false;
  264. }
  265. /* Enforce exclusive-ness for publishers and subscribers that have asked for
  266. * it.
  267. */
  268. if (pub_excl && smartlist_len(pub) > 1) {
  269. log_warn(LD_MESG|LD_BUG,
  270. "Message %u (%s) has multiple publishers, but at least one is "
  271. "marked as exclusive.",
  272. msg, get_message_id_name(msg));
  273. ok = false;
  274. }
  275. if (sub_excl && smartlist_len(sub) > 1) {
  276. log_warn(LD_MESG|LD_BUG,
  277. "Message %u (%s) has multiple subscribers, but at least one is "
  278. "marked as exclusive.",
  279. msg, get_message_id_name(msg));
  280. ok = false;
  281. }
  282. if (!ok) {
  283. /* There was a problem -- let's log all the publishers and subscribers on
  284. * this message */
  285. SMARTLIST_FOREACH(all, pubsub_cfg_t *, cfg,
  286. pubsub_cfg_dump(cfg, LOG_WARN, " "));
  287. }
  288. smartlist_free(all);
  289. bitarray_free(published_by);
  290. bitarray_free(subscribed_by);
  291. return ok ? 0 : -1;
  292. }
  293. /**
  294. * Check all the messages in <b>map</b> for consistency. Return 0 on success,
  295. * -1 on problems.
  296. **/
  297. static int
  298. pubsub_adjmap_check(const pubsub_adjmap_t *map)
  299. {
  300. bool all_ok = true;
  301. for (unsigned i = 0; i < map->n_msgs; ++i) {
  302. if (lint_message(map, i) < 0) {
  303. all_ok = false;
  304. }
  305. }
  306. return all_ok ? 0 : -1;
  307. }
  308. /**
  309. * Check builder for consistency and various constraints. Return 0 on success,
  310. * -1 on failure.
  311. **/
  312. int
  313. pubsub_builder_check(pubsub_builder_t *builder)
  314. {
  315. pubsub_adjmap_t *map = pubsub_build_adjacency_map(builder->items);
  316. int rv = -1;
  317. if (!map)
  318. goto err; // should be impossible
  319. if (pubsub_adjmap_check(map) < 0)
  320. goto err;
  321. rv = 0;
  322. err:
  323. pubsub_adjmap_free(map);
  324. return rv;
  325. }