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

systemd / systemd / 20401947236

20 Dec 2025 09:56PM UTC coverage: 72.701% (+0.1%) from 72.578%
20401947236

push

github

DaanDeMeyer
core/socket: modernize listen/accept_in_cgroup

4 of 9 new or added lines in 1 file covered. (44.44%)

7723 existing lines in 114 files now uncovered.

309972 of 426363 relevant lines covered (72.7%)

1133403.64 hits per line

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

70.64
/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);
106✔
55
STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
106✔
56
STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
106✔
57
STATIC_DESTRUCTOR_REGISTER(arg_component, freep);
106✔
58
STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
106✔
59
STATIC_DESTRUCTOR_REGISTER(arg_transfer_source, freep);
106✔
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) {
96✔
85
        if (!c)
96✔
86
                return NULL;
87

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

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

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

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

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

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

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

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

UNCOV
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(
96✔
121
                Context *c,
122
                const char **dirs,
123
                const char *suffix,
124
                const char *node) {
125

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

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

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

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

143
        FOREACH_ARRAY(i, files, n_files) {
652✔
UNCOV
144
                _cleanup_(transfer_freep) Transfer *t = NULL;
×
145
                Transfer **appended;
556✔
146
                ConfFile *e = *i;
556✔
147

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

152
                r = transfer_read_definition(t, e->result, dirs, c->features);
556✔
153
                if (r < 0)
556✔
154
                        return r;
155

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

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

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

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

180
        assert(c);
96✔
181

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

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

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

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

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

206
        ConfFile **files = NULL;
96✔
207
        size_t n_files = 0;
96✔
208

209
        CLEANUP_ARRAY(files, n_files, conf_file_free_many);
96✔
210

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

217
        FOREACH_ARRAY(i, files, n_files) {
188✔
UNCOV
218
                _cleanup_(feature_unrefp) Feature *f = NULL;
×
219
                ConfFile *e = *i;
92✔
220

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

225
                r = feature_read_definition(f, e->result, (const char**) dirs);
92✔
226
                if (r < 0)
92✔
227
                        return r;
228

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

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

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

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

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

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

259
        return 0;
260
}
261

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

265
        assert(c);
96✔
266

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

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

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

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

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

291
        return 0;
292
}
293

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

297
        assert(c);
76✔
298

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

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

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

312
        return 0;
313
}
314

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

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

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

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

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

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

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

346
                                assert(i);
9,290✔
347

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

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

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

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

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

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

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

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

382
                        assert(t);
3,044✔
383

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

481
                newest_found = true;
362✔
482

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

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

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

497
        return 0;
498
}
499

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

503
        assert(c);
90✔
504

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

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

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

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

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

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

UNCOV
527
        assert(c);
×
528

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

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

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

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

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

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

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

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

569
        return NULL;
570
}
571

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

UNCOV
756
                        have_sha256 = true;
×
757

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

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

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

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

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

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

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

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

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

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

840
                return 0;
841
        }
842
}
843

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

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

852
        assert(c);
20✔
853

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

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

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

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

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

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

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

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

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

904
        return 0;
905
}
906

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

911
        assert(ret);
96✔
912

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

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

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

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

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

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

936
        assert(ret);
90✔
937

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

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

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

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

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

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

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

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

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

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

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

991
        assert(c);
18✔
992

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

UNCOV
1001
                        if (ret_applied)
×
1002
                                *ret_applied = NULL;
×
1003

UNCOV
1004
                        return 0;
×
1005
                }
1006

1007
                us = c->candidate;
1008
        }
1009

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

UNCOV
1015
                if (ret_applied)
×
1016
                        *ret_applied = NULL;
×
1017

UNCOV
1018
                return 0;
×
1019
        }
1020

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1096
        if (ret_applied)
18✔
1097
                *ret_applied = us;
18✔
1098

1099
        return 1;
1100
}
1101

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

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

1111
        assert(ret_mounted_dir);
106✔
1112
        assert(ret_loop_device);
106✔
1113

1114
        if (!arg_image)
106✔
1115
                return 0;
1116

UNCOV
1117
        assert(!arg_root);
×
1118

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

UNCOV
1137
        arg_root = strdup(mounted_dir);
×
1138
        if (!arg_root)
×
1139
                return log_oom();
×
1140

UNCOV
1141
        *ret_mounted_dir = TAKE_PTR(mounted_dir);
×
1142
        *ret_loop_device = TAKE_PTR(loop_device);
×
1143

UNCOV
1144
        return 0;
×
1145
}
1146

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

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

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

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

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

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

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

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

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

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

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

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

1208
                return 0;
1209
        }
1210
}
1211

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

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

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

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

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

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

1241
                table = table_new_vertical();
2✔
1242
                if (!table)
2✔
UNCOV
1243
                        return log_oom();
×
1244

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

UNCOV
1343
        return 0;
