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

systemd / systemd / 25026908423

27 Apr 2026 07:14PM UTC coverage: 71.865% (-0.3%) from 72.175%
25026908423

push

github

daandemeyer
udev: don't assert on worker cap after killing a broken idle worker

manager_can_process_event() considers an event processable if either
there is room below children_max to spawn, or an idle worker exists.
When only the latter holds, event_run() picks the idle worker and
tries device_monitor_send(). If that send fails, event_run() SIGKILLs
the worker, marks it WORKER_KILLED and continues the loop. With no
other idle worker available, it falls through to worker_spawn(),
guarded by:

    assert(hashmap_size(manager->workers) < manager->config.children_max);

The just-killed worker is still in manager->workers until its SIGCHLD
is reaped by on_worker_exit(), so at the cap this assertion trips and
udevd aborts:

    Assertion 'hashmap_size(manager->workers) < manager->config.children_max'
    failed at src/udev/udev-manager.c:635, function event_run(). Aborting.

Instead of asserting, bail out when we are already at the worker
limit. The event remains in EVENT_QUEUED; once the killed worker's
SIGCHLD arrives and frees it from the hashmap, on_post() re-runs
event_queue_start() and the event is retried.

1 of 1 new or added line in 1 file covered. (100.0%)

7309 existing lines in 125 files now uncovered.

322519 of 448782 relevant lines covered (71.87%)

1173939.78 hits per line

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

55.48
/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) {
78✔
83
        if (!p)
78✔
84
                return NULL;
85

86
        assert(*p->remaining > 0);
78✔
87
        *p->remaining -= 1;
78✔
88
        if (*p->remaining == 0)
78✔
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);
38✔
92

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

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

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

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

106
static Operation* operation_new(
78✔
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;
78✔
114

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

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

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

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

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

142
                r = bus_call_method(bus, bus_sysupdate_mgr, "ListTargets", &error, &reply, NULL);
68✔
143
                if (r < 0)
68✔
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)");
68✔
147
                if (r < 0)
68✔
148
                        return bus_log_parse_error(r);
×
149

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

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

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

167
                r = sd_bus_message_exit_container(reply);
68✔
168
                if (r < 0)
68✔
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);
68✔
177
        return 0;
68✔
178
}
179

180
static int parse_target(
92✔
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;
92✔
185
        const char *s;
92✔
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);
92✔
193
        assert(ret_bus_path);
92✔
194
        assert(ret_version);
92✔
195

196
        s = strrchr(in, '@');
92✔
197
        if (s) {
92✔
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);
84✔
204
        if (!id)
92✔
205
                return -ENOMEM;
206

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

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

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

220
static int parse_targets(
68✔
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;
68✔
226
        _cleanup_strv_free_ char **versions = NULL;
68✔
227
        size_t n = 0;
68✔
228
        int r;
68✔
229

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

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

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

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

243
                if (version && !ret_versions)
76✔
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));
76✔
249
                if (r < 0)
76✔
250
                        return log_oom();
×
251

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

256
                n++;
76✔
257
        }
258

259
        *ret_n = n;
68✔
260
        *ret_bus_paths = TAKE_PTR(bus_paths);
68✔
261
        if (ret_versions)
68✔
262
                *ret_versions = TAKE_PTR(versions);
22✔
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) {
38✔
287
        _cleanup_(table_unrefp) Table *table = NULL;
38✔
288
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
38✔
289
        _cleanup_strv_free_ char **targets = NULL, **target_paths = NULL;
38✔
290
        size_t n;
38✔
291
        int r;
38✔
292

293
        assert(bus);
38✔
294

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

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

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

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

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

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

327
                r = table_add_many(table,
76✔
328
                                   TABLE_STRING, targets[i],
329
                                   TABLE_STRING, empty_to_dash(version),
330
                                   TABLE_STRING, path);
331
                if (r < 0)
38✔
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);
38✔
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) {
54✔
650
        sd_bus *bus = ASSERT_PTR(userdata);
54✔
651
        _cleanup_free_ char *target_path = NULL, *version = NULL;
54✔
652
        int r;
54✔
653

654
        if (argc == 1)
54✔
655
                return list_targets(bus);
38✔
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;
8✔
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) {
277✔
830
        OrderedHashmap *map = ASSERT_PTR(userdata);
277✔
831
        const char *target;
277✔
832
        void *p;
277✔
833
        unsigned total;
277✔
834
        size_t n;
277✔
835
        bool exiting;
277✔
836

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

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

842
        if (n == 0)
277✔
843
                return 0;
277✔
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;
233✔
848

849
        if (!terminal_is_dumb()) {
233✔
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) {
466✔
860
                int progress = PTR_TO_INT(p);
233✔
861

862
                if (progress == UPDATE_PROGRESS_FAILED) {
233✔
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) {
233✔
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 < 0) {
233✔
871
                        clear_progress_bar_unbuffered(target);
×
872
                        fprintf(stderr, "%s: %s %s\n", target, RED_CROSS_MARK(), STRERROR(progress));
×
873
                        total += 100;
×
874
                } else if (progress == UPDATE_PROGRESS_ACQUIRED) {
233✔
875
                        clear_progress_bar_unbuffered(target);
88✔
876
                        fprintf(stderr, "%s: %s Installing\n", target, glyph(GLYPH_DOWNLOAD));
88✔
877
                        total += 100;
88✔
878
                } else if (progress == UPDATE_PROGRESS_DONE) {
145✔
879
                        clear_progress_bar_unbuffered(target);
22✔
880
                        fprintf(stderr, "%s: %s Done\n", target, GREEN_CHECK_MARK());
22✔
881
                        total += 100;
22✔
882
                } else {
883
                        draw_progress_bar_unbuffered(target, progress);
123✔
884
                        fputs("\n", stderr);
123✔
885
                        total += progress;
123✔
886
                }
887
        }
888

889
        if (n > 1) {
233✔
890
                if (exiting)
×
891
                        clear_progress_bar_unbuffered(target);
×
892
                else {
893
                        draw_progress_bar_unbuffered("Total", (double) total / n);
×
894
                        if (terminal_is_dumb())
×
895
                                fputs("\n", stderr);
×
896
                }
897
        }
898

899
        if (!terminal_is_dumb()) {
233✔
900
                if (exiting)
×
901
                        fputs("\e[?25h", stderr); /* Show cursor again */
×
902
                else
903
                        fputs("\e8", stderr); /* Restore cursor position */
×
904
        } else if (!exiting)
233✔
905
                fputs("------\n", stderr);
211✔
906

907
        return 0;
233✔
908
}
909

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

921
        assert(m);
79✔
922

923
        r = sd_bus_message_read(m, "s", &interface);
79✔
924
        if (r < 0) {
79✔
925
                bus_log_parse_error_debug(r);
×
926
                return 0;
×
927
        }
928

929
        if (!streq(interface, "org.freedesktop.sysupdate1.Job"))
79✔
930
                return 0;
931

932
        r = bus_message_map_all_properties(m, prop_map, /* flags= */ 0, error, &progress);
79✔
933
        if (r < 0)
79✔
934
                return 0; /* map_all_properties does the debug logging internally... */
935

936
        if (progress == UINT_MAX)
79✔
937
                return 0;
938

939
        r = ordered_hashmap_replace(map, op->target_id, INT_TO_PTR((int) progress));
79✔
940
        if (r < 0)
79✔
941
                log_debug_errno(r, "Failed to update hashmap: %m");
×
942
        return 0;
943
}
944

945
static int update_install_started(sd_bus_message *reply, void *userdata, sd_bus_error *ret_error) {
22✔
946
        _cleanup_(operation_freep) Operation *op = ASSERT_PTR(userdata);
44✔
947
        OrderedHashmap *map = ASSERT_PTR(op->userdata);
22✔
948
        const sd_bus_error *e;
22✔
949
        const char *job_path;
22✔
950
        int r;
22✔
951

952
        assert(reply);
22✔
953

954
        e = sd_bus_message_get_error(reply);
22✔
955
        if (e) {
22✔
956
                r = -sd_bus_error_get_errno(e);
×
957

958
                r = ordered_hashmap_replace(map, op->target_id, INT_TO_PTR(r));
×
959
                if (r < 0)
×
960
                        log_debug_errno(r, "Failed to update hashmap: %m");
×
961

962
                return 0;
×
963
        }
964

965
        r = sd_bus_message_read(reply, "sto", NULL, &op->job_id, &job_path);
22✔
966
        if (r < 0)
22✔
967
                return bus_log_parse_error(r);
×
968
        r = free_and_strdup_warn(&op->job_path, job_path);
22✔
969
        if (r < 0)
22✔
970
                return r;
971

972
        /* Update this job in the hashmap. */
973
        r = ordered_hashmap_replace(map, op->target_id, INT_TO_PTR(UPDATE_PROGRESS_ACQUIRED));
22✔
974
        if (r < 0)
22✔
975
                log_debug_errno(r, "Failed to update hashmap: %m");
×
976

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

993
        TAKE_PTR(op); /* update_install_finished/update_interrupted take ownership of the data */
22✔
994

995
        return 0;
22✔
996
}
997

998
static int update_install_finished(sd_bus_message *m, void *userdata, sd_bus_error *error) {
22✔
999
        _cleanup_(operation_freep) Operation *op = ASSERT_PTR(userdata);
22✔
1000
        OrderedHashmap *map = ASSERT_PTR(op->userdata);
22✔
1001
        uint64_t id;
22✔
1002
        int r, status;
22✔
1003

1004
        assert(m);
22✔
1005

1006
        r = sd_bus_message_read(m, "toi", &id, NULL, &status);
22✔
1007
        if (r < 0) {
22✔
1008
                bus_log_parse_error_debug(r);
×
1009
                return 0;
×
1010
        }
1011

1012
        if (id != op->job_id) {
22✔
1013
                TAKE_PTR(op);
×
1014
                return 0;
×
1015
        }
1016

1017
        if (status == 0) /* success */
22✔
1018
                status = UPDATE_PROGRESS_DONE;
22✔
1019
        else if (status > 0) /* exit status without errno */
×
1020
                status = UPDATE_PROGRESS_FAILED; /* i.e. EXIT_FAILURE */
×
1021
        /* else errno */
1022

1023
        r = ordered_hashmap_replace(map, op->target_id, INT_TO_PTR(status));
22✔
1024
        if (r < 0)
22✔
1025
                log_debug_errno(r, "Failed to update hashmap: %m");
22✔
1026
        return 0;
1027
}
1028

1029
static int update_acquire_finished(sd_bus_message *m, void *userdata, sd_bus_error *error) {
22✔
1030
        _cleanup_(operation_freep) Operation *op = ASSERT_PTR(userdata);
44✔
1031
        OrderedHashmap *map = ASSERT_PTR(op->userdata);
22✔
1032
        uint64_t id;
22✔
1033
        int r, status;
22✔
1034

1035
        assert(m);
22✔
1036

1037
        r = sd_bus_message_read(m, "toi", &id, NULL, &status);
22✔
1038
        if (r < 0) {
22✔
1039
                bus_log_parse_error_debug(r);
×
1040
                return 0;
×
1041
        }
1042

1043
        if (id != op->job_id) {
22✔
1044
                TAKE_PTR(op);
×
1045
                return 0;
×
1046
        }
1047

1048
        if (status == 0) /* success */
22✔
1049
                status = UPDATE_PROGRESS_ACQUIRED;
22✔
1050
        else if (status > 0) /* exit status without errno */
×
1051
                status = UPDATE_PROGRESS_FAILED; /* i.e. EXIT_FAILURE */
×
1052
        /* else errno */
1053

1054
        r = ordered_hashmap_replace(map, op->target_id, INT_TO_PTR(status));
22✔
1055
        if (r < 0)
22✔
1056
                log_debug_errno(r, "Failed to update hashmap: %m");
×
1057

1058
        /* Renew the JobRemoved notification for the Install() call instead. */
1059
        sd_bus_slot_unref(op->job_finished_slot);
22✔
1060
        r = bus_match_signal_async(
22✔
1061
                        op->bus, &op->job_finished_slot, bus_sysupdate_mgr, "JobRemoved", update_install_finished, NULL, op);
1062
        if (r < 0)
22✔
1063
                return log_bus_error(r, NULL, op->target_id, "listen for JobRemoved");
×
1064

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

1084
        return 0;
22✔
1085
}
1086

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

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

1108
        r = ordered_hashmap_replace(map, op->target_id, INT_TO_PTR(-ECANCELED));
×
1109
        if (r < 0)
×
1110
                log_debug_errno(r, "Failed to update hashmap: %m");
×
1111

1112
        return 0;
1113
}
1114

1115
static int update_acquire_started(sd_bus_message *reply, void *userdata, sd_bus_error *ret_error) {
22✔
1116
        _cleanup_(operation_freep) Operation *op = ASSERT_PTR(userdata);
22✔
1117
        OrderedHashmap *map = ASSERT_PTR(op->userdata);
22✔
1118
        const sd_bus_error *e;
22✔
1119
        _cleanup_free_ char *key = NULL;
22✔
1120
        const char *new_version, *job_path;
22✔
1121
        int r;
22✔
1122

1123
        assert(reply);
22✔
1124

1125
        e = sd_bus_message_get_error(reply);
22✔
1126
        if (e) {
22✔
1127
                r = -sd_bus_error_get_errno(e);
×
1128

1129
                key = strdup(op->target_id);
×
1130
                if (!key)
×
1131
                        return log_oom();
×
1132
                r = ordered_hashmap_put(map, key, INT_TO_PTR(r));
×
1133
                if (r < 0)
×
1134
                        return log_error_errno(r, "Failed to update hashmap: %m");
×
1135
                TAKE_PTR(key);
22✔
1136

1137
                return 0;
1138
        }
1139

1140
        r = sd_bus_message_read(reply, "sto", &new_version, &op->job_id, &job_path);
22✔
1141
        if (r < 0)
22✔
1142
                return bus_log_parse_error(r);
×
1143
        op->job_path = strdup(job_path);
22✔
1144
        if (!op->job_path)
22✔
1145
                return log_oom();
×
1146

1147
        /* Store the version for the subsequent Install() call */
1148
        op->acquired_version = strdup(new_version);
22✔
1149
        if (!op->acquired_version)
22✔
1150
                return log_oom();
×
1151

1152
        if (isempty(new_version))
22✔
1153
                new_version = "latest";
×
1154

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

1167
        /* Cancel the job if the event loop exits */
1168
        r = sd_event_add_exit(op->event, &op->job_interrupt_source, update_interrupted, op);
22✔
1169
        if (r < 0)
22✔
1170
                return log_error_errno(r, "Failed to set up interrupt handler: %m");
×
1171

1172
        /* We need to cancel the job before the final iteration of the renderer runs */
1173
        r = sd_event_source_set_priority(op->job_interrupt_source, SD_EVENT_PRIORITY_IMPORTANT);
22✔
1174
        if (r < 0)
22✔
1175
                return log_error_errno(r, "Failed to set interrupt priority: %m");
×
1176

1177
        /* Register for progress notifications */
1178
        r = sd_bus_match_signal_async(
44✔
1179
                        op->bus,
1180
                        &op->job_properties_slot,
1181
                        bus_sysupdate_mgr->destination,
22✔
1182
                        job_path,
1183
                        "org.freedesktop.DBus.Properties",
1184
                        "PropertiesChanged",
1185
                        update_properties_changed,
1186
                        NULL,
1187
                        op);
1188
        if (r < 0)
22✔
1189
                return log_bus_error(r, NULL, op->target_id, "listen for PropertiesChanged");
×
1190

1191
        TAKE_PTR(op); /* update_acquire_finished/update_interrupted take ownership of the data */
22✔
1192

1193
        return 0;
22✔
1194
}
1195

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

1207
        assert(bus);
22✔
1208
        assert(targets);
22✔
1209

1210
        r = parse_targets(targets, &n, &target_paths, &versions);
22✔
1211
        if (r < 0)
22✔
1212
                return log_error_errno(r, "Could not parse targets: %m");
×
1213

1214
        map = ordered_hashmap_new(&string_hash_ops_free);
22✔
1215
        if (!map)
22✔
1216
                return log_oom();
×
1217

1218
        r = sd_event_default(&event);
22✔
1219
        if (r < 0)
22✔
1220
                return log_error_errno(r, "Failed to get event loop: %m");
×
1221

1222
        r = sd_bus_attach_event(bus, event, 0);
22✔
1223
        if (r < 0)
22✔
1224
                return log_error_errno(r, "Failed to attach bus to event loop: %m");
×
1225

1226
        r = sd_event_set_signal_exit(event, true);
22✔
1227
        if (r < 0)
22✔
1228
                return log_error_errno(r, "Failed to set up interrupt handler: %m");
×
1229

1230
        for (size_t i = 0; i < n; i++) {
44✔
1231
                _cleanup_(operation_freep) Operation *op = NULL;
×
1232
                op = operation_new(map, bus, &remaining, target_paths[i], targets[i]);
22✔
1233
                if (!op)
22✔
1234
                        return log_oom();
×
1235

1236
                /* Sign up for notification when the associated job finishes */
1237
                r = bus_match_signal_async(
22✔
1238
                                op->bus, &op->job_finished_slot, bus_sysupdate_mgr, "JobRemoved", update_acquire_finished, NULL, op);
1239
                if (r < 0)
22✔
1240
                        return log_bus_error(r, NULL, op->target_id, "listen for JobRemoved");
×
1241

1242
                r = sd_bus_call_method_async(
44✔
1243
                                bus,
1244
                                NULL,
1245
                                bus_sysupdate_mgr->destination,
22✔
1246
                                target_paths[i],
22✔
1247
                                SYSUPDATE_TARGET_INTERFACE,
1248
                                "Acquire",
1249
                                update_acquire_started,
1250
                                op,
1251
                                "st",
1252
                                versions[i],
22✔
1253
                                0LU);
1254
                if (r < 0)
22✔
1255
                        return log_bus_error(r, NULL, targets[i], "call Acquire");
×
1256

1257
                op->requested_version = strdup(versions[i]);
22✔
1258
                if (!op->requested_version)
22✔
1259
                        return log_oom();
×
1260

1261
                TAKE_PTR(op);
22✔
1262

1263
                remaining++;
22✔
1264
        }
1265

1266
        /* Set up the rendering */
1267
        r = sd_event_add_post(event, NULL, update_render_progress, map);
22✔
1268
        if (r < 0)
22✔
1269
                return log_error_errno(r, "Failed to add progress rendering callback: %m");
×
1270

1271
        r = sd_event_add_exit(event, &render_exit, update_render_progress, map);
22✔
1272
        if (r < 0)
22✔
1273
                return log_error_errno(r, "Failed to add exit callback: %m");
×
1274

1275
        r = sd_event_source_set_priority(render_exit, SD_EVENT_PRIORITY_IDLE);
22✔
1276
        if (r < 0)
22✔
1277
                return log_error_errno(r, "Failed to set priority of update job");
×
1278

1279
        r = sd_event_loop(event);
22✔
1280
        if (r < 0)
22✔
1281
                return log_error_errno(r, "Failed to start event loop");
×
1282

1283
        ORDERED_HASHMAP_FOREACH(p, map) {
44✔
1284
                r = PTR_TO_INT(p);
22✔
1285
                if (r == -EALREADY)
22✔
1286
                        continue;
×
1287
                if (r == UPDATE_PROGRESS_FAILED)
22✔
1288
                        return EXIT_FAILURE;
×
1289
                if (r < 0)
22✔
1290
                        return r;
1291

1292
                did_anything = true;
1293
        }
1294

1295
        return did_anything ? 1 : 0;
22✔
1296
}
1297

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

1306
        r = ensure_targets(bus, argv + 1, &targets);
22✔
1307
        if (r < 0)
22✔
1308
                return log_error_errno(r, "Could not find targets: %m");
×
1309

1310
        r = do_update(bus, targets);
22✔
1311
        if (r < 0)
22✔
1312
                return r;
1313
        if (r > 0)
22✔
1314
                did_anything = true;
22✔
1315

1316
        if (!arg_reboot)
22✔
1317
                return 0;
1318

1319
        if (did_anything)
×
1320
                return reboot_now();
×
1321

1322
        log_info("Nothing was updated... skipping reboot.");
×
1323
        return 0;
1324
}
1325

1326
static int do_vacuum(sd_bus *bus, const char *target, const char *path) {
×
1327
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
1328
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
×
1329
        uint32_t count, disabled;
×
1330
        int r;
×
1331

1332
        r = sd_bus_call_method(bus, bus_sysupdate_mgr->destination, path, SYSUPDATE_TARGET_INTERFACE, "Vacuum", &error, &reply, NULL);
×
1333
        if (r < 0)
×
1334
                return log_bus_error(r, &error, target, "call Vacuum");
×
1335

1336
        r = sd_bus_message_read(reply, "uu", &count, &disabled);
×
1337
        if (r < 0)
×
1338
                return bus_log_parse_error(r);
×
1339

1340
        if (count > 0 && disabled > 0)
×
1341
                log_info("Deleted %u instance(s) and %u disabled transfer(s) of %s.",
×
1342
                         count, disabled, target);
1343
        else if (count > 0)
×
1344
                log_info("Deleted %u instance(s) of %s.", count, target);
×
1345
        else if (disabled > 0)
×
1346
                log_info("Deleted %u disabled transfer(s) of %s.", disabled, target);
×
1347
        else
1348
                log_info("Found nothing to delete for %s.", target);
×
1349

1350
        return count + disabled > 0 ? 1 : 0;
×
1351
}
1352

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

1361
        r = ensure_targets(bus, argv + 1, &targets);
×
1362
        if (r < 0)
×
1363
                return log_error_errno(r, "Failed to find targets: %m");
×
1364

1365
        r = parse_targets(targets, &n, &target_paths, /* ret_versions= */ NULL);
×
1366
        if (r < 0)
×
1367
                return log_error_errno(r, "Failed to parse targets: %m");
×
1368

1369
        for (size_t i = 0; i < n; i++) {
×
1370
                r = do_vacuum(bus, targets[i], target_paths[i]);
×
1371
                if (r < 0)
×
1372
                        return r;
1373
        }
1374
        return 0;
1375
}
1376

1377
typedef struct Feature {
1378
        char *name;
1379
        char *description;
1380
        bool enabled;
1381
        char *documentation;
1382
        char **transfers;
1383
} Feature;
1384

1385
static void feature_done(Feature *f) {
×
1386
        assert(f);
×
1387
        f->name = mfree(f->name);
×
1388
        f->description = mfree(f->description);
×
1389
        f->documentation = mfree(f->documentation);
×
1390
        f->transfers = strv_free(f->transfers);
×
1391
}
×
1392

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

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

1410
        assert(bus);
×
1411
        assert(feature);
×
1412
        assert(ret);
×
1413

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

1427
        r = sd_bus_message_read_basic(reply, 's', &json);
×
1428
        if (r < 0)
×
1429
                return bus_log_parse_error(r);
×
1430

1431
        r = sd_json_parse(json, 0, &v, NULL, NULL);
×
1432
        if (r < 0)
×
1433
                return log_error_errno(r, "Failed to parse JSON: %m");
×
1434

1435
        r = sd_json_dispatch(v, dispatch_table, 0, &f);
×
1436
        if (r < 0)
×
1437
                return log_error_errno(r, "Failed to dispatch JSON: %m");
×
1438

1439
        *ret = TAKE_STRUCT(f);
×
1440
        return 0;
×
1441
}
1442

1443
static int list_features(sd_bus *bus) {
×
1444
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
1445
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
×
1446
        _cleanup_strv_free_ char **features = NULL;
×
1447
        _cleanup_(table_unrefp) Table *table = NULL;
×
1448
        int r;
×
1449

1450
        assert(bus);
×
1451

1452
        table = table_new("", "feature", "description");
×
1453
        if (!table)
×
1454
                return log_oom();
×
1455

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

1468
        r = sd_bus_message_read_strv(reply, &features);
×
1469
        if (r < 0)
×
1470
                return bus_log_parse_error(r);
×
1471

1472
        STRV_FOREACH(feature, features) {
×
1473
                _cleanup_(feature_done) Feature f = {};
×
1474
                _cleanup_free_ char *name_link = NULL;
×
1475

1476
                r = describe_feature(bus, *feature, &f);
×
1477
                if (r < 0)
×
1478
                        return r;
1479

1480
                if (urlify_enabled() && f.documentation) {
×
1481
                        name_link = strjoin(f.name, glyph(GLYPH_EXTERNAL_LINK));
×
1482
                        if (!name_link)
×
1483
                                return log_oom();
×
1484
                }
1485

1486
                r = table_add_many(table,
×
1487
                                   TABLE_BOOLEAN_CHECKMARK, f.enabled,
1488
                                   TABLE_SET_COLOR, ansi_highlight_green_red(f.enabled),
1489
                                   TABLE_STRING, name_link ?: f.name,
1490
                                   TABLE_SET_URL, f.documentation,
1491
                                   TABLE_STRING, f.description);
1492
                if (r < 0)
×
1493
                        return table_log_add_error(r);
×
1494
        }
1495

1496
        return table_print_with_pager(table, SD_JSON_FORMAT_OFF, arg_pager_flags, arg_legend);
×
1497
}
1498

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

1507
        if (argc == 1)
×
1508
                return list_features(bus);
×
1509

1510
        table = table_new_vertical();
×
1511
        if (!table)
×
1512
                return log_oom();
×
1513

1514
        r = describe_feature(bus, argv[1], &f);
×
1515
        if (r < 0)
×
1516
                return r;
1517

1518
        r = table_add_many(table,
×
1519
                           TABLE_FIELD, "Name",
1520
                           TABLE_STRING, f.name,
1521
                           TABLE_FIELD, "Enabled",
1522
                           TABLE_BOOLEAN, f.enabled);
1523
        if (r < 0)
×
1524
                return table_log_add_error(r);
×
1525

1526
        if (f.description) {
×
1527
                r = table_add_many(table, TABLE_FIELD, "Description", TABLE_STRING, f.description);
×
1528
                if (r < 0)
×
1529
                        return table_log_add_error(r);
×
1530
        }
1531

1532
        if (f.documentation) {
×
1533
                r = table_add_many(table,
×
1534
                                   TABLE_FIELD, "Documentation",
1535
                                   TABLE_STRING, f.documentation,
1536
                                   TABLE_SET_URL, f.documentation);
1537
                if (r < 0)
×
1538
                        return table_log_add_error(r);
×
1539
        }
1540

1541
        if (!strv_isempty(f.transfers)) {
×
1542
                r = table_add_many(table, TABLE_FIELD, "Transfers", TABLE_STRV_WRAPPED, f.transfers);
×
1543
                if (r < 0)
×
1544
                        return table_log_add_error(r);
×
1545
        }
1546

1547
        return table_print_with_pager(table, SD_JSON_FORMAT_OFF, arg_pager_flags, false);
×
1548
}
1549

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

1560
        enable = streq(argv[0], "enable");
×
1561
        features = strv_skip(argv, 1);
×
1562

1563
        STRV_FOREACH(feature, features) {
×
1564
                _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
1565

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

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

1584
        if (enable) {
×
1585
                _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
1586
                _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
×
1587
                _cleanup_free_ char *target = NULL;
×
1588
                char *version = NULL;
×
1589

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

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

1605
                r = sd_bus_message_read_basic(reply, 's', &version);
×
1606
                if (r < 0)
×
1607
                        return bus_log_parse_error(r);
×
1608

1609
                target = strjoin("host@", version);
×
1610
                if (!target)
×
1611
                        return log_oom();
×
1612

1613
                r = do_update(bus, STRV_MAKE(target));
×
1614
        } else
1615
                r = do_vacuum(bus, "host", SYSUPDATE_HOST_PATH);
×
1616
        if (r < 0)
×
1617
                return r;
1618
        if (r > 0)
×
1619
                did_anything = true;
×
1620

1621
        if (arg_reboot && did_anything)
×
1622
                return reboot_now();
×
1623
        else if (did_anything)
×
1624
                log_info("Feature(s) %s.", enable ? "downloaded" : "deleted");
×
1625
        else
1626
                log_info("Nothing %s%s.",
×
1627
                         enable ? "downloaded" : "deleted",
1628
                         arg_reboot ? ", skipping reboot" :"");
1629

1630
        return 0;
1631
}
1632

1633
static int help(void) {
×
1634
        _cleanup_free_ char *link = NULL;
×
1635
        _cleanup_(table_unrefp) Table *verbs = NULL, *verbs2 = NULL, *options = NULL;
×
1636
        int r;
×
1637

1638
        r = terminal_urlify_man("updatectl", "1", &link);
×
1639
        if (r < 0)
×
1640
                return log_oom();
×
1641

1642
        r = verbs_get_help_table(&verbs);
×
1643
        if (r < 0)
×
1644
                return r;
1645

1646
        r = option_parser_get_help_table_group("Verbs", &verbs2);
×
1647
        if (r < 0)
×
1648
                return r;
1649

1650
        r = option_parser_get_help_table(&options);
×
1651
        if (r < 0)
×
1652
                return r;
1653

1654
        (void) table_sync_column_widths(0, verbs, verbs2, options);
×
1655

1656
        printf("%s [OPTIONS...] [VERSION]\n"
×
1657
               "\n%sManage system updates.%s\n"
1658
               "\n%sCommands:%s\n",
1659
               program_invocation_short_name,
1660
               ansi_highlight(),
1661
               ansi_normal(),
1662
               ansi_underline(),
1663
               ansi_normal());
1664

1665
        r = table_print_or_warn(verbs);
×
1666
        if (r < 0)
×
1667
                return r;
1668
        r = table_print_or_warn(verbs2);
×
1669
        if (r < 0)
×
1670
                return r;
1671

1672
        printf("\n%sOptions:%s\n",
×
1673
               ansi_underline(),
1674
               ansi_normal());
1675

1676
        r = table_print_or_warn(options);
×
1677
        if (r < 0)
×
1678
                return r;
1679

1680
        printf("\nSee the %s for details.\n", link);
×
1681
        return 0;
1682
}
1683

1684
VERB_COMMON_HELP_HIDDEN(help);
×
1685

1686
static int parse_argv(int argc, char *argv[], char ***ret_args) {
84✔
1687
        assert(argc >= 0);
84✔
1688
        assert(argv);
84✔
1689

1690
        OptionParser opts = { argc, argv };
84✔
1691

1692
        FOREACH_OPTION(c, &opts, /* on_error= */ return c)
168✔
UNCOV
1693
                switch (c) {
×
1694

UNCOV
1695
                OPTION_LONG("reboot", NULL, "Reboot after updating to newer version"):
×
1696
                        arg_reboot = true;
×
1697
                        break;
×
1698

UNCOV
1699
                OPTION_LONG("offline", NULL, "Do not fetch metadata from the network"):
×
1700
                        arg_offline = true;
×
1701
                        break;
×
1702

UNCOV
1703
                OPTION_LONG("now", NULL, "Download/delete resources immediately"):
×
1704
                        arg_now = true;
×
1705
                        break;
×
1706

UNCOV
1707
                OPTION_COMMON_HOST:
×
1708
                        arg_transport = BUS_TRANSPORT_REMOTE;
×
1709
                        arg_host = opts.arg;
×
1710
                        break;
×
1711

UNCOV
1712
                OPTION_COMMON_NO_PAGER:
×
1713
                        arg_pager_flags |= PAGER_DISABLE;
×
1714
                        break;
×
1715

UNCOV
1716
                OPTION_COMMON_NO_LEGEND:
×
1717
                        arg_legend = false;
×
1718
                        break;
×
1719

UNCOV
1720
                OPTION_GROUP("Verbs"): {}
×
1721

UNCOV
1722
                OPTION_COMMON_HELP:
×
1723
                        return help();
×
1724

UNCOV
1725
                OPTION_COMMON_VERSION:
×
1726
                        return version();
×
1727
                }
1728

1729
        *ret_args = option_parser_get_args(&opts);
84✔
1730
        return 1;
84✔
1731
}
1732

1733
static int run(int argc, char *argv[]) {
84✔
1734
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
84✔
1735
        int r;
84✔
1736

1737
        setlocale(LC_ALL, "");
84✔
1738
        log_setup();
84✔
1739

1740
        (void) signal(SIGWINCH, columns_lines_cache_reset);
84✔
1741

1742
        char **args = NULL;
84✔
1743
        r = parse_argv(argc, argv, &args);
84✔
1744
        if (r <= 0)
84✔
1745
                return r;
1746

1747
        r = bus_connect_transport(arg_transport, arg_host, RUNTIME_SCOPE_SYSTEM, &bus);
84✔
1748
        if (r < 0)
84✔
UNCOV
1749
                return bus_log_connect_error(r, arg_transport, RUNTIME_SCOPE_SYSTEM);
×
1750

1751
        if (arg_transport == BUS_TRANSPORT_LOCAL)
84✔
1752
                polkit_agent_open();
84✔
1753

1754
        (void) sd_bus_set_allow_interactive_authorization(bus, true);
84✔
1755

1756
        return dispatch_verb_with_args(args, bus);
84✔
1757
}
1758

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

© 2026 Coveralls, Inc