• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 23172097450

16 Mar 2026 07:45PM UTC coverage: 72.568% (+0.06%) from 72.513%
23172097450

push

github

keszybz
add-ug-bo-translation

316055 of 435531 relevant lines covered (72.57%)

1182407.5 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.38
/src/busctl/busctl.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <getopt.h>
4
#include <unistd.h>
5

6
#include "sd-bus.h"
7
#include "sd-daemon.h"
8
#include "sd-event.h"
9
#include "sd-json.h"
10

11
#include "alloc-util.h"
12
#include "bitfield.h"
13
#include "build.h"
14
#include "bus-dump.h"
15
#include "bus-error.h"
16
#include "bus-internal.h"
17
#include "bus-signature.h"
18
#include "bus-type.h"
19
#include "bus-util.h"
20
#include "busctl-introspect.h"
21
#include "capsule-util.h"
22
#include "errno-util.h"
23
#include "escape.h"
24
#include "fd-util.h"
25
#include "fdset.h"
26
#include "fileio.h"
27
#include "format-table.h"
28
#include "glyph-util.h"
29
#include "log.h"
30
#include "logarithm.h"
31
#include "main-func.h"
32
#include "memstream-util.h"
33
#include "os-util.h"
34
#include "pager.h"
35
#include "parse-argument.h"
36
#include "parse-util.h"
37
#include "path-util.h"
38
#include "pretty-print.h"
39
#include "runtime-scope.h"
40
#include "set.h"
41
#include "string-util.h"
42
#include "strv.h"
43
#include "terminal-util.h"
44
#include "time-util.h"
45
#include "user-util.h"
46
#include "verbs.h"
47
#include "version.h"
48

49
static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF;
50
static PagerFlags arg_pager_flags = 0;
51
static bool arg_legend = true;
52
static int arg_full = -1;
53
static const char *arg_address = NULL;
54
static bool arg_unique = false;
55
static bool arg_acquired = false;
56
static bool arg_activatable = false;
57
static bool arg_show_machine = false;
58
static char **arg_matches = NULL;
59
static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
60
static const char *arg_host = NULL;
61
static RuntimeScope arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
62
static size_t arg_snaplen = 4096;
63
static bool arg_list = false;
64
static bool arg_quiet = false;
65
static bool arg_verbose = false;
66
static bool arg_xml_interface = false;
67
static bool arg_expect_reply = true;
68
static bool arg_auto_start = true;
69
static bool arg_allow_interactive_authorization = true;
70
static bool arg_augment_creds = true;
71
static bool arg_watch_bind = false;
72
static usec_t arg_timeout = 0;
73
static const char *arg_destination = NULL;
74
static uint64_t arg_limit_messages = UINT64_MAX;
75
static uint64_t arg_limit_signals = 1;
76

77
STATIC_DESTRUCTOR_REGISTER(arg_matches, strv_freep);
1,189✔
78

79
#define NAME_IS_ACQUIRED INT_TO_PTR(1)
80
#define NAME_IS_ACTIVATABLE INT_TO_PTR(2)
81

82
static int acquire_bus(bool set_monitor, sd_bus **ret) {
1,182✔
83
        _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL;
1,182✔
84
        _cleanup_close_ int pin_fd = -EBADF;
1,182✔
85
        int r;
1,182✔
86

87
        r = sd_bus_new(&bus);
1,182✔
88
        if (r < 0)
1,182✔
89
                return log_error_errno(r, "Failed to allocate bus: %m");
×
90

91
        (void) sd_bus_set_description(bus, "busctl");
1,182✔
92

93
        if (set_monitor) {
1,182✔
94
                r = sd_bus_set_monitor(bus, true);
2✔
95
                if (r < 0)
2✔
96
                        return log_error_errno(r, "Failed to set monitor mode: %m");
×
97

98
                r = sd_bus_negotiate_creds(bus, true, _SD_BUS_CREDS_ALL);
2✔
99
                if (r < 0)
2✔
100
                        return log_error_errno(r, "Failed to enable credentials: %m");
×
101

102
                r = sd_bus_negotiate_timestamp(bus, true);
2✔
103
                if (r < 0)
2✔
104
                        return log_error_errno(r, "Failed to enable timestamps: %m");
×
105

106
                r = sd_bus_negotiate_fds(bus, true);
2✔
107
                if (r < 0)
2✔
108
                        return log_error_errno(r, "Failed to enable fds: %m");
×
109
        }
110

111
        r = sd_bus_set_bus_client(bus, true);
1,182✔
112
        if (r < 0)
1,182✔
113
                return log_error_errno(r, "Failed to set bus client: %m");
×
114

115
        r = sd_bus_set_watch_bind(bus, arg_watch_bind);
1,182✔
116
        if (r < 0)
1,182✔
117
                return log_error_errno(r, "Failed to set watch-bind setting to '%s': %m",
×
118
                                       yes_no(arg_watch_bind));
119

120
        if (arg_address)
1,182✔
121
                r = sd_bus_set_address(bus, arg_address);
×
122
        else
123
                switch (arg_transport) {
1,182✔
124

125
                case BUS_TRANSPORT_LOCAL:
1,179✔
126

127
                        switch (arg_runtime_scope) {
1,179✔
128

129
                        case RUNTIME_SCOPE_USER:
4✔
130
                                r = bus_set_address_user(bus);
4✔
131
                                break;
132

133
                        case RUNTIME_SCOPE_SYSTEM:
1,175✔
134
                                r = bus_set_address_system(bus);
1,175✔
135
                                break;
136

137
                        default:
×
138
                                assert_not_reached();
×
139
                        }
140

141
                        break;
142

143
                case BUS_TRANSPORT_REMOTE:
×
144
                        r = bus_set_address_system_remote(bus, arg_host);
×
145
                        break;
146

147
                case BUS_TRANSPORT_MACHINE:
2✔
148
                        r = bus_set_address_machine(bus, arg_runtime_scope, arg_host);
2✔
149
                        break;
150

151
                case BUS_TRANSPORT_CAPSULE:
1✔
152
                        r = bus_set_address_capsule_bus(bus, arg_host, &pin_fd);
1✔
153
                        break;
154

155
                default:
×
156
                        assert_not_reached();
×
157
                }
158
        if (r < 0)
1,182✔
159
                return bus_log_address_error(r, arg_transport);
×
160

161
        r = sd_bus_start(bus);
1,182✔
162
        if (r < 0)
1,182✔
163
                return bus_log_connect_error(r, arg_transport, arg_runtime_scope);
×
164

165
        *ret = TAKE_PTR(bus);
1,182✔
166

167
        return 0;
1,182✔
168
}
169

170
static void notify_bus_error(const sd_bus_error *error) {
1,019✔
171

172
        if (!sd_bus_error_is_set(error))
1,019✔
173
                return;
174

175
        (void) sd_notifyf(/* unset_environment= */ false, "BUSERROR=%s", error->name);
1,019✔
176
}
177

178
static int list_bus_names(int argc, char **argv, void *userdata) {
6✔
179
        _cleanup_strv_free_ char **acquired = NULL, **activatable = NULL;
6✔
180
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
6✔
181
        _cleanup_hashmap_free_ Hashmap *names = NULL;
6✔
182
        _cleanup_(table_unrefp) Table *table = NULL;
6✔
183
        char *k;
6✔
184
        void *v;
6✔
185
        int r;
6✔
186

187
        enum {
6✔
188
                COLUMN_ACTIVATABLE,
189
                COLUMN_NAME,
190
                COLUMN_PID,
191
                COLUMN_PROCESS,
192
                COLUMN_USER,
193
                COLUMN_CONNECTION,
194
                COLUMN_UNIT,
195
                COLUMN_SESSION,
196
                COLUMN_DESCRIPTION,
197
                COLUMN_MACHINE,
198
        };
199

200
        if (!arg_unique && !arg_acquired && !arg_activatable)
6✔
201
                arg_unique = arg_acquired = arg_activatable = true;
4✔
202

203
        r = acquire_bus(false, &bus);
6✔
204
        if (r < 0)
6✔
205
                return r;
206

207
        r = sd_bus_list_names(bus,
6✔
208
                              (arg_acquired || arg_unique) ? &acquired : NULL,
6✔
209
                              arg_activatable ? &activatable : NULL);
6✔
210
        if (r < 0)
6✔
211
                return log_error_errno(r, "Failed to list names: %m");
×
212

213
        names = hashmap_new(&string_hash_ops);
6✔
214
        if (!names)
6✔
215
                return log_oom();
×
216

217
        STRV_FOREACH(i, acquired) {
78✔
218
                r = hashmap_put(names, *i, NAME_IS_ACQUIRED);
72✔
219
                if (r < 0)
72✔
220
                        return log_error_errno(r, "Failed to add to hashmap: %m");
×
221
        }
222

223
        STRV_FOREACH(i, activatable) {
75✔
224
                r = hashmap_put(names, *i, NAME_IS_ACTIVATABLE);
69✔
225
                if (r < 0 && r != -EEXIST)
69✔
226
                        return log_error_errno(r, "Failed to add to hashmap: %m");
×
227
        }
228

229
        table = table_new("activatable",
6✔
230
                          "name",
231
                          "pid",
232
                          "process",
233
                          "user",
234
                          "connection",
235
                          "unit",
236
                          "session",
237
                          "description",
238
                          "machine");
239
        if (!table)
6✔
240
                return log_oom();
×
241

242
        if (arg_full > 0)
6✔
243
                table_set_width(table, 0);
6✔
244

245
        r = table_set_align_percent(table, table_get_cell(table, 0, COLUMN_PID), 100);
6✔
246
        if (r < 0)
6✔
247
                return log_error_errno(r, "Failed to set alignment: %m");
×
248

249
        table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
6✔
250

251
        r = table_set_sort(table, (size_t) COLUMN_NAME);
6✔
252
        if (r < 0)
6✔
253
                return log_error_errno(r, "Failed to set sort column: %m");
×
254

255
        if (arg_show_machine)
6✔
256
                r = table_set_display(table, (size_t) COLUMN_NAME,
1✔
257
                                             (size_t) COLUMN_PID,
258
                                             (size_t) COLUMN_PROCESS,
259
                                             (size_t) COLUMN_USER,
260
                                             (size_t) COLUMN_CONNECTION,
261
                                             (size_t) COLUMN_UNIT,
262
                                             (size_t) COLUMN_SESSION,
263
                                             (size_t) COLUMN_DESCRIPTION,
264
                                             (size_t) COLUMN_MACHINE);
265
        else
266
                r = table_set_display(table, (size_t) COLUMN_NAME,
5✔
267
                                             (size_t) COLUMN_PID,
268
                                             (size_t) COLUMN_PROCESS,
269
                                             (size_t) COLUMN_USER,
270
                                             (size_t) COLUMN_CONNECTION,
271
                                             (size_t) COLUMN_UNIT,
272
                                             (size_t) COLUMN_SESSION,
273
                                             (size_t) COLUMN_DESCRIPTION);
274

275
        if (r < 0)
6✔
276
                return log_error_errno(r, "Failed to set columns to display: %m");
×
277

278
        HASHMAP_FOREACH_KEY(v, k, names) {
122✔
279
                _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
116✔
280

281
                if (v == NAME_IS_ACTIVATABLE) {
116✔
282
                        r = table_add_many(
44✔
283
                                        table,
284
                                        TABLE_INT, PTR_TO_INT(v),
285
                                        TABLE_STRING, k,
286
                                        TABLE_EMPTY,
287
                                        TABLE_EMPTY,
288
                                        TABLE_EMPTY,
289
                                        TABLE_STRING, "(activatable)", TABLE_SET_COLOR, ansi_grey(),
290
                                        TABLE_EMPTY,
291
                                        TABLE_EMPTY,
292
                                        TABLE_EMPTY,
293
                                        TABLE_EMPTY);
294
                        if (r < 0)
44✔
295
                                return table_log_add_error(r);
×
296

297
                        continue;
44✔
298
                }
299

300
                assert(v == NAME_IS_ACQUIRED);
72✔
301

302
                if (!arg_unique && k[0] == ':')
72✔
303
                        continue;
8✔
304

305
                if (!arg_acquired && k[0] != ':')
64✔
306
                        continue;
6✔
307

308
                r = table_add_many(table,
58✔
309
                                   TABLE_INT, PTR_TO_INT(v),
310
                                   TABLE_STRING, k);
311
                if (r < 0)
58✔
312
                        return table_log_add_error(r);
×
313

314
                r = sd_bus_get_name_creds(
58✔
315
                                bus, k,
316
                                (arg_augment_creds ? SD_BUS_CREDS_AUGMENT : 0) |
58✔
317
                                SD_BUS_CREDS_EUID|SD_BUS_CREDS_PID|SD_BUS_CREDS_COMM|
318
                                SD_BUS_CREDS_UNIQUE_NAME|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_SESSION|
319
                                SD_BUS_CREDS_DESCRIPTION, &creds);
320
                if (r < 0) {
58✔
321
                        log_debug_errno(r, "Failed to acquire credentials of service %s, ignoring: %m", k);
×
322

323
                        r = table_fill_empty(table, COLUMN_MACHINE);
×
324
                } else {
325
                        const char *unique = NULL, *session = NULL, *unit = NULL, *cn = NULL;
58✔
326
                        pid_t pid;
58✔
327
                        uid_t uid;
58✔
328

329
                        r = sd_bus_creds_get_pid(creds, &pid);
58✔
330
                        if (r >= 0) {
58✔
331
                                const char *comm = NULL;
58✔
332

333
                                (void) sd_bus_creds_get_comm(creds, &comm);
58✔
334

335
                                r = table_add_many(table,
59✔
336
                                                   TABLE_PID, pid,
337
                                                   TABLE_STRING, strna(comm));
338
                        } else
339
                                r = table_add_many(table, TABLE_EMPTY, TABLE_EMPTY);
×
340
                        if (r < 0)
58✔
341
                                return table_log_add_error(r);
×
342

343
                        r = sd_bus_creds_get_euid(creds, &uid);
58✔
344
                        if (r >= 0) {
58✔
345
                                _cleanup_free_ char *u = NULL;
58✔
346

347
                                u = uid_to_name(uid);
58✔
348
                                if (!u)
58✔
349
                                        return log_oom();
×
350

351
                                r = table_add_cell(table, NULL, TABLE_STRING, u);
58✔
352
                        } else
353
                                r = table_add_cell(table, NULL, TABLE_EMPTY, NULL);
×
354
                        if (r < 0)
58✔
355
                                return table_log_add_error(r);
×
356

357
                        (void) sd_bus_creds_get_unique_name(creds, &unique);
58✔
358
                        (void) sd_bus_creds_get_unit(creds, &unit);
58✔
359
                        (void) sd_bus_creds_get_session(creds, &session);
58✔
360
                        (void) sd_bus_creds_get_description(creds, &cn);
58✔
361

362
                        r = table_add_many(
58✔
363
                                        table,
364
                                        TABLE_STRING, unique,
365
                                        TABLE_STRING, unit,
366
                                        TABLE_STRING, session,
367
                                        TABLE_STRING, cn);
368
                }
369
                if (r < 0)
58✔
370
                        return table_log_add_error(r);
×
371

372
                if (arg_show_machine) {
58✔
373
                        sd_id128_t mid;
8✔
374

375
                        r = sd_bus_get_name_machine_id(bus, k, &mid);
8✔
376
                        if (r < 0)
8✔
377
                                log_debug_errno(r, "Failed to acquire credentials of service %s, ignoring: %m", k);
2✔
378
                        else {
379
                                r = table_add_cell(table, NULL, TABLE_ID128, &mid);
6✔
380
                                if (r < 0)
6✔
381
                                        return table_log_add_error(r);
×
382

383
                                continue; /* line fully filled, no need to fill the remainder below */
6✔
384
                        }
385
                }
386

387
                r = table_fill_empty(table, 0);
52✔
388
                if (r < 0)
52✔
389
                        return log_error_errno(r, "Failed to fill line: %m");
×
390
        }
391

392
        return table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
6✔
393
}
394

395
static void print_subtree(const char *prefix, const char *path, char **l) {
377✔
396
        /* We assume the list is sorted. Let's first skip over the
397
         * entry we are looking at. */
398
        for (;;) {
1,131✔
399
                if (!*l)
754✔
400
                        return;
401

402
                if (!streq(*l, path))
737✔
403
                        break;
404

405
                l++;
377✔
406
        }
407

408
        const char
360✔
409
                *vertical = strjoina(prefix, glyph(GLYPH_TREE_VERTICAL)),
1,800✔
410
                *space = strjoina(prefix, glyph(GLYPH_TREE_SPACE));
1,800✔
411

412
        for (;;) {
360✔
413
                bool has_more = false;
720✔
414
                char **n;
720✔
415

416
                if (!*l || !path_startswith(*l, path))
720✔
417
                        break;
418

419
                n = l + 1;
360✔
420
                for (;;) {
2,832✔
421
                        if (!*n || !path_startswith(*n, path))
1,596✔
422
                                break;
423

424
                        if (!path_startswith(*n, *l)) {
1,515✔
425
                                has_more = true;
426
                                break;
427
                        }
428

429
                        n++;
1,236✔
430
                }
431

432
                printf("%s%s %s\n",
720✔
433
                       prefix,
434
                       glyph(has_more ? GLYPH_TREE_BRANCH : GLYPH_TREE_RIGHT),
435
                       *l);
436

437
                print_subtree(has_more ? vertical : space, *l, l);
360✔
438
                l = n;
360✔
439
        }
440
}
441

442
static void print_tree(char **l) {
19✔
443
        if (arg_list)
19✔
444
                strv_print(l);
1✔
445
        else if (strv_isempty(l))
18✔
446
                printf("No objects discovered.\n");
1✔
447
        else if (streq(l[0], "/") && !l[1])
17✔
448
                printf("Only root object discovered.\n");
×
449
        else
450
                print_subtree("", "/", l);
17✔
451
}
19✔
452

453
static int on_path(const char *path, void *userdata) {
758✔
454
        Set *paths = ASSERT_PTR(userdata);
758✔
455
        int r;
758✔
456

457
        r = set_put_strdup(&paths, path);
758✔
458
        if (r < 0)
758✔
459
                return log_oom();
×
460

461
        return 0;
462
}
463

464
static int find_nodes(sd_bus *bus, const char *service, const char *path, Set *paths) {
389✔
465
        static const XMLIntrospectOps ops = {
389✔
466
                .on_path = on_path,
467
        };
468

469
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
389✔
470
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
389✔
471
        const char *xml;
389✔
472
        int r;
389✔
473

474
        r = sd_bus_call_method(bus, service, path,
389✔
475
                               "org.freedesktop.DBus.Introspectable", "Introspect",
476
                               &error, &reply, NULL);
477
        if (r < 0) {
389✔
478
                notify_bus_error(&error);
1✔
479
                printf("%sFailed to introspect object %s of service %s: %s%s\n",
2✔
480
                       ansi_highlight_red(),
481
                       path, service, bus_error_message(&error, r),
1✔
482
                       ansi_normal());
483
                return r;
1✔
484
        }
485

486
        r = sd_bus_message_read(reply, "s", &xml);
388✔
487
        if (r < 0)
388✔
488
                return bus_log_parse_error(r);
×
489

490
        return parse_xml_introspect(path, xml, &ops, paths);
388✔
491
}
492

493
static int tree_one(sd_bus *bus, const char *service) {
19✔
494
        _cleanup_set_free_ Set *paths = NULL, *done = NULL, *failed = NULL;
19✔
495
        _cleanup_free_ char **l = NULL;
19✔
496
        int r;
19✔
497

498
        r = set_put_strdup(&paths, "/");
19✔
499
        if (r < 0)
19✔
500
                return log_oom();
×
501

502
        for (;;) {
796✔
503
                _cleanup_free_ char *p = NULL;
388✔
504
                int q;
796✔
505

506
                p = set_steal_first(paths);
796✔
507
                if (!p)
796✔
508
                        break;
509

510
                if (set_contains(done, p) ||
777✔
511
                    set_contains(failed, p))
389✔
512
                        continue;
388✔
513

514
                q = find_nodes(bus, service, p, paths);
389✔
515
                if (q < 0 && r >= 0)
389✔
516
                        r = q;
517

518
                q = set_ensure_consume(q < 0 ? &failed : &done, &string_hash_ops_free, TAKE_PTR(p));
777✔
519
                assert(q != 0);
389✔
520
                if (q < 0)
389✔
521
                        return log_oom();
×
522
        }
523

524
        pager_open(arg_pager_flags);
19✔
525

526
        l = set_get_strv(done);
19✔
527
        if (!l)
19✔
528
                return log_oom();
×
529

530
        strv_sort(l);
19✔
531
        print_tree(l);
19✔
532

533
        fflush(stdout);
19✔
534

535
        return r;
536
}
537

538
static int tree(int argc, char **argv, void *userdata) {
14✔
539
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
14✔
540
        int r;
14✔
541

542
        /* Do superficial verification of arguments before even opening the bus */
543
        STRV_FOREACH(i, strv_skip(argv, 1))
27✔
544
                if (!sd_bus_service_name_is_valid(*i))
13✔
545
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
546
                                               "Invalid bus service name: %s", *i);
547

548
        if (!arg_unique && !arg_acquired)
14✔
549
                arg_acquired = true;
14✔
550

551
        r = acquire_bus(false, &bus);
14✔
552
        if (r < 0)
14✔
553
                return r;
554

555
        char **args = strv_skip(argv, 1);
14✔
556
        if (args)
14✔
557
                STRV_FOREACH(arg, args) {
26✔
558
                        if (arg != args)
13✔
559
                                puts("");
×
560

561
                        if (args[1]) {
13✔
562
                                pager_open(arg_pager_flags);
×
563
                                printf("Service %s%s%s:\n", ansi_highlight(), *arg, ansi_normal());
×
564
                        }
565

566
                        RET_GATHER(r, tree_one(bus, *arg));
13✔
567
                }
568
        else {
569
                _cleanup_strv_free_ char **names = NULL;
1✔
570

571
                r = sd_bus_list_names(bus, &names, NULL);
1✔
572
                if (r < 0)
1✔
573
                        return log_error_errno(r, "Failed to get name list: %m");
×
574

575
                pager_open(arg_pager_flags);
1✔
576

577
                STRV_FOREACH(name, names) {
15✔
578
                        if (!arg_unique && (*name)[0] == ':')
14✔
579
                                continue;
8✔
580

581
                        if (!arg_acquired && (*name)[0] == ':')
6✔
582
                                continue;
×
583

584
                        if (name != names)
6✔
585
                                puts("");
5✔
586

587
                        printf("Service %s%s%s:\n", ansi_highlight(), *name, ansi_normal());
18✔
588

589
                        RET_GATHER(r, tree_one(bus, *name));
6✔
590
                }
591
        }
592

593
        return r;
594
}
595

596
static int format_cmdline(sd_bus_message *m, FILE *f, bool needs_space) {
374✔
597
        int r;
826✔
598

599
        for (;;) {
826✔
600
                const char *contents = NULL;
826✔
601
                char type;
826✔
602
                union {
826✔
603
                        uint8_t u8;
604
                        uint16_t u16;
605
                        int16_t s16;
606
                        uint32_t u32;
607
                        int32_t s32;
608
                        uint64_t u64;
609
                        int64_t s64;
610
                        double d64;
611
                        const char *string;
612
                        int i;
613
                } basic;
614

615
                r = sd_bus_message_peek_type(m, &type, &contents);
826✔
616
                if (r < 0)
826✔
617
                        return r;
374✔
618
                if (r == 0)
826✔
619
                        return needs_space;
374✔
620

621
                if (bus_type_is_container(type) > 0) {
452✔
622

623
                        r = sd_bus_message_enter_container(m, type, contents);
14✔
624
                        if (r < 0)
14✔
625
                                return r;
626

627
                        if (type == SD_BUS_TYPE_ARRAY) {
14✔
628
                                unsigned n = 0;
629

630
                                /* count array entries */
631
                                for (;;) {
103✔
632

633
                                        r = sd_bus_message_skip(m, contents);
57✔
634
                                        if (r < 0)
57✔
635
                                                return r;
636
                                        if (r == 0)
57✔
637
                                                break;
638

639
                                        n++;
46✔
640
                                }
641

642
                                r = sd_bus_message_rewind(m, false);
11✔
643
                                if (r < 0)
11✔
644
                                        return r;
645

646
                                if (needs_space)
11✔
647
                                        fputc(' ', f);
1✔
648

649
                                fprintf(f, "%u", n);
11✔
650
                                needs_space = true;
651

652
                        } else if (type == SD_BUS_TYPE_VARIANT) {
3✔
653

654
                                if (needs_space)
×
655
                                        fputc(' ', f);
×
656

657
                                fprintf(f, "%s", contents);
×
658
                                needs_space = true;
659
                        }
660

661
                        r = format_cmdline(m, f, needs_space);
14✔
662
                        if (r < 0)
14✔
663
                                return r;
664

665
                        needs_space = r > 0;
14✔
666

667
                        r = sd_bus_message_exit_container(m);
14✔
668
                        if (r < 0)
14✔
669
                                return r;
670

671
                        continue;
14✔
672
                }
673

674
                r = sd_bus_message_read_basic(m, type, &basic);
438✔
675
                if (r < 0)
438✔
676
                        return r;
677

678
                if (needs_space)
438✔
679
                        fputc(' ', f);
88✔
680

681
                switch (type) {
438✔
682
                case SD_BUS_TYPE_BYTE:
6✔
683
                        fprintf(f, "%u", basic.u8);
6✔
684
                        break;
685

686
                case SD_BUS_TYPE_BOOLEAN:
21✔
687
                        fputs(true_false(basic.i), f);
34✔
688
                        break;
689

690
                case SD_BUS_TYPE_INT16:
×
691
                        fprintf(f, "%i", basic.s16);
×
692
                        break;
693

694
                case SD_BUS_TYPE_UINT16:
×
695
                        fprintf(f, "%u", basic.u16);
×
696
                        break;
697

698
                case SD_BUS_TYPE_INT32:
41✔
699
                        fprintf(f, "%i", basic.s32);
41✔
700
                        break;
701

702
                case SD_BUS_TYPE_UINT32:
15✔
703
                        fprintf(f, "%u", basic.u32);
15✔
704
                        break;
705

706
                case SD_BUS_TYPE_INT64:
×
707
                        fprintf(f, "%" PRIi64, basic.s64);
×
708
                        break;
709

710
                case SD_BUS_TYPE_UINT64:
177✔
711
                        fprintf(f, "%" PRIu64, basic.u64);
177✔
712
                        break;
713

714
                case SD_BUS_TYPE_DOUBLE:
2✔
715
                        fprintf(f, "%g", basic.d64);
2✔
716
                        break;
717

718
                case SD_BUS_TYPE_STRING:
176✔
719
                case SD_BUS_TYPE_OBJECT_PATH:
720
                case SD_BUS_TYPE_SIGNATURE: {
721
                        _cleanup_free_ char *b = NULL;
176✔
722

723
                        b = cescape(basic.string);
176✔
724
                        if (!b)
176✔
725
                                return -ENOMEM;
×
726

727
                        fprintf(f, "\"%s\"", b);
176✔
728
                        break;
176✔
729
                }
730

731
                case SD_BUS_TYPE_UNIX_FD:
×
732
                        fprintf(f, "%i", basic.i);
×
733
                        break;
734

735
                default:
×
736
                        assert_not_reached();
×
737
                }
738

739
                needs_space = true;
438✔
740
        }
741
}
742

743
typedef struct Member {
744
        const char *type;
745
        char *interface;
746
        char *name;
747
        char *signature;
748
        char *result;
749
        char *value;
750
        bool writable;
751
        uint64_t flags;
752
} Member;
753

754
static void member_hash_func(const Member *m, struct siphash *state) {
1,442✔
755
        uint64_t arity = 1;
1,442✔
756

757
        assert(m);
1,442✔
758
        assert(m->type);
1,442✔
759

760
        string_hash_func(m->type, state);
1,442✔
761

762
        arity += !!m->name + !!m->interface;
1,442✔
763

764
        uint64_hash_func(&arity, state);
1,442✔
765

766
        if (m->name)
1,442✔
767
                string_hash_func(m->name, state);
1,400✔
768

769
        if (m->signature)
1,442✔
770
                string_hash_func(m->signature, state);
1,306✔
771

772
        if (m->interface)
1,442✔
773
                string_hash_func(m->interface, state);
1,442✔
774
}
1,442✔
775

776
static int member_compare_func(const Member *x, const Member *y) {
3,671✔
777
        int d;
3,671✔
778

779
        assert(x);
3,671✔
780
        assert(y);
3,671✔
781
        assert(x->type);
3,671✔
782
        assert(y->type);
3,671✔
783

784
        d = strcmp_ptr(x->interface, y->interface);
3,671✔
785
        if (d != 0)
3,671✔
786
                return d;
787

788
        d = strcmp(x->type, y->type);
3,496✔
789
        if (d != 0)
3,496✔
790
                return d;
791

792
        d = strcmp_ptr(x->name, y->name);
2,901✔
793
        if (d != 0)
2,901✔
794
                return d;
795

796
        return strcmp_ptr(x->signature, y->signature);
252✔
797
}
798

799
static Member* member_free(Member *m) {
466✔
800
        if (!m)
466✔
801
                return NULL;
802

803
        free(m->interface);
466✔
804
        free(m->name);
466✔
805
        free(m->signature);
466✔
806
        free(m->result);
466✔
807
        free(m->value);
466✔
808
        return mfree(m);
466✔
809
}
810
DEFINE_TRIVIAL_CLEANUP_FUNC(Member*, member_free);
466✔
811

812
static int on_interface(const char *interface, uint64_t flags, void *userdata) {
8✔
813
        _cleanup_(member_freep) Member *m = NULL;
8✔
814
        Set *members = ASSERT_PTR(userdata);
8✔
815
        int r;
8✔
816

817
        assert(interface);
8✔
818

819
        m = new(Member, 1);
8✔
820
        if (!m)
8✔
821
                return log_oom();
×
822

823
        *m = (Member) {
8✔
824
                .type = "interface",
825
                .flags = flags,
826
        };
827

828
        r = strdup_to(&m->interface, interface);
8✔
829
        if (r < 0)
8✔
830
                return log_oom();
×
831

832
        r = set_consume(members, TAKE_PTR(m));
8✔
833
        if (r < 0)
8✔
834
                return log_oom();
×
835
        if (r == 0)
8✔
836
                return log_error_errno(SYNTHETIC_ERRNO(EEXIST),
×
837
                                       "Invalid introspection data: duplicate interface '%s'.", interface);
838

839
        return 0;
840
}
841

842
static int on_method(const char *interface, const char *name, const char *signature, const char *result, uint64_t flags, void *userdata) {
190✔
843
        _cleanup_(member_freep) Member *m = NULL;
190✔
844
        Set *members = userdata;
190✔
845
        int r;
190✔
846

847
        assert(interface);
190✔
848
        assert(name);
190✔
849

850
        m = new(Member, 1);
190✔
851
        if (!m)
190✔
852
                return log_oom();
×
853

854
        *m = (Member) {
190✔
855
                .type = "method",
856
                .flags = flags,
857
        };
858

859
        r = strdup_to(&m->interface, interface);
190✔
860
        if (r < 0)
190✔
861
                return log_oom();
×
862

863
        r = strdup_to(&m->name, name);
190✔
864
        if (r < 0)
190✔
865
                return log_oom();
×
866

867
        r = strdup_to(&m->signature, signature);
190✔
868
        if (r < 0)
190✔
869
                return log_oom();
×
870

871
        r = strdup_to(&m->result, result);
190✔
872
        if (r < 0)
190✔
873
                return log_oom();
×
874

875
        r = set_consume(members, TAKE_PTR(m));
190✔
876
        if (r < 0)
190✔
877
                return log_oom();
×
878
        if (r == 0)
190✔
879
                return log_error_errno(SYNTHETIC_ERRNO(EEXIST),
×
880
                                       "Invalid introspection data: duplicate method '%s' on interface '%s'.", name, interface);
881

882
        return 0;
883
}
884

885
static int on_signal(const char *interface, const char *name, const char *signature, uint64_t flags, void *userdata) {
16✔
886
        _cleanup_(member_freep) Member *m = NULL;
16✔
887
        Set *members = userdata;
16✔
888
        int r;
16✔
889

890
        assert(interface);
16✔
891
        assert(name);
16✔
892

893
        m = new(Member, 1);
16✔
894
        if (!m)
16✔
895
                return log_oom();
×
896

897
        *m = (Member) {
16✔
898
                .type = "signal",
899
                .flags = flags,
900
        };
901

902
        r = strdup_to(&m->interface, interface);
16✔
903
        if (r < 0)
16✔
904
                return log_oom();
×
905

906
        r = strdup_to(&m->name, name);
16✔
907
        if (r < 0)
16✔
908
                return log_oom();
×
909

910
        r = strdup_to(&m->signature, signature);
16✔
911
        if (r < 0)
16✔
912
                return log_oom();
×
913

914
        r = set_consume(members, TAKE_PTR(m));
16✔
915
        if (r < 0)
16✔
916
                return log_oom();
×
917
        if (r == 0)
16✔
918
                return log_error_errno(SYNTHETIC_ERRNO(EEXIST),
×
919
                                       "Invalid introspection data: duplicate signal '%s' on interface '%s'.", name, interface);
920

921
        return 0;
922
}
923

924
static int on_property(const char *interface, const char *name, const char *signature, bool writable, uint64_t flags, void *userdata) {
252✔
925
        _cleanup_(member_freep) Member *m = NULL;
252✔
926
        Set *members = userdata;
252✔
927
        int r;
252✔
928

929
        assert(interface);
252✔
930
        assert(name);
252✔
931

932
        m = new(Member, 1);
252✔
933
        if (!m)
252✔
934
                return log_oom();
×
935

936
        *m = (Member) {
252✔
937
                .type = "property",
938
                .flags = flags,
939
                .writable = writable,
940
        };
941

942
        r = strdup_to(&m->interface, interface);
252✔
943
        if (r < 0)
252✔
944
                return log_oom();
×
945

946
        r = strdup_to(&m->name, name);
252✔
947
        if (r < 0)
252✔
948
                return log_oom();
×
949

950
        r = strdup_to(&m->signature, signature);
252✔
951
        if (r < 0)
252✔
952
                return log_oom();
×
953

954
        r = set_consume(members, TAKE_PTR(m));
252✔
955
        if (r < 0)
252✔
956
                return log_oom();
×
957
        if (r == 0)
252✔
958
                return log_error_errno(SYNTHETIC_ERRNO(EEXIST),
×
959
                                       "Invalid introspection data: duplicate property '%s' on interface '%s'.", name, interface);
960

961
        return 0;
962
}
963

964
DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(
466✔
965
                member_hash_ops,
966
                Member,
967
                member_hash_func,
968
                member_compare_func,
969
                member_free);
970

971
static int members_flags_to_string(const Member *m, char **ret) {
455✔
972
        static const char *map[] = {
455✔
973
                [LOG2U(SD_BUS_VTABLE_DEPRECATED)]                  = "deprecated",
974
                [LOG2U(SD_BUS_VTABLE_METHOD_NO_REPLY)]             = "no-reply",
975
                [LOG2U(SD_BUS_VTABLE_PROPERTY_CONST)]              = "const",
976
                [LOG2U(SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE)]       = "emits-change",
977
                [LOG2U(SD_BUS_VTABLE_PROPERTY_EMITS_INVALIDATION)] = "emits-invalidation",
978
        };
979

980
        assert(m);
455✔
981
        assert(ret);
455✔
982

983
        _cleanup_free_ char *str = NULL;
455✔
984
        for (size_t i = 0; i < ELEMENTSOF(map); i++)
3,640✔
985
                if (BIT_SET(m->flags, i) && map[i])
3,185✔
986
                        if (!strextend_with_separator(&str, " ", map[i]))
204✔
987
                                return -ENOMEM;
988

989
        if (m->writable)
455✔
990
                if (!strextend_with_separator(&str, " ", "writable"))
16✔
991
                        return -ENOMEM;
992

993
        *ret = TAKE_PTR(str);
455✔
994
        return 0;
455✔
995
}
996

997
static int introspect(int argc, char **argv, void *userdata) {
6✔
998
        static const XMLIntrospectOps ops = {
6✔
999
                .on_interface = on_interface,
1000
                .on_method = on_method,
1001
                .on_signal = on_signal,
1002
                .on_property = on_property,
1003
        };
1004

1005
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
6✔
1006
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply_xml = NULL;
6✔
1007
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
6✔
1008
        _cleanup_set_free_ Set *members = NULL;
6✔
1009
        Member *m;
6✔
1010
        const char *xml;
6✔
1011
        int r;
6✔
1012

1013
        r = acquire_bus(false, &bus);
6✔
1014
        if (r < 0)
6✔
1015
                return r;
1016

1017
        members = set_new(&member_hash_ops);
6✔
1018
        if (!members)
6✔
1019
                return log_oom();
×
1020

1021
        r = sd_bus_call_method(bus, argv[1], argv[2],
6✔
1022
                               "org.freedesktop.DBus.Introspectable", "Introspect",
1023
                               &error, &reply_xml, NULL);
1024
        if (r < 0) {
6✔
1025
                notify_bus_error(&error);
3✔
1026
                return log_error_errno(r, "Failed to introspect object %s of service %s: %s",
3✔
1027
                                       argv[2], argv[1], bus_error_message(&error, r));
1028
        }
1029

1030
        r = sd_bus_message_read(reply_xml, "s", &xml);
3✔
1031
        if (r < 0)
3✔
1032
                return bus_log_parse_error(r);
×
1033

1034
        if (arg_xml_interface) {
3✔
1035
                /* Just dump the received XML and finish */
1036
                pager_open(arg_pager_flags);
1✔
1037
                puts(xml);
1✔
1038
                return 0;
1039
        }
1040

1041
        /* First, get list of all properties */
1042
        r = parse_xml_introspect(argv[2], xml, &ops, members);
2✔
1043
        if (r < 0)
2✔
1044
                return r;
1045

1046
        /* Second, find the current values for them */
1047
        SET_FOREACH(m, members) {
468✔
1048
                _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
466✔
1049

1050
                if (!streq(m->type, "property"))
466✔
1051
                        continue;
214✔
1052

1053
                if (m->value)
252✔
1054
                        continue;
250✔
1055

1056
                if (argv[3] && !streq(argv[3], m->interface))
2✔
1057
                        continue;
×
1058

1059
                r = sd_bus_call_method(bus, argv[1], argv[2],
2✔
1060
                                       "org.freedesktop.DBus.Properties", "GetAll",
1061
                                       &error, &reply, "s", m->interface);
1062
                if (r < 0) {
2✔
1063
                        notify_bus_error(&error);
×
1064
                        return log_error_errno(r, "Failed to get all properties on interface %s: %s",
×
1065
                                               m->interface, bus_error_message(&error, r));
1066
                }
1067

1068
                r = sd_bus_message_enter_container(reply, 'a', "{sv}");
2✔
1069
                if (r < 0)
2✔
1070
                        return bus_log_parse_error(r);
×
1071

1072
                for (;;) {
252✔
1073
                        _cleanup_(memstream_done) MemStream ms = {};
×
1074
                        _cleanup_free_ char *buf = NULL;
254✔
1075
                        const char *name, *contents;
254✔
1076
                        Member *z;
254✔
1077
                        char type;
254✔
1078
                        FILE *mf;
254✔
1079

1080
                        r = sd_bus_message_enter_container(reply, 'e', "sv");
254✔
1081
                        if (r < 0)
254✔
1082
                                return bus_log_parse_error(r);
×
1083
                        if (r == 0)
254✔
1084
                                break;
1085

1086
                        r = sd_bus_message_read(reply, "s", &name);
252✔
1087
                        if (r < 0)
252✔
1088
                                return bus_log_parse_error(r);
×
1089

1090
                        r = sd_bus_message_peek_type(reply, &type, &contents);
252✔
1091
                        if (r < 0)
252✔
1092
                                return bus_log_parse_error(r);
×
1093
                        if (type != 'v')
252✔
1094
                                return bus_log_parse_error(EINVAL);
×
1095

1096
                        r = sd_bus_message_enter_container(reply, 'v', contents);
252✔
1097
                        if (r < 0)
252✔
1098
                                return bus_log_parse_error(r);
×
1099

1100
                        mf = memstream_init(&ms);
252✔
1101
                        if (!mf)
252✔
1102
                                return log_oom();
×
1103

1104
                        r = format_cmdline(reply, mf, false);
252✔
1105
                        if (r < 0)
252✔
1106
                                return bus_log_parse_error(r);
×
1107

1108
                        r = memstream_finalize(&ms, &buf, NULL);
252✔
1109
                        if (r < 0)
252✔
1110
                                return log_error_errno(r, "Failed to flush and close memstream: %m");
×
1111

1112
                        z = set_get(members, &((Member) {
504✔
1113
                                                .type = "property",
1114
                                                .interface = m->interface,
252✔
1115
                                                .signature = (char*) contents,
1116
                                                .name = (char*) name }));
1117
                        if (z)
252✔
1118
                                free_and_replace(z->value, buf);
252✔
1119

1120
                        r = sd_bus_message_exit_container(reply);
252✔
1121
                        if (r < 0)
252✔
1122
                                return bus_log_parse_error(r);
×
1123

1124
                        r = sd_bus_message_exit_container(reply);
252✔
1125
                        if (r < 0)
252✔
1126
                                return bus_log_parse_error(r);
×
1127
                }
1128

1129
                r = sd_bus_message_exit_container(reply);
2✔
1130
                if (r < 0)
2✔
1131
                        return bus_log_parse_error(r);
×
1132
        }
1133

1134
        size_t n;
2✔
1135
        _cleanup_free_ Member **sorted = NULL;
2✔
1136
        r = set_dump_sorted(members, (void***) &sorted, &n);
2✔
1137
        if (r < 0)
2✔
1138
                return log_oom();
×
1139

1140
        _cleanup_(table_unrefp) Table *table = table_new("name", "type", "signature", "result/value", "flags");
4✔
1141
        if (!table)
2✔
1142
                return log_oom();
×
1143

1144
        if (arg_full)
2✔
1145
                table_set_width(table, 0);
2✔
1146
        (void) table_set_maximum_width(table, table_get_cell(table, 0, 3), arg_full ? 100 : 40);
2✔
1147
        table_set_header(table, arg_legend);
2✔
1148
        table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
2✔
1149

1150
        FOREACH_ARRAY(p, sorted, n) {
468✔
1151
                bool is_interface;
466✔
1152

1153
                m = *p;
466✔
1154

1155
                if (argv[3] && !streq(argv[3], m->interface))
466✔
1156
                        continue;
11✔
1157

1158
                is_interface = streq(m->type, "interface");
456✔
1159

1160
                if (argv[3] && is_interface)
456✔
1161
                        continue;
1✔
1162

1163
                if (is_interface)
233✔
1164
                        r = table_add_many(
8✔
1165
                                        table,
1166
                                        TABLE_STRING, m->interface,
1167
                                        TABLE_SET_COLOR, ansi_highlight());
1168
                else
1169
                        r = table_add_cell_stringf(
451✔
1170
                                        table, /* ret_cell= */ NULL, ".%s", m->name);
1171
                if (r < 0)
455✔
1172
                        return table_log_add_error(r);
×
1173

1174
                _cleanup_free_ char *flags = NULL;
455✔
1175
                r = members_flags_to_string(m, &flags);
455✔
1176
                if (r < 0)
455✔
1177
                        return log_oom();
×
1178

1179
                r = table_add_many(
455✔
1180
                                table,
1181
                                TABLE_STRING, m->type,
1182
                                TABLE_STRING, m->signature,
1183
                                TABLE_STRING, m->value ?: m->result,
1184
                                TABLE_STRING, flags);
1185
                if (r < 0)
455✔
1186
                        return table_log_add_error(r);
×
1187
        }
1188

1189
        pager_open(arg_pager_flags);
2✔
1190

1191
        r = table_print(table, NULL);
2✔
1192
        if (r < 0)
2✔
1193
                return table_log_print_error(r);
×
1194

1195
        return 0;
1196
}
1197

1198
static int message_dump(sd_bus_message *m, FILE *f) {
×
1199
        int r;
×
1200

1201
        assert(m);
×
1202

1203
        r = sd_bus_message_dump(m, f, SD_BUS_MESSAGE_DUMP_WITH_HEADER);
×
1204
        if (r < 0)
×
1205
                return log_error_errno(r, "Failed to dump DBus message: %m");
×
1206

1207
        return 0;
1208
}
1209

1210
static int message_pcap(sd_bus_message *m, FILE *f) {
×
1211
        int r;
×
1212

1213
        assert(m);
×
1214

1215
        r = bus_message_pcap_frame(m, arg_snaplen, f);
×
1216
        if (r < 0)
×
1217
                return log_error_errno(r, "Failed to dump DBus message in PCAP format: %m");
×
1218

1219
        return 0;
1220
}
1221

1222
static int message_json(sd_bus_message *m, FILE *f) {
×
1223
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
×
1224
        int r;
×
1225

1226
        assert(m);
×
1227

1228
        r = sd_bus_message_dump_json(m, SD_BUS_MESSAGE_DUMP_WITH_HEADER, &v);
×
1229
        if (r < 0)
×
1230
                return log_error_errno(r, "Failed to build JSON object from DBus message: %m");
×
1231

1232
        r = sd_json_variant_dump(v, arg_json_format_flags, f, NULL);
×
1233
        if (r < 0)
×
1234
                return log_error_errno(r, "Failed to show JSON object: %m");
×
1235

1236
        return 0;
1237
}
1238

1239
static int monitor(int argc, char **argv, int (*dump)(sd_bus_message *m, FILE *f)) {
2✔
1240
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
2✔
1241
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *message = NULL;
2✔
1242
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2✔
1243
        uint32_t flags = 0;
2✔
1244
        const char *unique_name;
2✔
1245
        bool is_monitor = false;
2✔
1246
        int r;
2✔
1247

1248
        r = acquire_bus(true, &bus);
2✔
1249
        if (r < 0)
2✔
1250
                return r;
1251

1252
        usec_t end = arg_timeout > 0 ?
2✔
1253
                usec_add(now(CLOCK_MONOTONIC), arg_timeout) : USEC_INFINITY;
2✔
1254

1255
        /* upgrade connection; it's not used for anything else after this call */
1256
        r = sd_bus_message_new_method_call(bus,
2✔
1257
                                           &message,
1258
                                           "org.freedesktop.DBus",
1259
                                           "/org/freedesktop/DBus",
1260
                                           "org.freedesktop.DBus.Monitoring",
1261
                                           "BecomeMonitor");
1262
        if (r < 0)
2✔
1263
                return bus_log_create_error(r);
×
1264

1265
        r = sd_bus_message_open_container(message, 'a', "s");
2✔
1266
        if (r < 0)
2✔
1267
                return bus_log_create_error(r);
×
1268

1269
        STRV_FOREACH(i, argv+1) {
2✔
1270
                _cleanup_free_ char *m = NULL;
×
1271

1272
                if (!sd_bus_service_name_is_valid(*i))
×
1273
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid service name '%s'", *i);
×
1274

1275
                m = strjoin("sender='", *i, "'");
×
1276
                if (!m)
×
1277
                        return log_oom();
×
1278

1279
                r = sd_bus_message_append_basic(message, 's', m);
×
1280
                if (r < 0)
×
1281
                        return bus_log_create_error(r);
×
1282

1283
                free(m);
×
1284
                m = strjoin("destination='", *i, "'");
×
1285
                if (!m)
×
1286
                        return log_oom();
×
1287

1288
                r = sd_bus_message_append_basic(message, 's', m);
×
1289
                if (r < 0)
×
1290
                        return bus_log_create_error(r);
×
1291
        }
1292

1293
        STRV_FOREACH(i, arg_matches) {
4✔
1294
                r = sd_bus_message_append_basic(message, 's', *i);
2✔
1295
                if (r < 0)
2✔
1296
                        return bus_log_create_error(r);
×
1297
        }
1298

1299
        r = sd_bus_message_close_container(message);
2✔
1300
        if (r < 0)
2✔
1301
                return bus_log_create_error(r);
×
1302

1303
        r = sd_bus_message_append_basic(message, 'u', &flags);
2✔
1304
        if (r < 0)
2✔
1305
                return bus_log_create_error(r);
×
1306

1307
        r = sd_bus_call(bus, message, arg_timeout, &error, NULL);
2✔
1308
        if (r < 0) {
2✔
1309
                notify_bus_error(&error);
×
1310
                return log_error_errno(r, "Call to org.freedesktop.DBus.Monitoring.BecomeMonitor failed: %s",
×
1311
                                       bus_error_message(&error, r));
1312
        }
1313

1314
        r = sd_bus_get_unique_name(bus, &unique_name);
2✔
1315
        if (r < 0)
2✔
1316
                return log_error_errno(r, "Failed to get unique name: %m");
×
1317

1318
        if (!arg_quiet && !sd_json_format_enabled(arg_json_format_flags))
2✔
1319
                log_info("Monitoring bus message stream.");
×
1320

1321
        (void) sd_notify(/* unset_environment=false */ false, "READY=1");
2✔
1322

1323
        for (;;) {
6✔
1324
                _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
6✔
1325

1326
                r = sd_bus_process(bus, &m);
6✔
1327
                if (r < 0)
6✔
1328
                        return log_error_errno(r, "Failed to process bus: %m");
×
1329

1330
                if (m) {
6✔
1331
                        if (!is_monitor) {
4✔
1332
                                const char *name;
4✔
1333

1334
                                /* wait until we lose our unique name */
1335
                                if (sd_bus_message_is_signal(m, "org.freedesktop.DBus", "NameLost") <= 0)
4✔
1336
                                        continue;
4✔
1337

1338
                                r = sd_bus_message_read(m, "s", &name);
2✔
1339
                                if (r < 0)
2✔
1340
                                        return bus_log_parse_error(r);
×
1341

1342
                                if (streq(name, unique_name))
2✔
1343
                                        is_monitor = true;
2✔
1344

1345
                                continue;
2✔
1346
                        }
1347

1348
                        dump(m, stdout);
×
1349
                        fflush(stdout);
×
1350

1351
                        if (arg_limit_messages != UINT64_MAX) {
×
1352
                                arg_limit_messages--;
×
1353

1354
                                if (arg_limit_messages == 0) {
×
1355
                                        if (!arg_quiet && !sd_json_format_enabled(arg_json_format_flags))
×
1356
                                                log_info("Received requested maximum number of messages, exiting.");
×
1357
                                        return 0;
×
1358
                                }
1359
                        }
1360

1361
                        if (sd_bus_message_is_signal(m, "org.freedesktop.DBus.Local", "Disconnected") > 0) {
×
1362
                                if (!arg_quiet && !sd_json_format_enabled(arg_json_format_flags))
×
1363
                                        log_info("Connection terminated, exiting.");
×
1364
                                return 0;
×
1365
                        }
1366

1367
                        continue;
×
1368
                }
1369

1370
                if (r > 0)
2✔
1371
                        continue;
×
1372

1373
                r = sd_bus_wait(bus, arg_timeout > 0 ? usec_sub_unsigned(end, now(CLOCK_MONOTONIC)) : UINT64_MAX);
4✔
1374
                if (r == 0 && arg_timeout > 0 && now(CLOCK_MONOTONIC) >= end) {
2✔
1375
                        if (!arg_quiet && !sd_json_format_enabled(arg_json_format_flags))
2✔
1376
                                log_info("Timed out waiting for messages, exiting.");
×
1377
                        return 0;
2✔
1378
                }
1379
                if (r < 0)
×
1380
                        return log_error_errno(r, "Failed to wait for bus: %m");
×
1381
        }
1382
}
1383

1384
static int verb_monitor(int argc, char **argv, void *userdata) {
2✔
1385
        return monitor(argc, argv, sd_json_format_enabled(arg_json_format_flags) ? message_json : message_dump);
4✔
1386
}
1387

1388
static int verb_capture(int argc, char **argv, void *userdata) {
×
1389
        _cleanup_free_ char *osname = NULL;
×
1390
        static const char info[] =
×
1391
                "busctl (systemd) " PROJECT_VERSION_FULL " (Git " GIT_VERSION ")";
1392
        int r;
×
1393

1394
        if (isatty_safe(STDOUT_FILENO))
×
1395
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1396
                                       "Refusing to write message data to console, please redirect output to a file.");
1397

1398
        r = parse_os_release(NULL, "PRETTY_NAME", &osname);
×
1399
        if (r < 0)
×
1400
                log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_INFO, r,
×
1401
                               "Failed to read os-release file, ignoring: %m");
1402
        bus_pcap_header(arg_snaplen, osname, info, stdout);
×
1403

1404
        r = monitor(argc, argv, message_pcap);
×
1405
        if (r < 0)
×
1406
                return r;
1407

1408
        r = fflush_and_check(stdout);
×
1409
        if (r < 0)
×
1410
                return log_error_errno(r, "Couldn't write capture file: %m");
×
1411

1412
        return r;
1413
}
1414

1415
static int status(int argc, char **argv, void *userdata) {
5✔
1416
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
5✔
1417
        _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
5✔
1418
        pid_t pid;
5✔
1419
        int r;
5✔
1420

1421
        r = acquire_bus(false, &bus);
5✔
1422
        if (r < 0)
5✔
1423
                return r;
1424

1425
        pager_open(arg_pager_flags);
5✔
1426

1427
        if (!isempty(argv[1])) {
5✔
1428
                r = parse_pid(argv[1], &pid);
2✔
1429
                if (r < 0)
2✔
1430
                        r = sd_bus_get_name_creds(
2✔
1431
                                        bus,
1432
                                        argv[1],
1433
                                        (arg_augment_creds ? SD_BUS_CREDS_AUGMENT : 0) | _SD_BUS_CREDS_ALL,
2✔
1434
                                        &creds);
1435
                else
1436
                        r = sd_bus_creds_new_from_pid(
×
1437
                                        &creds,
1438
                                        pid,
1439
                                        _SD_BUS_CREDS_ALL);
1440
        } else {
1441
                const char *scope, *address;
3✔
1442
                sd_id128_t bus_id;
3✔
1443

1444
                r = sd_bus_get_address(bus, &address);
3✔
1445
                if (r >= 0)
3✔
1446
                        printf("BusAddress=%s%s%s\n", ansi_highlight(), address, ansi_normal());
9✔
1447

1448
                r = sd_bus_get_scope(bus, &scope);
3✔
1449
                if (r >= 0)
3✔
1450
                        printf("BusScope=%s%s%s\n", ansi_highlight(), scope, ansi_normal());
3✔
1451

1452
                r = sd_bus_get_bus_id(bus, &bus_id);
3✔
1453
                if (r >= 0)
3✔
1454
                        printf("BusID=%s" SD_ID128_FORMAT_STR "%s\n",
6✔
1455
                               ansi_highlight(), SD_ID128_FORMAT_VAL(bus_id), ansi_normal());
3✔
1456

1457
                r = sd_bus_get_owner_creds(
3✔
1458
                                bus,
1459
                                (arg_augment_creds ? SD_BUS_CREDS_AUGMENT : 0) | _SD_BUS_CREDS_ALL,
3✔
1460
                                &creds);
1461
        }
1462

1463
        if (r < 0)
5✔
1464
                return log_error_errno(r, "Failed to get credentials: %m");
1✔
1465

1466
        bus_creds_dump(creds, NULL, false);
4✔
1467
        return 0;
1468
}
1469

1470
static int message_append_cmdline(sd_bus_message *m, const char *signature, FDSet **passed_fdset, char ***x) {
1,202✔
1471
        char **p;
1,202✔
1472
        int r;
1,202✔
1473

1474
        assert(m);
1,202✔
1475
        assert(signature);
1,202✔
1476
        assert(passed_fdset);
1,202✔
1477
        assert(x);
1,202✔
1478

1479
        p = *x;
1,202✔
1480

1481
        for (;;) {
2,491✔
1482
                const char *v;
2,491✔
1483
                char t;
2,491✔
1484

1485
                t = *signature;
2,491✔
1486
                v = *p;
2,491✔
1487

1488
                if (t == 0)
2,491✔
1489
                        break;
1490
                if (!v)
1,294✔
1491
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1✔
1492
                                               "Too few parameters for signature.");
1493

1494
                signature++;
1,293✔
1495
                p++;
1,293✔
1496

1497
                switch (t) {
1,293✔
1498

1499
                case SD_BUS_TYPE_BOOLEAN:
12✔
1500

1501
                        r = parse_boolean(v);
12✔
1502
                        if (r < 0)
12✔
1503
                                return log_error_errno(r, "Failed to parse '%s' as boolean: %m", v);
×
1504

1505
                        r = sd_bus_message_append_basic(m, t, &r);
12✔
1506
                        break;
12✔
1507

1508
                case SD_BUS_TYPE_BYTE: {
1509
                        uint8_t z;
32✔
1510

1511
                        r = safe_atou8(v, &z);
32✔
1512
                        if (r < 0)
32✔
1513
                                return log_error_errno(r, "Failed to parse '%s' as byte (unsigned 8-bit integer): %m", v);
×
1514

1515
                        r = sd_bus_message_append_basic(m, t, &z);
32✔
1516
                        break;
32✔
1517
                }
1518

1519
                case SD_BUS_TYPE_INT16: {
×
1520
                        int16_t z;
×
1521

1522
                        r = safe_atoi16(v, &z);
×
1523
                        if (r < 0)
×
1524
                                return log_error_errno(r, "Failed to parse '%s' as signed 16-bit integer: %m", v);
×
1525

1526
                        r = sd_bus_message_append_basic(m, t, &z);
×
1527
                        break;
×
1528
                }
1529

1530
                case SD_BUS_TYPE_UINT16: {
1531
                        uint16_t z;
×
1532

1533
                        r = safe_atou16(v, &z);
×
1534
                        if (r < 0)
×
1535
                                return log_error_errno(r, "Failed to parse '%s' as unsigned 16-bit integer: %m", v);
×
1536

1537
                        r = sd_bus_message_append_basic(m, t, &z);
×
1538
                        break;
×
1539
                }
1540

1541
                case SD_BUS_TYPE_INT32: {
1542
                        int32_t z;
2✔
1543

1544
                        r = safe_atoi32(v, &z);
2✔
1545
                        if (r < 0)
2✔
1546
                                return log_error_errno(r, "Failed to parse '%s' as signed 32-bit integer: %m", v);
×
1547

1548
                        r = sd_bus_message_append_basic(m, t, &z);
2✔
1549
                        break;
2✔
1550
                }
1551

1552
                case SD_BUS_TYPE_UINT32: {
1553
                        uint32_t z;
1,009✔
1554

1555
                        r = safe_atou32(v, &z);
1,009✔
1556
                        if (r < 0)
1,009✔
1557
                                return log_error_errno(r, "Failed to parse '%s' as unsigned 32-bit integer: %m", v);
1✔
1558

1559
                        r = sd_bus_message_append_basic(m, t, &z);
1,008✔
1560
                        break;
1,008✔
1561
                }
1562

1563
                case SD_BUS_TYPE_INT64: {
1564
                        int64_t z;
×
1565

1566
                        r = safe_atoi64(v, &z);
×
1567
                        if (r < 0)
×
1568
                                return log_error_errno(r, "Failed to parse '%s' as signed 64-bit integer: %m", v);
×
1569

1570
                        r = sd_bus_message_append_basic(m, t, &z);
×
1571
                        break;
×
1572
                }
1573

1574
                case SD_BUS_TYPE_UINT64: {
1575
                        uint64_t z;
8✔
1576

1577
                        r = safe_atou64(v, &z);
8✔
1578
                        if (r < 0)
8✔
1579
                                return log_error_errno(r, "Failed to parse '%s' as unsigned 64-bit integer: %m", v);
1✔
1580

1581
                        r = sd_bus_message_append_basic(m, t, &z);
7✔
1582
                        break;
7✔
1583
                }
1584

1585
                case SD_BUS_TYPE_DOUBLE: {
×
1586
                        double z;
×
1587

1588
                        r = safe_atod(v, &z);
×
1589
                        if (r < 0)
×
1590
                                return log_error_errno(r, "Failed to parse '%s' as double precision floating point: %m", v);
×
1591

1592
                        r = sd_bus_message_append_basic(m, t, &z);
×
1593
                        break;
×
1594
                }
1595

1596
                case SD_BUS_TYPE_STRING:
145✔
1597
                case SD_BUS_TYPE_OBJECT_PATH:
1598
                case SD_BUS_TYPE_SIGNATURE:
1599

1600
                        r = sd_bus_message_append_basic(m, t, v);
145✔
1601
                        break;
145✔
1602

1603
                case SD_BUS_TYPE_ARRAY: {
1604
                        uint32_t n;
43✔
1605
                        size_t k;
43✔
1606

1607
                        r = safe_atou32(v, &n);
43✔
1608
                        if (r < 0)
43✔
1609
                                return log_error_errno(r, "Failed to parse '%s' number of array entries: %m", v);
2✔
1610

1611
                        r = signature_element_length(signature, &k);
42✔
1612
                        if (r < 0)
42✔
1613
                                return log_error_errno(r, "Invalid array signature: %m");
×
1614

1615
                        {
42✔
1616
                                char s[k + 1];
42✔
1617
                                memcpy(s, signature, k);
42✔
1618
                                s[k] = 0;
42✔
1619

1620
                                r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, s);
42✔
1621
                                if (r < 0)
42✔
1622
                                        return bus_log_create_error(r);
1✔
1623

1624
                                for (unsigned i = 0; i < n; i++) {
120✔
1625
                                        r = message_append_cmdline(m, s, passed_fdset, &p);
79✔
1626
                                        if (r < 0)
79✔
1627
                                                return r;
1628
                                }
1629
                        }
1630

1631
                        signature += k;
41✔
1632

1633
                        r = sd_bus_message_close_container(m);
41✔
1634
                        break;
41✔
1635
                }
1636

1637
                case SD_BUS_TYPE_VARIANT:
16✔
1638
                        r = sd_bus_message_open_container(m, SD_BUS_TYPE_VARIANT, v);
16✔
1639
                        if (r < 0)
16✔
1640
                                return bus_log_create_error(r);
×
1641

1642
                        r = message_append_cmdline(m, v, passed_fdset, &p);
16✔
1643
                        if (r < 0)
16✔
1644
                                return r;
1645

1646
                        r = sd_bus_message_close_container(m);
16✔
1647
                        break;
16✔
1648

1649
                case SD_BUS_TYPE_STRUCT_BEGIN:
24✔
1650
                case SD_BUS_TYPE_DICT_ENTRY_BEGIN: {
1651
                        size_t k;
24✔
1652

1653
                        signature--;
24✔
1654
                        p--;
24✔
1655

1656
                        r = signature_element_length(signature, &k);
24✔
1657
                        if (r < 0 || k < 2) {
24✔
1658
                                if (r >= 0 && k < 2)
×
1659
                                        r = -ERANGE;
×
1660
                                return log_error_errno(r, "Invalid struct/dict entry signature: %m");
×
1661
                        }
1662

1663
                        {
24✔
1664
                                char s[k-1];
24✔
1665
                                memcpy(s, signature + 1, k - 2);
24✔
1666
                                s[k - 2] = 0;
24✔
1667

1668
                                const char ctype = t == SD_BUS_TYPE_STRUCT_BEGIN ?
24✔
1669
                                        SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY;
1670
                                r = sd_bus_message_open_container(m, ctype, s);
24✔
1671
                                if (r < 0)
24✔
1672
                                        return bus_log_create_error(r);
×
1673

1674
                                r = message_append_cmdline(m, s, passed_fdset, &p);
24✔
1675
                                if (r < 0)
24✔
1676
                                        return r;
1677
                        }
1678

1679
                        signature += k;
24✔
1680

1681
                        r = sd_bus_message_close_container(m);
24✔
1682
                        break;
24✔
1683
                }
1684

1685
                case SD_BUS_TYPE_UNIX_FD: {
2✔
1686
                        int fd;
2✔
1687

1688
                        fd = parse_fd(v);
2✔
1689
                        if (fd < 0)
2✔
1690
                                return log_error_errno(fd, "Failed to parse '%s' as a file descriptor: %m", v);
×
1691

1692
                        if (!*passed_fdset) {
2✔
1693
                                r = fdset_new_fill(/* filter_cloexec= */ 0, passed_fdset);
1✔
1694
                                if (r < 0)
1✔
1695
                                        return log_error_errno(r, "Failed to create fd set: %m");
×
1696
                        }
1697

1698
                        if (!fdset_contains(*passed_fdset, fd))
2✔
1699
                                return log_error_errno(SYNTHETIC_ERRNO(EBADF), "Failed to find file descriptor '%s' among passed file descriptors.", v);
×
1700

1701
                        r = sd_bus_message_append_basic(m, t, &fd);
2✔
1702
                        break;
2✔
1703
                }
1704

1705
                default:
1706
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1707
                                               "Unknown signature type %c.", t);
