| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-bus.h" |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "bus-internal.h" |
| 7 |
#include "bus-match.h" |
| 8 |
#include "bus-message.h" |
| 9 |
#include "hashmap.h" |
| 10 |
#include "hexdecoct.h" |
| 11 |
#include "memstream-util.h" |
| 12 |
#include "sort-util.h" |
| 13 |
#include "stdio-util.h" |
| 14 |
#include "string-util.h" |
| 15 |
#include "strv.h" |
| 16 |
|
| 17 |
/* Example: |
| 18 |
* |
| 19 |
* A: type=signal,sender=foo,interface=bar |
| 20 |
* B: type=signal,sender=quux,interface=fips |
| 21 |
* C: type=signal,sender=quux,interface=waldo |
| 22 |
* D: type=signal,member=test |
| 23 |
* E: sender=miau |
| 24 |
* F: type=signal |
| 25 |
* G: type=signal |
| 26 |
* |
| 27 |
* results in this tree: |
| 28 |
* |
| 29 |
* BUS_MATCH_ROOT |
| 30 |
* + BUS_MATCH_MESSAGE_TYPE |
| 31 |
* | ` BUS_MATCH_VALUE: value == signal |
| 32 |
* | + DBUS_MATCH_SENDER |
| 33 |
* | | + BUS_MATCH_VALUE: value == foo |
| 34 |
* | | | ` DBUS_MATCH_INTERFACE |
| 35 |
* | | | ` BUS_MATCH_VALUE: value == bar |
| 36 |
* | | | ` BUS_MATCH_LEAF: A |
| 37 |
* | | ` BUS_MATCH_VALUE: value == quux |
| 38 |
* | | ` DBUS_MATCH_INTERFACE |
| 39 |
* | | | BUS_MATCH_VALUE: value == fips |
| 40 |
* | | | ` BUS_MATCH_LEAF: B |
| 41 |
* | | ` BUS_MATCH_VALUE: value == waldo |
| 42 |
* | | ` BUS_MATCH_LEAF: C |
| 43 |
* | + DBUS_MATCH_MEMBER |
| 44 |
* | | ` BUS_MATCH_VALUE: value == test |
| 45 |
* | | ` BUS_MATCH_LEAF: D |
| 46 |
* | + BUS_MATCH_LEAF: F |
| 47 |
* | ` BUS_MATCH_LEAF: G |
| 48 |
* ` BUS_MATCH_SENDER |
| 49 |
* ` BUS_MATCH_VALUE: value == miau |
| 50 |
* ` BUS_MATCH_LEAF: E |
| 51 |
*/ |
| 52 |
|
| 53 |
static bool BUS_MATCH_IS_COMPARE(BusMatchNodeType t) { |
| 54 |
return t >= BUS_MATCH_SENDER && t <= BUS_MATCH_ARG_HAS_LAST; |
| 55 |
} |
| 56 |
|
| 57 |
static bool BUS_MATCH_CAN_HASH(BusMatchNodeType t) { |
| 58 |
return (t >= BUS_MATCH_MESSAGE_TYPE && t <= BUS_MATCH_PATH) || |
| 59 |
(t >= BUS_MATCH_ARG && t <= BUS_MATCH_ARG_LAST) || |
| 60 |
(t >= BUS_MATCH_ARG_HAS && t <= BUS_MATCH_ARG_HAS_LAST); |
| 61 |
} |
| 62 |
|
| 63 |
static void bus_match_node_free(BusMatchNode *node) { |
| 64 |
assert(node); |
| 65 |
assert(node->parent); |
| 66 |
assert(!node->child); |
| 67 |
assert(node->type != BUS_MATCH_ROOT); |
| 68 |
assert(node->type < _BUS_MATCH_NODE_TYPE_MAX); |
| 69 |
|
| 70 |
if (node->parent->child) { |
| 71 |
/* We are apparently linked into the parent's child |
| 72 |
* list. Let's remove us from there. */ |
| 73 |
if (node->prev) { |
| 74 |
assert(node->prev->next == node); |
| 75 |
node->prev->next = node->next; |
| 76 |
} else { |
| 77 |
assert(node->parent->child == node); |
| 78 |
node->parent->child = node->next; |
| 79 |
} |
| 80 |
|
| 81 |
if (node->next) |
| 82 |
node->next->prev = node->prev; |
| 83 |
} |
| 84 |
|
| 85 |
if (node->type == BUS_MATCH_VALUE) { |
| 86 |
/* We might be in the parent's hash table, so clean |
| 87 |
* this up */ |
| 88 |
|
| 89 |
if (node->parent->type == BUS_MATCH_MESSAGE_TYPE) |
| 90 |
hashmap_remove(node->parent->compare.children, UINT_TO_PTR(node->value.u8)); |
| 91 |
else if (BUS_MATCH_CAN_HASH(node->parent->type) && node->value.str) |
| 92 |
hashmap_remove(node->parent->compare.children, node->value.str); |
| 93 |
|
| 94 |
free(node->value.str); |
| 95 |
} |
| 96 |
|
| 97 |
if (BUS_MATCH_IS_COMPARE(node->type)) { |
| 98 |
assert(hashmap_isempty(node->compare.children)); |
| 99 |
hashmap_free(node->compare.children); |
| 100 |
} |
| 101 |
|
| 102 |
free(node); |
| 103 |
} |
| 104 |
|
| 105 |
static bool bus_match_node_maybe_free(BusMatchNode *node) { |
| 106 |
assert(node); |
| 107 |
|
| 108 |
if (node->type == BUS_MATCH_ROOT) |
| 109 |
return false; |
| 110 |
|
| 111 |
if (node->child) |
| 112 |
return false; |
| 113 |
|
| 114 |
if (BUS_MATCH_IS_COMPARE(node->type) && !hashmap_isempty(node->compare.children)) |
| 115 |
return true; |
| 116 |
|
| 117 |
bus_match_node_free(node); |
| 118 |
return true; |
| 119 |
} |
| 120 |
|
| 121 |
static bool value_node_test( |
| 122 |
BusMatchNode *node, |
| 123 |
BusMatchNodeType parent_type, |
| 124 |
uint8_t value_u8, |
| 125 |
const char *value_str, |
| 126 |
char **value_strv, |
| 127 |
sd_bus_message *m) { |
| 128 |
|
| 129 |
assert(node); |
| 130 |
assert(node->type == BUS_MATCH_VALUE); |
| 131 |
|
| 132 |
/* Tests parameters against this value node, doing prefix |
| 133 |
* magic and stuff. */ |
| 134 |
|
| 135 |
switch (parent_type) { |
| 136 |
|
| 137 |
case BUS_MATCH_MESSAGE_TYPE: |
| 138 |
return node->value.u8 == value_u8; |
| 139 |
|
| 140 |
case BUS_MATCH_SENDER: |
| 141 |
if (streq_ptr(node->value.str, value_str)) |
| 142 |
return true; |
| 143 |
|
| 144 |
if (m->creds.mask & SD_BUS_CREDS_WELL_KNOWN_NAMES) { |
| 145 |
/* on kdbus we have the well known names list |
| 146 |
* in the credentials, let's make use of that |
| 147 |
* for an accurate match */ |
| 148 |
|
| 149 |
STRV_FOREACH(i, m->creds.well_known_names) |
| 150 |
if (streq_ptr(node->value.str, *i)) |
| 151 |
return true; |
| 152 |
|
| 153 |
} else { |
| 154 |
|
| 155 |
/* If we don't have kdbus, we don't know the |
| 156 |
* well-known names of the senders. In that, |
| 157 |
* let's just hope that dbus-daemon doesn't |
| 158 |
* send us stuff we didn't want. */ |
| 159 |
|
| 160 |
if (node->value.str[0] != ':' && value_str && value_str[0] == ':') |
| 161 |
return true; |
| 162 |
} |
| 163 |
|
| 164 |
return false; |
| 165 |
|
| 166 |
case BUS_MATCH_DESTINATION: |
| 167 |
case BUS_MATCH_INTERFACE: |
| 168 |
case BUS_MATCH_MEMBER: |
| 169 |
case BUS_MATCH_PATH: |
| 170 |
case BUS_MATCH_ARG ... BUS_MATCH_ARG_LAST: |
| 171 |
|
| 172 |
if (value_str) |
| 173 |
return streq_ptr(node->value.str, value_str); |
| 174 |
|
| 175 |
return false; |
| 176 |
|
| 177 |
case BUS_MATCH_ARG_HAS ... BUS_MATCH_ARG_HAS_LAST: { |
| 178 |
STRV_FOREACH(i, value_strv) |
| 179 |
if (streq_ptr(node->value.str, *i)) |
| 180 |
return true; |
| 181 |
|
| 182 |
return false; |
| 183 |
} |
| 184 |
|
| 185 |
case BUS_MATCH_ARG_NAMESPACE ... BUS_MATCH_ARG_NAMESPACE_LAST: |
| 186 |
if (value_str) |
| 187 |
return namespace_simple_pattern(node->value.str, value_str); |
| 188 |
|
| 189 |
return false; |
| 190 |
|
| 191 |
case BUS_MATCH_PATH_NAMESPACE: |
| 192 |
return path_simple_pattern(node->value.str, value_str); |
| 193 |
|
| 194 |
case BUS_MATCH_ARG_PATH ... BUS_MATCH_ARG_PATH_LAST: |
| 195 |
if (value_str) |
| 196 |
return path_complex_pattern(node->value.str, value_str); |
| 197 |
|
| 198 |
return false; |
| 199 |
|
| 200 |
default: |
| 201 |
assert_not_reached(); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
static bool value_node_same( |
| 206 |
BusMatchNode *node, |
| 207 |
BusMatchNodeType parent_type, |
| 208 |
uint8_t value_u8, |
| 209 |
const char *value_str) { |
| 210 |
|
| 211 |
/* Tests parameters against this value node, not doing prefix |
| 212 |
* magic and stuff, i.e. this one actually compares the match |
| 213 |
* itself. */ |
| 214 |
|
| 215 |
assert(node); |
| 216 |
assert(node->type == BUS_MATCH_VALUE); |
| 217 |
|
| 218 |
switch (parent_type) { |
| 219 |
|
| 220 |
case BUS_MATCH_MESSAGE_TYPE: |
| 221 |
return node->value.u8 == value_u8; |
| 222 |
|
| 223 |
case BUS_MATCH_SENDER: |
| 224 |
case BUS_MATCH_DESTINATION: |
| 225 |
case BUS_MATCH_INTERFACE: |
| 226 |
case BUS_MATCH_MEMBER: |
| 227 |
case BUS_MATCH_PATH: |
| 228 |
case BUS_MATCH_ARG ... BUS_MATCH_ARG_LAST: |
| 229 |
case BUS_MATCH_ARG_HAS ... BUS_MATCH_ARG_HAS_LAST: |
| 230 |
case BUS_MATCH_ARG_NAMESPACE ... BUS_MATCH_ARG_NAMESPACE_LAST: |
| 231 |
case BUS_MATCH_PATH_NAMESPACE: |
| 232 |
case BUS_MATCH_ARG_PATH ... BUS_MATCH_ARG_PATH_LAST: |
| 233 |
return streq(node->value.str, value_str); |
| 234 |
|
| 235 |
default: |
| 236 |
assert_not_reached(); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
int bus_match_run( |
| 241 |
sd_bus *bus, |
| 242 |
BusMatchNode *node, |
| 243 |
sd_bus_message *m) { |
| 244 |
|
| 245 |
_cleanup_strv_free_ char **test_strv = NULL; |
| 246 |
const char *test_str = NULL; |
| 247 |
uint8_t test_u8 = 0; |
| 248 |
int r; |
| 249 |
|
| 250 |
assert(m); |
| 251 |
|
| 252 |
if (!node) |
| 253 |
return 0; |
| 254 |
|
| 255 |
if (bus && bus->match_callbacks_modified) |
| 256 |
return 0; |
| 257 |
|
| 258 |
/* Not these special semantics: when traversing the tree we |
| 259 |
* usually let bus_match_run() when called for a node |
| 260 |
* recursively invoke bus_match_run(). There's are two |
| 261 |
* exceptions here though, which are BUS_NODE_ROOT (which |
| 262 |
* cannot have a sibling), and BUS_NODE_VALUE (whose siblings |
| 263 |
* are invoked anyway by its parent. */ |
| 264 |
|
| 265 |
switch (node->type) { |
| 266 |
|
| 267 |
case BUS_MATCH_ROOT: |
| 268 |
|
| 269 |
/* Run all children. Since we cannot have any siblings |
| 270 |
* we won't call any. The children of the root node |
| 271 |
* are compares or leaves, they will automatically |
| 272 |
* call their siblings. */ |
| 273 |
return bus_match_run(bus, node->child, m); |
| 274 |
|
| 275 |
case BUS_MATCH_VALUE: |
| 276 |
|
| 277 |
/* Run all children. We don't execute any siblings, we |
| 278 |
* assume our caller does that. The children of value |
| 279 |
* nodes are compares or leaves, they will |
| 280 |
* automatically call their siblings */ |
| 281 |
|
| 282 |
assert(node->child); |
| 283 |
return bus_match_run(bus, node->child, m); |
| 284 |
|
| 285 |
case BUS_MATCH_LEAF: |
| 286 |
|
| 287 |
if (bus) { |
| 288 |
/* Don't run this match as long as the AddMatch() call is not complete yet. |
| 289 |
* |
| 290 |
* Don't run this match unless the 'after' counter has been reached. |
| 291 |
* |
| 292 |
* Don't run this match more than once per iteration */ |
| 293 |
|
| 294 |
if (node->leaf.callback->install_slot || |
| 295 |
m->read_counter <= node->leaf.callback->after || |
| 296 |
node->leaf.callback->last_iteration == bus->iteration_counter) |
| 297 |
return bus_match_run(bus, node->next, m); |
| 298 |
|
| 299 |
node->leaf.callback->last_iteration = bus->iteration_counter; |
| 300 |
} |
| 301 |
|
| 302 |
r = sd_bus_message_rewind(m, true); |
| 303 |
if (r < 0) |
| 304 |
return r; |
| 305 |
|
| 306 |
/* Run the callback. And then invoke siblings. */ |
| 307 |
if (node->leaf.callback->callback) { |
| 308 |
_cleanup_(sd_bus_error_free) sd_bus_error error_buffer = SD_BUS_ERROR_NULL; |
| 309 |
sd_bus_slot *slot; |
| 310 |
|
| 311 |
slot = container_of(node->leaf.callback, sd_bus_slot, match_callback); |
| 312 |
if (bus) { |
| 313 |
bus->current_slot = sd_bus_slot_ref(slot); |
| 314 |
bus->current_handler = node->leaf.callback->callback; |
| 315 |
bus->current_userdata = slot->userdata; |
| 316 |
} |
| 317 |
r = node->leaf.callback->callback(m, slot->userdata, &error_buffer); |
| 318 |
if (bus) { |
| 319 |
bus->current_userdata = NULL; |
| 320 |
bus->current_handler = NULL; |
| 321 |
bus->current_slot = sd_bus_slot_unref(slot); |
| 322 |
} |
| 323 |
|
| 324 |
r = bus_maybe_reply_error(m, r, &error_buffer); |
| 325 |
if (r != 0) |
| 326 |
return r; |
| 327 |
|
| 328 |
if (bus && bus->match_callbacks_modified) |
| 329 |
return 0; |
| 330 |
} |
| 331 |
|
| 332 |
return bus_match_run(bus, node->next, m); |
| 333 |
|
| 334 |
case BUS_MATCH_MESSAGE_TYPE: |
| 335 |
test_u8 = m->header->type; |
| 336 |
break; |
| 337 |
|
| 338 |
case BUS_MATCH_SENDER: |
| 339 |
test_str = m->sender; |
| 340 |
/* FIXME: resolve test_str from a well-known to a unique name first */ |
| 341 |
break; |
| 342 |
|
| 343 |
case BUS_MATCH_DESTINATION: |
| 344 |
test_str = m->destination; |
| 345 |
break; |
| 346 |
|
| 347 |
case BUS_MATCH_INTERFACE: |
| 348 |
test_str = m->interface; |
| 349 |
break; |
| 350 |
|
| 351 |
case BUS_MATCH_MEMBER: |
| 352 |
test_str = m->member; |
| 353 |
break; |
| 354 |
|
| 355 |
case BUS_MATCH_PATH: |
| 356 |
case BUS_MATCH_PATH_NAMESPACE: |
| 357 |
test_str = m->path; |
| 358 |
break; |
| 359 |
|
| 360 |
case BUS_MATCH_ARG ... BUS_MATCH_ARG_LAST: |
| 361 |
(void) bus_message_get_arg(m, node->type - BUS_MATCH_ARG, &test_str); |
| 362 |
break; |
| 363 |
|
| 364 |
case BUS_MATCH_ARG_PATH ... BUS_MATCH_ARG_PATH_LAST: |
| 365 |
(void) bus_message_get_arg(m, node->type - BUS_MATCH_ARG_PATH, &test_str); |
| 366 |
break; |
| 367 |
|
| 368 |
case BUS_MATCH_ARG_NAMESPACE ... BUS_MATCH_ARG_NAMESPACE_LAST: |
| 369 |
(void) bus_message_get_arg(m, node->type - BUS_MATCH_ARG_NAMESPACE, &test_str); |
| 370 |
break; |
| 371 |
|
| 372 |
case BUS_MATCH_ARG_HAS ... BUS_MATCH_ARG_HAS_LAST: |
| 373 |
(void) bus_message_get_arg_strv(m, node->type - BUS_MATCH_ARG_HAS, &test_strv); |
| 374 |
break; |
| 375 |
|
| 376 |
default: |
| 377 |
assert_not_reached(); |
| 378 |
} |
| 379 |
|
| 380 |
if (BUS_MATCH_CAN_HASH(node->type)) { |
| 381 |
BusMatchNode *found; |
| 382 |
|
| 383 |
/* Lookup via hash table, nice! So let's jump directly. */ |
| 384 |
|
| 385 |
if (test_str) |
| 386 |
found = hashmap_get(node->compare.children, test_str); |
| 387 |
else if (test_strv) { |
| 388 |
STRV_FOREACH(i, test_strv) { |
| 389 |
found = hashmap_get(node->compare.children, *i); |
| 390 |
if (found) { |
| 391 |
r = bus_match_run(bus, found, m); |
| 392 |
if (r != 0) |
| 393 |
return r; |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
found = NULL; |
| 398 |
} else if (node->type == BUS_MATCH_MESSAGE_TYPE) |
| 399 |
found = hashmap_get(node->compare.children, UINT_TO_PTR(test_u8)); |
| 400 |
else |
| 401 |
found = NULL; |
| 402 |
|
| 403 |
if (found) { |
| 404 |
r = bus_match_run(bus, found, m); |
| 405 |
if (r != 0) |
| 406 |
return r; |
| 407 |
} |
| 408 |
} else |
| 409 |
/* No hash table, so let's iterate manually... */ |
| 410 |
for (BusMatchNode *c = node->child; c; c = c->next) { |
| 411 |
if (!value_node_test(c, node->type, test_u8, test_str, test_strv, m)) |
| 412 |
continue; |
| 413 |
|
| 414 |
r = bus_match_run(bus, c, m); |
| 415 |
if (r != 0) |
| 416 |
return r; |
| 417 |
|
| 418 |
if (bus && bus->match_callbacks_modified) |
| 419 |
return 0; |
| 420 |
} |
| 421 |
|
| 422 |
if (bus && bus->match_callbacks_modified) |
| 423 |
return 0; |
| 424 |
|
| 425 |
/* And now, let's invoke our siblings */ |
| 426 |
return bus_match_run(bus, node->next, m); |
| 427 |
} |
| 428 |
|
| 429 |
static int bus_match_add_compare_value( |
| 430 |
BusMatchNode *where, |
| 431 |
BusMatchNodeType t, |
| 432 |
uint8_t value_u8, |
| 433 |
const char *value_str, |
| 434 |
BusMatchNode **ret) { |
| 435 |
|
| 436 |
BusMatchNode *c, *n = NULL; |
| 437 |
int r; |
| 438 |
|
| 439 |
assert(where); |
| 440 |
assert(IN_SET(where->type, BUS_MATCH_ROOT, BUS_MATCH_VALUE)); |
| 441 |
assert(BUS_MATCH_IS_COMPARE(t)); |
| 442 |
assert(ret); |
| 443 |
|
| 444 |
for (c = where->child; c && c->type != t; c = c->next) |
| 445 |
; |
| 446 |
|
| 447 |
if (c) { |
| 448 |
/* Comparison node already exists? Then let's see if the value node exists too. */ |
| 449 |
|
| 450 |
if (t == BUS_MATCH_MESSAGE_TYPE) |
| 451 |
n = hashmap_get(c->compare.children, UINT_TO_PTR(value_u8)); |
| 452 |
else if (BUS_MATCH_CAN_HASH(t)) |
| 453 |
n = hashmap_get(c->compare.children, value_str); |
| 454 |
else |
| 455 |
for (n = c->child; n && !value_node_same(n, t, value_u8, value_str); n = n->next) |
| 456 |
; |
| 457 |
|
| 458 |
if (n) { |
| 459 |
*ret = n; |
| 460 |
return 0; |
| 461 |
} |
| 462 |
} else { |
| 463 |
/* Comparison node, doesn't exist yet? Then let's create it. */ |
| 464 |
|
| 465 |
c = new0(BusMatchNode, 1); |
| 466 |
if (!c) { |
| 467 |
r = -ENOMEM; |
| 468 |
goto fail; |
| 469 |
} |
| 470 |
|
| 471 |
c->type = t; |
| 472 |
c->parent = where; |
| 473 |
c->next = where->child; |
| 474 |
if (c->next) |
| 475 |
c->next->prev = c; |
| 476 |
where->child = c; |
| 477 |
|
| 478 |
if (t == BUS_MATCH_MESSAGE_TYPE) { |
| 479 |
c->compare.children = hashmap_new(NULL); |
| 480 |
if (!c->compare.children) { |
| 481 |
r = -ENOMEM; |
| 482 |
goto fail; |
| 483 |
} |
| 484 |
} else if (BUS_MATCH_CAN_HASH(t)) { |
| 485 |
c->compare.children = hashmap_new(&string_hash_ops); |
| 486 |
if (!c->compare.children) { |
| 487 |
r = -ENOMEM; |
| 488 |
goto fail; |
| 489 |
} |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
n = new0(BusMatchNode, 1); |
| 494 |
if (!n) { |
| 495 |
r = -ENOMEM; |
| 496 |
goto fail; |
| 497 |
} |
| 498 |
|
| 499 |
n->type = BUS_MATCH_VALUE; |
| 500 |
n->value.u8 = value_u8; |
| 501 |
if (value_str) { |
| 502 |
n->value.str = strdup(value_str); |
| 503 |
if (!n->value.str) { |
| 504 |
r = -ENOMEM; |
| 505 |
goto fail; |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
n->parent = c; |
| 510 |
if (c->compare.children) { |
| 511 |
|
| 512 |
if (t == BUS_MATCH_MESSAGE_TYPE) |
| 513 |
r = hashmap_put(c->compare.children, UINT_TO_PTR(value_u8), n); |
| 514 |
else |
| 515 |
r = hashmap_put(c->compare.children, n->value.str, n); |
| 516 |
|
| 517 |
if (r < 0) |
| 518 |
goto fail; |
| 519 |
} else { |
| 520 |
n->next = c->child; |
| 521 |
if (n->next) |
| 522 |
n->next->prev = n; |
| 523 |
c->child = n; |
| 524 |
} |
| 525 |
|
| 526 |
*ret = n; |
| 527 |
return 1; |
| 528 |
|
| 529 |
fail: |
| 530 |
if (c) |
| 531 |
bus_match_node_maybe_free(c); |
| 532 |
|
| 533 |
if (n) { |
| 534 |
free(n->value.str); |
| 535 |
free(n); |
| 536 |
} |
| 537 |
|
| 538 |
return r; |
| 539 |
} |
| 540 |
|
| 541 |
static int bus_match_add_leaf( |
| 542 |
BusMatchNode *where, |
| 543 |
BusMatchCallback *callback) { |
| 544 |
|
| 545 |
BusMatchNode *n; |
| 546 |
|
| 547 |
assert(where); |
| 548 |
assert(IN_SET(where->type, BUS_MATCH_ROOT, BUS_MATCH_VALUE)); |
| 549 |
assert(callback); |
| 550 |
|
| 551 |
n = new0(BusMatchNode, 1); |
| 552 |
if (!n) |
| 553 |
return -ENOMEM; |
| 554 |
|
| 555 |
n->type = BUS_MATCH_LEAF; |
| 556 |
n->parent = where; |
| 557 |
n->next = where->child; |
| 558 |
if (n->next) |
| 559 |
n->next->prev = n; |
| 560 |
|
| 561 |
n->leaf.callback = callback; |
| 562 |
callback->match_node = n; |
| 563 |
|
| 564 |
where->child = n; |
| 565 |
|
| 566 |
return 1; |
| 567 |
} |
| 568 |
|
| 569 |
BusMatchNodeType bus_match_node_type_from_string(const char *k, size_t n) { |
| 570 |
assert(k); |
| 571 |
|
| 572 |
if (n == 4 && startswith(k, "type")) |
| 573 |
return BUS_MATCH_MESSAGE_TYPE; |
| 574 |
if (n == 6 && startswith(k, "sender")) |
| 575 |
return BUS_MATCH_SENDER; |
| 576 |
if (n == 11 && startswith(k, "destination")) |
| 577 |
return BUS_MATCH_DESTINATION; |
| 578 |
if (n == 9 && startswith(k, "interface")) |
| 579 |
return BUS_MATCH_INTERFACE; |
| 580 |
if (n == 6 && startswith(k, "member")) |
| 581 |
return BUS_MATCH_MEMBER; |
| 582 |
if (n == 4 && startswith(k, "path")) |
| 583 |
return BUS_MATCH_PATH; |
| 584 |
if (n == 14 && startswith(k, "path_namespace")) |
| 585 |
return BUS_MATCH_PATH_NAMESPACE; |
| 586 |
|
| 587 |
if (n == 4 && startswith(k, "arg")) { |
| 588 |
int j; |
| 589 |
|
| 590 |
j = undecchar(k[3]); |
| 591 |
if (j < 0) |
| 592 |
return -EINVAL; |
| 593 |
|
| 594 |
return BUS_MATCH_ARG + j; |
| 595 |
} |
| 596 |
|
| 597 |
if (n == 5 && startswith(k, "arg")) { |
| 598 |
int a, b; |
| 599 |
BusMatchNodeType t; |
| 600 |
|
| 601 |
a = undecchar(k[3]); |
| 602 |
b = undecchar(k[4]); |
| 603 |
if (a <= 0 || b < 0) |
| 604 |
return -EINVAL; |
| 605 |
|
| 606 |
t = BUS_MATCH_ARG + a * 10 + b; |
| 607 |
if (t > BUS_MATCH_ARG_LAST) |
| 608 |
return -EINVAL; |
| 609 |
|
| 610 |
return t; |
| 611 |
} |
| 612 |
|
| 613 |
if (n == 8 && startswith(k, "arg") && startswith(k + 4, "path")) { |
| 614 |
int j; |
| 615 |
|
| 616 |
j = undecchar(k[3]); |
| 617 |
if (j < 0) |
| 618 |
return -EINVAL; |
| 619 |
|
| 620 |
return BUS_MATCH_ARG_PATH + j; |
| 621 |
} |
| 622 |
|
| 623 |
if (n == 9 && startswith(k, "arg") && startswith(k + 5, "path")) { |
| 624 |
BusMatchNodeType t; |
| 625 |
int a, b; |
| 626 |
|
| 627 |
a = undecchar(k[3]); |
| 628 |
b = undecchar(k[4]); |
| 629 |
if (a <= 0 || b < 0) |
| 630 |
return -EINVAL; |
| 631 |
|
| 632 |
t = BUS_MATCH_ARG_PATH + a * 10 + b; |
| 633 |
if (t > BUS_MATCH_ARG_PATH_LAST) |
| 634 |
return -EINVAL; |
| 635 |
|
| 636 |
return t; |
| 637 |
} |
| 638 |
|
| 639 |
if (n == 13 && startswith(k, "arg") && startswith(k + 4, "namespace")) { |
| 640 |
int j; |
| 641 |
|
| 642 |
j = undecchar(k[3]); |
| 643 |
if (j < 0) |
| 644 |
return -EINVAL; |
| 645 |
|
| 646 |
return BUS_MATCH_ARG_NAMESPACE + j; |
| 647 |
} |
| 648 |
|
| 649 |
if (n == 14 && startswith(k, "arg") && startswith(k + 5, "namespace")) { |
| 650 |
BusMatchNodeType t; |
| 651 |
int a, b; |
| 652 |
|
| 653 |
a = undecchar(k[3]); |
| 654 |
b = undecchar(k[4]); |
| 655 |
if (a <= 0 || b < 0) |
| 656 |
return -EINVAL; |
| 657 |
|
| 658 |
t = BUS_MATCH_ARG_NAMESPACE + a * 10 + b; |
| 659 |
if (t > BUS_MATCH_ARG_NAMESPACE_LAST) |
| 660 |
return -EINVAL; |
| 661 |
|
| 662 |
return t; |
| 663 |
} |
| 664 |
|
| 665 |
if (n == 7 && startswith(k, "arg") && startswith(k + 4, "has")) { |
| 666 |
int j; |
| 667 |
|
| 668 |
j = undecchar(k[3]); |
| 669 |
if (j < 0) |
| 670 |
return -EINVAL; |
| 671 |
|
| 672 |
return BUS_MATCH_ARG_HAS + j; |
| 673 |
} |
| 674 |
|
| 675 |
if (n == 8 && startswith(k, "arg") && startswith(k + 5, "has")) { |
| 676 |
BusMatchNodeType t; |
| 677 |
int a, b; |
| 678 |
|
| 679 |
a = undecchar(k[3]); |
| 680 |
b = undecchar(k[4]); |
| 681 |
if (a <= 0 || b < 0) |
| 682 |
return -EINVAL; |
| 683 |
|
| 684 |
t = BUS_MATCH_ARG_HAS + a * 10 + b; |
| 685 |
if (t > BUS_MATCH_ARG_HAS_LAST) |
| 686 |
return -EINVAL; |
| 687 |
|
| 688 |
return t; |
| 689 |
} |
| 690 |
|
| 691 |
return -EINVAL; |
| 692 |
} |
| 693 |
|
| 694 |
static int match_component_compare(const BusMatchComponent *a, const BusMatchComponent *b) { |
| 695 |
return CMP(a->type, b->type); |
| 696 |
} |
| 697 |
|
| 698 |
void bus_match_parse_free(BusMatchComponent *components, size_t n_components) { |
| 699 |
for (size_t i = 0; i < n_components; i++) |
| 700 |
free(components[i].value_str); |
| 701 |
|
| 702 |
free(components); |
| 703 |
} |
| 704 |
|
| 705 |
int bus_match_parse( |
| 706 |
const char *match, |
| 707 |
BusMatchComponent **ret_components, |
| 708 |
size_t *ret_n_components) { |
| 709 |
|
| 710 |
BusMatchComponent *components = NULL; |
| 711 |
size_t n_components = 0; |
| 712 |
int r; |
| 713 |
|
| 714 |
assert(match); |
| 715 |
assert(ret_components); |
| 716 |
assert(ret_n_components); |
| 717 |
|
| 718 |
CLEANUP_ARRAY(components, n_components, bus_match_parse_free); |
| 719 |
|
| 720 |
while (*match != '\0') { |
| 721 |
const char *eq, *q; |
| 722 |
BusMatchNodeType t; |
| 723 |
size_t j = 0; |
| 724 |
_cleanup_free_ char *value = NULL; |
| 725 |
bool escaped = false, quoted; |
| 726 |
uint8_t u; |
| 727 |
|
| 728 |
/* Avahi's match rules appear to include whitespace, skip over it */ |
| 729 |
match += strspn(match, " "); |
| 730 |
|
| 731 |
eq = strchr(match, '='); |
| 732 |
if (!eq) |
| 733 |
return -EINVAL; |
| 734 |
|
| 735 |
t = bus_match_node_type_from_string(match, eq - match); |
| 736 |
if (t < 0) |
| 737 |
return -EINVAL; |
| 738 |
|
| 739 |
quoted = eq[1] == '\''; |
| 740 |
|
| 741 |
for (q = eq + 1 + quoted;; q++) { |
| 742 |
|
| 743 |
if (*q == '\0') { |
| 744 |
|
| 745 |
if (quoted) |
| 746 |
return -EINVAL; |
| 747 |
|
| 748 |
if (value) |
| 749 |
value[j] = '\0'; |
| 750 |
break; |
| 751 |
} |
| 752 |
|
| 753 |
if (!escaped) { |
| 754 |
if (*q == '\\') { |
| 755 |
escaped = true; |
| 756 |
continue; |
| 757 |
} |
| 758 |
|
| 759 |
if (quoted) { |
| 760 |
if (*q == '\'') { |
| 761 |
if (value) |
| 762 |
value[j] = '\0'; |
| 763 |
break; |
| 764 |
} |
| 765 |
} else { |
| 766 |
if (*q == ',') { |
| 767 |
if (value) |
| 768 |
value[j] = '\0'; |
| 769 |
break; |
| 770 |
} |
| 771 |
} |
| 772 |
} |
| 773 |
|
| 774 |
if (!GREEDY_REALLOC(value, j + 2)) |
| 775 |
return -ENOMEM; |
| 776 |
|
| 777 |
value[j++] = *q; |
| 778 |
escaped = false; |
| 779 |
} |
| 780 |
|
| 781 |
if (!value) { |
| 782 |
value = strdup(""); |
| 783 |
if (!value) |
| 784 |
return -ENOMEM; |
| 785 |
} |
| 786 |
|
| 787 |
if (t == BUS_MATCH_MESSAGE_TYPE) { |
| 788 |
r = bus_message_type_from_string(value, &u); |
| 789 |
if (r < 0) |
| 790 |
return r; |
| 791 |
|
| 792 |
value = mfree(value); |
| 793 |
} else |
| 794 |
u = 0; |
| 795 |
|
| 796 |
if (!GREEDY_REALLOC(components, n_components + 1)) |
| 797 |
return -ENOMEM; |
| 798 |
|
| 799 |
components[n_components++] = (BusMatchComponent) { |
| 800 |
.type = t, |
| 801 |
.value_str = TAKE_PTR(value), |
| 802 |
.value_u8 = u, |
| 803 |
}; |
| 804 |
|
| 805 |
if (q[quoted] == 0) |
| 806 |
break; |
| 807 |
|
| 808 |
if (q[quoted] != ',') |
| 809 |
return -EINVAL; |
| 810 |
|
| 811 |
match = q + 1 + quoted; |
| 812 |
} |
| 813 |
|
| 814 |
/* Order the whole thing, so that we always generate the same tree */ |
| 815 |
typesafe_qsort(components, n_components, match_component_compare); |
| 816 |
|
| 817 |
/* Check for duplicates */ |
| 818 |
for (size_t i = 0; i+1 < n_components; i++) |
| 819 |
if (components[i].type == components[i+1].type) |
| 820 |
return -EINVAL; |
| 821 |
|
| 822 |
*ret_components = TAKE_PTR(components); |
| 823 |
*ret_n_components = n_components; |
| 824 |
|
| 825 |
return 0; |
| 826 |
} |
| 827 |
|
| 828 |
char* bus_match_to_string(BusMatchComponent *components, size_t n_components) { |
| 829 |
_cleanup_(memstream_done) MemStream m = {}; |
| 830 |
FILE *f; |
| 831 |
int r; |
| 832 |
|
| 833 |
if (n_components <= 0) |
| 834 |
return strdup(""); |
| 835 |
|
| 836 |
assert(components); |
| 837 |
|
| 838 |
f = memstream_init(&m); |
| 839 |
if (!f) |
| 840 |
return NULL; |
| 841 |
|
| 842 |
for (size_t i = 0; i < n_components; i++) { |
| 843 |
char buf[32]; |
| 844 |
|
| 845 |
if (i != 0) |
| 846 |
fputc(',', f); |
| 847 |
|
| 848 |
fputs(bus_match_node_type_to_string(components[i].type, buf, sizeof(buf)), f); |
| 849 |
fputc('=', f); |
| 850 |
fputc('\'', f); |
| 851 |
|
| 852 |
if (components[i].type == BUS_MATCH_MESSAGE_TYPE) |
| 853 |
fputs(bus_message_type_to_string(components[i].value_u8), f); |
| 854 |
else |
| 855 |
fputs(components[i].value_str, f); |
| 856 |
|
| 857 |
fputc('\'', f); |
| 858 |
} |
| 859 |
|
| 860 |
char *buffer; |
| 861 |
r = memstream_finalize(&m, &buffer, NULL); |
| 862 |
if (r < 0) |
| 863 |
return NULL; |
| 864 |
|
| 865 |
return buffer; |
| 866 |
} |
| 867 |
|
| 868 |
int bus_match_add( |
| 869 |
BusMatchNode *root, |
| 870 |
BusMatchComponent *components, |
| 871 |
size_t n_components, |
| 872 |
BusMatchCallback *callback) { |
| 873 |
|
| 874 |
int r; |
| 875 |
|
| 876 |
assert(root); |
| 877 |
assert(callback); |
| 878 |
|
| 879 |
for (size_t i = 0; i < n_components; i++) { |
| 880 |
r = bus_match_add_compare_value(root, |
| 881 |
components[i].type, |
| 882 |
components[i].value_u8, |
| 883 |
components[i].value_str, |
| 884 |
&root); |
| 885 |
if (r < 0) |
| 886 |
return r; |
| 887 |
} |
| 888 |
|
| 889 |
return bus_match_add_leaf(root, callback); |
| 890 |
} |
| 891 |
|
| 892 |
int bus_match_remove( |
| 893 |
BusMatchNode *root, |
| 894 |
BusMatchCallback *callback) { |
| 895 |
|
| 896 |
BusMatchNode *node, *pp; |
| 897 |
|
| 898 |
assert(root); |
| 899 |
assert(callback); |
| 900 |
|
| 901 |
node = callback->match_node; |
| 902 |
if (!node) |
| 903 |
return 0; |
| 904 |
|
| 905 |
assert(node->type == BUS_MATCH_LEAF); |
| 906 |
|
| 907 |
callback->match_node = NULL; |
| 908 |
|
| 909 |
/* Free the leaf */ |
| 910 |
pp = node->parent; |
| 911 |
bus_match_node_free(node); |
| 912 |
|
| 913 |
/* Prune the tree above */ |
| 914 |
while (pp) { |
| 915 |
node = pp; |
| 916 |
pp = node->parent; |
| 917 |
|
| 918 |
if (!bus_match_node_maybe_free(node)) |
| 919 |
break; |
| 920 |
} |
| 921 |
|
| 922 |
return 1; |
| 923 |
} |
| 924 |
|
| 925 |
void bus_match_free(BusMatchNode *node) { |
| 926 |
BusMatchNode *c; |
| 927 |
|
| 928 |
if (!node) |
| 929 |
return; |
| 930 |
|
| 931 |
if (BUS_MATCH_CAN_HASH(node->type)) { |
| 932 |
|
| 933 |
HASHMAP_FOREACH(c, node->compare.children) |
| 934 |
bus_match_free(c); |
| 935 |
|
| 936 |
assert(hashmap_isempty(node->compare.children)); |
| 937 |
} |
| 938 |
|
| 939 |
while ((c = node->child)) |
| 940 |
bus_match_free(c); |
| 941 |
|
| 942 |
if (node->type != BUS_MATCH_ROOT) |
| 943 |
bus_match_node_free(node); |
| 944 |
} |
| 945 |
|
| 946 |
const char* bus_match_node_type_to_string(BusMatchNodeType t, char buf[], size_t l) { |
| 947 |
switch (t) { |
| 948 |
|
| 949 |
case BUS_MATCH_ROOT: |
| 950 |
return "root"; |
| 951 |
|
| 952 |
case BUS_MATCH_VALUE: |
| 953 |
return "value"; |
| 954 |
|
| 955 |
case BUS_MATCH_LEAF: |
| 956 |
return "leaf"; |
| 957 |
|
| 958 |
case BUS_MATCH_MESSAGE_TYPE: |
| 959 |
return "type"; |
| 960 |
|
| 961 |
case BUS_MATCH_SENDER: |
| 962 |
return "sender"; |
| 963 |
|
| 964 |
case BUS_MATCH_DESTINATION: |
| 965 |
return "destination"; |
| 966 |
|
| 967 |
case BUS_MATCH_INTERFACE: |
| 968 |
return "interface"; |
| 969 |
|
| 970 |
case BUS_MATCH_MEMBER: |
| 971 |
return "member"; |
| 972 |
|
| 973 |
case BUS_MATCH_PATH: |
| 974 |
return "path"; |
| 975 |
|
| 976 |
case BUS_MATCH_PATH_NAMESPACE: |
| 977 |
return "path_namespace"; |
| 978 |
|
| 979 |
case BUS_MATCH_ARG ... BUS_MATCH_ARG_LAST: |
| 980 |
return snprintf_ok(buf, l, "arg%i", t - BUS_MATCH_ARG); |
| 981 |
|
| 982 |
case BUS_MATCH_ARG_PATH ... BUS_MATCH_ARG_PATH_LAST: |
| 983 |
return snprintf_ok(buf, l, "arg%ipath", t - BUS_MATCH_ARG_PATH); |
| 984 |
|
| 985 |
case BUS_MATCH_ARG_NAMESPACE ... BUS_MATCH_ARG_NAMESPACE_LAST: |
| 986 |
return snprintf_ok(buf, l, "arg%inamespace", t - BUS_MATCH_ARG_NAMESPACE); |
| 987 |
|
| 988 |
case BUS_MATCH_ARG_HAS ... BUS_MATCH_ARG_HAS_LAST: |
| 989 |
return snprintf_ok(buf, l, "arg%ihas", t - BUS_MATCH_ARG_HAS); |
| 990 |
|
| 991 |
default: |
| 992 |
return NULL; |
| 993 |
} |
| 994 |
} |
| 995 |
|
| 996 |
void bus_match_dump(FILE *out, BusMatchNode *node, unsigned level) { |
| 997 |
char buf[32]; |
| 998 |
|
| 999 |
if (!node) |
| 1000 |
return; |
| 1001 |
|
| 1002 |
fprintf(out, "%*s[%s]", 2 * (int) level, "", bus_match_node_type_to_string(node->type, buf, sizeof(buf))); |
| 1003 |
|
| 1004 |
if (node->type == BUS_MATCH_VALUE) { |
| 1005 |
if (node->parent->type == BUS_MATCH_MESSAGE_TYPE) |
| 1006 |
fprintf(out, " <%u>\n", node->value.u8); |
| 1007 |
else |
| 1008 |
fprintf(out, " <%s>\n", node->value.str); |
| 1009 |
} else if (node->type == BUS_MATCH_ROOT) |
| 1010 |
fputs(" root\n", out); |
| 1011 |
else if (node->type == BUS_MATCH_LEAF) |
| 1012 |
fprintf(out, " %p/%p\n", node->leaf.callback->callback, |
| 1013 |
container_of(node->leaf.callback, sd_bus_slot, match_callback)->userdata); |
| 1014 |
else |
| 1015 |
putc('\n', out); |
| 1016 |
|
| 1017 |
if (BUS_MATCH_CAN_HASH(node->type)) { |
| 1018 |
BusMatchNode *c; |
| 1019 |
HASHMAP_FOREACH(c, node->compare.children) |
| 1020 |
bus_match_dump(out, c, level + 1); |
| 1021 |
} |
| 1022 |
|
| 1023 |
for (BusMatchNode *c = node->child; c; c = c->next) |
| 1024 |
bus_match_dump(out, c, level + 1); |
| 1025 |
} |
| 1026 |
|
| 1027 |
BusMatchScope bus_match_get_scope(const BusMatchComponent *components, size_t n_components) { |
| 1028 |
bool found_driver = false; |
| 1029 |
|
| 1030 |
if (n_components <= 0) |
| 1031 |
return BUS_MATCH_GENERIC; |
| 1032 |
|
| 1033 |
assert(components); |
| 1034 |
|
| 1035 |
/* Checks whether the specified match can only match the |
| 1036 |
* pseudo-service for local messages, which we detect by |
| 1037 |
* sender, interface or path. If a match is not restricted to |
| 1038 |
* local messages, then we check if it only matches on the |
| 1039 |
* driver. */ |
| 1040 |
|
| 1041 |
for (size_t i = 0; i < n_components; i++) { |
| 1042 |
const BusMatchComponent *c = components + i; |
| 1043 |
|
| 1044 |
if (c->type == BUS_MATCH_SENDER) { |
| 1045 |
if (streq_ptr(c->value_str, "org.freedesktop.DBus.Local")) |
| 1046 |
return BUS_MATCH_LOCAL; |
| 1047 |
|
| 1048 |
if (streq_ptr(c->value_str, "org.freedesktop.DBus")) |
| 1049 |
found_driver = true; |
| 1050 |
} |
| 1051 |
|
| 1052 |
if (c->type == BUS_MATCH_INTERFACE && streq_ptr(c->value_str, "org.freedesktop.DBus.Local")) |
| 1053 |
return BUS_MATCH_LOCAL; |
| 1054 |
|
| 1055 |
if (c->type == BUS_MATCH_PATH && streq_ptr(c->value_str, "/org/freedesktop/DBus/Local")) |
| 1056 |
return BUS_MATCH_LOCAL; |
| 1057 |
} |
| 1058 |
|
| 1059 |
return found_driver ? BUS_MATCH_DRIVER : BUS_MATCH_GENERIC; |
| 1060 |
} |