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

systemd / systemd / 22742800078

06 Mar 2026 12:17AM UTC coverage: 72.574% (+0.3%) from 72.313%
22742800078

push

github

bluca
zsh: fixup some recent zsh completers

These two completers are written in a stacked _arguments style, and some
generic options are valid before or after the verb. If the toplevel
_arguments is permitted to match options after the verb, it will halt
completion prematurely, so stop toplevel matching after the verb.

This corrects the following error:

$ userdbctl --output=class user <TAB> # completes users
$ userdbctl user --output=class <TAB> # completes nothing

316081 of 435528 relevant lines covered (72.57%)

1223983.74 hits per line

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

72.0
/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 "conf-files.h"
10
#include "constants.h"
11
#include "dissect-image.h"
12
#include "format-table.h"
13
#include "glyph-util.h"
14
#include "hexdecoct.h"
15
#include "image-policy.h"
16
#include "loop-util.h"
17
#include "main-func.h"
18
#include "mount-util.h"
19
#include "os-util.h"
20
#include "pager.h"
21
#include "parse-argument.h"
22
#include "parse-util.h"
23
#include "path-util.h"
24
#include "pretty-print.h"
25
#include "set.h"
26
#include "sort-util.h"
27
#include "specifier.h"
28
#include "string-util.h"
29
#include "strv.h"
30
#include "sysupdate.h"
31
#include "sysupdate-feature.h"
32
#include "sysupdate-instance.h"
33
#include "sysupdate-transfer.h"
34
#include "sysupdate-update-set.h"
35
#include "sysupdate-util.h"
36
#include "utf8.h"
37
#include "verbs.h"
38

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

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

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

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

71
        Transfer **disabled_transfers;
72
        size_t n_disabled_transfers;
73

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

76
        UpdateSet **update_sets;
77
        size_t n_update_sets;
78

79
        UpdateSet *newest_installed, *candidate;
80

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

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

88
        FOREACH_ARRAY(tr, c->transfers, c->n_transfers)
2,888✔
89
                transfer_free(*tr);
2,408✔
90
        free(c->transfers);
480✔
91

92
        FOREACH_ARRAY(tr, c->disabled_transfers, c->n_disabled_transfers)
912✔
93
                transfer_free(*tr);
432✔
94
        free(c->disabled_transfers);
480✔
95

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

98
        FOREACH_ARRAY(us, c->update_sets, c->n_update_sets)
2,258✔
99
                update_set_free(*us);
1,778✔
100
        free(c->update_sets);
480✔
101

102
        hashmap_free(c->web_cache);
480✔
103

104
        return mfree(c);
480✔
105
}
106

107
DEFINE_TRIVIAL_CLEANUP_FUNC(Context*, context_free);
1,416✔
108

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

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

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

126
        ConfFile **files = NULL;
480✔
127
        Transfer **transfers = NULL, **disabled = NULL;
480✔
128
        size_t n_files = 0, n_transfers = 0, n_disabled = 0;
480✔
129
        int r;
480✔
130

131
        CLEANUP_ARRAY(files, n_files, conf_file_free_many);
480✔
132
        CLEANUP_ARRAY(transfers, n_transfers, free_transfers);
480✔
133
        CLEANUP_ARRAY(disabled, n_disabled, free_transfers);
480✔
134

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

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

145
        FOREACH_ARRAY(i, files, n_files) {
3,320✔
146
                _cleanup_(transfer_freep) Transfer *t = NULL;
×
147
                Transfer **appended;
2,840✔
148
                ConfFile *e = *i;
2,840✔
149

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

154
                r = transfer_read_definition(t, e->result, dirs, c->features);
2,840✔
155
                if (r < 0)
2,840✔
156
                        return r;
157

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

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

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

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

182
        assert(c);
480✔
183

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

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

194
                STRV_FOREACH(dir, l) {
40✔
195
                        char *j;
32✔
196

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

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

208
        ConfFile **files = NULL;
480✔
209
        size_t n_files = 0;
480✔
210

211
        CLEANUP_ARRAY(files, n_files, conf_file_free_many);
480✔
212

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

219
        FOREACH_ARRAY(i, files, n_files) {
952✔
220
                _cleanup_(feature_unrefp) Feature *f = NULL;
×
221
                ConfFile *e = *i;
472✔
222

223
                f = feature_new();
472✔
224
                if (!f)
472✔
225
                        return log_oom();
×
226

227
                r = feature_read_definition(f, e->result, (const char**) dirs);
472✔
228
                if (r < 0)
472✔
229
                        return r;
230

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

237
        r = read_definitions(c, (const char**) dirs, ".transfer", node);
480✔
238
        if (r < 0)
480✔
239
                return r;
240

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

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

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

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

261
        return 0;
262
}
263

264
static int context_load_installed_instances(Context *c) {
480✔
265
        int r;
480✔
266

267
        assert(c);
480✔
268

269
        log_info("Discovering installed instances%s", glyph(GLYPH_ELLIPSIS));
960✔
270

271
        FOREACH_ARRAY(tr, c->transfers, c->n_transfers) {
2,888✔
272
                Transfer *t = *tr;
2,408✔
273

274
                r = resource_load_instances(
2,408✔
275
                                &t->target,
276
                                arg_verify >= 0 ? arg_verify : t->verify,
2,408✔
277
                                &c->web_cache);
1,892✔
278
                if (r < 0)
2,408✔
279
                        return r;
280
        }
281

282
        FOREACH_ARRAY(tr, c->disabled_transfers, c->n_disabled_transfers) {
912✔
283
                Transfer *t = *tr;
432✔
284

285
                r = resource_load_instances(
432✔
286
                                &t->target,
287
                                arg_verify >= 0 ? arg_verify : t->verify,
432✔
288
                                &c->web_cache);
348✔
289
                if (r < 0)
432✔
290
                        return r;
291
        }
292

293
        return 0;
294
}
295

296
static int context_load_available_instances(Context *c) {
348✔
297
        int r;
348✔
298

299
        assert(c);
348✔
300

301
        log_info("Discovering available instances%s", glyph(GLYPH_ELLIPSIS));
696✔
302

303
        FOREACH_ARRAY(tr, c->transfers, c->n_transfers) {
2,048✔
304
                Transfer *t = *tr;
1,708✔
305

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

314
        return 0;
315
}
316

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

322
        assert(c);
788✔
323
        assert(IN_SET(flags, UPDATE_AVAILABLE, UPDATE_INSTALLED));
788✔
324

325
        for (;;) {
3,706✔
326
                _cleanup_free_ Instance **cursor_instances = NULL;
×
327
                bool skip = false;
3,706✔
328
                UpdateSetFlags extra_flags = 0;
3,706✔
329
                _cleanup_free_ char *cursor = NULL;
2,918✔
330
                UpdateSet *us = NULL;
3,706✔
331

332
                /* First, let's find the newest version that's older than the boundary. */
333
                FOREACH_ARRAY(tr, c->transfers, c->n_transfers) {
20,528✔
334
                        Resource *rr;
17,162✔
335

336
                        assert(*tr);
17,162✔
337

338
                        if (flags == UPDATE_AVAILABLE)
17,162✔
339
                                rr = &(*tr)->source;
8,928✔
340
                        else {
341
                                assert(flags == UPDATE_INSTALLED);
8,234✔
342
                                rr = &(*tr)->target;
8,234✔
343
                        }
344

345
                        FOREACH_ARRAY(inst, rr->instances, rr->n_instances) {
48,790✔
346
                                Instance *i = *inst; /* Sorted newest-to-oldest */
44,666✔
347

348
                                assert(i);
44,666✔
349

350
                                if (boundary && strverscmp_improved(i->metadata.version, boundary) >= 0)
44,666✔
351
                                        continue; /* Not older than the boundary */
31,628✔
352

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

358
                                if (free_and_strdup(&cursor, i->metadata.version) < 0)
2,918✔
359
                                        return log_oom();
×
360

361
                                break; /* All subsequent instances will be older than this one */
362
                        }
363

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

372
                if (!cursor) /* We didn't find anything older than the boundary, so we're done. */
3,706✔
373
                        break;
374

375
                cursor_instances = new0(Instance*, c->n_transfers);
2,918✔
376
                if (!cursor_instances)
2,918✔
377
                        return log_oom();
×
378

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

384
                        assert(t);
14,550✔
385

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

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

408
                        cursor_instances[k] = match;
14,296✔
409

410
                        if (t->min_version && strverscmp_improved(t->min_version, cursor) > 0)
14,296✔
411
                                extra_flags |= UPDATE_OBSOLETE;
×
412

413
                        if (strv_contains(t->protected_versions, cursor))
14,296✔
414
                                extra_flags |= UPDATE_PROTECTED;
×
415

416
                        /* Partial or pending updates by definition are not incomplete, they’re
417
                         * partial/pending instead */
418
                        if (match && match->is_partial)
14,296✔
419
                                extra_flags = (extra_flags | UPDATE_PARTIAL) & ~UPDATE_INCOMPLETE;
×
420

421
                        if (match && match->is_pending)
12,736✔
422
                                extra_flags = (extra_flags | UPDATE_PENDING) & ~UPDATE_INCOMPLETE;
220✔
423
                }
424

425
                r = free_and_strdup_warn(&boundary, cursor);
2,918✔
426
                if (r < 0)
2,918✔
427
                        return r;
428

429
                if (skip)
2,918✔
430
                        continue;
254✔
431

432
                /* See if we already have this update set in our table */
433
                FOREACH_ARRAY(update_set, c->update_sets, c->n_update_sets) {
6,694✔
434
                        UpdateSet *u = *update_set;
4,916✔
435

436
                        if (strverscmp_improved(u->version, cursor) != 0)
4,916✔
437
                                continue;
4,030✔
438

439
                        /* Merge in what we've learned and continue onto the next version */
440

441
                        if (FLAGS_SET(u->flags, UPDATE_INCOMPLETE) ||
886✔
442
                            FLAGS_SET(u->flags, UPDATE_PARTIAL) ||
566✔
443
                            FLAGS_SET(u->flags, UPDATE_PENDING)) {
566✔
444
                                assert(u->n_instances == c->n_transfers);
332✔
445

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

449
                                for (size_t j = 0; j < u->n_instances; j++) {
2,056✔
450
                                        if (!u->instances[j])
1,724✔
451
                                                u->instances[j] = cursor_instances[j];
1,136✔
452

453
                                        /* Make sure that the list is full if the update is AVAILABLE */
454
                                        assert(flags != UPDATE_AVAILABLE || u->instances[j]);
1,724✔
455
                                }
456
                        }
457

458
                        u->flags |= flags | extra_flags;
886✔
459

460
                        /* If this is the newest installed version, that is incomplete and just became marked
461
                         * as available, and if there is no other candidate available, we promote this to be
462
                         * the candidate. Ignore partial or pending status on the update set. */
463
                        if (FLAGS_SET(u->flags, UPDATE_NEWEST|UPDATE_INSTALLED|UPDATE_INCOMPLETE|UPDATE_AVAILABLE) &&
886✔
464
                            !c->candidate && !FLAGS_SET(u->flags, UPDATE_OBSOLETE))
32✔
465
                                c->candidate = u;
32✔
466

467
                        skip = true;
886✔
468
                        newest_found = true;
886✔
469
                        break;
886✔
470
                }
471

472
                if (skip)
2,664✔
473
                        continue;
886✔
474

475
                /* Doesn't exist yet, let's add it */
476
                if (!GREEDY_REALLOC(c->update_sets, c->n_update_sets + 1))
1,778✔
477
                        return log_oom();
×
478

479
                us = new(UpdateSet, 1);
1,778✔
480
                if (!us)
1,778✔
481
                        return log_oom();
×
482

483
                *us = (UpdateSet) {
1,778✔
484
                        .flags = flags | (newest_found ? 0 : UPDATE_NEWEST) | extra_flags,
1,778✔
485
                        .version = TAKE_PTR(cursor),
1,778✔
486
                        .instances = TAKE_PTR(cursor_instances),
1,778✔
487
                        .n_instances = c->n_transfers,
1,778✔
488
                };
489

490
                c->update_sets[c->n_update_sets++] = us;
1,778✔
491

492
                newest_found = true;
1,778✔
493

494
                /* Remember which one is the newest installed */
495
                if ((us->flags & (UPDATE_NEWEST|UPDATE_INSTALLED)) == (UPDATE_NEWEST|UPDATE_INSTALLED))
1,778✔
496
                        c->newest_installed = us;
430✔
497

498
                /* Remember which is the newest non-obsolete, available (and not installed) version, which we declare the "candidate".
499
                 * It may be partial or pending. */
500
                if ((us->flags & (UPDATE_NEWEST|UPDATE_INSTALLED|UPDATE_AVAILABLE|UPDATE_OBSOLETE)) == (UPDATE_NEWEST|UPDATE_AVAILABLE))
1,778✔
501
                        c->candidate = us;
112✔
502
        }
503

504
        /* Newest installed is newer than or equal to candidate? Then suppress the candidate */
505
        if (c->newest_installed && !FLAGS_SET(c->newest_installed->flags, UPDATE_INCOMPLETE) &&
788✔
506
            c->candidate && strverscmp_improved(c->newest_installed->version, c->candidate->version) >= 0)
674✔
507
                c->candidate = NULL;
12✔
508

509
        /* Newest installed is still pending and no candidate is set? Then it becomes the candidate. */
510
        if (c->newest_installed && FLAGS_SET(c->newest_installed->flags, UPDATE_PENDING) &&
788✔
511
            !c->candidate)
56✔
512
                c->candidate = c->newest_installed;
56✔
513

514
        return 0;
515
}
516

517
static int context_discover_update_sets(Context *c) {
448✔
518
        int r;
448✔
519

520
        assert(c);
448✔
521

522
        log_info("Determining installed update sets%s", glyph(GLYPH_ELLIPSIS));
896✔
523

524
        r = context_discover_update_sets_by_flag(c, UPDATE_INSTALLED);
448✔
525
        if (r < 0)
448✔
526
                return r;
527

528
        if (!arg_offline) {
448✔
529
                log_info("Determining available update sets%s", glyph(GLYPH_ELLIPSIS));
680✔
530

531
                r = context_discover_update_sets_by_flag(c, UPDATE_AVAILABLE);
340✔
532
                if (r < 0)
340✔
533
                        return r;
534
        }
535

536
        typesafe_qsort(c->update_sets, c->n_update_sets, update_set_cmp);
448✔
537
        return 0;
448✔
538
}
539

540
static int context_show_table(Context *c) {
×
541
        _cleanup_(table_unrefp) Table *t = NULL;
×
542
        int r;
×
543

544
        assert(c);
×
545

546
        t = table_new("", "version", "installed", "available", "assessment");
×
547
        if (!t)
×
548
                return log_oom();
×
549

550
        (void) table_set_align_percent(t, table_get_cell(t, 0, 0), 100);
×
551
        (void) table_set_align_percent(t, table_get_cell(t, 0, 2), 50);
×
552
        (void) table_set_align_percent(t, table_get_cell(t, 0, 3), 50);
×
553

554
        FOREACH_ARRAY(update_set, c->update_sets, c->n_update_sets) {
×
555
                UpdateSet *us = *update_set;
×
556
                const char *color;
×
557

558
                color = update_set_flags_to_color(us->flags);
×
559

560
                r = table_add_many(t,
×
561
                                   TABLE_STRING,    update_set_flags_to_glyph(us->flags),
562
                                   TABLE_SET_COLOR, color,
563
                                   TABLE_STRING,    us->version,
564
                                   TABLE_SET_COLOR, color,
565
                                   TABLE_STRING,    glyph_check_mark_space(FLAGS_SET(us->flags, UPDATE_INSTALLED)),
566
                                   TABLE_SET_COLOR, color,
567
                                   TABLE_STRING,    glyph_check_mark_space(FLAGS_SET(us->flags, UPDATE_AVAILABLE)),
568
                                   TABLE_SET_COLOR, color,
569
                                   TABLE_STRING,    update_set_flags_to_string(us->flags),
570
                                   TABLE_SET_COLOR, color);
571
                if (r < 0)
×
572
                        return table_log_add_error(r);
×
573
        }
574

575
        return table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, arg_legend);
×
576
}
577

578
static UpdateSet* context_update_set_by_version(Context *c, const char *version) {
128✔
579
        assert(c);
128✔
580
        assert(version);
128✔
581

582
        FOREACH_ARRAY(update_set, c->update_sets, c->n_update_sets)
208✔
583
                if (streq((*update_set)->version, version))
208✔
584
                        return *update_set;
585

586
        return NULL;
587
}
588

589
static int context_show_version(Context *c, const char *version) {
88✔
590
        bool show_fs_columns = false, show_partition_columns = false,
88✔
591
                have_fs_attributes = false, have_partition_attributes = false,
88✔
592
                have_size = false, have_tries = false, have_no_auto = false,
88✔
593
                have_read_only = false, have_growfs = false, have_sha256 = false;
88✔
594
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
88✔
595
        _cleanup_(table_unrefp) Table *t = NULL;
88✔
596
        _cleanup_strv_free_ char **changelog_urls = NULL;
88✔
597
        UpdateSet *us;
88✔
598
        int r;
88✔
599

600
        assert(c);
88✔
601
        assert(version);
88✔
602

603
        us = context_update_set_by_version(c, version);
88✔
604
        if (!us)
88✔
605
                return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "Update '%s' not found.", version);
×
606

607
        if (arg_json_format_flags & (SD_JSON_FORMAT_OFF|SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_PRETTY_AUTO))
88✔
608
                pager_open(arg_pager_flags);
40✔
609

610
        if (!sd_json_format_enabled(arg_json_format_flags))
88✔
611
                printf("%s%s%s Version: %s\n"
320✔
612
                       "    State: %s%s%s\n"
613
                       "Installed: %s%s%s%s\n"
614
                       "Available: %s%s\n"
615
                       "Protected: %s%s%s\n"
616
                       " Obsolete: %s%s%s\n\n",
617
                       strempty(update_set_flags_to_color(us->flags)), update_set_flags_to_glyph(us->flags), ansi_normal(), us->version,
40✔
618
                       strempty(update_set_flags_to_color(us->flags)), update_set_flags_to_string(us->flags), ansi_normal(),
40✔
619
                       yes_no(us->flags & UPDATE_INSTALLED), FLAGS_SET(us->flags, UPDATE_INSTALLED|UPDATE_NEWEST) ? " (newest)" : "",
40✔
620
                       FLAGS_SET(us->flags, UPDATE_INSTALLED|UPDATE_PENDING) ? " (pending)" : "", FLAGS_SET(us->flags, UPDATE_INSTALLED|UPDATE_PARTIAL) ? " (partial)" : "",
80✔
621
                       yes_no(us->flags & UPDATE_AVAILABLE), (us->flags & (UPDATE_INSTALLED|UPDATE_AVAILABLE|UPDATE_NEWEST)) == (UPDATE_AVAILABLE|UPDATE_NEWEST) ? " (newest)" : "",
80✔
622
                       FLAGS_SET(us->flags, UPDATE_INSTALLED|UPDATE_PROTECTED) ? ansi_highlight() : "", yes_no(FLAGS_SET(us->flags, UPDATE_INSTALLED|UPDATE_PROTECTED)), ansi_normal(),
40✔
623
                       us->flags & UPDATE_OBSOLETE ? ansi_highlight_red() : "", yes_no(us->flags & UPDATE_OBSOLETE), ansi_normal());
40✔
624

625
        t = table_new("type", "path", "ptuuid", "ptflags", "mtime", "mode", "size", "tries-done", "tries-left", "noauto", "ro", "growfs", "sha256");
88✔
626
        if (!t)
88✔
627
                return log_oom();
×
628

629
        (void) table_set_align_percent(t, table_get_cell(t, 0, 3), 100);
88✔
630
        (void) table_set_align_percent(t, table_get_cell(t, 0, 4), 100);
88✔
631
        (void) table_set_align_percent(t, table_get_cell(t, 0, 5), 100);
88✔
632
        (void) table_set_align_percent(t, table_get_cell(t, 0, 6), 100);
88✔
633
        (void) table_set_align_percent(t, table_get_cell(t, 0, 7), 100);
88✔
634
        (void) table_set_align_percent(t, table_get_cell(t, 0, 8), 100);
88✔
635
        table_set_ersatz_string(t, TABLE_ERSATZ_DASH);
88✔
636

637
        /* Starting in v257, these fields would be automatically formatted with underscores. This would have
638
         * been a breaking change, so to avoid that let's hard-code their original names. */
639
        (void) table_set_json_field_name(t, 7, "tries-done");
88✔
640
        (void) table_set_json_field_name(t, 8, "tries-left");
88✔
641

642
        /* Determine if the target will make use of partition/fs attributes for any of the transfers */
643
        FOREACH_ARRAY(transfer, c->transfers, c->n_transfers) {
544✔
644
                Transfer *tr = *transfer;
456✔
645

646
                if (tr->target.type == RESOURCE_PARTITION)
456✔
647
                        show_partition_columns = true;
176✔
648
                if (RESOURCE_IS_FILESYSTEM(tr->target.type))
456✔
649
                        show_fs_columns = true;
650

651
                STRV_FOREACH(changelog, tr->changelog) {
456✔
652
                        assert(*changelog);
×
653

654
                        _cleanup_free_ char *changelog_url = strreplace(*changelog, "@v", version);
×
655
                        if (!changelog_url)
×
656
                                return log_oom();
×
657

658
                        /* Avoid duplicates */
659
                        if (strv_contains(changelog_urls, changelog_url))
×
660
                                continue;
×
661

662
                        /* changelog_urls takes ownership of expanded changelog_url */
663
                        r = strv_consume(&changelog_urls, TAKE_PTR(changelog_url));
×
664
                        if (r < 0)
×
665
                                return log_oom();
×
666
                }
667
        }
668

669
        FOREACH_ARRAY(inst, us->instances, us->n_instances) {
544✔
670
                Instance *i = *inst;
456✔
671

672
                if (!i) {
456✔
673
                        assert(FLAGS_SET(us->flags, UPDATE_INCOMPLETE));
16✔
674
                        continue;
16✔
675
                }
676

677
                r = table_add_many(t,
440✔
678
                                   TABLE_STRING, resource_type_to_string(i->resource->type),
679
                                   TABLE_PATH, i->path);
680
                if (r < 0)
440✔
681
                        return table_log_add_error(r);
×
682

683
                if (i->metadata.partition_uuid_set) {
440✔
684
                        have_partition_attributes = true;
128✔
685
                        r = table_add_cell(t, NULL, TABLE_UUID, &i->metadata.partition_uuid);
128✔
686
                } else
687
                        r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
312✔
688
                if (r < 0)
440✔
689
                        return table_log_add_error(r);
×
690

691
                if (i->metadata.partition_flags_set) {
440✔
692
                        have_partition_attributes = true;
128✔
693
                        r = table_add_cell(t, NULL, TABLE_UINT64_HEX, &i->metadata.partition_flags);
128✔
694
                } else
695
                        r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
312✔
696
                if (r < 0)
440✔
697
                        return table_log_add_error(r);
×
698

699
                if (i->metadata.mtime != USEC_INFINITY) {
440✔
700
                        have_fs_attributes = true;
312✔
701
                        r = table_add_cell(t, NULL, TABLE_TIMESTAMP, &i->metadata.mtime);
312✔
702
                } else
703
                        r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
128✔
704
                if (r < 0)
440✔
705
                        return table_log_add_error(r);
×
706

707
                if (i->metadata.mode != MODE_INVALID) {
440✔
708
                        have_fs_attributes = true;
312✔
709
                        r = table_add_cell(t, NULL, TABLE_MODE, &i->metadata.mode);
312✔
710
                } else
711
                        r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
128✔
712
                if (r < 0)
440✔
713
                        return table_log_add_error(r);
×
714

715
                if (i->metadata.size != UINT64_MAX) {
440✔
716
                        have_size = true;
×
717
                        r = table_add_cell(t, NULL, TABLE_SIZE, &i->metadata.size);
×
718
                } else
719
                        r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
440✔
720
                if (r < 0)
440✔
721
                        return table_log_add_error(r);
×
722

723
                if (i->metadata.tries_done != UINT64_MAX) {
440✔
724
                        have_tries = true;
64✔
725
                        r = table_add_cell(t, NULL, TABLE_UINT64, &i->metadata.tries_done);
64✔
726
                } else
727
                        r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
376✔
728
                if (r < 0)
440✔
729
                        return table_log_add_error(r);
×
730

731
                if (i->metadata.tries_left != UINT64_MAX) {
440✔
732
                        have_tries = true;
64✔
733
                        r = table_add_cell(t, NULL, TABLE_UINT64, &i->metadata.tries_left);
64✔
734
                } else
735
                        r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
376✔
736
                if (r < 0)
440✔
737
                        return table_log_add_error(r);
×
738

739
                if (i->metadata.no_auto >= 0) {
440✔
740
                        bool b;
×
741

742
                        have_no_auto = true;
×
743
                        b = i->metadata.no_auto;
×
744
                        r = table_add_cell(t, NULL, TABLE_BOOLEAN, &b);
×
745
                } else
746
                        r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
440✔
747
                if (r < 0)
440✔
748
                        return table_log_add_error(r);
×
749
                if (i->metadata.read_only >= 0) {
440✔
750
                        bool b;
128✔
751

752
                        have_read_only = true;
128✔
753
                        b = i->metadata.read_only;
128✔
754
                        r = table_add_cell(t, NULL, TABLE_BOOLEAN, &b);
128✔
755
                } else
756
                        r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
312✔
757
                if (r < 0)
440✔
758
                        return table_log_add_error(r);
×
759

760
                if (i->metadata.growfs >= 0) {
440✔
761
                        bool b;
×
762

763
                        have_growfs = true;
×
764
                        b = i->metadata.growfs;
×
765
                        r = table_add_cell(t, NULL, TABLE_BOOLEAN, &b);
×
766
                } else
767
                        r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
440✔
768
                if (r < 0)
440✔
769
                        return table_log_add_error(r);
×
770

771
                if (i->metadata.sha256sum_set) {
440✔
772
                        _cleanup_free_ char *formatted = NULL;
×
773

774
                        have_sha256 = true;
×
775

776
                        formatted = hexmem(i->metadata.sha256sum, sizeof(i->metadata.sha256sum));
×
777
                        if (!formatted)
×
778
                                return log_oom();
×
779

780
                        r = table_add_cell(t, NULL, TABLE_STRING, formatted);
×
781
                } else
782
                        r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
440✔
783
                if (r < 0)
440✔
784
                        return table_log_add_error(r);
×
785
        }
786

787
        /* Hide the fs/partition columns if we don't have any data to show there */
788
        if (!have_fs_attributes)
88✔
789
                show_fs_columns = false;
×
790
        if (!have_partition_attributes)
88✔
791
                show_partition_columns = false;
792

793
        if (!show_partition_columns)
64✔
794
                (void) table_hide_column_from_display(t, 2, 3);
24✔
795
        if (!show_fs_columns)
88✔
796
                (void) table_hide_column_from_display(t, 4, 5);
×
797
        if (!have_size)
88✔
798
                (void) table_hide_column_from_display(t, 6);
88✔
799
        if (!have_tries)
88✔
800
                (void) table_hide_column_from_display(t, 7, 8);
24✔
801
        if (!have_no_auto)
88✔
802
                (void) table_hide_column_from_display(t, 9);
88✔
803
        if (!have_read_only)
88✔
804
                (void) table_hide_column_from_display(t, 10);
24✔
805
        if (!have_growfs)
88✔
806
                (void) table_hide_column_from_display(t, 11);
88✔
807
        if (!have_sha256)
88✔
808
                (void) table_hide_column_from_display(t, 12);
88✔
809

810
        if (!sd_json_format_enabled(arg_json_format_flags)) {
88✔
811
                printf("%s%s%s Version: %s\n"
360✔
812
                       "    State: %s%s%s\n"
813
                       "Installed: %s%s%s%s%s%s%s\n"
814
                       "Available: %s%s\n"
815
                       "Protected: %s%s%s\n"
816
                       " Obsolete: %s%s%s\n",
817
                       strempty(update_set_flags_to_color(us->flags)), update_set_flags_to_glyph(us->flags), ansi_normal(), us->version,
40✔
818
                       strempty(update_set_flags_to_color(us->flags)), update_set_flags_to_string(us->flags), ansi_normal(),
40✔
819
                       yes_no(us->flags & UPDATE_INSTALLED), FLAGS_SET(us->flags, UPDATE_INSTALLED|UPDATE_NEWEST) ? " (newest)" : "",
40✔
820
                       FLAGS_SET(us->flags, UPDATE_INCOMPLETE) ? ansi_highlight_yellow() : "", FLAGS_SET(us->flags, UPDATE_INCOMPLETE) ? " (incomplete)" : "", ansi_normal(),
56✔
821
                       FLAGS_SET(us->flags, UPDATE_INSTALLED|UPDATE_PENDING) ? " (pending)" : "", FLAGS_SET(us->flags, UPDATE_INSTALLED|UPDATE_PARTIAL) ? " (partial)" : "",
80✔
822
                       yes_no(us->flags & UPDATE_AVAILABLE), (us->flags & (UPDATE_INSTALLED|UPDATE_AVAILABLE|UPDATE_NEWEST)) == (UPDATE_AVAILABLE|UPDATE_NEWEST) ? " (newest)" : "",
80✔
823
                       FLAGS_SET(us->flags, UPDATE_INSTALLED|UPDATE_PROTECTED) ? ansi_highlight() : "", yes_no(FLAGS_SET(us->flags, UPDATE_INSTALLED|UPDATE_PROTECTED)), ansi_normal(),
40✔
824
                       us->flags & UPDATE_OBSOLETE ? ansi_highlight_red() : "", yes_no(us->flags & UPDATE_OBSOLETE), ansi_normal());
40✔
825

826
                STRV_FOREACH(url, changelog_urls) {
40✔
827
                        _cleanup_free_ char *changelog_link = NULL;
×
828
                        r = terminal_urlify(*url, NULL, &changelog_link);
×
829
                        if (r < 0)
×
830
                                return log_oom();
×
831
                        printf("ChangeLog: %s\n", changelog_link);
×
832
                }
833
                printf("\n");
40✔
834

835
                return table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, arg_legend);
40✔
836
        } else {
837
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *t_json = NULL;
48✔
838

839
                r = table_to_json(t, &t_json);
48✔
840
                if (r < 0)
48✔
841
                        return log_error_errno(r, "failed to convert table to JSON: %m");
×
842

843
                r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_STRING("version", us->version),
48✔
844
                                          SD_JSON_BUILD_PAIR_BOOLEAN("newest", FLAGS_SET(us->flags, UPDATE_NEWEST)),
845
                                          SD_JSON_BUILD_PAIR_BOOLEAN("available", FLAGS_SET(us->flags, UPDATE_AVAILABLE)),
846
                                          SD_JSON_BUILD_PAIR_BOOLEAN("installed", FLAGS_SET(us->flags, UPDATE_INSTALLED)),
847
                                          SD_JSON_BUILD_PAIR_BOOLEAN("partial", FLAGS_SET(us->flags, UPDATE_PARTIAL)),
848
                                          SD_JSON_BUILD_PAIR_BOOLEAN("pending", FLAGS_SET(us->flags, UPDATE_PENDING)),
849
                                          SD_JSON_BUILD_PAIR_BOOLEAN("obsolete", FLAGS_SET(us->flags, UPDATE_OBSOLETE)),
850
                                          SD_JSON_BUILD_PAIR_BOOLEAN("protected", FLAGS_SET(us->flags, UPDATE_PROTECTED)),
851
                                          SD_JSON_BUILD_PAIR_BOOLEAN("incomplete", FLAGS_SET(us->flags, UPDATE_INCOMPLETE)),
852
                                          SD_JSON_BUILD_PAIR_STRV("changelogUrls", changelog_urls),
853
                                          SD_JSON_BUILD_PAIR_VARIANT("contents", t_json));
854
                if (r < 0)
48✔
855
                        return log_error_errno(r, "Failed to create JSON: %m");
×
856

857
                r = sd_json_variant_dump(json, arg_json_format_flags, stdout, NULL);
48✔
858
                if (r < 0)
48✔
859
                        return log_error_errno(r, "Failed to print JSON: %m");
×
860

861
                return 0;
862
        }
863
}
864

865
static int context_vacuum(
80✔
866
                Context *c,
867
                uint64_t space,
868
                const char *extra_protected_version) {
869

870
        size_t disabled_count = 0;
80✔
871
        int r, count = 0;
80✔
872

873
        assert(c);
80✔
874

875
        if (space == 0)
80✔
876
                log_info("Making room%s", glyph(GLYPH_ELLIPSIS));
16✔
877
        else
878
                log_info("Making room for %" PRIu64 " updates%s", space, glyph(GLYPH_ELLIPSIS));
144✔
879

880
        FOREACH_ARRAY(tr, c->transfers, c->n_transfers) {
488✔
881
                Transfer *t = *tr;
408✔
882

883
                /* Don't bother clearing out space if we're not going to be downloading anything */
884
                if (extra_protected_version && resource_find_instance(&t->target, extra_protected_version))
408✔
885
                        continue;
72✔
886

887
                r = transfer_vacuum(t, space, extra_protected_version);
336✔
888
                if (r < 0)
336✔
889
                        return r;
890

891
                count = MAX(count, r);
336✔
892
        }
893

894
        FOREACH_ARRAY(tr, c->disabled_transfers, c->n_disabled_transfers) {
152✔
895
                r = transfer_vacuum(*tr, UINT64_MAX /* wipe all instances */, NULL);
72✔
896
                if (r < 0)
72✔
897
                        return r;
898
                if (r > 0)
72✔
899
                        disabled_count++;
8✔
900
        }
901

902
        if (!sd_json_format_enabled(arg_json_format_flags)) {
80✔
903
                if (count > 0 && disabled_count > 0)
60✔
904
                        log_info("Removed %i instances, and %zu disabled transfers.", count, disabled_count);
×
905
                else if (count > 0)
60✔
906
                        log_info("Removed %i instances.", count);
24✔
907
                else if (disabled_count > 0)
36✔
908
                        log_info("Removed %zu disabled transfers.", disabled_count);
8✔
909
                else
910
                        log_info("Found nothing to remove.");
28✔
911
        } else {
912
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
20✔
913

914
                r = sd_json_buildo(&json,
20✔
915
                                   SD_JSON_BUILD_PAIR_INTEGER("removed", count),
916
                                   SD_JSON_BUILD_PAIR_UNSIGNED("disabledTransfers", disabled_count));
917
                if (r < 0)
20✔
918
                        return log_error_errno(r, "Failed to create JSON: %m");
×
919

920
                r = sd_json_variant_dump(json, arg_json_format_flags, stdout, NULL);
20✔
921
                if (r < 0)
20✔
922
                        return log_error_errno(r, "Failed to print JSON: %m");
×
923
        }
924

925
        return 0;
926
}
927

928
static int context_make_offline(Context **ret, const char *node, bool requires_enabled_transfers) {
480✔
929
        _cleanup_(context_freep) Context* context = NULL;
480✔
930
        int r;
480✔
931

932
        assert(ret);
480✔
933

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

937
        context = context_new();
480✔
938
        if (!context)
480✔
939
                return log_oom();
×
940

941
        r = context_read_definitions(context, node, requires_enabled_transfers);
480✔
942
        if (r < 0)
480✔
943
                return r;
944

945
        r = context_load_installed_instances(context);
480✔
946
        if (r < 0)
480✔
947
                return r;
948

949
        *ret = TAKE_PTR(context);
480✔
950
        return 0;
480✔
951
}
952

953
static int context_make_online(Context **ret, const char *node) {
456✔
954
        _cleanup_(context_freep) Context* context = NULL;
456✔
955
        int r;
456✔
956

957
        assert(ret);
456✔
958

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

962
        r = context_make_offline(&context, node, /* requires_enabled_transfers= */ true);
456✔
963
        if (r < 0)
456✔
964
                return r;
965

966
        if (!arg_offline) {
456✔
967
                r = context_load_available_instances(context);
348✔
968
                if (r < 0)
348✔
969
                        return r;
970
        }
971

972
        r = context_discover_update_sets(context);
448✔
973
        if (r < 0)
448✔
974
                return r;
975

976
        *ret = TAKE_PTR(context);
448✔
977
        return 0;
448✔
978
}
979

980
static int context_on_acquire_progress(const Transfer *t, const Instance *inst, unsigned percentage) {
248✔
981
        const Context *c = ASSERT_PTR(t->context);
248✔
982
        size_t i, n = c->n_transfers;
248✔
983
        uint64_t base, scaled;
248✔
984
        unsigned overall;
248✔
985

986
        for (i = 0; i < n; i++)
688✔
987
                if (c->transfers[i] == t)
688✔
988
                        break;
989
        assert(i < n); /* We should have found the index */
248✔
990

991
        base = (100 * 100 * i) / n;
248✔
992
        scaled = (100 * percentage) / n;
248✔
993
        overall = (unsigned) ((base + scaled) / 100);
248✔
994
        assert(overall <= 100);
248✔
995

996
        log_debug("Transfer %zu/%zu is %u%% complete (%u%% overall).", i+1, n, percentage, overall);
248✔
997
        return sd_notifyf(/* unset_environment= */ false, "X_SYSUPDATE_PROGRESS=%u\n"
496✔
998
                                              "X_SYSUPDATE_TRANSFERS_LEFT=%zu\n"
999
                                              "X_SYSUPDATE_TRANSFERS_DONE=%zu\n"
1000
                                              "STATUS=Updating to '%s' (%u%% complete).",
1001
                                              overall, n - i, i, inst->metadata.version, overall);
248✔
1002
}
1003

1004
static int context_process_partial_and_pending(Context *c, const char *version);
1005

1006
static int context_acquire(
108✔
1007
                Context *c,
1008
                const char *version) {
1009

1010
        UpdateSet *us = NULL;
108✔
1011
        int r;
108✔
1012

1013
        assert(c);
108✔
1014

1015
        if (version) {
108✔
1016
                us = context_update_set_by_version(c, version);
×
1017
                if (!us)
×
1018
                        return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "Update '%s' not found.", version);
108✔
1019
        } else {
1020
                if (!c->candidate) {
108✔
1021
                        log_info("No update needed.");
24✔
1022

1023
                        return 0;
24✔
1024
                }
1025

1026
                us = c->candidate;
1027
        }
1028

1029
        if (FLAGS_SET(us->flags, UPDATE_INCOMPLETE))
84✔
1030
                log_info("Selected update '%s' is already installed, but incomplete. Repairing.", us->version);
16✔
1031
        else if (FLAGS_SET(us->flags, UPDATE_PARTIAL)) {
68✔
1032
                log_info("Selected update '%s' is already acquired and partially installed. Vacuum it to try installing again.", us->version);
×
1033

1034
                return 0;
×
1035
        } else if (FLAGS_SET(us->flags, UPDATE_PENDING)) {
68✔
1036
                log_info("Selected update '%s' is already acquired and pending installation.", us->version);
12✔
1037

1038
                return context_process_partial_and_pending(c, version);
12✔
1039
        } else if (FLAGS_SET(us->flags, UPDATE_INSTALLED)) {
56✔
1040
                log_info("Selected update '%s' is already installed. Skipping update.", us->version);
×
1041

1042
                return 0;
×
1043
        }
1044

1045
        if (!FLAGS_SET(us->flags, UPDATE_AVAILABLE))
72✔
1046
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Selected update '%s' is not available, refusing.", us->version);
×
1047
        if (FLAGS_SET(us->flags, UPDATE_OBSOLETE))
72✔
1048
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Selected update '%s' is obsolete, refusing.", us->version);
×
1049

1050
        if (!FLAGS_SET(us->flags, UPDATE_NEWEST))
72✔
1051
                log_notice("Selected update '%s' is not the newest, proceeding anyway.", us->version);
×
1052
        if (c->newest_installed && strverscmp_improved(c->newest_installed->version, us->version) > 0)
72✔
1053
                log_notice("Selected update '%s' is older than newest installed version, proceeding anyway.", us->version);
×
1054

1055
        log_info("Selected update '%s' for install.", us->version);
72✔
1056

1057
        _cleanup_free_ InstanceMetadata *metadata = new0(InstanceMetadata, c->n_transfers);
144✔
1058
        if (!metadata)
72✔
1059
                return log_oom();
×
1060

1061
        /* Compute up the temporary paths before vacuuming so we don't vacuum anything if we fail to compute
1062
         * any paths because of failed validations (e.g. exceeding the gpt partition label size). */
1063
        for (size_t i = 0; i < c->n_transfers; i++) {
440✔
1064
                Instance *inst = us->instances[i];
368✔
1065
                Transfer *t = c->transfers[i];
368✔
1066

1067
                assert(inst);
368✔
1068

1069
                r = transfer_compute_temporary_paths(t, inst, metadata + i);
368✔
1070
                if (r < 0)
368✔
1071
                        return r;
1072
        }
1073

1074
        (void) sd_notifyf(/* unset_environment= */ false,
72✔
1075
                          "READY=1\n"
1076
                          "X_SYSUPDATE_VERSION=%s\n"
1077
                          "STATUS=Making room for '%s'.", us->version, us->version);
1078

1079
        /* Let's make some room. We make sure for each transfer we have one free space to fill. While
1080
         * removing stuff we'll protect the version we are trying to acquire. Why that? Maybe an earlier
1081
         * download succeeded already, in which case we shouldn't remove it just to acquire it again */
1082
        r = context_vacuum(
144✔
1083
                        c,
1084
                        /* space= */ 1,
1085
                        /* extra_protected_version= */ us->version);
72✔
1086
        if (r < 0)
72✔
1087
                return r;
1088

1089
        if (arg_sync)
72✔
1090
                sync();
72✔
1091

1092
        (void) sd_notifyf(/* unset_environment= */ false,
72✔
1093
                          "STATUS=Updating to '%s'.", us->version);
1094

1095
        /* There should now be one instance picked for each transfer, and the order is the same */
1096
        assert(us->n_instances == c->n_transfers);
72✔
1097

1098
        for (size_t i = 0; i < c->n_transfers; i++) {
440✔
1099
                Instance *inst = us->instances[i];
368✔
1100
                Transfer *t = c->transfers[i];
368✔
1101

1102
                assert(inst); /* ditto */
368✔
1103

1104
                if (inst->resource == &t->target) { /* a present transfer in an incomplete installation */
368✔
1105
                        assert(FLAGS_SET(us->flags, UPDATE_INCOMPLETE));
72✔
1106
                        continue;
72✔
1107
                }
1108

1109
                r = transfer_acquire_instance(t, inst, metadata + i, context_on_acquire_progress, c);
296✔
1110
                if (r < 0)
296✔
1111
                        return r;
1112
        }
1113

1114
        if (arg_sync)
72✔
1115
                sync();
72✔
1116

1117
        return 1;
1118
}
1119

1120
/* Check to see if we have an update set acquired and pending installation. */
1121
static int context_process_partial_and_pending(
44✔
1122
                Context *c,
1123
                const char *version) {
1124

1125
        UpdateSet *us = NULL;
44✔
1126
        int r;
44✔
1127

1128
        assert(c);
44✔
1129

1130
        if (version) {
44✔
1131
                us = context_update_set_by_version(c, version);
20✔
1132
                if (!us)
20✔
1133
                        return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "Update '%s' not found.", version);
×
1134
        } else {
1135
                if (!c->candidate) {
24✔
1136
                        log_info("No update needed.");
×
1137

1138
                        return 0;
×
1139
                }
1140

1141
                us = c->candidate;
1142
        }
1143

1144
        if (FLAGS_SET(us->flags, UPDATE_INCOMPLETE))
44✔
1145
                log_info("Selected update '%s' is already installed, but incomplete. Repairing.", us->version);
×
1146
        else if ((us->flags & (UPDATE_PARTIAL|UPDATE_PENDING|UPDATE_INSTALLED)) == UPDATE_INSTALLED) {
44✔
1147
                log_info("Selected update '%s' is already installed. Skipping update.", us->version);
×
1148

1149
                return 0;
×
1150
        }
1151

1152
        if (FLAGS_SET(us->flags, UPDATE_PARTIAL))
44✔
1153
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Selected update '%s' is only partially downloaded, refusing.", us->version);
×
1154
        if (!FLAGS_SET(us->flags, UPDATE_PENDING))
44✔
1155
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Selected update '%s' is not pending installation, refusing.", us->version);
×
1156

1157
        if (FLAGS_SET(us->flags, UPDATE_OBSOLETE))
44✔
1158
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Selected update '%s' is obsolete, refusing.", us->version);
×
1159

1160
        if (!FLAGS_SET(us->flags, UPDATE_NEWEST))
44✔
1161
                log_notice("Selected update '%s' is not the newest, proceeding anyway.", us->version);
×
1162
        if (c->newest_installed && strverscmp_improved(c->newest_installed->version, us->version) > 0)
44✔
1163
                log_notice("Selected update '%s' is older than newest installed version, proceeding anyway.", us->version);
×
1164

1165
        log_info("Selected update '%s' for install.", us->version);
44✔
1166

1167
        /* There should now be one instance picked for each transfer, and the order is the same */
1168
        assert(us->n_instances == c->n_transfers);
44✔
1169

1170
        for (size_t i = 0; i < c->n_transfers; i++) {
264✔
1171
                Instance *inst = us->instances[i];
220✔
1172
                Transfer *t = c->transfers[i];
220✔
1173

1174
                assert(inst);
220✔
1175

1176
                r = transfer_process_partial_and_pending_instance(t, inst);
220✔
1177
                if (r < 0)
220✔
1178
                        return r;
1179
        }
1180

1181
        return 1;
1182
}
1183

1184
static int context_install(
72✔
1185
                Context *c,
1186
                const char *version,
1187
                UpdateSet **ret_applied) {
1188

1189
        UpdateSet *us = NULL;
72✔
1190
        int r;
72✔
1191

1192
        assert(c);
72✔
1193

1194
        if (version) {
72✔
1195
                us = context_update_set_by_version(c, version);
20✔
1196
                if (!us)
20✔
1197
                        return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "Update '%s' not found.", version);
×
1198
        } else {
1199
                if (!c->candidate) {
52✔
1200
                        log_info("No update needed.");
×
1201

1202
                        return 0;
×
1203
                }
1204

1205
                us = c->candidate;
1206
        }
1207

1208
        (void) sd_notifyf(/* unset_environment=*/ false,
72✔
1209
                          "STATUS=Installing '%s'.", us->version);
1210

1211
        for (size_t i = 0; i < c->n_transfers; i++) {
440✔
1212
                Instance *inst = us->instances[i];
368✔
1213
                Transfer *t = c->transfers[i];
368✔
1214

1215
                if (inst->resource == &t->target &&
368✔
1216
                    !inst->is_pending)
292✔
1217
                        continue;
72✔
1218

1219
                r = transfer_install_instance(t, inst, arg_root);
296✔
1220
                if (r < 0)
296✔
1221
                        return r;
1222
        }
1223

1224
        log_info("%s Successfully installed update '%s'.", glyph(GLYPH_SPARKLES), us->version);
72✔
1225

1226
        (void) sd_notifyf(/* unset_environment= */ false,
72✔
1227
                          "STATUS=Installed '%s'.", us->version);
1228

1229
        if (ret_applied)
72✔
1230
                *ret_applied = us;
72✔
1231

1232
        return 1;
1233
}
1234

1235
static int process_image(
552✔
1236
                bool ro,
1237
                char **ret_mounted_dir,
1238
                LoopDevice **ret_loop_device) {
1239

1240
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
552✔
1241
        _cleanup_(umount_and_freep) char *mounted_dir = NULL;
552✔
1242
        int r;
552✔
1243

1244
        assert(ret_mounted_dir);
552✔
1245
        assert(ret_loop_device);
552✔
1246

1247
        if (!arg_image)
552✔
1248
                return 0;
1249

1250
        assert(!arg_root);
×
1251

1252
        r = mount_image_privately_interactively(
×
1253
                        arg_image,
1254
                        arg_image_policy,
1255
                        (ro ? DISSECT_IMAGE_READ_ONLY : 0) |
1256
                        DISSECT_IMAGE_FSCK |
1257
                        DISSECT_IMAGE_MKDIR |
1258
                        DISSECT_IMAGE_GROWFS |
1259
                        DISSECT_IMAGE_RELAX_VAR_CHECK |
1260
                        DISSECT_IMAGE_USR_NO_ROOT |
1261
                        DISSECT_IMAGE_GENERIC_ROOT |
1262
                        DISSECT_IMAGE_REQUIRE_ROOT |
1263
                        DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
1264
                        &mounted_dir,
1265
                        /* ret_dir_fd= */ NULL,
1266
                        &loop_device);
1267
        if (r < 0)
×
1268
                return r;
1269

1270
        arg_root = strdup(mounted_dir);
×
1271
        if (!arg_root)
×
1272
                return log_oom();
×
1273

1274
        *ret_mounted_dir = TAKE_PTR(mounted_dir);
×
1275
        *ret_loop_device = TAKE_PTR(loop_device);
×
1276

1277
        return 0;
×
1278
}
1279

1280
static int verb_list(int argc, char **argv, void *userdata) {
132✔
1281
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
132✔
1282
        _cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL;
132✔
1283
        _cleanup_(context_freep) Context* context = NULL;
132✔
1284
        _cleanup_strv_free_ char **appstream_urls = NULL;
132✔
1285
        const char *version;
132✔
1286
        int r;
132✔
1287

1288
        assert(argc <= 2);
132✔
1289
        version = argc >= 2 ? argv[1] : NULL;
132✔
1290

1291
        r = process_image(/* ro= */ true, &mounted_dir, &loop_device);
132✔
1292
        if (r < 0)
132✔
1293
                return r;
1294

1295
        r = context_make_online(&context, loop_device ? loop_device->node : NULL);
132✔
1296
        if (r < 0)
132✔
1297
                return r;
1298

1299
        if (version)
132✔
1300
                return context_show_version(context, version);
88✔
1301
        else if (!sd_json_format_enabled(arg_json_format_flags))
44✔
1302
                return context_show_table(context);
×
1303
        else {
1304
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
44✔
1305
                _cleanup_strv_free_ char **versions = NULL;
44✔
1306
                const char *current = NULL;
44✔
1307
                bool current_is_pending = false;
44✔
1308

1309
                FOREACH_ARRAY(update_set, context->update_sets, context->n_update_sets) {
180✔
1310
                        UpdateSet *us = *update_set;
136✔
1311

1312
                        if (FLAGS_SET(us->flags, UPDATE_INSTALLED) &&
136✔
1313
                            FLAGS_SET(us->flags, UPDATE_NEWEST)) {
120✔
1314
                                current = us->version;
42✔
1315
                                current_is_pending = FLAGS_SET(us->flags, UPDATE_PENDING);
42✔
1316
                        }
1317

1318
                        r = strv_extend(&versions, us->version);
136✔
1319
                        if (r < 0)
136✔
1320
                                return log_oom();
×
1321
                }
1322

1323
                FOREACH_ARRAY(tr, context->transfers, context->n_transfers)
264✔
1324
                        STRV_FOREACH(appstream_url, (*tr)->appstream) {
220✔
1325
                                /* Avoid duplicates */
1326
                                if (strv_contains(appstream_urls, *appstream_url))
×
1327
                                        continue;
×
1328

1329
                                r = strv_extend(&appstream_urls, *appstream_url);
×
1330
                                if (r < 0)
×
1331
                                        return log_oom();
×
1332
                        }
1333

1334
                r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_STRING(current_is_pending ? "current+pending" : "current", current),
88✔
1335
                                          SD_JSON_BUILD_PAIR_STRV("all", versions),
1336
                                          SD_JSON_BUILD_PAIR_STRV("appstreamUrls", appstream_urls));
1337
                if (r < 0)
44✔
1338
                        return log_error_errno(r, "Failed to create JSON: %m");
×
1339

1340
                r = sd_json_variant_dump(json, arg_json_format_flags, stdout, NULL);
44✔
1341
                if (r < 0)
44✔
1342
                        return log_error_errno(r, "Failed to print JSON: %m");
×
1343

1344
                return 0;
1345
        }
1346
}
1347

1348
static int verb_features(int argc, char **argv, void *userdata) {
16✔
1349
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
16✔
1350
        _cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL;
16✔
1351
        _cleanup_(context_freep) Context* context = NULL;
16✔
1352
        _cleanup_(table_unrefp) Table *table = NULL;
16✔
1353
        const char *feature_id;
16✔
1354
        Feature *f;
16✔
1355
        int r;
16✔
1356

1357
        assert(argc <= 2);
16✔
1358
        feature_id = argc >= 2 ? argv[1] : NULL;
16✔
1359

1360
        r = process_image(/* ro= */ true, &mounted_dir, &loop_device);
16✔
1361
        if (r < 0)
16✔
1362
                return r;
1363

1364
        r = context_make_offline(&context, loop_device ? loop_device->node : NULL, /* requires_enabled_transfers= */ false);
16✔
1365
        if (r < 0)
16✔
1366
                return r;
1367

1368
        if (feature_id) {
16✔
1369
                _cleanup_strv_free_ char **transfers = NULL;
8✔
1370

1371
                f = hashmap_get(context->features, feature_id);
8✔
1372
                if (!f)
8✔
1373
                        return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
×
1374
                                               "Optional feature not found: %s",
1375
                                               feature_id);
1376

1377
                table = table_new_vertical();
8✔
1378
                if (!table)
8✔
1379
                        return log_oom();
×
1380

1381
                FOREACH_ARRAY(tr, context->transfers, context->n_transfers) {
48✔
1382
                        Transfer *t = *tr;
40✔
1383

1384
                        if (!strv_contains(t->features, f->id) && !strv_contains(t->requisite_features, f->id))
40✔
1385
                                continue;
40✔
1386

1387
                        r = strv_extend(&transfers, t->id);
×
1388
                        if (r < 0)
×
1389
                                return log_oom();
×
1390
                }
1391

1392
                FOREACH_ARRAY(tr, context->disabled_transfers, context->n_disabled_transfers) {
16✔
1393
                        Transfer *t = *tr;
8✔
1394

1395
                        if (!strv_contains(t->features, f->id) && !strv_contains(t->requisite_features, f->id))
8✔
1396
                                continue;
×
1397

1398
                        r = strv_extend(&transfers, t->id);
8✔
1399
                        if (r < 0)
8✔
1400
                                return log_oom();
×
1401
                }
1402

1403
                r = table_add_many(table,
8✔
1404
                                   TABLE_FIELD, "Name",
1405
                                   TABLE_STRING, f->id,
1406
                                   TABLE_FIELD, "Enabled",
1407
                                   TABLE_BOOLEAN, f->enabled);
1408
                if (r < 0)
8✔
1409
                        return table_log_add_error(r);
×
1410

1411
                if (f->description) {
8✔
1412
                        r = table_add_many(table, TABLE_FIELD, "Description", TABLE_STRING, f->description);
8✔
1413
                        if (r < 0)
8✔
1414
                                return table_log_add_error(r);
×
1415
                }
1416

1417
                if (f->documentation) {
8✔
1418
                        r = table_add_many(table,
×
1419
                                           TABLE_FIELD, "Documentation",
1420
                                           TABLE_STRING, f->documentation,
1421
                                           TABLE_SET_URL, f->documentation);
1422
                        if (r < 0)
×
1423
                                return table_log_add_error(r);
×
1424
                }
1425

1426
                if (f->appstream) {
8✔
1427
                        r = table_add_many(table,
×
1428
                                           TABLE_FIELD, "AppStream",
1429
                                           TABLE_STRING, f->appstream,
1430
                                           TABLE_SET_URL, f->appstream);
1431
                        if (r < 0)
×
1432
                                return table_log_add_error(r);
×
1433
                }
1434

1435
                if (!strv_isempty(transfers)) {
8✔
1436
                        r = table_add_many(table, TABLE_FIELD, "Transfers", TABLE_STRV_WRAPPED, transfers);
8✔
1437
                        if (r < 0)
8✔
1438
                                return table_log_add_error(r);
×
1439
                }
1440

1441
                return table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
8✔
1442
        } else if (FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) {
8✔
1443
                table = table_new("", "feature", "description", "documentation");
8✔
1444
                if (!table)
8✔
1445
                        return log_oom();
×
1446

1447
                HASHMAP_FOREACH(f, context->features) {
16✔
1448
                        r = table_add_many(table,
8✔
1449
                                           TABLE_BOOLEAN_CHECKMARK, f->enabled,
1450
                                           TABLE_SET_COLOR, ansi_highlight_green_red(f->enabled),
1451
                                           TABLE_STRING, f->id,
1452
                                           TABLE_STRING, f->description,
1453
                                           TABLE_STRING, f->documentation,
1454
                                           TABLE_SET_URL, f->documentation);
1455
                        if (r < 0)
8✔
1456
                                return table_log_add_error(r);
×
1457
                }
1458

1459
                return table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
8✔
1460
        } else {
1461
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
×
1462
                _cleanup_strv_free_ char **features = NULL;
×
1463

1464
                HASHMAP_FOREACH(f, context->features) {
×
1465
                        r = strv_extend(&features, f->id);
×
1466
                        if (r < 0)
×
1467
                                return log_oom();
×
1468
                }
1469

1470
                r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_STRV("features", features));
×
1471
                if (r < 0)
×
1472
                        return log_error_errno(r, "Failed to create JSON: %m");
×
1473

1474
                r = sd_json_variant_dump(json, arg_json_format_flags, stdout, NULL);
×
1475
                if (r < 0)
×
1476
                        return log_error_errno(r, "Failed to print JSON: %m");
×
1477
        }
1478

1479
        return 0;
×
1480
}
1481

1482
static int verb_check_new(int argc, char **argv, void *userdata) {
176✔
1483
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
176✔
1484
        _cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL;
176✔
1485
        _cleanup_(context_freep) Context* context = NULL;
176✔
1486
        int r;
176✔
1487

1488
        assert(argc <= 1);
176✔
1489

1490
        r = process_image(/* ro= */ true, &mounted_dir, &loop_device);
176✔
1491
        if (r < 0)
176✔
1492
                return r;
1493

1494
        r = context_make_online(&context, loop_device ? loop_device->node : NULL);
176✔
1495
        if (r < 0)
176✔
1496
                return r;
1497

1498
        if (!sd_json_format_enabled(arg_json_format_flags)) {
176✔
1499
                if (!context->candidate) {
160✔
1500
                        log_debug("No candidate found.");
88✔
1501
                        return EXIT_FAILURE;
88✔
1502
                }
1503

1504
                puts(context->candidate->version);
72✔
1505
        } else {
1506
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
16✔
1507

1508
                if (context->candidate)
16✔
1509
                        r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_STRING("available", context->candidate->version));
×
1510
                else
1511
                        r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_NULL("available"));
16✔
1512
                if (r < 0)
16✔
1513
                        return log_error_errno(r, "Failed to create JSON: %m");
×
1514

1515
                r = sd_json_variant_dump(json, arg_json_format_flags, stdout, NULL);
16✔
1516
                if (r < 0)
16✔
1517
                        return log_error_errno(r, "Failed to print JSON: %m");
×
1518
        }
1519

1520
        return EXIT_SUCCESS;
1521
}
1522

1523
static int verb_vacuum(int argc, char **argv, void *userdata) {
8✔
1524
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
8✔
1525
        _cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL;
8✔
1526
        _cleanup_(context_freep) Context* context = NULL;
8✔
1527
        int r;
8✔
1528

1529
        assert(argc <= 1);
8✔
1530

1531
        if (arg_instances_max < 1)
8✔
1532
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1533
                                      "The --instances-max argument must be >= 1 while vacuuming");
1534

1535
        r = process_image(/* ro= */ false, &mounted_dir, &loop_device);
8✔
1536
        if (r < 0)
8✔
1537
                return r;
1538

1539
        r = context_make_offline(&context, loop_device ? loop_device->node : NULL, /* requires_enabled_transfers= */ false);
8✔
1540
        if (r < 0)
8✔
1541
                return r;
1542

1543
        return context_vacuum(context, 0, NULL);
8✔
1544
}
1545

1546
typedef enum {
1547
        UPDATE_ACTION_ACQUIRE = 1 << 0,
1548
        UPDATE_ACTION_INSTALL = 1 << 1,
1549
} UpdateActionFlags;
1550

1551
static int verb_update_impl(int argc, char **argv, UpdateActionFlags action_flags) {
148✔
1552
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
148✔
1553
        _cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL;
148✔
1554
        _cleanup_(context_freep) Context* context = NULL;
×
1555
        _cleanup_free_ char *booted_version = NULL;
148✔
1556
        UpdateSet *applied = NULL;
148✔
1557
        const char *version;
148✔
1558
        int r;
148✔
1559

1560
        assert(argc <= 2);
148✔
1561
        version = argc >= 2 ? argv[1] : NULL;
148✔
1562

1563
        if (arg_instances_max < 2)
148✔
1564
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1565
                                      "The --instances-max argument must be >= 2 while updating");
1566

1567
        if (arg_reboot) {
148✔
1568
                /* If automatic reboot on completion is requested, let's first determine the currently booted image */
1569

1570
                r = parse_os_release(arg_root, "IMAGE_VERSION", &booted_version);
×
1571
                if (r < 0)
×
1572
                        return log_error_errno(r, "Failed to parse /etc/os-release: %m");
×
1573
                if (!booted_version)
×
1574
                        return log_error_errno(SYNTHETIC_ERRNO(ENODATA), "/etc/os-release lacks IMAGE_VERSION field.");
×
1575
        }
1576

1577
        r = process_image(/* ro= */ false, &mounted_dir, &loop_device);
148✔
1578
        if (r < 0)
148✔
1579
                return r;
1580

1581
        r = context_make_online(&context, loop_device ? loop_device->node : NULL);
148✔
1582
        if (r < 0)
148✔
1583
                return r;
1584

1585
        if (action_flags & UPDATE_ACTION_ACQUIRE)
140✔
1586
                r = context_acquire(context, version);
108✔
1587
        else
1588
                r = context_process_partial_and_pending(context, version);
32✔
1589
        if (r < 0)
140✔
1590
                return r;  /* error */
1591

1592
        if (action_flags & UPDATE_ACTION_INSTALL && r > 0)  /* update needed */
140✔
1593
                r = context_install(context, version, &applied);
72✔
1594
        if (r < 0)
140✔
1595
                return r;
1596

1597
        if (r > 0 && arg_reboot) {
140✔
1598
                assert(applied);
×
1599
                assert(booted_version);
×
1600

1601
                if (strverscmp_improved(applied->version, booted_version) > 0) {
×
1602
                        log_notice("Newly installed version is newer than booted version, rebooting.");
×
1603
                        return reboot_now();
×
1604
                }
1605

1606
                if (strverscmp_improved(applied->version, booted_version) == 0 &&
×
1607
                    FLAGS_SET(applied->flags, UPDATE_INCOMPLETE)) {
×
1608
                        log_notice("Currently booted version was incomplete and has been repaired, rebooting.");
×
1609
                        return reboot_now();
×
1610
                }
1611

1612
                log_info("Booted version is newer or identical to newly installed version, not rebooting.");
×
1613
        }
1614

1615
        return 0;
1616
}
1617

1618
static int verb_update(int argc, char **argv, void *userdata) {
104✔
1619
        UpdateActionFlags flags = UPDATE_ACTION_INSTALL;
104✔
1620

1621
        if (!arg_offline)
104✔
1622
                flags |= UPDATE_ACTION_ACQUIRE;
72✔
1623

1624
        return verb_update_impl(argc, argv, flags);
104✔
1625
}
1626

1627
static int verb_acquire(int argc, char **argv, void *userdata) {
44✔
1628
        return verb_update_impl(argc, argv, UPDATE_ACTION_ACQUIRE);
44✔
1629
}
1630

1631
static int verb_pending_or_reboot(int argc, char **argv, void *userdata) {
×
1632
        _cleanup_(context_freep) Context* context = NULL;
×
1633
        _cleanup_free_ char *booted_version = NULL;
×
1634
        int r;
×
1635

1636
        assert(argc == 1);
×
1637

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

1642
        r = context_make_offline(&context, /* node= */ NULL, /* requires_enabled_transfers= */ true);
×
1643
        if (r < 0)
×
1644
                return r;
1645

1646
        log_info("Determining installed update sets%s", glyph(GLYPH_ELLIPSIS));
×
1647

1648
        r = context_discover_update_sets_by_flag(context, UPDATE_INSTALLED);
×
1649
        if (r < 0)
×
1650
                return r;
1651
        if (!context->newest_installed)
×
1652
                return log_error_errno(SYNTHETIC_ERRNO(ENODATA), "Couldn't find any suitable installed versions.");
×
1653

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

1661
        r = strverscmp_improved(context->newest_installed->version, booted_version);
×
1662
        if (r > 0) {
×
1663
                log_notice("Newest installed version '%s' is newer than booted version '%s'.%s",
×
1664
                           context->newest_installed->version, booted_version,
1665
                           streq(argv[0], "pending") ? " Reboot recommended." : "");
1666

1667
                if (streq(argv[0], "reboot"))
×
1668
                        return reboot_now();
×
1669

1670
                return EXIT_SUCCESS;
1671
        } else if (r == 0)
×
1672
                log_info("Newest installed version '%s' matches booted version '%s'.",
×
1673
                         context->newest_installed->version, booted_version);
1674
        else
1675
                log_warning("Newest installed version '%s' is older than booted version '%s'.",
×
1676
                            context->newest_installed->version, booted_version);
1677

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

1681
        return EXIT_SUCCESS;
1682
}
1683

1684
static int component_name_valid(const char *c) {
32✔
1685
        _cleanup_free_ char *j = NULL;
32✔
1686

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

1689
        if (isempty(c))
64✔
1690
                return false;
1691

1692
        if (string_has_cc(c, NULL))
32✔
1693
                return false;
1694

1695
        if (!utf8_is_valid(c))
32✔
1696
                return false;
1697

1698
        j = strjoin("sysupdate.", c, ".d");
32✔
1699
        if (!j)
32✔
1700
                return -ENOMEM;
1701

1702
        return filename_is_valid(j);
32✔
1703
}
1704

1705
static int verb_components(int argc, char **argv, void *userdata) {
72✔
1706
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
72✔
1707
        _cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL;
72✔
1708
        _cleanup_set_free_ Set *names = NULL;
72✔
1709
        bool has_default_component = false;
72✔
1710
        int r;
72✔
1711

1712
        assert(argc <= 1);
72✔
1713

1714
        r = process_image(/* ro= */ false, &mounted_dir, &loop_device);
72✔
1715
        if (r < 0)
72✔
1716
                return r;
1717

1718
        ConfFile **directories = NULL;
72✔
1719
        size_t n_directories = 0;
72✔
1720

1721
        CLEANUP_ARRAY(directories, n_directories, conf_file_free_many);
72✔
1722

1723
        r = conf_files_list_strv_full(".d", arg_root, CONF_FILES_DIRECTORY|CONF_FILES_WARN,
144✔
1724
                                      (const char * const *) CONF_PATHS_STRV(""), &directories, &n_directories);
72✔
1725
        if (r < 0)
72✔
1726
                return log_error_errno(r, "Failed to enumerate directories: %m");
×
1727

1728
        FOREACH_ARRAY(i, directories, n_directories) {
1,896✔
1729
                ConfFile *e = *i;
1,824✔
1730

1731
                if (streq(e->filename, "sysupdate.d")) {
1,824✔
1732
                        has_default_component = true;
72✔
1733
                        continue;
72✔
1734
                }
1735

1736
                const char *s = startswith(e->filename, "sysupdate.");
1,752✔
1737
                if (!s)
1,752✔
1738
                        continue;
1,728✔
1739

1740
                const char *a = endswith(s, ".d");
24✔
1741
                if (!a)
24✔
1742
                        continue;
×
1743

1744
                _cleanup_free_ char *n = strndup(s, a - s);
×
1745
                if (!n)
24✔
1746
                        return log_oom();
×
1747

1748
                r = component_name_valid(n);
24✔
1749
                if (r < 0)
24✔
1750
                        return log_error_errno(r, "Unable to validate component name '%s': %m", n);
×
1751
                if (r == 0)
24✔
1752
                        continue;
×
1753

1754
                r = set_ensure_put(&names, &string_hash_ops_free, n);
24✔
1755
                if (r < 0 && r != -EEXIST)
24✔
1756
                        return log_error_errno(r, "Failed to add component '%s' to set: %m", n);
×
1757
                TAKE_PTR(n);
1758
        }
1759

1760
        /* We use simple free() rather than strv_free() here, since set_free() will free the strings for us */
1761
        _cleanup_free_ char **z = set_get_strv(names);
144✔
1762
        if (!z)
72✔
1763
                return log_oom();
×
1764

1765
        strv_sort(z);
72✔
1766

1767
        if (!sd_json_format_enabled(arg_json_format_flags)) {
72✔
1768
                if (!has_default_component && set_isempty(names)) {
×
1769
                        log_info("No components defined.");
×
1770
                        return 0;
×
1771
                }
1772

1773
                if (has_default_component)
×
1774
                        printf("%s<default>%s\n",
×
1775
                               ansi_highlight(), ansi_normal());
1776

1777
                STRV_FOREACH(i, z)
×
1778
                        puts(*i);
×
1779
        } else {
1780
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
72✔
1781

1782
                r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_BOOLEAN("default", has_default_component),
72✔
1783
                                          SD_JSON_BUILD_PAIR_STRV("components", z));
1784
                if (r < 0)
72✔
1785
                        return log_error_errno(r, "Failed to create JSON: %m");
×
1786

1787
                r = sd_json_variant_dump(json, arg_json_format_flags, stdout, NULL);
72✔
1788
                if (r < 0)
72✔
1789
                        return log_error_errno(r, "Failed to print JSON: %m");
×
1790
        }
1791

1792
        return 0;
1793
}
1794

1795
static int verb_help(int argc, char **argv, void *userdata) {
×
1796
        _cleanup_free_ char *link = NULL;
×
1797
        int r;
×
1798

1799
        r = terminal_urlify_man("systemd-sysupdate", "8", &link);
×
1800
        if (r < 0)
×
1801
                return log_oom();
×
1802

1803
        printf("%1$s [OPTIONS...] [VERSION]\n"
×
1804
               "\n%5$sUpdate OS images.%6$s\n"
1805
               "\n%3$sCommands:%4$s\n"
1806
               "  list [VERSION]          Show installed and available versions\n"
1807
               "  features [FEATURE]      Show optional features\n"
1808
               "  check-new               Check if there's a new version available\n"
1809
               "  update [VERSION]        Install new version now\n"
1810
               "  acquire [VERSION]       Acquire (download) new version now\n"
1811
               "  vacuum                  Make room, by deleting old versions\n"
1812
               "  pending                 Report whether a newer version is installed than\n"
1813
               "                          currently booted\n"
1814
               "  reboot                  Reboot if a newer version is installed than booted\n"
1815
               "  components              Show list of components\n"
1816
               "  -h --help               Show this help\n"
1817
               "     --version            Show package version\n"
1818
               "\n%3$sOptions:%4$s\n"
1819
               "  -C --component=NAME     Select component to update\n"
1820
               "     --definitions=DIR    Find transfer definitions in specified directory\n"
1821
               "     --root=PATH          Operate on an alternate filesystem root\n"
1822
               "     --image=PATH         Operate on disk image as filesystem root\n"
1823
               "     --image-policy=POLICY\n"
1824
               "                          Specify disk image dissection policy\n"
1825
               "  -m --instances-max=INT  How many instances to maintain\n"
1826
               "     --sync=BOOL          Controls whether to sync data to disk\n"
1827
               "     --verify=BOOL        Force signature verification on or off\n"
1828
               "     --reboot             Reboot after updating to newer version\n"
1829
               "     --offline            Do not fetch metadata from the network\n"
1830
               "     --no-pager           Do not pipe output into a pager\n"
1831
               "     --no-legend          Do not show the headers and footers\n"
1832
               "     --json=pretty|short|off\n"
1833
               "                          Generate JSON output\n"
1834
               "     --transfer-source=PATH\n"
1835
               "                          Specify the directory to transfer sources from\n"
1836
               "\nSee the %2$s for details.\n",
1837
               program_invocation_short_name,
1838
               link,
1839
               ansi_underline(),
1840
               ansi_normal(),
1841
               ansi_highlight(),
1842
               ansi_normal());
1843

1844
        return 0;
1845
}
1846

1847
static int parse_argv(int argc, char *argv[]) {
552✔
1848

1849
        enum {
552✔
1850
                ARG_VERSION = 0x100,
1851
                ARG_NO_PAGER,
1852
                ARG_NO_LEGEND,
1853
                ARG_SYNC,
1854
                ARG_DEFINITIONS,
1855
                ARG_JSON,
1856
                ARG_ROOT,
1857
                ARG_IMAGE,
1858
                ARG_IMAGE_POLICY,
1859
                ARG_REBOOT,
1860
                ARG_VERIFY,
1861
                ARG_OFFLINE,
1862
                ARG_TRANSFER_SOURCE,
1863
        };
1864

1865
        static const struct option options[] = {
552✔
1866
                { "help",              no_argument,       NULL, 'h'                   },
1867
                { "version",           no_argument,       NULL, ARG_VERSION           },
1868
                { "no-pager",          no_argument,       NULL, ARG_NO_PAGER          },
1869
                { "no-legend",         no_argument,       NULL, ARG_NO_LEGEND         },
1870
                { "definitions",       required_argument, NULL, ARG_DEFINITIONS       },
1871
                { "instances-max",     required_argument, NULL, 'm'                   },
1872
                { "sync",              required_argument, NULL, ARG_SYNC              },
1873
                { "json",              required_argument, NULL, ARG_JSON              },
1874
                { "root",              required_argument, NULL, ARG_ROOT              },
1875
                { "image",             required_argument, NULL, ARG_IMAGE             },
1876
                { "image-policy",      required_argument, NULL, ARG_IMAGE_POLICY      },
1877
                { "reboot",            no_argument,       NULL, ARG_REBOOT            },
1878
                { "component",         required_argument, NULL, 'C'                   },
1879
                { "verify",            required_argument, NULL, ARG_VERIFY            },
1880
                { "offline",           no_argument,       NULL, ARG_OFFLINE           },
1881
                { "transfer-source",   required_argument, NULL, ARG_TRANSFER_SOURCE   },
1882
                {}
1883
        };
1884

1885
        int c, r;
552✔
1886

1887
        assert(argc >= 0);
552✔
1888
        assert(argv);
552✔
1889

1890
        while ((c = getopt_long(argc, argv, "hm:C:", options, NULL)) >= 0) {
1,268✔
1891

1892
                switch (c) {
716✔
1893

1894
                case 'h':
×
1895
                        return verb_help(0, NULL, NULL);
×
1896

1897
                case ARG_VERSION:
×
1898
                        return version();
×
1899

1900
                case ARG_NO_PAGER:
×
1901
                        arg_pager_flags |= PAGER_DISABLE;
×
1902
                        break;
×
1903

1904
                case ARG_NO_LEGEND:
×
1905
                        arg_legend = false;
×
1906
                        break;
×
1907

1908
                case 'm':
×
1909
                        r = safe_atou64(optarg, &arg_instances_max);
×
1910
                        if (r < 0)
×
1911
                                return log_error_errno(r, "Failed to parse --instances-max= parameter: %s", optarg);
×
1912

1913
                        break;
1914

1915
                case ARG_SYNC:
×
1916
                        r = parse_boolean_argument("--sync=", optarg, &arg_sync);
×
1917
                        if (r < 0)
×
1918
                                return r;
1919
                        break;
1920

1921
                case ARG_DEFINITIONS:
×
1922
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_definitions);
×
1923
                        if (r < 0)
×
1924
                                return r;
1925
                        break;
1926

1927
                case ARG_JSON:
220✔
1928
                        r = parse_json_argument(optarg, &arg_json_format_flags);
220✔
1929
                        if (r <= 0)
220✔
1930
                                return r;
1931

1932
                        break;
1933

1934
                case ARG_ROOT:
×
1935
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_root);
×
1936
                        if (r < 0)
×
1937
                                return r;
1938
                        break;
1939

1940
                case ARG_IMAGE:
×
1941
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_image);
×
1942
                        if (r < 0)
×
1943
                                return r;
1944
                        break;
1945

1946
                case ARG_IMAGE_POLICY:
×
1947
                        r = parse_image_policy_argument(optarg, &arg_image_policy);
×
1948
                        if (r < 0)
×
1949
                                return r;
1950
                        break;
1951

1952
                case ARG_REBOOT:
×
1953
                        arg_reboot = true;
×
1954
                        break;
×
1955

1956
                case 'C':
8✔
1957
                        if (isempty(optarg)) {
8✔
1958
                                arg_component = mfree(arg_component);
×
1959
                                break;
×
1960
                        }
1961

1962
                        r = component_name_valid(optarg);
8✔
1963
                        if (r < 0)
8✔
1964
                                return log_error_errno(r, "Failed to determine if component name is valid: %m");
×
1965
                        if (r == 0)
8✔
1966
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Component name invalid: %s", optarg);
×
1967

1968
                        r = free_and_strdup_warn(&arg_component, optarg);
8✔
1969
                        if (r < 0)
8✔
1970
                                return r;
1971

1972
                        break;
1973

1974
                case ARG_VERIFY: {
380✔
1975
                        bool b;
380✔
1976

1977
                        r = parse_boolean_argument("--verify=", optarg, &b);
380✔
1978
                        if (r < 0)
380✔
1979
                                return r;
×
1980

1981
                        arg_verify = b;
380✔
1982
                        break;
380✔
1983
                }
1984

1985
                case ARG_OFFLINE:
108✔
1986
                        arg_offline = true;
108✔
1987
                        break;
108✔
1988

1989
                case ARG_TRANSFER_SOURCE:
×
1990
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_transfer_source);
×
1991
                        if (r < 0)
×
1992
                                return r;
1993

1994
                        break;
1995

1996
                case '?':
1997
                        return -EINVAL;
1998

1999
                default:
×
2000
                        assert_not_reached();
×
2001
                }
2002
        }
2003

2004
        if (arg_image && arg_root)
552✔
2005
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Please specify either --root= or --image=, the combination of both is not supported.");
×
2006

2007
        if ((arg_image || arg_root) && arg_reboot)
552✔
2008
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "The --reboot switch may not be combined with --root= or --image=.");
×
2009

2010
        if (arg_definitions && arg_component)
552✔
2011
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "The --definitions= and --component= switches may not be combined.");
×
2012

2013
        return 1;
2014
}
2015

2016
static int sysupdate_main(int argc, char *argv[]) {
552✔
2017

2018
        static const Verb verbs[] = {
552✔
2019
                { "list",       VERB_ANY, 2, VERB_DEFAULT, verb_list              },
2020
                { "components", VERB_ANY, 1, 0,            verb_components        },
2021
                { "features",   VERB_ANY, 2, 0,            verb_features          },
2022
                { "check-new",  VERB_ANY, 1, 0,            verb_check_new         },
2023
                { "update",     VERB_ANY, 2, 0,            verb_update            },
2024
                { "acquire",    VERB_ANY, 2, 0,            verb_acquire           },
2025
                { "vacuum",     VERB_ANY, 1, 0,            verb_vacuum            },
2026
                { "reboot",     1,        1, 0,            verb_pending_or_reboot },
2027
                { "pending",    1,        1, 0,            verb_pending_or_reboot },
2028
                { "help",       VERB_ANY, 1, 0,            verb_help              },
2029
                {}
2030
        };
2031

2032
        return dispatch_verb(argc, argv, verbs, NULL);
552✔
2033
}
2034

2035
static int run(int argc, char *argv[]) {
552✔
2036
        int r;
552✔
2037

2038
        log_setup();
552✔
2039

2040
        r = parse_argv(argc, argv);
552✔
2041
        if (r <= 0)
552✔
2042
                return r;
2043

2044
        return sysupdate_main(argc, argv);
552✔
2045
}
2046

2047
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);
552✔
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