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

systemd / systemd / 25705508282

11 May 2026 11:07PM UTC coverage: 72.65% (+0.1%) from 72.511%
25705508282

push

github

bluca
firstboot,sysinstall,hostnamed: always show FANCY_NAME=

This makes sure that whenever we want to show the OS name we can show
the fancy name. Thus this moves the escaping/validation of the fancy
name out of hostnamed into generic code, and then makes use of it in
sysinstall,firstboot,prompt-util.

17 of 36 new or added lines in 6 files covered. (47.22%)

2673 existing lines in 72 files now uncovered.

327104 of 450245 relevant lines covered (72.65%)

1200575.51 hits per line

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

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

3
#include <locale.h>
4

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

9
#include "alloc-util.h"
10
#include "build.h"
11
#include "bus-error.h"
12
#include "bus-label.h"
13
#include "bus-locator.h"
14
#include "bus-map-properties.h"
15
#include "bus-util.h"
16
#include "errno-util.h"
17
#include "format-table.h"
18
#include "hashmap.h"
19
#include "json-util.h"
20
#include "main-func.h"
21
#include "options.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) {
96✔
52
        assert(v);
96✔
53

54
        v->version = mfree(v->version);
96✔
55
        v->changelog = strv_free(v->changelog);
96✔
56
        v->flags = 0;
96✔
57
        v->contents_json = mfree(v->contents_json);
96✔
58
}
96✔
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

76
        /* Only used for Acquire()/Install() operations: */
77
        char *acquired_version;
78
        /* The version the user requested, possibly empty */
79
        char *requested_version;
80
} Operation;
81

82
static Operation* operation_free(Operation *p) {
84✔
83
        if (!p)
84✔
84
                return NULL;
85

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

93
        free(p->job_path);
84✔
94
        free(p->acquired_version);
84✔
95
        free(p->requested_version);
84✔
96

97
        sd_event_source_disable_unref(p->job_interrupt_source);
84✔
98
        sd_bus_slot_unref(p->job_properties_slot);
84✔
99
        sd_bus_slot_unref(p->job_finished_slot);
84✔
100

101
        return mfree(p);
84✔
102
}
103

104
DEFINE_TRIVIAL_CLEANUP_FUNC(Operation*, operation_free);
332✔
105

106
static Operation* operation_new(
84✔
107
                void *userdata,
108
                sd_bus *bus,
109
                unsigned *remaining,
110
                const char *target_path,
111
                const char *target_id) {
112

113
        _cleanup_(operation_freep) Operation *o = NULL;
84✔
114

115
        o = new(Operation, 1);
84✔
116
        if (!o)
84✔
117
                return NULL;
118

119
        *o = (Operation) {
168✔
120
                .userdata = userdata,
121
                .bus = bus,
122
                .event = sd_bus_get_event(bus),
84✔
123
                .remaining = remaining,
124
                .target_path = target_path,
125
                .target_id = target_id,
126
        };
127
        return TAKE_PTR(o);
84✔
128
}
129

130
static int ensure_targets(sd_bus *bus, char **argv, char ***ret_targets) {
80✔
131
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
80✔
132
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
80✔
133
        _cleanup_strv_free_ char **targets = NULL;
80✔
134
        int r;
80✔
135

136
        assert(bus);
80✔
137
        assert(ret_targets);
80✔
138

139
        if (strv_isempty(argv)) {
80✔
140
                const char *class, *name, *path;
80✔
141

142
                r = bus_call_method(bus, bus_sysupdate_mgr, "ListTargets", &error, &reply, NULL);
80✔
143
                if (r < 0)
80✔
144
                        return log_error_errno(r, "Failed to call ListTargets: %s", bus_error_message(&error, r));
×
145

146
                r = sd_bus_message_enter_container(reply, 'a', "(sso)");
80✔
147
                if (r < 0)
80✔
148
                        return bus_log_parse_error(r);
×
149

150
                while ((r = sd_bus_message_read(reply, "(sso)", &class, &name, &path)) > 0) {
168✔
151
                        _cleanup_free_ char *id = NULL;
×
152

153
                        if (streq(class, "host"))
88✔
154
                                id = strdup("host");
80✔
155
                        else
156
                                id = strjoin(class, ":", name);
8✔
157
                        if (!id)
88✔
158
                                return log_oom();
×
159

160
                        r = strv_consume(&targets, TAKE_PTR(id));
88✔
161
                        if (r < 0)
88✔
162
                                return log_oom();
×
163
                }
164
                if (r < 0)
80✔
165
                        return bus_log_parse_error(r);
×
166

167
                r = sd_bus_message_exit_container(reply);
80✔
168
                if (r < 0)
80✔
169
                        return bus_log_parse_error(r);
×
170
        } else {
171
                r = strv_extend_strv(&targets, argv, true);
×
172
                if (r < 0)
×
173
                        return log_oom();
×
174
        }
175

176
        *ret_targets = TAKE_PTR(targets);
80✔
177
        return 0;
80✔
178
}
179

180
static int parse_target(
104✔
181
                const char *in,
182
                char **ret_bus_path,
183
                char **ret_version) {
184
        _cleanup_free_ char *id = NULL, *version = NULL, *escaped = NULL, *objpath = NULL;
104✔
185
        const char *s;
104✔
186

187
        /*
188
         * Parses the TARGET[@VERSION] syntax from the command line into
189
         * a bus object path and an optional version number.
190
         */
191

192
        assert(in);
104✔
193
        assert(ret_bus_path);
104✔
194
        assert(ret_version);
104✔
195

196
        s = strrchr(in, '@');
104✔
197
        if (s) {
104✔
198
                version = strdup(s + 1);
8✔
199
                if (!version)
8✔
200
                        return -ENOMEM;
201
                id = strndup(in, s - in);
8✔
202
        } else
203
                id = strdup(in);
96✔
204
        if (!id)
104✔
205
                return -ENOMEM;
206

207
        escaped = bus_label_escape(id);
104✔
208
        if (!escaped)
104✔
209
                return -ENOMEM;
210

211
        objpath = strjoin("/org/freedesktop/sysupdate1/target/", escaped);
104✔
212
        if (!objpath)
104✔
213
                return -ENOMEM;
214

215
        *ret_bus_path = TAKE_PTR(objpath);
104✔
216
        *ret_version = TAKE_PTR(version);
104✔
217
        return 0;
104✔
218
}
219

220
static int parse_targets(
80✔
221
                char **targets,
222
                size_t *ret_n,
223
                char ***ret_bus_paths,
224
                char ***ret_versions) {
225
        _cleanup_strv_free_ char **bus_paths = NULL;
80✔
226
        _cleanup_strv_free_ char **versions = NULL;
80✔
227
        size_t n = 0;
80✔
228
        int r;
80✔
229

230
        assert(ret_bus_paths);
80✔
231
        assert(ret_n);
80✔
232

233
        if (strv_isempty(targets))
80✔
234
                return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "No targets found.");
×
235

236
        STRV_FOREACH(id, targets) {
168✔
237
                _cleanup_free_ char *bus_path = NULL, *version = NULL;
88✔
238

239
                r = parse_target(*id, &bus_path, &version);
88✔
240
                if (r < 0)
88✔
241
                        return log_oom();
×
242

243
                if (version && !ret_versions)
88✔
244
                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
245
                                               "Unexpected version specifier in target: %s",
246
                                               *id);
247

248
                r = strv_extend(&bus_paths, strempty(bus_path));
88✔
249
                if (r < 0)
88✔
250
                        return log_oom();
×
251

252
                r = strv_extend(&versions, strempty(version));
176✔
253
                if (r < 0)
88✔
254
                        return log_oom();
×
255

256
                n++;
88✔
257
        }
258

259
        *ret_n = n;
80✔
260
        *ret_bus_paths = TAKE_PTR(bus_paths);
80✔
261
        if (ret_versions)
80✔
262
                *ret_versions = TAKE_PTR(versions);
28✔
263
        return 0;
264
}
265

266
static int log_bus_error(int r, const sd_bus_error *error, const char *target, const char *action) {
×
267
        assert(action);
×
268

269
        if (r == 0) {
×
270
                assert(sd_bus_error_is_set(error));
×
271
                r = sd_bus_error_get_errno(error);
×
272
        }
273

274
        if (sd_bus_error_has_name(error, SD_BUS_ERROR_UNKNOWN_OBJECT)) {
×
275
                if (target)
×
276
                        return log_error_errno(r, "Invalid target: %s", target);
×
277
                return log_error_errno(r, "Invalid target");
×
278
        }
279

280
        if (target)
×
281
                return log_error_errno(r, "Failed to %s for '%s': %s", action, target,
×
282
                                       bus_error_message(error, r));
283
        return log_error_errno(r, "Failed to %s: %s", action, bus_error_message(error, r));
×
284
}
285

