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

systemd / systemd / 13877892945

15 Mar 2025 08:56PM UTC coverage: 71.915% (+0.2%) from 71.757%
13877892945

push

github

web-flow
Fix bootctl status to not print strange glyphs in logs (#36745)

146 of 198 new or added lines in 57 files covered. (73.74%)

153 existing lines in 28 files now uncovered.

296065 of 411690 relevant lines covered (71.91%)

715276.25 hits per line

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

55.08
/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-json.h"
8

9
#include "build.h"
10
#include "bus-error.h"
11
#include "bus-label.h"
12
#include "bus-locator.h"
13
#include "bus-map-properties.h"
14
#include "bus-util.h"
15
#include "conf-files.h"
16
#include "conf-parser.h"
17
#include "errno-list.h"
18
#include "fd-util.h"
19
#include "fileio.h"
20
#include "format-table.h"
21
#include "fs-util.h"
22
#include "json-util.h"
23
#include "main-func.h"
24
#include "os-util.h"
25
#include "pager.h"
26
#include "path-util.h"
27
#include "pretty-print.h"
28
#include "strv.h"
29
#include "sysupdate-update-set-flags.h"
30
#include "sysupdate-util.h"
31
#include "terminal-util.h"
32
#include "verbs.h"
33

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

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

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

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

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

61
typedef struct Operation {
62
        void *userdata;
63

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

250
                n++;
10✔
251
        }
252

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

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

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

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

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

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

287
        assert(bus);
2✔
288

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

411
        assert(reply);
10✔
412

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

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

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

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

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

440
        return 0;
441
}
442

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

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

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

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

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

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

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

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

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

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

509
                remaining++;
10✔
510
        }
511

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

664
        assert(reply);
×
665

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

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

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

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

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

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

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

706
        return 0;
707
}
708

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

715
        assert(reply);
4✔
716

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

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

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

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

743
        return 0;
×
744
}
745

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

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

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

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

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

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

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

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

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

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

792
                remaining++;
4✔
793
        }
794

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

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

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

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

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

820
        exiting = sd_event_get_state(sd_event_source_get_event(source)) == SD_EVENT_EXITING;
18✔
821

822
        total = 0;
18✔
823
        n = ordered_hashmap_size(map);
18✔
824

825
        if (n == 0)
18✔
826
                return 0;
18✔
827

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

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

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

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

842
        ORDERED_HASHMAP_FOREACH_KEY(p, target, map) {
28✔
843
                int progress = PTR_TO_INT(p);
14✔
844

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

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

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

886
        return 0;
14✔
887
}
888

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

900
        assert(m);
8✔
901

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

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

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

915
        if (progress == UINT_MAX)
8✔
916
                return 0;
917

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

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

930
        assert(m);
2✔
931

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

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

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

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

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

963
        r = sd_bus_call_method(op->bus,
×
964
                               bus_sysupdate_mgr->destination,
×
965
                               op->job_path,
×
966
                               "org.freedesktop.sysupdate1.Job",
967
                               "Cancel",
968
                               &error, /* reply= */ NULL,
969
                               NULL);
970
        if (r < 0)
×
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)
×
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✔
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)
×
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✔
1007
                return bus_log_parse_error(r);
×
1008
        op->job_path = strdup(job_path);
2✔
1009
        if (!op->job_path)
2✔
1010
                return log_oom();
×
1011
        if (isempty(new_version))
2✔
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✔
1016
                key = strdup(op->target_id);
×
1017
        else
1018
                key = strjoin(op->target_id, "@", new_version);
2✔
1019
        if (!key)
2✔
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✔
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✔
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✔
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✔
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✔
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✔
1075
                return log_oom();
×
1076

1077
        r = sd_event_default(&event);
2✔
1078
        if (r < 0)
2✔
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✔
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✔
1087
                return log_error_errno(r, "Failed to set up interrupt handler: %m");
×
1088

1089
        for (size_t i = 0; i < n; i++) {
4✔
1090
                _cleanup_(operation_freep) Operation *op = NULL;
×
1091
                op = operation_new(map, bus, &remaining, target_paths[i], targets[i]);
2✔
1092
                if (!op)
2✔
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✔
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✔
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✔
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✔
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✔
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✔
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✔
1140
                        continue;
×
1141
                if (r == UPDATE_PROGRESS_FAILED)
2✔
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;
×
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✔
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)
×
1172
                return reboot_now();
×
1173

1174
        log_info("Nothing was updated... skipping reboot.");
2✔
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;
×
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)
×
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)
×
1190
                return bus_log_parse_error(r);
×
1191

1192
        if (count > 0 && disabled > 0)
×
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)
×
1198
                log_info("Deleted %u disabled transfer(s) of %s.", disabled, target);
×
1199
        else
1200
                log_info("Found nothing to delete for %s.", target);
×
1201

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;
×
1209
        int r;
×
1210

1211
        r = ensure_targets(bus, argv + 1, &targets);
×
1212
        if (r < 0)
×
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)
×
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]);
×
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);
×
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;
×
1249
        int r;
×
1250

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);
×
1262
        assert(ret);
×
1263

1264
        r = sd_bus_call_method(bus,
×
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)
×
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)
×
1279
                return bus_log_parse_error(r);
×
1280

1281
        r = sd_json_parse(json, 0, &v, NULL, NULL);
×
1282
        if (r < 0)
×
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)
×
1287
                return log_error_errno(r, "Failed to dispatch JSON: %m");
×
1288

1289
        *ret = TAKE_STRUCT(f);
×
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;
×
1298
        int r;
×
1299

1300
        assert(bus);
×
1301

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

1306
        r = sd_bus_call_method(bus,
×
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)
×
1316
                return log_bus_error(r, &error, "host", "lookup feature");
×
1317

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

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

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

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

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)
×
1343
                        return table_log_add_error(r);
×
1344
        }
1345

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 = {};
×
1353
        int r;
×
1354

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

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

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

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)
×
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)
×
1377
                        return table_log_add_error(r);
×
1378
        }
1379

1380
        if (f.documentation) {
×
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)
×
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)
×
1392
                        return table_log_add_error(r);
×
1393
        }
1394

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;
×
1402
        int r;
×
1403

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

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

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

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;
×
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,
×
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)
×
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)
×
1451
                        return bus_log_parse_error(r);
×
1452

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

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

1465
        if (arg_reboot && did_anything)
×
1466
                return reboot_now();
×
1467
        else if (did_anything)
×
1468
                log_info("Feature(s) %s.", enable ? "downloaded" : "deleted");
×
1469
        else
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;
×
1479
        int r;
×
1480

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

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✔
1543
                switch (c) {
×
1544

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

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

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

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

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

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

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

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

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

1579
                default:
×
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✔
1613
                return bus_log_connect_error(r, arg_transport, RUNTIME_SCOPE_SYSTEM);
10✔
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);
20✔
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