1708
                }
1709

1710
                if (r < 0)
1,289✔
1711
                        return bus_log_create_error(r);
×
1712
        }
1713

1714
        *x = p;
1,197✔
1715
        return 0;
1,197✔
1716
}
1717

1718
static int bus_message_dump(sd_bus_message *m, uint64_t flags) {
132✔
1719
        const char *contents;
132✔
1720
        int r;
132✔
1721

1722
        assert(m);
132✔
1723

1724
        r = sd_bus_message_rewind(m, !FLAGS_SET(flags, SD_BUS_MESSAGE_DUMP_SUBTREE_ONLY));
132✔
1725
        if (r < 0)
132✔
1726
                return log_error_errno(r, "Failed to rewind: %m");
132✔
1727

1728
        if (FLAGS_SET(flags, SD_BUS_MESSAGE_DUMP_SUBTREE_ONLY)) {
132✔
1729
                r = sd_bus_message_peek_type(m, /* ret_type= */ NULL, &contents);
62✔
1730
                if (r < 0)
62✔
1731
                        return bus_log_parse_error(r);
×
1732

1733
                r = sd_bus_message_enter_container(m, 'v', contents);
62✔
1734
                if (r < 0)
62✔
1735
                        return bus_log_parse_error(r);
×
1736
        } else {
1737
                r = sd_bus_message_is_empty(m);
70✔
1738
                if (r < 0)
70✔
1739
                        return bus_log_parse_error(r);
×
1740
                if (r > 0 || arg_quiet)
70✔
1741
                        return 0;
1742
        }
1743

1744
        if (sd_json_format_enabled(arg_json_format_flags)) {
132✔
1745
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
19✔
1746

1747
                if (arg_json_format_flags & (SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_PRETTY_AUTO))
19✔
1748
                        pager_open(arg_pager_flags);
9✔
1749

1750
                r = sd_bus_message_dump_json(m, flags, &v);
19✔
1751
                if (r < 0)
19✔
1752
                        return log_error_errno(r, "Failed to dump DBus message to JSON object: %m");
×
1753

1754
                r = sd_json_variant_dump(v, arg_json_format_flags, NULL, NULL);
19✔
1755
                if (r < 0)
19✔
1756
                        return log_error_errno(r, "Failed to dump JSON object: %m");
×
1757

1758
        } else if (arg_verbose) {
113✔
1759
                pager_open(arg_pager_flags);
5✔
1760

1761
                r = sd_bus_message_dump(m, NULL, flags);
5✔
1762
                if (r < 0)
5✔
1763
                        return log_error_errno(r, "Failed to dump DBus message: %m");
×
1764
        } else {
1765

1766
                fputs(FLAGS_SET(flags, SD_BUS_MESSAGE_DUMP_SUBTREE_ONLY) ? contents : sd_bus_message_get_signature(m, true), stdout);
108✔
1767
                fputc(' ', stdout);
108✔
1768

1769
                r = format_cmdline(m, stdout, /* needs_space= */ false);
108✔
1770
                if (r < 0)
108✔
1771
                        return bus_log_parse_error(r);
×
1772

1773
                fputc('\n', stdout);
108✔
1774
        }
1775

1776
        if (FLAGS_SET(flags, SD_BUS_MESSAGE_DUMP_SUBTREE_ONLY)) {
132✔
1777
                r = sd_bus_message_exit_container(m);
62✔
1778
                if (r < 0)
62✔
1779
                        return bus_log_parse_error(r);
×
1780
        }
1781

1782
        return 0;
1783
}
1784

1785
static int call(int argc, char **argv, void *userdata) {
1,078✔
1786
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1,078✔
1787
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
1788
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
2,156✔
1789
        _cleanup_fdset_free_ FDSet *passed_fdset = NULL;
1,078✔
1790
        int r;
1,078✔
1791

1792
        r = acquire_bus(false, &bus);
1,078✔
1793
        if (r < 0)
1,078✔
1794
                return r;
1795

1796
        if (!service_name_is_valid(argv[1]))
1,078✔
1797
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid service name: %s", argv[1]);
×
1798
        if (!object_path_is_valid(argv[2]))
1,078✔
1799
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid object path: %s", argv[2]);
×
1800
        if (!interface_name_is_valid(argv[3]))
1,078✔
1801
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid interface name: %s", argv[3]);
×
1802
        if (!member_name_is_valid(argv[4]))
1,078✔
1803
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid member name: %s", argv[4]);
×
1804

1805
        r = sd_bus_message_new_method_call(bus, &m, argv[1], argv[2], argv[3], argv[4]);
1,078✔
1806
        if (r < 0)
1,078✔
1807
                return bus_log_create_error(r);
×
1808

1809
        r = sd_bus_message_set_expect_reply(m, arg_expect_reply);
1,078✔
1810
        if (r < 0)
1,078✔
1811
                return bus_log_create_error(r);
×
1812

1813
        r = sd_bus_message_set_auto_start(m, arg_auto_start);
1,078✔
1814
        if (r < 0)
1,078✔
1815
                return bus_log_create_error(r);
×
1816

1817
        r = sd_bus_message_set_allow_interactive_authorization(m, arg_allow_interactive_authorization);
1,078✔
1818
        if (r < 0)
1,078✔
1819
                return bus_log_create_error(r);
×
1820

1821
        if (!isempty(argv[5])) {
1,078✔
1822
                char **p;
1,069✔
1823

1824
                p = argv+6;
1,069✔
1825

1826
                r = message_append_cmdline(m, argv[5], &passed_fdset, &p);
1,069✔
1827
                if (r < 0)
1,069✔
1828
                        return r;
3✔
1829

1830
                if (*p)
1,066✔
1831
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1832
                                               "Too many parameters for signature.");
1833
        }
1834

1835
        if (!arg_expect_reply) {
1,075✔
1836
                r = sd_bus_send(bus, m, NULL);
×
1837
                if (r < 0)
×
1838
                        return log_error_errno(r, "Failed to send message: %m");
×
1839

1840
                return 0;
1841
        }
1842

1843
        r = sd_bus_call(bus, m, arg_timeout, &error, &reply);
1,075✔
1844
        if (r < 0) {
1,075✔
1845
                notify_bus_error(&error);
1,011✔
1846
                return log_error_errno(r, "Call failed: %s", bus_error_message(&error, r));
1,011✔
1847
        }
1848

1849
        return bus_message_dump(reply, /* flags= */ 0);
64✔
1850
}
1851

1852
static int emit_signal(int argc, char **argv, void *userdata) {
10✔
1853
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
10✔
1854
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
10✔
1855
        _cleanup_fdset_free_ FDSet *passed_fdset = NULL;
10✔
1856
        int r;
10✔
1857

1858
        r = acquire_bus(false, &bus);
10✔
1859
        if (r < 0)
10✔
1860
                return r;
1861

1862
        r = sd_bus_message_new_signal(bus, &m, argv[1], argv[2], argv[3]);
10✔
1863
        if (r < 0)
10✔
1864
                return bus_log_create_error(r);
×
1865

1866
        if (arg_destination) {
10✔
1867
                r = sd_bus_message_set_destination(m, arg_destination);
1✔
1868
                if (r < 0)
1✔
1869
                        return bus_log_create_error(r);
×
1870
        }
1871

1872
        r = sd_bus_message_set_auto_start(m, arg_auto_start);
10✔
1873
        if (r < 0)
10✔
1874
                return bus_log_create_error(r);
×
1875

1876
        if (!isempty(argv[4])) {
10✔
1877
                char **p;
10✔
1878

1879
                p = argv+5;
10✔
1880

1881
                r = message_append_cmdline(m, argv[4], &passed_fdset, &p);
10✔
1882
                if (r < 0)
10✔
1883
                        return r;
×
1884

1885
                if (*p)
10✔
1886
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1887
                                               "Too many parameters for signature.");
1888
        }
1889

1890
        r = sd_bus_send(bus, m, NULL);
10✔
1891
        if (r < 0)
10✔
1892
                return log_error_errno(r, "Failed to send signal: %m");
×
1893

1894
        return 0;
1895
}
1896

1897
static int get_property(int argc, char **argv, void *userdata) {
57✔
1898
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
57✔
1899
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
57✔
1900
        int r;
57✔
1901

1902
        if (!service_name_is_valid(argv[1]))
57✔
1903
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid service name: %s", argv[1]);
1✔
1904
        if (!object_path_is_valid(argv[2]))
56✔
1905
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid object path: %s", argv[2]);
1✔
1906
        if (!interface_name_is_valid(argv[3]))
55✔
1907
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid interface name: %s", argv[3]);
1✔
1908

1909
        r = acquire_bus(false, &bus);
54✔
1910
        if (r < 0)
54✔
1911
                return r;
1912

1913
        STRV_FOREACH(i, argv + 4) {
116✔
1914
                _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
64✔
1915

1916
                r = sd_bus_call_method(bus, argv[1], argv[2],
64✔
1917
                                       "org.freedesktop.DBus.Properties", "Get",
1918
                                       &error, &reply, "ss", argv[3], *i);
1919
                if (r < 0) {
64✔
1920
                        notify_bus_error(&error);
2✔
1921
                        return log_error_errno(r, "Failed to get property %s on interface %s: %s",
2✔
1922
                                               *i, argv[3],
1923
                                               bus_error_message(&error, r));
1924
                }
1925

1926
                r = bus_message_dump(reply, SD_BUS_MESSAGE_DUMP_SUBTREE_ONLY);
62✔
1927
                if (r < 0)
62✔
1928
                        return r;
1929
        }
1930

1931
        return 0;
1932
}
1933

1934
static int on_bus_signal(sd_bus_message *msg, void *userdata, sd_bus_error *ret_error) {
6✔
1935
        int r;
6✔
1936

1937
        r = bus_message_dump(msg, /* flags= */ 0);
6✔
1938
        if (r < 0)
6✔
1939
                return sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(ASSERT_PTR(msg))), r);
×
1940

1941
        if (arg_limit_signals != UINT64_MAX) {
6✔
1942
                arg_limit_signals--;
4✔
1943

1944
                if (arg_limit_signals == 0) {
4✔
1945
                        if (!arg_quiet && !sd_json_format_enabled(arg_json_format_flags))
2✔
1946
                                log_info("Received requested maximum number of signals, exiting.");
2✔
1947

1948
                        return sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(ASSERT_PTR(msg))), 0);
2✔
1949
                }
1950
        }
1951

1952
        return 0;
1953
}
1954

1955
static int wait_signal(int argc, char **argv, void *userdata) {
3✔
1956
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
3✔
1957
        _cleanup_(sd_event_unrefp) sd_event *e = NULL;
3✔
1958
        _cleanup_(sd_event_source_unrefp) sd_event_source *timer = NULL;
3✔
1959
        int argn = 1, r;
3✔
1960

1961
        const char *sender = argc == 5 ? argv[argn++] : NULL;
3✔
1962
        const char *path = argv[argn++];
3✔
1963
        const char *interface = argv[argn++];
3✔
1964
        const char *member = argv[argn++];
3✔
1965

1966
        if (sender && !service_name_is_valid(sender))
3✔
1967
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid service name: %s", sender);
×
1968
        if (!object_path_is_valid(path))
3✔
1969
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid object path: %s", path);
×
1970
        if (!interface_name_is_valid(interface))
3✔
1971
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid interface name: %s", interface);
×
1972
        if (!member_name_is_valid(member))
3✔
1973
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid member name: %s", member);
×
1974

1975
        r = acquire_bus(/* set_monitor= */ false, &bus);
3✔
1976
        if (r < 0)
3✔
1977
                return r;
1978

1979
        r = sd_bus_match_signal(bus, NULL, sender, path, interface, member, on_bus_signal, NULL);
3✔
1980
        if (r < 0)
3✔
1981
                return log_error_errno(r, "Failed to match signal %s on interface %s: %m", member, interface);
×
1982

1983
        r = sd_event_new(&e);
3✔
1984
        if (r < 0)
3✔
1985
                return log_error_errno(r, "Failed to allocate event loop: %m\n");
×
1986

1987
        r = sd_bus_attach_event(bus, e, SD_EVENT_PRIORITY_NORMAL);
3✔
1988
        if (r < 0)
3✔
1989
                return log_error_errno(r, "Failed to attach bus event: %m\n");
×
1990

1991
        if (arg_timeout) {
3✔
1992
                r = sd_event_add_time_relative(e, &timer, CLOCK_MONOTONIC, arg_timeout, 0, NULL, NULL);
3✔
1993
                if (r < 0)
3✔
1994
                        return log_error_errno(r, "Failed to schedule timeout: %m\n");
×
1995
        }
1996

1997
        /* The match is installed and we're ready to observe the signal */
1998
        sd_notify(/* unset_environment= */ false, "READY=1");
3✔
1999

2000
        return sd_event_loop(e);
3✔
2001
}
2002

2003
static int set_property(int argc, char **argv, void *userdata) {
4✔
2004
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
4✔
2005
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
4✔
2006
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
4✔
2007
        _cleanup_fdset_free_ FDSet *passed_fdset = NULL;
4✔
2008
        char **p;
4✔
2009
        int r;
4✔
2010

2011
        if (!service_name_is_valid(argv[1]))
4✔
2012
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid service name: %s", argv[1]);
×
2013
        if (!object_path_is_valid(argv[2]))
4✔
2014
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid object path: %s", argv[2]);
×
2015
        if (!interface_name_is_valid(argv[3]))
4✔
2016
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid interface name: %s", argv[3]);
×
2017

2018
        r = acquire_bus(false, &bus);
4✔
2019
        if (r < 0)
4✔
2020
                return r;
2021

2022
        r = sd_bus_message_new_method_call(bus, &m, argv[1], argv[2],
4✔
2023
                                           "org.freedesktop.DBus.Properties", "Set");
2024
        if (r < 0)
4✔
2025
                return bus_log_create_error(r);
×
2026

2027
        r = sd_bus_message_append(m, "ss", argv[3], argv[4]);
4✔
2028
        if (r < 0)
4✔
2029
                return bus_log_create_error(r);
×
2030

2031
        r = sd_bus_message_open_container(m, 'v', argv[5]);
4✔
2032
        if (r < 0)
4✔
2033
                return bus_log_create_error(r);
×
2034

2035
        p = argv + 6;
4✔
2036
        r = message_append_cmdline(m, argv[5], &passed_fdset, &p);
4✔
2037
        if (r < 0)
4✔
2038
                return r;
2039

2040
        r = sd_bus_message_close_container(m);
3✔
2041
        if (r < 0)
3✔
2042
                return bus_log_create_error(r);
×
2043

2044
        if (*p)
3✔
2045
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Too many parameters for signature.");
×
2046

2047
        r = sd_bus_call(bus, m, arg_timeout, &error, NULL);
3✔
2048
        if (r < 0) {
3✔
2049
                notify_bus_error(&error);
2✔
2050
                return log_error_errno(r, "Failed to set property %s on interface %s: %s",
2✔
2051
                                       argv[4], argv[3],
2052
                                       bus_error_message(&error, r));
2053
        }
2054

2055
        return 0;
2056
}
2057

2058
static int help(void) {
2✔
2059
        _cleanup_free_ char *link = NULL;
2✔
2060
        int r;
2✔
2061

2062
        r = terminal_urlify_man("busctl", "1", &link);
2✔
2063
        if (r < 0)
2✔
2064
                return log_oom();
×
2065

2066
        pager_open(arg_pager_flags);
2✔
2067

2068
        printf("%1$s [OPTIONS...] COMMAND ...\n\n"
4✔
2069
               "%5$sIntrospect the D-Bus IPC bus.%6$s\n"
2070
               "\n%3$sCommands%4$s:\n"
2071
               "  list                     List bus names\n"
2072
               "  status [SERVICE]         Show bus service, process or bus owner credentials\n"
2073
               "  monitor [SERVICE...]     Show bus traffic\n"
2074
               "  capture [SERVICE...]     Capture bus traffic as pcap\n"
2075
               "  tree [SERVICE...]        Show object tree of service\n"
2076
               "  introspect SERVICE OBJECT [INTERFACE]\n"
2077
               "  call SERVICE OBJECT INTERFACE METHOD [SIGNATURE [ARGUMENT...]]\n"
2078
               "                           Call a method\n"
2079
               "  emit OBJECT INTERFACE SIGNAL [SIGNATURE [ARGUMENT...]]\n"
2080
               "                           Emit a signal\n"
2081
               "  wait OBJECT INTERFACE SIGNAL\n"
2082
               "                           Wait for a signal\n"
2083
               "  get-property SERVICE OBJECT INTERFACE PROPERTY...\n"
2084
               "                           Get property value\n"
2085
               "  set-property SERVICE OBJECT INTERFACE PROPERTY SIGNATURE ARGUMENT...\n"
2086
               "                           Set property value\n"
2087
               "  help                     Show this help\n"
2088
               "\n%3$sOptions:%4$s\n"
2089
               "  -h --help                Show this help\n"
2090
               "     --version             Show package version\n"
2091
               "     --no-pager            Do not pipe output into a pager\n"
2092
               "     --no-legend           Do not show the headers and footers\n"
2093
               "  -l --full                Do not ellipsize output\n"
2094
               "     --system              Connect to system bus\n"
2095
               "     --user                Connect to user bus\n"
2096
               "  -H --host=[USER@]HOST    Operate on remote host\n"
2097
               "  -M --machine=CONTAINER   Operate on local container\n"
2098
               "     --address=ADDRESS     Connect to bus specified by address\n"
2099
               "     --show-machine        Show machine ID column in list\n"
2100
               "     --unique              Only show unique names\n"
2101
               "     --acquired            Only show acquired names\n"
2102
               "     --activatable         Only show activatable names\n"
2103
               "     --match=MATCH         Only show matching messages\n"
2104
               "     --size=SIZE           Maximum length of captured packet\n"
2105
               "     --list                Don't show tree, but simple object path list\n"
2106
               "  -q --quiet               Don't show method call reply\n"
2107
               "     --verbose             Show result values in long format\n"
2108
               "     --json=MODE           Output as JSON\n"
2109
               "  -j                       Same as --json=pretty on tty, --json=short otherwise\n"
2110
               "     --xml-interface       Dump the XML description in introspect command\n"
2111
               "     --expect-reply=BOOL   Expect a method call reply\n"
2112
               "     --auto-start=BOOL     Auto-start destination service\n"
2113
               "     --allow-interactive-authorization=BOOL\n"
2114
               "                           Allow interactive authorization for operation\n"
2115
               "     --timeout=SECS        Maximum time to wait for method call completion\n"
2116
               "     --augment-creds=BOOL  Extend credential data with data read from /proc/$PID\n"
2117
               "     --watch-bind=BOOL     Wait for bus AF_UNIX socket to be bound in the file\n"
2118
               "                           system\n"
2119
               "     --destination=SERVICE Destination service of a signal\n"
2120
               "  -N --limit-messages=NUMBER\n"
2121
               "                           Stop monitoring after receiving the specified number\n"
2122
               "                           of messages\n"
2123
               "\nSee the %2$s for details.\n",
2124
               program_invocation_short_name,
2125
               link,
2126
               ansi_underline(),
2127
               ansi_normal(),
2128
               ansi_highlight(),
2129
               ansi_normal());
2130

2131
        return 0;
2132
}
2133

2134
static int verb_help(int argc, char **argv, void *userdata) {
1✔
2135
        return help();
1✔
2136
}
2137