286
static int list_targets(sd_bus *bus) {
44✔
287
        _cleanup_(table_unrefp) Table *table = NULL;
44✔
288
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
44✔
289
        _cleanup_strv_free_ char **targets = NULL, **target_paths = NULL;
44✔
290
        size_t n;
44✔
291
        int r;
44✔
292

293
        assert(bus);
44✔
294

295
        r = ensure_targets(bus, /* argv= */ NULL, &targets);
44✔
296
        if (r < 0)
44✔
297
                return log_error_errno(r, "Failed to find targets: %m");
×
298

299
        r = parse_targets(targets, &n, &target_paths, /* ret_versions= */ NULL);
44✔
300
        if (r < 0)
44✔
301
                return log_error_errno(r, "Failed to parse targets: %m");
×
302

303
        table = table_new("target", "version", "path");
44✔
304
        if (!table)
44✔
305
                return log_oom();
×
306

307
        for (size_t i = 0; i < n; i++) {
88✔
308
                char *version = NULL;
44✔
309
                _cleanup_free_ char *path = NULL;
44✔
310
                _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
44✔
311

312
                r = sd_bus_call_method(bus, bus_sysupdate_mgr->destination,
88✔
313
                                       target_paths[i], SYSUPDATE_TARGET_INTERFACE,
44✔
314
                                       "GetVersion", &error, &reply, NULL);
315
                if (r < 0)
44✔
316
                        return log_bus_error(r, &error, targets[i], "get current version");
×
317
                r = sd_bus_message_read_basic(reply, 's', &version);
44✔
318
                if (r < 0)
44✔
319
                        return bus_log_parse_error(r);
×
320

321
                r = sd_bus_get_property_string(bus, bus_sysupdate_mgr->destination,
88✔
322
                                               target_paths[i], SYSUPDATE_TARGET_INTERFACE,
44✔
323
                                               "Path", &error, &path);
324
                if (r < 0)
44✔
325
                        return log_bus_error(r, &error, targets[i], "get target bus path");
×
326

327
                r = table_add_many(table,
88✔
328
                                   TABLE_STRING, targets[i],
329
                                   TABLE_STRING, empty_to_dash(version),
330
                                   TABLE_STRING, path);
331
                if (r < 0)
44✔
332
                        return table_log_add_error(r);
×
333
        }
334

335
        return table_print_with_pager(table, SD_JSON_FORMAT_OFF, arg_pager_flags, arg_legend);
44✔
336
}
337

338
typedef struct DescribeParams {
339
        Version v;
340
        sd_json_variant *contents_json;
341
        bool newest;
342
        bool available;
343
        bool installed;
344
        bool partial;
345
        bool pending;
346
        bool obsolete;
347
        bool protected;
348
        bool incomplete;
349
} DescribeParams;
350

351
static void describe_params_done(DescribeParams *p) {
48✔
352
        assert(p);
48✔
353

354
        version_done(&p->v);
48✔
355
        sd_json_variant_unref(p->contents_json);
48✔
356
}
48✔
357

358
static int parse_describe(sd_bus_message *reply, Version *ret) {
48✔
359
        char *version_json = NULL;
48✔
360
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
48✔
361
        int r;
48✔
362

363
        assert(reply);
48✔
364
        assert(ret);
48✔
365

366
        r = sd_bus_message_read_basic(reply, 's', &version_json);
48✔
367
        if (r < 0)
48✔
368
                return bus_log_parse_error(r);
×
369

370
        r = sd_json_parse(version_json, 0, &json, NULL, NULL);
48✔
371
        if (r < 0)
48✔
372
                return log_error_errno(r, "Failed to parse JSON: %m");
×
373

374
        assert(sd_json_variant_is_object(json));
48✔
375

376
        static const sd_json_dispatch_field dispatch_table[] = {
48✔
377
                { "version",       SD_JSON_VARIANT_STRING,  json_dispatch_version,    offsetof(DescribeParams, v.version),     0 },
378
                { "newest",        SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(DescribeParams, newest),        0 },
379
                { "available",     SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(DescribeParams, available),     0 },
380
                { "installed",     SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(DescribeParams, installed),     0 },
381
                { "partial",       SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(DescribeParams, partial),       0 },
382
                { "pending",       SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(DescribeParams, pending),       0 },
383
                { "obsolete",      SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(DescribeParams, obsolete),      0 },
384
                { "protected",     SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(DescribeParams, protected),     0 },
385
                { "incomplete",    SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(DescribeParams, incomplete),    0 },
386
                { "changelogUrls", SD_JSON_VARIANT_ARRAY,   sd_json_dispatch_strv,    offsetof(DescribeParams, v.changelog),   0 },
387
                { "contents",      SD_JSON_VARIANT_ARRAY,   sd_json_dispatch_variant, offsetof(DescribeParams, contents_json), 0 },
388
                {},
389
        };
390

391
        _cleanup_(describe_params_done) DescribeParams p = {};
48✔
392

393
        r = sd_json_dispatch(json, dispatch_table, SD_JSON_ALLOW_EXTENSIONS, &p);
48✔
394
        if (r < 0)
48✔
395
                return log_error_errno(r, "Failed to parse JSON: %m");
×
396

397
        SET_FLAG(p.v.flags, UPDATE_NEWEST, p.newest);
48✔
398
        SET_FLAG(p.v.flags, UPDATE_AVAILABLE, p.available);
48✔
399
        SET_FLAG(p.v.flags, UPDATE_INSTALLED, p.installed);
48✔
400
        SET_FLAG(p.v.flags, UPDATE_PARTIAL, p.partial);
48✔
401
        SET_FLAG(p.v.flags, UPDATE_PENDING, p.pending);
48✔
402
        SET_FLAG(p.v.flags, UPDATE_OBSOLETE, p.obsolete);
48✔
403
        SET_FLAG(p.v.flags, UPDATE_PROTECTED, p.protected);
48✔
404
        SET_FLAG(p.v.flags, UPDATE_INCOMPLETE, p.incomplete);
48✔
405

406
        r = sd_json_variant_format(p.contents_json, 0, &p.v.contents_json);
48✔
407
        if (r < 0)
48✔
408
                return log_error_errno(r, "Failed to format JSON for contents: %m");
×
409

410
        *ret = TAKE_STRUCT(p.v);
48✔
411
        return 0;
48✔
412
}
413

414
static int list_versions_finished(sd_bus_message *reply, void *userdata, sd_bus_error *ret_error) {
40✔
415
        _cleanup_(operation_freep) Operation *op = ASSERT_PTR(userdata);
80✔
416
        Table *table = ASSERT_PTR(op->userdata);
40✔
417
        const sd_bus_error *e;
40✔
418
        _cleanup_(version_done) Version v = {};
×
419
        _cleanup_free_ char *version_link = NULL;
40✔
420
        const char *color;
40✔
421
        int r;
40✔
422

423
        assert(reply);
40✔
424

425
        e = sd_bus_message_get_error(reply);
40✔
426
        if (e)
40✔
427
                return log_bus_error(0, e, NULL, "call Describe");
×
428

429
        r = parse_describe(reply, &v);
40✔
430
        if (r < 0)
40✔
431
                return log_error_errno(r, "Failed to parse Describe output: %m");
×
432

433
        color = update_set_flags_to_color(v.flags);
40✔
434

435
        if (urlify_enabled() && !strv_isempty(v.changelog)) {
40✔
436
                version_link = strjoin(v.version, glyph(GLYPH_EXTERNAL_LINK));
×
437
                if (!version_link)
×
438
                        return log_oom();
×
439
        }
440

441
        r = table_add_many(table,
40✔
442
                           TABLE_STRING,    update_set_flags_to_glyph(v.flags),
443
                           TABLE_SET_COLOR, color,
444
                           TABLE_STRING,    version_link ?: v.version,
445
                           TABLE_SET_COLOR, color,
446
                           TABLE_SET_URL,   strv_isempty(v.changelog) ? NULL : v.changelog[0],
447
                           TABLE_STRING,    update_set_flags_to_string(v.flags),
448
                           TABLE_SET_COLOR, color);
449
        if (r < 0)
40✔
450
                return table_log_add_error(r);
×
451

452
        return 0;
453
}
454

455
static int list_versions(sd_bus *bus, const char *target_path) {
8✔
456
        _cleanup_(sd_event_unrefp) sd_event *event = NULL;
8✔
457
        _cleanup_(table_unrefp) Table *table = NULL;
×
458
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
8✔
459
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
8✔
460
        _cleanup_strv_free_ char **versions = NULL;
8✔
461
        unsigned remaining = 0;
8✔
462
        int r;
8✔
463

464
        r = sd_bus_call_method(
24✔
465
                        bus,
466
                        bus_sysupdate_mgr->destination,
8✔
467
                        target_path,
468
                        SYSUPDATE_TARGET_INTERFACE,
469
                        "List",
470
                        &error,
471
                        &reply,
472
                        "t",
473
                        arg_offline ? SD_SYSUPDATE_OFFLINE : 0);
8✔
474
        if (r < 0)
8✔
475
                return log_bus_error(r, &error, NULL, "call List");
×
476

477
        r = sd_bus_message_read_strv(reply, &versions);
8✔
478
        if (r < 0)
8✔
479
                return bus_log_parse_error(r);
×
480

481
        table = table_new("", "version", "status");
8✔
482
        if (!table)
8✔
483
                return log_oom();
×
484

485
        (void) table_set_sort(table, 1);
8✔
486
        (void) table_set_reverse(table, 1, true);
8✔
487

488
        r = sd_event_default(&event);
8✔
489
        if (r < 0)
8✔
490
                return log_error_errno(r, "Failed to get event loop: %m");
×
491

492
        r = sd_bus_attach_event(bus, event, 0);
8✔
493
        if (r < 0)
8✔
494
                return log_error_errno(r, "Failed to attach bus to event loop: %m");
×
495

496
        r = sd_event_set_signal_exit(event, true);
8✔
497
        if (r < 0)
8✔
498
                return log_error_errno(r, "Failed to set up interrupt handler: %m");
×
499

500
        STRV_FOREACH(version, versions) {
48✔
501
                _cleanup_(operation_freep) Operation *op = NULL;
×
502
                op = operation_new(table, bus, &remaining, NULL, NULL);
40✔
503
                if (!op)
40✔
504
                        return log_oom();
×
505

506
                r = sd_bus_call_method_async(bus,
120✔
507
                                             NULL,
508
                                             bus_sysupdate_mgr->destination,
40✔
509
                                             target_path,
510
                                             SYSUPDATE_TARGET_INTERFACE,
511
                                             "Describe",
512
                                             list_versions_finished,
513
                                             op,
514
                                             "st",
515
                                             *version,
516
                                             arg_offline ? SD_SYSUPDATE_OFFLINE : 0);
40✔
517
                if (r < 0)
40✔
518
                        return log_error_errno(r, "Failed to call Describe: %m");
×
519
                TAKE_PTR(op);
40✔
520

521
                remaining++;
40✔
522
        }
523

524
        r = sd_event_loop(event);
8✔
525
        if (r < 0)
8✔
526
                return log_error_errno(r, "Failed to start event loop: %m");
×
527

528
        return table_print_with_pager(table, SD_JSON_FORMAT_OFF, arg_pager_flags, arg_legend);
8✔
529
}
530

531
static int describe(sd_bus *bus, const char *target_path, const char *version) {
8✔
532
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
8✔
533
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
8✔
534
        _cleanup_(table_unrefp) Table *table = NULL;
×
535
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
8✔
536
        _cleanup_(version_done) Version v = {};
8✔
537
        sd_json_variant *entry;
8✔
538
        const char *color;
8✔
539
        int r;
8✔
540

541
        r = sd_bus_call_method(
24✔
542
                        bus,
543
                        bus_sysupdate_mgr->destination,
8✔
544
                        target_path,
545
                        SYSUPDATE_TARGET_INTERFACE,
546
                        "Describe",
547
                        &error,
548
                        &reply,
549
                        "st",
550
                        version,
551
                        arg_offline ? SD_SYSUPDATE_OFFLINE : 0);
8✔
552
        if (r < 0)
8✔
553
                return log_bus_error(r, &error, NULL, "call Describe");
×
554

555
        r = parse_describe(reply, &v);
8✔
556
        if (r < 0)
8✔
557
                return log_error_errno(r, "Failed to parse Describe output: %m");
×
558

559
        color = strempty(update_set_flags_to_color(v.flags));
8✔
560

561
        printf("%s%s%s Version: %s\n"
24✔
562
               "    State: %s%s%s\n",
563
               color,
564
               update_set_flags_to_glyph(v.flags),
565
               ansi_normal(),
566
               v.version,
567
               color,
568
               update_set_flags_to_string(v.flags),
569
               ansi_normal());
570

571
        STRV_FOREACH(url, v.changelog) {
8✔
572
                _cleanup_free_ char *changelog_link = NULL;
×
573

574
                r = terminal_urlify(*url, NULL, &changelog_link);
×
575
                if (r < 0)
×
576
                        return log_error_errno(r, "Could not urlify link to change-log: %m");
×
577

578
                printf("ChangeLog: %s\n", strna(changelog_link));
×
579
        }
580
        printf("\n");
8✔
581

582
        r = sd_json_parse(v.contents_json, 0, &json, NULL, NULL);
8✔
583
        if (r < 0)
8✔
584
                return log_error_errno(r, "Failed to parse JSON: %m");
×
585

586
        assert(sd_json_variant_is_array(json));
8✔
587

588
        JSON_VARIANT_ARRAY_FOREACH(entry, json) {
48✔
589
                assert(sd_json_variant_is_object(entry));
40✔
590
                const char *key;
40✔
591
                sd_json_variant *value;
40✔
592

593
                if (!table) {
40✔
594
                        table = table_new_raw(sd_json_variant_elements(entry) / 2);
8✔
595
                        if (!table)
8✔
596
                                return log_oom();
×
597

598
                        JSON_VARIANT_OBJECT_FOREACH(key, value, entry) {
80✔
599

600
                                r = table_add_cell(table, NULL, TABLE_HEADER, key);
72✔
601
                                if (r < 0)
72✔
602
                                        return table_log_add_error(r);
×
603
                        }
604
                }
605

606
                JSON_VARIANT_OBJECT_FOREACH(key, value, entry) {
400✔
607
                        TableDataType type;
360✔
608
                        uint64_t number;
360✔
609
                        bool boolean;
360✔
610
                        const void *data;
360✔
611

612
                        if (sd_json_variant_is_string(value)) {
360✔
613
                                type = TABLE_STRING;
96✔
614
                                assert_se(data = sd_json_variant_string(value));
96✔
615
                        } else if (sd_json_variant_is_unsigned(value)) {
264✔
616
                                type = TABLE_UINT64;
80✔
617
                                number = sd_json_variant_unsigned(value);
80✔
618
                                data = &number;
80✔
619
                        } else if (sd_json_variant_is_boolean(value)) {
184✔
620
                                type = TABLE_BOOLEAN;
16✔
621
                                boolean = sd_json_variant_boolean(value);
16✔
622
                                data = &boolean;
16✔
623
                        } else if (sd_json_variant_is_null(value)) {
168✔
624
                                type = TABLE_EMPTY;
625
                                data = NULL;
626
                        } else
627
                                assert_not_reached();
×
628

629
                        if (streq(key, "ptflags"))
360✔
630
                                type = TABLE_UINT64_HEX;
631
                        else if (streq(key, "size"))
320✔
632
                                type = TABLE_SIZE;
633
                        else if (streq(key, "mode"))
320✔
634
                                type = TABLE_MODE;
635
                        else if (streq(key, "mtime"))
280✔
636
                                type = TABLE_TIMESTAMP;
40✔
637

638
                        r = table_add_cell(table, NULL, type, data);
360✔
639
                        if (r < 0)
360✔
640
                                return table_log_add_error(r);
×
641
                }
642
        }
643

644
        return table_print_with_pager(table, SD_JSON_FORMAT_OFF, arg_pager_flags, arg_legend);
8✔
645
}
646

647
VERB(verb_list, "list", "[TARGET[@VERSION]]", VERB_ANY, 2, VERB_DEFAULT|VERB_ONLINE_ONLY,
648
     "List available targets and versions");
649
static int verb_list(int argc, char *argv[], uintptr_t _data, void *userdata) {
60✔
650
        sd_bus *bus = ASSERT_PTR(userdata);
60✔
651
        _cleanup_free_ char *target_path = NULL, *version = NULL;
60✔
652
        int r;
60✔
653

654
        if (argc == 1)
60✔
655
                return list_targets(bus);
44✔
656

657
        r = parse_target(argv[1], &target_path, &version);
16✔
658
        if (r < 0)
16✔
659
                return log_oom();
×
660

661
        if (version)
16✔
662
                return describe(bus, target_path, version);
8✔
663
        else
664
                return list_versions(bus, target_path);
8✔
665
}
666

667
static int check_describe_finished(sd_bus_message *reply, void *userdata, sd_bus_error *ret_error) {
×
668
        _cleanup_(operation_freep) Operation *op = ASSERT_PTR(userdata);
×
669
        Table *table = ASSERT_PTR(op->userdata);
×
670
        _cleanup_(version_done) Version v = {};
×
671
        _cleanup_free_ char *update = NULL;
×
672
        const sd_bus_error *e;
×
673
        sd_bus_error error = {};
×
674
        const char *lnk = NULL;
×
675
        char *current;
×
676
        int r;
×
677

678
        assert(reply);
×
679

680
        e = sd_bus_message_get_error(reply);
×
681
        if (e)
×
682
                return log_bus_error(0, e, NULL, "call Describe");
×
683

684
        r = parse_describe(reply, &v);
×
685
        if (r < 0)
×
686
                return log_error_errno(r, "Failed to parse output of Describe: %m");
×
687

688
        r = sd_bus_call_method(
×
689
                        op->bus,
690
                        bus_sysupdate_mgr->destination,
×
691
                        op->target_path,
×
692
                        SYSUPDATE_TARGET_INTERFACE,
693
                        "GetVersion",
694
                        &error,
695
                        &reply,
696
                        NULL);
697
        if (r < 0)
×
698
                return log_bus_error(r, &error, op->target_id, "get current version");
×
699

700
        r = sd_bus_message_read_basic(reply, 's', &current);
×
701
        if (r < 0)
×
702
                return bus_log_parse_error(r);
×
703

704
        if (urlify_enabled() && !strv_isempty(v.changelog))
×
705
                lnk = glyph(GLYPH_EXTERNAL_LINK);
×
706

707
        update = strjoin(empty_to_dash(current), " ",
×
708
                         glyph(GLYPH_ARROW_RIGHT), " ",
709
                         v.version, strempty(lnk));
710
        if (!update)
×
711
                return log_oom();
×
712

713
        r = table_add_many(table,
×
714
                           TABLE_STRING,  op->target_id,
715
                           TABLE_STRING,  update,
716
                           TABLE_SET_URL, strv_isempty(v.changelog) ? NULL : v.changelog[0]);
717
        if (r < 0)
×
718
                return table_log_add_error(r);
×
719

720
        return 0;
721
}
722

