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

systemd / systemd / 15232239991

24 May 2025 08:01PM UTC coverage: 72.053% (-0.02%) from 72.07%
15232239991

push

github

web-flow
docs: add man pages for sd_device_enumerator_[new,ref,unref,unrefp] (#37586)

For #20929.

299160 of 415197 relevant lines covered (72.05%)

703671.29 hits per line

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

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

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

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

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

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

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

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

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

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

60
typedef struct Operation {
61
        void *userdata;
62

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

249
                n++;
10✔
250
        }
251

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

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

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

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

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

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

286
        assert(bus);
2✔
287

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

410
        assert(reply);
10✔
411

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

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

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

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

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

439
        return 0;
440
}
441

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

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

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

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

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

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

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

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

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

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

508
                remaining++;
10✔
509
        }
510

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

663
        assert(reply);
×
664

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

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

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

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

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

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

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

705
        return 0;
706
}
707

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

714
        assert(reply);
4✔
715

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

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

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

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

742
        return 0;
×
743
}
744

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

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

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

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

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

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

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

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

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

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

791
                remaining++;
4✔
792
        }
793

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

885
        return 0;
13✔
886
}
887

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

899
        assert(m);
7✔
900

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

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

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

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

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

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

929
        assert(m);
2✔
930

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

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

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

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

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

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

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

976
        return 0;
977
}
978

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

987
        assert(reply);
2✔
988

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

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

1001
                return 0;
1002
        }
1003

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

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

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

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

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

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

1051
        return 0;
2✔
1052
}
1053

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

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

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

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

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

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

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

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

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

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

1116
                remaining++;
2✔
1117
        }
1118

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

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

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

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

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

1145
                did_anything = true;
1146
        }
1147

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

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

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

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

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

1170
        if (did_anything)
×
1171
                return reboot_now();
×
1172

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1259
        assert(bus);
×
1260
        assert(feature);
×
1261
        assert(ret);
×
1262

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

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

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

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

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

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

1299
        assert(bus);
×
1300

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1473
        return 0;
1474
}
1475

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

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

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

1510
        return 0;
1511
}
1512

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

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

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

1536
        int c;
10✔
1537

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

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

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

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

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

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

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

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

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

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

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

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

1583
        return 1;
1584
}
1585

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

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

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

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

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

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

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

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

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

© 2026 Coveralls, Inc