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

systemd / systemd / 13877892945

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

push

github

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

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

153 existing lines in 28 files now uncovered.

296065 of 411690 relevant lines covered (71.91%)

715276.25 hits per line

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

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

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

6
#include "build.h"
7
#include "chase.h"
8
#include "conf-files.h"
9
#include "constants.h"
10
#include "dirent-util.h"
11
#include "dissect-image.h"
12
#include "fd-util.h"
13
#include "format-table.h"
14
#include "glyph-util.h"
15
#include "hexdecoct.h"
16
#include "json-util.h"
17
#include "main-func.h"
18
#include "mount-util.h"
19
#include "os-util.h"
20
#include "pager.h"
21
#include "parse-argument.h"
22
#include "parse-util.h"
23
#include "path-util.h"
24
#include "pretty-print.h"
25
#include "set.h"
26
#include "signal-util.h"
27
#include "sort-util.h"
28
#include "specifier.h"
29
#include "string-util.h"
30
#include "strv.h"
31
#include "sysupdate.h"
32
#include "sysupdate-feature.h"
33
#include "sysupdate-transfer.h"
34
#include "sysupdate-update-set.h"
35
#include "sysupdate-util.h"
36
#include "terminal-util.h"
37
#include "utf8.h"
38
#include "verbs.h"
39

40
static char *arg_definitions = NULL;
41
bool arg_sync = true;
42
uint64_t arg_instances_max = UINT64_MAX;
43
static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF;
44
static PagerFlags arg_pager_flags = 0;
45
static bool arg_legend = true;
46
char *arg_root = NULL;
47
static char *arg_image = NULL;
48
static bool arg_reboot = false;
49
static char *arg_component = NULL;
50
static int arg_verify = -1;
51
static ImagePolicy *arg_image_policy = NULL;
52
static bool arg_offline = false;
53
char *arg_transfer_source = NULL;
54

55
STATIC_DESTRUCTOR_REGISTER(arg_definitions, freep);
106✔
56
STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
106✔
57
STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
106✔
58
STATIC_DESTRUCTOR_REGISTER(arg_component, freep);
106✔
59
STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
106✔
60
STATIC_DESTRUCTOR_REGISTER(arg_transfer_source, freep);
106✔
61

62
const Specifier specifier_table[] = {
63
        COMMON_SYSTEM_SPECIFIERS,
64
        COMMON_TMP_SPECIFIERS,
65
        {}
66
};
67

68
typedef struct Context {
69
        Transfer **transfers;
70
        size_t n_transfers;
71

72
        Transfer **disabled_transfers;
73
        size_t n_disabled_transfers;
74

75
        Hashmap *features; /* Defined features, keyed by ID */
76

77
        UpdateSet **update_sets;
78
        size_t n_update_sets;
79

80
        UpdateSet *newest_installed, *candidate;
81

82
        Hashmap *web_cache; /* Cache for downloaded resources, keyed by URL */
83
} Context;
84

85
static Context* context_free(Context *c) {
96✔
86
        if (!c)
96✔
87
                return NULL;
88

89
        FOREACH_ARRAY(tr, c->transfers, c->n_transfers)
570✔
90
                transfer_free(*tr);
474✔
91
        free(c->transfers);
96✔
92

93
        FOREACH_ARRAY(tr, c->disabled_transfers, c->n_disabled_transfers)
178✔
94
                transfer_free(*tr);
82✔
95
        free(c->disabled_transfers);
96✔
96

97
        hashmap_free(c->features);
96✔
98

99
        FOREACH_ARRAY(us, c->update_sets, c->n_update_sets)
458✔
100
                update_set_free(*us);
362✔
101
        free(c->update_sets);
96✔
102

103
        hashmap_free(c->web_cache);
96✔
104

105
        return mfree(c);
96✔
106
}
107

108
DEFINE_TRIVIAL_CLEANUP_FUNC(Context*, context_free);
282✔
109

110
static Context* context_new(void) {
96✔
111
        /* For now, no fields to initialize non-zero */
112
        return new0(Context, 1);
96✔
113
}
114

115
static void free_transfers(Transfer **array, size_t n) {
×
116
        FOREACH_ARRAY(t, array, n)
×
117
                transfer_free(*t);
×
118
        free(array);
×
119
}
×
120

121
static int read_definitions(
96✔
122
                Context *c,
123
                const char **dirs,
124
                const char *suffix,
125
                const char *node) {
126

127
        _cleanup_strv_free_ char **files = NULL;
96✔
128
        Transfer **transfers = NULL, **disabled = NULL;
96✔
129
        size_t n_transfers = 0, n_disabled = 0;
96✔
130
        int r;
96✔
131

132
        CLEANUP_ARRAY(transfers, n_transfers, free_transfers);
96✔
133
        CLEANUP_ARRAY(disabled, n_disabled, free_transfers);
96✔
134

135
        assert(c);
96✔
136
        assert(dirs);
96✔
137
        assert(suffix);
96✔
138

139
        r = conf_files_list_strv(&files, suffix, arg_root, CONF_FILES_REGULAR|CONF_FILES_FILTER_MASKED, dirs);
96✔
140
        if (r < 0)
96✔
141
                return log_error_errno(r, "Failed to enumerate sysupdate.d/*%s definitions: %m", suffix);
×
142

143
        STRV_FOREACH(p, files) {
652✔
144
                _cleanup_(transfer_freep) Transfer *t = NULL;
×
145
                Transfer **appended;
556✔
146

147
                t = transfer_new(c);
556✔
148
                if (!t)
556✔
149
                        return log_oom();
×
150

151
                r = transfer_read_definition(t, *p, dirs, c->features);
556✔
152
                if (r < 0)
556✔
153
                        return r;
154

155
                r = transfer_resolve_paths(t, arg_root, node);
556✔
156
                if (r < 0)
556✔
157
                        return r;
158

159
                if (t->enabled)
556✔
160
                        appended = GREEDY_REALLOC_APPEND(transfers, n_transfers, &t, 1);
474✔
161
                else
162
                        appended = GREEDY_REALLOC_APPEND(disabled, n_disabled, &t, 1);
82✔
163
                if (!appended)
556✔
164
                        return log_oom();
×
165
                TAKE_PTR(t);
556✔
166
        }
167

168
        c->transfers = TAKE_PTR(transfers);
96✔
169
        c->n_transfers = n_transfers;
96✔
170
        c->disabled_transfers = TAKE_PTR(disabled);
96✔
171
        c->n_disabled_transfers = n_disabled;
96✔
172
        return 0;
96✔
173
}
174

175
static int context_read_definitions(Context *c, const char* node, bool requires_enabled_transfers) {
96✔
176
        _cleanup_strv_free_ char **dirs = NULL, **files = NULL;
96✔
177
        int r;
96✔
178

179
        assert(c);
96✔
180

181
        if (arg_definitions)
96✔
182
                dirs = strv_new(arg_definitions);
×
183
        else if (arg_component) {
96✔
184
                char **l = CONF_PATHS_STRV("");
4✔
185
                size_t i = 0;
4✔
186

187
                dirs = new0(char*, strv_length(l) + 1);
4✔
188
                if (!dirs)
4✔
189
                        return log_oom();
×
190

191
                STRV_FOREACH(dir, l) {
20✔
192
                        char *j;
16✔
193

194
                        j = strjoin(*dir, "sysupdate.", arg_component, ".d");
16✔
195
                        if (!j)
16✔
196
                                return log_oom();
×
197

198
                        dirs[i++] = j;
16✔
199
                }
200
        } else
201
                dirs = strv_new(CONF_PATHS("sysupdate.d"));
92✔
202
        if (!dirs)
96✔
203
                return log_oom();
×
204

205
        r = conf_files_list_strv(&files,
96✔
206
                                 ".feature",
207
                                 arg_root,
208
                                 CONF_FILES_REGULAR|CONF_FILES_FILTER_MASKED,
209
                                 (const char**) dirs);
210
        if (r < 0)
96✔
211
                return log_error_errno(r, "Failed to enumerate sysupdate.d/*.feature definitions: %m");
×
212

213
        STRV_FOREACH(p, files) {
188✔
214
                _cleanup_(feature_unrefp) Feature *f = NULL;
×
215

216
                f = feature_new();
92✔
217
                if (!f)
92✔
218
                        return log_oom();
×
219

220
                r = feature_read_definition(f, *p, (const char**) dirs);
92✔
221
                if (r < 0)
92✔
222
                        return r;
223

224
                r = hashmap_ensure_put(&c->features, &feature_hash_ops, f->id, f);
92✔
225
                if (r < 0)
92✔
226
                        return log_error_errno(r, "Failed to insert feature '%s' into map: %m", f->id);
×
227
                TAKE_PTR(f);
92✔
228
        }
229

230
        r = read_definitions(c, (const char**) dirs, ".transfer", node);
96✔
231
        if (r < 0)
96✔
232
                return r;
233

234
        if (c->n_transfers + c->n_disabled_transfers == 0) {
96✔
235
                /* Backwards-compat: If no .transfer defs are found, fall back to trying .conf! */
236
                r = read_definitions(c, (const char**) dirs, ".conf", node);
×
237
                if (r < 0)
×
238
                        return r;
239

240
                if (c->n_transfers + c->n_disabled_transfers > 0)
×
241
                        log_warning("As of v257, transfer definitions should have the '.transfer' extension.");
×
242
        }
243

244
        if (c->n_transfers + (requires_enabled_transfers ? 0 : c->n_disabled_transfers) == 0) {
96✔
245
                if (arg_component)
×
246
                        return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
×
247
                                               "No transfer definitions for component '%s' found.",
248
                                               arg_component);
249

250
                return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
×
251
                                       "No transfer definitions found.");
252
        }
253

254
        return 0;
255
}
256

257
static int context_load_installed_instances(Context *c) {
96✔
258
        int r;
96✔
259

260
        assert(c);
96✔
261

262
        log_info("Discovering installed instances%s", glyph(GLYPH_ELLIPSIS));
192✔
263

264
        FOREACH_ARRAY(tr, c->transfers, c->n_transfers) {
570✔
265
                Transfer *t = *tr;
474✔
266

267
                r = resource_load_instances(
474✔
268
                                &t->target,
269
                                arg_verify >= 0 ? arg_verify : t->verify,
474✔
270
                                &c->web_cache);
378✔
271
                if (r < 0)
474✔
272
                        return r;
273
        }
274

275
        FOREACH_ARRAY(tr, c->disabled_transfers, c->n_disabled_transfers) {
178✔
276
                Transfer *t = *tr;
82✔
277

278
                r = resource_load_instances(
82✔
279
                                &t->target,
280
                                arg_verify >= 0 ? arg_verify : t->verify,
82✔
281
                                &c->web_cache);
68✔
282
                if (r < 0)
82✔
283
                        return r;
284
        }
285

286
        return 0;
287
}
288

289
static int context_load_available_instances(Context *c) {
76✔
290
        int r;
76✔
291

292
        assert(c);
76✔
293

294
        log_info("Discovering available instances%s", glyph(GLYPH_ELLIPSIS));
152✔
295

296
        FOREACH_ARRAY(tr, c->transfers, c->n_transfers) {
454✔
297
                Transfer *t = *tr;
378✔
298

299
                r = resource_load_instances(
378✔
300
                                &t->source,
301
                                arg_verify >= 0 ? arg_verify : t->verify,
378✔
302
                                &c->web_cache);
378✔
303
                if (r < 0)
378✔
304
                        return r;
305
        }
306

307
        return 0;
308
}
309

310
static int context_discover_update_sets_by_flag(Context *c, UpdateSetFlags flags) {
166✔
311
        _cleanup_free_ char *boundary = NULL;
166✔
312
        bool newest_found = false;
166✔
313
        int r;
166✔
314

315
        assert(c);
166✔
316
        assert(IN_SET(flags, UPDATE_AVAILABLE, UPDATE_INSTALLED));
166✔
317

318
        for (;;) {
780✔
319
                _cleanup_free_ Instance **cursor_instances = NULL;
×
320
                bool skip = false;
780✔
321
                UpdateSetFlags extra_flags = 0;
780✔
322
                _cleanup_free_ char *cursor = NULL;
614✔
323
                UpdateSet *us = NULL;
780✔
324

325
                /* First, let's find the newest version that's older than the boundary. */
326
                FOREACH_ARRAY(tr, c->transfers, c->n_transfers) {
4,274✔
327
                        Resource *rr;
3,570✔
328

329
                        assert(*tr);
3,570✔
330

331
                        if (flags == UPDATE_AVAILABLE)
3,570✔
332
                                rr = &(*tr)->source;
1,948✔
333
                        else {
334
                                assert(flags == UPDATE_INSTALLED);
1,622✔
335
                                rr = &(*tr)->target;
1,622✔
336
                        }
337

338
                        FOREACH_ARRAY(inst, rr->instances, rr->n_instances) {
10,116✔
339
                                Instance *i = *inst; /* Sorted newest-to-oldest */
9,290✔
340

341
                                assert(i);
9,290✔
342

343
                                if (boundary && strverscmp_improved(i->metadata.version, boundary) >= 0)
9,290✔
344
                                        continue; /* Not older than the boundary */
6,546✔
345

346
                                if (cursor && strverscmp(i->metadata.version, cursor) <= 0)
2,744✔
347
                                        break; /* Not newer than the cursor. The same will be true for all
348
                                                * subsequent instances (due to sorting) so let's skip to the
349
                                                * next transfer. */
350

351
                                if (free_and_strdup(&cursor, i->metadata.version) < 0)
614✔
352
                                        return log_oom();
×
353

354
                                break; /* All subsequent instances will be older than this one */
355
                        }
356

357
                        if (flags == UPDATE_AVAILABLE && !cursor)
3,570✔
358
                                break; /* This transfer didn't have a version older than the boundary,
359
                                        * so any older-than-boundary version that might exist in a different
360
                                        * transfer must always be incomplete. For reasons described below,
361
                                        * we don't include incomplete versions for AVAILABLE updates. So we
362
                                        * are completely done looking. */
363
                }
364

365
                if (!cursor) /* We didn't find anything older than the boundary, so we're done. */
780✔
366
                        break;
367

368
                cursor_instances = new0(Instance*, c->n_transfers);
614✔
369
                if (!cursor_instances)
614✔
370
                        return log_oom();
×
371

372
                /* Now let's find all the instances that match the version of the cursor, if we have them */
373
                for (size_t k = 0; k < c->n_transfers; k++) {
3,602✔
374
                        Transfer *t = c->transfers[k];
3,044✔
375
                        Instance *match = NULL;
3,044✔
376

377
                        assert(t);
3,044✔
378

379
                        if (flags == UPDATE_AVAILABLE) {
3,044✔
380
                                match = resource_find_instance(&t->source, cursor);
1,866✔
381
                                if (!match) {
1,866✔
382
                                        /* When we're looking for updates to download, we don't offer
383
                                         * incomplete versions at all. The server wants to send us an update
384
                                         * with parts of the OS missing. For robustness sake, let's not do
385
                                         * that. */
386
                                        skip = true;
387
                                        break;
388
                                }
389
                        } else {
390
                                assert(flags == UPDATE_INSTALLED);
1,178✔
391

392
                                match = resource_find_instance(&t->target, cursor);
1,178✔
393
                                if (!match) {
1,178✔
394
                                        /* When we're looking for installed versions, let's be robust and treat
395
                                         * an incomplete installation as an installation. Otherwise, there are
396
                                         * situations that can lead to sysupdate wiping the currently booted OS.
397
                                         * See https://github.com/systemd/systemd/issues/33339 */
398
                                        extra_flags |= UPDATE_INCOMPLETE;
312✔
399
                                }
400
                        }
401

402
                        cursor_instances[k] = match;
2,988✔
403

404
                        if (t->min_version && strverscmp_improved(t->min_version, cursor) > 0)
2,988✔
405
                                extra_flags |= UPDATE_OBSOLETE;
×
406

407
                        if (strv_contains(t->protected_versions, cursor))
2,988✔
408
                                extra_flags |= UPDATE_PROTECTED;
×
409
                }
410

411
                r = free_and_strdup_warn(&boundary, cursor);
614✔
412
                if (r < 0)
614✔
413
                        return r;
414

415
                if (skip)
614✔
416
                        continue;
56✔
417

418
                /* See if we already have this update set in our table */
419
                FOREACH_ARRAY(update_set, c->update_sets, c->n_update_sets) {
1,394✔
420
                        UpdateSet *u = *update_set;
1,032✔
421

422
                        if (strverscmp_improved(u->version, cursor) != 0)
1,032✔
423
                                continue;
836✔
424

425
                        /* Merge in what we've learned and continue onto the next version */
426

427
                        if (FLAGS_SET(u->flags, UPDATE_INCOMPLETE)) {
196✔
428
                                assert(u->n_instances == c->n_transfers);
72✔
429

430
                                /* Incomplete updates will have picked NULL instances for the transfers that
431
                                 * are missing. Now we have more information, so let's try to fill them in. */
432

433
                                for (size_t j = 0; j < u->n_instances; j++) {
448✔
434
                                        if (!u->instances[j])
376✔
435
                                                u->instances[j] = cursor_instances[j];
252✔
436

437
                                        /* Make sure that the list is full if the update is AVAILABLE */
438
                                        assert(flags != UPDATE_AVAILABLE || u->instances[j]);
376✔
439
                                }
440
                        }
441

442
                        u->flags |= flags | extra_flags;
196✔
443

444
                        /* If this is the newest installed version, that is incomplete and just became marked
445
                         * as available, and if there is no other candidate available, we promote this to be
446
                         * the candidate. */
447
                        if (FLAGS_SET(u->flags, UPDATE_NEWEST|UPDATE_INSTALLED|UPDATE_INCOMPLETE|UPDATE_AVAILABLE) &&
196✔
448
                            !c->candidate && !FLAGS_SET(u->flags, UPDATE_OBSOLETE))
8✔
449
                                c->candidate = u;
8✔
450

451
                        skip = true;
196✔
452
                        newest_found = true;
196✔
453
                        break;
196✔
454
                }
455

456
                if (skip)
558✔
457
                        continue;
196✔
458

459
                /* Doesn't exist yet, let's add it */
460
                if (!GREEDY_REALLOC(c->update_sets, c->n_update_sets + 1))
362✔
461
                        return log_oom();
×
462

463
                us = new(UpdateSet, 1);
362✔
464
                if (!us)
362✔
465
                        return log_oom();
×
466

467
                *us = (UpdateSet) {
362✔
468
                        .flags = flags | (newest_found ? 0 : UPDATE_NEWEST) | extra_flags,
362✔
469
                        .version = TAKE_PTR(cursor),
362✔
470
                        .instances = TAKE_PTR(cursor_instances),
362✔
471
                        .n_instances = c->n_transfers,
362✔
472
                };
473

474
                c->update_sets[c->n_update_sets++] = us;
362✔
475

476
                newest_found = true;
362✔
477

478
                /* Remember which one is the newest installed */
479
                if ((us->flags & (UPDATE_NEWEST|UPDATE_INSTALLED)) == (UPDATE_NEWEST|UPDATE_INSTALLED))
362✔
480
                        c->newest_installed = us;
86✔
481

482
                /* Remember which is the newest non-obsolete, available (and not installed) version, which we declare the "candidate" */
483
                if ((us->flags & (UPDATE_NEWEST|UPDATE_INSTALLED|UPDATE_AVAILABLE|UPDATE_OBSOLETE)) == (UPDATE_NEWEST|UPDATE_AVAILABLE))
362✔
484
                        c->candidate = us;
28✔
485
        }
486

487
        /* Newest installed is newer than or equal to candidate? Then suppress the candidate */
488
        if (c->newest_installed && !FLAGS_SET(c->newest_installed->flags, UPDATE_INCOMPLETE) &&
166✔
489
            c->candidate && strverscmp_improved(c->newest_installed->version, c->candidate->version) >= 0)
138✔
490
                c->candidate = NULL;
×
491

492
        return 0;
493
}
494

495
static int context_discover_update_sets(Context *c) {
90✔
496
        int r;
90✔
497

498
        assert(c);
90✔
499

500
        log_info("Determining installed update sets%s", glyph(GLYPH_ELLIPSIS));
180✔
501

502
        r = context_discover_update_sets_by_flag(c, UPDATE_INSTALLED);
90✔
503
        if (r < 0)
90✔
504
                return r;
505

506
        if (!arg_offline) {
90✔
507
                log_info("Determining available update sets%s", glyph(GLYPH_ELLIPSIS));
152✔
508

509
                r = context_discover_update_sets_by_flag(c, UPDATE_AVAILABLE);
76✔
510
                if (r < 0)
76✔
511
                        return r;
512
        }
513

514
        typesafe_qsort(c->update_sets, c->n_update_sets, update_set_cmp);
90✔
515
        return 0;
90✔
516
}
517

518
static int context_show_table(Context *c) {
×
519
        _cleanup_(table_unrefp) Table *t = NULL;
×
520
        int r;
×
521

522
        assert(c);
×
523

524
        t = table_new("", "version", "installed", "available", "assessment");
×
525
        if (!t)
×
526
                return log_oom();
×
527

528
        (void) table_set_align_percent(t, table_get_cell(t, 0, 0), 100);
×
529
        (void) table_set_align_percent(t, table_get_cell(t, 0, 2), 50);
×
530
        (void) table_set_align_percent(t, table_get_cell(t, 0, 3), 50);
×
531

532
        FOREACH_ARRAY(update_set, c->update_sets, c->n_update_sets) {
×
533
                UpdateSet *us = *update_set;
×
534
                const char *color;
×
535

536
                color = update_set_flags_to_color(us->flags);
×
537

538
                r = table_add_many(t,
×
539
                                   TABLE_STRING,    update_set_flags_to_glyph(us->flags),
540
                                   TABLE_SET_COLOR, color,
541
                                   TABLE_STRING,    us->version,
542
                                   TABLE_SET_COLOR, color,
543
                                   TABLE_STRING,    glyph_check_mark_space(FLAGS_SET(us->flags, UPDATE_INSTALLED)),
544
                                   TABLE_SET_COLOR, color,
545
                                   TABLE_STRING,    glyph_check_mark_space(FLAGS_SET(us->flags, UPDATE_AVAILABLE)),
546
                                   TABLE_SET_COLOR, color,
547
                                   TABLE_STRING,    update_set_flags_to_string(us->flags),
548
                                   TABLE_SET_COLOR, color);
549
                if (r < 0)
×
550
                        return table_log_add_error(r);
×
551
        }
552

553
        return table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, arg_legend);
×
554
}
555

556
static UpdateSet* context_update_set_by_version(Context *c, const char *version) {
22✔
557
        assert(c);
22✔
558
        assert(version);
22✔
559

560
        FOREACH_ARRAY(update_set, c->update_sets, c->n_update_sets)
42✔
561
                if (streq((*update_set)->version, version))
42✔
562
                        return *update_set;
563

564
        return NULL;
565
}
566

567
static int context_show_version(Context *c, const char *version) {
22✔
568
        bool show_fs_columns = false, show_partition_columns = false,
22✔
569
                have_fs_attributes = false, have_partition_attributes = false,
22✔
570
                have_size = false, have_tries = false, have_no_auto = false,
22✔
571
                have_read_only = false, have_growfs = false, have_sha256 = false;
22✔
572
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
22✔
573
        _cleanup_(table_unrefp) Table *t = NULL;
22✔
574
        _cleanup_strv_free_ char **changelog_urls = NULL;
22✔
575
        UpdateSet *us;
22✔
576
        int r;
22✔
577

578
        assert(c);
22✔
579
        assert(version);
22✔
580

581
        us = context_update_set_by_version(c, version);
22✔
582
        if (!us)
22✔
583
                return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "Update '%s' not found.", version);
×
584

585
        if (arg_json_format_flags & (SD_JSON_FORMAT_OFF|SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_PRETTY_AUTO))
22✔
586
                (void) pager_open(arg_pager_flags);
10✔
587

588
        if (!sd_json_format_enabled(arg_json_format_flags))
22✔
589
                printf("%s%s%s Version: %s\n"
80✔
590
                       "    State: %s%s%s\n"
591
                       "Installed: %s%s\n"
592
                       "Available: %s%s\n"
593
                       "Protected: %s%s%s\n"
594
                       " Obsolete: %s%s%s\n\n",
595
                       strempty(update_set_flags_to_color(us->flags)), update_set_flags_to_glyph(us->flags), ansi_normal(), us->version,
10✔
596
                       strempty(update_set_flags_to_color(us->flags)), update_set_flags_to_string(us->flags), ansi_normal(),
10✔
597
                       yes_no(us->flags & UPDATE_INSTALLED), FLAGS_SET(us->flags, UPDATE_INSTALLED|UPDATE_NEWEST) ? " (newest)" : "",
10✔
598
                       yes_no(us->flags & UPDATE_AVAILABLE), (us->flags & (UPDATE_INSTALLED|UPDATE_AVAILABLE|UPDATE_NEWEST)) == (UPDATE_AVAILABLE|UPDATE_NEWEST) ? " (newest)" : "",
20✔
599
                       FLAGS_SET(us->flags, UPDATE_INSTALLED|UPDATE_PROTECTED) ? ansi_highlight() : "", yes_no(FLAGS_SET(us->flags, UPDATE_INSTALLED|UPDATE_PROTECTED)), ansi_normal(),
10✔
600
                       us->flags & UPDATE_OBSOLETE ? ansi_highlight_red() : "", yes_no(us->flags & UPDATE_OBSOLETE), ansi_normal());
10✔
601

602
        t = table_new("type", "path", "ptuuid", "ptflags", "mtime", "mode", "size", "tries-done", "tries-left", "noauto", "ro", "growfs", "sha256");
22✔
603
        if (!t)
22✔
604
                return log_oom();
×
605

606
        (void) table_set_align_percent(t, table_get_cell(t, 0, 3), 100);
22✔
607
        (void) table_set_align_percent(t, table_get_cell(t, 0, 4), 100);
22✔
608
        (void) table_set_align_percent(t, table_get_cell(t, 0, 5), 100);
22✔
609
        (void) table_set_align_percent(t, table_get_cell(t, 0, 6), 100);
22✔
610
        (void) table_set_align_percent(t, table_get_cell(t, 0, 7), 100);
22✔
611
        (void) table_set_align_percent(t, table_get_cell(t, 0, 8), 100);
22✔
612
        table_set_ersatz_string(t, TABLE_ERSATZ_DASH);
22✔
613

614
        /* Starting in v257, these fields would be automatically formatted with underscores. This would have
615
         * been a breaking change, so to avoid that let's hard-code their original names. */
616
        (void) table_set_json_field_name(t, 7, "tries-done");
22✔
617
        (void) table_set_json_field_name(t, 8, "tries-left");
22✔
618

619
        /* Determine if the target will make use of partition/fs attributes for any of the transfers */
620
        FOREACH_ARRAY(transfer, c->transfers, c->n_transfers) {
136✔
621
                Transfer *tr = *transfer;
114✔
622

623
                if (tr->target.type == RESOURCE_PARTITION)
114✔
624
                        show_partition_columns = true;
44✔
625
                if (RESOURCE_IS_FILESYSTEM(tr->target.type))
114✔
626
                        show_fs_columns = true;
627

628
                STRV_FOREACH(changelog, tr->changelog) {
114✔
629
                        assert(*changelog);
×
630

631
                        _cleanup_free_ char *changelog_url = strreplace(*changelog, "@v", version);
×
632
                        if (!changelog_url)
×
633
                                return log_oom();
×
634

635
                        /* Avoid duplicates */
636
                        if (strv_contains(changelog_urls, changelog_url))
×
637
                                continue;
×
638

639
                        /* changelog_urls takes ownership of expanded changelog_url */
640
                        r = strv_consume(&changelog_urls, TAKE_PTR(changelog_url));
×
641
                        if (r < 0)
×
642
                                return log_oom();
×
643
                }
644
        }
645

646
        FOREACH_ARRAY(inst, us->instances, us->n_instances) {
136✔
647
                Instance *i = *inst;
114✔
648

649
                if (!i) {
114✔
650
                        assert(FLAGS_SET(us->flags, UPDATE_INCOMPLETE));
4✔
651
                        continue;
4✔
652
                }
653

654
                r = table_add_many(t,
110✔
655
                                   TABLE_STRING, resource_type_to_string(i->resource->type),
656
                                   TABLE_PATH, i->path);
657
                if (r < 0)
110✔
658
                        return table_log_add_error(r);
×
659

660
                if (i->metadata.partition_uuid_set) {
110✔
661
                        have_partition_attributes = true;
32✔
662
                        r = table_add_cell(t, NULL, TABLE_UUID, &i->metadata.partition_uuid);
32✔
663
                } else
664
                        r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
78✔
665
                if (r < 0)
110✔
666
                        return table_log_add_error(r);
×
667

668
                if (i->metadata.partition_flags_set) {
110✔
669
                        have_partition_attributes = true;
32✔
670
                        r = table_add_cell(t, NULL, TABLE_UINT64_HEX, &i->metadata.partition_flags);
32✔
671
                } else
672
                        r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
78✔
673
                if (r < 0)
110✔
674
                        return table_log_add_error(r);
×
675

676
                if (i->metadata.mtime != USEC_INFINITY) {
110✔
677
                        have_fs_attributes = true;
78✔
678
                        r = table_add_cell(t, NULL, TABLE_TIMESTAMP, &i->metadata.mtime);
78✔
679
                } else
680
                        r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
32✔
681
                if (r < 0)
110✔
682
                        return table_log_add_error(r);
×
683

684
                if (i->metadata.mode != MODE_INVALID) {
110✔
685
                        have_fs_attributes = true;
78✔
686
                        r = table_add_cell(t, NULL, TABLE_MODE, &i->metadata.mode);
78✔
687
                } else
688
                        r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
32✔
689
                if (r < 0)
110✔
690
                        return table_log_add_error(r);
×
691

692
                if (i->metadata.size != UINT64_MAX) {
110✔
693
                        have_size = true;
×
694
                        r = table_add_cell(t, NULL, TABLE_SIZE, &i->metadata.size);
×
695
                } else
696
                        r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
110✔
697
                if (r < 0)
110✔
698
                        return table_log_add_error(r);
×
699

700
                if (i->metadata.tries_done != UINT64_MAX) {
110✔
701
                        have_tries = true;
16✔
702
                        r = table_add_cell(t, NULL, TABLE_UINT64, &i->metadata.tries_done);
16✔
703
                } else
704
                        r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
94✔
705
                if (r < 0)
110✔
706
                        return table_log_add_error(r);
×
707

708
                if (i->metadata.tries_left != UINT64_MAX) {
110✔
709
                        have_tries = true;
16✔
710
                        r = table_add_cell(t, NULL, TABLE_UINT64, &i->metadata.tries_left);
16✔
711
                } else
712
                        r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
94✔
713
                if (r < 0)
110✔
714
                        return table_log_add_error(r);
×
715

716
                if (i->metadata.no_auto >= 0) {
110✔
717
                        bool b;
×
718

719
                        have_no_auto = true;
×
720
                        b = i->metadata.no_auto;
×
721
                        r = table_add_cell(t, NULL, TABLE_BOOLEAN, &b);
×
722
                } else
723
                        r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
110✔
724
                if (r < 0)
110✔
725
                        return table_log_add_error(r);
×
726
                if (i->metadata.read_only >= 0) {
110✔
727
                        bool b;
32✔
728

729
                        have_read_only = true;
32✔
730
                        b = i->metadata.read_only;
32✔
731
                        r = table_add_cell(t, NULL, TABLE_BOOLEAN, &b);
32✔
732
                } else
733
                        r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
78✔
734
                if (r < 0)
110✔
735
                        return table_log_add_error(r);
×
736

737
                if (i->metadata.growfs >= 0) {
110✔
738
                        bool b;
×
739

740
                        have_growfs = true;
×
741
                        b = i->metadata.growfs;
×
742
                        r = table_add_cell(t, NULL, TABLE_BOOLEAN, &b);
×
743
                } else
744
                        r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
110✔
745
                if (r < 0)
110✔
746
                        return table_log_add_error(r);
×
747

748
                if (i->metadata.sha256sum_set) {
110✔
749
                        _cleanup_free_ char *formatted = NULL;
×
750

751
                        have_sha256 = true;
×
752

753
                        formatted = hexmem(i->metadata.sha256sum, sizeof(i->metadata.sha256sum));
×
754
                        if (!formatted)
×
755
                                return log_oom();
×
756

757
                        r = table_add_cell(t, NULL, TABLE_STRING, formatted);
×
758
                } else
759
                        r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
110✔
760
                if (r < 0)
110✔
761
                        return table_log_add_error(r);
×
762
        }
763

764
        /* Hide the fs/partition columns if we don't have any data to show there */
765
        if (!have_fs_attributes)
22✔
766
                show_fs_columns = false;
×
767
        if (!have_partition_attributes)
22✔
768
                show_partition_columns = false;
769

770
        if (!show_partition_columns)
16✔
771
                (void) table_hide_column_from_display(t, 2, 3);
6✔
772
        if (!show_fs_columns)
22✔
773
                (void) table_hide_column_from_display(t, 4, 5);
×
774
        if (!have_size)
22✔
775
                (void) table_hide_column_from_display(t, 6);
22✔
776
        if (!have_tries)
22✔
777
                (void) table_hide_column_from_display(t, 7, 8);
6✔
778
        if (!have_no_auto)
22✔
779
                (void) table_hide_column_from_display(t, 9);
22✔
780
        if (!have_read_only)
22✔
781
                (void) table_hide_column_from_display(t, 10);
6✔
782
        if (!have_growfs)
22✔
783
                (void) table_hide_column_from_display(t, 11);
22✔
784
        if (!have_sha256)
22✔
785
                (void) table_hide_column_from_display(t, 12);
22✔
786

787
        if (!sd_json_format_enabled(arg_json_format_flags)) {
22✔
788
                printf("%s%s%s Version: %s\n"
100✔
789
                       "    State: %s%s%s\n"
790
                       "Installed: %s%s%s%s%s\n"
791
                       "Available: %s%s\n"
792
                       "Protected: %s%s%s\n"
793
                       " Obsolete: %s%s%s\n",
794
                       strempty(update_set_flags_to_color(us->flags)), update_set_flags_to_glyph(us->flags), ansi_normal(), us->version,
10✔
795
                       strempty(update_set_flags_to_color(us->flags)), update_set_flags_to_string(us->flags), ansi_normal(),
10✔
796
                       yes_no(us->flags & UPDATE_INSTALLED), FLAGS_SET(us->flags, UPDATE_INSTALLED|UPDATE_NEWEST) ? " (newest)" : "",
10✔
797
                       FLAGS_SET(us->flags, UPDATE_INCOMPLETE) ? ansi_highlight_yellow() : "", FLAGS_SET(us->flags, UPDATE_INCOMPLETE) ? " (incomplete)" : "", ansi_normal(),
14✔
798
                       yes_no(us->flags & UPDATE_AVAILABLE), (us->flags & (UPDATE_INSTALLED|UPDATE_AVAILABLE|UPDATE_NEWEST)) == (UPDATE_AVAILABLE|UPDATE_NEWEST) ? " (newest)" : "",
20✔
799
                       FLAGS_SET(us->flags, UPDATE_INSTALLED|UPDATE_PROTECTED) ? ansi_highlight() : "", yes_no(FLAGS_SET(us->flags, UPDATE_INSTALLED|UPDATE_PROTECTED)), ansi_normal(),
10✔
800
                       us->flags & UPDATE_OBSOLETE ? ansi_highlight_red() : "", yes_no(us->flags & UPDATE_OBSOLETE), ansi_normal());
10✔
801

802
                STRV_FOREACH(url, changelog_urls) {
10✔
803
                        _cleanup_free_ char *changelog_link = NULL;
×
804
                        r = terminal_urlify(*url, NULL, &changelog_link);
×
805
                        if (r < 0)
×
806
                                return log_oom();
×
807
                        printf("ChangeLog: %s\n", changelog_link);
×
808
                }
809
                printf("\n");
10✔
810

811
                return table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, arg_legend);
10✔
812
        } else {
813
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *t_json = NULL;
12✔
814

815
                r = table_to_json(t, &t_json);
12✔
816
                if (r < 0)
12✔
817
                        return log_error_errno(r, "failed to convert table to JSON: %m");
×
818

819
                r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_STRING("version", us->version),
12✔
820
                                          SD_JSON_BUILD_PAIR_BOOLEAN("newest", FLAGS_SET(us->flags, UPDATE_NEWEST)),
821
                                          SD_JSON_BUILD_PAIR_BOOLEAN("available", FLAGS_SET(us->flags, UPDATE_AVAILABLE)),
822
                                          SD_JSON_BUILD_PAIR_BOOLEAN("installed", FLAGS_SET(us->flags, UPDATE_INSTALLED)),
823
                                          SD_JSON_BUILD_PAIR_BOOLEAN("obsolete", FLAGS_SET(us->flags, UPDATE_OBSOLETE)),
824
                                          SD_JSON_BUILD_PAIR_BOOLEAN("protected", FLAGS_SET(us->flags, UPDATE_PROTECTED)),
825
                                          SD_JSON_BUILD_PAIR_BOOLEAN("incomplete", FLAGS_SET(us->flags, UPDATE_INCOMPLETE)),
826
                                          SD_JSON_BUILD_PAIR_STRV("changelogUrls", changelog_urls),
827
                                          SD_JSON_BUILD_PAIR_VARIANT("contents", t_json));
828
                if (r < 0)
12✔
829
                        return log_error_errno(r, "Failed to create JSON: %m");
×
830

831
                r = sd_json_variant_dump(json, arg_json_format_flags, stdout, NULL);
12✔
832
                if (r < 0)
12✔
833
                        return log_error_errno(r, "Failed to print JSON: %m");
×
834

835
                return 0;
836
        }
837
}
838

839
static int context_vacuum(
20✔
840
                Context *c,
841
                uint64_t space,
842
                const char *extra_protected_version) {
843

844
        size_t disabled_count = 0;
20✔
845
        int r, count = 0;
20✔
846

847
        assert(c);
20✔
848

849
        if (space == 0)
20✔
850
                log_info("Making room%s", glyph(GLYPH_ELLIPSIS));
4✔
851
        else
852
                log_info("Making room for %" PRIu64 " updates%s", space, glyph(GLYPH_ELLIPSIS));
36✔
853

854
        FOREACH_ARRAY(tr, c->transfers, c->n_transfers) {
122✔
855
                Transfer *t = *tr;
102✔
856

857
                /* Don't bother clearing out space if we're not going to be downloading anything */
858
                if (extra_protected_version && resource_find_instance(&t->target, extra_protected_version))
102✔
859
                        continue;
18✔
860

861
                r = transfer_vacuum(t, space, extra_protected_version);
84✔
862
                if (r < 0)
84✔
863
                        return r;
864

865
                count = MAX(count, r);
84✔
866
        }
867

868
        FOREACH_ARRAY(tr, c->disabled_transfers, c->n_disabled_transfers) {
38✔
869
                r = transfer_vacuum(*tr, UINT64_MAX /* wipe all instances */, NULL);
18✔
870
                if (r < 0)
18✔
871
                        return r;
872
                if (r > 0)
18✔
873
                        disabled_count++;
2✔
874
        }
875

876
        if (!sd_json_format_enabled(arg_json_format_flags)) {
20✔
877
                if (count > 0 && disabled_count > 0)
18✔
878
                        log_info("Removed %i instances, and %zu disabled transfers.", count, disabled_count);
×
879
                else if (count > 0)
18✔
880
                        log_info("Removed %i instances.", count);
8✔
881
                else if (disabled_count > 0)
10✔
882
                        log_info("Removed %zu disabled transfers.", disabled_count);
2✔
883
                else
884
                        log_info("Found nothing to remove.");
20✔
885
        } else {
886
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
2✔
887

888
                r = sd_json_buildo(&json,
2✔
889
                                   SD_JSON_BUILD_PAIR_INTEGER("removed", count),
890
                                   SD_JSON_BUILD_PAIR_UNSIGNED("disabledTransfers", disabled_count));
891
                if (r < 0)
2✔
892
                        return log_error_errno(r, "Failed to create JSON: %m");
×
893

894
                r = sd_json_variant_dump(json, arg_json_format_flags, stdout, NULL);
2✔
895
                if (r < 0)
2✔
896
                        return log_error_errno(r, "Failed to print JSON: %m");
×
897
        }
898

899
        return 0;
900
}
901

902
static int context_make_offline(Context **ret, const char *node, bool requires_enabled_transfers) {
96✔
903
        _cleanup_(context_freep) Context* context = NULL;
96✔
904
        int r;
96✔
905

906
        assert(ret);
96✔
907

908
        /* Allocates a context object and initializes everything we can initialize offline, i.e. without
909
         * checking on the update source (i.e. the Internet) what versions are available */
910

911
        context = context_new();
96✔
912
        if (!context)
96✔
913
                return log_oom();
×
914

915
        r = context_read_definitions(context, node, requires_enabled_transfers);
96✔
916
        if (r < 0)
96✔
917
                return r;
918

919
        r = context_load_installed_instances(context);
96✔
920
        if (r < 0)
96✔
921
                return r;
922

923
        *ret = TAKE_PTR(context);
96✔
924
        return 0;
96✔
925
}
926

927
static int context_make_online(Context **ret, const char *node) {
90✔
928
        _cleanup_(context_freep) Context* context = NULL;
90✔
929
        int r;
90✔
930

931
        assert(ret);
90✔
932

933
        /* Like context_make_offline(), but also communicates with the update source looking for new
934
         * versions (as long as --offline is not specified on the command line). */
935

936
        r = context_make_offline(&context, node, /* requires_enabled_transfers= */ true);
90✔
937
        if (r < 0)
90✔
938
                return r;
939

940
        if (!arg_offline) {
90✔
941
                r = context_load_available_instances(context);
76✔
942
                if (r < 0)
76✔
943
                        return r;
944
        }
945

946
        r = context_discover_update_sets(context);
90✔
947
        if (r < 0)
90✔
948
                return r;
949

950
        *ret = TAKE_PTR(context);
90✔
951
        return 0;
90✔
952
}
953

954
static int context_on_acquire_progress(const Transfer *t, const Instance *inst, unsigned percentage) {
76✔
955
        const Context *c = ASSERT_PTR(t->context);
76✔
956
        size_t i, n = c->n_transfers;
76✔
957
        uint64_t base, scaled;
76✔
958
        unsigned overall;
76✔
959

960
        for (i = 0; i < n; i++)
211✔
961
                if (c->transfers[i] == t)
211✔
962
                        break;
963
        assert(i < n); /* We should have found the index */
76✔
964

965
        base = (100 * 100 * i) / n;
76✔
966
        scaled = (100 * percentage) / n;
76✔
967
        overall = (unsigned) ((base + scaled) / 100);
76✔
968
        assert(overall <= 100);
76✔
969

970
        log_debug("Transfer %zu/%zu is %u%% complete (%u%% overall).", i+1, n, percentage, overall);
76✔
971
        return sd_notifyf(/* unset= */ false, "X_SYSUPDATE_PROGRESS=%u\n"
152✔
972
                                              "X_SYSUPDATE_TRANSFERS_LEFT=%zu\n"
973
                                              "X_SYSUPDATE_TRANSFERS_DONE=%zu\n"
974
                                              "STATUS=Updating to '%s' (%u%% complete).",
975
                                              overall, n - i, i, inst->metadata.version, overall);
76✔
976
}
977

978
static int context_apply(
18✔
979
                Context *c,
980
                const char *version,
981
                UpdateSet **ret_applied) {
982

983
        UpdateSet *us = NULL;
18✔
984
        int r;
18✔
985

986
        assert(c);
18✔
987

988
        if (version) {
18✔
989
                us = context_update_set_by_version(c, version);
×
990
                if (!us)
×
991
                        return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "Update '%s' not found.", version);
×
992
        } else {
993
                if (!c->candidate) {
18✔
994
                        log_info("No update needed.");
×
995

996
                        if (ret_applied)
×
997
                                *ret_applied = NULL;
×
998

999
                        return 0;
×
1000
                }
1001

1002
                us = c->candidate;
1003
        }
1004

1005
        if (FLAGS_SET(us->flags, UPDATE_INCOMPLETE))
18✔
1006
                log_info("Selected update '%s' is already installed, but incomplete. Repairing.", us->version);
4✔
1007
        else if (FLAGS_SET(us->flags, UPDATE_INSTALLED)) {
14✔
1008
                log_info("Selected update '%s' is already installed. Skipping update.", us->version);
×
1009

1010
                if (ret_applied)
×
1011
                        *ret_applied = NULL;
×
1012

1013
                return 0;
×
1014
        }
1015

1016
        if (!FLAGS_SET(us->flags, UPDATE_AVAILABLE))
18✔
1017
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Selected update '%s' is not available, refusing.", us->version);
×
1018
        if (FLAGS_SET(us->flags, UPDATE_OBSOLETE))
18✔
1019
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Selected update '%s' is obsolete, refusing.", us->version);
×
1020

1021
        if (!FLAGS_SET(us->flags, UPDATE_NEWEST))
18✔
1022
                log_notice("Selected update '%s' is not the newest, proceeding anyway.", us->version);
×
1023
        if (c->newest_installed && strverscmp_improved(c->newest_installed->version, us->version) > 0)
18✔
1024
                log_notice("Selected update '%s' is older than newest installed version, proceeding anyway.", us->version);
×
1025

1026
        log_info("Selected update '%s' for install.", us->version);
18✔
1027

1028
        (void) sd_notifyf(/* unset= */ false,
18✔
1029
                          "READY=1\n"
1030
                          "X_SYSUPDATE_VERSION=%s\n"
1031
                          "STATUS=Making room for '%s'.", us->version, us->version);
1032

1033
        /* Let's make some room. We make sure for each transfer we have one free space to fill. While
1034
         * removing stuff we'll protect the version we are trying to acquire. Why that? Maybe an earlier
1035
         * download succeeded already, in which case we shouldn't remove it just to acquire it again */
1036
        r = context_vacuum(
36✔
1037
                        c,
1038
                        /* space = */ 1,
1039
                        /* extra_protected_version = */ us->version);
18✔
1040
        if (r < 0)
18✔
1041
                return r;
1042

1043
        if (arg_sync)
18✔
1044
                sync();
18✔
1045

1046
        (void) sd_notifyf(/* unset= */ false,
18✔
1047
                          "STATUS=Updating to '%s'.", us->version);
1048

1049
        /* There should now be one instance picked for each transfer, and the order is the same */
1050
        assert(us->n_instances == c->n_transfers);
18✔
1051

1052
        for (size_t i = 0; i < c->n_transfers; i++) {
110✔
1053
                Instance *inst = us->instances[i];
92✔
1054
                Transfer *t = c->transfers[i];
92✔
1055

1056
                assert(inst); /* ditto */
92✔
1057

1058
                if (inst->resource == &t->target) { /* a present transfer in an incomplete installation */
92✔
1059
                        assert(FLAGS_SET(us->flags, UPDATE_INCOMPLETE));
18✔
1060
                        continue;
18✔
1061
                }
1062

1063
                r = transfer_acquire_instance(t, inst, context_on_acquire_progress, c);
74✔
1064
                if (r < 0)
74✔
1065
                        return r;
1066
        }
1067

1068
        if (arg_sync)
18✔
1069
                sync();
18✔
1070

1071
        (void) sd_notifyf(/* unset= */ false,
18✔
1072
                          "STATUS=Installing '%s'.", us->version);
1073

1074
        for (size_t i = 0; i < c->n_transfers; i++) {
110✔
1075
                Instance *inst = us->instances[i];
92✔
1076
                Transfer *t = c->transfers[i];
92✔
1077

1078
                if (inst->resource == &t->target)
92✔
1079
                        continue;
18✔
1080

1081
                r = transfer_install_instance(t, inst, arg_root);
74✔
1082
                if (r < 0)
74✔
1083
                        return r;
1084
        }
1085

1086
        log_info("%s Successfully installed update '%s'.", glyph(GLYPH_SPARKLES), us->version);
18✔
1087

1088
        if (ret_applied)
18✔
1089
                *ret_applied = us;
18✔
1090

1091
        return 1;
1092
}
1093

1094
static int process_image(
106✔
1095
                bool ro,
1096
                char **ret_mounted_dir,
1097
                LoopDevice **ret_loop_device) {
1098

1099
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
106✔
1100
        _cleanup_(umount_and_freep) char *mounted_dir = NULL;
106✔
1101
        int r;
106✔
1102

1103
        assert(ret_mounted_dir);
106✔
1104
        assert(ret_loop_device);
106✔
1105

1106
        if (!arg_image)
106✔
1107
                return 0;
1108

1109
        assert(!arg_root);
×
1110

1111
        r = mount_image_privately_interactively(
×
1112
                        arg_image,
1113
                        arg_image_policy,
1114
                        (ro ? DISSECT_IMAGE_READ_ONLY : 0) |
1115
                        DISSECT_IMAGE_FSCK |
1116
                        DISSECT_IMAGE_MKDIR |
1117
                        DISSECT_IMAGE_GROWFS |
1118
                        DISSECT_IMAGE_RELAX_VAR_CHECK |
1119
                        DISSECT_IMAGE_USR_NO_ROOT |
1120
                        DISSECT_IMAGE_GENERIC_ROOT |
1121
                        DISSECT_IMAGE_REQUIRE_ROOT |
1122
                        DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
1123
                        &mounted_dir,
1124
                        /* ret_dir_fd= */ NULL,
1125
                        &loop_device);
1126
        if (r < 0)
×
1127
                return r;
1128

1129
        arg_root = strdup(mounted_dir);
×
1130
        if (!arg_root)
×
1131
                return log_oom();
×
1132

1133
        *ret_mounted_dir = TAKE_PTR(mounted_dir);
×
1134
        *ret_loop_device = TAKE_PTR(loop_device);
×
1135

1136
        return 0;
×
1137
}
1138

1139
static int verb_list(int argc, char **argv, void *userdata) {
28✔
1140
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
28✔
1141
        _cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL;
28✔
1142
        _cleanup_(context_freep) Context* context = NULL;
28✔
1143
        _cleanup_strv_free_ char **appstream_urls = NULL;
28✔
1144
        const char *version;
28✔
1145
        int r;
28✔
1146

1147
        assert(argc <= 2);
28✔
1148
        version = argc >= 2 ? argv[1] : NULL;
28✔
1149

1150
        r = process_image(/* ro= */ true, &mounted_dir, &loop_device);
28✔
1151
        if (r < 0)
28✔
1152
                return r;
1153

1154
        r = context_make_online(&context, loop_device ? loop_device->node : NULL);
28✔
1155
        if (r < 0)
28✔
1156
                return r;
1157

1158
        if (version)
28✔
1159
                return context_show_version(context, version);
22✔
1160
        else if (!sd_json_format_enabled(arg_json_format_flags))
6✔
1161
                return context_show_table(context);
×
1162
        else {
1163
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
6✔
1164
                _cleanup_strv_free_ char **versions = NULL;
6✔
1165
                const char *current = NULL;
6✔
1166

1167
                FOREACH_ARRAY(update_set, context->update_sets, context->n_update_sets) {
26✔
1168
                        UpdateSet *us = *update_set;
20✔
1169

1170
                        if (FLAGS_SET(us->flags, UPDATE_INSTALLED) &&
20✔
1171
                            FLAGS_SET(us->flags, UPDATE_NEWEST))
1172
                                current = us->version;
6✔
1173

1174
                        r = strv_extend(&versions, us->version);
20✔
1175
                        if (r < 0)
20✔
1176
                                return log_oom();
×
1177
                }
1178

1179
                FOREACH_ARRAY(tr, context->transfers, context->n_transfers)
28✔
1180
                        STRV_FOREACH(appstream_url, (*tr)->appstream) {
22✔
1181
                                /* Avoid duplicates */
1182
                                if (strv_contains(appstream_urls, *appstream_url))
×
1183
                                        continue;
×
1184

1185
                                r = strv_extend(&appstream_urls, *appstream_url);
×
1186
                                if (r < 0)
×
1187
                                        return log_oom();
×
1188
                        }
1189

1190
                r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_STRING("current", current),
6✔
1191
                                          SD_JSON_BUILD_PAIR_STRV("all", versions),
1192
                                          SD_JSON_BUILD_PAIR_STRV("appstreamUrls", appstream_urls));
1193
                if (r < 0)
6✔
1194
                        return log_error_errno(r, "Failed to create JSON: %m");
×
1195

1196
                r = sd_json_variant_dump(json, arg_json_format_flags, stdout, NULL);
6✔
1197
                if (r < 0)
6✔
1198
                        return log_error_errno(r, "Failed to print JSON: %m");
×
1199

1200
                return 0;
1201
        }
1202
}
1203

1204
static int verb_features(int argc, char **argv, void *userdata) {
4✔
1205
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
4✔
1206
        _cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL;
4✔
1207
        _cleanup_(context_freep) Context* context = NULL;
4✔
1208
        _cleanup_(table_unrefp) Table *table = NULL;
4✔
1209
        const char *feature_id;
4✔
1210
        Feature *f;
4✔
1211
        int r;
4✔
1212

1213
        assert(argc <= 2);
4✔
1214
        feature_id = argc >= 2 ? argv[1] : NULL;
4✔
1215

1216
        r = process_image(/* ro= */ true, &mounted_dir, &loop_device);
4✔
1217
        if (r < 0)
4✔
1218
                return r;
1219

1220
        r = context_make_offline(&context, loop_device ? loop_device->node : NULL, /* requires_enabled_transfers= */ false);
4✔
1221
        if (r < 0)
4✔
1222
                return r;
1223

1224
        if (feature_id) {
4✔
1225
                _cleanup_strv_free_ char **transfers = NULL;
2✔
1226

1227
                f = hashmap_get(context->features, feature_id);
2✔
1228
                if (!f)
2✔
1229
                        return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
×
1230
                                               "Optional feature not found: %s",
1231
                                               feature_id);
1232

1233
                table = table_new_vertical();
2✔
1234
                if (!table)
2✔
1235
                        return log_oom();
×
1236

1237
                FOREACH_ARRAY(tr, context->transfers, context->n_transfers) {
12✔
1238
                        Transfer *t = *tr;
10✔
1239

1240
                        if (!strv_contains(t->features, f->id) && !strv_contains(t->requisite_features, f->id))
10✔
1241
                                continue;
10✔
1242

1243
                        r = strv_extend(&transfers, t->id);
×
1244
                        if (r < 0)
×
1245
                                return log_oom();
×
1246
                }
1247

1248
                FOREACH_ARRAY(tr, context->disabled_transfers, context->n_disabled_transfers) {
4✔
1249
                        Transfer *t = *tr;
2✔
1250

1251
                        if (!strv_contains(t->features, f->id) && !strv_contains(t->requisite_features, f->id))
2✔
1252
                                continue;
×
1253

1254
                        r = strv_extend(&transfers, t->id);
2✔
1255
                        if (r < 0)
2✔
1256
                                return log_oom();
×
1257
                }
1258

1259
                r = table_add_many(table,
2✔
1260
                                   TABLE_FIELD, "Name",
1261
                                   TABLE_STRING, f->id,
1262
                                   TABLE_FIELD, "Enabled",
1263
                                   TABLE_BOOLEAN, f->enabled);
1264
                if (r < 0)
2✔
1265
                        return table_log_add_error(r);
×
1266

1267
                if (f->description) {
2✔
1268
                        r = table_add_many(table, TABLE_FIELD, "Description", TABLE_STRING, f->description);
2✔
1269
                        if (r < 0)
2✔
1270
                                return table_log_add_error(r);
×
1271
                }
1272

1273
                if (f->documentation) {
2✔
1274
                        r = table_add_many(table,
×
1275
                                           TABLE_FIELD, "Documentation",
1276
                                           TABLE_STRING, f->documentation,
1277
                                           TABLE_SET_URL, f->documentation);
1278
                        if (r < 0)
×
1279
                                return table_log_add_error(r);
×
1280
                }
1281

1282
                if (f->appstream) {
2✔
1283
                        r = table_add_many(table,
×
1284
                                           TABLE_FIELD, "AppStream",
1285
                                           TABLE_STRING, f->appstream,
1286
                                           TABLE_SET_URL, f->appstream);
1287
                        if (r < 0)
×
1288
                                return table_log_add_error(r);
×
1289
                }
1290

1291
                if (!strv_isempty(transfers)) {
2✔
1292
                        r = table_add_many(table, TABLE_FIELD, "Transfers", TABLE_STRV_WRAPPED, transfers);
2✔
1293
                        if (r < 0)
2✔
1294
                                return table_log_add_error(r);
×
1295
                }
1296

1297
                return table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
2✔
1298
        } else if (FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) {
2✔
1299
                table = table_new("", "feature", "description", "documentation");
2✔
1300
                if (!table)
2✔
1301
                        return log_oom();
×
1302

1303
                HASHMAP_FOREACH(f, context->features) {
4✔
1304
                        r = table_add_many(table,
2✔
1305
                                           TABLE_BOOLEAN_CHECKMARK, f->enabled,
1306
                                           TABLE_SET_COLOR, ansi_highlight_green_red(f->enabled),
1307
                                           TABLE_STRING, f->id,
1308
                                           TABLE_STRING, f->description,
1309
                                           TABLE_STRING, f->documentation,
1310
                                           TABLE_SET_URL, f->documentation);
1311
                        if (r < 0)
2✔
1312
                                return table_log_add_error(r);
×
1313
                }
1314

1315
                return table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
2✔
1316
        } else {
1317
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
×
1318
                _cleanup_strv_free_ char **features = NULL;
×
1319

1320
                HASHMAP_FOREACH(f, context->features) {
×
1321
                        r = strv_extend(&features, f->id);
×
1322
                        if (r < 0)
×
1323
                                return log_oom();
×
1324
                }
1325

1326
                r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_STRV("features", features));
×
1327
                if (r < 0)
×
1328
                        return log_error_errno(r, "Failed to create JSON: %m");
×
1329

1330
                r = sd_json_variant_dump(json, arg_json_format_flags, stdout, NULL);
×
1331
                if (r < 0)
×
1332
                        return log_error_errno(r, "Failed to print JSON: %m");
×
1333
        }
1334

1335
        return 0;
×
1336
}
1337

1338
static int verb_check_new(int argc, char **argv, void *userdata) {
44✔
1339
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
44✔
1340
        _cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL;
44✔
1341
        _cleanup_(context_freep) Context* context = NULL;
44✔
1342
        int r;
44✔
1343

1344
        assert(argc <= 1);
44✔
1345

1346
        r = process_image(/* ro= */ true, &mounted_dir, &loop_device);
44✔
1347
        if (r < 0)
44✔
1348
                return r;
1349

1350
        r = context_make_online(&context, loop_device ? loop_device->node : NULL);
44✔
1351
        if (r < 0)
44✔
1352
                return r;
1353

1354
        if (!sd_json_format_enabled(arg_json_format_flags)) {
44✔
1355
                if (!context->candidate) {
40✔
1356
                        log_debug("No candidate found.");
22✔
1357
                        return EXIT_FAILURE;
22✔
1358
                }
1359

1360
                puts(context->candidate->version);
18✔
1361
        } else {
1362
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
4✔
1363

1364
                if (context->candidate)
4✔
1365
                        r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_STRING("available", context->candidate->version));
×
1366
                else
1367
                        r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_NULL("available"));
4✔
1368
                if (r < 0)
4✔
1369
                        return log_error_errno(r, "Failed to create JSON: %m");
×
1370

1371
                r = sd_json_variant_dump(json, arg_json_format_flags, stdout, NULL);
4✔
1372
                if (r < 0)
4✔
1373
                        return log_error_errno(r, "Failed to print JSON: %m");
×
1374
        }
1375

1376
        return EXIT_SUCCESS;
1377
}
1378

1379
static int verb_vacuum(int argc, char **argv, void *userdata) {
2✔
1380
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
2✔
1381
        _cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL;
2✔
1382
        _cleanup_(context_freep) Context* context = NULL;
2✔
1383
        int r;
2✔
1384

1385
        assert(argc <= 1);
2✔
1386

1387
        if (arg_instances_max < 1)
2✔
1388
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1389
                                      "The --instances-max argument must be >= 1 while vacuuming");
1390

1391
        r = process_image(/* ro= */ false, &mounted_dir, &loop_device);
2✔
1392
        if (r < 0)
2✔
1393
                return r;
1394

1395
        r = context_make_offline(&context, loop_device ? loop_device->node : NULL, /* requires_enabled_transfers= */ false);
2✔
1396
        if (r < 0)
2✔
1397
                return r;
1398

1399
        return context_vacuum(context, 0, NULL);
2✔
1400
}
1401

1402
static int verb_update(int argc, char **argv, void *userdata) {
18✔
1403
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
18✔
1404
        _cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL;
18✔
1405
        _cleanup_(context_freep) Context* context = NULL;
×
1406
        _cleanup_free_ char *booted_version = NULL;
18✔
1407
        UpdateSet *applied = NULL;
18✔
1408
        const char *version;
18✔
1409
        int r;
18✔
1410

1411
        assert(argc <= 2);
18✔
1412
        version = argc >= 2 ? argv[1] : NULL;
18✔
1413

1414
        if (arg_instances_max < 2)
18✔
1415
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1416
                                      "The --instances-max argument must be >= 2 while updating");
1417

1418
        if (arg_reboot) {
18✔
1419
                /* If automatic reboot on completion is requested, let's first determine the currently booted image */
1420

1421
                r = parse_os_release(arg_root, "IMAGE_VERSION", &booted_version);
×
1422
                if (r < 0)
×
1423
                        return log_error_errno(r, "Failed to parse /etc/os-release: %m");
×
1424
                if (!booted_version)
×
1425
                        return log_error_errno(SYNTHETIC_ERRNO(ENODATA), "/etc/os-release lacks IMAGE_VERSION field.");
×
1426
        }
1427

1428
        r = process_image(/* ro= */ false, &mounted_dir, &loop_device);
18✔
1429
        if (r < 0)
18✔
1430
                return r;
1431

1432
        r = context_make_online(&context, loop_device ? loop_device->node : NULL);
18✔
1433
        if (r < 0)
18✔
1434
                return r;
1435

1436
        r = context_apply(context, version, &applied);
18✔
1437
        if (r < 0)
18✔
1438
                return r;
1439

1440
        if (r > 0 && arg_reboot) {
18✔
1441
                assert(applied);
×
1442
                assert(booted_version);
×
1443

1444
                if (strverscmp_improved(applied->version, booted_version) > 0) {
×
1445
                        log_notice("Newly installed version is newer than booted version, rebooting.");
×
1446
                        return reboot_now();
×
1447
                }
1448

1449
                if (strverscmp_improved(applied->version, booted_version) == 0 &&
×
1450
                    FLAGS_SET(applied->flags, UPDATE_INCOMPLETE)) {
×
1451
                        log_notice("Currently booted version was incomplete and has been repaired, rebooting.");
×
1452
                        return reboot_now();
×
1453
                }
1454

1455
                log_info("Booted version is newer or identical to newly installed version, not rebooting.");
18✔
1456
        }
1457

1458
        return 0;
1459
}
1460

1461
static int verb_pending_or_reboot(int argc, char **argv, void *userdata) {
×
1462
        _cleanup_(context_freep) Context* context = NULL;
×
1463
        _cleanup_free_ char *booted_version = NULL;
×
1464
        int r;
×
1465

1466
        assert(argc == 1);
×
1467

1468
        if (arg_image || arg_root)
×
1469
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1470
                                       "The --root=/--image= switches may not be combined with the '%s' operation.", argv[0]);
1471

1472
        r = context_make_offline(&context, /* node= */ NULL, /* requires_enabled_transfers= */ true);
×
1473
        if (r < 0)
×
1474
                return r;
1475

NEW
1476
        log_info("Determining installed update sets%s", glyph(GLYPH_ELLIPSIS));
×
1477

1478
        r = context_discover_update_sets_by_flag(context, UPDATE_INSTALLED);
×
1479
        if (r < 0)
×
1480
                return r;
1481
        if (!context->newest_installed)
×
1482
                return log_error_errno(SYNTHETIC_ERRNO(ENODATA), "Couldn't find any suitable installed versions.");
×
1483

1484
        r = parse_os_release(arg_root, "IMAGE_VERSION", &booted_version);
×
1485
        if (r < 0) /* yes, arg_root is NULL here, but we have to pass something, and it's a lot more readable
×
1486
                    * if we see what the first argument is about */
1487
                return log_error_errno(r, "Failed to parse /etc/os-release: %m");
×
1488
        if (!booted_version)
×
1489
                return log_error_errno(SYNTHETIC_ERRNO(ENODATA), "/etc/os-release lacks IMAGE_VERSION= field.");
×
1490

1491
        r = strverscmp_improved(context->newest_installed->version, booted_version);
×
1492
        if (r > 0) {
×
1493
                log_notice("Newest installed version '%s' is newer than booted version '%s'.%s",
×
1494
                           context->newest_installed->version, booted_version,
1495
                           streq(argv[0], "pending") ? " Reboot recommended." : "");
1496

1497
                if (streq(argv[0], "reboot"))
×
1498
                        return reboot_now();
×
1499

1500
                return EXIT_SUCCESS;
1501
        } else if (r == 0)
×
1502
                log_info("Newest installed version '%s' matches booted version '%s'.",
×
1503
                         context->newest_installed->version, booted_version);
1504
        else
1505
                log_warning("Newest installed version '%s' is older than booted version '%s'.",
×
1506
                            context->newest_installed->version, booted_version);
1507

1508
        if (streq(argv[0], "pending")) /* When called as 'pending' tell the caller via failure exit code that there's nothing newer installed */
×
1509
                return EXIT_FAILURE;
×
1510

1511
        return EXIT_SUCCESS;
1512
}
1513

1514
static int component_name_valid(const char *c) {
12✔
1515
        _cleanup_free_ char *j = NULL;
12✔
1516

1517
        /* See if the specified string enclosed in the directory prefix+suffix would be a valid file name */
1518

1519
        if (isempty(c))
24✔
1520
                return false;
1521

1522
        if (string_has_cc(c, NULL))
12✔
1523
                return false;
1524

1525
        if (!utf8_is_valid(c))
12✔
1526
                return false;
1527

1528
        j = strjoin("sysupdate.", c, ".d");
12✔
1529
        if (!j)
12✔
1530
                return -ENOMEM;
1531

1532
        return filename_is_valid(j);
12✔
1533
}
1534

1535
static int verb_components(int argc, char **argv, void *userdata) {
10✔
1536
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
10✔
1537
        _cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL;
10✔
1538
        _cleanup_set_free_ Set *names = NULL;
×
1539
        _cleanup_free_ char **z = NULL; /* We use simple free() rather than strv_free() here, since set_free() will free the strings for us */
10✔
1540
        char **l = CONF_PATHS_STRV("");
10✔
1541
        bool has_default_component = false;
10✔
1542
        int r;
10✔
1543

1544
        assert(argc <= 1);
10✔
1545

1546
        r = process_image(/* ro= */ false, &mounted_dir, &loop_device);
10✔
1547
        if (r < 0)
10✔
1548
                return r;
1549

1550
        STRV_FOREACH(i, l) {
50✔
1551
                _cleanup_closedir_ DIR *d = NULL;
40✔
1552
                _cleanup_free_ char *p = NULL;
40✔
1553

1554
                r = chase_and_opendir(*i, arg_root, CHASE_PREFIX_ROOT, &p, &d);
40✔
1555
                if (r == -ENOENT)
40✔
1556
                        continue;
×
1557
                if (r < 0)
40✔
1558
                        return log_error_errno(r, "Failed to open directory '%s': %m", *i);
×
1559

1560
                for (;;) {
17,958✔
1561
                        _cleanup_free_ char *n = NULL;
17,910✔
1562
                        struct dirent *de;
17,958✔
1563
                        const char *e, *a;
17,958✔
1564

1565
                        de = readdir_ensure_type(d);
17,958✔
1566
                        if (!de) {
17,958✔
1567
                                if (errno != 0)
40✔
1568
                                        return log_error_errno(errno, "Failed to enumerate directory '%s': %m", p);
×
1569

1570
                                break;
40✔
1571
                        }
1572

1573
                        if (de->d_type != DT_DIR)
17,918✔
1574
                                continue;
16,190✔
1575

1576
                        if (dot_or_dot_dot(de->d_name))
1,728✔
1577
                                continue;
80✔
1578

1579
                        if (streq(de->d_name, "sysupdate.d")) {
1,648✔
1580
                                has_default_component = true;
10✔
1581
                                continue;
10✔
1582
                        }
1583

1584
                        e = startswith(de->d_name, "sysupdate.");
1,638✔
1585
                        if (!e)
1,638✔
1586
                                continue;
1,630✔
1587

1588
                        a = endswith(e, ".d");
8✔
1589
                        if (!a)
8✔
1590
                                continue;
×
1591

1592
                        n = strndup(e, a - e);
8✔
1593
                        if (!n)
8✔
1594
                                return log_oom();
×
1595

1596
                        r = component_name_valid(n);
8✔
1597
                        if (r < 0)
8✔
1598
                                return log_error_errno(r, "Unable to validate component name: %m");
×
1599
                        if (r == 0)
8✔
1600
                                continue;
×
1601

1602
                        r = set_ensure_consume(&names, &string_hash_ops_free, TAKE_PTR(n));
8✔
1603
                        if (r < 0 && r != -EEXIST)
8✔
1604
                                return log_error_errno(r, "Failed to add component to set: %m");
×
1605
                }
1606
        }
1607

1608
        z = set_get_strv(names);
10✔
1609
        if (!z)
10✔
1610
                return log_oom();
×
1611

1612
        strv_sort(z);
10✔
1613

1614
        if (!sd_json_format_enabled(arg_json_format_flags)) {
10✔
1615
                if (!has_default_component && set_isempty(names)) {
×
1616
                        log_info("No components defined.");
×
1617
                        return 0;
×
1618
                }
1619

1620
                if (has_default_component)
×
1621
                        printf("%s<default>%s\n",
×
1622
                               ansi_highlight(), ansi_normal());
1623

1624
                STRV_FOREACH(i, z)
×
1625
                        puts(*i);
×
1626
        } else {
1627
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
10✔
1628

1629
                r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_BOOLEAN("default", has_default_component),
10✔
1630
                                          SD_JSON_BUILD_PAIR_STRV("components", z));
1631
                if (r < 0)
10✔
1632
                        return log_error_errno(r, "Failed to create JSON: %m");
×
1633

1634
                r = sd_json_variant_dump(json, arg_json_format_flags, stdout, NULL);
10✔
1635
                if (r < 0)
10✔
1636
                        return log_error_errno(r, "Failed to print JSON: %m");
×
1637
        }
1638

1639
        return 0;
1640
}
1641

1642
static int verb_help(int argc, char **argv, void *userdata) {
×
1643
        _cleanup_free_ char *link = NULL;
×
1644
        int r;
×
1645

1646
        r = terminal_urlify_man("systemd-sysupdate", "8", &link);
×
1647
        if (r < 0)
×
1648
                return log_oom();
×
1649

1650
        printf("%1$s [OPTIONS...] [VERSION]\n"
×
1651
               "\n%5$sUpdate OS images.%6$s\n"
1652
               "\n%3$sCommands:%4$s\n"
1653
               "  list [VERSION]          Show installed and available versions\n"
1654
               "  features [FEATURE]      Show optional features\n"
1655
               "  check-new               Check if there's a new version available\n"
1656
               "  update [VERSION]        Install new version now\n"
1657
               "  vacuum                  Make room, by deleting old versions\n"
1658
               "  pending                 Report whether a newer version is installed than\n"
1659
               "                          currently booted\n"
1660
               "  reboot                  Reboot if a newer version is installed than booted\n"
1661
               "  components              Show list of components\n"
1662
               "  -h --help               Show this help\n"
1663
               "     --version            Show package version\n"
1664
               "\n%3$sOptions:%4$s\n"
1665
               "  -C --component=NAME     Select component to update\n"
1666
               "     --definitions=DIR    Find transfer definitions in specified directory\n"
1667
               "     --root=PATH          Operate on an alternate filesystem root\n"
1668
               "     --image=PATH         Operate on disk image as filesystem root\n"
1669
               "     --image-policy=POLICY\n"
1670
               "                          Specify disk image dissection policy\n"
1671
               "  -m --instances-max=INT  How many instances to maintain\n"
1672
               "     --sync=BOOL          Controls whether to sync data to disk\n"
1673
               "     --verify=BOOL        Force signature verification on or off\n"
1674
               "     --reboot             Reboot after updating to newer version\n"
1675
               "     --offline            Do not fetch metadata from the network\n"
1676
               "     --no-pager           Do not pipe output into a pager\n"
1677
               "     --no-legend          Do not show the headers and footers\n"
1678
               "     --json=pretty|short|off\n"
1679
               "                          Generate JSON output\n"
1680
               "     --transfer-source=PATH\n"
1681
               "                          Specify the directory to transfer sources from\n"
1682
               "\nSee the %2$s for details.\n",
1683
               program_invocation_short_name,
1684
               link,
1685
               ansi_underline(),
1686
               ansi_normal(),
1687
               ansi_highlight(),
1688
               ansi_normal());
1689

1690
        return 0;
1691
}
1692

1693
static int parse_argv(int argc, char *argv[]) {
106✔
1694

1695
        enum {
106✔
1696
                ARG_VERSION = 0x100,
1697
                ARG_NO_PAGER,
1698
                ARG_NO_LEGEND,
1699
                ARG_SYNC,
1700
                ARG_DEFINITIONS,
1701
                ARG_JSON,
1702
                ARG_ROOT,
1703
                ARG_IMAGE,
1704
                ARG_IMAGE_POLICY,
1705
                ARG_REBOOT,
1706
                ARG_VERIFY,
1707
                ARG_OFFLINE,
1708
                ARG_TRANSFER_SOURCE,
1709
        };
1710

1711
        static const struct option options[] = {
106✔
1712
                { "help",              no_argument,       NULL, 'h'                   },
1713
                { "version",           no_argument,       NULL, ARG_VERSION           },
1714
                { "no-pager",          no_argument,       NULL, ARG_NO_PAGER          },
1715
                { "no-legend",         no_argument,       NULL, ARG_NO_LEGEND         },
1716
                { "definitions",       required_argument, NULL, ARG_DEFINITIONS       },
1717
                { "instances-max",     required_argument, NULL, 'm'                   },
1718
                { "sync",              required_argument, NULL, ARG_SYNC              },
1719
                { "json",              required_argument, NULL, ARG_JSON              },
1720
                { "root",              required_argument, NULL, ARG_ROOT              },
1721
                { "image",             required_argument, NULL, ARG_IMAGE             },
1722
                { "image-policy",      required_argument, NULL, ARG_IMAGE_POLICY      },
1723
                { "reboot",            no_argument,       NULL, ARG_REBOOT            },
1724
                { "component",         required_argument, NULL, 'C'                   },
1725
                { "verify",            required_argument, NULL, ARG_VERIFY            },
1726
                { "offline",           no_argument,       NULL, ARG_OFFLINE           },
1727
                { "transfer-source",   required_argument, NULL, ARG_TRANSFER_SOURCE   },
1728
                {}
1729
        };
1730

1731
        int c, r;
106✔
1732

1733
        assert(argc >= 0);
106✔
1734
        assert(argv);
106✔
1735

1736
        while ((c = getopt_long(argc, argv, "hm:C:", options, NULL)) >= 0) {
234✔
1737

1738
                switch (c) {
128✔
1739

1740
                case 'h':
×
1741
                        return verb_help(0, NULL, NULL);
×
1742

1743
                case ARG_VERSION:
×
1744
                        return version();
×
1745

1746
                case ARG_NO_PAGER:
×
1747
                        arg_pager_flags |= PAGER_DISABLE;
×
1748
                        break;
×
1749

1750
                case ARG_NO_LEGEND:
×
1751
                        arg_legend = false;
×
1752
                        break;
×
1753

1754
                case 'm':
×
1755
                        r = safe_atou64(optarg, &arg_instances_max);
×
1756
                        if (r < 0)
×
1757
                                return log_error_errno(r, "Failed to parse --instances-max= parameter: %s", optarg);
×
1758

1759
                        break;
1760

1761
                case ARG_SYNC:
×
1762
                        r = parse_boolean_argument("--sync=", optarg, &arg_sync);
×
1763
                        if (r < 0)
×
1764
                                return r;
1765
                        break;
1766

1767
                case ARG_DEFINITIONS:
×
1768
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_definitions);
×
1769
                        if (r < 0)
×
1770
                                return r;
1771
                        break;
1772

1773
                case ARG_JSON:
34✔
1774
                        r = parse_json_argument(optarg, &arg_json_format_flags);
34✔
1775
                        if (r <= 0)
34✔
1776
                                return r;
1777

1778
                        break;
1779

1780
                case ARG_ROOT:
×
1781
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_root);
×
1782
                        if (r < 0)
×
1783
                                return r;
1784
                        break;
1785

1786
                case ARG_IMAGE:
×
1787
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_image);
×
1788
                        if (r < 0)
×
1789
                                return r;
1790
                        break;
1791

1792
                case ARG_IMAGE_POLICY:
×
1793
                        r = parse_image_policy_argument(optarg, &arg_image_policy);
×
1794
                        if (r < 0)
×
1795
                                return r;
1796
                        break;
1797

1798
                case ARG_REBOOT:
×
1799
                        arg_reboot = true;
×
1800
                        break;
×
1801

1802
                case 'C':
4✔
1803
                        if (isempty(optarg)) {
4✔
1804
                                arg_component = mfree(arg_component);
×
1805
                                break;
×
1806
                        }
1807

1808
                        r = component_name_valid(optarg);
4✔
1809
                        if (r < 0)
4✔
1810
                                return log_error_errno(r, "Failed to determine if component name is valid: %m");
×
1811
                        if (r == 0)
4✔
1812
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Component name invalid: %s", optarg);
×
1813

1814
                        r = free_and_strdup_warn(&arg_component, optarg);
4✔
1815
                        if (r < 0)
4✔
1816
                                return r;
1817

1818
                        break;
1819

1820
                case ARG_VERIFY: {
76✔
1821
                        bool b;
76✔
1822

1823
                        r = parse_boolean_argument("--verify=", optarg, &b);
76✔
1824
                        if (r < 0)
76✔
1825
                                return r;
×
1826

1827
                        arg_verify = b;
76✔
1828
                        break;
76✔
1829
                }
1830

1831
                case ARG_OFFLINE:
14✔
1832
                        arg_offline = true;
14✔
1833
                        break;
14✔
1834

1835
                case ARG_TRANSFER_SOURCE:
×
1836
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_transfer_source);
×
1837
                        if (r < 0)
×
1838
                                return r;
1839

1840
                        break;
1841

1842
                case '?':
1843
                        return -EINVAL;
1844

1845
                default:
×
1846
                        assert_not_reached();
×
1847
                }
1848
        }
1849

1850
        if (arg_image && arg_root)
106✔
1851
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported.");
×
1852

1853
        if ((arg_image || arg_root) && arg_reboot)
106✔
1854
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "The --reboot switch may not be combined with --root= or --image=.");
×
1855

1856
        if (arg_definitions && arg_component)
106✔
1857
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "The --definitions= and --component= switches may not be combined.");
×
1858

1859
        return 1;
1860
}
1861

1862
static int sysupdate_main(int argc, char *argv[]) {
106✔
1863

1864
        static const Verb verbs[] = {
106✔
1865
                { "list",       VERB_ANY, 2, VERB_DEFAULT, verb_list              },
1866
                { "components", VERB_ANY, 1, 0,            verb_components        },
1867
                { "features",   VERB_ANY, 2, 0,            verb_features          },
1868
                { "check-new",  VERB_ANY, 1, 0,            verb_check_new         },
1869
                { "update",     VERB_ANY, 2, 0,            verb_update            },
1870
                { "vacuum",     VERB_ANY, 1, 0,            verb_vacuum            },
1871
                { "reboot",     1,        1, 0,            verb_pending_or_reboot },
1872
                { "pending",    1,        1, 0,            verb_pending_or_reboot },
1873
                { "help",       VERB_ANY, 1, 0,            verb_help              },
1874
                {}
1875
        };
1876

1877
        return dispatch_verb(argc, argv, verbs, NULL);
106✔
1878
}
1879

1880
static int run(int argc, char *argv[]) {
106✔
1881
        int r;
106✔
1882

1883
        log_setup();
106✔
1884

1885
        r = parse_argv(argc, argv);
106✔
1886
        if (r <= 0)
106✔
1887
                return r;
1888

1889
        /* SIGCHLD signal must be blocked for sd_event_add_child to work */
1890
        assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGCHLD) >= 0);
106✔
1891

1892
        return sysupdate_main(argc, argv);
106✔
1893
}
1894

1895
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);
212✔
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