×
1344
}
1345

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

1352
        assert(argc <= 1);
44✔
1353

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

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

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

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

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

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

1384
        return EXIT_SUCCESS;
1385
}
1386

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

1393
        assert(argc <= 1);
2✔
1394

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

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

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

1407
        return context_vacuum(context, 0, NULL);
2✔
1408
}
1409

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

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

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

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

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

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

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

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

1448
        if (r > 0 && arg_reboot) {
18✔
UNCOV
1449
                assert(applied);
×
1450
                assert(booted_version);
×
1451

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

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

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

1466
        return 0;
1467
}
1468

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

UNCOV
1474
        assert(argc == 1);
×
1475

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

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

UNCOV
1484
        log_info("Determining installed update sets%s", glyph(GLYPH_ELLIPSIS));
×
1485

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

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

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

UNCOV
1505
                if (streq(argv[0], "reboot"))
×
1506
                        return reboot_now();
×
1507

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

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

1519
        return EXIT_SUCCESS;
1520
}
1521

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

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

1527
        if (isempty(c))
24✔
1528
                return false;
1529

1530
        if (string_has_cc(c, NULL))
12✔
1531
                return false;
1532

1533
        if (!utf8_is_valid(c))
12✔
1534
                return false;
1535

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

1540
        return filename_is_valid(j);
12✔
1541
}
1542

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

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

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

1556
        ConfFile **directories = NULL;
10✔
1557
        size_t n_directories = 0;
10✔
1558

1559
        CLEANUP_ARRAY(directories, n_directories, conf_file_free_many);
10✔
1560

1561
        r = conf_files_list_strv_full(".d", arg_root, CONF_FILES_DIRECTORY, (const char * const *) CONF_PATHS_STRV(""), &directories, &n_directories);
10✔
1562
        if (r < 0)
10✔
UNCOV
1563
                return log_error_errno(r, "Failed to enumerate directories: %m");
×
1564

1565
        FOREACH_ARRAY(i, directories, n_directories) {
278✔
1566
                ConfFile *e = *i;
268✔
1567

1568
                if (streq(e->name, "sysupdate.d")) {
268✔
1569
                        has_default_component = true;
10✔
1570
                        continue;
10✔
1571
                }
1572

1573
                const char *s = startswith(e->name, "sysupdate.");
258✔
1574
                if (!s)
258✔
1575
                        continue;
250✔
1576

1577
                const char *a = endswith(s, ".d");
8✔
1578
                if (!a)
8✔
UNCOV
1579
                        continue;
×
1580

UNCOV
1581
                _cleanup_free_ char *n = strndup(s, a - s);
×
1582
                if (!n)
8✔
UNCOV
1583
                        return log_oom();
×
1584

1585
                r = component_name_valid(n);
8✔
1586
                if (r < 0)
8✔
UNCOV
1587
                        return log_error_errno(r, "Unable to validate component name '%s': %m", n);
×
1588
                if (r == 0)
8✔
UNCOV
1589
                        continue;
×
1590

1591
                r = set_ensure_put(&names, &string_hash_ops_free, n);
8✔
1592
                if (r < 0 && r != -EEXIST)
8✔
UNCOV
1593
                        return log_error_errno(r, "Failed to add component '%s' to set: %m", n);
×
1594
                TAKE_PTR(n);
1595
        }
1596

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

1602
        strv_sort(z);
10✔
1603

1604
        if (!sd_json_format_enabled(arg_json_format_flags)) {
10✔
UNCOV
1605
                if (!has_default_component && set_isempty(names)) {
×
1606
                        log_info("No components defined.");
×
1607
                        return 0;
×
1608
                }
1609

UNCOV
1610
                if (has_default_component)
×
1611
                        printf("%s<default>%s\n",
×
1612
                               ansi_highlight(), ansi_normal());
1613

UNCOV
1614
                STRV_FOREACH(i, z)
×
1615
                        puts(*i);
×
1616
        } else {
1617
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
10✔
1618

1619
                r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_BOOLEAN("default", has_default_component),
10✔
1620
                                          SD_JSON_BUILD_PAIR_STRV("components", z));
1621
                if (r < 0)
10✔
UNCOV
1622
                        return log_error_errno(r, "Failed to create JSON: %m");
×
1623

1624
                r = sd_json_variant_dump(json, arg_json_format_flags, stdout, NULL);
10✔
1625
                if (r < 0)
10✔
UNCOV
1626
                        return log_error_errno(r, "Failed to print JSON: %m");
×
1627
        }
1628

1629
        return 0;
1630
}
1631

UNCOV
1632
static int verb_help(int argc, char **argv, void *userdata) {
×
1633
        _cleanup_free_ char *link = NULL;
×
1634
        int r;
×
1635

UNCOV
1636
        r = terminal_urlify_man("systemd-sysupdate", "8", &link);
×
1637
        if (r < 0)
×
1638
                return log_oom();
×
1639

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

1680
        return 0;
1681
}
1682

1683
static int parse_argv(int argc, char *argv[]) {
106✔
1684

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

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

1721
        int c, r;
106✔
1722

1723
        assert(argc >= 0);
106✔
1724
        assert(argv);
106✔
1725

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

1728
                switch (c) {
128✔
1729

UNCOV
1730
                case 'h':
×
1731
                        return verb_help(0, NULL, NULL);
×
1732

UNCOV
1733
                case ARG_VERSION:
×
1734
                        return version();
×
1735

UNCOV
1736
                case ARG_NO_PAGER:
×
1737
                        arg_pager_flags |= PAGER_DISABLE;
×
1738
                        break;
×
1739

UNCOV
1740
                case ARG_NO_LEGEND:
×
1741
                        arg_legend = false;
×
1742
                        break;
×
1743

UNCOV
1744
                case 'm':
×
1745
                        r = safe_atou64(optarg, &arg_instances_max);
×
1746
                        if (r < 0)
×
1747
                                return log_error_errno(r, "Failed to parse --instances-max= parameter: %s", optarg);
×
1748

1749
                        break;
1750

UNCOV
1751
                case ARG_SYNC:
×
1752
                        r = parse_boolean_argument("--sync=", optarg, &arg_sync);
×
1753
                        if (r < 0)
×
1754
                                return r;
1755
                        break;
1756

UNCOV
1757
                case ARG_DEFINITIONS:
×
1758
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_definitions);
×
1759
                        if (r < 0)
×
1760
                                return r;
1761
                        break;
1762

1763
                case ARG_JSON:
34✔
1764
                        r = parse_json_argument(optarg, &arg_json_format_flags);
34✔
1765
                        if (r <= 0)
34✔
1766
                                return r;
1767

1768
                        break;
1769

UNCOV
1770
                case ARG_ROOT:
×
1771
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_root);
×
1772
                        if (r < 0)
×
1773
                                return r;
1774
                        break;
1775

UNCOV
1776
                case ARG_IMAGE:
×
1777
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_image);
×
1778
                        if (r < 0)
×
1779
                                return r;
1780
                        break;
1781

UNCOV
1782
                case ARG_IMAGE_POLICY:
×
1783
                        r = parse_image_policy_argument(optarg, &arg_image_policy);
×
1784
                        if (r < 0)
×
1785
                                return r;
1786
                        break;
1787

UNCOV
1788
                case ARG_REBOOT:
×
1789
                        arg_reboot = true;
×
1790
                        break;
×
1791

1792
                case 'C':
4✔
1793
                        if (isempty(optarg)) {
4✔
UNCOV
1794
                                arg_component = mfree(arg_component);
×
1795
                                break;
×
1796
                        }
1797

1798
                        r = component_name_valid(optarg);
4✔
1799
                        if (r < 0)
4✔
UNCOV
1800
                                return log_error_errno(r, "Failed to determine if component name is valid: %m");
×
1801
                        if (r == 0)
4✔
UNCOV
1802
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Component name invalid: %s", optarg);
×
1803

1804
                        r = free_and_strdup_warn(&arg_component, optarg);
4✔
1805
                        if (r < 0)
4✔
1806
                                return r;
1807

1808
                        break;
1809

1810
                case ARG_VERIFY: {
76✔
1811
                        bool b;
76✔
1812

1813
                        r = parse_boolean_argument("--verify=", optarg, &b);
76✔
1814
                        if (r < 0)
76✔
UNCOV
1815
                                return r;
×
1816

1817
                        arg_verify = b;
76✔
1818
                        break;
76✔
1819
                }
1820

1821
                case ARG_OFFLINE:
14✔
1822
                        arg_offline = true;
14✔
1823
                        break;
14✔
1824

UNCOV
1825
                case ARG_TRANSFER_SOURCE:
×
1826
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_transfer_source);
×
1827
                        if (r < 0)
×
1828
                                return r;
1829

1830
                        break;
1831

1832
                case '?':
1833
                        return -EINVAL;
1834

UNCOV
1835
                default:
×
1836
                        assert_not_reached();
×
1837
                }
1838
        }
1839

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

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

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

1849
        return 1;
1850
}
1851

1852
static int sysupdate_main(int argc, char *argv[]) {
106✔
1853

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

1867
        return dispatch_verb(argc, argv, verbs, NULL);
106✔
1868
}
1869

1870
static int run(int argc, char *argv[]) {
106✔
1871
        int r;
106✔
1872

1873
        log_setup();
106✔
1874

1875
        r = parse_argv(argc, argv);
106✔
1876
        if (r <= 0)
106✔
1877
                return r;
1878

1879
        return sysupdate_main(argc, argv);
106✔
1880
}
1881

1882
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);
106✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc