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

systemd / systemd / 17813902210

17 Sep 2025 11:56PM UTC coverage: 72.24% (-0.04%) from 72.281%
17813902210

push

github

web-flow
sysupdate: use conf_files_list_strv_full() where possible (#38198)

37 of 44 new or added lines in 1 file covered. (84.09%)

6236 existing lines in 79 files now uncovered.

302695 of 419015 relevant lines covered (72.24%)

1046897.1 hits per line

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

70.67
/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
        ConfFile **files = NULL;
96✔
131
        Transfer **transfers = NULL, **disabled = NULL;
96✔
132
        size_t n_files = 0, n_transfers = 0, n_disabled = 0;
96✔
133
        int r;
96✔
134

135
        CLEANUP_ARRAY(files, n_files, conf_file_free_many);
96✔
136
        CLEANUP_ARRAY(transfers, n_transfers, free_transfers);
96✔
137
        CLEANUP_ARRAY(disabled, n_disabled, free_transfers);
96✔
138

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

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

147
        FOREACH_ARRAY(i, files, n_files) {
652✔
148
                _cleanup_(transfer_freep) Transfer *t = NULL;
×
149
                Transfer **appended;
556✔
150
                ConfFile *e = *i;
556✔
151

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

156
                r = transfer_read_definition(t, e->result, dirs, c->features);
556✔
157
                if (r < 0)
556✔
158
                        return r;
159

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

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

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

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

184
        assert(c);
96✔
185

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

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

196
                STRV_FOREACH(dir, l) {
20✔
197
                        char *j;
16✔
198

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

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

210
        ConfFile **files = NULL;
96✔
211
        size_t n_files = 0;
96✔
212

213
        CLEANUP_ARRAY(files, n_files, conf_file_free_many);
96✔
214

215
        r = conf_files_list_strv_full(".feature", arg_root,
96✔
216
                                      CONF_FILES_REGULAR|CONF_FILES_FILTER_MASKED,
217
                                      (const char**) dirs, &files, &n_files);
218
        if (r < 0)
96✔
219
                return log_error_errno(r, "Failed to enumerate sysupdate.d/*.feature definitions: %m");
×
220

221
        FOREACH_ARRAY(i, files, n_files) {
188✔
222
                _cleanup_(feature_unrefp) Feature *f = NULL;
×
223
                ConfFile *e = *i;
92✔
224

225
                f = feature_new();
92✔
226
                if (!f)
92✔
227
                        return log_oom();
×
228

229
                r = feature_read_definition(f, e->result, (const char**) dirs);
92✔
230
                if (r < 0)
92✔
231
                        return r;
232

233
                r = hashmap_ensure_put(&c->features, &feature_hash_ops, f->id, f);
92✔
234
                if (r < 0)
92✔
235
                        return log_error_errno(r, "Failed to insert feature '%s' into map: %m", f->id);
×
236
                TAKE_PTR(f);
92✔
237
        }
238

239
        r = read_definitions(c, (const char**) dirs, ".transfer", node);
96✔
240
        if (r < 0)
96✔
241
                return r;
242

243
        if (c->n_transfers + c->n_disabled_transfers == 0) {
96✔
244
                /* Backwards-compat: If no .transfer defs are found, fall back to trying .conf! */
245
                r = read_definitions(c, (const char**) dirs, ".conf", node);
×
246
                if (r < 0)
×
247
                        return r;
248

249
                if (c->n_transfers + c->n_disabled_transfers > 0)
×
250
                        log_warning("As of v257, transfer definitions should have the '.transfer' extension.");
×
251
        }
252

253
        if (c->n_transfers + (requires_enabled_transfers ? 0 : c->n_disabled_transfers) == 0) {
96✔
254
                if (arg_component)
×
255
                        return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
×
256
                                               "No transfer definitions for component '%s' found.",
257
                                               arg_component);
258

259
                return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
×
260
                                       "No transfer definitions found.");
261
        }
262

263
        return 0;
264
}
265

266
static int context_load_installed_instances(Context *c) {
96✔
267
        int r;
96✔
268

269
        assert(c);
96✔
270

271
        log_info("Discovering installed instances%s", glyph(GLYPH_ELLIPSIS));
192✔
272

273
        FOREACH_ARRAY(tr, c->transfers, c->n_transfers) {
570✔
274
                Transfer *t = *tr;
474✔
275

276
                r = resource_load_instances(
474✔
277
                                &t->target,
278
                                arg_verify >= 0 ? arg_verify : t->verify,
474✔
279
                                &c->web_cache);
378✔
280
                if (r < 0)
474✔
281
                        return r;
282
        }
283

284
        FOREACH_ARRAY(tr, c->disabled_transfers, c->n_disabled_transfers) {
178✔
285
                Transfer *t = *tr;
82✔
286

287
                r = resource_load_instances(
82✔
288
                                &t->target,
289
                                arg_verify >= 0 ? arg_verify : t->verify,
82✔
290
                                &c->web_cache);
68✔
291
                if (r < 0)
82✔
292
                        return r;
293
        }
294

295
        return 0;
296
}
297

298
static int context_load_available_instances(Context *c) {
76✔
299
        int r;
76✔
300

301
        assert(c);
76✔
302

303
        log_info("Discovering available instances%s", glyph(GLYPH_ELLIPSIS));
152✔
304

305
        FOREACH_ARRAY(tr, c->transfers, c->n_transfers) {
454✔
306
                Transfer *t = *tr;
378✔
307

308
                r = resource_load_instances(
378✔
309
                                &t->source,
310
                                arg_verify >= 0 ? arg_verify : t->verify,
378✔
311
                                &c->web_cache);
378✔
312
                if (r < 0)
378✔
313
                        return r;
314
        }
315

316
        return 0;
317
}
318

319
static int context_discover_update_sets_by_flag(Context *c, UpdateSetFlags flags) {
166✔
320
        _cleanup_free_ char *boundary = NULL;
166✔
321
        bool newest_found = false;
166✔
322
        int r;
166✔
323

324
        assert(c);
166✔
325
        assert(IN_SET(flags, UPDATE_AVAILABLE, UPDATE_INSTALLED));
166✔
326

327
        for (;;) {
780✔
328
                _cleanup_free_ Instance **cursor_instances = NULL;
×
329
                bool skip = false;
780✔
330
                UpdateSetFlags extra_flags = 0;
780✔
331
                _cleanup_free_ char *cursor = NULL;
614✔
332
                UpdateSet *us = NULL;
780✔
333

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

338
                        assert(*tr);
3,570✔
339

340
                        if (flags == UPDATE_AVAILABLE)
3,570✔
341
                                rr = &(*tr)->source;
1,948✔
342
                        else {
343
                                assert(flags == UPDATE_INSTALLED);
1,622✔
344
                                rr = &(*tr)->target;
1,622✔
345
                        }
346

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

350
                                assert(i);
9,290✔
351

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

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

360
                                if (free_and_strdup(&cursor, i->metadata.version) < 0)
614✔
361
                                        return log_oom();
×
362

363
                                break; /* All subsequent instances will be older than this one */
364
                        }
365

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

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

377
                cursor_instances = new0(Instance*, c->n_transfers);
614✔
378
                if (!cursor_instances)
614✔
379
                        return log_oom();
×
380

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

386
                        assert(t);
3,044✔
387

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

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

411
                        cursor_instances[k] = match;
2,988✔
412

413
                        if (t->min_version && strverscmp_improved(t->min_version, cursor) > 0)
2,988✔
414
                                extra_flags |= UPDATE_OBSOLETE;
×
415

416
                        if (strv_contains(t->protected_versions, cursor))
2,988✔
417
                                extra_flags |= UPDATE_PROTECTED;
×
418
                }
419

420
                r = free_and_strdup_warn(&boundary, cursor);
614✔
421
                if (r < 0)
614✔
422
                        return r;
423

424
                if (skip)
614✔
425
                        continue;
56✔
426

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

431
                        if (strverscmp_improved(u->version, cursor) != 0)
1,032✔
432
                                continue;
836✔
433

434
                        /* Merge in what we've learned and continue onto the next version */
435

436
                        if (FLAGS_SET(u->flags, UPDATE_INCOMPLETE)) {
196✔
437
                                assert(u->n_instances == c->n_transfers);
72✔
438

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

442
                                for (size_t j = 0; j < u->n_instances; j++) {
448✔
443
                                        if (!u->instances[j])
376✔
444
                                                u->instances[j] = cursor_instances[j];
252✔
445

446
                                        /* Make sure that the list is full if the update is AVAILABLE */
447
                                        assert(flags != UPDATE_AVAILABLE || u->instances[j]);
376✔
448
                                }
449
                        }
450

451
                        u->flags |= flags | extra_flags;
196✔
452

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

460
                        skip = true;
196✔
461
                        newest_found = true;
196✔
462
                        break;
196✔
463
                }
464

465
                if (skip)
558✔
466
                        continue;
196✔
467

468
                /* Doesn't exist yet, let's add it */
469
                if (!GREEDY_REALLOC(c->update_sets, c->n_update_sets + 1))
362✔
470
                        return log_oom();
×
471

472
                us = new(UpdateSet, 1);
362✔
473
                if (!us)
362✔
474
                        return log_oom();
×
475

476
                *us = (UpdateSet) {
362✔
477
                        .flags = flags | (newest_found ? 0 : UPDATE_NEWEST) | extra_flags,
362✔
478
                        .version = TAKE_PTR(cursor),
362✔
479
                        .instances = TAKE_PTR(cursor_instances),
362✔
480
                        .n_instances = c->n_transfers,
362✔
481
                };
482

483
                c->update_sets[c->n_update_sets++] = us;
362✔
484

485
                newest_found = true;
362✔
486

487
                /* Remember which one is the newest installed */
488
                if ((us->flags & (UPDATE_NEWEST|UPDATE_INSTALLED)) == (UPDATE_NEWEST|UPDATE_INSTALLED))
362✔
489
                        c->newest_installed = us;
86✔
490

491
                /* Remember which is the newest non-obsolete, available (and not installed) version, which we declare the "candidate" */
492
                if ((us->flags & (UPDATE_NEWEST|UPDATE_INSTALLED|UPDATE_AVAILABLE|UPDATE_OBSOLETE)) == (UPDATE_NEWEST|UPDATE_AVAILABLE))
362✔
493
                        c->candidate = us;
28✔
494
        }
495

496
        /* Newest installed is newer than or equal to candidate? Then suppress the candidate */
497
        if (c->newest_installed && !FLAGS_SET(c->newest_installed->flags, UPDATE_INCOMPLETE) &&
166✔
498
            c->candidate && strverscmp_improved(c->newest_installed->version, c->candidate->version) >= 0)
138✔
499
                c->candidate = NULL;
×
500

501
        return 0;
502
}
503

504
static int context_discover_update_sets(Context *c) {
90✔
505
        int r;
90✔
506

507
        assert(c);
90✔
508

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

511
        r = context_discover_update_sets_by_flag(c, UPDATE_INSTALLED);
90✔
512
        if (r < 0)
90✔
513
                return r;
514

515
        if (!arg_offline) {
90✔
516
                log_info("Determining available update sets%s", glyph(GLYPH_ELLIPSIS));
152✔
517

518
                r = context_discover_update_sets_by_flag(c, UPDATE_AVAILABLE);
76✔
519
                if (r < 0)
76✔
520
                        return r;
521
        }
522

523
        typesafe_qsort(c->update_sets, c->n_update_sets, update_set_cmp);
90✔
524
        return 0;
90✔
525
}
526

527
static int context_show_table(Context *c) {
×
528
        _cleanup_(table_unrefp) Table *t = NULL;
×
529
        int r;
×
530

531
        assert(c);
×
532

533
        t = table_new("", "version", "installed", "available", "assessment");
×
534
        if (!t)
×
535
                return log_oom();
×
536

537
        (void) table_set_align_percent(t, table_get_cell(t, 0, 0), 100);
×
538
        (void) table_set_align_percent(t, table_get_cell(t, 0, 2), 50);
×
539
        (void) table_set_align_percent(t, table_get_cell(t, 0, 3), 50);
×
540

541
        FOREACH_ARRAY(update_set, c->update_sets, c->n_update_sets) {
×
542
                UpdateSet *us = *update_set;
×
543
                const char *color;
×
544

545
                color = update_set_flags_to_color(us->flags);
×
546

547
                r = table_add_many(t,
×
548
                                   TABLE_STRING,    update_set_flags_to_glyph(us->flags),
549
                                   TABLE_SET_COLOR, color,
550
                                   TABLE_STRING,    us->version,
551
                                   TABLE_SET_COLOR, color,
552
                                   TABLE_STRING,    glyph_check_mark_space(FLAGS_SET(us->flags, UPDATE_INSTALLED)),
553
                                   TABLE_SET_COLOR, color,
554
                                   TABLE_STRING,    glyph_check_mark_space(FLAGS_SET(us->flags, UPDATE_AVAILABLE)),
555
                                   TABLE_SET_COLOR, color,
556
                                   TABLE_STRING,    update_set_flags_to_string(us->flags),
557
                                   TABLE_SET_COLOR, color);
558
                if (r < 0)
×
559
                        return table_log_add_error(r);
×
560
        }
561

562
        return table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, arg_legend);
×
563
}
564

565
static UpdateSet* context_update_set_by_version(Context *c, const char *version) {
22✔
566
        assert(c);
22✔
567
        assert(version);
22✔
568

569
        FOREACH_ARRAY(update_set, c->update_sets, c->n_update_sets)
42✔
570
                if (streq((*update_set)->version, version))
42✔
571
                        return *update_set;
572

573
        return NULL;
574
}
575

576
static int context_show_version(Context *c, const char *version) {
22✔
577
        bool show_fs_columns = false, show_partition_columns = false,
22✔
578
                have_fs_attributes = false, have_partition_attributes = false,
22✔
579
                have_size = false, have_tries = false, have_no_auto = false,
22✔
580
                have_read_only = false, have_growfs = false, have_sha256 = false;
22✔
581
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
22✔
582
        _cleanup_(table_unrefp) Table *t = NULL;
22✔
583
        _cleanup_strv_free_ char **changelog_urls = NULL;
22✔
584
        UpdateSet *us;
22✔
585
        int r;
22✔
586

587
        assert(c);
22✔
588
        assert(version);
22✔
589

590
        us = context_update_set_by_version(c, version);
22✔
591
        if (!us)
22✔
592
                return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "Update '%s' not found.", version);
×
593

594
        if (arg_json_format_flags & (SD_JSON_FORMAT_OFF|SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_PRETTY_AUTO))
22✔
595
                (void) pager_open(arg_pager_flags);
10✔
596

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

611
        t = table_new("type", "path", "ptuuid", "ptflags", "mtime", "mode", "size", "tries-done", "tries-left", "noauto", "ro", "growfs", "sha256");
22✔
612
        if (!t)
22✔
613
                return log_oom();
×
614

615
        (void) table_set_align_percent(t, table_get_cell(t, 0, 3), 100);
22✔
616
        (void) table_set_align_percent(t, table_get_cell(t, 0, 4), 100);
22✔
617
        (void) table_set_align_percent(t, table_get_cell(t, 0, 5), 100);
22✔
618
        (void) table_set_align_percent(t, table_get_cell(t, 0, 6), 100);
22✔
619
        (void) table_set_align_percent(t, table_get_cell(t, 0, 7), 100);
22✔
620
        (void) table_set_align_percent(t, table_get_cell(t, 0, 8), 100);
22✔
621
        table_set_ersatz_string(t, TABLE_ERSATZ_DASH);
22✔
622

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

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

632
                if (tr->target.type == RESOURCE_PARTITION)
114✔
633
                        show_partition_columns = true;
44✔
634
                if (RESOURCE_IS_FILESYSTEM(tr->target.type))
114✔
635
                        show_fs_columns = true;
636

637
                STRV_FOREACH(changelog, tr->changelog) {
114✔
638
                        assert(*changelog);
×
639

640
                        _cleanup_free_ char *changelog_url = strreplace(*changelog, "@v", version);
×
641
                        if (!changelog_url)
×
642
                                return log_oom();
×
643

644
                        /* Avoid duplicates */
645
                        if (strv_contains(changelog_urls, changelog_url))
×
646
                                continue;
×
647

648
                        /* changelog_urls takes ownership of expanded changelog_url */
649
                        r = strv_consume(&changelog_urls, TAKE_PTR(changelog_url));
×
650
                        if (r < 0)
×
651
                                return log_oom();
×
652
                }
653
        }
654

655
        FOREACH_ARRAY(inst, us->instances, us->n_instances) {
136✔
656
                Instance *i = *inst;
114✔
657

658
                if (!i) {
114✔
659
                        assert(FLAGS_SET(us->flags, UPDATE_INCOMPLETE));
4✔
660
                        continue;
4✔
661
                }
662

663
                r = table_add_many(t,
110✔
664
                                   TABLE_STRING, resource_type_to_string(i->resource->type),
665
                                   TABLE_PATH, i->path);
666
                if (r < 0)
110✔
667
                        return table_log_add_error(r);
×
668

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

677
                if (i->metadata.partition_flags_set) {
110✔
678
                        have_partition_attributes = true;
32✔
679
                        r = table_add_cell(t, NULL, TABLE_UINT64_HEX, &i->metadata.partition_flags);
32✔
680
                } else
681
                        r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
78✔
682
                if (r < 0)
110✔
683
                        return table_log_add_error(r);
×
684

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

693
                if (i->metadata.mode != MODE_INVALID) {
110✔
694
                        have_fs_attributes = true;
78✔
695
                        r = table_add_cell(t, NULL, TABLE_MODE, &i->metadata.mode);
78✔
696
                } else
697
                        r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
32✔
698
                if (r < 0)
110✔
699
                        return table_log_add_error(r);
×
700

701
                if (i->metadata.size != UINT64_MAX) {
110✔
702
                        have_size = true;
×
703
                        r = table_add_cell(t, NULL, TABLE_SIZE, &i->metadata.size);
×
704
                } else
705
                        r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
110✔
706
                if (r < 0)
110✔
707
                        return table_log_add_error(r);
×
708

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

717
                if (i->metadata.tries_left != UINT64_MAX) {
110✔
718
                        have_tries = true;
16✔
719
                        r = table_add_cell(t, NULL, TABLE_UINT64, &i->metadata.tries_left);
16✔
720
                } else
721
                        r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
94✔
722
                if (r < 0)
110✔
723
                        return table_log_add_error(r);
×
724

725
                if (i->metadata.no_auto >= 0) {
110✔
726
                        bool b;
×
727

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

738
                        have_read_only = true;
32✔
739
                        b = i->metadata.read_only;
32✔
740
                        r = table_add_cell(t, NULL, TABLE_BOOLEAN, &b);
32✔
741
                } else
742
                        r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
78✔
743
                if (r < 0)
110✔
744
                        return table_log_add_error(r);
×
745

746
                if (i->metadata.growfs >= 0) {
110✔
747
                        bool b;
×
748

749
                        have_growfs = true;
×
750
                        b = i->metadata.growfs;
×
751
                        r = table_add_cell(t, NULL, TABLE_BOOLEAN, &b);
×
752
                } else
753
                        r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
110✔
754
                if (r < 0)
110✔
755
                        return table_log_add_error(r);
×
756

757
                if (i->metadata.sha256sum_set) {
110✔
758
                        _cleanup_free_ char *formatted = NULL;
×
759

760
                        have_sha256 = true;
×
761

762
                        formatted = hexmem(i->metadata.sha256sum, sizeof(i->metadata.sha256sum));
×
763
                        if (!formatted)
×
764
                                return log_oom();
×
765

766
                        r = table_add_cell(t, NULL, TABLE_STRING, formatted);
×
767
                } else
768
                        r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
110✔
769
                if (r < 0)
110✔
770
                        return table_log_add_error(r);
×
771
        }
772

773
        /* Hide the fs/partition columns if we don't have any data to show there */
774
        if (!have_fs_attributes)
22✔
775
                show_fs_columns = false;
×
776
        if (!have_partition_attributes)
22✔
777
                show_partition_columns = false;
778

779
        if (!show_partition_columns)
16✔
780
                (void) table_hide_column_from_display(t, 2, 3);
6✔
781
        if (!show_fs_columns)
22✔
782
                (void) table_hide_column_from_display(t, 4, 5);
×
783
        if (!have_size)
22✔
784
                (void) table_hide_column_from_display(t, 6);
22✔
785
        if (!have_tries)
22✔
786
                (void) table_hide_column_from_display(t, 7, 8);
6✔
787
        if (!have_no_auto)
22✔
788
                (void) table_hide_column_from_display(t, 9);
22✔
789
        if (!have_read_only)
22✔
790
                (void) table_hide_column_from_display(t, 10);
6✔
791
        if (!have_growfs)
22✔
792
                (void) table_hide_column_from_display(t, 11);
22✔
793
        if (!have_sha256)
22✔
794
                (void) table_hide_column_from_display(t, 12);
22✔
795

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

811
                STRV_FOREACH(url, changelog_urls) {
10✔
812
                        _cleanup_free_ char *changelog_link = NULL;
×
813
                        r = terminal_urlify(*url, NULL, &changelog_link);
×
814
                        if (r < 0)
×
815
                                return log_oom();
×
816
                        printf("ChangeLog: %s\n", changelog_link);
×
817
                }
818
                printf("\n");
10✔
819

820
                return table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, arg_legend);
10✔
821
        } else {
822
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *t_json = NULL;
12✔
823

824
                r = table_to_json(t, &t_json);
12✔
825
                if (r < 0)
12✔
826
                        return log_error_errno(r, "failed to convert table to JSON: %m");
×
827

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

840
                r = sd_json_variant_dump(json, arg_json_format_flags, stdout, NULL);
12✔
841
                if (r < 0)
12✔
842
                        return log_error_errno(r, "Failed to print JSON: %m");
×
843

844
                return 0;
845
        }
846
}
847

848
static int context_vacuum(
20✔
849
                Context *c,
850
                uint64_t space,
851
                const char *extra_protected_version) {
852

853
        size_t disabled_count = 0;
20✔
854
        int r, count = 0;
20✔
855

856
        assert(c);
20✔
857

858
        if (space == 0)
20✔
859
                log_info("Making room%s", glyph(GLYPH_ELLIPSIS));
4✔
860
        else
861
                log_info("Making room for %" PRIu64 " updates%s", space, glyph(GLYPH_ELLIPSIS));
36✔
862

863
        FOREACH_ARRAY(tr, c->transfers, c->n_transfers) {
122✔
864
                Transfer *t = *tr;
102✔
865

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

870
                r = transfer_vacuum(t, space, extra_protected_version);
84✔
871
                if (r < 0)
84✔
872
                        return r;
873

874
                count = MAX(count, r);
84✔
875
        }
876

877
        FOREACH_ARRAY(tr, c->disabled_transfers, c->n_disabled_transfers) {
38✔
878
                r = transfer_vacuum(*tr, UINT64_MAX /* wipe all instances */, NULL);
18✔
879
                if (r < 0)
18✔
880
                        return r;
881
                if (r > 0)
18✔
882
                        disabled_count++;
2✔
883
        }
884

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

897
                r = sd_json_buildo(&json,
2✔
898
                                   SD_JSON_BUILD_PAIR_INTEGER("removed", count),
899
                                   SD_JSON_BUILD_PAIR_UNSIGNED("disabledTransfers", disabled_count));
900
                if (r < 0)
2✔
901
                        return log_error_errno(r, "Failed to create JSON: %m");
×
902

903
                r = sd_json_variant_dump(json, arg_json_format_flags, stdout, NULL);
2✔
904
                if (r < 0)
2✔
905
                        return log_error_errno(r, "Failed to print JSON: %m");
×
906
        }
907

908
        return 0;
909
}
910

911
static int context_make_offline(Context **ret, const char *node, bool requires_enabled_transfers) {
96✔
912
        _cleanup_(context_freep) Context* context = NULL;
96✔
913
        int r;
96✔
914

915
        assert(ret);
96✔
916

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

920
        context = context_new();
96✔
921
        if (!context)
96✔
922
                return log_oom();
×
923

924
        r = context_read_definitions(context, node, requires_enabled_transfers);
96✔
925
        if (r < 0)
96✔
926
                return r;
927

928
        r = context_load_installed_instances(context);
96✔
929
        if (r < 0)
96✔
930
                return r;
931

932
        *ret = TAKE_PTR(context);
96✔
933
        return 0;
96✔
934
}
935

936
static int context_make_online(Context **ret, const char *node) {
90✔
937
        _cleanup_(context_freep) Context* context = NULL;
90✔
938
        int r;
90✔
939

940
        assert(ret);
90✔
941

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

945
        r = context_make_offline(&context, node, /* requires_enabled_transfers= */ true);
90✔
946
        if (r < 0)
90✔
947
                return r;
948

949
        if (!arg_offline) {
90✔
950
                r = context_load_available_instances(context);
76✔
951
                if (r < 0)
76✔
952
                        return r;
953
        }
954

955
        r = context_discover_update_sets(context);
90✔
956
        if (r < 0)
90✔
957
                return r;
958

959
        *ret = TAKE_PTR(context);
90✔
960
        return 0;
90✔
961
}
962

963
static int context_on_acquire_progress(const Transfer *t, const Instance *inst, unsigned percentage) {
76✔
964
        const Context *c = ASSERT_PTR(t->context);
76✔
965
        size_t i, n = c->n_transfers;
76✔
966
        uint64_t base, scaled;
76✔
967
        unsigned overall;
76✔
968

969
        for (i = 0; i < n; i++)
222✔
970
                if (c->transfers[i] == t)
222✔
971
                        break;
972
        assert(i < n); /* We should have found the index */
76✔
973

974
        base = (100 * 100 * i) / n;
76✔
975
        scaled = (100 * percentage) / n;
76✔
976
        overall = (unsigned) ((base + scaled) / 100);
76✔
977
        assert(overall <= 100);
76✔
978

979
        log_debug("Transfer %zu/%zu is %u%% complete (%u%% overall).", i+1, n, percentage, overall);
76✔
980
        return sd_notifyf(/* unset_environment=*/ false, "X_SYSUPDATE_PROGRESS=%u\n"
152✔
981
                                              "X_SYSUPDATE_TRANSFERS_LEFT=%zu\n"
982
                                              "X_SYSUPDATE_TRANSFERS_DONE=%zu\n"
983
                                              "STATUS=Updating to '%s' (%u%% complete).",
984
                                              overall, n - i, i, inst->metadata.version, overall);
76✔
985
}
986

987
static int context_apply(
18✔
988
                Context *c,
989
                const char *version,
990
                UpdateSet **ret_applied) {
991

992
        UpdateSet *us = NULL;
18✔
993
        int r;
18✔
994

995
        assert(c);
18✔
996

997
        if (version) {
18✔
998
                us = context_update_set_by_version(c, version);
×
999
                if (!us)
×
1000
                        return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "Update '%s' not found.", version);
×
1001
        } else {
1002
                if (!c->candidate) {
18✔
1003
                        log_info("No update needed.");
×
1004

1005
                        if (ret_applied)
×
1006
                                *ret_applied = NULL;
×
1007

1008
                        return 0;
×
1009
                }
1010

1011
                us = c->candidate;
1012
        }
1013

1014
        if (FLAGS_SET(us->flags, UPDATE_INCOMPLETE))
18✔
1015
                log_info("Selected update '%s' is already installed, but incomplete. Repairing.", us->version);
4✔
1016
        else if (FLAGS_SET(us->flags, UPDATE_INSTALLED)) {
14✔
1017
                log_info("Selected update '%s' is already installed. Skipping update.", us->version);
×
1018

1019
                if (ret_applied)
×
1020
                        *ret_applied = NULL;
×
1021

1022
                return 0;
×
1023
        }
1024

1025
        if (!FLAGS_SET(us->flags, UPDATE_AVAILABLE))
18✔
1026
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Selected update '%s' is not available, refusing.", us->version);
×
1027
        if (FLAGS_SET(us->flags, UPDATE_OBSOLETE))
18✔
1028
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Selected update '%s' is obsolete, refusing.", us->version);
×
1029

1030
        if (!FLAGS_SET(us->flags, UPDATE_NEWEST))
18✔
1031
                log_notice("Selected update '%s' is not the newest, proceeding anyway.", us->version);
×
1032
        if (c->newest_installed && strverscmp_improved(c->newest_installed->version, us->version) > 0)
18✔
1033
                log_notice("Selected update '%s' is older than newest installed version, proceeding anyway.", us->version);
×
1034

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

1037
        (void) sd_notifyf(/* unset_environment=*/ false,
18✔
1038
                          "READY=1\n"
1039
                          "X_SYSUPDATE_VERSION=%s\n"
1040
                          "STATUS=Making room for '%s'.", us->version, us->version);
1041

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

1052
        if (arg_sync)
18✔
1053
                sync();
18✔
1054

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

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

1061
        for (size_t i = 0; i < c->n_transfers; i++) {
110✔
1062
                Instance *inst = us->instances[i];
92✔
1063
                Transfer *t = c->transfers[i];
92✔
1064

1065
                assert(inst); /* ditto */
92✔
1066

1067
                if (inst->resource == &t->target) { /* a present transfer in an incomplete installation */
92✔
1068
                        assert(FLAGS_SET(us->flags, UPDATE_INCOMPLETE));
18✔
1069
                        continue;
18✔
1070
                }
1071

1072
                r = transfer_acquire_instance(t, inst, context_on_acquire_progress, c);
74✔
1073
                if (r < 0)
74✔
1074
                        return r;
1075
        }
1076

1077
        if (arg_sync)
18✔
1078
                sync();
18✔
1079

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

1083
        for (size_t i = 0; i < c->n_transfers; i++) {
110✔
1084
                Instance *inst = us->instances[i];
92✔
1085
                Transfer *t = c->transfers[i];
92✔
1086

1087
                if (inst->resource == &t->target)
92✔
1088
                        continue;
18✔
1089

1090
                r = transfer_install_instance(t, inst, arg_root);
74✔
1091
                if (r < 0)
74✔
1092
                        return r;
1093
        }
1094

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

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

1100
        if (ret_applied)
18✔
1101
                *ret_applied = us;
18✔
1102

1103
        return 1;
1104
}
1105

1106
static int process_image(
106✔
1107
                bool ro,
1108
                char **ret_mounted_dir,
1109
                LoopDevice **ret_loop_device) {
1110

1111
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
106✔
1112
        _cleanup_(umount_and_freep) char *mounted_dir = NULL;
106✔
1113
        int r;
106✔
1114

1115
        assert(ret_mounted_dir);
106✔
1116
        assert(ret_loop_device);
106✔
1117

1118
        if (!arg_image)
106✔
1119
                return 0;
1120

1121
        assert(!arg_root);
×
1122

1123
        r = mount_image_privately_interactively(
×
1124
                        arg_image,
1125
                        arg_image_policy,
1126
                        (ro ? DISSECT_IMAGE_READ_ONLY : 0) |
1127
                        DISSECT_IMAGE_FSCK |
1128
                        DISSECT_IMAGE_MKDIR |
1129
                        DISSECT_IMAGE_GROWFS |
1130
                        DISSECT_IMAGE_RELAX_VAR_CHECK |
1131
                        DISSECT_IMAGE_USR_NO_ROOT |
1132
                        DISSECT_IMAGE_GENERIC_ROOT |
1133
                        DISSECT_IMAGE_REQUIRE_ROOT |
1134
                        DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
1135
                        &mounted_dir,
1136
                        /* ret_dir_fd= */ NULL,
1137
                        &loop_device);
1138
        if (r < 0)
×
1139
                return r;
1140

1141
        arg_root = strdup(mounted_dir);
×
1142
        if (!arg_root)
×
1143
                return log_oom();
×
1144

1145
        *ret_mounted_dir = TAKE_PTR(mounted_dir);
×
1146
        *ret_loop_device = TAKE_PTR(loop_device);
×
1147

1148
        return 0;
×
1149
}
1150

1151
static int verb_list(int argc, char **argv, void *userdata) {
28✔
1152
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
28✔
1153
        _cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL;
28✔
1154
        _cleanup_(context_freep) Context* context = NULL;
28✔
1155
        _cleanup_strv_free_ char **appstream_urls = NULL;
28✔
1156
        const char *version;
28✔
1157
        int r;
28✔
1158

1159
        assert(argc <= 2);
28✔
1160
        version = argc >= 2 ? argv[1] : NULL;
28✔
1161

1162
        r = process_image(/* ro= */ true, &mounted_dir, &loop_device);
28✔
1163
        if (r < 0)
28✔
1164
                return r;
1165

1166
        r = context_make_online(&context, loop_device ? loop_device->node : NULL);
28✔
1167
        if (r < 0)
28✔
1168
                return r;
1169

1170
        if (version)
28✔
1171
                return context_show_version(context, version);
22✔
1172
        else if (!sd_json_format_enabled(arg_json_format_flags))
6✔
1173
                return context_show_table(context);
×
1174
        else {
1175
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
6✔
1176
                _cleanup_strv_free_ char **versions = NULL;
6✔
1177
                const char *current = NULL;
6✔
1178

1179
                FOREACH_ARRAY(update_set, context->update_sets, context->n_update_sets) {
26✔
1180
                        UpdateSet *us = *update_set;
20✔
1181

1182
                        if (FLAGS_SET(us->flags, UPDATE_INSTALLED) &&
20✔
1183
                            FLAGS_SET(us->flags, UPDATE_NEWEST))
16✔
1184
                                current = us->version;
6✔
1185

1186
                        r = strv_extend(&versions, us->version);
20✔
1187
                        if (r < 0)
20✔
1188
                                return log_oom();
×
1189
                }
1190

1191
                FOREACH_ARRAY(tr, context->transfers, context->n_transfers)
28✔
1192
                        STRV_FOREACH(appstream_url, (*tr)->appstream) {
22✔
1193
                                /* Avoid duplicates */
1194
                                if (strv_contains(appstream_urls, *appstream_url))
×
1195
                                        continue;
×
1196

1197
                                r = strv_extend(&appstream_urls, *appstream_url);
×
1198
                                if (r < 0)
×
1199
                                        return log_oom();
×
1200
                        }
1201

1202
                r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_STRING("current", current),
6✔
1203
                                          SD_JSON_BUILD_PAIR_STRV("all", versions),
1204
                                          SD_JSON_BUILD_PAIR_STRV("appstreamUrls", appstream_urls));
1205
                if (r < 0)
6✔
1206
                        return log_error_errno(r, "Failed to create JSON: %m");
×
1207

1208
                r = sd_json_variant_dump(json, arg_json_format_flags, stdout, NULL);
6✔
1209
                if (r < 0)
6✔
1210
                        return log_error_errno(r, "Failed to print JSON: %m");
×
1211

1212
                return 0;
1213
        }
1214
}
1215

1216
static int verb_features(int argc, char **argv, void *userdata) {
4✔
1217
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
4✔
1218
        _cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL;
4✔
1219
        _cleanup_(context_freep) Context* context = NULL;
4✔
1220
        _cleanup_(table_unrefp) Table *table = NULL;
4✔
1221
        const char *feature_id;
4✔
1222
        Feature *f;
4✔
1223
        int r;
4✔
1224

1225
        assert(argc <= 2);
4✔
1226
        feature_id = argc >= 2 ? argv[1] : NULL;
4✔
1227

1228
        r = process_image(/* ro= */ true, &mounted_dir, &loop_device);
4✔
1229
        if (r < 0)
4✔
1230
                return r;
1231

1232
        r = context_make_offline(&context, loop_device ? loop_device->node : NULL, /* requires_enabled_transfers= */ false);
4✔
1233
        if (r < 0)
4✔
1234
                return r;
1235

1236
        if (feature_id) {
4✔
1237
                _cleanup_strv_free_ char **transfers = NULL;
2✔
1238

1239
                f = hashmap_get(context->features, feature_id);
2✔
1240
                if (!f)
2✔
1241
                        return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
×
1242
                                               "Optional feature not found: %s",
1243
                                               feature_id);
1244

1245
                table = table_new_vertical();
2✔
1246
                if (!table)
2✔
1247
                        return log_oom();
×
1248

1249
                FOREACH_ARRAY(tr, context->transfers, context->n_transfers) {
12✔
1250
                        Transfer *t = *tr;
10✔
1251

1252
                        if (!strv_contains(t->features, f->id) && !strv_contains(t->requisite_features, f->id))
10✔
1253
                                continue;
10✔
1254

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

1260
                FOREACH_ARRAY(tr, context->disabled_transfers, context->n_disabled_transfers) {
4✔
1261
                        Transfer *t = *tr;
2✔
1262

1263
                        if (!strv_contains(t->features, f->id) && !strv_contains(t->requisite_features, f->id))
2✔
1264
                                continue;
×
1265

1266
                        r = strv_extend(&transfers, t->id);
2✔
1267
                        if (r < 0)
2✔
1268
                                return log_oom();
×
1269
                }
1270

1271
                r = table_add_many(table,
2✔
1272
                                   TABLE_FIELD, "Name",
1273
                                   TABLE_STRING, f->id,
1274
                                   TABLE_FIELD, "Enabled",
1275
                                   TABLE_BOOLEAN, f->enabled);
1276
                if (r < 0)
2✔
1277
                        return table_log_add_error(r);
×
1278

1279
                if (f->description) {
2✔
1280
                        r = table_add_many(table, TABLE_FIELD, "Description", TABLE_STRING, f->description);
2✔
1281
                        if (r < 0)
2✔
1282
                                return table_log_add_error(r);
×
1283
                }
1284

1285
                if (f->documentation) {
2✔
1286
                        r = table_add_many(table,
×
1287
                                           TABLE_FIELD, "Documentation",
1288
                                           TABLE_STRING, f->documentation,
1289
                                           TABLE_SET_URL, f->documentation);
1290
                        if (r < 0)
×
1291
                                return table_log_add_error(r);
×
1292
                }
1293

1294
                if (f->appstream) {
2✔
1295
                        r = table_add_many(table,
×
1296
                                           TABLE_FIELD, "AppStream",
1297
                                           TABLE_STRING, f->appstream,
1298
                                           TABLE_SET_URL, f->appstream);
1299
                        if (r < 0)
×
1300
                                return table_log_add_error(r);
×
1301
                }
1302

1303
                if (!strv_isempty(transfers)) {
2✔
1304
                        r = table_add_many(table, TABLE_FIELD, "Transfers", TABLE_STRV_WRAPPED, transfers);
2✔
1305
                        if (r < 0)
2✔
1306
                                return table_log_add_error(r);
×
1307
                }
1308

1309
                return table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
2✔
1310
        } else if (FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) {
2✔
1311
                table = table_new("", "feature", "description", "documentation");
2✔
1312
                if (!table)
2✔
1313
                        return log_oom();
×
1314

1315
                HASHMAP_FOREACH(f, context->features) {
4✔
1316
                        r = table_add_many(table,
2✔
1317
                                           TABLE_BOOLEAN_CHECKMARK, f->enabled,
1318
                                           TABLE_SET_COLOR, ansi_highlight_green_red(f->enabled),
1319
                                           TABLE_STRING, f->id,
1320
                                           TABLE_STRING, f->description,
1321
                                           TABLE_STRING, f->documentation,
1322
                                           TABLE_SET_URL, f->documentation);
1323
                        if (r < 0)
2✔
1324
                                return table_log_add_error(r);
×
1325
                }
1326

1327
                return table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
2✔
1328
        } else {
1329
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
×
1330
                _cleanup_strv_free_ char **features = NULL;
×
1331

1332
                HASHMAP_FOREACH(f, context->features) {
×
1333
                        r = strv_extend(&features, f->id);
×
1334
                        if (r < 0)
×
1335
                                return log_oom();
×
1336
                }
1337

1338
                r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_STRV("features", features));
×
1339
                if (r < 0)
×
1340
                        return log_error_errno(r, "Failed to create JSON: %m");
×
1341

1342
                r = sd_json_variant_dump(json, arg_json_format_flags, stdout, NULL);
×
1343
                if (r < 0)
×
1344
                        return log_error_errno(r, "Failed to print JSON: %m");
×
1345
        }
1346

1347
        return 0;
×
1348
}
1349

1350
static int verb_check_new(int argc, char **argv, void *userdata) {
44✔
1351
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
44✔
1352
        _cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL;
44✔
1353
        _cleanup_(context_freep) Context* context = NULL;
44✔
1354
        int r;
44✔
1355

1356
        assert(argc <= 1);
44✔
1357

1358
        r = process_image(/* ro= */ true, &mounted_dir, &loop_device);
44✔
1359
        if (r < 0)
44✔
1360
                return r;
1361

1362
        r = context_make_online(&context, loop_device ? loop_device->node : NULL);
44✔
1363
        if (r < 0)
44✔
1364
                return r;
1365

1366
        if (!sd_json_format_enabled(arg_json_format_flags)) {
44✔
1367
                if (!context->candidate) {
40✔
1368
                        log_debug("No candidate found.");
22✔
1369
                        return EXIT_FAILURE;
22✔
1370
                }
1371

1372
                puts(context->candidate->version);
18✔
1373
        } else {
1374
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
4✔
1375

1376
                if (context->candidate)
4✔
1377
                        r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_STRING("available", context->candidate->version));
×
1378
                else
1379
                        r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_NULL("available"));
4✔
1380
                if (r < 0)
4✔
1381
                        return log_error_errno(r, "Failed to create JSON: %m");
×
1382

1383
                r = sd_json_variant_dump(json, arg_json_format_flags, stdout, NULL);
4✔
1384
                if (r < 0)
4✔
1385
                        return log_error_errno(r, "Failed to print JSON: %m");
×
1386
        }
1387

1388
        return EXIT_SUCCESS;
1389
}
1390

1391
static int verb_vacuum(int argc, char **argv, void *userdata) {
2✔
1392
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
2✔
1393
        _cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL;
2✔
1394
        _cleanup_(context_freep) Context* context = NULL;
2✔
1395
        int r;
2✔
1396

1397
        assert(argc <= 1);
2✔
1398

1399
        if (arg_instances_max < 1)
2✔
1400
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1401
                                      "The --instances-max argument must be >= 1 while vacuuming");
1402

1403
        r = process_image(/* ro= */ false, &mounted_dir, &loop_device);
2✔
1404
        if (r < 0)
2✔
1405
                return r;
1406

1407
        r = context_make_offline(&context, loop_device ? loop_device->node : NULL, /* requires_enabled_transfers= */ false);
2✔
1408
        if (r < 0)
2✔
1409
                return r;
1410

1411
        return context_vacuum(context, 0, NULL);
2✔
1412
}
1413

1414
static int verb_update(int argc, char **argv, void *userdata) {
18✔
1415
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
18✔
1416
        _cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL;
18✔
1417
        _cleanup_(context_freep) Context* context = NULL;
×
1418
        _cleanup_free_ char *booted_version = NULL;
18✔
1419
        UpdateSet *applied = NULL;
18✔
1420
        const char *version;
18✔
1421
        int r;
18✔
1422

1423
        assert(argc <= 2);
18✔
1424
        version = argc >= 2 ? argv[1] : NULL;
18✔
1425

1426
        if (arg_instances_max < 2)
18✔
1427
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1428
                                      "The --instances-max argument must be >= 2 while updating");
1429

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

1433
                r = parse_os_release(arg_root, "IMAGE_VERSION", &booted_version);
×
1434
                if (r < 0)
×
1435
                        return log_error_errno(r, "Failed to parse /etc/os-release: %m");
×
1436
                if (!booted_version)
×
1437
                        return log_error_errno(SYNTHETIC_ERRNO(ENODATA), "/etc/os-release lacks IMAGE_VERSION field.");
×
1438
        }
1439

1440
        r = process_image(/* ro= */ false, &mounted_dir, &loop_device);
18✔
1441
        if (r < 0)
18✔
1442
                return r;
1443

1444
        r = context_make_online(&context, loop_device ? loop_device->node : NULL);
18✔
1445
        if (r < 0)
18✔
1446
                return r;
1447

1448
        r = context_apply(context, version, &applied);
18✔
1449
        if (r < 0)
18✔
1450
                return r;
1451

1452
        if (r > 0 && arg_reboot) {
18✔
1453
                assert(applied);
×
1454
                assert(booted_version);
×
1455

1456
                if (strverscmp_improved(applied->version, booted_version) > 0) {
×
1457
                        log_notice("Newly installed version is newer than booted version, rebooting.");
×
1458
                        return reboot_now();
×
1459
                }
1460

1461
                if (strverscmp_improved(applied->version, booted_version) == 0 &&
×
1462
                    FLAGS_SET(applied->flags, UPDATE_INCOMPLETE)) {
×
1463
                        log_notice("Currently booted version was incomplete and has been repaired, rebooting.");
×
1464
                        return reboot_now();
×
1465
                }
1466

1467
                log_info("Booted version is newer or identical to newly installed version, not rebooting.");
×
1468
        }
1469

1470
        return 0;
1471
}
1472

1473
static int verb_pending_or_reboot(int argc, char **argv, void *userdata) {
×
1474
        _cleanup_(context_freep) Context* context = NULL;
×
1475
        _cleanup_free_ char *booted_version = NULL;
×
1476
        int r;
×
1477

1478
        assert(argc == 1);
×
1479

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

1484
        r = context_make_offline(&context, /* node= */ NULL, /* requires_enabled_transfers= */ true);
×
1485
        if (r < 0)
×
1486
                return r;
1487

1488
        log_info("Determining installed update sets%s", glyph(GLYPH_ELLIPSIS));
×
1489

1490
        r = context_discover_update_sets_by_flag(context, UPDATE_INSTALLED);
×
1491
        if (r < 0)
×
1492
                return r;
1493
        if (!context->newest_installed)
×
1494
                return log_error_errno(SYNTHETIC_ERRNO(ENODATA), "Couldn't find any suitable installed versions.");
×
1495

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

1503
        r = strverscmp_improved(context->newest_installed->version, booted_version);
×
1504
        if (r > 0) {
×
1505
                log_notice("Newest installed version '%s' is newer than booted version '%s'.%s",
×
1506
                           context->newest_installed->version, booted_version,
1507
                           streq(argv[0], "pending") ? " Reboot recommended." : "");
1508

1509
                if (streq(argv[0], "reboot"))
×
1510
                        return reboot_now();
×
1511

1512
                return EXIT_SUCCESS;
1513
        } else if (r == 0)
×
1514
                log_info("Newest installed version '%s' matches booted version '%s'.",
×
1515
                         context->newest_installed->version, booted_version);
1516
        else
1517
                log_warning("Newest installed version '%s' is older than booted version '%s'.",
×
1518
                            context->newest_installed->version, booted_version);
1519

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

1523
        return EXIT_SUCCESS;
1524
}
1525

1526
static int component_name_valid(const char *c) {
12✔
1527
        _cleanup_free_ char *j = NULL;
12✔
1528

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

1531
        if (isempty(c))
24✔
1532
                return false;
1533

1534
        if (string_has_cc(c, NULL))
12✔
1535
                return false;
1536

1537
        if (!utf8_is_valid(c))
12✔
1538
                return false;
1539

1540
        j = strjoin("sysupdate.", c, ".d");
12✔
1541
        if (!j)
12✔
1542
                return -ENOMEM;
1543

1544
        return filename_is_valid(j);
12✔
1545
}
1546

1547
static int verb_components(int argc, char **argv, void *userdata) {
10✔
1548
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
10✔
1549
        _cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL;
10✔
1550
        _cleanup_set_free_ Set *names = NULL;
10✔
1551
        bool has_default_component = false;
10✔
1552
        int r;
10✔
1553

1554
        assert(argc <= 1);
10✔
1555

1556
        r = process_image(/* ro= */ false, &mounted_dir, &loop_device);
10✔
1557
        if (r < 0)
10✔
1558
                return r;
1559

1560
        ConfFile **directories = NULL;
10✔
1561
        size_t n_directories = 0;
10✔
1562

1563
        CLEANUP_ARRAY(directories, n_directories, conf_file_free_many);
10✔
1564

1565
        r = conf_files_list_strv_full(".d", arg_root, CONF_FILES_DIRECTORY, (const char * const *) CONF_PATHS_STRV(""), &directories, &n_directories);
10✔
1566
        if (r < 0)
10✔
NEW
1567
                return log_error_errno(r, "Failed to enumerate directories: %m");
×
1568

1569
        FOREACH_ARRAY(i, directories, n_directories) {
268✔
1570
                ConfFile *e = *i;
258✔
1571

1572
                if (streq(e->name, "sysupdate.d")) {
258✔
1573
                        has_default_component = true;
10✔
1574
                        continue;
10✔
1575
                }
1576

1577
                const char *s = startswith(e->name, "sysupdate.");
248✔
1578
                if (!s)
248✔
1579
                        continue;
240✔
1580

1581
                const char *a = endswith(s, ".d");
8✔
1582
                if (!a)
8✔
NEW
1583
                        continue;
×
1584

NEW
1585
                _cleanup_free_ char *n = strndup(s, a - s);
×
1586
                if (!n)
8✔
NEW
1587
                        return log_oom();
×
1588

1589
                r = component_name_valid(n);
8✔
1590
                if (r < 0)
8✔
NEW
1591
                        return log_error_errno(r, "Unable to validate component name '%s': %m", n);
×
1592
                if (r == 0)
8✔
NEW
1593
                        continue;
×
1594

1595
                r = set_ensure_put(&names, &string_hash_ops_free, n);
8✔
1596
                if (r < 0 && r != -EEXIST)
8✔
NEW
1597
                        return log_error_errno(r, "Failed to add component '%s' to set: %m", n);
×
1598
                TAKE_PTR(n);
1599
        }
1600

1601
        /* We use simple free() rather than strv_free() here, since set_free() will free the strings for us */
1602
        _cleanup_free_ char **z = set_get_strv(names);
20✔
1603
        if (!z)
10✔
1604
                return log_oom();
×
1605

1606
        strv_sort(z);
10✔
1607

1608
        if (!sd_json_format_enabled(arg_json_format_flags)) {
10✔
1609
                if (!has_default_component && set_isempty(names)) {
×
1610
                        log_info("No components defined.");
×
1611
                        return 0;
×
1612
                }
1613

1614
                if (has_default_component)
×
1615
                        printf("%s<default>%s\n",
×
1616
                               ansi_highlight(), ansi_normal());
1617

1618
                STRV_FOREACH(i, z)
×
1619
                        puts(*i);
×
1620
        } else {
1621
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
10✔
1622

1623
                r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_BOOLEAN("default", has_default_component),
10✔
1624
                                          SD_JSON_BUILD_PAIR_STRV("components", z));
1625
                if (r < 0)
10✔
1626
                        return log_error_errno(r, "Failed to create JSON: %m");
×
1627

1628
                r = sd_json_variant_dump(json, arg_json_format_flags, stdout, NULL);
10✔
1629
                if (r < 0)
10✔
1630
                        return log_error_errno(r, "Failed to print JSON: %m");
×
1631
        }
1632

1633
        return 0;
1634
}
1635

1636
static int verb_help(int argc, char **argv, void *userdata) {
×
1637
        _cleanup_free_ char *link = NULL;
×
1638
        int r;
×
1639

1640
        r = terminal_urlify_man("systemd-sysupdate", "8", &link);
×
1641
        if (r < 0)
×
1642
                return log_oom();
×
1643

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

1684
        return 0;
1685
}
1686

1687
static int parse_argv(int argc, char *argv[]) {
106✔
1688

1689
        enum {
106✔
1690
                ARG_VERSION = 0x100,
1691
                ARG_NO_PAGER,
1692
                ARG_NO_LEGEND,
1693
                ARG_SYNC,
1694
                ARG_DEFINITIONS,
1695
                ARG_JSON,
1696
                ARG_ROOT,
1697
                ARG_IMAGE,
1698
                ARG_IMAGE_POLICY,
1699
                ARG_REBOOT,
1700
                ARG_VERIFY,
1701
                ARG_OFFLINE,
1702
                ARG_TRANSFER_SOURCE,
1703
        };
1704

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

1725
        int c, r;
106✔
1726

1727
        assert(argc >= 0);
106✔
1728
        assert(argv);
106✔
1729

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

1732
                switch (c) {
128✔
1733

1734
                case 'h':
×
1735
                        return verb_help(0, NULL, NULL);
×
1736

1737
                case ARG_VERSION:
×
1738
                        return version();
×
1739

1740
                case ARG_NO_PAGER:
×
1741
                        arg_pager_flags |= PAGER_DISABLE;
×
1742
                        break;
×
1743

1744
                case ARG_NO_LEGEND:
×
1745
                        arg_legend = false;
×
1746
                        break;
×
1747

1748
                case 'm':
×
1749
                        r = safe_atou64(optarg, &arg_instances_max);
×
1750
                        if (r < 0)
×
1751
                                return log_error_errno(r, "Failed to parse --instances-max= parameter: %s", optarg);
×
1752

1753
                        break;
1754

1755
                case ARG_SYNC:
×
1756
                        r = parse_boolean_argument("--sync=", optarg, &arg_sync);
×
1757
                        if (r < 0)
×
1758
                                return r;
1759
                        break;
1760

1761
                case ARG_DEFINITIONS:
×
1762
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_definitions);
×
1763
                        if (r < 0)
×
1764
                                return r;
1765
                        break;
1766

1767
                case ARG_JSON:
34✔
1768
                        r = parse_json_argument(optarg, &arg_json_format_flags);
34✔
1769
                        if (r <= 0)
34✔
1770
                                return r;
1771

1772
                        break;
1773

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

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

1786
                case ARG_IMAGE_POLICY:
×
1787
                        r = parse_image_policy_argument(optarg, &arg_image_policy);
×
1788
                        if (r < 0)
×
1789
                                return r;
1790
                        break;
1791

1792
                case ARG_REBOOT:
×
1793
                        arg_reboot = true;
×
1794
                        break;
×
1795

1796
                case 'C':
4✔
1797
                        if (isempty(optarg)) {
4✔
1798
                                arg_component = mfree(arg_component);
×
1799
                                break;
×
1800
                        }
1801

1802
                        r = component_name_valid(optarg);
4✔
1803
                        if (r < 0)
4✔
1804
                                return log_error_errno(r, "Failed to determine if component name is valid: %m");
×
1805
                        if (r == 0)
4✔
1806
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Component name invalid: %s", optarg);
×
1807

1808
                        r = free_and_strdup_warn(&arg_component, optarg);
4✔
1809
                        if (r < 0)
4✔
1810
                                return r;
1811

1812
                        break;
1813

1814
                case ARG_VERIFY: {
76✔
1815
                        bool b;
76✔
1816

1817
                        r = parse_boolean_argument("--verify=", optarg, &b);
76✔
1818
                        if (r < 0)
76✔
1819
                                return r;
×
1820

1821
                        arg_verify = b;
76✔
1822
                        break;
76✔
1823
                }
1824

1825
                case ARG_OFFLINE:
14✔
1826
                        arg_offline = true;
14✔
1827
                        break;
14✔
1828

1829
                case ARG_TRANSFER_SOURCE:
×
1830
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_transfer_source);
×
1831
                        if (r < 0)
×
1832
                                return r;
1833

1834
                        break;
1835

1836
                case '?':
1837
                        return -EINVAL;
1838

1839
                default:
×
1840
                        assert_not_reached();
×
1841
                }
1842
        }
1843

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

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

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

1853
        return 1;
1854
}
1855

1856
static int sysupdate_main(int argc, char *argv[]) {
106✔
1857

1858
        static const Verb verbs[] = {
106✔
1859
                { "list",       VERB_ANY, 2, VERB_DEFAULT, verb_list              },
1860
                { "components", VERB_ANY, 1, 0,            verb_components        },
1861
                { "features",   VERB_ANY, 2, 0,            verb_features          },
1862
                { "check-new",  VERB_ANY, 1, 0,            verb_check_new         },
1863
                { "update",     VERB_ANY, 2, 0,            verb_update            },
1864
                { "vacuum",     VERB_ANY, 1, 0,            verb_vacuum            },
1865
                { "reboot",     1,        1, 0,            verb_pending_or_reboot },
1866
                { "pending",    1,        1, 0,            verb_pending_or_reboot },
1867
                { "help",       VERB_ANY, 1, 0,            verb_help              },
1868
                {}
1869
        };
1870

1871
        return dispatch_verb(argc, argv, verbs, NULL);
106✔
1872
}
1873

1874
static int run(int argc, char *argv[]) {
106✔
1875
        int r;
106✔
1876

1877
        log_setup();
106✔
1878

1879
        r = parse_argv(argc, argv);
106✔
1880
        if (r <= 0)
106✔
1881
                return r;
1882

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

1886
        return sysupdate_main(argc, argv);
106✔
1887
}
1888

1889
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