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

systemd / systemd / 15746219612

18 Jun 2025 03:36PM UTC coverage: 72.045% (-0.04%) from 72.087%
15746219612

push

github

bluca
test-cpu-set-util: use ASSERT_XYZ() macros

119 of 121 new or added lines in 1 file covered. (98.35%)

1096 existing lines in 62 files now uncovered.

300222 of 416715 relevant lines covered (72.04%)

712541.77 hits per line

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

54.85
/src/sysupdate/updatectl.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

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

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

10
#include "alloc-util.h"
11
#include "build.h"
12
#include "bus-error.h"
13
#include "bus-label.h"
14
#include "bus-locator.h"
15
#include "bus-map-properties.h"
16
#include "bus-util.h"
17
#include "errno-util.h"
18
#include "format-table.h"
19
#include "hashmap.h"
20
#include "json-util.h"
21
#include "main-func.h"
22
#include "pager.h"
23
#include "polkit-agent.h"
24
#include "pretty-print.h"
25
#include "runtime-scope.h"
26
#include "string-util.h"
27
#include "strv.h"
28
#include "sysupdate-update-set-flags.h"
29
#include "sysupdate-util.h"
30
#include "terminal-util.h"
31
#include "verbs.h"
32

33
static PagerFlags arg_pager_flags = 0;
34
static bool arg_legend = true;
35
static bool arg_reboot = false;
36
static bool arg_offline = false;
37
static bool arg_now = false;
38
static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
39
static const char *arg_host = NULL;
40

41
#define SYSUPDATE_HOST_PATH "/org/freedesktop/sysupdate1/target/host"
42
#define SYSUPDATE_TARGET_INTERFACE "org.freedesktop.sysupdate1.Target"
43

44
typedef struct Version {
45
        char *version;
46
        UpdateSetFlags flags;
47
        char **changelog;
48
        char *contents_json;
49
} Version;
50

51
static void version_done(Version *v) {
24✔
52
        assert(v);
24✔
53

54
        v->version = mfree(v->version);
24✔
55
        v->changelog = strv_free(v->changelog);
24✔
56
        v->flags = 0;
24✔
57
        v->contents_json = mfree(v->contents_json);
24✔
58
}
24✔
59

60
typedef struct Operation {
61
        void *userdata;
62

63
        sd_bus *bus;
64
        sd_event *event;
65
        unsigned *remaining;
66

67
        const char *target_path;
68
        const char *target_id;
69

70
        uint64_t job_id;
71
        char *job_path;
72
        sd_event_source *job_interrupt_source;
73
        sd_bus_slot *job_properties_slot;
74
        sd_bus_slot *job_finished_slot;
75
} Operation;
76

77
static Operation* operation_free(Operation *p) {
16✔
78
        if (!p)
16✔
79
                return NULL;
80

81
        assert(*p->remaining > 0);
16✔
82
        *p->remaining -= 1;
16✔
83
        if (*p->remaining == 0)
16✔
84
                /* We want to crash the program if we can't exit the loop
85
                 * cleanly, otherwise it will just hang */
86
                assert_se(sd_event_exit(p->event, 0) >= 0);
6✔
87

88
        free(p->job_path);
16✔
89

90
        sd_event_source_disable_unref(p->job_interrupt_source);
16✔
91
        sd_bus_slot_unref(p->job_properties_slot);
16✔
92
        sd_bus_slot_unref(p->job_finished_slot);
16✔
93

94
        return mfree(p);
16✔
95
}
96

97
DEFINE_TRIVIAL_CLEANUP_FUNC(Operation*, operation_free);
50✔
98

99
static Operation* operation_new(
16✔
100
                void *userdata,
101
                sd_bus *bus,
102
                unsigned *remaining,
103
                const char *target_path,
104
                const char *target_id) {
105

106
        _cleanup_(operation_freep) Operation *o = NULL;
16✔
107

108
        o = new(Operation, 1);
16✔
109
        if (!o)
16✔
110
                return NULL;
111

112
        *o = (Operation) {
32✔
113
                .userdata = userdata,
114
                .bus = bus,
115
                .event = sd_bus_get_event(bus),
16✔
116
                .remaining = remaining,
117
                .target_path = target_path,
118
                .target_id = target_id,
119
        };
120
        return TAKE_PTR(o);
16✔
121
}
122

123
static int ensure_targets(sd_bus *bus, char **argv, char ***ret_targets) {
6✔
124
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
6✔
125
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
6✔
126
        _cleanup_strv_free_ char **targets = NULL;
6✔
127
        int r;
6✔
128

129
        assert(bus);
6✔
130
        assert(ret_targets);
6✔
131

132
        if (strv_isempty(argv)) {
6✔
133
                const char *class, *name, *path;
6✔
134

135
                r = bus_call_method(bus, bus_sysupdate_mgr, "ListTargets", &error, &reply, NULL);
6✔
136
                if (r < 0)
6✔
137
                        return log_error_errno(r, "Failed to call ListTargets: %s", bus_error_message(&error, r));
×
138

139
                r = sd_bus_message_enter_container(reply, 'a', "(sso)");
6✔
140
                if (r < 0)
6✔
141
                        return bus_log_parse_error(r);
×
142

143
                while ((r = sd_bus_message_read(reply, "(sso)", &class, &name, &path)) > 0) {
16✔
144
                        _cleanup_free_ char *id = NULL;
×
145

146
                        if (streq(class, "host"))
10✔
147
                                id = strdup("host");
6✔
148
                        else
149
                                id = strjoin(class, ":", name);
4✔
150
                        if (!id)
10✔
151
                                return log_oom();
×
152

153
                        r = strv_consume(&targets, TAKE_PTR(id));
10✔
154
                        if (r < 0)
10✔
155
                                return log_oom();
×
156
                }
157
                if (r < 0)
6✔
158
                        return bus_log_parse_error(r);
×
159

160
                r = sd_bus_message_exit_container(reply);
6✔
161
                if (r < 0)
6✔
162
                        return bus_log_parse_error(r);
×
163
        } else {
164
                r = strv_extend_strv(&targets, argv, true);
×
165
                if (r < 0)
×
166
                        return log_oom();
×
167
        }
168

169
        *ret_targets = TAKE_PTR(targets);
6✔
170
        return 0;
6✔
171
}
172

173
static int parse_target(
14✔
174
                const char *in,
175
                char **ret_bus_path,
176
                char **ret_version) {
177
        _cleanup_free_ char *id = NULL, *version = NULL, *escaped = NULL, *objpath = NULL;
14✔
178
        const char *s;
14✔
179

180
        /*
181
         * Parses the TARGET[@VERSION] syntax from the command line into
182
         * a bus object path and an optional version number.
183
         */
184

185
        assert(in);
14✔
186
        assert(ret_bus_path);
14✔
187
        assert(ret_version);
14✔
188

189
        s = strrchr(in, '@');
14✔
190
        if (s) {
14✔
191
                version = strdup(s + 1);
2✔
192
                if (!version)
2✔
193
                        return -ENOMEM;
194
                id = strndup(in, s - in);
2✔
195
        } else
196
                id = strdup(in);
12✔
197
        if (!id)
14✔
198
                return -ENOMEM;
199

200
        escaped = bus_label_escape(id);
14✔
201
        if (!escaped)
14✔
202
                return -ENOMEM;
203

204
        objpath = strjoin("/org/freedesktop/sysupdate1/target/", escaped);
14✔
205
        if (!objpath)
14✔
206
                return -ENOMEM;
207

208
        *ret_bus_path = TAKE_PTR(objpath);
14✔
209
        *ret_version = TAKE_PTR(version);
14✔
210
        return 0;
14✔
211
}
212

213
static int parse_targets(
6✔
214
                char **targets,
215
                size_t *ret_n,
216
                char ***ret_bus_paths,
217
                char ***ret_versions) {
218
        _cleanup_strv_free_ char **bus_paths = NULL;
6✔
219
        _cleanup_strv_free_ char **versions = NULL;
6✔
220
        size_t n = 0;
6✔
221
        int r;
6✔
222

223
        assert(ret_bus_paths);
6✔
224
        assert(ret_n);
6✔
225

226
        if (strv_isempty(targets))
6✔
227
                return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "No targets found.");
×
228

229
        STRV_FOREACH(id, targets) {
16✔
230
                _cleanup_free_ char *bus_path = NULL, *version = NULL;
10✔
231

232
                r = parse_target(*id, &bus_path, &version);
10✔
233
                if (r < 0)
10✔
234
                        return log_oom();
×
235

236
                if (version && !ret_versions)
10✔
237
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
238
                                               "Unexpected version specifier in target: %s",
239
                                               *id);
240

241
                r = strv_extend(&bus_paths, strempty(bus_path));
10✔
242
                if (r < 0)