723
static int check_finished(sd_bus_message *reply, void *userdata, sd_bus_error *ret_error) {
16✔
724
        _cleanup_(operation_freep) Operation *op = ASSERT_PTR(userdata);
16✔
725
        const sd_bus_error *e;
16✔
726
        const char *new_version = NULL;
16✔
727
        int r;
16✔
728

729
        assert(reply);
16✔
730

731
        e = sd_bus_message_get_error(reply);
16✔
732
        if (e)
16✔
733
                return log_bus_error(0, e, op->target_id, "call CheckNew");
×
734

735
        r = sd_bus_message_read(reply, "s", &new_version);
16✔
736
        if (r < 0)
16✔
737
                return bus_log_parse_error(r);
×
738

739
        if (isempty(new_version))
32✔
740
                return 0;
741

742
        r = sd_bus_call_method_async(op->bus,
×
743
                                     NULL,
744
                                     bus_sysupdate_mgr->destination,
×
745
                                     op->target_path,
746
                                     SYSUPDATE_TARGET_INTERFACE,
747
                                     "Describe",
748
                                     check_describe_finished,
749
                                     op,
750
                                     "st",
751
                                     new_version,
752
                                     arg_offline ? SD_SYSUPDATE_OFFLINE : 0);
×
753
        if (r < 0)
×
754
                return log_error_errno(r, "Failed to call Describe: %m");
×
755
        TAKE_PTR(op);
×
756

757
        return 0;
×
758
}
759

760
VERB(verb_check, "check", "[TARGET...]", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY,
761
     "Check for updates");
762
static int verb_check(int argc, char *argv[], uintptr_t _data, void *userdata) {
8✔
763
        sd_bus *bus = ASSERT_PTR(userdata);
8✔
764
        _cleanup_(table_unrefp) Table *table = NULL;
×
765
        _cleanup_(sd_event_unrefp) sd_event *event = NULL;
8✔
766
        _cleanup_strv_free_ char **targets = NULL, **target_paths = NULL;
8✔
767
        size_t n;
8✔
768
        unsigned remaining = 0;
8✔
769
        int r;
8✔
770

771
        r = ensure_targets(bus, argv + 1, &targets);
8✔
772
        if (r < 0)
8✔
773
                return log_error_errno(r, "Failed to find targets: %m");
×
774

775
        r = parse_targets(targets, &n, &target_paths, /* ret_versions= */ NULL);
8✔
776
        if (r < 0)
8✔
777
                return log_error_errno(r, "Failed to parse targets: %m");
×
778

779
        table = table_new("target", "update");
8✔
780
        if (!table)
8✔
781
                return log_oom();
×
782

783
        (void) table_set_sort(table, 0);
8✔
784

785
        r = sd_event_default(&event);
8✔
786
        if (r < 0)
8✔
787
                return log_error_errno(r, "Failed to get event loop: %m");
×
788

789
        r = sd_bus_attach_event(bus, event, 0);
8✔
790
        if (r < 0)
8✔
791
                return log_error_errno(r, "Failed to attach bus to event loop: %m");
×
792

793
        r = sd_event_set_signal_exit(event, true);
8✔
794
        if (r < 0)
8✔
795
                return log_error_errno(r, "Failed to set up interrupt handler: %m");
×
796

797
        for (size_t i = 0; i < n; i++) {
24✔
798
                _cleanup_(operation_freep) Operation *op = NULL;
×
799
                op = operation_new(table, bus, &remaining, target_paths[i], targets[i]);
16✔
800
                if (!op)
16✔
801
                        return log_oom();
×
802

803
                r = sd_bus_call_method_async(bus, NULL, bus_sysupdate_mgr->destination, target_paths[i], SYSUPDATE_TARGET_INTERFACE, "CheckNew", check_finished, op, NULL);
16✔
804
                if (r < 0)
16✔
805
                        return log_error_errno(r, "Failed to call CheckNew for target %s: %m", targets[i]);
×
806
                TAKE_PTR(op);
16✔
807

808
                remaining++;
16✔
809
        }
810

811
        r = sd_event_loop(event);
8✔
812
        if (r < 0)
8✔
813
                return log_error_errno(r, "Failed to start event loop: %m");
×
814

815
        if (table_isempty(table)) {
16✔
816
                log_info("No updates available.");
8✔
817
                return 0;
818
        }
819

820
        return table_print_with_pager(table, SD_JSON_FORMAT_OFF, arg_pager_flags, arg_legend);
×
821
}
822

823
#define UPDATE_PROGRESS_FAILED INT_MIN
824
#define UPDATE_PROGRESS_ACQUIRED (INT_MAX - 1)
825
#define UPDATE_PROGRESS_DONE INT_MAX
826
/* Make sure it doesn't overlap w/ errno values */
827
assert_cc(UPDATE_PROGRESS_FAILED < -ERRNO_MAX);
828

829
static int update_render_progress(sd_event_source *source, void *userdata) {
338✔
830
        OrderedHashmap *map = ASSERT_PTR(userdata);
338✔
831
        const char *target;
338✔
832
        void *p;
338✔
833
        unsigned total;
338✔
834
        size_t n;
338✔
835
        bool exiting;
338✔
836

837
        exiting = sd_event_get_state(sd_event_source_get_event(source)) == SD_EVENT_EXITING;
338✔
838

839
        total = 0;
338✔
840
        n = ordered_hashmap_size(map);
338✔
841

842
        if (n == 0)
338✔
843
                return 0;
338✔
844

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

849
        if (!terminal_is_dumb()) {
280✔
850
                for (size_t i = 0; i <= n; i++)
×
851
                        fputs("\n", stderr); /* Possibly scroll the terminal to make room (including total) */
×
852

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

855
                fputs("\e7", stderr); /* Save cursor position */
×
856
                fputs("\e[?25l", stderr); /* Hide cursor */
×
857
        }
858

859
        ORDERED_HASHMAP_FOREACH_KEY(p, target, map) {
560✔
860
                int progress = PTR_TO_INT(p);
280✔
861

862
                if (progress == UPDATE_PROGRESS_FAILED) {
280✔
863
                        clear_progress_bar_unbuffered(target);
×
864
                        fprintf(stderr, "%s: %s Unknown failure\n", target, RED_CROSS_MARK());
×
865
                        total += 100;
×
866
                } else if (progress == -EALREADY) {
280✔
867
                        clear_progress_bar_unbuffered(target);
×
868
                        fprintf(stderr, "%s: %s Already up-to-date\n", target, GREEN_CHECK_MARK());
×
869
                        n--; /* Don't consider this target in the total */
×
870
                } else if (progress == -EUCLEAN) {
280✔
871
                        clear_progress_bar_unbuffered(target);
2✔
872
                        fprintf(stderr, "%s: %s Update is already acquired and partially installed. Vacuum it to try installing again.\n", target, RED_CROSS_MARK());
2✔
873
                        total += 100;
2✔
874
                } else if (progress < 0) {
278✔
875
                        clear_progress_bar_unbuffered(target);
8✔
876
                        fprintf(stderr, "%s: %s %s\n", target, RED_CROSS_MARK(), STRERROR(progress));
8✔
877
                        total += 100;
8✔
878
                } else if (progress == UPDATE_PROGRESS_ACQUIRED) {
270✔
879
                        clear_progress_bar_unbuffered(target);
96✔
880
                        fprintf(stderr, "%s: %s Installing\n", target, glyph(GLYPH_DOWNLOAD));
96✔
881
                        total += 100;
96✔
882
                } else if (progress == UPDATE_PROGRESS_DONE) {
174✔
883
                        clear_progress_bar_unbuffered(target);
24✔
884
                        fprintf(stderr, "%s: %s Done\n", target, GREEN_CHECK_MARK());
24✔
885
                        total += 100;
24✔
886
                } else {
887
                        draw_progress_bar_unbuffered(target, progress);
150✔
888
                        fputs("\n", stderr);
150✔
889
                        total += progress;
150✔
890
                }
891
        }
892

893
        if (n > 1) {
280✔
894
                if (exiting)
×
895
                        clear_progress_bar_unbuffered(target);
×
896
                else {
UNCOV
897
                        draw_progress_bar_unbuffered("Total", (double) total / n);
×
UNCOV
898
                        if (terminal_is_dumb())
×
UNCOV
899
                                fputs("\n", stderr);
×
900
                }
901
        }
902

903
        if (!terminal_is_dumb()) {
280✔
UNCOV
904
                if (exiting)
×
UNCOV
905
                        fputs("\e[?25h", stderr); /* Show cursor again */
×
906
                else
UNCOV
907
                        fputs("\e8", stderr); /* Restore cursor position */
×
908
        } else if (!exiting)
280✔
909
                fputs("------\n", stderr);
252✔
910

911
        return 0;
280✔
912
}
913

914
static int update_properties_changed(sd_bus_message *m, void *userdata, sd_bus_error *error) {
98✔
915
        Operation *op = ASSERT_PTR(userdata);
98✔
916
        OrderedHashmap *map = ASSERT_PTR(op->userdata);
98✔
917
        const char *interface;
98✔
918
        uint32_t progress = UINT32_MAX;
98✔
919
        static const struct bus_properties_map prop_map[] = {
98✔
920
                { "Progress", "u", NULL, 0 },
921
                {}
922
        };
923
        int r;
98✔
924

925
        assert(m);
98✔
926

927
        r = sd_bus_message_read(m, "s", &interface);
98✔
928
        if (r < 0) {
98✔
UNCOV
929
                bus_log_parse_error_debug(r);
×
930
                return 0;
98✔
931
        }
932

933
        if (!streq(interface, "org.freedesktop.sysupdate1.Job"))
98✔
934
                return 0;
935

936
        r = bus_message_map_all_properties(m, prop_map, /* flags= */ 0, error, &progress);
98✔
937
        if (r < 0)
98✔
938
                return 0; /* map_all_properties does the debug logging internally... */
939

940
        if (progress == UINT_MAX)
98✔
941
                return 0;
942

943
        r = ordered_hashmap_replace(map, op->target_id, INT_TO_PTR((int) progress));
98✔
944
        if (r < 0)
98✔
UNCOV
945
                log_debug_errno(r, "Failed to update hashmap: %m");
×
946
        return 0;
947
}
948

949
static int update_install_started(sd_bus_message *reply, void *userdata, sd_bus_error *ret_error) {
26✔
950
        _cleanup_(operation_freep) Operation *op = ASSERT_PTR(userdata);
26✔
951
        OrderedHashmap *map = ASSERT_PTR(op->userdata);
26✔
952
        const sd_bus_error *e;
26✔
953
        const char *job_path;
26✔
954
        int r;
26✔
955

956
        assert(reply);
26✔
957

958
        e = sd_bus_message_get_error(reply);
26✔
959
        if (e) {
26✔
960
                r = -sd_bus_error_get_errno(e);
2✔
961

962
                r = ordered_hashmap_replace(map, op->target_id, INT_TO_PTR(r));
2✔
963
                if (r < 0)
2✔
964
                        log_debug_errno(r, "Failed to update hashmap: %m");
26✔
965

966
                return 0;
967
        }
968

969
        r = sd_bus_message_read(reply, "sto", NULL, &op->job_id, &job_path);
24✔
970
        if (r < 0)
24✔
UNCOV
971
                return bus_log_parse_error(r);
×
972
        r = free_and_strdup_warn(&op->job_path, job_path);
24✔
973
        if (r < 0)
24✔
974
                return r;
975

976
        /* Update this job in the hashmap. */
977
        r = ordered_hashmap_replace(map, op->target_id, INT_TO_PTR(UPDATE_PROGRESS_ACQUIRED));
24✔
978
        if (r < 0)
24✔
UNCOV
979
                log_debug_errno(r, "Failed to update hashmap: %m");
×
980

981
        /* Register for progress notifications for this Install() D-Bus call; previously
982
         * op->job_properties_slot was registered for progress notifications for the Acquire() D-Bus call. */
983
        sd_bus_slot_unref(TAKE_PTR(op->job_properties_slot));
24✔
984
        r = sd_bus_match_signal_async(
48✔
985
                        op->bus,
986
                        &op->job_properties_slot,
987
                        bus_sysupdate_mgr->destination,
24✔
988
                        job_path,
989
                        "org.freedesktop.DBus.Properties",
990
                        "PropertiesChanged",
991
                        update_properties_changed,
992
                        NULL,
993
                        op);
994
        if (r < 0)
24✔
UNCOV
995
                return log_bus_error(r, NULL, op->target_id, "listen for PropertiesChanged");
×
996

997
        TAKE_PTR(op); /* update_install_finished/update_interrupted take ownership of the data */
24✔
998

999
        return 0;
24✔
1000
}
1001

1002
static int update_install_finished(sd_bus_message *m, void *userdata, sd_bus_error *error) {
26✔
1003
        _cleanup_(operation_freep) Operation *op = ASSERT_PTR(userdata);
26✔
1004
        OrderedHashmap *map = ASSERT_PTR(op->userdata);
26✔
1005
        uint64_t id;
26✔
1006
        int r, status;
26✔
1007

1008
        assert(m);
26✔
1009

1010
        r = sd_bus_message_read(m, "toi", &id, NULL, &status);
26✔
1011
        if (r < 0) {
26✔
UNCOV
1012
                bus_log_parse_error_debug(r);
×
1013
                return 0;
1014
        }
1015

1016
        if (id != op->job_id) {
26✔
1017
                TAKE_PTR(op);
2✔
1018
                return 0;
2✔
1019
        }
1020

1021
        if (status == 0) /* success */
24✔
1022
                status = UPDATE_PROGRESS_DONE;
24✔
UNCOV
1023
        else if (status > 0) /* exit status without errno */
×
UNCOV
1024
                status = UPDATE_PROGRESS_FAILED; /* i.e. EXIT_FAILURE */
×
1025
        /* else errno */
1026

1027
        r = ordered_hashmap_replace(map, op->target_id, INT_TO_PTR(status));
24✔
1028
        if (r < 0)
24✔
1029
                log_debug_errno(r, "Failed to update hashmap: %m");
26✔
1030
        return 0;
1031
}
1032

1033
static int update_acquire_finished(sd_bus_message *m, void *userdata, sd_bus_error *error) {
28✔
1034
        _cleanup_(operation_freep) Operation *op = ASSERT_PTR(userdata);
28✔
1035
        OrderedHashmap *map = ASSERT_PTR(op->userdata);
28✔
1036
        uint64_t id;
28✔
1037
        int r, status;
28✔
1038

1039
        assert(m);
28✔
1040

1041
        r = sd_bus_message_read(m, "toi", &id, NULL, &status);
28✔
1042
        if (r < 0) {
28✔
1043
                bus_log_parse_error_debug(r);
28✔
1044
                return 0;
1045
        }
1046

1047
        if (id != op->job_id) {
28✔
1048
                TAKE_PTR(op);
2✔
1049
                return 0;
2✔
1050
        }
1051

1052
        if (status == 0) /* success */
26✔
1053
                status = UPDATE_PROGRESS_ACQUIRED;
24✔
1054
        else if (status > 0) /* exit status without errno */
2✔
UNCOV
1055
                status = UPDATE_PROGRESS_FAILED; /* i.e. EXIT_FAILURE */
×
1056
        /* else errno */
1057

1058
        r = ordered_hashmap_replace(map, op->target_id, INT_TO_PTR(status));
26✔
1059
        if (r < 0)
26✔
UNCOV
1060
                log_debug_errno(r, "Failed to update hashmap: %m");
×
1061

1062
        /* Renew the JobRemoved notification for the Install() call instead. */
1063
        sd_bus_slot_unref(op->job_finished_slot);
26✔
1064
        r = bus_match_signal_async(
26✔
1065
                        op->bus, &op->job_finished_slot, bus_sysupdate_mgr, "JobRemoved", update_install_finished, NULL, op);
1066
        if (r < 0)
26✔
UNCOV
1067
                return log_bus_error(r, NULL, op->target_id, "listen for JobRemoved");
×
1068

1069
        /* With the Acquire() call finished, immediately call Install() to deploy the downloaded update.
1070
         * This reuses the same Operation struct so the progress reporting continues to be done in the same
1071
         * slot in the terminal. */
1072
        r = sd_bus_call_method_async(
52✔
1073
                        op->bus,
1074
                        NULL,
1075
                        bus_sysupdate_mgr->destination,
26✔
1076
                        op->target_path,
1077
                        SYSUPDATE_TARGET_INTERFACE,
1078
                        "Install",
1079
                        update_install_started,
1080
                        op,
1081
                        "st",
1082
                        op->requested_version,
26✔
1083
                        0LU);
1084
        if (r < 0)
26✔
UNCOV
1085
                return log_bus_error(r, NULL, op->target_id, "call Install");
×
1086
        TAKE_PTR(op);
26✔
1087

1088
        return 0;
26✔
1089
}
1090

1091
static int update_interrupted(sd_event_source *source, void *userdata) {
×
1092
        /* Since the event loop is exiting, we will never receive the JobRemoved
1093
         * signal. So, we must free the userdata here. */
UNCOV
1094
        _cleanup_(operation_freep) Operation *op = ASSERT_PTR(userdata);
×
UNCOV
1095
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
UNCOV
1096
        OrderedHashmap *map = ASSERT_PTR(op->userdata);
×
1097
        int r;
×
1098

1099
        /* This call should work regardless of whether we’re cancelling the Acquire() call or the Install()
1100
         * call. */
UNCOV
1101
        r = sd_bus_call_method(op->bus,
×
UNCOV
1102
                               bus_sysupdate_mgr->destination,
×
UNCOV
1103
                               op->job_path,
×
1104
                               "org.freedesktop.sysupdate1.Job",
1105
                               "Cancel",
1106
                               &error,
1107
                               /* ret_reply= */ NULL,
1108
                               NULL);
1109
        if (r < 0)
×
1110
                return log_bus_error(r, &error, NULL, "call Cancel");
×
1111

UNCOV
1112
        r = ordered_hashmap_replace(map, op->target_id, INT_TO_PTR(-ECANCELED));
×
UNCOV
1113
        if (r < 0)
×
UNCOV
1114
                log_debug_errno(r, "Failed to update hashmap: %m");
×
1115

1116
        return 0;
1117
}
1118

