| 1 |
/* |
| 2 |
* D-Bus Match Rules |
| 3 |
*/ |
| 4 |
|
| 5 |
#include <c-dvar.h> |
| 6 |
#include <c-list.h> |
| 7 |
#include <c-rbtree.h> |
| 8 |
#include <c-stdaux.h> |
| 9 |
#include "bus/match.h" |
| 10 |
#include "dbus/address.h" |
| 11 |
#include "dbus/message.h" |
| 12 |
#include "dbus/protocol.h" |
| 13 |
#include "util/error.h" |
| 14 |
#include "util/misc.h" |
| 15 |
#include "util/string.h" |
| 16 |
|
| 17 |
static bool match_key_equal(const char *key1, const char *key2, size_t n_key2) { |
| 18 |
if (strlen(key1) != n_key2) |
| 19 |
return false; |
| 20 |
|
| 21 |
return !strncmp(key1, key2, n_key2); |
| 22 |
} |
| 23 |
|
| 24 |
static int match_keys_assign(MatchKeys *keys, const char *key, size_t n_key, const char *value, bool allow_eavesdrop) { |
| 25 |
if (match_key_equal("type", key, n_key)) { |
| 26 |
if (keys->filter.type != DBUS_MESSAGE_TYPE_INVALID) |
| 27 |
return MATCH_E_INVALID; |
| 28 |
|
| 29 |
if (strcmp(value, "signal") == 0) |
| 30 |
keys->filter.type = DBUS_MESSAGE_TYPE_SIGNAL; |
| 31 |
else if (strcmp(value, "method_call") == 0) |
| 32 |
keys->filter.type = DBUS_MESSAGE_TYPE_METHOD_CALL; |
| 33 |
else if (strcmp(value, "method_return") == 0) |
| 34 |
keys->filter.type = DBUS_MESSAGE_TYPE_METHOD_RETURN; |
| 35 |
else if (strcmp(value, "error") == 0) |
| 36 |
keys->filter.type = DBUS_MESSAGE_TYPE_ERROR; |
| 37 |
else |
| 38 |
return MATCH_E_INVALID; |
| 39 |
} else if (match_key_equal("sender", key, n_key)) { |
| 40 |
Address addr; |
| 41 |
|
| 42 |
if (keys->sender) |
| 43 |
return MATCH_E_INVALID; |
| 44 |
if (!dbus_validate_name(value, strlen(value))) |
| 45 |
return MATCH_E_INVALID; |
| 46 |
keys->sender = value; |
| 47 |
|
| 48 |
address_from_string(&addr, value); |
| 49 |
if (addr.type == ADDRESS_TYPE_ID) { |
| 50 |
/* |
| 51 |
* Usually rules are indexed by sender, however, we also allow rules that match |
| 52 |
* on a sender ID to not be indexed, in case the peer with the ID does not yet |
| 53 |
* exits. Therefore, we must also remember the sender id. |
| 54 |
*/ |
| 55 |
keys->filter.sender = addr.id; |
| 56 |
} |
| 57 |
} else if (match_key_equal("destination", key, n_key)) { |
| 58 |
if (keys->destination) |
| 59 |
return MATCH_E_INVALID; |
| 60 |
if (!dbus_validate_name(value, strlen(value))) |
| 61 |
return MATCH_E_INVALID; |
| 62 |
keys->destination = value; |
| 63 |
} else if (match_key_equal("interface", key, n_key)) { |
| 64 |
if (keys->filter.interface) |
| 65 |
return MATCH_E_INVALID; |
| 66 |
if (!dbus_validate_interface(value, strlen(value))) |
| 67 |
return MATCH_E_INVALID; |
| 68 |
keys->filter.interface = value; |
| 69 |
} else if (match_key_equal("member", key, n_key)) { |
| 70 |
if (keys->filter.member) |
| 71 |
return MATCH_E_INVALID; |
| 72 |
if (!dbus_validate_member(value, strlen(value))) |
| 73 |
return MATCH_E_INVALID; |
| 74 |
keys->filter.member = value; |
| 75 |
} else if (match_key_equal("path", key, n_key)) { |
| 76 |
if (keys->filter.path || keys->path_namespace) |
| 77 |
return MATCH_E_INVALID; |
| 78 |
if (!c_dvar_is_path(value, strlen(value))) |
| 79 |
return MATCH_E_INVALID; |
| 80 |
keys->filter.path = value; |
| 81 |
} else if (match_key_equal("path_namespace", key, n_key)) { |
| 82 |
if (keys->path_namespace || keys->filter.path) |
| 83 |
return MATCH_E_INVALID; |
| 84 |
if (!c_dvar_is_path(value, strlen(value))) |
| 85 |
return MATCH_E_INVALID; |
| 86 |
keys->path_namespace = value; |
| 87 |
} else if (match_key_equal("eavesdrop", key, n_key)) { |
| 88 |
/* |
| 89 |
* If the caller explicitly allows eavesdrop filters, we parse |
| 90 |
* them but immediately discard it. We only support this to |
| 91 |
* allow BecomeMonitor() to work with legacy eavesdrop filters |
| 92 |
* which `dbus-monitor` seems to enforce on all its matches. |
| 93 |
* In all other cases, we never support eavesdropping, nor do |
| 94 |
* we allow such filters to be parsed. |
| 95 |
*/ |
| 96 |
if (!allow_eavesdrop) |
| 97 |
return MATCH_E_INVALID; |
| 98 |
if (strcmp(value, "true") != 0 && strcmp(value, "false") != 0) |
| 99 |
return MATCH_E_INVALID; |
| 100 |
} else if (match_key_equal("arg0namespace", key, n_key)) { |
| 101 |
if (keys->arg0namespace || keys->filter.args[0] || keys->filter.argpaths[0]) |
| 102 |
return MATCH_E_INVALID; |
| 103 |
if (!dbus_validate_namespace(value, strlen(value))) |
| 104 |
return MATCH_E_INVALID; |
| 105 |
keys->arg0namespace = value; |
| 106 |
} else if (n_key >= strlen("arg") && match_key_equal("arg", key, strlen("arg"))) { |
| 107 |
unsigned int i = 0; |
| 108 |
|
| 109 |
key += strlen("arg"); |
| 110 |
n_key -= strlen("arg"); |
| 111 |
|
| 112 |
for (unsigned int j = 0; j < 2 && n_key; ++j, ++key, --n_key) { |
| 113 |
if (*key < '0' || *key > '9') |
| 114 |
break; |
| 115 |
|
| 116 |
i = i * 10 + *key - '0'; |
| 117 |
} |
| 118 |
|
| 119 |
if (i == 0 && keys->arg0namespace) |
| 120 |
return MATCH_E_INVALID; |
| 121 |
if (i > 63) |
| 122 |
return MATCH_E_INVALID; |
| 123 |
|
| 124 |
if (keys->filter.args[i] || keys->filter.argpaths[i]) |
| 125 |
return MATCH_E_INVALID; |
| 126 |
|
| 127 |
if (match_key_equal("", key, n_key)) { |
| 128 |
keys->filter.args[i] = value; |
| 129 |
if (i + 1 > keys->filter.n_args) |
| 130 |
keys->filter.n_args = i + 1; |
| 131 |
} else if (match_key_equal("path", key, n_key)) { |
| 132 |
keys->filter.argpaths[i] = value; |
| 133 |
if (i + 1 > keys->filter.n_argpaths) |
| 134 |
keys->filter.n_argpaths = i + 1; |
| 135 |
} else |
| 136 |
return MATCH_E_INVALID; |
| 137 |
} else { |
| 138 |
return MATCH_E_INVALID; |
| 139 |
} |
| 140 |
|
| 141 |
return 0; |
| 142 |
} |
| 143 |
|
| 144 |
static int match_copy_value(const char **match, char **p) { |
| 145 |
const char *m = *match; |
| 146 |
bool quoted = false; |
| 147 |
char c; |
| 148 |
|
| 149 |
/* |
| 150 |
* Within single quotes (apostrophe), a backslash represents itself, |
| 151 |
* and an apostrophe ends the quoted section. Outside single quotes, \' |
| 152 |
* (backslash, apostrophe) represents an apostrophe, and any backslash |
| 153 |
* not followed by an apostrophe represents itself. |
| 154 |
* |
| 155 |
* Note that this is quite counter-intuitive to everyone used to |
| 156 |
* shell-style quoting. However, we strictly follow the D-Bus |
| 157 |
* specification and reference-implementation here! |
| 158 |
*/ |
| 159 |
|
| 160 |
do { |
| 161 |
for ( ; *m == '\''; ++m) |
| 162 |
quoted = !quoted; |
| 163 |
|
| 164 |
switch ((c = *m++)) { |
| 165 |
case 0: |
| 166 |
--m; /* leave zero-terminating for caller */ |
| 167 |
break; |
| 168 |
case ',': |
| 169 |
c = quoted ? ',' : 0; |
| 170 |
break; |
| 171 |
case '\\': |
| 172 |
c = (!quoted && *m == '\'') ? *m++ : '\\'; |
| 173 |
break; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
**p = c; |
| 178 |
} while (*(*p)++); |
| 179 |
|
| 180 |
if (quoted) |
| 181 |
return MATCH_E_INVALID; |
| 182 |
|
| 183 |
*match = m; |
| 184 |
return 0; |
| 185 |
} |
| 186 |
|
| 187 |
static int match_parse_key(const char **match, const char **keyp, size_t *n_keyp) { |
| 188 |
const char *key; |
| 189 |
size_t n_key = 0; |
| 190 |
|
| 191 |
/* skip any leading whitespace and stray equal signs */ |
| 192 |
*match += strspn(*match, " \t\n\r="); |
| 193 |
if (!**match) |
| 194 |
return MATCH_E_EOF; |
| 195 |
|
| 196 |
/* skip over the key, recording its length */ |
| 197 |
n_key = strcspn(*match, " \t\n\r="); |
| 198 |
key = *match; |
| 199 |
*match += n_key; |
| 200 |
if (!**match) |
| 201 |
return MATCH_E_INVALID; |
| 202 |
|
| 203 |
/* drop trailing whitespace */ |
| 204 |
*match += strspn(*match, " \t\n\r"); |
| 205 |
|
| 206 |
/* skip over the equals sign between the key and the value */ |
| 207 |
if (**match != '=') |
| 208 |
return MATCH_E_INVALID; |
| 209 |
else |
| 210 |
++*match; |
| 211 |
|
| 212 |
*keyp = key; |
| 213 |
*n_keyp = n_key; |
| 214 |
return 0; |
| 215 |
} |
| 216 |
|
| 217 |
static int match_keys_parse(MatchKeys *keys, const char *string, bool allow_eavesdrop) { |
| 218 |
const char *key, *value; |
| 219 |
size_t n_key, n_buffer; |
| 220 |
char *p; |
| 221 |
int r; |
| 222 |
|
| 223 |
/* |
| 224 |
* Parse the rule-string @string into @keys. We repeatedly pop off a |
| 225 |
* key from @string and copy over the value into @keys, remembering the |
| 226 |
* pointer to it. If anything fails, we simply bail out. |
| 227 |
* |
| 228 |
* Note that we rely on @string to be zero-terminated! |
| 229 |
*/ |
| 230 |
|
| 231 |
p = keys->buffer; |
| 232 |
|
| 233 |
for (;;) { |
| 234 |
r = match_parse_key(&string, &key, &n_key); |
| 235 |
if (r) |
| 236 |
break; |
| 237 |
|
| 238 |
value = p; |
| 239 |
r = match_copy_value(&string, &p); |
| 240 |
if (r) |
| 241 |
break; |
| 242 |
|
| 243 |
r = match_keys_assign(keys, key, n_key, value, allow_eavesdrop); |
| 244 |
if (r) |
| 245 |
break; |
| 246 |
} |
| 247 |
|
| 248 |
n_buffer = p - keys->buffer; |
| 249 |
|
| 250 |
c_assert(n_buffer <= keys->n_buffer); |
| 251 |
|
| 252 |
keys->n_buffer = p - keys->buffer; |
| 253 |
|
| 254 |
return (r == MATCH_E_EOF) ? 0 : error_trace(r); |
| 255 |
} |
| 256 |
|
| 257 |
static void match_keys_deinit(MatchKeys *keys) { |
| 258 |
*keys = (MatchKeys)MATCH_KEYS_NULL; |
| 259 |
} |
| 260 |
|
| 261 |
C_DEFINE_CLEANUP(MatchKeys *, match_keys_deinit); |
| 262 |
|
| 263 |
static int match_keys_init(MatchKeys *k, const char *string, size_t n_string, bool allow_eavesdrop) { |
| 264 |
_c_cleanup_(match_keys_deinitp) MatchKeys *keys = k; |
| 265 |
int r; |
| 266 |
|
| 267 |
c_assert(n_string > 0); |
| 268 |
c_assert(n_string - 1 <= MATCH_RULE_LENGTH_MAX); |
| 269 |
|
| 270 |
*keys = (MatchKeys)MATCH_KEYS_NULL; |
| 271 |
keys->n_buffer = n_string; |
| 272 |
|
| 273 |
r = match_keys_parse(keys, string, allow_eavesdrop); |
| 274 |
if (r) |
| 275 |
return error_trace(r); |
| 276 |
|
| 277 |
keys = NULL; |
| 278 |
return 0; |
| 279 |
} |
| 280 |
|
| 281 |
static int match_keys_clone(MatchKeys *k, MatchKeys *old) { |
| 282 |
_c_cleanup_(match_keys_deinitp) MatchKeys *keys = k; |
| 283 |
size_t n_buffer; |
| 284 |
char *p; |
| 285 |
|
| 286 |
*keys = (MatchKeys)MATCH_KEYS_NULL; |
| 287 |
keys->n_buffer = old->n_buffer; |
| 288 |
|
| 289 |
keys->filter.type = old->filter.type; |
| 290 |
keys->filter.sender = old->filter.sender; |
| 291 |
|
| 292 |
p = keys->buffer; |
| 293 |
|
| 294 |
if (old->filter.interface) { |
| 295 |
keys->filter.interface = p; |
| 296 |
p = stpcpy(p, old->filter.interface) + 1; |
| 297 |
} |
| 298 |
|
| 299 |
if (old->filter.member) { |
| 300 |
keys->filter.member = p; |
| 301 |
p = stpcpy(p, old->filter.member) + 1; |
| 302 |
} |
| 303 |
|
| 304 |
if (old->filter.path) { |
| 305 |
keys->filter.path = p; |
| 306 |
p = stpcpy(p, old->filter.path) + 1; |
| 307 |
} |
| 308 |
|
| 309 |
for (size_t i = 0; i < old->filter.n_args; ++i) { |
| 310 |
if (old->filter.args[i]) { |
| 311 |
keys->filter.args[i] = p; |
| 312 |
p = stpcpy(p, old->filter.args[i]) + 1; |
| 313 |
} |
| 314 |
} |
| 315 |
keys->filter.n_args = old->filter.n_args; |
| 316 |
|
| 317 |
for (size_t i = 0; i < old->filter.n_argpaths; ++i) { |
| 318 |
if (old->filter.argpaths[i]) { |
| 319 |
keys->filter.argpaths[i] = p; |
| 320 |
p = stpcpy(p, old->filter.argpaths[i]) + 1; |
| 321 |
} |
| 322 |
} |
| 323 |
keys->filter.n_argpaths = old->filter.n_argpaths; |
| 324 |
|
| 325 |
if (old->destination) { |
| 326 |
keys->destination = p; |
| 327 |
p = stpcpy(p, old->destination) + 1; |
| 328 |
} |
| 329 |
|
| 330 |
if (old->sender) { |
| 331 |
keys->sender = p; |
| 332 |
p = stpcpy(p, old->sender) + 1; |
| 333 |
} |
| 334 |
|
| 335 |
if (old->path_namespace) { |
| 336 |
keys->path_namespace = p; |
| 337 |
p = stpcpy(p, old->path_namespace) + 1; |
| 338 |
} |
| 339 |
|
| 340 |
if (old->arg0namespace) { |
| 341 |
keys->arg0namespace = p; |
| 342 |
p = stpcpy(p, old->arg0namespace) + 1; |
| 343 |
} |
| 344 |
|
| 345 |
n_buffer = p - keys->buffer; |
| 346 |
|
| 347 |
c_assert(n_buffer <= keys->n_buffer); |
| 348 |
|
| 349 |
keys->n_buffer = p - keys->buffer; |
| 350 |
|
| 351 |
|
| 352 |
keys = NULL; |
| 353 |
return 0; |
| 354 |
} |
| 355 |
|
| 356 |
static MatchKeys *match_keys_free(MatchKeys *keys) { |
| 357 |
if (keys) { |
| 358 |
match_keys_deinit(keys); |
| 359 |
free(keys); |
| 360 |
} |
| 361 |
return NULL; |
| 362 |
} |
| 363 |
|
| 364 |
C_DEFINE_CLEANUP(MatchKeys *, match_keys_free); |
| 365 |
|
| 366 |
static int match_keys_new(MatchKeys **keysp, const char *string) { |
| 367 |
_c_cleanup_(match_keys_freep) MatchKeys *keys = NULL; |
| 368 |
size_t n_string; |
| 369 |
int r; |
| 370 |
|
| 371 |
n_string = strlen(string) + 1; |
| 372 |
if (n_string - 1 > MATCH_RULE_LENGTH_MAX) |
| 373 |
return MATCH_E_INVALID; |
| 374 |
|
| 375 |
keys = calloc(1, sizeof(*keys) + n_string); |
| 376 |
if (!keys) |
| 377 |
return error_origin(-ENOMEM); |
| 378 |
|
| 379 |
r = match_keys_init(keys, string, n_string, false); |
| 380 |
if (r) |
| 381 |
return error_trace(r); |
| 382 |
|
| 383 |
*keysp = keys; |
| 384 |
keys = NULL; |
| 385 |
return 0; |
| 386 |
} |
| 387 |
|
| 388 |
static bool match_string_prefix(const char *string, const char *prefix, char delimiter, bool delimiter_included) { |
| 389 |
char *tail; |
| 390 |
|
| 391 |
if (string == prefix) |
| 392 |
return true; |
| 393 |
|
| 394 |
if (!string || !prefix) |
| 395 |
return false; |
| 396 |
|
| 397 |
tail = string_prefix(string, prefix); |
| 398 |
if (!tail) |
| 399 |
return false; |
| 400 |
|
| 401 |
if (delimiter_included) { |
| 402 |
if (tail == string || (*tail != '\0' && *(tail - 1) != delimiter)) |
| 403 |
return false; |
| 404 |
} else { |
| 405 |
if (*tail != '\0' && *tail != delimiter) |
| 406 |
return false; |
| 407 |
} |
| 408 |
|
| 409 |
return true; |
| 410 |
} |
| 411 |
|
| 412 |
static bool match_keys_match_metadata(MatchKeys *keys, MessageMetadata *metadata) { |
| 413 |
if (keys->filter.n_args > metadata->n_args) |
| 414 |
return false; |
| 415 |
|
| 416 |
if (keys->filter.n_argpaths > metadata->n_args) |
| 417 |
return false; |
| 418 |
|
| 419 |
if (keys->path_namespace && !match_string_prefix(metadata->fields.path, keys->path_namespace, '/', false)) |
| 420 |
return false; |
| 421 |
|
| 422 |
if (keys->arg0namespace && !(metadata->args[0].element == 's' && match_string_prefix(metadata->args[0].value, keys->arg0namespace, '.', false))) |
| 423 |
return false; |
| 424 |
|
| 425 |
for (unsigned int i = 0; i < keys->filter.n_args || i < keys->filter.n_argpaths; i ++) { |
| 426 |
if (keys->filter.args[i] && !(metadata->args[i].element == 's' && string_equal(keys->filter.args[i], metadata->args[i].value))) |
| 427 |
return false; |
| 428 |
|
| 429 |
if (keys->filter.argpaths[i]) { |
| 430 |
if (!match_string_prefix(metadata->args[i].value, keys->filter.argpaths[i], '/', true) && |
| 431 |
!match_string_prefix(keys->filter.argpaths[i], metadata->args[i].value, '/', true)) |
| 432 |
return false; |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
if (keys->filter.type != DBUS_MESSAGE_TYPE_INVALID && keys->filter.type != metadata->header.type) |
| 437 |
return false; |
| 438 |
|
| 439 |
if (keys->filter.sender != ADDRESS_ID_INVALID && keys->filter.sender != metadata->sender_id) |
| 440 |
return false; |
| 441 |
|
| 442 |
if (keys->filter.interface && !string_equal(keys->filter.interface, metadata->fields.interface)) |
| 443 |
return false; |
| 444 |
|
| 445 |
if (keys->filter.member && !string_equal(keys->filter.member, metadata->fields.member)) |
| 446 |
return false; |
| 447 |
|
| 448 |
if (keys->filter.path && !string_equal(keys->filter.path, metadata->fields.path)) |
| 449 |
return false; |
| 450 |
|
| 451 |
return true; |
| 452 |
} |
| 453 |
|
| 454 |
static int match_registry_by_path_compare(CRBTree *tree, void *k, CRBNode *rb) { |
| 455 |
MatchRegistryByPath *registry = c_container_of(rb, MatchRegistryByPath, registry_node); |
| 456 |
const char *path1 = k ?: "", *path2 = registry->path; |
| 457 |
|
| 458 |
return strcmp(path1, path2); |
| 459 |
} |
| 460 |
|
| 461 |
static int match_registry_by_path_new(MatchRegistryByPath **registryp, const char *path) { |
| 462 |
MatchRegistryByPath *registry; |
| 463 |
size_t n_path; |
| 464 |
|
| 465 |
if (!path) |
| 466 |
path = ""; |
| 467 |
|
| 468 |
n_path = strlen(path); |
| 469 |
|
| 470 |
registry = malloc(sizeof(*registry) + n_path + 1); |
| 471 |
if (!registry) |
| 472 |
return error_origin(-ENOMEM); |
| 473 |
|
| 474 |
*registry = (MatchRegistryByPath)MATCH_REGISTRY_BY_PATH_INIT(*registry); |
| 475 |
strcpy(registry->path, path); |
| 476 |
|
| 477 |
*registryp = registry; |
| 478 |
return 0; |
| 479 |
} |
| 480 |
|
| 481 |
static MatchRegistryByPath *match_registry_by_path_ref(MatchRegistryByPath *registry) { |
| 482 |
if (!registry) |
| 483 |
return NULL; |
| 484 |
|
| 485 |
c_assert(registry->n_refs > 0); |
| 486 |
|
| 487 |
++registry->n_refs; |
| 488 |
|
| 489 |
return registry; |
| 490 |
} |
| 491 |
|
| 492 |
static MatchRegistryByPath *match_registry_by_path_unref(MatchRegistryByPath *registry) { |
| 493 |
if (!registry || --registry->n_refs > 0) |
| 494 |
return NULL; |
| 495 |
|
| 496 |
c_assert(c_rbtree_is_empty(®istry->interface_tree)); |
| 497 |
|
| 498 |
c_rbnode_unlink(®istry->registry_node); |
| 499 |
free(registry); |
| 500 |
|
| 501 |
return NULL; |
| 502 |
} |
| 503 |
|
| 504 |
C_DEFINE_CLEANUP(MatchRegistryByPath *, match_registry_by_path_unref); |
| 505 |
|
| 506 |
static void match_registry_by_path_link(MatchRegistryByPath *registry, CRBTree *tree, CRBNode *parent, CRBNode **slot) { |
| 507 |
c_rbtree_add(tree, parent, slot, ®istry->registry_node); |
| 508 |
} |
| 509 |
|
| 510 |
static int match_registry_by_interface_compare(CRBTree *tree, void *k, CRBNode *rb) { |
| 511 |
MatchRegistryByInterface *registry = c_container_of(rb, MatchRegistryByInterface, registry_node); |
| 512 |
const char *interface1 = k ?: "", *interface2 = registry->interface; |
| 513 |
|
| 514 |
return strcmp(interface1, interface2); |
| 515 |
} |
| 516 |
|
| 517 |
static int match_registry_by_interface_new(MatchRegistryByInterface **registryp, const char *interface) { |
| 518 |
MatchRegistryByInterface *registry; |
| 519 |
size_t n_interface; |
| 520 |
|
| 521 |
if (!interface) |
| 522 |
interface = ""; |
| 523 |
|
| 524 |
n_interface = strlen(interface); |
| 525 |
|
| 526 |
registry = malloc(sizeof(*registry) + n_interface + 1); |
| 527 |
if (!registry) |
| 528 |
return error_origin(-ENOMEM); |
| 529 |
|
| 530 |
*registry = (MatchRegistryByInterface)MATCH_REGISTRY_BY_INTERFACE_INIT(*registry); |
| 531 |
strcpy(registry->interface, interface); |
| 532 |
|
| 533 |
*registryp = registry; |
| 534 |
return 0; |
| 535 |
} |
| 536 |
|
| 537 |
static MatchRegistryByInterface *match_registry_by_interface_ref(MatchRegistryByInterface *registry) { |
| 538 |
if (!registry) |
| 539 |
return NULL; |
| 540 |
|
| 541 |
c_assert(registry->n_refs > 0); |
| 542 |
|
| 543 |
++registry->n_refs; |
| 544 |
|
| 545 |
return registry; |
| 546 |
} |
| 547 |
|
| 548 |
static MatchRegistryByInterface *match_registry_by_interface_unref(MatchRegistryByInterface *registry) { |
| 549 |
if (!registry || --registry->n_refs > 0) |
| 550 |
return NULL; |
| 551 |
|
| 552 |
c_assert(c_rbtree_is_empty(®istry->member_tree)); |
| 553 |
|
| 554 |
c_rbnode_unlink(®istry->registry_node); |
| 555 |
match_registry_by_path_unref(registry->registry_by_path); |
| 556 |
free(registry); |
| 557 |
|
| 558 |
return NULL; |
| 559 |
} |
| 560 |
|
| 561 |
C_DEFINE_CLEANUP(MatchRegistryByInterface *, match_registry_by_interface_unref); |
| 562 |
|
| 563 |
static void match_registry_by_interface_link(MatchRegistryByInterface *registry, MatchRegistryByPath *registry_by_path, CRBNode *parent, CRBNode **slot) { |
| 564 |
c_rbtree_add(®istry_by_path->interface_tree, parent, slot, ®istry->registry_node); |
| 565 |
registry->registry_by_path = match_registry_by_path_ref(registry_by_path); |
| 566 |
} |
| 567 |
|
| 568 |
static int match_registry_by_member_compare(CRBTree *tree, void *k, CRBNode *rb) { |
| 569 |
MatchRegistryByMember *registry = c_container_of(rb, MatchRegistryByMember, registry_node); |
| 570 |
const char *member1 = k ?: "", *member2 = registry->member; |
| 571 |
|
| 572 |
return strcmp(member1, member2); |
| 573 |
} |
| 574 |
|
| 575 |
static int match_registry_by_member_new(MatchRegistryByMember **registryp, const char *member) { |
| 576 |
MatchRegistryByMember *registry; |
| 577 |
size_t n_member; |
| 578 |
|
| 579 |
if (!member) |
| 580 |
member = ""; |
| 581 |
|
| 582 |
n_member = strlen(member); |
| 583 |
|
| 584 |
registry = malloc(sizeof(*registry) + n_member + 1); |
| 585 |
if (!registry) |
| 586 |
return error_origin(-ENOMEM); |
| 587 |
|
| 588 |
*registry = (MatchRegistryByMember)MATCH_REGISTRY_BY_MEMBER_INIT(*registry); |
| 589 |
strcpy(registry->member, member); |
| 590 |
|
| 591 |
*registryp = registry; |
| 592 |
return 0; |
| 593 |
} |
| 594 |
|
| 595 |
static MatchRegistryByMember *match_registry_by_member_ref(MatchRegistryByMember *registry) { |
| 596 |
if (!registry) |
| 597 |
return NULL; |
| 598 |
|
| 599 |
c_assert(registry->n_refs > 0); |
| 600 |
|
| 601 |
++registry->n_refs; |
| 602 |
|
| 603 |
return registry; |
| 604 |
} |
| 605 |
|
| 606 |
static MatchRegistryByMember *match_registry_by_member_unref(MatchRegistryByMember *registry) { |
| 607 |
if (!registry || --registry->n_refs > 0) |
| 608 |
return NULL; |
| 609 |
|
| 610 |
c_assert(c_rbtree_is_empty(®istry->keys_tree)); |
| 611 |
|
| 612 |
c_rbnode_unlink(®istry->registry_node); |
| 613 |
match_registry_by_interface_unref(registry->registry_by_interface); |
| 614 |
free(registry); |
| 615 |
|
| 616 |
return NULL; |
| 617 |
} |
| 618 |
|
| 619 |
C_DEFINE_CLEANUP(MatchRegistryByMember *, match_registry_by_member_unref); |
| 620 |
|
| 621 |
static void match_registry_by_member_link(MatchRegistryByMember *registry, MatchRegistryByInterface *registry_by_interface, CRBNode *parent, CRBNode **slot) { |
| 622 |
c_rbtree_add(®istry_by_interface->member_tree, parent, slot, ®istry->registry_node); |
| 623 |
registry->registry_by_interface = match_registry_by_interface_ref(registry_by_interface); |
| 624 |
} |
| 625 |
|
| 626 |
static int match_keys_compare(MatchKeys *key1, MatchKeys *key2) { |
| 627 |
int r; |
| 628 |
|
| 629 |
if ((r = string_compare(key1->sender, key2->sender)) || |
| 630 |
(r = string_compare(key1->destination, key2->destination)) || |
| 631 |
(r = string_compare(key1->filter.interface, key2->filter.interface)) || |
| 632 |
(r = string_compare(key1->filter.member, key2->filter.member)) || |
| 633 |
(r = string_compare(key1->filter.path, key2->filter.path)) || |
| 634 |
(r = string_compare(key1->path_namespace, key2->path_namespace)) || |
| 635 |
(r = string_compare(key1->arg0namespace, key2->arg0namespace))) |
| 636 |
return r; |
| 637 |
|
| 638 |
if (key1->filter.type > key2->filter.type) |
| 639 |
return 1; |
| 640 |
if (key1->filter.type < key2->filter.type) |
| 641 |
return -1; |
| 642 |
|
| 643 |
if (key1->filter.n_args < key2->filter.n_args) |
| 644 |
return -1; |
| 645 |
if (key1->filter.n_args > key2->filter.n_args) |
| 646 |
return 1; |
| 647 |
|
| 648 |
if (key1->filter.n_argpaths < key2->filter.n_argpaths) |
| 649 |
return -1; |
| 650 |
if (key1->filter.n_argpaths > key2->filter.n_argpaths) |
| 651 |
return 1; |
| 652 |
|
| 653 |
for (size_t i = 0; i < key1->filter.n_args || i < key1->filter.n_argpaths; i ++) { |
| 654 |
if ((r = string_compare(key1->filter.args[i], key2->filter.args[i])) || |
| 655 |
(r = string_compare(key1->filter.argpaths[i], key2->filter.argpaths[i]))) |
| 656 |
return r; |
| 657 |
} |
| 658 |
|
| 659 |
return 0; |
| 660 |
} |
| 661 |
|
| 662 |
static int match_registry_by_keys_compare(CRBTree *tree, void *k, CRBNode *rb) { |
| 663 |
MatchRegistryByKeys *registry = c_container_of(rb, MatchRegistryByKeys, registry_node); |
| 664 |
MatchKeys *keys1 = k, *keys2 = ®istry->keys; |
| 665 |
|
| 666 |
return match_keys_compare(keys1, keys2); |
| 667 |
} |
| 668 |
|
| 669 |
static int match_registry_by_keys_new(MatchRegistryByKeys **registryp, MatchKeys *keys) { |
| 670 |
MatchRegistryByKeys *registry; |
| 671 |
|
| 672 |
registry = malloc(sizeof(*registry) + keys->n_buffer); |
| 673 |
if (!registry) |
| 674 |
return error_origin(-ENOMEM); |
| 675 |
|
| 676 |
*registry = (MatchRegistryByKeys)MATCH_REGISTRY_BY_KEYS_INIT(*registry); |
| 677 |
match_keys_clone(®istry->keys, keys); |
| 678 |
|
| 679 |
*registryp = registry; |
| 680 |
return 0; |
| 681 |
} |
| 682 |
|
| 683 |
static MatchRegistryByKeys *match_registry_by_keys_ref(MatchRegistryByKeys *registry) { |
| 684 |
if (!registry) |
| 685 |
return NULL; |
| 686 |
|
| 687 |
c_assert(registry->n_refs > 0); |
| 688 |
|
| 689 |
++registry->n_refs; |
| 690 |
|
| 691 |
return registry; |
| 692 |
} |
| 693 |
|
| 694 |
static MatchRegistryByKeys *match_registry_by_keys_unref(MatchRegistryByKeys *registry) { |
| 695 |
if (!registry || --registry->n_refs > 0) |
| 696 |
return NULL; |
| 697 |
|
| 698 |
c_assert(c_list_is_empty(®istry->rule_list)); |
| 699 |
|
| 700 |
c_rbnode_unlink(®istry->registry_node); |
| 701 |
match_registry_by_member_unref(registry->registry_by_member); |
| 702 |
free(registry); |
| 703 |
|
| 704 |
return NULL; |
| 705 |
} |
| 706 |
|
| 707 |
C_DEFINE_CLEANUP(MatchRegistryByKeys *, match_registry_by_keys_unref); |
| 708 |
|
| 709 |
static void match_registry_by_keys_link(MatchRegistryByKeys *registry, MatchRegistryByMember *registry_by_member, CRBNode *parent, CRBNode **slot) { |
| 710 |
c_rbtree_add(®istry_by_member->keys_tree, parent, slot, ®istry->registry_node); |
| 711 |
registry->registry_by_member = match_registry_by_member_ref(registry_by_member); |
| 712 |
} |
| 713 |
|
| 714 |
static int match_rule_compare(CRBTree *tree, void *k, CRBNode *rb) { |
| 715 |
MatchRule *rule = c_container_of(rb, MatchRule, owner_node); |
| 716 |
MatchKeys *key1 = k, *key2 = &rule->keys; |
| 717 |
|
| 718 |
return match_keys_compare(key1, key2); |
| 719 |
} |
| 720 |
|
| 721 |
static MatchRule *match_rule_free(MatchRule *rule) { |
| 722 |
if (!rule) |
| 723 |
return NULL; |
| 724 |
|
| 725 |
c_assert(!rule->n_user_refs); |
| 726 |
|
| 727 |
match_keys_deinit(&rule->keys); |
| 728 |
user_charge_deinit(&rule->charge[1]); |
| 729 |
user_charge_deinit(&rule->charge[0]); |
| 730 |
c_rbnode_unlink(&rule->owner_node); |
| 731 |
match_rule_unlink(rule); |
| 732 |
free(rule); |
| 733 |
|
| 734 |
return NULL; |
| 735 |
} |
| 736 |
|
| 737 |
C_DEFINE_CLEANUP(MatchRule *, match_rule_free); |
| 738 |
|
| 739 |
static int match_rule_new(MatchRule **rulep, MatchOwner *owner, User *user, const char *string, bool allow_eavesdrop) { |
| 740 |
_c_cleanup_(match_rule_freep) MatchRule *rule = NULL; |
| 741 |
size_t n_string; |
| 742 |
int r; |
| 743 |
|
| 744 |
n_string = strlen(string) + 1; |
| 745 |
if (n_string - 1 > MATCH_RULE_LENGTH_MAX) |
| 746 |
return MATCH_E_INVALID; |
| 747 |
|
| 748 |
rule = calloc(1, sizeof(*rule) + n_string); |
| 749 |
if (!rule) |
| 750 |
return error_origin(-ENOMEM); |
| 751 |
|
| 752 |
*rule = (MatchRule)MATCH_RULE_NULL(*rule); |
| 753 |
rule->owner = owner; |
| 754 |
|
| 755 |
r = user_charge(user, &rule->charge[0], NULL, USER_SLOT_BYTES, sizeof(*rule) + n_string); |
| 756 |
r = r ?: user_charge(user, &rule->charge[1], NULL, USER_SLOT_MATCHES, 1); |
| 757 |
if (r) |
| 758 |
return (r == USER_E_QUOTA) ? MATCH_E_QUOTA : error_fold(r); |
| 759 |
|
| 760 |
r = match_keys_init(&rule->keys, string, n_string, allow_eavesdrop); |
| 761 |
if (r) |
| 762 |
return error_trace(r); |
| 763 |
|
| 764 |
*rulep = rule; |
| 765 |
rule = NULL; |
| 766 |
return 0; |
| 767 |
} |
| 768 |
|
| 769 |
/** |
| 770 |
* match_rule_user_ref() - XXX |
| 771 |
*/ |
| 772 |
MatchRule *match_rule_user_ref(MatchRule *rule) { |
| 773 |
if (!rule) |
| 774 |
return NULL; |
| 775 |
|
| 776 |
c_assert(rule->n_user_refs > 0); |
| 777 |
|
| 778 |
++rule->n_user_refs; |
| 779 |
|
| 780 |
return rule; |
| 781 |
} |
| 782 |
|
| 783 |
/** |
| 784 |
* match_rule_user_unref() - XXX |
| 785 |
*/ |
| 786 |
MatchRule *match_rule_user_unref(MatchRule *rule) { |
| 787 |
if (!rule) |
| 788 |
return NULL; |
| 789 |
|
| 790 |
c_assert(rule->n_user_refs > 0); |
| 791 |
|
| 792 |
--rule->n_user_refs; |
| 793 |
|
| 794 |
if (rule->n_user_refs == 0) |
| 795 |
match_rule_free(rule); |
| 796 |
|
| 797 |
return NULL; |
| 798 |
} |
| 799 |
|
| 800 |
static void match_rule_link_by_keys(MatchRule *rule, MatchRegistryByKeys *registry) { |
| 801 |
c_list_link_tail(®istry->rule_list, &rule->registry_link); |
| 802 |
rule->registry_by_keys = match_registry_by_keys_ref(registry); |
| 803 |
} |
| 804 |
|
| 805 |
static int match_rule_link_by_member(MatchRule *rule, MatchRegistryByMember *registry) { |
| 806 |
_c_cleanup_(match_registry_by_keys_unrefp) MatchRegistryByKeys *registry_by_keys = NULL; |
| 807 |
CRBNode **slot, *parent; |
| 808 |
int r; |
| 809 |
|
| 810 |
slot = c_rbtree_find_slot(®istry->keys_tree, match_registry_by_keys_compare, &rule->keys, &parent); |
| 811 |
if (!slot) { |
| 812 |
registry_by_keys = match_registry_by_keys_ref(c_rbnode_entry(parent, MatchRegistryByKeys, registry_node)); |
| 813 |
} else { |
| 814 |
r = match_registry_by_keys_new(®istry_by_keys, &rule->keys); |
| 815 |
if (r) |
| 816 |
return error_trace(r); |
| 817 |
|
| 818 |
match_registry_by_keys_link(registry_by_keys, registry, parent, slot); |
| 819 |
} |
| 820 |
|
| 821 |
match_rule_link_by_keys(rule, registry_by_keys); |
| 822 |
|
| 823 |
return 0; |
| 824 |
} |
| 825 |
|
| 826 |
static int match_rule_link_by_interface(MatchRule *rule, MatchRegistryByInterface *registry) { |
| 827 |
_c_cleanup_(match_registry_by_member_unrefp) MatchRegistryByMember *registry_by_member = NULL; |
| 828 |
CRBNode **slot, *parent; |
| 829 |
int r; |
| 830 |
|
| 831 |
slot = c_rbtree_find_slot(®istry->member_tree, match_registry_by_member_compare, rule->keys.filter.member, &parent); |
| 832 |
if (!slot) { |
| 833 |
registry_by_member = match_registry_by_member_ref(c_rbnode_entry(parent, MatchRegistryByMember, registry_node)); |
| 834 |
} else { |
| 835 |
r = match_registry_by_member_new(®istry_by_member, rule->keys.filter.member); |
| 836 |
if (r) |
| 837 |
return error_trace(r); |
| 838 |
|
| 839 |
match_registry_by_member_link(registry_by_member, registry, parent, slot); |
| 840 |
} |
| 841 |
|
| 842 |
r = match_rule_link_by_member(rule, registry_by_member); |
| 843 |
if (r) |
| 844 |
return error_trace(r); |
| 845 |
|
| 846 |
return 0; |
| 847 |
} |
| 848 |
|
| 849 |
static int match_rule_link_by_path(MatchRule *rule, MatchRegistryByPath *registry) { |
| 850 |
_c_cleanup_(match_registry_by_interface_unrefp) MatchRegistryByInterface *registry_by_interface = NULL; |
| 851 |
CRBNode **slot, *parent; |
| 852 |
int r; |
| 853 |
|
| 854 |
slot = c_rbtree_find_slot(®istry->interface_tree, match_registry_by_interface_compare, rule->keys.filter.interface, &parent); |
| 855 |
if (!slot) { |
| 856 |
registry_by_interface = match_registry_by_interface_ref(c_rbnode_entry(parent, MatchRegistryByInterface, registry_node)); |
| 857 |
} else { |
| 858 |
r = match_registry_by_interface_new(®istry_by_interface, rule->keys.filter.interface); |
| 859 |
if (r) |
| 860 |
return error_trace(r); |
| 861 |
|
| 862 |
match_registry_by_interface_link(registry_by_interface, registry, parent, slot); |
| 863 |
} |
| 864 |
|
| 865 |
r = match_rule_link_by_interface(rule, registry_by_interface); |
| 866 |
if (r) |
| 867 |
return error_trace(r); |
| 868 |
|
| 869 |
return 0; |
| 870 |
} |
| 871 |
|
| 872 |
/** |
| 873 |
* match_rule_link() - XXX |
| 874 |
*/ |
| 875 |
int match_rule_link(MatchRule *rule, MatchCounters *counters, MatchRegistry *registry, bool monitor) { |
| 876 |
_c_cleanup_(match_registry_by_path_unrefp) MatchRegistryByPath *registry_by_path = NULL; |
| 877 |
CRBTree *tree; |
| 878 |
CRBNode **slot, *parent; |
| 879 |
int r; |
| 880 |
|
| 881 |
if (rule->registry) { |
| 882 |
c_assert(registry == rule->registry); |
| 883 |
c_assert(c_list_is_linked(&rule->registry_link)); |
| 884 |
c_assert(counters == rule->counters); |
| 885 |
|
| 886 |
return 0; |
| 887 |
} |
| 888 |
|
| 889 |
if (monitor) |
| 890 |
tree = ®istry->monitor_tree; |
| 891 |
else |
| 892 |
tree = ®istry->subscription_tree; |
| 893 |
|
| 894 |
slot = c_rbtree_find_slot(tree, match_registry_by_path_compare, rule->keys.filter.path, &parent); |
| 895 |
if (!slot) { |
| 896 |
registry_by_path = match_registry_by_path_ref(c_rbnode_entry(parent, MatchRegistryByPath, registry_node)); |
| 897 |
} else { |
| 898 |
r = match_registry_by_path_new(®istry_by_path, rule->keys.filter.path); |
| 899 |
if (r) |
| 900 |
return error_trace(r); |
| 901 |
|
| 902 |
match_registry_by_path_link(registry_by_path, tree, parent, slot); |
| 903 |
} |
| 904 |
|
| 905 |
r = match_rule_link_by_path(rule, registry_by_path); |
| 906 |
if (r) |
| 907 |
return error_trace(r); |
| 908 |
|
| 909 |
rule->registry = registry; |
| 910 |
rule->counters = counters; |
| 911 |
|
| 912 |
if (counters) { |
| 913 |
++counters->n_subscriptions; |
| 914 |
++rule->owner->n_owner_subscriptions; |
| 915 |
util_peak_update(&counters->n_subscriptions_peak, counters->n_subscriptions); |
| 916 |
util_peak_update(&counters->n_owner_subscriptions_peak, rule->owner->n_owner_subscriptions); |
| 917 |
} |
| 918 |
|
| 919 |
return 0; |
| 920 |
} |
| 921 |
|
| 922 |
/** |
| 923 |
* match_rule_unlink() - XXX |
| 924 |
*/ |
| 925 |
void match_rule_unlink(MatchRule *rule) { |
| 926 |
if (rule->registry) { |
| 927 |
if (rule->counters) { |
| 928 |
--rule->owner->n_owner_subscriptions; |
| 929 |
--rule->counters->n_subscriptions; |
| 930 |
} |
| 931 |
|
| 932 |
c_list_unlink(&rule->registry_link); |
| 933 |
rule->registry_by_keys = match_registry_by_keys_unref(rule->registry_by_keys); |
| 934 |
rule->counters = NULL; |
| 935 |
rule->registry = NULL; |
| 936 |
} |
| 937 |
} |
| 938 |
|
| 939 |
/** |
| 940 |
* match_owner_init() - XXX |
| 941 |
*/ |
| 942 |
void match_owner_init(MatchOwner *owner) { |
| 943 |
*owner = (MatchOwner)MATCH_OWNER_INIT(*owner); |
| 944 |
} |
| 945 |
|
| 946 |
/** |
| 947 |
* match_owner_deinit() - XXX |
| 948 |
*/ |
| 949 |
void match_owner_deinit(MatchOwner *owner) { |
| 950 |
c_assert(c_rbtree_is_empty(&owner->rule_tree)); |
| 951 |
c_assert(!c_list_is_linked(&owner->destinations_link)); |
| 952 |
} |
| 953 |
|
| 954 |
/** |
| 955 |
* match_owner_get_stats() - XXX |
| 956 |
*/ |
| 957 |
void match_owner_get_stats(MatchOwner *owner, unsigned int *n_bytesp, unsigned int *n_matchesp) { |
| 958 |
MatchRule *rule; |
| 959 |
unsigned int n_bytes = 0, n_matches = 0; |
| 960 |
|
| 961 |
c_rbtree_for_each_entry(rule, &owner->rule_tree, owner_node) { |
| 962 |
n_bytes += rule->charge[0].charge; |
| 963 |
n_matches += rule->charge[1].charge; |
| 964 |
} |
| 965 |
|
| 966 |
*n_bytesp = n_bytes; |
| 967 |
*n_matchesp = n_matches; |
| 968 |
} |
| 969 |
|
| 970 |
/** |
| 971 |
* match_owner_move() - XXX |
| 972 |
*/ |
| 973 |
void match_owner_move(MatchOwner *to, MatchOwner *from) { |
| 974 |
c_rbtree_move(&to->rule_tree, &from->rule_tree); |
| 975 |
} |
| 976 |
|
| 977 |
/** |
| 978 |
* match_owner_ref_rule() - XXX |
| 979 |
*/ |
| 980 |
int match_owner_ref_rule(MatchOwner *owner, MatchRule **rulep, User *user, const char *rule_string, bool allow_eavesdrop) { |
| 981 |
CRBNode **slot, *parent; |
| 982 |
MatchRule *rule; |
| 983 |
int r; |
| 984 |
|
| 985 |
r = match_rule_new(&rule, owner, user, rule_string, allow_eavesdrop); |
| 986 |
if (r) |
| 987 |
return error_trace(r); |
| 988 |
|
| 989 |
++rule->n_user_refs; |
| 990 |
|
| 991 |
slot = c_rbtree_find_slot(&owner->rule_tree, match_rule_compare, &rule->keys, &parent); |
| 992 |
if (!slot) { |
| 993 |
/* one already exists, take a ref on that instead and drop the one we created */ |
| 994 |
match_rule_user_unref(rule); |
| 995 |
rule = match_rule_user_ref(c_container_of(parent, MatchRule, owner_node)); |
| 996 |
} else { |
| 997 |
/* link the new rule into the rbtree */ |
| 998 |
c_rbtree_add(&owner->rule_tree, parent, slot, &rule->owner_node); |
| 999 |
} |
| 1000 |
|
| 1001 |
if (rulep) |
| 1002 |
*rulep = rule; |
| 1003 |
return 0; |
| 1004 |
} |
| 1005 |
|
| 1006 |
/** |
| 1007 |
* match_owner_find_rule() - XXX |
| 1008 |
*/ |
| 1009 |
int match_owner_find_rule(MatchOwner *owner, MatchRule **rulep, const char *rule_string) { |
| 1010 |
_c_cleanup_(match_keys_freep) MatchKeys *keys = NULL; |
| 1011 |
int r; |
| 1012 |
|
| 1013 |
r = match_keys_new(&keys, rule_string); |
| 1014 |
if (r) |
| 1015 |
return error_trace(r); |
| 1016 |
|
| 1017 |
*rulep = c_rbtree_find_entry(&owner->rule_tree, match_rule_compare, keys, MatchRule, owner_node); |
| 1018 |
return 0; |
| 1019 |
} |
| 1020 |
|
| 1021 |
/** |
| 1022 |
* match_registry_init() - XXX |
| 1023 |
*/ |
| 1024 |
void match_registry_init(MatchRegistry *registry) { |
| 1025 |
*registry = (MatchRegistry)MATCH_REGISTRY_INIT(*registry); |
| 1026 |
} |
| 1027 |
|
| 1028 |
/** |
| 1029 |
* match_registry_deinit() - XXX |
| 1030 |
*/ |
| 1031 |
void match_registry_deinit(MatchRegistry *registry) { |
| 1032 |
c_assert(c_rbtree_is_empty(®istry->subscription_tree)); |
| 1033 |
c_assert(c_rbtree_is_empty(®istry->monitor_tree)); |
| 1034 |
} |
| 1035 |
|
| 1036 |
static void match_registry_by_keys_get_destinations(MatchRegistryByKeys *registry, CList *destinations) { |
| 1037 |
MatchRule *rule; |
| 1038 |
|
| 1039 |
c_list_for_each_entry(rule, ®istry->rule_list, registry_link) { |
| 1040 |
if (c_list_is_linked(&rule->owner->destinations_link)) |
| 1041 |
/* only link a destination once, despite matching in several different ways */ |
| 1042 |
continue; |
| 1043 |
|
| 1044 |
c_list_link_tail(destinations, &rule->owner->destinations_link); |
| 1045 |
} |
| 1046 |
} |
| 1047 |
|
| 1048 |
static void match_registry_by_member_get_destinations(MatchRegistryByMember *registry, CList *destinations, MessageMetadata *metadata) { |
| 1049 |
MatchRegistryByKeys *registry_by_keys; |
| 1050 |
|
| 1051 |
c_rbtree_for_each_entry_postorder(registry_by_keys, ®istry->keys_tree, registry_node) { |
| 1052 |
if (!match_keys_match_metadata(®istry_by_keys->keys, metadata)) |
| 1053 |
continue; |
| 1054 |
|
| 1055 |
match_registry_by_keys_get_destinations(registry_by_keys, destinations); |
| 1056 |
} |
| 1057 |
} |
| 1058 |
|
| 1059 |
static void match_registry_by_interface_get_destinations(MatchRegistryByInterface *registry, CList *destinations, MessageMetadata *metadata) { |
| 1060 |
MatchRegistryByMember *registry_by_member; |
| 1061 |
|
| 1062 |
registry_by_member = c_rbtree_find_entry(®istry->member_tree, match_registry_by_member_compare, NULL, MatchRegistryByMember, registry_node); |
| 1063 |
if (registry_by_member) |
| 1064 |
match_registry_by_member_get_destinations(registry_by_member, destinations, metadata); |
| 1065 |
|
| 1066 |
if (metadata->fields.member) { |
| 1067 |
registry_by_member = c_rbtree_find_entry(®istry->member_tree, match_registry_by_member_compare, metadata->fields.member, MatchRegistryByMember, registry_node); |
| 1068 |
if (registry_by_member) |
| 1069 |
match_registry_by_member_get_destinations(registry_by_member, destinations, metadata); |
| 1070 |
} |
| 1071 |
} |
| 1072 |
|
| 1073 |
static void match_registry_by_path_get_destinations(MatchRegistryByPath *registry, CList *destinations, MessageMetadata *metadata) { |
| 1074 |
MatchRegistryByInterface *registry_by_interface; |
| 1075 |
|
| 1076 |
registry_by_interface = c_rbtree_find_entry(®istry->interface_tree, match_registry_by_interface_compare, NULL, MatchRegistryByInterface, registry_node); |
| 1077 |
if (registry_by_interface) |
| 1078 |
match_registry_by_interface_get_destinations(registry_by_interface, destinations, metadata); |
| 1079 |
|
| 1080 |
if (metadata->fields.interface) { |
| 1081 |
registry_by_interface = c_rbtree_find_entry(®istry->interface_tree, match_registry_by_interface_compare, metadata->fields.interface, MatchRegistryByInterface, registry_node); |
| 1082 |
if (registry_by_interface) |
| 1083 |
match_registry_by_interface_get_destinations(registry_by_interface, destinations, metadata); |
| 1084 |
} |
| 1085 |
|
| 1086 |
} |
| 1087 |
|
| 1088 |
static void match_registry_get_destinations(CRBTree *tree, CList *destinations, MessageMetadata *metadata) { |
| 1089 |
MatchRegistryByPath *registry_by_path; |
| 1090 |
|
| 1091 |
registry_by_path = c_rbtree_find_entry(tree, match_registry_by_path_compare, NULL, MatchRegistryByPath, registry_node); |
| 1092 |
if (registry_by_path) |
| 1093 |
match_registry_by_path_get_destinations(registry_by_path, destinations, metadata); |
| 1094 |
|
| 1095 |
if (metadata->fields.path) { |
| 1096 |
registry_by_path = c_rbtree_find_entry(tree, match_registry_by_path_compare, metadata->fields.path, MatchRegistryByPath, registry_node); |
| 1097 |
if (registry_by_path) |
| 1098 |
match_registry_by_path_get_destinations(registry_by_path, destinations, metadata); |
| 1099 |
} |
| 1100 |
|
| 1101 |
} |
| 1102 |
|
| 1103 |
void match_registry_get_subscribers(MatchRegistry *registry, CList *destinations, MessageMetadata *metadata) { |
| 1104 |
match_registry_get_destinations(®istry->subscription_tree, destinations, metadata); |
| 1105 |
} |
| 1106 |
|
| 1107 |
void match_registry_get_monitors(MatchRegistry *registry, CList *destinations, MessageMetadata *metadata) { |
| 1108 |
match_registry_get_destinations(®istry->monitor_tree, destinations, metadata); |
| 1109 |
} |
| 1110 |
|
| 1111 |
static void match_registry_by_keys_flush(MatchRegistryByKeys *registry) { |
| 1112 |
MatchRule *rule, *rule_safe; |
| 1113 |
|
| 1114 |
c_list_for_each_entry_safe(rule, rule_safe, ®istry->rule_list, registry_link) |
| 1115 |
match_rule_unlink(rule); |
| 1116 |
|
| 1117 |
c_assert(c_list_is_empty(®istry->rule_list)); |
| 1118 |
} |
| 1119 |
|
| 1120 |
static void match_registry_by_member_flush(MatchRegistryByMember *registry) { |
| 1121 |
MatchRegistryByKeys *registry_by_keys, *registry_by_keys_safe; |
| 1122 |
|
| 1123 |
c_rbtree_for_each_entry_safe(registry_by_keys, registry_by_keys_safe, ®istry->keys_tree, registry_node) { |
| 1124 |
match_registry_by_keys_ref(registry_by_keys); |
| 1125 |
match_registry_by_keys_flush(registry_by_keys); |
| 1126 |
match_registry_by_keys_unref(registry_by_keys); |
| 1127 |
} |
| 1128 |
|
| 1129 |
c_assert(c_rbtree_is_empty(®istry->keys_tree)); |
| 1130 |
} |
| 1131 |
|
| 1132 |
static void match_registry_by_interface_flush(MatchRegistryByInterface *registry) { |
| 1133 |
MatchRegistryByMember *registry_by_member, *registry_by_member_safe; |
| 1134 |
|
| 1135 |
c_rbtree_for_each_entry_safe(registry_by_member, registry_by_member_safe, ®istry->member_tree, registry_node) { |
| 1136 |
match_registry_by_member_ref(registry_by_member); |
| 1137 |
match_registry_by_member_flush(registry_by_member); |
| 1138 |
match_registry_by_member_unref(registry_by_member); |
| 1139 |
} |
| 1140 |
|
| 1141 |
c_assert(c_rbtree_is_empty(®istry->member_tree)); |
| 1142 |
} |
| 1143 |
|
| 1144 |
static void match_registry_by_path_flush(MatchRegistryByPath *registry) { |
| 1145 |
MatchRegistryByInterface *registry_by_interface, *registry_by_interface_safe; |
| 1146 |
|
| 1147 |
c_rbtree_for_each_entry_safe(registry_by_interface, registry_by_interface_safe, ®istry->interface_tree, registry_node) { |
| 1148 |
match_registry_by_interface_ref(registry_by_interface); |
| 1149 |
match_registry_by_interface_flush(registry_by_interface); |
| 1150 |
match_registry_by_interface_unref(registry_by_interface); |
| 1151 |
} |
| 1152 |
|
| 1153 |
c_assert(c_rbtree_is_empty(®istry->interface_tree)); |
| 1154 |
} |
| 1155 |
|
| 1156 |
/** |
| 1157 |
* match_registry_flush() - flush all links in this registry |
| 1158 |
* @registry: registry to operate on |
| 1159 |
* |
| 1160 |
* This flushes all links in this registry. Usually, the match owner still |
| 1161 |
* holds a reference, so the matches will likely stay around. The registry |
| 1162 |
* object merely holds the remote links for fast lookup. Once those are |
| 1163 |
* dropped, this registry object will no longer yield any results on lookup. |
| 1164 |
* |
| 1165 |
* Note that the match objects are not relinked by this function. Once you call |
| 1166 |
* this, the match objects will be unlinked and thus cannot be yielded by |
| 1167 |
* lookups anymore. Usually, you only want to call this when the owning object |
| 1168 |
* of this registry goes away, and all the linked matches are meant to be |
| 1169 |
* stale from this point on. |
| 1170 |
*/ |
| 1171 |
void match_registry_flush(MatchRegistry *registry) { |
| 1172 |
CRBTree *trees[] = { |
| 1173 |
®istry->subscription_tree, |
| 1174 |
®istry->monitor_tree, |
| 1175 |
}; |
| 1176 |
MatchRegistryByPath *registry_by_path, *registry_by_path_safe; |
| 1177 |
size_t i; |
| 1178 |
|
| 1179 |
for (i = 0; i < C_ARRAY_SIZE(trees); ++i) { |
| 1180 |
c_rbtree_for_each_entry_safe(registry_by_path, registry_by_path_safe, trees[i], registry_node) { |
| 1181 |
match_registry_by_path_ref(registry_by_path); |
| 1182 |
match_registry_by_path_flush(registry_by_path); |
| 1183 |
match_registry_by_path_unref(registry_by_path); |
| 1184 |
} |
| 1185 |
|
| 1186 |
c_assert(c_rbtree_is_empty(trees[i])); |
| 1187 |
} |
| 1188 |
} |