10✔
243
                        return log_oom();
×
244

245
                r = strv_extend(&versions, strempty(version));
20✔
246
                if (r < 0)
10✔
247
                        return log_oom();
×
248

249
                n++;
10✔
250
        }
251

252
        *ret_n = n;
6✔
253
        *ret_bus_paths = TAKE_PTR(bus_paths);
6✔
254
        if (ret_versions)
6✔
255
                *ret_versions = TAKE_PTR(versions);
2✔
256
        return 0;
257
}
258

259
static int log_bus_error(int r, const sd_bus_error *error, const char *target, const char *action) {
×
260
        assert(action);
×
261

262
        if (r == 0) {
×
263
                assert(sd_bus_error_is_set(error));
×
264
                r = sd_bus_error_get_errno(error);
×
265
        }
266

267
        if (sd_bus_error_has_name(error, SD_BUS_ERROR_UNKNOWN_OBJECT)) {
×
268
                if (target)
×
269
                        return log_error_errno(r, "Invalid target: %s", target);
×
270
                return log_error_errno(r, "Invalid target");
×
271
        }
272

273
        if (target)
×
274
                return log_error_errno(r, "Failed to %s for '%s': %s", action, target,
×
275
                                       bus_error_message(error, r));
276
        return log_error_errno(r, "Failed to %s: %s", action, bus_error_message(error, r));
×
277
}
278

279
static int list_targets(sd_bus *bus) {
2✔
280
        _cleanup_(table_unrefp) Table *table = NULL;
2✔
281
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2✔
282
        _cleanup_strv_free_ char **targets = NULL, **target_paths = NULL;
2✔
283
        size_t n;
2✔
284
        int r;
2✔
285

286
        assert(bus);
2✔
287

288
        r = ensure_targets(bus, /* argv= */ NULL, &targets);
2✔
289
        if (r < 0)
2✔
290
                return log_error_errno(r, "Failed to find targets: %m");
×
291

292
        r = parse_targets(targets, &n, &target_paths, /* ret_versions= */ NULL);
2✔
293
        if (r < 0)
2✔
294
                return log_error_errno(r, "Failed to parse targets: %m");
×
295

296
        table = table_new("target", "version", "path");
2✔
297
        if (!table)
2✔
298
                return log_oom();
×
299

300
        for (size_t i = 0; i < n; i++) {
6✔
301
                char *version = NULL;
4✔
302
                _cleanup_free_ char *path = NULL;
4✔
303
                _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
4✔
304

305
                r = sd_bus_call_method(bus, bus_sysupdate_mgr->destination,
8✔
306
                                       target_paths[i], SYSUPDATE_TARGET_INTERFACE,
4✔
307
                                       "GetVersion", &error, &reply, NULL);
308
                if (r < 0)
4✔
309
                        return log_bus_error(r, &error, targets[i], "get current version");
×
310
                r = sd_bus_message_read_basic(reply, 's', &version);
4✔
311
                if (r < 0)
4✔
312
                        return bus_log_parse_error(r);
×
313

314
                r = sd_bus_get_property_string(bus, bus_sysupdate_mgr->destination,
8✔
315
                                               target_paths[i], SYSUPDATE_TARGET_INTERFACE,
4✔
316
                                               "Path", &error, &path);
317
                if (r < 0)
4✔
318
                        return log_bus_error(r, &error, targets[i], "get target bus path");
×
319

320
                r = table_add_many(table,
8✔
321
                                   TABLE_STRING, targets[i],
322
                                   TABLE_STRING, empty_to_dash(version),
323
                                   TABLE_STRING, path);
324
                if (r < 0)
4✔
325
                        return table_log_add_error(r);
×
326
        }
327

328
        return table_print_with_pager(table, SD_JSON_FORMAT_OFF, arg_pager_flags, arg_legend);
2✔
329
}
330

331
typedef struct DescribeParams {
332
        Version v;
333
        sd_json_variant *contents_json;
334
        bool newest;
335
        bool available;
336
        bool installed;
337
        bool obsolete;
338
        bool protected;
339
        bool incomplete;
340
} DescribeParams;
341

342
static void describe_params_done(DescribeParams *p) {
12✔
343
        assert(p);
12✔
344

345
        version_done(&p->v);
12✔
346
        sd_json_variant_unref(p->contents_json);
12✔
347
}
12✔
348

349
static int parse_describe(sd_bus_message *reply, Version *ret) {
12✔
350
        char *version_json = NULL;
12✔
351
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
12✔
352
        int r;
12✔
353

354
        assert(reply);
12✔
355
        assert(ret);
12✔
356

357
        r = sd_bus_message_read_basic(reply, 's', &version_json);
12✔
358
        if (r < 0)
12✔
359
                return bus_log_parse_error(r);
×
360

361
        r = sd_json_parse(version_json, 0, &json, NULL, NULL);
12✔
362
        if (r < 0)
12✔
363
                return log_error_errno(r, "Failed to parse JSON: %m");
×
364

365
        assert(sd_json_variant_is_object(json));
12✔
366

367
        static const sd_json_dispatch_field dispatch_table[] = {
12✔
368
                { "version",       SD_JSON_VARIANT_STRING,  sd_json_dispatch_string,  offsetof(DescribeParams, v.version),     0 },
369
                { "newest",        SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(DescribeParams, newest),        0 },
370
                { "available",     SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(DescribeParams, available),     0 },
371
                { "installed",     SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(DescribeParams, installed),     0 },
372
                { "obsolete",      SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(DescribeParams, obsolete),      0 },
373
                { "protected",     SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(DescribeParams, protected),     0 },
374
                { "incomplete",    SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(DescribeParams, incomplete),    0 },
375
                { "changelogUrls", SD_JSON_VARIANT_ARRAY,   sd_json_dispatch_strv,    offsetof(DescribeParams, v.changelog),   0 },
376
                { "contents",      SD_JSON_VARIANT_ARRAY,   sd_json_dispatch_variant, offsetof(DescribeParams, contents_json), 0 },
377
                {},
378
        };
379

380
        _cleanup_(describe_params_done) DescribeParams p = {};
12✔
381

382
        r = sd_json_dispatch(json, dispatch_table, SD_JSON_ALLOW_EXTENSIONS, &p);
12✔
383
        if (r < 0)
12✔
384
                return log_error_errno(r, "Failed to parse JSON: %m");
×
385

386
        SET_FLAG(p.v.flags, UPDATE_NEWEST, p.newest);
12✔
387
        SET_FLAG(p.v.flags, UPDATE_AVAILABLE, p.available);
12✔
388
        SET_FLAG(p.v.flags, UPDATE_INSTALLED, p.installed);
12✔
389
        SET_FLAG(p.v.flags, UPDATE_OBSOLETE, p.obsolete);
12✔
390
        SET_FLAG(p.v.flags, UPDATE_PROTECTED, p.protected);
12✔
391
        SET_FLAG(p.v.flags, UPDATE_INCOMPLETE, p.incomplete);
12✔
392

393
        r = sd_json_variant_format(p.contents_json, 0, &p.v.contents_json);
12✔
394
        if (r < 0)
12✔
395
                return log_error_errno(r, "Failed to format JSON for contents: %m");
×
396

397
        *ret = TAKE_STRUCT(p.v);
12✔
398
        return 0;
12✔
399
}
400

401
static int list_versions_finished(sd_bus_message *reply, void *userdata, sd_bus_error *ret_error) {
10✔
402
        _cleanup_(operation_freep) Operation *op = ASSERT_PTR(userdata);
20✔
403
        Table *table = ASSERT_PTR(op->userdata);
10✔
404
        const sd_bus_error *e;
10✔
405
        _cleanup_(version_done) Version v = {};
×
406
        _cleanup_free_ char *version_link = NULL;
10✔
407
        const char *color;
10✔
408
        int r;
10✔
409

410
        assert(reply);
10✔
411

412
        e = sd_bus_message_get_error(reply);
10✔
413
        if (e)
10✔
414
                return log_bus_error(0, e, NULL, "call Describe");
×
415

416
        r = parse_describe(reply, &v);
10✔
417
        if (r < 0)
10✔
418
                return log_error_errno(r, "Failed to parse Describe output: %m");
×
419

420
        color = update_set_flags_to_color(v.flags);
10✔
421

422
        if (urlify_enabled() && !strv_isempty(v.changelog)) {
10✔
423
                version_link = strjoin(v.version, glyph(GLYPH_EXTERNAL_LINK));
×
424
                if (!version_link)
×
425
                        return log_oom();
×
426
        }
427

428
        r = table_add_many(table,
10✔
429
                           TABLE_STRING,    update_set_flags_to_glyph(v.flags),
430
                           TABLE_SET_COLOR, color,
431
                           TABLE_STRING,    version_link ?: v.version,
432
                           TABLE_SET_COLOR, color,
433
                           TABLE_SET_URL,   strv_isempty(v.changelog) ? NULL : v.changelog[0],
434
                           TABLE_STRING,    update_set_flags_to_string(v.flags),
435
                           TABLE_SET_COLOR, color);
436
        if (r < 0)
10✔
437
                return table_log_add_error(r);
×
438

439
        return 0;
440
}
441

442
static int list_versions(sd_bus *bus, const char *target_path) {
2✔
443
        _cleanup_(sd_event_unrefp) sd_event *event = NULL;
2✔
444
        _cleanup_(table_unrefp) Table *table = NULL;
×
445
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
2✔
446
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2✔
447
        _cleanup_strv_free_ char **versions = NULL;
2✔
448
        unsigned remaining = 0;
2✔
449
        int r;
2✔
450

451
        r = sd_bus_call_method(
6✔
452
                        bus,
453
                        bus_sysupdate_mgr->destination,
2✔
454
                        target_path,
455
                        SYSUPDATE_TARGET_INTERFACE,
456
                        "List",
457
                        &error,
458
                        &reply,
459
                        "t",
460
                        arg_offline ? SD_SYSUPDATE_OFFLINE : 0);
2✔
461
        if (r < 0)
2✔
462
                return log_bus_error(r, &error, NULL, "call List");
×
463

464
        r = sd_bus_message_read_strv(reply, &versions);
2✔
465
        if (r < 0)
2✔
466
                return bus_log_parse_error(r);
×
467

468
        table = table_new("", "version", "status");
2✔
469
        if (!table)
2✔
470
                return log_oom();
×
471

472
        (void) table_set_sort(table, 1);
2✔
473
        (void) table_set_reverse(table, 1, true);
2✔
474

475
        r = sd_event_default(&event);
2✔
476
        if (r < 0)
2✔
477
                return log_error_errno(r, "Failed to get event loop: %m");
×
478

479
        r = sd_bus_attach_event(bus, event, 0);
2✔
480
        if (r < 0)
2✔
481
                return log_error_errno(r, "Failed to attach bus to event loop: %m");
×
482

483
        r = sd_event_set_signal_exit(event, true);
2✔
484
        if (r < 0)
2✔
485
                return log_error_errno(r, "Failed to set up interrupt handler: %m");
×
486

487
        STRV_FOREACH(version, versions) {
12✔
488
                _cleanup_(operation_freep) Operation *op = NULL;
×
489
                op = operation_new(table, bus, &remaining, NULL, NULL);
10✔
490
                if (!op)
10✔
491
                        return log_oom();
×
492

493
                r = sd_bus_call_method_async(bus,
30✔
494
                                             NULL,
495
                                             bus_sysupdate_mgr->destination,
10✔
496
                                             target_path,
497
                                             SYSUPDATE_TARGET_INTERFACE,
498
                                             "Describe",
499
                                             list_versions_finished,
500
                                             op,
501
                                             "st",
502
                                             *version,
503
                                             arg_offline ? SD_SYSUPDATE_OFFLINE : 0);
10✔
504
                if (r < 0)
10✔
505
                        return log_error_errno(r, "Failed to call Describe: %m");
×
506
                TAKE_PTR(op);
10✔
507

508
                remaining++;
10✔
509
        }
510

511
        r = sd_event_loop(event);
2✔
512
        if (r < 0)
2✔
513
                return log_error_errno(r, "Failed to start event loop: %m");
×
514

515
        return table_print_with_pager(table, SD_JSON_FORMAT_OFF, arg_pager_flags, arg_legend);
2✔
516
}
517

518
static int describe(sd_bus *bus, const char *target_path, const char *version) {
2✔
519
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
2✔
520
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2✔
521
        _cleanup_(table_unrefp) Table *table = NULL;
×
522
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
2✔
523
        _cleanup_(version_done) Version v = {};
2✔
524
        sd_json_variant *entry;
2✔
525
        const char *color;
2✔
526
        int r;
2✔
527

528
        r = sd_bus_call_method(
6✔
529
                        bus,
530
                        bus_sysupdate_mgr->destination,
2✔
531
                        target_path,
532
                        SYSUPDATE_TARGET_INTERFACE,
533
                        "Describe",
534
                        &error,
535
                        &reply,
536
                        "st",
537
                        version,
538
                        arg_offline ? SD_SYSUPDATE_OFFLINE : 0);
2✔
539
        if (r < 0)
2✔
540
                return log_bus_error(r, &error, NULL, "call Describe");
×
541

542
        r = parse_describe(reply, &v);
2✔
543
        if (r < 0)
2✔
544
                return log_error_errno(r, "Failed to parse Describe output: %m");
×
545

546
        color = strempty(update_set_flags_to_color(v.flags));
2✔
547

548
        printf("%s%s%s Version: %s\n"
6✔
549
               "    State: %s%s%s\n",
550
               color,
551
               update_set_flags_to_glyph(v.flags),
552
               ansi_normal(),
553
               v.version,
554
               color,
555
               update_set_flags_to_string(v.flags),
556
               ansi_normal());
557

558
        STRV_FOREACH(url, v.changelog) {
2✔
559
                _cleanup_free_ char *changelog_link = NULL;
×
560

561
                r = terminal_urlify(*url, NULL, &changelog_link);
×
562
                if (r < 0)
×
563
                        return log_error_errno(r, "Could not urlify link to change-log: %m");
×
564

565
                printf("ChangeLog: %s\n", strna(changelog_link));
×
566
        }
567
        printf("\n");
2✔
568

569
        r = sd_json_parse(v.contents_json, 0, &json, NULL, NULL);
2✔
570
        if (r < 0)
2✔
571
                return log_error_errno(r, "Failed to parse JSON: %m");
×
572

573
        assert(sd_json_variant_is_array(json));
2✔
574

575
        JSON_VARIANT_ARRAY_FOREACH(entry, json) {
12✔
576
                assert(sd_json_variant_is_object(entry));
10✔
577
                const char *key;
10✔
578
                sd_json_variant *value;
10✔
579

580
                if (!table) {
10✔
581
                        table = table_new_raw(sd_json_variant_elements(entry) / 2);
2✔
582
                        if (!table)
2✔
583
                                return log_oom();
×
584

585
                        JSON_VARIANT_OBJECT_FOREACH(key, value, entry) {
20✔
586

587
                                r = table_add_cell(table, NULL, TABLE_HEADER, key);
18✔
588
                                if (r < 0)
18✔
589
                                        return table_log_add_error(r);
×
590
                        }
591
                }
592

593
                JSON_VARIANT_OBJECT_FOREACH(key, value, entry) {
100✔
594
                        TableDataType type;
90✔
595
                        uint64_t number;
90✔
596
                        bool boolean;
90✔
597
                        const void *data;
90✔
598

599
                        if (sd_json_variant_is_string(value)) {
90✔
600
                                type = TABLE_STRING;
24✔
601
                                assert_se(data = sd_json_variant_string(value));
24✔
602
                        } else if (sd_json_variant_is_unsigned(value)) {
66✔
603
                                type = TABLE_UINT64;
20✔
604
                                number = sd_json_variant_unsigned(value);
20✔
605
                                data = &number;
20✔
606
                        } else if (sd_json_variant_is_boolean(value)) {
46✔
607
                                type = TABLE_BOOLEAN;
4✔
608
                                boolean = sd_json_variant_boolean(value);
4✔
609
                                data = &boolean;
4✔
610
                        } else if (sd_json_variant_is_null(value)) {
42✔
611
                                type = TABLE_EMPTY;
612
                                data = NULL;
613
                        } else
614
                                assert_not_reached();
×
615

616
                        if (streq(key, "ptflags"))
90✔
617
                                type = TABLE_UINT64_HEX;
618
                        else if (streq(key, "size"))
80✔
619
                                type = TABLE_SIZE;
620
                        else if (streq(key, "mode"))
80✔
621
                                type = TABLE_MODE;
622
                        else if (streq(key, "mtime"))
70✔
623
                                type = TABLE_TIMESTAMP;
10✔
624

625
                        r = table_add_cell(table, NULL, type, data);
90✔
626
                        if (r < 0)
90✔
627
                                return table_log_add_error(r);
×
628
                }
629
        }
630

631
        return table_print_with_pager(table, SD_JSON_FORMAT_OFF, arg_pager_flags, arg_legend);
2✔
632
}
633

634
static int verb_list(int argc, char **argv, void *userdata) {
6✔
635
        sd_bus *bus = ASSERT_PTR(userdata);
6✔
636
        _cleanup_free_ char *target_path = NULL, *version = NULL;
6✔
637
        int r;
6✔
638

639
        if (argc == 1)
6✔
640
                return list_targets(bus);
2✔
641

642
        r = parse_target(argv[1], &target_path, &version);
4✔
643
        if (r < 0)
4✔
644
                return log_oom();
×
645

646
        if (version)
4✔
647
                return describe(bus, target_path, version);
2✔
648
        else
649
                return list_versions(bus, target_path);
2✔
650
}
651

