Debian Codegraph @

src/busctl/busctl-introspect.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-bus-vtable.h"
4
5 #include "alloc-util.h"
6 #include "busctl-introspect.h"
7 #include "log.h"
8 #include "path-util.h"
9 #include "string-util.h"
10 #include "xml.h"
11
12 #define NODE_DEPTH_MAX 16
13
14 typedef struct Context {
15 const XMLIntrospectOps *ops;
16 void *userdata;
17
18 char *interface_name;
19 uint64_t interface_flags;
20
21 char *member_name;
22 char *member_signature;
23 char *member_result;
24 uint64_t member_flags;
25 bool member_writable;
26
27 const char *current;
28 void *xml_state;
29 } Context;
30
31 static void context_reset_member(Context *c) {
32 free(c->member_name);
33 free(c->member_signature);
34 free(c->member_result);
35
36 c->member_name = c->member_signature = c->member_result = NULL;
37 c->member_flags = 0;
38 c->member_writable = false;
39 }
40
41 static void context_reset_interface(Context *c) {
42 c->interface_name = mfree(c->interface_name);
43 c->interface_flags = 0;
44
45 context_reset_member(c);
46 }
47
48 static int parse_xml_annotation(Context *context, uint64_t *flags) {
49
50 enum {
51 STATE_ANNOTATION,
52 STATE_NAME,
53 STATE_VALUE
54 } state = STATE_ANNOTATION;
55
56 _cleanup_free_ char *field = NULL, *value = NULL;
57
58 assert(context);
59
60 for (;;) {
61 _cleanup_free_ char *name = NULL;
62
63 int t;
64
65 t = xml_tokenize(&context->current, &name, &context->xml_state, NULL);
66 if (t < 0) {
67 log_error("XML parse error.");
68 return t;
69 }
70
71 if (t == XML_END)
72 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG),
73 "Premature end of XML data.");
74
75 switch (state) {
76
77 case STATE_ANNOTATION:
78
79 if (t == XML_ATTRIBUTE_NAME) {
80
81 if (streq_ptr(name, "name"))
82 state = STATE_NAME;
83
84 else if (streq_ptr(name, "value"))
85 state = STATE_VALUE;
86
87 else
88 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG),
89 "Unexpected <annotation> attribute %s.",
90 name);
91
92 } else if (t == XML_TAG_CLOSE_EMPTY ||
93 (t == XML_TAG_CLOSE && streq_ptr(name, "annotation"))) {
94
95 if (flags) {
96 if (streq_ptr(field, "org.freedesktop.DBus.Deprecated")) {
97
98 if (streq_ptr(value, "true"))
99 *flags |= SD_BUS_VTABLE_DEPRECATED;
100
101 } else if (streq_ptr(field, "org.freedesktop.DBus.Method.NoReply")) {
102
103 if (streq_ptr(value, "true"))
104 *flags |= SD_BUS_VTABLE_METHOD_NO_REPLY;
105
106 } else if (streq_ptr(field, "org.freedesktop.DBus.Property.EmitsChangedSignal")) {
107
108 if (streq_ptr(value, "const"))
109 *flags = (*flags & ~(SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION|SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE)) | SD_BUS_VTABLE_PROPERTY_CONST;
110 else if (streq_ptr(value, "invalidates"))
111 *flags = (*flags & ~(SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE|SD_BUS_VTABLE_PROPERTY_CONST)) | SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION;
112 else if (streq_ptr(value, "false"))
113 *flags = *flags & ~(SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE|SD_BUS_VTABLE_PROPERTY_CONST|SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION);
114 }
115 }
116
117 return 0;
118
119 } else if (t != XML_TEXT || !in_charset(name, WHITESPACE))
120 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
121 "Unexpected token in <annotation>. (1)");
122
123 break;
124
125 case STATE_NAME:
126
127 if (t == XML_ATTRIBUTE_VALUE) {
128 free_and_replace(field, name);
129
130 state = STATE_ANNOTATION;
131 } else
132 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
133 "Unexpected token in <annotation>. (2)");
134
135 break;
136
137 case STATE_VALUE:
138
139 if (t == XML_ATTRIBUTE_VALUE) {
140 free_and_replace(value, name);
141
142 state = STATE_ANNOTATION;
143 } else
144 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
145 "Unexpected token in <annotation>. (3)");
146
147 break;
148
149 default:
150 assert_not_reached();
151 }
152 }
153 }
154
155 static int parse_xml_node(Context *context, const char *prefix, unsigned n_depth) {
156
157 enum {
158 STATE_NODE,
159 STATE_NODE_NAME,
160 STATE_INTERFACE,
161 STATE_INTERFACE_NAME,
162 STATE_METHOD,
163 STATE_METHOD_NAME,
164 STATE_METHOD_ARG,
165 STATE_METHOD_ARG_NAME,
166 STATE_METHOD_ARG_TYPE,
167 STATE_METHOD_ARG_DIRECTION,
168 STATE_SIGNAL,
169 STATE_SIGNAL_NAME,
170 STATE_SIGNAL_ARG,
171 STATE_SIGNAL_ARG_NAME,
172 STATE_SIGNAL_ARG_TYPE,
173 STATE_SIGNAL_ARG_DIRECTION,
174 STATE_PROPERTY,
175 STATE_PROPERTY_NAME,
176 STATE_PROPERTY_TYPE,
177 STATE_PROPERTY_ACCESS,
178 } state = STATE_NODE;
179
180 _cleanup_free_ char *node_path = NULL, *argument_type = NULL, *argument_direction = NULL;
181 const char *np = ASSERT_PTR(prefix);
182 int r;
183
184 assert(context);
185
186 if (n_depth > NODE_DEPTH_MAX)
187 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "<node> depth too high.");
188
189 for (;;) {
190 _cleanup_free_ char *name = NULL;
191 int t;
192
193 t = xml_tokenize(&context->current, &name, &context->xml_state, NULL);
194 if (t < 0) {
195 log_error("XML parse error.");
196 return t;
197 }
198
199 if (t == XML_END)
200 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Premature end of XML data.");
201
202 switch (state) {
203
204 case STATE_NODE:
205 if (t == XML_ATTRIBUTE_NAME) {
206
207 if (streq_ptr(name, "name"))
208 state = STATE_NODE_NAME;
209 else
210 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG),
211 "Unexpected <node> attribute %s.", name);
212
213 } else if (t == XML_TAG_OPEN) {
214
215 if (streq_ptr(name, "interface"))
216 state = STATE_INTERFACE;
217 else if (streq_ptr(name, "node")) {
218
219 r = parse_xml_node(context, np, n_depth+1);
220 if (r < 0)
221 return r;
222 } else
223 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG),
224 "Unexpected <node> tag %s.", name);
225
226 } else if (t == XML_TAG_CLOSE_EMPTY ||
227 (t == XML_TAG_CLOSE && streq_ptr(name, "node"))) {
228
229 if (context->ops->on_path) {
230 r = context->ops->on_path(node_path ?: np, context->userdata);
231 if (r < 0)
232 return r;
233 }
234
235 return 0;
236
237 } else if (t != XML_TEXT || !in_charset(name, WHITESPACE))
238 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
239 "Unexpected token in <node>. (1)");
240
241 break;
242
243 case STATE_NODE_NAME:
244
245 if (t == XML_ATTRIBUTE_VALUE) {
246
247 free(node_path);
248
249 if (name[0] == '/')
250 node_path = TAKE_PTR(name);
251 else {
252 node_path = path_join(prefix, name);
253 if (!node_path)
254 return log_oom();
255 }
256
257 np = node_path;
258 state = STATE_NODE;
259 } else
260 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
261 "Unexpected token in <node>. (2)");
262
263 break;
264
265 case STATE_INTERFACE:
266
267 if (t == XML_ATTRIBUTE_NAME) {
268 if (streq_ptr(name, "name"))
269 state = STATE_INTERFACE_NAME;
270 else
271 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG),
272 "Unexpected <interface> attribute %s.",
273 name);
274
275 } else if (t == XML_TAG_OPEN) {
276 if (streq_ptr(name, "method"))
277 state = STATE_METHOD;
278 else if (streq_ptr(name, "signal"))
279 state = STATE_SIGNAL;
280 else if (streq_ptr(name, "property")) {
281 context->member_flags |= SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE;
282 state = STATE_PROPERTY;
283 } else if (streq_ptr(name, "annotation")) {
284 r = parse_xml_annotation(context, &context->interface_flags);
285 if (r < 0)
286 return r;
287 } else
288 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
289 "Unexpected <interface> tag %s.", name);
290 } else if (t == XML_TAG_CLOSE_EMPTY ||
291 (t == XML_TAG_CLOSE && streq_ptr(name, "interface"))) {
292
293 if (n_depth == 0) {
294 if (context->ops->on_interface) {
295 r = context->ops->on_interface(context->interface_name, context->interface_flags, context->userdata);
296 if (r < 0)
297 return r;
298 }
299
300 context_reset_interface(context);
301 }
302
303 state = STATE_NODE;
304
305 } else if (t != XML_TEXT || !in_charset(name, WHITESPACE))
306 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
307 "Unexpected token in <interface>. (1)");
308
309 break;
310
311 case STATE_INTERFACE_NAME:
312
313 if (t == XML_ATTRIBUTE_VALUE) {
314 if (n_depth == 0)
315 free_and_replace(context->interface_name, name);
316
317 state = STATE_INTERFACE;
318 } else
319 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
320 "Unexpected token in <interface>. (2)");
321
322 break;
323
324 case STATE_METHOD:
325
326 if (t == XML_ATTRIBUTE_NAME) {
327 if (streq_ptr(name, "name"))
328 state = STATE_METHOD_NAME;
329 else
330 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG),
331 "Unexpected <method> attribute %s",
332 name);
333 } else if (t == XML_TAG_OPEN) {
334 if (streq_ptr(name, "arg"))
335 state = STATE_METHOD_ARG;
336 else if (streq_ptr(name, "annotation")) {
337 r = parse_xml_annotation(context, &context->member_flags);
338 if (r < 0)
339 return r;
340 } else
341 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
342 "Unexpected <method> tag %s.",
343 name);
344 } else if (t == XML_TAG_CLOSE_EMPTY ||
345 (t == XML_TAG_CLOSE && streq_ptr(name, "method"))) {
346
347 if (n_depth == 0) {
348 if (context->ops->on_method) {
349 r = context->ops->on_method(context->interface_name, context->member_name, context->member_signature, context->member_result, context->member_flags, context->userdata);
350 if (r < 0)
351 return r;
352 }
353
354 context_reset_member(context);
355 }
356
357 state = STATE_INTERFACE;
358
359 } else if (t != XML_TEXT || !in_charset(name, WHITESPACE))
360 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
361 "Unexpected token in <method> (1).");
362
363 break;
364
365 case STATE_METHOD_NAME:
366
367 if (t == XML_ATTRIBUTE_VALUE) {
368 if (n_depth == 0)
369 free_and_replace(context->member_name, name);
370
371 state = STATE_METHOD;
372 } else
373 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
374 "Unexpected token in <method> (2).");
375
376 break;
377
378 case STATE_METHOD_ARG:
379
380 if (t == XML_ATTRIBUTE_NAME) {
381 if (streq_ptr(name, "name"))
382 state = STATE_METHOD_ARG_NAME;
383 else if (streq_ptr(name, "type"))
384 state = STATE_METHOD_ARG_TYPE;
385 else if (streq_ptr(name, "direction"))
386 state = STATE_METHOD_ARG_DIRECTION;
387 else
388 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG),
389 "Unexpected method <arg> attribute %s.",
390 name);
391 } else if (t == XML_TAG_OPEN) {
392 if (streq_ptr(name, "annotation")) {
393 r = parse_xml_annotation(context, NULL);
394 if (r < 0)
395 return r;
396 } else
397 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
398 "Unexpected method <arg> tag %s.",
399 name);
400 } else if (t == XML_TAG_CLOSE_EMPTY ||
401 (t == XML_TAG_CLOSE && streq_ptr(name, "arg"))) {
402
403 if (n_depth == 0) {
404
405 if (argument_type) {
406 if (!argument_direction || streq(argument_direction, "in")) {
407 if (!strextend(&context->member_signature, argument_type))
408 return log_oom();
409 } else if (streq(argument_direction, "out")) {
410 if (!strextend(&context->member_result, argument_type))
411 return log_oom();
412 } else
413 log_error("Unexpected method <arg> direction value '%s'.", argument_direction);
414 }
415
416 argument_type = mfree(argument_type);
417 argument_direction = mfree(argument_direction);
418 }
419
420 state = STATE_METHOD;
421 } else if (t != XML_TEXT || !in_charset(name, WHITESPACE))
422 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
423 "Unexpected token in method <arg>. (1)");
424
425 break;
426
427 case STATE_METHOD_ARG_NAME:
428
429 if (t == XML_ATTRIBUTE_VALUE)
430 state = STATE_METHOD_ARG;
431 else
432 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
433 "Unexpected token in method <arg>. (2)");
434
435 break;
436
437 case STATE_METHOD_ARG_TYPE:
438
439 if (t == XML_ATTRIBUTE_VALUE) {
440 free_and_replace(argument_type, name);
441
442 state = STATE_METHOD_ARG;
443 } else
444 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
445 "Unexpected token in method <arg>. (3)");
446
447 break;
448
449 case STATE_METHOD_ARG_DIRECTION:
450
451 if (t == XML_ATTRIBUTE_VALUE) {
452 free_and_replace(argument_direction, name);
453
454 state = STATE_METHOD_ARG;
455 } else
456 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
457 "Unexpected token in method <arg>. (4)");
458
459 break;
460
461 case STATE_SIGNAL:
462
463 if (t == XML_ATTRIBUTE_NAME) {
464 if (streq_ptr(name, "name"))
465 state = STATE_SIGNAL_NAME;
466 else
467 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG),
468 "Unexpected <signal> attribute %s.",
469 name);
470 } else if (t == XML_TAG_OPEN) {
471 if (streq_ptr(name, "arg"))
472 state = STATE_SIGNAL_ARG;
473 else if (streq_ptr(name, "annotation")) {
474 r = parse_xml_annotation(context, &context->member_flags);
475 if (r < 0)
476 return r;
477 } else
478 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
479 "Unexpected <signal> tag %s.",
480 name);
481 } else if (t == XML_TAG_CLOSE_EMPTY ||
482 (t == XML_TAG_CLOSE && streq_ptr(name, "signal"))) {
483
484 if (n_depth == 0) {
485 if (context->ops->on_signal) {
486 r = context->ops->on_signal(context->interface_name, context->member_name, context->member_signature, context->member_flags, context->userdata);
487 if (r < 0)
488 return r;
489 }
490
491 context_reset_member(context);
492 }
493
494 state = STATE_INTERFACE;
495
496 } else if (t != XML_TEXT || !in_charset(name, WHITESPACE))
497 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
498 "Unexpected token in <signal>. (1)");
499
500 break;
501
502 case STATE_SIGNAL_NAME:
503
504 if (t == XML_ATTRIBUTE_VALUE) {
505 if (n_depth == 0)
506 free_and_replace(context->member_name, name);
507
508 state = STATE_SIGNAL;
509 } else
510 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
511 "Unexpected token in <signal>. (2)");
512
513 break;
514
515 case STATE_SIGNAL_ARG:
516
517 if (t == XML_ATTRIBUTE_NAME) {
518 if (streq_ptr(name, "name"))
519 state = STATE_SIGNAL_ARG_NAME;
520 else if (streq_ptr(name, "type"))
521 state = STATE_SIGNAL_ARG_TYPE;
522 else if (streq_ptr(name, "direction"))
523 state = STATE_SIGNAL_ARG_DIRECTION;
524 else
525 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG),
526 "Unexpected signal <arg> attribute %s.",
527 name);
528 } else if (t == XML_TAG_OPEN) {
529 if (streq_ptr(name, "annotation")) {
530 r = parse_xml_annotation(context, NULL);
531 if (r < 0)
532 return r;
533 } else
534 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
535 "Unexpected signal <arg> tag %s.",
536 name);
537 } else if (t == XML_TAG_CLOSE_EMPTY ||
538 (t == XML_TAG_CLOSE && streq_ptr(name, "arg"))) {
539
540 if (argument_type) {
541 if (!argument_direction || streq(argument_direction, "out")) {
542 if (!strextend(&context->member_signature, argument_type))
543 return log_oom();
544 } else
545 log_error("Unexpected signal <arg> direction value '%s'.", argument_direction);
546
547 argument_type = mfree(argument_type);
548 }
549
550 state = STATE_SIGNAL;
551 } else if (t != XML_TEXT || !in_charset(name, WHITESPACE))
552 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
553 "Unexpected token in signal <arg> (1).");
554
555 break;
556
557 case STATE_SIGNAL_ARG_NAME:
558
559 if (t == XML_ATTRIBUTE_VALUE)
560 state = STATE_SIGNAL_ARG;
561 else
562 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
563 "Unexpected token in signal <arg> (2).");
564
565 break;
566
567 case STATE_SIGNAL_ARG_TYPE:
568
569 if (t == XML_ATTRIBUTE_VALUE) {
570 free_and_replace(argument_type, name);
571
572 state = STATE_SIGNAL_ARG;
573 } else
574 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
575 "Unexpected token in signal <arg> (3).");
576
577 break;
578
579 case STATE_SIGNAL_ARG_DIRECTION:
580
581 if (t == XML_ATTRIBUTE_VALUE) {
582 free_and_replace(argument_direction, name);
583
584 state = STATE_SIGNAL_ARG;
585 } else
586 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
587 "Unexpected token in signal <arg>. (4)");
588
589 break;
590
591 case STATE_PROPERTY:
592
593 if (t == XML_ATTRIBUTE_NAME) {
594 if (streq_ptr(name, "name"))
595 state = STATE_PROPERTY_NAME;
596 else if (streq_ptr(name, "type"))
597 state = STATE_PROPERTY_TYPE;
598 else if (streq_ptr(name, "access"))
599 state = STATE_PROPERTY_ACCESS;
600 else
601 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG),
602 "Unexpected <property> attribute %s.",
603 name);
604 } else if (t == XML_TAG_OPEN) {
605
606 if (streq_ptr(name, "annotation")) {
607 r = parse_xml_annotation(context, &context->member_flags);
608 if (r < 0)
609 return r;
610 } else
611 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
612 "Unexpected <property> tag %s.",
613 name);
614
615 } else if (t == XML_TAG_CLOSE_EMPTY ||
616 (t == XML_TAG_CLOSE && streq_ptr(name, "property"))) {
617
618 if (n_depth == 0) {
619 if (context->ops->on_property) {
620 r = context->ops->on_property(context->interface_name, context->member_name, context->member_signature, context->member_writable, context->member_flags, context->userdata);
621 if (r < 0)
622 return r;
623 }
624
625 context_reset_member(context);
626 }
627
628 state = STATE_INTERFACE;
629
630 } else if (t != XML_TEXT || !in_charset(name, WHITESPACE))
631 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
632 "Unexpected token in <property>. (1)");
633
634 break;
635
636 case STATE_PROPERTY_NAME:
637
638 if (t == XML_ATTRIBUTE_VALUE) {
639 if (n_depth == 0)
640 free_and_replace(context->member_name, name);
641
642 state = STATE_PROPERTY;
643 } else
644 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
645 "Unexpected token in <property>. (2)");
646
647 break;
648
649 case STATE_PROPERTY_TYPE:
650
651 if (t == XML_ATTRIBUTE_VALUE) {
652 if (n_depth == 0)
653 free_and_replace(context->member_signature, name);
654
655 state = STATE_PROPERTY;
656 } else
657 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
658 "Unexpected token in <property>. (3)");
659
660 break;
661
662 case STATE_PROPERTY_ACCESS:
663
664 if (t == XML_ATTRIBUTE_VALUE) {
665
666 if (streq(name, "readwrite") || streq(name, "write"))
667 context->member_writable = true;
668
669 state = STATE_PROPERTY;
670 } else
671 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
672 "Unexpected token in <property>. (4)");
673
674 break;
675 }
676 }
677 }
678
679 int parse_xml_introspect(const char *prefix, const char *xml, const XMLIntrospectOps *ops, void *userdata) {
680 _cleanup_(context_reset_interface) Context context = {
681 .ops = ops,
682 .userdata = userdata,
683 .current = xml,
684 };
685
686 int r;
687
688 assert(prefix);
689 assert(xml);
690 assert(ops);
691
692 for (;;) {
693 _cleanup_free_ char *name = NULL;
694
695 r = xml_tokenize(&context.current, &name, &context.xml_state, NULL);
696 if (r < 0)
697 return log_error_errno(r, "XML parse error");
698
699 if (r == XML_END)
700 break;
701
702 if (r == XML_TAG_OPEN) {
703
704 if (streq(name, "node")) {
705 r = parse_xml_node(&context, prefix, 0);
706 if (r < 0)
707 return r;
708 } else
709 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG),
710 "Unexpected tag '%s' in introspection data.", name);
711 } else if (r != XML_TEXT || !in_charset(name, WHITESPACE))
712 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Unexpected token.");
713 }
714
715 return 0;
716 }