forked from adamlaska/systemd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbusctl.c
More file actions
2561 lines (1993 loc) · 88.2 KB
/
busctl.c
File metadata and controls
2561 lines (1993 loc) · 88.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <getopt.h>
#include "sd-bus.h"
#include "alloc-util.h"
#include "bus-dump.h"
#include "bus-internal.h"
#include "bus-message.h"
#include "bus-signature.h"
#include "bus-type.h"
#include "bus-util.h"
#include "busctl-introspect.h"
#include "escape.h"
#include "fd-util.h"
#include "fileio.h"
#include "format-table.h"
#include "glyph-util.h"
#include "json.h"
#include "log.h"
#include "main-func.h"
#include "os-util.h"
#include "pager.h"
#include "parse-argument.h"
#include "parse-util.h"
#include "path-util.h"
#include "pretty-print.h"
#include "set.h"
#include "sort-util.h"
#include "strv.h"
#include "terminal-util.h"
#include "user-util.h"
#include "verbs.h"
#include "version.h"
static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF;
static PagerFlags arg_pager_flags = 0;
static bool arg_legend = true;
static bool arg_full = false;
static const char *arg_address = NULL;
static bool arg_unique = false;
static bool arg_acquired = false;
static bool arg_activatable = false;
static bool arg_show_machine = false;
static char **arg_matches = NULL;
static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
static const char *arg_host = NULL;
static bool arg_user = false;
static size_t arg_snaplen = 4096;
static bool arg_list = false;
static bool arg_quiet = false;
static bool arg_verbose = false;
static bool arg_xml_interface = false;
static bool arg_expect_reply = true;
static bool arg_auto_start = true;
static bool arg_allow_interactive_authorization = true;
static bool arg_augment_creds = true;
static bool arg_watch_bind = false;
static usec_t arg_timeout = 0;
static const char *arg_destination = NULL;
STATIC_DESTRUCTOR_REGISTER(arg_matches, strv_freep);
#define NAME_IS_ACQUIRED INT_TO_PTR(1)
#define NAME_IS_ACTIVATABLE INT_TO_PTR(2)
static int json_transform_message(sd_bus_message *m, JsonVariant **ret);
static int acquire_bus(bool set_monitor, sd_bus **ret) {
_cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL;
int r;
r = sd_bus_new(&bus);
if (r < 0)
return log_error_errno(r, "Failed to allocate bus: %m");
if (set_monitor) {
r = sd_bus_set_monitor(bus, true);
if (r < 0)
return log_error_errno(r, "Failed to set monitor mode: %m");
r = sd_bus_negotiate_creds(bus, true, _SD_BUS_CREDS_ALL);
if (r < 0)
return log_error_errno(r, "Failed to enable credentials: %m");
r = sd_bus_negotiate_timestamp(bus, true);
if (r < 0)
return log_error_errno(r, "Failed to enable timestamps: %m");
r = sd_bus_negotiate_fds(bus, true);
if (r < 0)
return log_error_errno(r, "Failed to enable fds: %m");
}
r = sd_bus_set_bus_client(bus, true);
if (r < 0)
return log_error_errno(r, "Failed to set bus client: %m");
r = sd_bus_set_watch_bind(bus, arg_watch_bind);
if (r < 0)
return log_error_errno(r, "Failed to set watch-bind setting to '%s': %m",
yes_no(arg_watch_bind));
if (arg_address)
r = sd_bus_set_address(bus, arg_address);
else
switch (arg_transport) {
case BUS_TRANSPORT_LOCAL:
if (arg_user)
r = bus_set_address_user(bus);
else
r = bus_set_address_system(bus);
break;
case BUS_TRANSPORT_REMOTE:
r = bus_set_address_system_remote(bus, arg_host);
break;
case BUS_TRANSPORT_MACHINE:
r = bus_set_address_machine(bus, arg_user, arg_host);
break;
default:
assert_not_reached();
}
if (r < 0)
return bus_log_address_error(r, arg_transport);
r = sd_bus_start(bus);
if (r < 0)
return bus_log_connect_error(r, arg_transport);
*ret = TAKE_PTR(bus);
return 0;
}
static int list_bus_names(int argc, char **argv, void *userdata) {
_cleanup_strv_free_ char **acquired = NULL, **activatable = NULL;
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
_cleanup_hashmap_free_ Hashmap *names = NULL;
_cleanup_(table_unrefp) Table *table = NULL;
char *k;
void *v;
int r;
enum {
COLUMN_ACTIVATABLE,
COLUMN_NAME,
COLUMN_PID,
COLUMN_PROCESS,
COLUMN_USER,
COLUMN_CONNECTION,
COLUMN_UNIT,
COLUMN_SESSION,
COLUMN_DESCRIPTION,
COLUMN_MACHINE,
};
if (!arg_unique && !arg_acquired && !arg_activatable)
arg_unique = arg_acquired = arg_activatable = true;
r = acquire_bus(false, &bus);
if (r < 0)
return r;
r = sd_bus_list_names(bus,
(arg_acquired || arg_unique) ? &acquired : NULL,
arg_activatable ? &activatable : NULL);
if (r < 0)
return log_error_errno(r, "Failed to list names: %m");
names = hashmap_new(&string_hash_ops);
if (!names)
return log_oom();
STRV_FOREACH(i, acquired) {
r = hashmap_put(names, *i, NAME_IS_ACQUIRED);
if (r < 0)
return log_error_errno(r, "Failed to add to hashmap: %m");
}
STRV_FOREACH(i, activatable) {
r = hashmap_put(names, *i, NAME_IS_ACTIVATABLE);
if (r < 0 && r != -EEXIST)
return log_error_errno(r, "Failed to add to hashmap: %m");
}
table = table_new("activatable",
"name",
"pid",
"process",
"user",
"connection",
"unit",
"session",
"description",
"machine");
if (!table)
return log_oom();
if (arg_full)
table_set_width(table, 0);
r = table_set_align_percent(table, table_get_cell(table, 0, COLUMN_PID), 100);
if (r < 0)
return log_error_errno(r, "Failed to set alignment: %m");
r = table_set_empty_string(table, "-");
if (r < 0)
return log_error_errno(r, "Failed to set empty string: %m");
r = table_set_sort(table, (size_t) COLUMN_NAME);
if (r < 0)
return log_error_errno(r, "Failed to set sort column: %m");
if (arg_show_machine)
r = table_set_display(table, (size_t) COLUMN_NAME,
(size_t) COLUMN_PID,
(size_t) COLUMN_PROCESS,
(size_t) COLUMN_USER,
(size_t) COLUMN_CONNECTION,
(size_t) COLUMN_UNIT,
(size_t) COLUMN_SESSION,
(size_t) COLUMN_DESCRIPTION,
(size_t) COLUMN_MACHINE);
else
r = table_set_display(table, (size_t) COLUMN_NAME,
(size_t) COLUMN_PID,
(size_t) COLUMN_PROCESS,
(size_t) COLUMN_USER,
(size_t) COLUMN_CONNECTION,
(size_t) COLUMN_UNIT,
(size_t) COLUMN_SESSION,
(size_t) COLUMN_DESCRIPTION);
if (r < 0)
return log_error_errno(r, "Failed to set columns to display: %m");
HASHMAP_FOREACH_KEY(v, k, names) {
_cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
if (v == NAME_IS_ACTIVATABLE) {
r = table_add_many(
table,
TABLE_INT, PTR_TO_INT(v),
TABLE_STRING, k,
TABLE_EMPTY,
TABLE_EMPTY,
TABLE_EMPTY,
TABLE_STRING, "(activatable)", TABLE_SET_COLOR, ansi_grey(),
TABLE_EMPTY,
TABLE_EMPTY,
TABLE_EMPTY,
TABLE_EMPTY);
if (r < 0)
return table_log_add_error(r);
continue;
}
assert(v == NAME_IS_ACQUIRED);
if (!arg_unique && k[0] == ':')
continue;
if (!arg_acquired && k[0] != ':')
continue;
r = table_add_many(table,
TABLE_INT, PTR_TO_INT(v),
TABLE_STRING, k);
if (r < 0)
return table_log_add_error(r);
r = sd_bus_get_name_creds(
bus, k,
(arg_augment_creds ? SD_BUS_CREDS_AUGMENT : 0) |
SD_BUS_CREDS_EUID|SD_BUS_CREDS_PID|SD_BUS_CREDS_COMM|
SD_BUS_CREDS_UNIQUE_NAME|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_SESSION|
SD_BUS_CREDS_DESCRIPTION, &creds);
if (r < 0) {
log_debug_errno(r, "Failed to acquire credentials of service %s, ignoring: %m", k);
r = table_fill_empty(table, COLUMN_MACHINE);
} else {
const char *unique = NULL, *session = NULL, *unit = NULL, *cn = NULL;
pid_t pid;
uid_t uid;
r = sd_bus_creds_get_pid(creds, &pid);
if (r >= 0) {
const char *comm = NULL;
(void) sd_bus_creds_get_comm(creds, &comm);
r = table_add_many(table,
TABLE_PID, pid,
TABLE_STRING, strna(comm));
} else
r = table_add_many(table, TABLE_EMPTY, TABLE_EMPTY);
if (r < 0)
return table_log_add_error(r);
r = sd_bus_creds_get_euid(creds, &uid);
if (r >= 0) {
_cleanup_free_ char *u = NULL;
u = uid_to_name(uid);
if (!u)
return log_oom();
r = table_add_cell(table, NULL, TABLE_STRING, u);
} else
r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
if (r < 0)
return table_log_add_error(r);
(void) sd_bus_creds_get_unique_name(creds, &unique);
(void) sd_bus_creds_get_unit(creds, &unit);
(void) sd_bus_creds_get_session(creds, &session);
(void) sd_bus_creds_get_description(creds, &cn);
r = table_add_many(
table,
TABLE_STRING, unique,
TABLE_STRING, unit,
TABLE_STRING, session,
TABLE_STRING, cn);
}
if (r < 0)
return table_log_add_error(r);
if (arg_show_machine) {
sd_id128_t mid;
r = sd_bus_get_name_machine_id(bus, k, &mid);
if (r < 0)
log_debug_errno(r, "Failed to acquire credentials of service %s, ignoring: %m", k);
else {
r = table_add_cell(table, NULL, TABLE_ID128, &mid);
if (r < 0)
return table_log_add_error(r);
continue; /* line fully filled, no need to fill the remainder below */
}
}
r = table_fill_empty(table, 0);
if (r < 0)
return log_error_errno(r, "Failed to fill line: %m");
}
return table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
}
static void print_subtree(const char *prefix, const char *path, char **l) {
const char *vertical, *space;
char **n;
/* We assume the list is sorted. Let's first skip over the
* entry we are looking at. */
for (;;) {
if (!*l)
return;
if (!streq(*l, path))
break;
l++;
}
vertical = strjoina(prefix, special_glyph(SPECIAL_GLYPH_TREE_VERTICAL));
space = strjoina(prefix, special_glyph(SPECIAL_GLYPH_TREE_SPACE));
for (;;) {
bool has_more = false;
if (!*l || !path_startswith(*l, path))
break;
n = l + 1;
for (;;) {
if (!*n || !path_startswith(*n, path))
break;
if (!path_startswith(*n, *l)) {
has_more = true;
break;
}
n++;
}
printf("%s%s%s\n",
prefix,
special_glyph(has_more ? SPECIAL_GLYPH_TREE_BRANCH : SPECIAL_GLYPH_TREE_RIGHT),
*l);
print_subtree(has_more ? vertical : space, *l, l);
l = n;
}
}
static void print_tree(char **l) {
if (arg_list)
strv_print(l);
else if (strv_isempty(l))
printf("No objects discovered.\n");
else if (streq(l[0], "/") && !l[1])
printf("Only root object discovered.\n");
else
print_subtree("", "/", l);
}
static int on_path(const char *path, void *userdata) {
Set *paths = userdata;
int r;
assert(paths);
r = set_put_strdup(&paths, path);
if (r < 0)
return log_oom();
return 0;
}
static int find_nodes(sd_bus *bus, const char *service, const char *path, Set *paths) {
static const XMLIntrospectOps ops = {
.on_path = on_path,
};
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
const char *xml;
int r;
r = sd_bus_call_method(bus, service, path,
"org.freedesktop.DBus.Introspectable", "Introspect",
&error, &reply, NULL);
if (r < 0) {
printf("%sFailed to introspect object %s of service %s: %s%s\n",
ansi_highlight_red(),
path, service, bus_error_message(&error, r),
ansi_normal());
return r;
}
r = sd_bus_message_read(reply, "s", &xml);
if (r < 0)
return bus_log_parse_error(r);
return parse_xml_introspect(path, xml, &ops, paths);
}
static int tree_one(sd_bus *bus, const char *service) {
_cleanup_set_free_ Set *paths = NULL, *done = NULL, *failed = NULL;
_cleanup_free_ char **l = NULL;
int r;
r = set_put_strdup(&paths, "/");
if (r < 0)
return log_oom();
for (;;) {
_cleanup_free_ char *p = NULL;
int q;
p = set_steal_first(paths);
if (!p)
break;
if (set_contains(done, p) ||
set_contains(failed, p))
continue;
q = find_nodes(bus, service, p, paths);
if (q < 0 && r >= 0)
r = q;
q = set_ensure_consume(q < 0 ? &failed : &done, &string_hash_ops_free, TAKE_PTR(p));
assert(q != 0);
if (q < 0)
return log_oom();
}
pager_open(arg_pager_flags);
l = set_get_strv(done);
if (!l)
return log_oom();
strv_sort(l);
print_tree(l);
fflush(stdout);
return r;
}
static int tree(int argc, char **argv, void *userdata) {
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
int r;
/* Do superficial verification of arguments before even opening the bus */
STRV_FOREACH(i, strv_skip(argv, 1))
if (!sd_bus_service_name_is_valid(*i))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Invalid bus service name: %s", *i);
if (!arg_unique && !arg_acquired)
arg_acquired = true;
r = acquire_bus(false, &bus);
if (r < 0)
return r;
if (argc <= 1) {
_cleanup_strv_free_ char **names = NULL;
bool not_first = false;
r = sd_bus_list_names(bus, &names, NULL);
if (r < 0)
return log_error_errno(r, "Failed to get name list: %m");
pager_open(arg_pager_flags);
STRV_FOREACH(i, names) {
int q;
if (!arg_unique && (*i)[0] == ':')
continue;
if (!arg_acquired && (*i)[0] == ':')
continue;
if (not_first)
printf("\n");
printf("Service %s%s%s:\n", ansi_highlight(), *i, ansi_normal());
q = tree_one(bus, *i);
if (q < 0 && r >= 0)
r = q;
not_first = true;
}
} else
STRV_FOREACH(i, strv_skip(argv, 1)) {
int q;
if (i > argv+1)
printf("\n");
if (argv[2]) {
pager_open(arg_pager_flags);
printf("Service %s%s%s:\n", ansi_highlight(), *i, ansi_normal());
}
q = tree_one(bus, *i);
if (q < 0 && r >= 0)
r = q;
}
return r;
}
static int format_cmdline(sd_bus_message *m, FILE *f, bool needs_space) {
int r;
for (;;) {
const char *contents = NULL;
char type;
union {
uint8_t u8;
uint16_t u16;
int16_t s16;
uint32_t u32;
int32_t s32;
uint64_t u64;
int64_t s64;
double d64;
const char *string;
int i;
} basic;
r = sd_bus_message_peek_type(m, &type, &contents);
if (r < 0)
return r;
if (r == 0)
return needs_space;
if (bus_type_is_container(type) > 0) {
r = sd_bus_message_enter_container(m, type, contents);
if (r < 0)
return r;
if (type == SD_BUS_TYPE_ARRAY) {
unsigned n = 0;
/* count array entries */
for (;;) {
r = sd_bus_message_skip(m, contents);
if (r < 0)
return r;
if (r == 0)
break;
n++;
}
r = sd_bus_message_rewind(m, false);
if (r < 0)
return r;
if (needs_space)
fputc(' ', f);
fprintf(f, "%u", n);
needs_space = true;
} else if (type == SD_BUS_TYPE_VARIANT) {
if (needs_space)
fputc(' ', f);
fprintf(f, "%s", contents);
needs_space = true;
}
r = format_cmdline(m, f, needs_space);
if (r < 0)
return r;
needs_space = r > 0;
r = sd_bus_message_exit_container(m);
if (r < 0)
return r;
continue;
}
r = sd_bus_message_read_basic(m, type, &basic);
if (r < 0)
return r;
if (needs_space)
fputc(' ', f);
switch (type) {
case SD_BUS_TYPE_BYTE:
fprintf(f, "%u", basic.u8);
break;
case SD_BUS_TYPE_BOOLEAN:
fputs(true_false(basic.i), f);
break;
case SD_BUS_TYPE_INT16:
fprintf(f, "%i", basic.s16);
break;
case SD_BUS_TYPE_UINT16:
fprintf(f, "%u", basic.u16);
break;
case SD_BUS_TYPE_INT32:
fprintf(f, "%i", basic.s32);
break;
case SD_BUS_TYPE_UINT32:
fprintf(f, "%u", basic.u32);
break;
case SD_BUS_TYPE_INT64:
fprintf(f, "%" PRIi64, basic.s64);
break;
case SD_BUS_TYPE_UINT64:
fprintf(f, "%" PRIu64, basic.u64);
break;
case SD_BUS_TYPE_DOUBLE:
fprintf(f, "%g", basic.d64);
break;
case SD_BUS_TYPE_STRING:
case SD_BUS_TYPE_OBJECT_PATH:
case SD_BUS_TYPE_SIGNATURE: {
_cleanup_free_ char *b = NULL;
b = cescape(basic.string);
if (!b)
return -ENOMEM;
fprintf(f, "\"%s\"", b);
break;
}
case SD_BUS_TYPE_UNIX_FD:
fprintf(f, "%i", basic.i);
break;
default:
assert_not_reached();
}
needs_space = true;
}
}
typedef struct Member {
const char *type;
char *interface;
char *name;
char *signature;
char *result;
char *value;
bool writable;
uint64_t flags;
} Member;
static void member_hash_func(const Member *m, struct siphash *state) {
uint64_t arity = 1;
assert(m);
assert(m->type);
string_hash_func(m->type, state);
arity += !!m->name + !!m->interface;
uint64_hash_func(&arity, state);
if (m->name)
string_hash_func(m->name, state);
if (m->interface)
string_hash_func(m->interface, state);
}
static int member_compare_func(const Member *x, const Member *y) {
int d;
assert(x);
assert(y);
assert(x->type);
assert(y->type);
d = strcmp_ptr(x->interface, y->interface);
if (d != 0)
return d;
d = strcmp(x->type, y->type);
if (d != 0)
return d;
return strcmp_ptr(x->name, y->name);
}
static int member_compare_funcp(Member * const *a, Member * const *b) {
return member_compare_func(*a, *b);
}
static Member* member_free(Member *m) {
if (!m)
return NULL;
free(m->interface);
free(m->name);
free(m->signature);
free(m->result);
free(m->value);
return mfree(m);
}
DEFINE_TRIVIAL_CLEANUP_FUNC(Member*, member_free);
static Set* member_set_free(Set *s) {
return set_free_with_destructor(s, member_free);
}
DEFINE_TRIVIAL_CLEANUP_FUNC(Set*, member_set_free);
static int on_interface(const char *interface, uint64_t flags, void *userdata) {
_cleanup_(member_freep) Member *m = NULL;
Set *members = userdata;
int r;
assert(interface);
assert(members);
m = new(Member, 1);
if (!m)
return log_oom();
*m = (Member) {
.type = "interface",
.flags = flags,
};
r = free_and_strdup(&m->interface, interface);
if (r < 0)
return log_oom();
r = set_put(members, m);
if (r == -EEXIST)
return log_error_errno(r, "Invalid introspection data: duplicate interface '%s'.", interface);
if (r < 0)
return log_oom();
m = NULL;
return 0;
}
static int on_method(const char *interface, const char *name, const char *signature, const char *result, uint64_t flags, void *userdata) {
_cleanup_(member_freep) Member *m = NULL;
Set *members = userdata;
int r;
assert(interface);
assert(name);
m = new(Member, 1);
if (!m)
return log_oom();
*m = (Member) {
.type = "method",
.flags = flags,
};
r = free_and_strdup(&m->interface, interface);
if (r < 0)
return log_oom();
r = free_and_strdup(&m->name, name);
if (r < 0)
return log_oom();
r = free_and_strdup(&m->signature, signature);
if (r < 0)
return log_oom();
r = free_and_strdup(&m->result, result);
if (r < 0)
return log_oom();
r = set_put(members, m);
if (r == -EEXIST)
return log_error_errno(r, "Invalid introspection data: duplicate method '%s' on interface '%s'.", name, interface);
if (r < 0)
return log_oom();
m = NULL;
return 0;
}
static int on_signal(const char *interface, const char *name, const char *signature, uint64_t flags, void *userdata) {
_cleanup_(member_freep) Member *m = NULL;
Set *members = userdata;
int r;
assert(interface);
assert(name);
m = new(Member, 1);
if (!m)
return log_oom();
*m = (Member) {
.type = "signal",
.flags = flags,
};
r = free_and_strdup(&m->interface, interface);
if (r < 0)
return log_oom();
r = free_and_strdup(&m->name, name);
if (r < 0)
return log_oom();
r = free_and_strdup(&m->signature, signature);
if (r < 0)
return log_oom();
r = set_put(members, m);
if (r == -EEXIST)
return log_error_errno(r, "Invalid introspection data: duplicate signal '%s' on interface '%s'.", name, interface);
if (r < 0)
return log_oom();
m = NULL;
return 0;
}
static int on_property(const char *interface, const char *name, const char *signature, bool writable, uint64_t flags, void *userdata) {
_cleanup_(member_freep) Member *m = NULL;
Set *members = userdata;
int r;
assert(interface);
assert(name);
m = new(Member, 1);
if (!m)
return log_oom();
*m = (Member) {
.type = "property",
.flags = flags,
.writable = writable,
};
r = free_and_strdup(&m->interface, interface);
if (r < 0)
return log_oom();
r = free_and_strdup(&m->name, name);
if (r < 0)
return log_oom();
r = free_and_strdup(&m->signature, signature);
if (r < 0)
return log_oom();
r = set_put(members, m);
if (r == -EEXIST)
return log_error_errno(r, "Invalid introspection data: duplicate property '%s' on interface '%s'.", name, interface);
if (r < 0)
return log_oom();
m = NULL;
return 0;
}
DEFINE_PRIVATE_HASH_OPS(member_hash_ops, Member, member_hash_func, member_compare_func);
static int introspect(int argc, char **argv, void *userdata) {
static const XMLIntrospectOps ops = {
.on_interface = on_interface,
.on_method = on_method,
.on_signal = on_signal,
.on_property = on_property,
};
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply_xml = NULL;
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(member_set_freep) Set *members = NULL;
unsigned name_width, type_width, signature_width, result_width, j, k = 0;
Member *m, **sorted = NULL;
const char *xml;
int r;
r = acquire_bus(false, &bus);
if (r < 0)
return r;
members = set_new(&member_hash_ops);
if (!members)
return log_oom();
r = sd_bus_call_method(bus, argv[1], argv[2],
"org.freedesktop.DBus.Introspectable", "Introspect",
&error, &reply_xml, NULL);
if (r < 0)
return log_error_errno(r, "Failed to introspect object %s of service %s: %s",
argv[2], argv[1], bus_error_message(&error, r));
r = sd_bus_message_read(reply_xml, "s", &xml);
if (r < 0)
return bus_log_parse_error(r);
if (arg_xml_interface) {
/* Just dump the received XML and finish */
pager_open(arg_pager_flags);
puts(xml);
return 0;
}
/* First, get list of all properties */
r = parse_xml_introspect(argv[2], xml, &ops, members);
if (r < 0)
return r;
/* Second, find the current values for them */
SET_FOREACH(m, members) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
if (!streq(m->type, "property"))
continue;
if (m->value)