2138
static int parse_argv(int argc, char *argv[]) {
1,189✔
2139

2140
        enum {
1,189✔
2141
                ARG_VERSION = 0x100,
2142
                ARG_NO_PAGER,
2143
                ARG_NO_LEGEND,
2144
                ARG_SYSTEM,
2145
                ARG_USER,
2146
                ARG_ADDRESS,
2147
                ARG_MATCH,
2148
                ARG_SHOW_MACHINE,
2149
                ARG_UNIQUE,
2150
                ARG_ACQUIRED,
2151
                ARG_ACTIVATABLE,
2152
                ARG_SIZE,
2153
                ARG_LIST,
2154
                ARG_VERBOSE,
2155
                ARG_XML_INTERFACE,
2156
                ARG_EXPECT_REPLY,
2157
                ARG_AUTO_START,
2158
                ARG_ALLOW_INTERACTIVE_AUTHORIZATION,
2159
                ARG_TIMEOUT,
2160
                ARG_AUGMENT_CREDS,
2161
                ARG_WATCH_BIND,
2162
                ARG_JSON,
2163
                ARG_DESTINATION,
2164
        };
2165

2166
        static const struct option options[] = {
1,189✔
2167
                { "help",                            no_argument,       NULL, 'h'                                 },
2168
                { "version",                         no_argument,       NULL, ARG_VERSION                         },
2169
                { "no-pager",                        no_argument,       NULL, ARG_NO_PAGER                        },
2170
                { "no-legend",                       no_argument,       NULL, ARG_NO_LEGEND                       },
2171
                { "full",                            no_argument,       NULL, 'l'                                 },
2172
                { "system",                          no_argument,       NULL, ARG_SYSTEM                          },
2173
                { "user",                            no_argument,       NULL, ARG_USER                            },
2174
                { "address",                         required_argument, NULL, ARG_ADDRESS                         },
2175
                { "show-machine",                    no_argument,       NULL, ARG_SHOW_MACHINE                    },
2176
                { "unique",                          no_argument,       NULL, ARG_UNIQUE                          },
2177
                { "acquired",                        no_argument,       NULL, ARG_ACQUIRED                        },
2178
                { "activatable",                     no_argument,       NULL, ARG_ACTIVATABLE                     },
2179
                { "match",                           required_argument, NULL, ARG_MATCH                           },
2180
                { "host",                            required_argument, NULL, 'H'                                 },
2181
                { "machine",                         required_argument, NULL, 'M'                                 },
2182
                { "capsule",                         required_argument, NULL, 'C'                                 },
2183
                { "size",                            required_argument, NULL, ARG_SIZE                            },
2184
                { "list",                            no_argument,       NULL, ARG_LIST                            },
2185
                { "quiet",                           no_argument,       NULL, 'q'                                 },
2186
                { "verbose",                         no_argument,       NULL, ARG_VERBOSE                         },
2187
                { "xml-interface",                   no_argument,       NULL, ARG_XML_INTERFACE                   },
2188
                { "expect-reply",                    required_argument, NULL, ARG_EXPECT_REPLY                    },
2189
                { "auto-start",                      required_argument, NULL, ARG_AUTO_START                      },
2190
                { "allow-interactive-authorization", required_argument, NULL, ARG_ALLOW_INTERACTIVE_AUTHORIZATION },
2191
                { "timeout",                         required_argument, NULL, ARG_TIMEOUT                         },
2192
                { "augment-creds",                   required_argument, NULL, ARG_AUGMENT_CREDS                   },
2193
                { "watch-bind",                      required_argument, NULL, ARG_WATCH_BIND                      },
2194
                { "json",                            required_argument, NULL, ARG_JSON                            },
2195
                { "destination",                     required_argument, NULL, ARG_DESTINATION                     },
2196
                { "limit-messages",                  required_argument, NULL, 'N'                                 },
2197
                {},
2198
        };
2199

2200
        int c, r;
1,189✔
2201

2202
        assert(argc >= 0);
1,189✔
2203
        assert(argv);
1,189✔
2204

2205
        while ((c = getopt_long(argc, argv, "hH:M:C:J:qjlN:", options, NULL)) >= 0)
1,252✔
2206

2207
                switch (c) {
65✔
2208

2209
                case 'h':
1✔
2210
                        return help();
1✔
2211

2212
                case ARG_VERSION:
1✔
2213
                        return version();
1✔
2214

2215
                case ARG_NO_PAGER:
11✔
2216
                        arg_pager_flags |= PAGER_DISABLE;
11✔
2217
                        break;
11✔
2218

2219
                case ARG_NO_LEGEND:
1✔
2220
                        arg_legend = false;
1✔
2221
                        break;
1✔
2222

2223
                case 'l':
1✔
2224
                        arg_full = true;
1✔
2225
                        break;
1✔
2226

2227
                case ARG_USER:
5✔
2228
                        arg_runtime_scope = RUNTIME_SCOPE_USER;
5✔
2229
                        break;
5✔
2230

2231
                case ARG_SYSTEM:
1✔
2232
                        arg_runtime_scope = RUNTIME_SCOPE_SYSTEM;
1✔
2233
                        break;
1✔
2234

2235
                case ARG_ADDRESS:
×
2236
                        arg_address = optarg;
×
2237
                        break;
×
2238

2239
                case ARG_SHOW_MACHINE:
1✔
2240
                        arg_show_machine = true;
1✔
2241
                        break;
1✔
2242

2243
                case ARG_UNIQUE:
1✔
2244
                        arg_unique = true;
1✔
2245
                        break;
1✔
2246

2247
                case ARG_ACQUIRED:
1✔
2248
                        arg_acquired = true;
1✔
2249
                        break;
1✔
2250

2251
                case ARG_ACTIVATABLE:
1✔
2252
                        arg_activatable = true;
1✔
2253
                        break;
1✔
2254

2255
                case ARG_MATCH:
2✔
2256
                        if (strv_extend(&arg_matches, optarg) < 0)
2✔
2257
                                return log_oom();
×
2258
                        break;
2259

2260
                case ARG_SIZE: {
×
2261
                        uint64_t sz;
×
2262

2263
                        r = parse_size(optarg, 1024, &sz);
×
2264
                        if (r < 0)
×
2265
                                return log_error_errno(r, "Failed to parse size '%s': %m", optarg);
×
2266

2267
                        if ((uint64_t) (size_t) sz !=  sz)
×
2268
                                return log_error_errno(SYNTHETIC_ERRNO(E2BIG),
2269
                                                       "Size out of range.");
2270

2271
                        arg_snaplen = (size_t) sz;
×
2272
                        break;
×
2273
                }
2274

2275
                case ARG_LIST:
1✔
2276
                        arg_list = true;
1✔
2277
                        break;
1✔
2278

2279
                case 'H':
×
2280
                        arg_transport = BUS_TRANSPORT_REMOTE;
×
2281
                        arg_host = optarg;
×
2282
                        break;
×
2283

2284
                case 'M':
2✔
2285
                        r = parse_machine_argument(optarg, &arg_host, &arg_transport);
2✔
2286
                        if (r < 0)
2✔
2287
                                return r;
2288
                        break;
2289

2290
                case 'C':
1✔
2291
                        r = capsule_name_is_valid(optarg);
1✔
2292
                        if (r < 0)
1✔
2293
                                return log_error_errno(r, "Unable to validate capsule name '%s': %m", optarg);
×
2294
                        if (r == 0)
1✔
2295
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid capsule name: %s", optarg);
×
2296

2297
                        arg_host = optarg;
1✔
2298
                        arg_transport = BUS_TRANSPORT_CAPSULE;
1✔
2299
                        break;
1✔
2300

2301
                case 'q':
2✔
2302
                        arg_quiet = true;
2✔
2303
                        break;
2✔
2304

2305
                case ARG_VERBOSE:
2✔
2306
                        arg_verbose = true;
2✔
2307
                        break;
2✔
2308

2309
                case ARG_XML_INTERFACE:
1✔
2310
                        arg_xml_interface = true;
1✔
2311
                        break;
1✔
2312

2313
                case ARG_EXPECT_REPLY:
1✔
2314
                        r = parse_boolean_argument("--expect-reply=", optarg, &arg_expect_reply);
1✔
2315
                        if (r < 0)
1✔
2316
                                return r;
2317
                        break;
2318

2319
                case ARG_AUTO_START:
1✔
2320
                        r = parse_boolean_argument("--auto-start=", optarg, &arg_auto_start);
1✔
2321
                        if (r < 0)
1✔
2322
                                return r;
2323
                        break;
2324

2325
                case ARG_ALLOW_INTERACTIVE_AUTHORIZATION:
1✔
2326
                        r = parse_boolean_argument("--allow-interactive-authorization=", optarg,
1✔
2327
                                                   &arg_allow_interactive_authorization);
2328
                        if (r < 0)
1✔
2329
                                return r;
2330
                        break;
2331

2332
                case ARG_TIMEOUT:
6✔
2333
                        if (isempty(optarg)) {
6✔
2334
                                arg_timeout = 0; /* Reset to default */
×
2335
                                break;
×
2336
                        }
2337

2338
                        r = parse_sec(optarg, &arg_timeout);
6✔
2339
                        if (r < 0)
6✔
2340
                                return log_error_errno(r, "Failed to parse --timeout= parameter '%s': %m", optarg);
×
2341

2342
                        break;
2343

2344
                case ARG_AUGMENT_CREDS:
1✔
2345
                        r = parse_boolean_argument("--augment-creds=", optarg, &arg_augment_creds);
1✔
2346
                        if (r < 0)
1✔
2347
                                return r;
2348
                        break;
2349

2350
                case ARG_WATCH_BIND:
1✔
2351
                        r = parse_boolean_argument("--watch-bind=", optarg, &arg_watch_bind);
1✔
2352
                        if (r < 0)
1✔
2353
                                return r;
2354
                        break;
2355

2356
                case 'j':
4✔
2357
                        arg_json_format_flags = SD_JSON_FORMAT_PRETTY_AUTO|SD_JSON_FORMAT_COLOR_AUTO;
4✔
2358
                        break;
4✔
2359

2360
                case ARG_JSON:
10✔
2361
                        r = parse_json_argument(optarg, &arg_json_format_flags);
10✔
2362
                        if (r <= 0)
10✔
2363
                                return r;
2364

2365
                        break;
2366

2367
                case ARG_DESTINATION:
1✔
2368
                        arg_destination = optarg;
1✔
2369
                        break;
1✔
2370

2371
                case 'N':
3✔
2372
                        if (isempty(optarg)) {
3✔
2373
                                /* Reset to default */
2374
                                arg_limit_messages = UINT64_MAX;
×
2375
                                arg_limit_signals = 1;
×
2376
                                break;
×
2377
                        }
2378

2379
                        if (streq(optarg, "infinity")) {
3✔
2380
                                arg_limit_signals = arg_limit_messages = UINT64_MAX;
1✔
2381
                                break;
1✔
2382
                        }
2383

2384
                        r = safe_atou64(optarg, &arg_limit_messages);
2✔
2385
                        if (r < 0)
2✔
2386
                                return log_error_errno(r, "Failed to parse --limit-messages= parameter: %s", optarg);
×
2387
                        if (arg_limit_messages == 0)
2✔
2388
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--limit-messages= parameter cannot be 0");
×
2389

2390
                        arg_limit_signals = arg_limit_messages;
2✔
2391
                        break;
2✔
2392

2393
                case '?':
2394
                        return -EINVAL;
2395

2396
                default:
×
2397
                        assert_not_reached();
×
2398
                }
2399

2400
        if (arg_full < 0)
1,187✔
2401
                arg_full = terminal_is_dumb();
1,186✔
2402

2403
        return 1;
2404
}
2405

2406
static int busctl_main(int argc, char *argv[]) {
1,187✔
2407
        static const Verb verbs[] = {
1,187✔
2408
                { "list",         VERB_ANY, 1,        VERB_DEFAULT, list_bus_names },
2409
                { "status",       VERB_ANY, 2,        0,            status         },
2410
                { "monitor",      VERB_ANY, VERB_ANY, 0,            verb_monitor   },
2411
                { "capture",      VERB_ANY, VERB_ANY, 0,            verb_capture   },
2412
                { "tree",         VERB_ANY, VERB_ANY, 0,            tree           },
2413
                { "introspect",   3,        4,        0,            introspect     },
2414
                { "call",         5,        VERB_ANY, 0,            call           },
2415
                { "emit",         4,        VERB_ANY, 0,            emit_signal    },
2416
                { "wait",         4,        5,        0,            wait_signal    },
2417
                { "get-property", 5,        VERB_ANY, 0,            get_property   },
2418
                { "set-property", 6,        VERB_ANY, 0,            set_property   },
2419
                { "help",         VERB_ANY, VERB_ANY, 0,            verb_help      },
2420
                {}
2421
        };
2422

2423
        return dispatch_verb(argc, argv, verbs, NULL);
1,187✔
2424
}
2425

2426
static int run(int argc, char *argv[]) {
1,189✔
2427
        int r;
1,189✔
2428

2429
        log_setup();
1,189✔
2430

2431
        r = parse_argv(argc, argv);
1,189✔
2432
        if (r <= 0)
1,189✔
2433
                return r;
2434

2435
        return busctl_main(argc, argv);
1,187✔
2436
}
2437

2438
DEFINE_MAIN_FUNCTION(run);
1,189✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc