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

systemd / systemd / 14872145375

06 May 2025 09:07PM UTC coverage: 72.232% (+0.02%) from 72.214%
14872145375

push

github

DaanDeMeyer
string-table: annotate _to_string and _from_string with _const_ and _pure_, respectively

Follow-up for c94f6ab1b

297286 of 411572 relevant lines covered (72.23%)

695615.99 hits per line

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

70.7
/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 "json-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 "terminal-util.h"
41
#include "utf8.h"
42
#include "verbs.h"
43

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

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

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

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

76
        Transfer **disabled_transfers;
77
        size_t n_disabled_transfers;
78

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

81
        UpdateSet **update_sets;
82
        size_t n_update_sets;
83

84
        UpdateSet *newest_installed, *candidate;
85

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

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

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

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

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

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

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

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

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

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

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

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

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

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(&files, suffix, arg_root, CONF_FILES_REGULAR|CONF_FILES_FILTER_MASKED, dirs);
96✔
144
        if (r < 0)
96✔
145
                return log_error_errno(r, "Failed to enumerate sysupdate.d/*%s definitions: %m", suffix);
×
146

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

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

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

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

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

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

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

183
        assert(c);
96✔
184

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

258
        return 0;
259
}
260

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

264
        assert(c);
96✔
265

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

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

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

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

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

290
        return 0;
291
}
292

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

296
        assert(c);
76✔
297

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

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

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

311
        return 0;
312
}
313

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

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

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

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

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

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

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

345
                                assert(i);
9,290✔
346

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

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

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

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

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

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

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

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

381
                        assert(t);
3,044✔
382

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

480
                newest_found = true;
362✔
481

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

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

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

496
        return 0;
497
}
498

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

502
        assert(c);
90✔
503

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

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

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

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

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

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

526
        assert(c);
×
527

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

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

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

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

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

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

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

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

568
        return NULL;
569
}
570

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

755
                        have_sha256 = true;
×
756

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

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

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

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

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

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

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

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

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

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

839
                return 0;
840
        }
841
}
842

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

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

851
        assert(c);
20✔
852

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

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

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

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

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

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

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

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

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

903
        return 0;
904
}
905

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

910
        assert(ret);
96✔
911

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

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

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

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

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

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

935
        assert(ret);
90✔
936

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

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

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

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

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

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

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

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

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

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

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

990
        assert(c);
18✔
991

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

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

1003
                        return 0;
×
1004
                }
1005

1006
                us = c->candidate;
1007
        }
1008

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

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

1017
                return 0;
×
1018
        }
1019

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1092
        if (ret_applied)
18✔
1093
                *ret_applied = us;
18✔
1094

1095
        return 1;
1096
}
1097

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

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

1107
        assert(ret_mounted_dir);
106✔
1108
        assert(ret_loop_device);
106✔
1109

1110
        if (!arg_image)
106✔
1111
                return 0;
1112

1113
        assert(!arg_root);
×
1114

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

1133
        arg_root = strdup(mounted_dir);
×
1134
        if (!arg_root)
×
1135
                return log_oom();
×
1136

1137
        *ret_mounted_dir = TAKE_PTR(mounted_dir);
×
1138
        *ret_loop_device = TAKE_PTR(loop_device);
×
1139

1140
        return 0;
×
1141
}
1142

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

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

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

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

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

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

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

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

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

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

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

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

1204
                return 0;
1205
        }
1206
}
1207

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

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

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

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

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

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

1237
                table = table_new_vertical();
2✔
1238
                if (!table)
2✔
1239
                        return log_oom();
×
1240

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1339
        return 0;
×
1340
}
1341

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

1348
        assert(argc <= 1);
44✔
1349

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

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

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

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

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

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

1380
        return EXIT_SUCCESS;
1381
}
1382

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

1389
        assert(argc <= 1);
2✔
1390

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

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

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

1403
        return context_vacuum(context, 0, NULL);
2✔
1404
}
1405

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

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

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

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

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

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

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

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

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

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

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

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

1462
        return 0;
1463
}
1464

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

1470
        assert(argc == 1);
×
1471

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

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

1480
        log_info("Determining installed update sets%s", glyph(GLYPH_ELLIPSIS));
×
1481

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

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

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

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

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

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

1515
        return EXIT_SUCCESS;
1516
}
1517

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

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

1523
        if (isempty(c))
24✔
1524
                return false;
1525

1526
        if (string_has_cc(c, NULL))
12✔
1527
                return false;
1528

1529
        if (!utf8_is_valid(c))
12✔
1530
                return false;
1531

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

1536
        return filename_is_valid(j);
12✔
1537
}
1538

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

1548
        assert(argc <= 1);
10✔
1549

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

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

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

1564
                for (;;) {
18,038✔
1565
                        _cleanup_free_ char *n = NULL;
17,990✔
1566
                        struct dirent *de;
18,038✔
1567
                        const char *e, *a;
18,038✔
1568

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

1574
                                break;
40✔
1575
                        }
1576

1577
                        if (de->d_type != DT_DIR)
17,998✔
1578
                                continue;
16,260✔
1579

1580
                        if (dot_or_dot_dot(de->d_name))
1,738✔
1581
                                continue;
80✔
1582

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

1588
                        e = startswith(de->d_name, "sysupdate.");
1,648✔
1589
                        if (!e)
1,648✔
1590
                                continue;
1,640✔
1591

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

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

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

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

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

1616
        strv_sort(z);
10✔
1617

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

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

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

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

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

1643
        return 0;
1644
}
1645

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

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

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

1694
        return 0;
1695
}
1696

1697
static int parse_argv(int argc, char *argv[]) {
106✔
1698

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

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

1735
        int c, r;
106✔
1736

1737
        assert(argc >= 0);
106✔
1738
        assert(argv);
106✔
1739

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

1742
                switch (c) {
128✔
1743

1744
                case 'h':
×
1745
                        return verb_help(0, NULL, NULL);
×
1746

1747
                case ARG_VERSION:
×
1748
                        return version();
×
1749

1750
                case ARG_NO_PAGER:
×
1751
                        arg_pager_flags |= PAGER_DISABLE;
×
1752
                        break;
×
1753

1754
                case ARG_NO_LEGEND:
×
1755
                        arg_legend = false;
×
1756
                        break;
×
1757

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

1763
                        break;
1764

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

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

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

1782
                        break;
1783

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

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

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

1802
                case ARG_REBOOT:
×
1803
                        arg_reboot = true;
×
1804
                        break;
×
1805

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

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

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

1822
                        break;
1823

1824
                case ARG_VERIFY: {
76✔
1825
                        bool b;
76✔
1826

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

1831
                        arg_verify = b;
76✔
1832
                        break;
76✔
1833
                }
1834

1835
                case ARG_OFFLINE:
14✔
1836
                        arg_offline = true;
14✔
1837
                        break;
14✔
1838

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

1844
                        break;
1845

1846
                case '?':
1847
                        return -EINVAL;
1848

1849
                default:
×
1850
                        assert_not_reached();
×
1851
                }
1852
        }
1853

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

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

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

1863
        return 1;
1864
}
1865

1866
static int sysupdate_main(int argc, char *argv[]) {
106✔
1867

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

1881
        return dispatch_verb(argc, argv, verbs, NULL);
106✔
1882
}
1883

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

1887
        log_setup();
106✔
1888

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

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

1896
        return sysupdate_main(argc, argv);
106✔
1897
}
1898

1899
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