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