1119
static int update_acquire_started(sd_bus_message *reply, void *userdata, sd_bus_error *ret_error) {
28✔
1120
        _cleanup_(operation_freep) Operation *op = ASSERT_PTR(userdata);
28✔
1121
        OrderedHashmap *map = ASSERT_PTR(op->userdata);
28✔
1122
        const sd_bus_error *e;
28✔
1123
        _cleanup_free_ char *key = NULL;
28✔
1124
        const char *new_version, *job_path;
28✔
1125
        int r;
28✔
1126

1127
        assert(reply);
28✔
1128

1129
        e = sd_bus_message_get_error(reply);
28✔
1130
        if (e) {
28✔
1131
                r = -sd_bus_error_get_errno(e);
2✔
1132

1133
                key = strdup(op->target_id);
2✔
1134
                if (!key)
2✔
UNCOV
1135
                        return log_oom();
×
1136
                r = ordered_hashmap_put(map, key, INT_TO_PTR(r));
2✔
1137
                if (r < 0)
2✔
UNCOV
1138
                        return log_error_errno(r, "Failed to update hashmap: %m");
×
1139
                TAKE_PTR(key);
28✔
1140

1141
                return 0;
1142
        }
1143

1144
        r = sd_bus_message_read(reply, "sto", &new_version, &op->job_id, &job_path);
26✔
1145
        if (r < 0)
26✔
UNCOV
1146
                return bus_log_parse_error(r);
×
1147
        op->job_path = strdup(job_path);
26✔
1148
        if (!op->job_path)
26✔
UNCOV
1149
                return log_oom();
×
1150

1151
        /* Store the version for the subsequent Install() call */
1152
        op->acquired_version = strdup(new_version);
26✔
1153
        if (!op->acquired_version)
26✔
UNCOV
1154
                return log_oom();
×
1155

1156
        if (isempty(new_version))
26✔
1157
                new_version = "latest";
×
1158

1159
        /* Register this job into the hashmap. This will give it a progress bar */
1160
        if (strchr(op->target_id, '@'))
26✔
1161
                key = strdup(op->target_id);
×
1162
        else
1163
                key = strjoin(op->target_id, "@", new_version);
26✔
1164
        if (!key)
26✔
UNCOV
1165
                return log_oom();
×
1166
        r = ordered_hashmap_put(map, key, INT_TO_PTR(0)); /* takes ownership of key */
26✔
1167
        if (r < 0)
26✔
UNCOV
1168
                return log_error_errno(r, "Failed to add target to tracking map: %m");
×
1169
        op->target_id = TAKE_PTR(key); /* just borrowing */
26✔
1170

1171
        /* Cancel the job if the event loop exits */
1172
        r = sd_event_add_exit(op->event, &op->job_interrupt_source, update_interrupted, op);
26✔
1173
        if (r < 0)
26✔
UNCOV
1174
                return log_error_errno(r, "Failed to set up interrupt handler: %m");
×
1175

1176
        /* We need to cancel the job before the final iteration of the renderer runs */
1177
        r = sd_event_source_set_priority(op->job_interrupt_source, SD_EVENT_PRIORITY_IMPORTANT);
26✔
1178
        if (r < 0)
26✔
UNCOV
1179
                return log_error_errno(r, "Failed to set interrupt priority: %m");
×
1180

1181
        /* Register for progress notifications */
1182
        r = sd_bus_match_signal_async(
52✔
1183
                        op->bus,
1184
                        &op->job_properties_slot,
1185
                        bus_sysupdate_mgr->destination,
26✔
1186
                        job_path,
1187
                        "org.freedesktop.DBus.Properties",
1188
                        "PropertiesChanged",
1189
                        update_properties_changed,
1190
                        NULL,
1191
                        op);
1192
        if (r < 0)
26✔
UNCOV
1193
                return log_bus_error(r, NULL, op->target_id, "listen for PropertiesChanged");
×
1194

1195
        TAKE_PTR(op); /* update_acquire_finished/update_interrupted take ownership of the data */
26✔
1196

1197
        return 0;
26✔
1198
}
1199

1200
static int do_update(sd_bus *bus, char **targets) {
28✔
1201
        _cleanup_(sd_event_unrefp) sd_event *event = NULL;
28✔
1202
        _cleanup_(sd_event_source_unrefp) sd_event_source *render_exit = NULL;
28✔
1203
        _cleanup_ordered_hashmap_free_ OrderedHashmap *map = NULL;
28✔
1204
        _cleanup_strv_free_ char **versions = NULL, **target_paths = NULL;
28✔
1205
        size_t n;
28✔
1206
        unsigned remaining = 0;
28✔
1207
        void *p;
28✔
1208
        bool did_anything = false;
28✔
1209
        int r;
28✔
1210

1211
        assert(bus);
28✔
1212
        assert(targets);
28✔
1213

1214
        r = parse_targets(targets, &n, &target_paths, &versions);
28✔
1215
        if (r < 0)
28✔
1216
                return log_error_errno(r, "Could not parse targets: %m");
×
1217

1218
        map = ordered_hashmap_new(&string_hash_ops_free);
28✔
1219
        if (!map)
28✔
1220
                return log_oom();
×
1221

1222
        r = sd_event_default(&event);
28✔
1223
        if (r < 0)
28✔
1224
                return log_error_errno(r, "Failed to get event loop: %m");
×
1225

1226
        r = sd_bus_attach_event(bus, event, 0);
28✔
1227
        if (r < 0)
28✔
1228
                return log_error_errno(r, "Failed to attach bus to event loop: %m");
×
1229

1230
        r = sd_event_set_signal_exit(event, true);
28✔
1231
        if (r < 0)
28✔
UNCOV
1232
                return log_error_errno(r, "Failed to set up interrupt handler: %m");
×
1233

1234
        for (size_t i = 0; i < n; i++) {
56✔
UNCOV
1235
                _cleanup_(operation_freep) Operation *op = NULL;
×
1236
                op = operation_new(map, bus, &remaining, target_paths[i], targets[i]);
28✔
1237
                if (!op)
28✔
UNCOV
1238
                        return log_oom();
×
1239

1240
                /* Sign up for notification when the associated job finishes */
1241
                r = bus_match_signal_async(
28✔
1242
                                op->bus, &op->job_finished_slot, bus_sysupdate_mgr, "JobRemoved", update_acquire_finished, NULL, op);
1243
                if (r < 0)
28✔
UNCOV
1244
                        return log_bus_error(r, NULL, op->target_id, "listen for JobRemoved");
×
1245

1246
                r = sd_bus_call_method_async(
56✔
1247
                                bus,
1248
                                NULL,
1249
                                bus_sysupdate_mgr->destination,
28✔
1250
                                target_paths[i],
28✔
1251
                                SYSUPDATE_TARGET_INTERFACE,
1252
                                "Acquire",
1253
                                update_acquire_started,
1254
                                op,
1255
                                "st",
1256
                                versions[i],
28✔
1257
                                0LU);
1258
                if (r < 0)
28✔
1259
                        return log_bus_error(r, NULL, targets[i], "call Acquire");
×
1260

1261
                op->requested_version = strdup(versions[i]);
28✔
1262
                if (!op->requested_version)
28✔
UNCOV
1263
                        return log_oom();
×
1264

1265
                TAKE_PTR(op);
28✔
1266

1267
                remaining++;
28✔
1268
        }
1269

1270
        /* Set up the rendering */
1271
        r = sd_event_add_post(event, NULL, update_render_progress, map);
28✔
1272
        if (r < 0)
28✔
1273
                return log_error_errno(r, "Failed to add progress rendering callback: %m");
×
1274

1275
        r = sd_event_add_exit(event, &render_exit, update_render_progress, map);
28✔
1276
        if (r < 0)
28✔
1277
                return log_error_errno(r, "Failed to add exit callback: %m");
×
1278

1279
        r = sd_event_source_set_priority(render_exit, SD_EVENT_PRIORITY_IDLE);
28✔
1280
        if (r < 0)
28✔
1281
                return log_error_errno(r, "Failed to set priority of update job");
×
1282

1283
        r = sd_event_loop(event);
28✔
1284
        if (r < 0)
28✔
UNCOV
1285
                return log_error_errno(r, "Failed to start event loop");
×
1286

1287
        ORDERED_HASHMAP_FOREACH(p, map) {
52✔
1288
                r = PTR_TO_INT(p);
28✔
1289
                if (r == -EALREADY)
28✔
UNCOV
1290
                        continue;
×
1291
                if (r == UPDATE_PROGRESS_FAILED)
28✔
1292
                        return EXIT_FAILURE;
4✔
1293
                if (r < 0)
28✔
1294
                        return r;
1295

1296
                did_anything = true;
1297
        }
1298

1299
        return did_anything ? 1 : 0;
24✔
1300
}
1301

1302
VERB(verb_update, "update", "[TARGET[@VERSION]...]", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY,
1303
     "Install updates");
1304
static int verb_update(int argc, char *argv[], uintptr_t _data, void *userdata) {
28✔
1305
        sd_bus *bus = ASSERT_PTR(userdata);
28✔
1306
        _cleanup_strv_free_ char **targets = NULL;
28✔
1307
        bool did_anything = false;
28✔
1308
        int r;
28✔
1309

1310
        r = ensure_targets(bus, argv + 1, &targets);
28✔
1311
        if (r < 0)
28✔
UNCOV
1312
                return log_error_errno(r, "Could not find targets: %m");
×
1313

1314
        r = do_update(bus, targets);
28✔
1315
        if (r < 0)
28✔
1316
                return r;
1317
        if (r > 0)
24✔
1318
                did_anything = true;
24✔
1319

1320
        if (!arg_reboot)
24✔
1321
                return 0;
1322

UNCOV
1323
        if (did_anything)
×
UNCOV
1324
                return reboot_now();
×
1325

1326
        log_info("Nothing was updated... skipping reboot.");
×
1327
        return 0;
1328
}
1329

1330
static int do_vacuum(sd_bus *bus, const char *target, const char *path) {
×
UNCOV
1331
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
1332
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
×
1333
        uint32_t count, disabled;
×
1334
        int r;
×
1335

1336
        r = sd_bus_call_method(bus, bus_sysupdate_mgr->destination, path, SYSUPDATE_TARGET_INTERFACE, "Vacuum", &error, &reply, NULL);
×
1337
        if (r < 0)
×
1338
                return log_bus_error(r, &error, target, "call Vacuum");
×
1339

1340
        r = sd_bus_message_read(reply, "uu", &count, &disabled);
×
1341
        if (r < 0)
×
UNCOV
1342
                return bus_log_parse_error(r);
×
1343

1344
        if (count > 0 && disabled > 0)
×
1345
                log_info("Deleted %u instance(s) and %u disabled transfer(s) of %s.",
×
1346
                         count, disabled, target);
UNCOV
1347
        else if (count > 0)
×
1348
                log_info("Deleted %u instance(s) of %s.", count, target);
×
UNCOV
1349
        else if (disabled > 0)
×
1350
                log_info("Deleted %u disabled transfer(s) of %s.", disabled, target);
×
1351
        else
UNCOV
1352
                log_info("Found nothing to delete for %s.", target);
×
1353

UNCOV
1354
        return count + disabled > 0 ? 1 : 0;
×
1355
}
1356

1357
VERB(verb_vacuum, "vacuum", "[TARGET...]", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY,
1358
     "Clean up old updates");
1359
static int verb_vacuum(int argc, char *argv[], uintptr_t _data, void *userdata) {
×
UNCOV
1360
        sd_bus *bus = ASSERT_PTR(userdata);
×
1361
        _cleanup_strv_free_ char **targets = NULL, **target_paths = NULL;
×
1362
        size_t n;
×
1363
        int r;
×
1364

1365
        r = ensure_targets(bus, argv + 1, &targets);
×
1366
        if (r < 0)
×
1367
                return log_error_errno(r, "Failed to find targets: %m");
×
1368

1369
        r = parse_targets(targets, &n, &target_paths, /* ret_versions= */ NULL);
×
1370
        if (r < 0)
×
1371
                return log_error_errno(r, "Failed to parse targets: %m");
×
1372

UNCOV
1373
        for (size_t i = 0; i < n; i++) {
×
UNCOV
1374
                r = do_vacuum(bus, targets[i], target_paths[i]);
×
UNCOV
1375
                if (r < 0)
×
1376
                        return r;
1377
        }
1378
        return 0;
1379
}
1380

1381
typedef struct Feature {
1382
        char *name;
1383
        char *description;
1384
        bool enabled;
1385
        char *documentation;
1386
        char **transfers;
1387
} Feature;
1388

1389
static void feature_done(Feature *f) {
×
1390
        assert(f);
×
1391
        f->name = mfree(f->name);
×
UNCOV
1392
        f->description = mfree(f->description);
×
1393
        f->documentation = mfree(f->documentation);
×
1394
        f->transfers = strv_free(f->transfers);
×
1395
}
×
1396

1397
static int describe_feature(sd_bus *bus, const char *feature, Feature *ret) {
×
1398
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
1399
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
×
UNCOV
1400
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
×
1401
        _cleanup_(feature_done) Feature f = {};
×
UNCOV
1402
        char *json;
×
UNCOV
1403
        int r;
×
1404

UNCOV
1405
        static const sd_json_dispatch_field dispatch_table[] = {
×
1406
                { "name",             SD_JSON_VARIANT_STRING,  sd_json_dispatch_string,  offsetof(Feature, name),          SD_JSON_MANDATORY },
1407
                { "description",      SD_JSON_VARIANT_STRING,  sd_json_dispatch_string,  offsetof(Feature, description),   0                 },
1408
                { "enabled",          SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(Feature, enabled),       SD_JSON_MANDATORY },
1409
                { "documentationUrl", SD_JSON_VARIANT_STRING,  sd_json_dispatch_string,  offsetof(Feature, documentation), 0                 },
1410
                { "transfers",        SD_JSON_VARIANT_ARRAY,   sd_json_dispatch_strv,    offsetof(Feature, transfers),     0                 },
1411
                {}
1412
        };
1413

1414
        assert(bus);
×
1415
        assert(feature);
×
UNCOV
1416
        assert(ret);
×
1417

UNCOV
1418
        r = sd_bus_call_method(bus,
×
UNCOV
1419
                               bus_sysupdate_mgr->destination,
×
1420
                               SYSUPDATE_HOST_PATH,
1421
                               SYSUPDATE_TARGET_INTERFACE,
1422
                               "DescribeFeature",
1423
                               &error,
1424
                               &reply,
1425
                               "st",
1426
                               feature,
1427
                               UINT64_C(0));
1428
        if (r < 0)
×
1429
                return log_bus_error(r, &error, "host", "lookup feature");
×
1430

1431
        r = sd_bus_message_read_basic(reply, 's', &json);
×
1432
        if (r < 0)
×
1433
                return bus_log_parse_error(r);
×
1434

1435
        r = sd_json_parse(json, 0, &v, NULL, NULL);
×
1436
        if (r < 0)
×
1437
                return log_error_errno(r, "Failed to parse JSON: %m");
×
1438

1439
        r = sd_json_dispatch(v, dispatch_table, 0, &f);
×
1440
        if (r < 0)
×
UNCOV
1441
                return log_error_errno(r, "Failed to dispatch JSON: %m");
×
1442

1443
        *ret = TAKE_STRUCT(f);
×
1444
        return 0;
×
1445
}
1446

1447
static int list_features(sd_bus *bus) {
×
1448
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
UNCOV
1449
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
×
1450
        _cleanup_strv_free_ char **features = NULL;
×
UNCOV
1451
        _cleanup_(table_unrefp) Table *table = NULL;
×
1452
        int r;
×
1453

1454
        assert(bus);
×
1455

1456
        table = table_new("", "feature", "description");
×
1457
        if (!table)
×
UNCOV
1458
                return log_oom();
×
1459

UNCOV
1460
        r = sd_bus_call_method(bus,
×
UNCOV
1461
                               bus_sysupdate_mgr->destination,
×
1462
                               SYSUPDATE_HOST_PATH,
1463
                               SYSUPDATE_TARGET_INTERFACE,
1464
                               "ListFeatures",
1465
                               &error,
1466
                               &reply,
1467
                               "t",
1468
                               UINT64_C(0));
1469
        if (r < 0)
×
1470
                return log_bus_error(r, &error, "host", "lookup feature");
×
1471

1472
        r = sd_bus_message_read_strv(reply, &features);
×
1473
        if (r < 0)
×
1474
                return bus_log_parse_error(r);
×
1475

1476
        STRV_FOREACH(feature, features) {
×
1477
                _cleanup_(feature_done) Feature f = {};
×
UNCOV
1478
                _cleanup_free_ char *name_link = NULL;
×
1479

1480
                r = describe_feature(bus, *feature, &f);
×
1481
                if (r < 0)
×
1482
                        return r;
1483

UNCOV
1484
                if (urlify_enabled() && f.documentation) {
×
UNCOV
1485
                        name_link = strjoin(f.name, glyph(GLYPH_EXTERNAL_LINK));
×
1486
                        if (!name_link)
×
UNCOV
1487
                                return log_oom();
×
1488
                }
1489

UNCOV
1490
                r = table_add_many(table,
×
1491
                                   TABLE_BOOLEAN_CHECKMARK, f.enabled,
1492
                                   TABLE_SET_COLOR, ansi_highlight_green_red(f.enabled),
1493
                                   TABLE_STRING, name_link ?: f.name,
1494
                                   TABLE_SET_URL, f.documentation,
1495
                                   TABLE_STRING, f.description);
1496
                if (r < 0)
×
UNCOV
1497
                        return table_log_add_error(r);
×
1498
        }
1499

UNCOV
1500
        return table_print_with_pager(table, SD_JSON_FORMAT_OFF, arg_pager_flags, arg_legend);
×
1501
}
1502

1503
VERB(verb_features, "features", "[FEATURE]", VERB_ANY, 2, VERB_ONLINE_ONLY,
1504
     "List and inspect optional features on host OS");
1505
static int verb_features(int argc, char *argv[], uintptr_t _data, void *userdata) {
×
UNCOV
1506
        sd_bus *bus = ASSERT_PTR(userdata);
×
1507
        _cleanup_(table_unrefp) Table *table = NULL;
×
1508
        _cleanup_(feature_done) Feature f = {};
×
UNCOV
1509
        int r;
×
1510

1511
        if (argc == 1)
×
1512
                return list_features(bus);
×
1513

1514
        table = table_new_vertical();
×
1515
        if (!table)
×
UNCOV
1516
                return log_oom();
×
1517

1518
        r = describe_feature(bus, argv[1], &f);
×
UNCOV
1519
        if (r < 0)
×
1520
                return r;
1521

UNCOV
1522
        r = table_add_many(table,
×
1523
                           TABLE_FIELD, "Name",
1524
                           TABLE_STRING, f.name,
1525
                           TABLE_FIELD, "Enabled",
1526
                           TABLE_BOOLEAN, f.enabled);
1527
        if (r < 0)
×
1528
                return table_log_add_error(r);
×
1529

UNCOV
1530
        if (f.description) {
×
UNCOV
1531
                r = table_add_many(table, TABLE_FIELD, "Description", TABLE_STRING, f.description);
×
1532
                if (r < 0)
×
1533
                        return table_log_add_error(r);
×
1534
        }
1535

UNCOV
1536
        if (f.documentation) {
×
1537
                r = table_add_many(table,
×
1538
                                   TABLE_FIELD, "Documentation",
1539
                                   TABLE_STRING, f.documentation,
1540
                                   TABLE_SET_URL, f.documentation);
1541
                if (r < 0)
×
1542
                        return table_log_add_error(r);
×
1543
        }
1544

UNCOV
1545
        if (!strv_isempty(f.transfers)) {
×
UNCOV
1546
                r = table_add_many(table, TABLE_FIELD, "Transfers", TABLE_STRV_WRAPPED, f.transfers);
×
1547
                if (r < 0)
×
UNCOV
1548
                        return table_log_add_error(r);
×
1549
        }
1550

UNCOV
1551
        return table_print_with_pager(table, SD_JSON_FORMAT_OFF, arg_pager_flags, false);
×
1552
}
1553

1554
VERB(verb_enable, "enable", "FEATURE...", 2, VERB_ANY, VERB_ONLINE_ONLY,
1555
     "Enable optional feature on host OS");
1556
VERB(verb_enable, "disable", "FEATURE...", 2, VERB_ANY, VERB_ONLINE_ONLY,
1557
     "Disable optional feature on host OS");
1558
static int verb_enable(int argc, char *argv[], uintptr_t _data, void *userdata) {
×
UNCOV
1559
        sd_bus *bus = ASSERT_PTR(userdata);
×
1560
        bool did_anything = false, enable;
×
1561
        char **features;
×
UNCOV
1562
        int r;
×
1563

1564
        enable = streq(argv[0], "enable");
×
UNCOV
1565
        features = strv_skip(argv, 1);
×
1566

1567
        STRV_FOREACH(feature, features) {
×
UNCOV
1568
                _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
1569

UNCOV
1570
                r = sd_bus_call_method(bus,
×
UNCOV
1571
                                       bus_sysupdate_mgr->destination,
×
1572
                                       SYSUPDATE_HOST_PATH,
1573
                                       SYSUPDATE_TARGET_INTERFACE,
1574
                                       "SetFeatureEnabled",
1575
                                       &error,
1576
                                       /* ret_reply= */ NULL,
1577
                                       "sit",
1578
                                       *feature,
1579
                                       (int) enable,
1580
                                       UINT64_C(0));
1581
                if (r < 0)
×
UNCOV
1582
                        return log_bus_error(r, &error, "host", "call SetFeatureEnabled");
×
1583
        }
1584

1585
        if (!arg_now) /* We weren't asked to apply the changes, so we're done! */
×
1586
                return 0;
1587

1588
        if (enable) {
×
UNCOV
1589
                _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
UNCOV
1590
                _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
×
UNCOV
1591
                _cleanup_free_ char *target = NULL;
×
UNCOV
1592
                char *version = NULL;
×
1593

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

UNCOV
1598
                r = sd_bus_call_method(bus,
×
UNCOV
1599
                                       bus_sysupdate_mgr->destination,
×
1600
                                       SYSUPDATE_HOST_PATH,
1601
                                       SYSUPDATE_TARGET_INTERFACE,
1602
                                       "GetVersion",
1603
                                       &error,
1604
                                       &reply,
1605
                                       NULL);
1606
                if (r < 0)
×
1607
                        return log_bus_error(r, &error, "host", "get current version");
×
1608

1609
                r = sd_bus_message_read_basic(reply, 's', &version);
×
1610
                if (r < 0)
×
1611
                        return bus_log_parse_error(r);
×
1612

1613
                target = strjoin("host@", version);
×
UNCOV
1614
                if (!target)
×
1615
                        return log_oom();
×
1616

UNCOV
1617
                r = do_update(bus, STRV_MAKE(target));
×
1618
        } else
1619
                r = do_vacuum(bus, "host", SYSUPDATE_HOST_PATH);
×
UNCOV
1620
        if (r < 0)
×
1621
                return r;
1622
        if (r > 0)
×
1623
                did_anything = true;
×
1624

UNCOV
1625
        if (arg_reboot && did_anything)
×
1626
                return reboot_now();
×
UNCOV
1627
        else if (did_anything)
×
UNCOV
1628
                log_info("Feature(s) %s.", enable ? "downloaded" : "deleted");
×
1629
        else
UNCOV
1630
                log_info("Nothing %s%s.",
×
1631
                         enable ? "downloaded" : "deleted",
1632
                         arg_reboot ? ", skipping reboot" :"");
1633

1634
        return 0;
1635
}
1636

UNCOV
1637
static int help(void) {
×
1638
        _cleanup_free_ char *link = NULL;
×
1639
        _cleanup_(table_unrefp) Table *verbs = NULL, *verbs2 = NULL, *options = NULL;
×
1640
        int r;
×
1641

1642
        r = terminal_urlify_man("updatectl", "1", &link);
×
1643
        if (r < 0)
×
UNCOV
1644
                return log_oom();
×
1645

1646
        r = verbs_get_help_table(&verbs);
×
1647
        if (r < 0)
×
1648
                return r;
1649

1650
        r = option_parser_get_help_table_group("Verbs", &verbs2);
×
1651
        if (r < 0)
×
1652
                return r;
1653

1654
        r = option_parser_get_help_table(&options);
×
UNCOV
1655
        if (r < 0)
×
1656
                return r;
1657

UNCOV
1658
        (void) table_sync_column_widths(0, verbs, verbs2, options);
×
1659

UNCOV
1660
        printf("%s [OPTIONS...] [VERSION]\n"
×
1661
               "\n%sManage system updates.%s\n"
1662
               "\n%sCommands:%s\n",
1663
               program_invocation_short_name,
1664
               ansi_highlight(),
1665
               ansi_normal(),
1666
               ansi_underline(),
1667
               ansi_normal());
1668

1669
        r = table_print_or_warn(verbs);
×
UNCOV
1670
        if (r < 0)
×
1671
                return r;
1672
        r = table_print_or_warn(verbs2);
×
UNCOV
1673
        if (r < 0)
×
1674
                return r;
1675

1676
        printf("\n%sOptions:%s\n",
×
1677
               ansi_underline(),
1678
               ansi_normal());
1679

1680
        r = table_print_or_warn(options);
×
UNCOV
1681
        if (r < 0)
×
1682
                return r;
1683

1684
        printf("\nSee the %s for details.\n", link);
×
1685
        return 0;
1686
}
1687

UNCOV
1688
VERB_COMMON_HELP_HIDDEN(help);
×
1689

1690
static int parse_argv(int argc, char *argv[], char ***ret_args) {
96✔
1691
        assert(argc >= 0);
96✔
1692
        assert(argv);
96✔
1693

1694
        OptionParser opts = { argc, argv };
96✔
1695

1696
        FOREACH_OPTION_OR_RETURN(c, &opts)
192✔
1697
                switch (c) {
×
1698

1699
                OPTION_LONG("reboot", NULL, "Reboot after updating to newer version"):
×
1700
                        arg_reboot = true;
×
1701
                        break;
×
1702

1703
                OPTION_LONG("offline", NULL, "Do not fetch metadata from the network"):
×
1704
                        arg_offline = true;
×
1705
                        break;
×
1706

1707
                OPTION_LONG("now", NULL, "Download/delete resources immediately"):
×
1708
                        arg_now = true;
×
1709
                        break;
×
1710

UNCOV
1711
                OPTION_COMMON_HOST:
×
1712
                        arg_transport = BUS_TRANSPORT_REMOTE;
×
1713
                        arg_host = opts.arg;
×
1714
                        break;
×
1715

1716
                OPTION_COMMON_NO_PAGER:
×
1717
                        arg_pager_flags |= PAGER_DISABLE;
×
1718
                        break;
×
1719

1720
                OPTION_COMMON_NO_LEGEND:
×
UNCOV
1721
                        arg_legend = false;
×
1722
                        break;
×
1723

UNCOV
1724
                OPTION_GROUP("Verbs"): {}
×
1725

1726
                OPTION_COMMON_HELP:
×
UNCOV
1727
                        return help();
×
1728

UNCOV
1729
                OPTION_COMMON_VERSION:
×
UNCOV
1730
                        return version();
×
1731
                }
1732

1733
        *ret_args = option_parser_get_args(&opts);
96✔
1734
        return 1;
96✔
1735
}
1736

1737
static int run(int argc, char *argv[]) {
96✔
1738
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
96✔
1739
        int r;
96✔
1740

1741
        setlocale(LC_ALL, "");
96✔
1742
        log_setup();
96✔
1743

1744
        (void) signal(SIGWINCH, columns_lines_cache_reset);
96✔
1745

1746
        char **args = NULL;
96✔
1747
        r = parse_argv(argc, argv, &args);
96✔
1748
        if (r <= 0)
96✔
1749
                return r;
1750

1751
        r = bus_connect_transport(arg_transport, arg_host, RUNTIME_SCOPE_SYSTEM, &bus);
96✔
1752
        if (r < 0)
96✔
UNCOV
1753
                return bus_log_connect_error(r, arg_transport, RUNTIME_SCOPE_SYSTEM);
×
1754

1755
        if (arg_transport == BUS_TRANSPORT_LOCAL)
96✔
1756
                polkit_agent_open();
96✔
1757

1758
        (void) sd_bus_set_allow_interactive_authorization(bus, true);
96✔
1759

1760
        return dispatch_verb_with_args(args, bus);
96✔
1761
}
1762

1763
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);
96✔
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