652
static int check_describe_finished(sd_bus_message *reply, void *userdata, sd_bus_error *ret_error) {
×
653
        _cleanup_(operation_freep) Operation *op = ASSERT_PTR(userdata);
×
654
        Table *table = ASSERT_PTR(op->userdata);
×
655
        _cleanup_(version_done) Version v = {};
×
656
        _cleanup_free_ char *update = NULL;
×
657
        const sd_bus_error *e;
×
658
        sd_bus_error error = {};
×
659
        const char *lnk = NULL;
×
660
        char *current;
×
661
        int r;
×
662

663
        assert(reply);
×
664

665
        e = sd_bus_message_get_error(reply);
×
666
        if (e)
×
667
                return log_bus_error(0, e, NULL, "call Describe");
×
668

669
        r = parse_describe(reply, &v);
×
670
        if (r < 0)
×
671
                return log_error_errno(r, "Failed to parse output of Describe: %m");
×
672

673
        r = sd_bus_call_method(
×
674
                        op->bus,
675
                        bus_sysupdate_mgr->destination,
×
676
                        op->target_path,
×
677
                        SYSUPDATE_TARGET_INTERFACE,
678
                        "GetVersion",
679
                        &error,
680
                        &reply,
681
                        NULL);
682
        if (r < 0)
×
683
                return log_bus_error(r, &error, op->target_id, "get current version");
×
684

685
        r = sd_bus_message_read_basic(reply, 's', &current);
×
686
        if (r < 0)
×
687
                return bus_log_parse_error(r);
×
688

689
        if (urlify_enabled() && !strv_isempty(v.changelog))
×
690
                lnk = glyph(GLYPH_EXTERNAL_LINK);
×
691

692
        update = strjoin(empty_to_dash(current), " ",
×
693
                         glyph(GLYPH_ARROW_RIGHT), " ",
694
                         v.version, strempty(lnk));
695
        if (!update)
×
696
                return log_oom();
×
697

698
        r = table_add_many(table,
×
699
                           TABLE_STRING,  op->target_id,
700
                           TABLE_STRING,  update,
701
                           TABLE_SET_URL, strv_isempty(v.changelog) ? NULL : v.changelog[0]);
702
        if (r < 0)
×
703
                return table_log_add_error(r);
×
704

705
        return 0;
706
}
707

708
static int check_finished(sd_bus_message *reply, void *userdata, sd_bus_error *ret_error) {
4✔
709
        _cleanup_(operation_freep) Operation *op = ASSERT_PTR(userdata);
4✔
710
        const sd_bus_error *e;
4✔
711
        const char *new_version = NULL;
4✔
712
        int r;
4✔
713

714
        assert(reply);
4✔
715

716
        e = sd_bus_message_get_error(reply);
4✔
717
        if (e)
4✔
718
                return log_bus_error(0, e, op->target_id, "call CheckNew");
×
719

720
        r = sd_bus_message_read(reply, "s", &new_version);
4✔
721
        if (r < 0)
4✔
722
                return bus_log_parse_error(r);
×
723

724
        if (isempty(new_version))
8✔
725
                return 0;
726

727
        r = sd_bus_call_method_async(op->bus,
×
728
                                     NULL,
729
                                     bus_sysupdate_mgr->destination,
×
730
                                     op->target_path,
731
                                     SYSUPDATE_TARGET_INTERFACE,
732
                                     "Describe",
733
                                     check_describe_finished,
734
                                     op,
735
                                     "st",
736
                                     new_version,
737
                                     arg_offline ? SD_SYSUPDATE_OFFLINE : 0);
×
738
        if (r < 0)
×
739
                return log_error_errno(r, "Failed to call Describe: %m");
×
740
        TAKE_PTR(op);
×
741

742
        return 0;
×
743
}
744

745
static int verb_check(int argc, char **argv, void *userdata) {
2✔
746
        sd_bus *bus = ASSERT_PTR(userdata);
2✔
747
        _cleanup_(table_unrefp) Table *table = NULL;
×
748
        _cleanup_(sd_event_unrefp) sd_event *event = NULL;
2✔
749
        _cleanup_strv_free_ char **targets = NULL, **target_paths = NULL;
2✔
750
        size_t n;
2✔
751
        unsigned remaining = 0;
2✔
752
        int r;
2✔
753

754
        r = ensure_targets(bus, argv + 1, &targets);
2✔
755
        if (r < 0)
2✔
756
                return log_error_errno(r, "Failed to find targets: %m");
×
757

758
        r = parse_targets(targets, &n, &target_paths, /* ret_versions= */ NULL);
2✔
759
        if (r < 0)
2✔
760
                return log_error_errno(r, "Failed to parse targets: %m");
×
761

762
        table = table_new("target", "update");
2✔
763
        if (!table)
2✔
764
                return log_oom();
×
765

766
        (void) table_set_sort(table, 0);
2✔
767

768
        r = sd_event_default(&event);
2✔
769
        if (r < 0)
2✔
770
                return log_error_errno(r, "Failed to get event loop: %m");
×
771

772
        r = sd_bus_attach_event(bus, event, 0);
2✔
773
        if (r < 0)
2✔
774
                return log_error_errno(r, "Failed to attach bus to event loop: %m");
×
775

776
        r = sd_event_set_signal_exit(event, true);
2✔
777
        if (r < 0)
2✔
778
                return log_error_errno(r, "Failed to set up interrupt handler: %m");
×
779

780
        for (size_t i = 0; i < n; i++) {
6✔
781
                _cleanup_(operation_freep) Operation *op = NULL;
×
782
                op = operation_new(table, bus, &remaining, target_paths[i], targets[i]);
4✔
783
                if (!op)
4✔
784
                        return log_oom();
×
785

786
                r = sd_bus_call_method_async(bus, NULL, bus_sysupdate_mgr->destination, target_paths[i], SYSUPDATE_TARGET_INTERFACE, "CheckNew", check_finished, op, NULL);
4✔
787
                if (r < 0)
4✔
788
                        return log_error_errno(r, "Failed to call CheckNew for target %s: %m", targets[i]);
×
789
                TAKE_PTR(op);
4✔
790

791
                remaining++;
4✔
792
        }
793

794
        r = sd_event_loop(event);
2✔
795
        if (r < 0)
2✔
796
                return log_error_errno(r, "Failed to start event loop: %m");
×
797

798
        if (table_isempty(table)) {
4✔
799
                log_info("No updates available.");
2✔
800
                return 0;
2✔
801
        }
802

803
        return table_print_with_pager(table, SD_JSON_FORMAT_OFF, arg_pager_flags, arg_legend);
×
804
}
805

806
#define UPDATE_PROGRESS_FAILED INT_MIN
807
#define UPDATE_PROGRESS_DONE INT_MAX
808
/* Make sure it doesn't overlap w/ errno values */
809
assert_cc(UPDATE_PROGRESS_FAILED < -ERRNO_MAX);
810

811
static int update_render_progress(sd_event_source *source, void *userdata) {
15✔
812
        OrderedHashmap *map = ASSERT_PTR(userdata);
15✔
813
        const char *target;
15✔
814
        void *p;
15✔
815
        unsigned total;
15✔
816
        size_t n;
15✔
817
        bool exiting;
15✔
818

819
        exiting = sd_event_get_state(sd_event_source_get_event(source)) == SD_EVENT_EXITING;
15✔
820

821
        total = 0;
15✔
822
        n = ordered_hashmap_size(map);
15✔
823

824
        if (n == 0)
15✔
825
                return 0;
15✔
826

827
        /* We're outputting lots of small strings to STDERR, which is unbuffered by default. So let's turn
828
         * on full buffering, so we pass this all to the TTY in one go, to make things more efficient */
829
        WITH_BUFFERED_STDERR;
11✔
830

831
        if (!terminal_is_dumb()) {
11✔
832
                for (size_t i = 0; i <= n; i++)
×
833
                        fputs("\n", stderr); /* Possibly scroll the terminal to make room (including total) */
×
834

835
                fprintf(stderr, "\e[%zuF", n+1); /* Go back */
×
836

837
                fputs("\e7", stderr); /* Save cursor position */
×
838
                fputs("\e[?25l", stderr); /* Hide cursor */
×
839
        }
840

841
        ORDERED_HASHMAP_FOREACH_KEY(p, target, map) {
22✔
842
                int progress = PTR_TO_INT(p);
11✔
843

844
                if (progress == UPDATE_PROGRESS_FAILED) {
11✔
845
                        clear_progress_bar_unbuffered(target);
×
846
                        fprintf(stderr, "%s: %s Unknown failure\n", target, RED_CROSS_MARK());
×
847
                        total += 100;
×
848
                } else if (progress == -EALREADY) {
11✔
849
                        clear_progress_bar_unbuffered(target);
×
850
                        fprintf(stderr, "%s: %s Already up-to-date\n", target, GREEN_CHECK_MARK());
×
851
                        n--; /* Don't consider this target in the total */
×
852
                } else if (progress < 0) {
11✔
853
                        clear_progress_bar_unbuffered(target);
×
854
                        fprintf(stderr, "%s: %s %s\n", target, RED_CROSS_MARK(), STRERROR(progress));
×
855
                        total += 100;
×
856
                } else if (progress == UPDATE_PROGRESS_DONE) {
11✔
857
                        clear_progress_bar_unbuffered(target);
2✔
858
                        fprintf(stderr, "%s: %s Done\n", target, GREEN_CHECK_MARK());
2✔
859
                        total += 100;
2✔
860
                } else {
861
                        draw_progress_bar_unbuffered(target, progress);
9✔
862
                        fputs("\n", stderr);
9✔
863
                        total += progress;
9✔
864
                }
865
        }
866

867
        if (n > 1) {
11✔
868
                if (exiting)
×
869
                        clear_progress_bar_unbuffered(target);
×
870
                else {
871
                        draw_progress_bar_unbuffered("Total", (double) total / n);
×
872
                        if (terminal_is_dumb())
×
873
                                fputs("\n", stderr);
×
874
                }
875
        }
876

877
        if (!terminal_is_dumb()) {
11✔
878
                if (exiting)
×
879
                        fputs("\e[?25h", stderr); /* Show cursor again */
×
880
                else
881
                        fputs("\e8", stderr); /* Restore cursor position */
×
882
        } else if (!exiting)
11✔
883
                fputs("------\n", stderr);
9✔
884

885
        return 0;
11✔
886
}
887

888
static int update_properties_changed(sd_bus_message *m, void *userdata, sd_bus_error *error) {
5✔
889
        Operation *op = ASSERT_PTR(userdata);
5✔
890
        OrderedHashmap *map = ASSERT_PTR(op->userdata);
5✔
891
        const char *interface;
5✔
892
        uint32_t progress = UINT32_MAX;
5✔
893
        static const struct bus_properties_map prop_map[] = {
5✔
894
                { "Progress", "u", NULL, 0 },
895
                {}
896
        };
897
        int r;
5✔
898

899
        assert(m);
5✔
900

901
        r = sd_bus_message_read(m, "s", &interface);
5✔
902
        if (r < 0) {
5✔
903
                bus_log_parse_error_debug(r);
×
904
                return 0;
×
905
        }
906

907
        if (!streq(interface, "org.freedesktop.sysupdate1.Job"))
5✔
908
                return 0;
909

910
        r = bus_message_map_all_properties(m, prop_map, /* flags= */ 0, error, &progress);
5✔
911
        if (r < 0)
5✔
912
                return 0; /* map_all_properties does the debug logging internally... */
913

914
        if (progress == UINT_MAX)
5✔
915
                return 0;
916

917
        r = ordered_hashmap_replace(map, op->target_id, INT_TO_PTR((int) progress));
5✔
918
        if (r < 0)
5✔
919
                log_debug_errno(r, "Failed to update hashmap: %m");
×
920
        return 0;
921
}
922

923
static int update_finished(sd_bus_message *m, void *userdata, sd_bus_error *error) {
2✔
924
        _cleanup_(operation_freep) Operation *op = ASSERT_PTR(userdata);
2✔
925
        OrderedHashmap *map = ASSERT_PTR(op->userdata);
2✔
926
        uint64_t id;
2✔
927
        int r, status;
2✔
928

929
        assert(m);
2✔
930

931
        r = sd_bus_message_read(m, "toi", &id, NULL, &status);
2✔
932
        if (r < 0) {
2✔
933
                bus_log_parse_error_debug(r);
×
934
                return 0;
×
935
        }
936

937
        if (id != op->job_id) {
2✔
938
                TAKE_PTR(op);
×
939
                return 0;
×
940
        }
941

942
        if (status == 0) /* success */
2✔
943
                status = UPDATE_PROGRESS_DONE;
2✔
944
        else if (status > 0) /* exit status without errno */
×
945
                status = UPDATE_PROGRESS_FAILED; /* i.e. EXIT_FAILURE */
×
946
        /* else errno */
947

948
        r = ordered_hashmap_replace(map, op->target_id, INT_TO_PTR(status));
2✔
949
        if (r < 0)
2✔
950
                log_debug_errno(r, "Failed to update hashmap: %m");
2✔
951
        return 0;
952
}
953

954
static int update_interrupted(sd_event_source *source, void *userdata) {
×
955
        /* Since the event loop is exiting, we will never receive the JobRemoved
956
         * signal. So, we must free the userdata here. */
957
        _cleanup_(operation_freep) Operation *op = ASSERT_PTR(userdata);
×
958
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
959
        OrderedHashmap *map = ASSERT_PTR(op->userdata);
×
960
        int r;
×
961

962
        r = sd_bus_call_method(op->bus,
×
963
                               bus_sysupdate_mgr->destination,
×
964
                               op->job_path,
×
965
                               "org.freedesktop.sysupdate1.Job",
966
                               "Cancel",
967
                               &error,
968
                               /* ret_reply= */ NULL,
969
                               NULL);
970
        if (r < 0)
×
UNCOV
971
                return log_bus_error(r, &error, NULL, "call Cancel");
×
972

973
        r = ordered_hashmap_replace(map, op->target_id, INT_TO_PTR(-ECANCELED));
×
974
        if (r < 0)
×
UNCOV
975
                log_debug_errno(r, "Failed to update hashmap: %m");
×
976

977
        return 0;
978
}
979

980
static int update_started(sd_bus_message *reply, void *userdata, sd_bus_error *ret_error) {
2✔
981
        _cleanup_(operation_freep) Operation *op = ASSERT_PTR(userdata);
2✔
982
        OrderedHashmap *map = ASSERT_PTR(op->userdata);
2✔
983
        const sd_bus_error *e;
2✔
984
        _cleanup_free_ char *key = NULL;
2✔
985
        const char *new_version, *job_path;
2✔
986
        int r;
2✔
987

988
        assert(reply);
2✔
989

990
        e = sd_bus_message_get_error(reply);
2✔
991
        if (e) {
2✔
UNCOV
992
                r = -sd_bus_error_get_errno(e);
×
993

994
                key = strdup(op->target_id);
×
995
                if (!key)
×
996
                        return log_oom();
×
997
                r = ordered_hashmap_put(map, key, INT_TO_PTR(r));
×
998
                if (r < 0)
×
UNCOV
999
                        return log_error_errno(r, "Failed to update hashmap: %m");
×
1000
                TAKE_PTR(key);
2✔
1001

1002
                return 0;
1003
        }
1004

1005
        r = sd_bus_message_read(reply, "sto", &new_version, &op->job_id, &job_path);
2✔
1006
        if (r < 0)
2✔
UNCOV
1007
                return bus_log_parse_error(r);
×
1008
        op->job_path = strdup(job_path);
2✔
1009
        if (!op->job_path)
2✔
UNCOV
1010
                return log_oom();
×
1011
        if (isempty(new_version))
2✔
UNCOV
1012
                new_version = "latest";
×
1013

1014
        /* Register this job into the hashmap. This will give it a progress bar */
1015
        if (strchr(op->target_id, '@'))
2✔
UNCOV
1016
                key = strdup(op->target_id);
×
1017
        else
1018
                key = strjoin(op->target_id, "@", new_version);
2✔
1019
        if (!key)
2✔
UNCOV
1020
                return log_oom();
×
1021
        r = ordered_hashmap_put(map, key, INT_TO_PTR(0)); /* takes ownership of key */
2✔
1022
        if (r < 0)
2✔
UNCOV
1023
                return log_error_errno(r, "Failed to add target to tracking map: %m");
×
1024
        op->target_id = TAKE_PTR(key); /* just borrowing */
2✔
1025

1026
        /* Cancel the job if the event loop exits */
1027
        r = sd_event_add_exit(op->event, &op->job_interrupt_source, update_interrupted, op);
2✔
1028
        if (r < 0)
2✔
UNCOV
1029
                return log_error_errno(r, "Failed to set up interrupt handler: %m");
×
1030

1031
        /* We need to cancel the job before the final iteration of the renderer runs */
1032
        r = sd_event_source_set_priority(op->job_interrupt_source, SD_EVENT_PRIORITY_IMPORTANT);
2✔
1033
        if (r < 0)
2✔
UNCOV
1034
                return log_error_errno(r, "Failed to set interrupt priority: %m");
×
1035

1036
        /* Register for progress notifications */
1037
        r = sd_bus_match_signal_async(
4✔
1038
                        op->bus,
1039
                        &op->job_properties_slot,
1040
                        bus_sysupdate_mgr->destination,
2✔
1041
                        job_path,
1042
                        "org.freedesktop.DBus.Properties",
1043
                        "PropertiesChanged",
1044
                        update_properties_changed,
1045
                        NULL,
1046
                        op);
1047
        if (r < 0)
2✔
UNCOV
1048
                return log_bus_error(r, NULL, op->target_id, "listen for PropertiesChanged");
×
1049

1050
        TAKE_PTR(op); /* update_finished/update_interrupted take ownership of the data */
2✔
1051

1052
        return 0;
2✔
1053
}
1054

1055
static int do_update(sd_bus *bus, char **targets) {
2✔
1056
        _cleanup_(sd_event_unrefp) sd_event *event = NULL;
2✔
1057
        _cleanup_(sd_event_source_unrefp) sd_event_source *render_exit = NULL;
2✔
1058
        _cleanup_ordered_hashmap_free_ OrderedHashmap *map = NULL;
2✔
1059
        _cleanup_strv_free_ char **versions = NULL, **target_paths = NULL;
2✔
1060
        size_t n;
2✔
1061
        unsigned remaining = 0;
2✔
1062
        void *p;
2✔
1063
        bool did_anything = false;
2✔
1064
        int r;
2✔
1065

1066
        assert(bus);
2✔
1067
        assert(targets);
2✔
1068

1069
        r = parse_targets(targets, &n, &target_paths, &versions);
2✔
1070
        if (r < 0)
2✔
UNCOV
1071
                return log_error_errno(r, "Could not parse targets: %m");
×
1072

1073
        map = ordered_hashmap_new(&string_hash_ops_free);
2✔
1074
        if (!map)
2✔
UNCOV
1075
                return log_oom();
×
1076

1077
        r = sd_event_default(&event);
2✔
1078
        if (r < 0)
2✔
UNCOV
1079
                return log_error_errno(r, "Failed to get event loop: %m");
×
1080

1081
        r = sd_bus_attach_event(bus, event, 0);
2✔
1082
        if (r < 0)
2✔
UNCOV
1083
                return log_error_errno(r, "Failed to attach bus to event loop: %m");
×
1084

1085
        r = sd_event_set_signal_exit(event, true);
2✔
1086
        if (r < 0)
2✔
UNCOV
1087
                return log_error_errno(r, "Failed to set up interrupt handler: %m");
×
1088

1089
        for (size_t i = 0; i < n; i++) {
4✔
UNCOV
1090
                _cleanup_(operation_freep) Operation *op = NULL;
×
1091
                op = operation_new(map, bus, &remaining, target_paths[i], targets[i]);
2✔
1092
                if (!op)
2✔
UNCOV
1093
                        return log_oom();
×
1094

1095
                /* Sign up for notification when the associated job finishes */
1096
                r = bus_match_signal_async(
2✔
1097
                                op->bus, &op->job_finished_slot, bus_sysupdate_mgr, "JobRemoved", update_finished, NULL, op);
1098
                if (r < 0)
2✔
UNCOV
1099
                        return log_bus_error(r, NULL, op->target_id, "listen for JobRemoved");
×
1100

1101
                r = sd_bus_call_method_async(
4✔
1102
                                bus,
1103
                                NULL,
1104
                                bus_sysupdate_mgr->destination,
2✔
1105
                                target_paths[i],
2✔
1106
                                SYSUPDATE_TARGET_INTERFACE,
1107
                                "Update",
1108
                                update_started,
1109
                                op,
1110
                                "st",
1111
                                versions[i],
2✔
1112
                                0LU);
1113
                if (r < 0)
2✔
UNCOV
1114
                        return log_bus_error(r, NULL, targets[i], "call Update");
×
1115
                TAKE_PTR(op);
2✔
1116

1117
                remaining++;
2✔
1118
        }
1119

1120
        /* Set up the rendering */
1121
        r = sd_event_add_post(event, NULL, update_render_progress, map);
2✔
1122
        if (r < 0)
2✔
UNCOV
1123
                return log_error_errno(r, "Failed to add progress rendering callback: %m");
×
1124

1125
        r = sd_event_add_exit(event, &render_exit, update_render_progress, map);
2✔
1126
        if (r < 0)
2✔
UNCOV
1127
                return log_error_errno(r, "Failed to add exit callback: %m");
×
1128

1129
        r = sd_event_source_set_priority(render_exit, SD_EVENT_PRIORITY_IDLE);
2✔
1130
        if (r < 0)
2✔
UNCOV
1131
                return log_error_errno(r, "Failed to set priority of update job");
×
1132

1133
        r = sd_event_loop(event);
2✔
1134
        if (r < 0)
2✔
UNCOV
1135
                return log_error_errno(r, "Failed to start event loop");
×
1136

1137
        ORDERED_HASHMAP_FOREACH(p, map) {
4✔
1138
                r = PTR_TO_INT(p);
2✔
1139
                if (r == -EALREADY)
2✔
UNCOV
1140
                        continue;
×
1141
                if (r == UPDATE_PROGRESS_FAILED)
2✔
UNCOV
1142
                        return EXIT_FAILURE;
×
1143
                if (r < 0)
2✔
1144
                        return r;
1145

1146
                did_anything = true;
1147
        }
1148

1149
        return did_anything ? 1 : 0;
2✔
1150
}
1151

1152
static int verb_update(int argc, char **argv, void *userdata) {
2✔
1153
        sd_bus *bus = ASSERT_PTR(userdata);
2✔
1154
        _cleanup_strv_free_ char **targets = NULL;
2✔
1155
        bool did_anything = false;
2✔
1156
        int r;
2✔
1157

1158
        r = ensure_targets(bus, argv + 1, &targets);
2✔
1159
        if (r < 0)
2✔
UNCOV
1160
                return log_error_errno(r, "Could not find targets: %m");
×
1161

1162
        r = do_update(bus, targets);
2✔
1163
        if (r < 0)
2✔
1164
                return r;
1165
        if (r > 0)
2✔
1166
                did_anything = true;
2✔
1167

1168
        if (!arg_reboot)
2✔
1169
                return 0;
1170

1171
        if (did_anything)
×
UNCOV
1172
                return reboot_now();
×
1173

UNCOV
1174
        log_info("Nothing was updated... skipping reboot.");
×
1175
        return 0;
1176
}
1177

1178
static int do_vacuum(sd_bus *bus, const char *target, const char *path) {
×
1179
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
1180
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
×
1181
        uint32_t count, disabled;
×
UNCOV
1182
        int r;
×
1183

1184
        r = sd_bus_call_method(bus, bus_sysupdate_mgr->destination, path, SYSUPDATE_TARGET_INTERFACE, "Vacuum", &error, &reply, NULL);
×
1185
        if (r < 0)
×
UNCOV
1186
                return log_bus_error(r, &error, target, "call Vacuum");
×
1187

1188
        r = sd_bus_message_read(reply, "uu", &count, &disabled);
×
1189
        if (r < 0)
×
UNCOV
1190
                return bus_log_parse_error(r);
×
1191

1192
        if (count > 0 && disabled > 0)
×
UNCOV
1193
                log_info("Deleted %u instance(s) and %u disabled transfer(s) of %s.",
×
1194
                         count, disabled, target);
1195
        else if (count > 0)
×
1196
                log_info("Deleted %u instance(s) of %s.", count, target);
×
1197
        else if (disabled > 0)
×
UNCOV
1198
                log_info("Deleted %u disabled transfer(s) of %s.", disabled, target);
×
1199
        else
UNCOV
1200
                log_info("Found nothing to delete for %s.", target);
×
1201

UNCOV
1202
        return count + disabled > 0 ? 1 : 0;
×
1203
}
1204

1205
static int verb_vacuum(int argc, char **argv, void *userdata) {
×
1206
        sd_bus *bus = ASSERT_PTR(userdata);
×
1207
        _cleanup_strv_free_ char **targets = NULL, **target_paths = NULL;
×
1208
        size_t n;
×
UNCOV
1209
        int r;
×
1210

1211
        r = ensure_targets(bus, argv + 1, &targets);
×
1212
        if (r < 0)
×
UNCOV
1213
                return log_error_errno(r, "Failed to find targets: %m");
×
1214

1215
        r = parse_targets(targets, &n, &target_paths, /* ret_versions= */ NULL);
×
1216
        if (r < 0)
×
UNCOV
1217
                return log_error_errno(r, "Failed to parse targets: %m");
×
1218

1219
        for (size_t i = 0; i < n; i++) {
×
1220
                r = do_vacuum(bus, targets[i], target_paths[i]);
×
UNCOV
1221
                if (r < 0)
×
1222
                        return r;
1223
        }
1224
        return 0;
1225
}
1226

1227
typedef struct Feature {
1228
        char *name;
1229
        char *description;
1230
        bool enabled;
1231
        char *documentation;
1232
        char **transfers;
1233
} Feature;
1234

1235
static void feature_done(Feature *f) {
×
1236
        assert(f);
×
1237
        f->name = mfree(f->name);
×
1238
        f->description = mfree(f->description);
×
1239
        f->documentation = mfree(f->documentation);
×
1240
        f->transfers = strv_free(f->transfers);
×
UNCOV
1241
}
×
1242

1243
static int describe_feature(sd_bus *bus, const char *feature, Feature *ret) {
×
1244
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
1245
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
×
1246
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
×
1247
        _cleanup_(feature_done) Feature f = {};
×
1248
        char *json;
×
UNCOV
1249
        int r;
×
1250

UNCOV
1251
        static const sd_json_dispatch_field dispatch_table[] = {
×
1252
                { "name",             SD_JSON_VARIANT_STRING,  sd_json_dispatch_string,  offsetof(Feature, name),          SD_JSON_MANDATORY },
1253
                { "description",      SD_JSON_VARIANT_STRING,  sd_json_dispatch_string,  offsetof(Feature, description),   0                 },
1254
                { "enabled",          SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(Feature, enabled),       SD_JSON_MANDATORY },
1255
                { "documentationUrl", SD_JSON_VARIANT_STRING,  sd_json_dispatch_string,  offsetof(Feature, documentation), 0                 },
1256
                { "transfers",        SD_JSON_VARIANT_ARRAY,   sd_json_dispatch_strv,    offsetof(Feature, transfers),     0                 },
1257
                {}
1258
        };
1259

1260
        assert(bus);
×
1261
        assert(feature);
×
UNCOV
1262
        assert(ret);
×
1263

1264
        r = sd_bus_call_method(bus,
×
UNCOV
1265
                               bus_sysupdate_mgr->destination,
×
1266
                               SYSUPDATE_HOST_PATH,
1267
                               SYSUPDATE_TARGET_INTERFACE,
1268
                               "DescribeFeature",
1269
                               &error,
1270
                               &reply,
1271
                               "st",
1272
                               feature,
1273
                               UINT64_C(0));
1274
        if (r < 0)
×
UNCOV
1275
                return log_bus_error(r, &error, "host", "lookup feature");
×
1276

1277
        r = sd_bus_message_read_basic(reply, 's', &json);
×
1278
        if (r < 0)
×
UNCOV
1279
                return bus_log_parse_error(r);
×
1280

1281
        r = sd_json_parse(json, 0, &v, NULL, NULL);
×
1282
        if (r < 0)
×
UNCOV
1283
                return log_error_errno(r, "Failed to parse JSON: %m");
×
1284

1285
        r = sd_json_dispatch(v, dispatch_table, 0, &f);
×
1286
        if (r < 0)
×
UNCOV
1287
                return log_error_errno(r, "Failed to dispatch JSON: %m");
×
1288

1289
        *ret = TAKE_STRUCT(f);
×
UNCOV
1290
        return 0;
×
1291
}
1292

1293
static int list_features(sd_bus *bus) {
×
1294
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
1295
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
×
1296
        _cleanup_strv_free_ char **features = NULL;
×
1297
        _cleanup_(table_unrefp) Table *table = NULL;
×
UNCOV
1298
        int r;
×
1299

UNCOV
1300
        assert(bus);
×
1301

1302
        table = table_new("", "feature", "description");
×
1303
        if (!table)
×
UNCOV
1304
                return log_oom();
×
1305

1306
        r = sd_bus_call_method(bus,
×
UNCOV
1307
                               bus_sysupdate_mgr->destination,
×
1308
                               SYSUPDATE_HOST_PATH,
1309
                               SYSUPDATE_TARGET_INTERFACE,
1310
                               "ListFeatures",
1311
                               &error,
1312
                               &reply,
1313
                               "t",
1314
                               UINT64_C(0));
1315
        if (r < 0)
×
UNCOV
1316
                return log_bus_error(r, &error, "host", "lookup feature");
×
1317

1318
        r = sd_bus_message_read_strv(reply, &features);
×
1319
        if (r < 0)
×
UNCOV
1320
                return bus_log_parse_error(r);
×
1321

1322
        STRV_FOREACH(feature, features) {
×
1323
                _cleanup_(feature_done) Feature f = {};
×
UNCOV
1324
                _cleanup_free_ char *name_link = NULL;
×
1325

1326
                r = describe_feature(bus, *feature, &f);
×
UNCOV
1327
                if (r < 0)
×
1328
                        return r;
1329

1330
                if (urlify_enabled() && f.documentation) {
×
1331
                        name_link = strjoin(f.name, glyph(GLYPH_EXTERNAL_LINK));
×
1332
                        if (!name_link)
×
UNCOV
1333
                                return log_oom();
×
1334
                }
1335

UNCOV
1336
                r = table_add_many(table,
×
1337
                                   TABLE_BOOLEAN_CHECKMARK, f.enabled,
1338
                                   TABLE_SET_COLOR, ansi_highlight_green_red(f.enabled),
1339
                                   TABLE_STRING, name_link ?: f.name,
1340
                                   TABLE_SET_URL, f.documentation,
1341
                                   TABLE_STRING, f.description);
1342
                if (r < 0)
×
UNCOV
1343
                        return table_log_add_error(r);
×
1344
        }
1345

UNCOV
1346
        return table_print_with_pager(table, SD_JSON_FORMAT_OFF, arg_pager_flags, arg_legend);
×
1347
}
1348

1349
static int verb_features(int argc, char **argv, void *userdata) {
×
1350
        sd_bus *bus = ASSERT_PTR(userdata);
×
1351
        _cleanup_(table_unrefp) Table *table = NULL;
×
1352
        _cleanup_(feature_done) Feature f = {};
×
UNCOV
1353
        int r;
×
1354

1355
        if (argc == 1)
×
UNCOV
1356
                return list_features(bus);
×
1357

1358
        table = table_new_vertical();
×
1359
        if (!table)
×
UNCOV
1360
                return log_oom();
×
1361

1362
        r = describe_feature(bus, argv[1], &f);
×
UNCOV
1363
        if (r < 0)
×
1364
                return r;
1365

UNCOV
1366
        r = table_add_many(table,
×
1367
                           TABLE_FIELD, "Name",
1368
                           TABLE_STRING, f.name,
1369
                           TABLE_FIELD, "Enabled",
1370
                           TABLE_BOOLEAN, f.enabled);
1371
        if (r < 0)
×
UNCOV
1372
                return table_log_add_error(r);
×
1373

1374
        if (f.description) {
×
1375
                r = table_add_many(table, TABLE_FIELD, "Description", TABLE_STRING, f.description);
×
1376
                if (r < 0)
×
UNCOV
1377
                        return table_log_add_error(r);
×
1378
        }
1379

1380
        if (f.documentation) {
×
UNCOV
1381
                r = table_add_many(table,
×
1382
                                   TABLE_FIELD, "Documentation",
1383
                                   TABLE_STRING, f.documentation,
1384
                                   TABLE_SET_URL, f.documentation);
1385
                if (r < 0)
×
UNCOV
1386
                        return table_log_add_error(r);
×
1387
        }
1388

1389
        if (!strv_isempty(f.transfers)) {
×
1390
                r = table_add_many(table, TABLE_FIELD, "Transfers", TABLE_STRV_WRAPPED, f.transfers);
×
1391
                if (r < 0)
×
UNCOV
1392
                        return table_log_add_error(r);
×
1393
        }
1394

UNCOV
1395
        return table_print_with_pager(table, SD_JSON_FORMAT_OFF, arg_pager_flags, false);
×
1396
}
1397

1398
static int verb_enable(int argc, char **argv, void *userdata) {
×
1399
        sd_bus *bus = ASSERT_PTR(userdata);
×
1400
        bool did_anything = false, enable;
×
1401
        char **features;
×
UNCOV
1402
        int r;
×
1403

1404
        enable = streq(argv[0], "enable");
×
UNCOV
1405
        features = strv_skip(argv, 1);
×
1406

1407
        STRV_FOREACH(feature, features) {
×
UNCOV
1408
                _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
1409

1410
                r = sd_bus_call_method(bus,
×
UNCOV
1411
                                       bus_sysupdate_mgr->destination,
×
1412
                                       SYSUPDATE_HOST_PATH,
1413
                                       SYSUPDATE_TARGET_INTERFACE,
1414
                                       "SetFeatureEnabled",
1415
                                       &error,
1416
                                       /* ret_reply= */ NULL,
1417
                                       "sit",
1418
                                       *feature,
1419
                                       (int) enable,
1420
                                       UINT64_C(0));
1421
                if (r < 0)
×
UNCOV
1422
                        return log_bus_error(r, &error, "host", "call SetFeatureEnabled");
×
1423
        }
1424

UNCOV
1425
        if (!arg_now) /* We weren't asked to apply the changes, so we're done! */
×
1426
                return 0;
1427

1428
        if (enable) {
×
1429
                _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
1430
                _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
×
1431
                _cleanup_free_ char *target = NULL;
×
UNCOV
1432
                char *version = NULL;
×
1433

1434
                /* We're downloading the new feature into the "current" version, which is either going to be
1435
                 * the currently booted version or it's going to be a pending update that has already been
1436
                 * installed and is just waiting for us to reboot into it. */
1437

1438
                r = sd_bus_call_method(bus,
×
UNCOV
1439
                                       bus_sysupdate_mgr->destination,
×
1440
                                       SYSUPDATE_HOST_PATH,
1441
                                       SYSUPDATE_TARGET_INTERFACE,
1442
                                       "GetVersion",
1443
                                       &error,
1444
                                       &reply,
1445
                                       NULL);
1446
                if (r < 0)
×
UNCOV
1447
                        return log_bus_error(r, &error, "host", "get current version");
×
1448

1449
                r = sd_bus_message_read_basic(reply, 's', &version);
×
1450
                if (r < 0)
×
UNCOV
1451
                        return bus_log_parse_error(r);
×
1452

1453
                target = strjoin("host@", version);
×
1454
                if (!target)
×
UNCOV
1455
                        return log_oom();
×
1456

UNCOV
1457
                r = do_update(bus, STRV_MAKE(target));
×
1458
        } else
1459
                r = do_vacuum(bus, "host", SYSUPDATE_HOST_PATH);
×
UNCOV
1460
        if (r < 0)
×
1461
                return r;
1462
        if (r > 0)
×
UNCOV
1463
                did_anything = true;
×
1464

1465
        if (arg_reboot && did_anything)
×
1466
                return reboot_now();
×
1467
        else if (did_anything)
×
UNCOV
1468
                log_info("Feature(s) %s.", enable ? "downloaded" : "deleted");
×
1469
        else
UNCOV
1470
                log_info("Nothing %s%s.",
×
1471
                         enable ? "downloaded" : "deleted",
1472
                         arg_reboot ? ", skipping reboot" :"");
1473

1474
        return 0;
1475
}
1476

1477
static int help(void) {
×
1478
        _cleanup_free_ char *link = NULL;
×
UNCOV
1479
        int r;
×
1480

1481
        r = terminal_urlify_man("updatectl", "1", &link);
×
1482
        if (r < 0)
×
UNCOV
1483
                return log_oom();
×
1484

UNCOV
1485
        printf("%1$s [OPTIONS...] [VERSION]\n"
×
1486
               "\n%5$sManage system updates.%6$s\n"
1487
               "\n%3$sCommands:%4$s\n"
1488
               "  list [TARGET[@VERSION]]       List available targets and versions\n"
1489
               "  check [TARGET...]             Check for updates\n"
1490
               "  update [TARGET[@VERSION]...]  Install updates\n"
1491
               "  vacuum [TARGET...]            Clean up old updates\n"
1492
               "  features [FEATURE]            List and inspect optional features on host OS\n"
1493
               "  enable FEATURE...             Enable optional feature on host OS\n"
1494
               "  disable FEATURE...            Disable optional feature on host OS\n"
1495
               "  -h --help                     Show this help\n"
1496
               "     --version                  Show package version\n"
1497
               "\n%3$sOptions:%4$s\n"
1498
               "     --reboot             Reboot after updating to newer version\n"
1499
               "     --offline            Do not fetch metadata from the network\n"
1500
               "     --now                Download/delete resources immediately\n"
1501
               "  -H --host=[USER@]HOST   Operate on remote host\n"
1502
               "     --no-pager           Do not pipe output into a pager\n"
1503
               "     --no-legend          Do not show the headers and footers\n"
1504
               "\nSee the %2$s for details.\n"
1505
               , program_invocation_short_name
1506
               , link
1507
               , ansi_underline(), ansi_normal()
1508
               , ansi_highlight(), ansi_normal()
1509
        );
1510

1511
        return 0;
1512
}
1513

1514
static int parse_argv(int argc, char *argv[]) {
10✔
1515

1516
        enum {
10✔
1517
                ARG_VERSION = 0x100,
1518
                ARG_NO_PAGER,
1519
                ARG_NO_LEGEND,
1520
                ARG_REBOOT,
1521
                ARG_OFFLINE,
1522
                ARG_NOW,
1523
        };
1524

1525
        static const struct option options[] = {
10✔
1526
                { "help",      no_argument,       NULL, 'h'             },
1527
                { "version",   no_argument,       NULL, ARG_VERSION     },
1528
                { "no-pager",  no_argument,       NULL, ARG_NO_PAGER    },
1529
                { "no-legend", no_argument,       NULL, ARG_NO_LEGEND   },
1530
                { "host",      required_argument, NULL, 'H'             },
1531
                { "reboot",    no_argument,       NULL, ARG_REBOOT      },
1532
                { "offline",   no_argument,       NULL, ARG_OFFLINE     },
1533
                { "now",       no_argument,       NULL, ARG_NOW         },
1534
                {}
1535
        };
1536

1537
        int c;
10✔
1538

1539
        assert(argc >= 0);
10✔
1540
        assert(argv);
10✔
1541

1542
        while ((c = getopt_long(argc, argv, "hH:", options, NULL)) >= 0) {
10✔
UNCOV
1543
                switch (c) {
×
1544

1545
                case 'h':
×
UNCOV
1546
                        return help();
×
1547

1548
                case ARG_VERSION:
×
UNCOV
1549
                        return version();
×
1550

1551
                case ARG_NO_PAGER:
×
1552
                        arg_pager_flags |= PAGER_DISABLE;
×
UNCOV
1553
                        break;
×
1554

1555
                case ARG_NO_LEGEND:
×
1556
                        arg_legend = false;
×
UNCOV
1557
                        break;
×
1558

1559
                case 'H':
×
1560
                        arg_transport = BUS_TRANSPORT_REMOTE;
×
1561
                        arg_host = optarg;
×
UNCOV
1562
                        break;
×
1563

1564
                case ARG_REBOOT:
×
1565
                        arg_reboot = true;
×
UNCOV
1566
                        break;
×
1567

1568
                case ARG_OFFLINE:
×
1569
                        arg_offline = true;
×
UNCOV
1570
                        break;
×
1571

1572
                case ARG_NOW:
×
1573
                        arg_now = true;
×
UNCOV
1574
                        break;
×
1575

1576
                case '?':
1577
                        return -EINVAL;
1578

1579
                default:
×
UNCOV
1580
                        assert_not_reached();
×
1581
                }
1582
        }
1583

1584
        return 1;
1585
}
1586

1587
static int run(int argc, char *argv[]) {
10✔
1588
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
10✔
1589
        int r;
10✔
1590

1591
        static const Verb verbs[] = {
10✔
1592
                { "list",     VERB_ANY, 2,        VERB_DEFAULT|VERB_ONLINE_ONLY, verb_list     },
1593
                { "check",    VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY,              verb_check    },
1594
                { "update",   VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY,              verb_update   },
1595
                { "vacuum",   VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY,              verb_vacuum   },
1596
                { "features", VERB_ANY, 2,        VERB_ONLINE_ONLY,              verb_features },
1597
                { "enable",   2,        VERB_ANY, VERB_ONLINE_ONLY,              verb_enable   },
1598
                { "disable",  2,        VERB_ANY, VERB_ONLINE_ONLY,              verb_enable   },
1599
                {}
1600
        };
1601

1602
        setlocale(LC_ALL, "");
10✔
1603
        log_setup();
10✔
1604

1605
        (void) signal(SIGWINCH, columns_lines_cache_reset);
10✔
1606

1607
        r = parse_argv(argc, argv);
10✔
1608
        if (r <= 0)
10✔
1609
                return r;
1610

1611
        r = bus_connect_transport(arg_transport, arg_host, RUNTIME_SCOPE_SYSTEM, &bus);
10✔
1612
        if (r < 0)
10✔
UNCOV
1613
                return bus_log_connect_error(r, arg_transport, RUNTIME_SCOPE_SYSTEM);
×
1614

1615
        if (arg_transport == BUS_TRANSPORT_LOCAL)
10✔
1616
                polkit_agent_open();
10✔
1617

1618
        return dispatch_verb(argc, argv, verbs, bus);
10✔
1619
}
1620

1621
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);
10✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc