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

systemd / systemd / 15336122756

29 May 2025 09:26PM UTC coverage: 72.064% (+0.02%) from 72.04%
15336122756

push

github

yuwata
NEWS: fix typos

299688 of 415863 relevant lines covered (72.06%)

699722.54 hits per line

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

70.73
/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 "sd-daemon.h"
7

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

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

58
STATIC_DESTRUCTOR_REGISTER(arg_definitions, freep);
106✔
59
STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
106✔
60
STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
106✔
61
STATIC_DESTRUCTOR_REGISTER(arg_component, freep);
106✔
62
STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
106✔
63
STATIC_DESTRUCTOR_REGISTER(arg_transfer_source, freep);
106✔
64

65
const Specifier specifier_table[] = {
66
        COMMON_SYSTEM_SPECIFIERS,
67
        COMMON_TMP_SPECIFIERS,
68
        {}
69
};
70

71
typedef struct Context {
72
        Transfer **transfers;
73
        size_t n_transfers;
74

75
        Transfer **disabled_transfers;
76
        size_t n_disabled_transfers;
77

78
        Hashmap *features; /* Defined features, keyed by ID */
79

80
        UpdateSet **update_sets;
81
        size_t n_update_sets;
82

83
        UpdateSet *newest_installed, *candidate;
84

85
        Hashmap *web_cache; /* Cache for downloaded resources, keyed by URL */
86
} Context;
87

88
static Context* context_free(Context *c) {
96✔
89
        if (!c)
96✔
90
                return NULL;
91

92
        FOREACH_ARRAY(tr, c->transfers, c->n_transfers)
570✔
93
                transfer_free(*tr);
474✔
94
        free(c->transfers);
96✔
95

96
        FOREACH_ARRAY(tr, c->disabled_transfers, c->n_disabled_transfers)
178✔
97
                transfer_free(*tr);
82✔
98
        free(c->disabled_transfers);
96✔
99

100
        hashmap_free(c->features);
96✔
101

102
        FOREACH_ARRAY(us, c->update_sets, c->n_update_sets)
458✔
103
                update_set_free(*us);
362✔
104
        free(c->update_sets);
96✔
105

106
        hashmap_free(c->web_cache);
96✔
107

108
        return mfree(c);
96✔
109
}
110

111
DEFINE_TRIVIAL_CLEANUP_FUNC(Context*, context_free);
282✔
112

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

118
static void free_transfers(Transfer **array, size_t n) {
×
119
        FOREACH_ARRAY(t, array, n)
×
120
                transfer_free(*t);
×
121
        free(array);
×
122
}
×
123

124
static int read_definitions(
96✔
125
                Context *c,
126
                const char **dirs,
127
                const char *suffix,
128
                const char *node) {
129

130
        _cleanup_strv_free_ char **files = NULL;
96✔
131
        Transfer **transfers = NULL, **disabled = NULL;
96✔
132
        size_t n_transfers = 0, n_disabled = 0;
96✔
133
        int r;
96✔
134

135
        CLEANUP_ARRAY(transfers, n_transfers, free_transfers);
96✔
136
        CLEANUP_ARRAY(disabled, n_disabled, free_transfers);
96✔
137

138
        assert(c);
96✔
139
        assert(dirs);
96✔
140
        assert(suffix);
96✔
141

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

146
        STRV_FOREACH(p, files) {
652✔
147
                _cleanup_(transfer_freep) Transfer *t = NULL;
×
148
                Transfer **appended;
556✔
149

150
                t = transfer_new(c);
556✔
151
                if (!t)
556✔
152
                        return log_oom();
×
153

154
                r = transfer_read_definition(t, *p, dirs, c->features);
556✔
155
                if (r < 0)
556✔
156
                        return r;
157

158
                r = transfer_resolve_paths(t, arg_root, node);
556✔
159
                if (r < 0)
556✔
160
                        return r;
161

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

171
        c->transfers = TAKE_PTR(transfers);
96✔
172
        c->n_transfers = n_transfers;
96✔
173
        c->disabled_transfers = TAKE_PTR(disabled);
96✔
174
        c->n_disabled_transfers = n_disabled;
96✔
175
        return 0;
96✔
176
}
177

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

182
        assert(c);
96✔
183

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

190
                dirs = new0(char*, strv_length(l) + 1);
4✔
191
                if (!dirs)
4✔
192
                        return log_oom();
×
193

194
                STRV_FOREACH(dir, l) {
20✔
195
                        char *j;
16✔
196

197
                        j = strjoin(*dir, "sysupdate.", arg_component, ".d");
16✔
198
                        if (!j)
16✔
199
                                return log_oom();
×
200

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

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

216
        STRV_FOREACH(p, files) {
188✔
217
                _cleanup_(feature_unrefp) Feature *f = NULL;
×
218

219
                f = feature_new();
92✔
220
                if (!f)
92✔
221
                        return log_oom();
×
222

223
                r = feature_read_definition(f, *p, (const char**) dirs);
92✔
224
                if (r < 0)
92✔
225
                        return r;
226

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

233
        r = read_definitions(c, (const char**) dirs, ".transfer", node);
96✔
234
        if (r < 0)
96✔
235
                return r;
236

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

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

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

253
                return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
×
254
                                       "No transfer definitions found.");
255
        }
256

257
        return 0;
258
}
259

260
static int context_load_installed_instances(Context *c) {
96✔
261
        int r;
96✔
262

263
        assert(c);
96✔
264

265
        log_info("Discovering installed instances%s", glyph(GLYPH_ELLIPSIS));
192✔
266

267
        FOREACH_ARRAY(tr, c->transfers, c->n_transfers) {
570✔
268
                Transfer *t = *tr;
474✔
269

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

278
        FOREACH_ARRAY(tr, c->disabled_transfers, c->n_disabled_transfers) {
178✔
279
                Transfer *t = *tr;
82✔
280

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

289
        return 0;
290
}
291

292
static int context_load_available_instances(Context *c) {
76✔
293
        int r;
76✔
294

295
        assert(c);
76✔
296

297
        log_info("Discovering available instances%s", glyph(GLYPH_ELLIPSIS));
152✔
298

299
        FOREACH_ARRAY(tr, c->transfers, c->n_transfers) {
454✔
300
                Transfer *t = *tr;
378✔
301

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

310
        return 0;
311
}
312

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

318
        assert(c);
166✔
319
        assert(IN_SET(flags, UPDATE_AVAILABLE, UPDATE_INSTALLED));
166✔
320

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

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

332
                        assert(*tr);
3,570✔
333

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

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

344
                                assert(i);
9,290✔
345

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

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

354
                                if (free_and_strdup(&cursor, i->metadata.version) < 0)
614✔
355
                                        return log_oom();
×
356

357
                                break; /* All subsequent instances will be older than this one */
358
                        }
359

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

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

371
                cursor_instances = new0(Instance*, c->n_transfers);
614✔
372
                if (!cursor_instances)
614✔
373
                        return log_oom();
×
374

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

380
                        assert(t);
3,044✔
381

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

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

405
                        cursor_instances[k] = match;
2,988✔
406

407
                        if (t->min_version && strverscmp_improved(t->min_version, cursor) > 0)
2,988✔
408
                                extra_flags |= UPDATE_OBSOLETE;
×
409

410
                        if (strv_contains(t->protected_versions, cursor))
2,988✔
411
                                extra_flags |= UPDATE_PROTECTED;
×
412
                }
413

414
                r = free_and_strdup_warn(&boundary, cursor);
614✔
415
                if (r < 0)
614✔
416
                        return r;
417

418
                if (skip)
614✔
419
                        continue;
56✔
420

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

425
                        if (strverscmp_improved(u->version, cursor) != 0)
1,032✔
426
                                continue;
836✔
427

428
                        /* Merge in what we've learned and continue onto the next version */
429

430
                        if (FLAGS_SET(u->flags, UPDATE_INCOMPLETE)) {
196✔
431
                                assert(u->n_instances == c->n_transfers);
72✔
432

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

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

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

445
                        u->flags |= flags | extra_flags;
196✔
446

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

454
                        skip = true;
196✔
455
                        newest_found = true;
196✔
456
                        break;
196✔
457
                }
458

459
                if (skip)
558✔
460
                        continue;
196✔
461

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

466
                us = new(UpdateSet, 1);
362✔
467
                if (!us)
362✔
468
                        return log_oom();
×
469

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

477
                c->update_sets[c->n_update_sets++] = us;
362✔
478

479
                newest_found = true;
362✔
480

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

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

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

495
        return 0;
496
}
497

498
static int context_discover_update_sets(Context *c) {
90✔
499
        int r;
90✔
500

501
        assert(c);
90✔
502

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

505
        r = context_discover_update_sets_by_flag(c, UPDATE_INSTALLED);
90✔
506
        if (r < 0)
90✔
507
                return r;
508

509
        if (!arg_offline) {
90✔
510
                log_info("Determining available update sets%s", glyph(GLYPH_ELLIPSIS));
152✔
511

512
                r = context_discover_update_sets_by_flag(c, UPDATE_AVAILABLE);
76✔
513
                if (r < 0)
76✔
514
                        return r;
515
        }
516

517
        typesafe_qsort(c->update_sets, c->n_update_sets, update_set_cmp);
90✔
518
        return 0;
90✔
519
}
520

521
static int context_show_table(Context *c) {
×
522
        _cleanup_(table_unrefp) Table *t = NULL;
×
523
        int r;
×
524

525
        assert(c);
×
526

527
        t = table_new("", "version", "installed", "available", "assessment");
×
528
        if (!t)
×
529
                return log_oom();
×
530

531
        (void) table_set_align_percent(t, table_get_cell(t, 0, 0), 100);
×
532
        (void) table_set_align_percent(t, table_get_cell(t, 0, 2), 50);
×
533
        (void) table_set_align_percent(t, table_get_cell(t, 0, 3), 50);
×
534

535
        FOREACH_ARRAY(update_set, c->update_sets, c->n_update_sets) {
×
536
                UpdateSet *us = *update_set;
×
537
                const char *color;
×
538

539
                color = update_set_flags_to_color(us->flags);
×
540

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

556
        return table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, arg_legend);
×
557
}
558

559
static UpdateSet* context_update_set_by_version(Context *c, const char *version) {
22✔
560
        assert(c);
22✔
561
        assert(version);
22✔
562

563
        FOREACH_ARRAY(update_set, c->update_sets, c->n_update_sets)
42✔
564
                if (streq((*update_set)->version, version))
42✔
565
                        return *update_set;
566

567
        return NULL;
568
}
569

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

581
        assert(c);
22✔
582
        assert(version);
22✔
583

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

588
        if (arg_json_format_flags & (SD_JSON_FORMAT_OFF|SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_PRETTY_AUTO))
22✔
589
                (void) pager_open(arg_pager_flags);
10✔
590

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

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

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

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

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

626
                if (tr->target.type == RESOURCE_PARTITION)
114✔
627
                        show_partition_columns = true;
44✔
628
                if (RESOURCE_IS_FILESYSTEM(tr->target.type))
114✔
629
                        show_fs_columns = true;
630

631
                STRV_FOREACH(changelog, tr->changelog) {
114✔
632
                        assert(*changelog);
×
633

634
                        _cleanup_free_ char *changelog_url = strreplace(*changelog, "@v", version);
×
635
                        if (!changelog_url)
×
636
                                return log_oom();
×
637

638
                        /* Avoid duplicates */
639
                        if (strv_contains(changelog_urls, changelog_url))
×
640
                                continue;
×
641

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

649
        FOREACH_ARRAY(inst, us->instances, us->n_instances) {
136✔
650
                Instance *i = *inst;
114✔
651

652
                if (!i) {
114✔
653
                        assert(FLAGS_SET(us->flags, UPDATE_INCOMPLETE));
4✔
654
                        continue;
4✔
655
                }
656

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

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

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

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

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

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

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

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

719
                if (i->metadata.no_auto >= 0) {
110✔
720
                        bool b;
×
721

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

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

740
                if (i->metadata.growfs >= 0) {
110✔
741
                        bool b;
×
742

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

751
                if (i->metadata.sha256sum_set) {
110✔
752
                        _cleanup_free_ char *formatted = NULL;
×
753

754
                        have_sha256 = true;
×
755

756
                        formatted = hexmem(i->metadata.sha256sum, sizeof(i->metadata.sha256sum));
×
757
                        if (!formatted)
×
758
                                return log_oom();
×
759

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

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

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

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

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

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

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

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

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

838
                return 0;
839
        }
840
}
841

842
static int context_vacuum(
20✔
843
                Context *c,
844
                uint64_t space,
845
                const char *extra_protected_version) {
846

847
        size_t disabled_count = 0;
20✔
848
        int r, count = 0;
20✔
849

850
        assert(c);
20✔
851

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

857
        FOREACH_ARRAY(tr, c->transfers, c->n_transfers) {
122✔
858
                Transfer *t = *tr;
102✔
859

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

864
                r = transfer_vacuum(t, space, extra_protected_version);
84✔
865
                if (r < 0)
84✔
866
                        return r;
867

868
                count = MAX(count, r);
84✔
869
        }
870

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

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

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

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

902
        return 0;
903
}
904

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

909
        assert(ret);
96✔
910

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

914
        context = context_new();
96✔
915
        if (!context)
96✔
916
                return log_oom();
×
917

918
        r = context_read_definitions(context, node, requires_enabled_transfers);
96✔
919
        if (r < 0)
96✔
920
                return r;
921

922
        r = context_load_installed_instances(context);
96✔
923
        if (r < 0)
96✔
924
                return r;
925

926
        *ret = TAKE_PTR(context);
96✔
927
        return 0;
96✔
928
}
929

930
static int context_make_online(Context **ret, const char *node) {
90✔
931
        _cleanup_(context_freep) Context* context = NULL;
90✔
932
        int r;
90✔
933

934
        assert(ret);
90✔
935

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

939
        r = context_make_offline(&context, node, /* requires_enabled_transfers= */ true);
90✔
940
        if (r < 0)
90✔
941
                return r;
942

943
        if (!arg_offline) {
90✔
944
                r = context_load_available_instances(context);
76✔
945
                if (r < 0)
76✔
946
                        return r;
947
        }
948

949
        r = context_discover_update_sets(context);
90✔
950
        if (r < 0)
90✔
951
                return r;
952

953
        *ret = TAKE_PTR(context);
90✔
954
        return 0;
90✔
955
}
956

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

963
        for (i = 0; i < n; i++)
184✔
964
                if (c->transfers[i] == t)
184✔
965
                        break;
966
        assert(i < n); /* We should have found the index */
66✔
967

968
        base = (100 * 100 * i) / n;
66✔
969
        scaled = (100 * percentage) / n;
66✔
970
        overall = (unsigned) ((base + scaled) / 100);
66✔
971
        assert(overall <= 100);
66✔
972

973
        log_debug("Transfer %zu/%zu is %u%% complete (%u%% overall).", i+1, n, percentage, overall);
66✔
974
        return sd_notifyf(/* unset_environment=*/ false, "X_SYSUPDATE_PROGRESS=%u\n"
132✔
975
                                              "X_SYSUPDATE_TRANSFERS_LEFT=%zu\n"
976
                                              "X_SYSUPDATE_TRANSFERS_DONE=%zu\n"
977
                                              "STATUS=Updating to '%s' (%u%% complete).",
978
                                              overall, n - i, i, inst->metadata.version, overall);
66✔
979
}
980

981
static int context_apply(
18✔
982
                Context *c,
983
                const char *version,
984
                UpdateSet **ret_applied) {
985

986
        UpdateSet *us = NULL;
18✔
987
        int r;
18✔
988

989
        assert(c);
18✔
990

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

999
                        if (ret_applied)
×
1000
                                *ret_applied = NULL;
×
1001

1002
                        return 0;
×
1003
                }
1004

1005
                us = c->candidate;
1006
        }
1007

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

1013
                if (ret_applied)
×
1014
                        *ret_applied = NULL;
×
1015

1016
                return 0;
×
1017
        }
1018

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

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

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

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

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

1046
        if (arg_sync)
18✔
1047
                sync();
18✔
1048

1049
        (void) sd_notifyf(/* unset_environment=*/ false,
18✔
1050
                          "STATUS=Updating to '%s'.", us->version);
1051

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

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

1059
                assert(inst); /* ditto */
92✔
1060

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

1066
                r = transfer_acquire_instance(t, inst, context_on_acquire_progress, c);
74✔
1067
                if (r < 0)
74✔
1068
                        return r;
1069
        }
1070

1071
        if (arg_sync)
18✔
1072
                sync();
18✔
1073

1074
        (void) sd_notifyf(/* unset_environment=*/ false,
18✔
1075
                          "STATUS=Installing '%s'.", us->version);
1076

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

1081
                if (inst->resource == &t->target)
92✔
1082
                        continue;
18✔
1083

1084
                r = transfer_install_instance(t, inst, arg_root);
74✔
1085
                if (r < 0)
74✔
1086
                        return r;
1087
        }
1088

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

1091
        (void) sd_notifyf(/* unset_environment=*/ false,
18✔
1092
                          "STATUS=Installed '%s'.", us->version);
1093

1094
        if (ret_applied)
18✔
1095
                *ret_applied = us;
18✔
1096

1097
        return 1;
1098
}
1099

1100
static int process_image(
106✔
1101
                bool ro,
1102
                char **ret_mounted_dir,
1103
                LoopDevice **ret_loop_device) {
1104

1105
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
106✔
1106
        _cleanup_(umount_and_freep) char *mounted_dir = NULL;
106✔
1107
        int r;
106✔
1108

1109
        assert(ret_mounted_dir);
106✔
1110
        assert(ret_loop_device);
106✔
1111

1112
        if (!arg_image)
106✔
1113
                return 0;
1114

1115
        assert(!arg_root);
×
1116

1117
        r = mount_image_privately_interactively(
×
1118
                        arg_image,
1119
                        arg_image_policy,
1120
                        (ro ? DISSECT_IMAGE_READ_ONLY : 0) |
1121
                        DISSECT_IMAGE_FSCK |
1122
                        DISSECT_IMAGE_MKDIR |
1123
                        DISSECT_IMAGE_GROWFS |
1124
                        DISSECT_IMAGE_RELAX_VAR_CHECK |
1125
                        DISSECT_IMAGE_USR_NO_ROOT |
1126
                        DISSECT_IMAGE_GENERIC_ROOT |
1127
                        DISSECT_IMAGE_REQUIRE_ROOT |
1128
                        DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
1129
                        &mounted_dir,
1130
                        /* ret_dir_fd= */ NULL,
1131
                        &loop_device);
1132
        if (r < 0)
×
1133
                return r;
1134

1135
        arg_root = strdup(mounted_dir);
×
1136
        if (!arg_root)
×
1137
                return log_oom();
×
1138

1139
        *ret_mounted_dir = TAKE_PTR(mounted_dir);
×
1140
        *ret_loop_device = TAKE_PTR(loop_device);
×
1141

1142
        return 0;
×
1143
}
1144

1145
static int verb_list(int argc, char **argv, void *userdata) {
28✔
1146
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
28✔
1147
        _cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL;
28✔
1148
        _cleanup_(context_freep) Context* context = NULL;
28✔
1149
        _cleanup_strv_free_ char **appstream_urls = NULL;
28✔
1150
        const char *version;
28✔
1151
        int r;
28✔
1152

1153
        assert(argc <= 2);
28✔
1154
        version = argc >= 2 ? argv[1] : NULL;
28✔
1155

1156
        r = process_image(/* ro= */ true, &mounted_dir, &loop_device);
28✔
1157
        if (r < 0)
28✔
1158
                return r;
1159

1160
        r = context_make_online(&context, loop_device ? loop_device->node : NULL);
28✔
1161
        if (r < 0)
28✔
1162
                return r;
1163

1164
        if (version)
28✔
1165
                return context_show_version(context, version);
22✔
1166
        else if (!sd_json_format_enabled(arg_json_format_flags))
6✔
1167
                return context_show_table(context);
×
1168
        else {
1169
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
6✔
1170
                _cleanup_strv_free_ char **versions = NULL;
6✔
1171
                const char *current = NULL;
6✔
1172

1173
                FOREACH_ARRAY(update_set, context->update_sets, context->n_update_sets) {
26✔
1174
                        UpdateSet *us = *update_set;
20✔
1175

1176
                        if (FLAGS_SET(us->flags, UPDATE_INSTALLED) &&
20✔
1177
                            FLAGS_SET(us->flags, UPDATE_NEWEST))
16✔
1178
                                current = us->version;
6✔
1179

1180
                        r = strv_extend(&versions, us->version);
20✔
1181
                        if (r < 0)
20✔
1182
                                return log_oom();
×
1183
                }
1184

1185
                FOREACH_ARRAY(tr, context->transfers, context->n_transfers)
28✔
1186
                        STRV_FOREACH(appstream_url, (*tr)->appstream) {
22✔
1187
                                /* Avoid duplicates */
1188
                                if (strv_contains(appstream_urls, *appstream_url))
×
1189
                                        continue;
×
1190

1191
                                r = strv_extend(&appstream_urls, *appstream_url);
×
1192
                                if (r < 0)
×
1193
                                        return log_oom();
×
1194
                        }
1195

1196
                r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_STRING("current", current),
6✔
1197
                                          SD_JSON_BUILD_PAIR_STRV("all", versions),
1198
                                          SD_JSON_BUILD_PAIR_STRV("appstreamUrls", appstream_urls));
1199
                if (r < 0)
6✔
1200
                        return log_error_errno(r, "Failed to create JSON: %m");
×
1201

1202
                r = sd_json_variant_dump(json, arg_json_format_flags, stdout, NULL);
6✔
1203
                if (r < 0)
6✔
1204
                        return log_error_errno(r, "Failed to print JSON: %m");
×
1205

1206
                return 0;
1207
        }
1208
}
1209

1210
static int verb_features(int argc, char **argv, void *userdata) {
4✔
1211
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
4✔
1212
        _cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL;
4✔
1213
        _cleanup_(context_freep) Context* context = NULL;
4✔
1214
        _cleanup_(table_unrefp) Table *table = NULL;
4✔
1215
        const char *feature_id;
4✔
1216
        Feature *f;
4✔
1217
        int r;
4✔
1218

1219
        assert(argc <= 2);
4✔
1220
        feature_id = argc >= 2 ? argv[1] : NULL;
4✔
1221

1222
        r = process_image(/* ro= */ true, &mounted_dir, &loop_device);
4✔
1223
        if (r < 0)
4✔
1224
                return r;
1225

1226
        r = context_make_offline(&context, loop_device ? loop_device->node : NULL, /* requires_enabled_transfers= */ false);
4✔
1227
        if (r < 0)
4✔
1228
                return r;
1229

1230
        if (feature_id) {
4✔
1231
                _cleanup_strv_free_ char **transfers = NULL;
2✔
1232

1233
                f = hashmap_get(context->features, feature_id);
2✔
1234
                if (!f)
2✔
1235
                        return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
×
1236
                                               "Optional feature not found: %s",
1237
                                               feature_id);
1238

1239
                table = table_new_vertical();
2✔
1240
                if (!table)
2✔
1241
                        return log_oom();
×
1242

1243
                FOREACH_ARRAY(tr, context->transfers, context->n_transfers) {
12✔
1244
                        Transfer *t = *tr;
10✔
1245

1246
                        if (!strv_contains(t->features, f->id) && !strv_contains(t->requisite_features, f->id))
10✔
1247
                                continue;
10✔
1248

1249
                        r = strv_extend(&transfers, t->id);
×
1250
                        if (r < 0)
×
1251
                                return log_oom();
×
1252
                }
1253

1254
                FOREACH_ARRAY(tr, context->disabled_transfers, context->n_disabled_transfers) {
4✔
1255
                        Transfer *t = *tr;
2✔
1256

1257
                        if (!strv_contains(t->features, f->id) && !strv_contains(t->requisite_features, f->id))
2✔
1258
                                continue;
×
1259

1260
                        r = strv_extend(&transfers, t->id);
2✔
1261
                        if (r < 0)
2✔
1262
                                return log_oom();
×
1263
                }
1264

1265
                r = table_add_many(table,
2✔
1266
                                   TABLE_FIELD, "Name",
1267
                                   TABLE_STRING, f->id,
1268
                                   TABLE_FIELD, "Enabled",
1269
                                   TABLE_BOOLEAN, f->enabled);
1270
                if (r < 0)
2✔
1271
                        return table_log_add_error(r);
×
1272

1273
                if (f->description) {
2✔
1274
                        r = table_add_many(table, TABLE_FIELD, "Description", TABLE_STRING, f->description);
2✔
1275
                        if (r < 0)
2✔
1276
                                return table_log_add_error(r);
×
1277
                }
1278

1279
                if (f->documentation) {
2✔
1280
                        r = table_add_many(table,
×
1281
                                           TABLE_FIELD, "Documentation",
1282
                                           TABLE_STRING, f->documentation,
1283
                                           TABLE_SET_URL, f->documentation);
1284
                        if (r < 0)
×
1285
                                return table_log_add_error(r);
×
1286
                }
1287

1288
                if (f->appstream) {
2✔
1289
                        r = table_add_many(table,
×
1290
                                           TABLE_FIELD, "AppStream",
1291
                                           TABLE_STRING, f->appstream,
1292
                                           TABLE_SET_URL, f->appstream);
1293
                        if (r < 0)
×
1294
                                return table_log_add_error(r);
×
1295
                }
1296

1297
                if (!strv_isempty(transfers)) {
2✔
1298
                        r = table_add_many(table, TABLE_FIELD, "Transfers", TABLE_STRV_WRAPPED, transfers);
2✔
1299
                        if (r < 0)
2✔
1300
                                return table_log_add_error(r);
×
1301
                }
1302

1303
                return table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
2✔
1304
        } else if (FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) {
2✔
1305
                table = table_new("", "feature", "description", "documentation");
2✔
1306
                if (!table)
2✔
1307
                        return log_oom();
×
1308

1309
                HASHMAP_FOREACH(f, context->features) {
4✔
1310
                        r = table_add_many(table,
2✔
1311
                                           TABLE_BOOLEAN_CHECKMARK, f->enabled,
1312
                                           TABLE_SET_COLOR, ansi_highlight_green_red(f->enabled),
1313
                                           TABLE_STRING, f->id,
1314
                                           TABLE_STRING, f->description,
1315
                                           TABLE_STRING, f->documentation,
1316
                                           TABLE_SET_URL, f->documentation);
1317
                        if (r < 0)
2✔
1318
                                return table_log_add_error(r);
×
1319
                }
1320

1321
                return table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
2✔
1322
        } else {
1323
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
×
1324
                _cleanup_strv_free_ char **features = NULL;
×
1325

1326
                HASHMAP_FOREACH(f, context->features) {
×
1327
                        r = strv_extend(&features, f->id);
×
1328
                        if (r < 0)
×
1329
                                return log_oom();
×
1330
                }
1331

1332
                r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_STRV("features", features));
×
1333
                if (r < 0)
×
1334
                        return log_error_errno(r, "Failed to create JSON: %m");
×
1335

1336
                r = sd_json_variant_dump(json, arg_json_format_flags, stdout, NULL);
×
1337
                if (r < 0)
×
1338
                        return log_error_errno(r, "Failed to print JSON: %m");
×
1339
        }
1340

1341
        return 0;
×
1342
}
1343

1344
static int verb_check_new(int argc, char **argv, void *userdata) {
44✔
1345
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
44✔
1346
        _cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL;
44✔
1347
        _cleanup_(context_freep) Context* context = NULL;
44✔
1348
        int r;
44✔
1349

1350
        assert(argc <= 1);
44✔
1351

1352
        r = process_image(/* ro= */ true, &mounted_dir, &loop_device);
44✔
1353
        if (r < 0)
44✔
1354
                return r;
1355

1356
        r = context_make_online(&context, loop_device ? loop_device->node : NULL);
44✔
1357
        if (r < 0)
44✔
1358
                return r;
1359

1360
        if (!sd_json_format_enabled(arg_json_format_flags)) {
44✔
1361
                if (!context->candidate) {
40✔
1362
                        log_debug("No candidate found.");
22✔
1363
                        return EXIT_FAILURE;
22✔
1364
                }
1365

1366
                puts(context->candidate->version);
18✔
1367
        } else {
1368
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
4✔
1369

1370
                if (context->candidate)
4✔
1371
                        r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_STRING("available", context->candidate->version));
×
1372
                else
1373
                        r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_NULL("available"));
4✔
1374
                if (r < 0)
4✔
1375
                        return log_error_errno(r, "Failed to create JSON: %m");
×
1376

1377
                r = sd_json_variant_dump(json, arg_json_format_flags, stdout, NULL);
4✔
1378
                if (r < 0)
4✔
1379
                        return log_error_errno(r, "Failed to print JSON: %m");
×
1380
        }
1381

1382
        return EXIT_SUCCESS;
1383
}
1384

1385
static int verb_vacuum(int argc, char **argv, void *userdata) {
2✔
1386
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
2✔
1387
        _cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL;
2✔
1388
        _cleanup_(context_freep) Context* context = NULL;
2✔
1389
        int r;
2✔
1390

1391
        assert(argc <= 1);
2✔
1392

1393
        if (arg_instances_max < 1)
2✔
1394
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1395
                                      "The --instances-max argument must be >= 1 while vacuuming");
1396

1397
        r = process_image(/* ro= */ false, &mounted_dir, &loop_device);
2✔
1398
        if (r < 0)
2✔
1399
                return r;
1400

1401
        r = context_make_offline(&context, loop_device ? loop_device->node : NULL, /* requires_enabled_transfers= */ false);
2✔
1402
        if (r < 0)
2✔
1403
                return r;
1404

1405
        return context_vacuum(context, 0, NULL);
2✔
1406
}
1407

1408
static int verb_update(int argc, char **argv, void *userdata) {
18✔
1409
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
18✔
1410
        _cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL;
18✔
1411
        _cleanup_(context_freep) Context* context = NULL;
×
1412
        _cleanup_free_ char *booted_version = NULL;
18✔
1413
        UpdateSet *applied = NULL;
18✔
1414
        const char *version;
18✔
1415
        int r;
18✔
1416

1417
        assert(argc <= 2);
18✔
1418
        version = argc >= 2 ? argv[1] : NULL;
18✔
1419

1420
        if (arg_instances_max < 2)
18✔
1421
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1422
                                      "The --instances-max argument must be >= 2 while updating");
1423

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

1427
                r = parse_os_release(arg_root, "IMAGE_VERSION", &booted_version);
×
1428
                if (r < 0)
×
1429
                        return log_error_errno(r, "Failed to parse /etc/os-release: %m");
×
1430
                if (!booted_version)
×
1431
                        return log_error_errno(SYNTHETIC_ERRNO(ENODATA), "/etc/os-release lacks IMAGE_VERSION field.");
×
1432
        }
1433

1434
        r = process_image(/* ro= */ false, &mounted_dir, &loop_device);
18✔
1435
        if (r < 0)
18✔
1436
                return r;
1437

1438
        r = context_make_online(&context, loop_device ? loop_device->node : NULL);
18✔
1439
        if (r < 0)
18✔
1440
                return r;
1441

1442
        r = context_apply(context, version, &applied);
18✔
1443
        if (r < 0)
18✔
1444
                return r;
1445

1446
        if (r > 0 && arg_reboot) {
18✔
1447
                assert(applied);
×
1448
                assert(booted_version);
×
1449

1450
                if (strverscmp_improved(applied->version, booted_version) > 0) {
×
1451
                        log_notice("Newly installed version is newer than booted version, rebooting.");
×
1452
                        return reboot_now();
×
1453
                }
1454

1455
                if (strverscmp_improved(applied->version, booted_version) == 0 &&
×
1456
                    FLAGS_SET(applied->flags, UPDATE_INCOMPLETE)) {
×
1457
                        log_notice("Currently booted version was incomplete and has been repaired, rebooting.");
×
1458
                        return reboot_now();
×
1459
                }
1460

1461
                log_info("Booted version is newer or identical to newly installed version, not rebooting.");
×
1462
        }
1463

1464
        return 0;
1465
}
1466

1467
static int verb_pending_or_reboot(int argc, char **argv, void *userdata) {
×
1468
        _cleanup_(context_freep) Context* context = NULL;
×
1469
        _cleanup_free_ char *booted_version = NULL;
×
1470
        int r;
×
1471

1472
        assert(argc == 1);
×
1473

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

1478
        r = context_make_offline(&context, /* node= */ NULL, /* requires_enabled_transfers= */ true);
×
1479
        if (r < 0)
×
1480
                return r;
1481

1482
        log_info("Determining installed update sets%s", glyph(GLYPH_ELLIPSIS));
×
1483

1484
        r = context_discover_update_sets_by_flag(context, UPDATE_INSTALLED);
×
1485
        if (r < 0)
×
1486
                return r;
1487
        if (!context->newest_installed)
×
1488
                return log_error_errno(SYNTHETIC_ERRNO(ENODATA), "Couldn't find any suitable installed versions.");
×
1489

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

1497
        r = strverscmp_improved(context->newest_installed->version, booted_version);
×
1498
        if (r > 0) {
×
1499
                log_notice("Newest installed version '%s' is newer than booted version '%s'.%s",
×
1500
                           context->newest_installed->version, booted_version,
1501
                           streq(argv[0], "pending") ? " Reboot recommended." : "");
1502

1503
                if (streq(argv[0], "reboot"))
×
1504
                        return reboot_now();
×
1505

1506
                return EXIT_SUCCESS;
1507
        } else if (r == 0)
×
1508
                log_info("Newest installed version '%s' matches booted version '%s'.",
×
1509
                         context->newest_installed->version, booted_version);
1510
        else
1511
                log_warning("Newest installed version '%s' is older than booted version '%s'.",
×
1512
                            context->newest_installed->version, booted_version);
1513

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

1517
        return EXIT_SUCCESS;
1518
}
1519

1520
static int component_name_valid(const char *c) {
12✔
1521
        _cleanup_free_ char *j = NULL;
12✔
1522

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

1525
        if (isempty(c))
24✔
1526
                return false;
1527

1528
        if (string_has_cc(c, NULL))
12✔
1529
                return false;
1530

1531
        if (!utf8_is_valid(c))
12✔
1532
                return false;
1533

1534
        j = strjoin("sysupdate.", c, ".d");
12✔
1535
        if (!j)
12✔
1536
                return -ENOMEM;
1537

1538
        return filename_is_valid(j);
12✔
1539
}
1540

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

1550
        assert(argc <= 1);
10✔
1551

1552
        r = process_image(/* ro= */ false, &mounted_dir, &loop_device);
10✔
1553
        if (r < 0)
10✔
1554
                return r;
1555

1556
        STRV_FOREACH(i, l) {
50✔
1557
                _cleanup_closedir_ DIR *d = NULL;
40✔
1558
                _cleanup_free_ char *p = NULL;
40✔
1559

1560
                r = chase_and_opendir(*i, arg_root, CHASE_PREFIX_ROOT, &p, &d);
40✔
1561
                if (r == -ENOENT)
40✔
1562
                        continue;
×
1563
                if (r < 0)
40✔
1564
                        return log_error_errno(r, "Failed to open directory '%s': %m", *i);
×
1565

1566
                for (;;) {
17,918✔
1567
                        _cleanup_free_ char *n = NULL;
17,870✔
1568
                        struct dirent *de;
17,918✔
1569
                        const char *e, *a;
17,918✔
1570

1571
                        de = readdir_ensure_type(d);
17,918✔
1572
                        if (!de) {
17,918✔
1573
                                if (errno != 0)
40✔
1574
                                        return log_error_errno(errno, "Failed to enumerate directory '%s': %m", p);
×
1575

1576
                                break;
40✔
1577
                        }
1578

1579
                        if (de->d_type != DT_DIR)
17,878✔
1580
                                continue;
16,150✔
1581

1582
                        if (dot_or_dot_dot(de->d_name))
1,728✔
1583
                                continue;
80✔
1584

1585
                        if (streq(de->d_name, "sysupdate.d")) {
1,648✔
1586
                                has_default_component = true;
10✔
1587
                                continue;
10✔
1588
                        }
1589

1590
                        e = startswith(de->d_name, "sysupdate.");
1,638✔
1591
                        if (!e)
1,638✔
1592
                                continue;
1,630✔
1593

1594
                        a = endswith(e, ".d");
8✔
1595
                        if (!a)
8✔
1596
                                continue;
×
1597

1598
                        n = strndup(e, a - e);
8✔
1599
                        if (!n)
8✔
1600
                                return log_oom();
×
1601

1602
                        r = component_name_valid(n);
8✔
1603
                        if (r < 0)
8✔
1604
                                return log_error_errno(r, "Unable to validate component name: %m");
×
1605
                        if (r == 0)
8✔
1606
                                continue;
×
1607

1608
                        r = set_ensure_consume(&names, &string_hash_ops_free, TAKE_PTR(n));
8✔
1609
                        if (r < 0 && r != -EEXIST)
8✔
1610
                                return log_error_errno(r, "Failed to add component to set: %m");
×
1611
                }
1612
        }
1613

1614
        z = set_get_strv(names);
10✔
1615
        if (!z)
10✔
1616
                return log_oom();
×
1617

1618
        strv_sort(z);
10✔
1619

1620
        if (!sd_json_format_enabled(arg_json_format_flags)) {
10✔
1621
                if (!has_default_component && set_isempty(names)) {
×
1622
                        log_info("No components defined.");
×
1623
                        return 0;
×
1624
                }
1625

1626
                if (has_default_component)
×
1627
                        printf("%s<default>%s\n",
×
1628
                               ansi_highlight(), ansi_normal());
1629

1630
                STRV_FOREACH(i, z)
×
1631
                        puts(*i);
×
1632
        } else {
1633
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
10✔
1634

1635
                r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_BOOLEAN("default", has_default_component),
10✔
1636
                                          SD_JSON_BUILD_PAIR_STRV("components", z));
1637
                if (r < 0)
10✔
1638
                        return log_error_errno(r, "Failed to create JSON: %m");
×
1639

1640
                r = sd_json_variant_dump(json, arg_json_format_flags, stdout, NULL);
10✔
1641
                if (r < 0)
10✔
1642
                        return log_error_errno(r, "Failed to print JSON: %m");
×
1643
        }
1644

1645
        return 0;
1646
}
1647

1648
static int verb_help(int argc, char **argv, void *userdata) {
×
1649
        _cleanup_free_ char *link = NULL;
×
1650
        int r;
×
1651

1652
        r = terminal_urlify_man("systemd-sysupdate", "8", &link);
×
1653
        if (r < 0)
×
1654
                return log_oom();
×
1655

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

1696
        return 0;
1697
}
1698

1699
static int parse_argv(int argc, char *argv[]) {
106✔
1700

1701
        enum {
106✔
1702
                ARG_VERSION = 0x100,
1703
                ARG_NO_PAGER,
1704
                ARG_NO_LEGEND,
1705
                ARG_SYNC,
1706
                ARG_DEFINITIONS,
1707
                ARG_JSON,
1708
                ARG_ROOT,
1709
                ARG_IMAGE,
1710
                ARG_IMAGE_POLICY,
1711
                ARG_REBOOT,
1712
                ARG_VERIFY,
1713
                ARG_OFFLINE,
1714
                ARG_TRANSFER_SOURCE,
1715
        };
1716

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

1737
        int c, r;
106✔
1738

1739
        assert(argc >= 0);
106✔
1740
        assert(argv);
106✔
1741

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

1744
                switch (c) {
128✔
1745

1746
                case 'h':
×
1747
                        return verb_help(0, NULL, NULL);
×
1748

1749
                case ARG_VERSION:
×
1750
                        return version();
×
1751

1752
                case ARG_NO_PAGER:
×
1753
                        arg_pager_flags |= PAGER_DISABLE;
×
1754
                        break;
×
1755

1756
                case ARG_NO_LEGEND:
×
1757
                        arg_legend = false;
×
1758
                        break;
×
1759

1760
                case 'm':
×
1761
                        r = safe_atou64(optarg, &arg_instances_max);
×
1762
                        if (r < 0)
×
1763
                                return log_error_errno(r, "Failed to parse --instances-max= parameter: %s", optarg);
×
1764

1765
                        break;
1766

1767
                case ARG_SYNC:
×
1768
                        r = parse_boolean_argument("--sync=", optarg, &arg_sync);
×
1769
                        if (r < 0)
×
1770
                                return r;
1771
                        break;
1772

1773
                case ARG_DEFINITIONS:
×
1774
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_definitions);
×
1775
                        if (r < 0)
×
1776
                                return r;
1777
                        break;
1778

1779
                case ARG_JSON:
34✔
1780
                        r = parse_json_argument(optarg, &arg_json_format_flags);
34✔
1781
                        if (r <= 0)
34✔
1782
                                return r;
1783

1784
                        break;
1785

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

1792
                case ARG_IMAGE:
×
1793
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_image);
×
1794
                        if (r < 0)
×
1795
                                return r;
1796
                        break;
1797

1798
                case ARG_IMAGE_POLICY:
×
1799
                        r = parse_image_policy_argument(optarg, &arg_image_policy);
×
1800
                        if (r < 0)
×
1801
                                return r;
1802
                        break;
1803

1804
                case ARG_REBOOT:
×
1805
                        arg_reboot = true;
×
1806
                        break;
×
1807

1808
                case 'C':
4✔
1809
                        if (isempty(optarg)) {
4✔
1810
                                arg_component = mfree(arg_component);
×
1811
                                break;
×
1812
                        }
1813

1814
                        r = component_name_valid(optarg);
4✔
1815
                        if (r < 0)
4✔
1816
                                return log_error_errno(r, "Failed to determine if component name is valid: %m");
×
1817
                        if (r == 0)
4✔
1818
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Component name invalid: %s", optarg);
×
1819

1820
                        r = free_and_strdup_warn(&arg_component, optarg);
4✔
1821
                        if (r < 0)
4✔
1822
                                return r;
1823

1824
                        break;
1825

1826
                case ARG_VERIFY: {
76✔
1827
                        bool b;
76✔
1828

1829
                        r = parse_boolean_argument("--verify=", optarg, &b);
76✔
1830
                        if (r < 0)
76✔
1831
                                return r;
×
1832

1833
                        arg_verify = b;
76✔
1834
                        break;
76✔
1835
                }
1836

1837
                case ARG_OFFLINE:
14✔
1838
                        arg_offline = true;
14✔
1839
                        break;
14✔
1840

1841
                case ARG_TRANSFER_SOURCE:
×
1842
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_transfer_source);
×
1843
                        if (r < 0)
×
1844
                                return r;
1845

1846
                        break;
1847

1848
                case '?':
1849
                        return -EINVAL;
1850

1851
                default:
×
1852
                        assert_not_reached();
×
1853
                }
1854
        }
1855

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

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

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

1865
        return 1;
1866
}
1867

1868
static int sysupdate_main(int argc, char *argv[]) {
106✔
1869

1870
        static const Verb verbs[] = {
106✔
1871
                { "list",       VERB_ANY, 2, VERB_DEFAULT, verb_list              },
1872
                { "components", VERB_ANY, 1, 0,            verb_components        },
1873
                { "features",   VERB_ANY, 2, 0,            verb_features          },
1874
                { "check-new",  VERB_ANY, 1, 0,            verb_check_new         },
1875
                { "update",     VERB_ANY, 2, 0,            verb_update            },
1876
                { "vacuum",     VERB_ANY, 1, 0,            verb_vacuum            },
1877
                { "reboot",     1,        1, 0,            verb_pending_or_reboot },
1878
                { "pending",    1,        1, 0,            verb_pending_or_reboot },
1879
                { "help",       VERB_ANY, 1, 0,            verb_help              },
1880
                {}
1881
        };
1882

1883
        return dispatch_verb(argc, argv, verbs, NULL);
106✔
1884
}
1885

1886
static int run(int argc, char *argv[]) {
106✔
1887
        int r;
106✔
1888

1889
        log_setup();
106✔
1890

1891
        r = parse_argv(argc, argv);
106✔
1892
        if (r <= 0)
106✔
1893
                return r;
1894

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

1898
        return sysupdate_main(argc, argv);
106✔
1899
}
1900

1901
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);
106✔
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