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

systemd / systemd / 21084785420

16 Jan 2026 11:47PM UTC coverage: 72.748% (+0.2%) from 72.522%
21084785420

push

github

yuwata
socket: turn of loud logging when setting up sockopts in container fails due to privs

Various socktops will fail if we run in a container, due to lack of
privs (for example SO_RECVFORCE as used by the journald sockets). That's
typically not a big issue. Hence downgrade the log level.

Follow-up for: f7df0eab8

310553 of 426888 relevant lines covered (72.75%)

1128365.0 hits per line

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

70.67
/src/sysupdate/sysupdate.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <getopt.h>
4
#include <unistd.h>
5

6
#include "sd-daemon.h"
7

8
#include "build.h"
9
#include "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

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

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

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

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

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

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

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

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

182
        assert(c);
96✔
183

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

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

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

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

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

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

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

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

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

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

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

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

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

241
        if (c->n_transfers + c->n_disabled_transfers == 0) {
96✔
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) {
96✔
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) {
96✔
265
        int r;
96✔
266

267
        assert(c);
96✔
268

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

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

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

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

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

293
        return 0;
294
}
295

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

299
        assert(c);
76✔
300

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

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

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

314
        return 0;
315
}
316

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

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

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

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

336
                        assert(*tr);
3,570✔
337

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

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

348
                                assert(i);
9,290✔
349

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

353
                                if (cursor && strverscmp(i->metadata.version, cursor) <= 0)
2,744✔
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)
614✔
359
                                        return log_oom();
×
360

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

364
                        if (flags == UPDATE_AVAILABLE && !cursor)
3,570✔
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. */
780✔
373
                        break;
374

375
                cursor_instances = new0(Instance*, c->n_transfers);
614✔
376
                if (!cursor_instances)
614✔
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++) {
3,602✔
381
                        Transfer *t = c->transfers[k];
3,044✔
382
                        Instance *match = NULL;
3,044✔
383

384
                        assert(t);
3,044✔
385

386
                        if (flags == UPDATE_AVAILABLE) {
3,044✔
387
                                match = resource_find_instance(&t->source, cursor);
1,866✔
388
                                if (!match) {
1,866✔
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);
1,178✔
398

399
                                match = resource_find_instance(&t->target, cursor);
1,178✔
400
                                if (!match) {
1,178✔
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;
312✔
406
                                }
407
                        }
408

409
                        cursor_instances[k] = match;
2,988✔
410

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

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

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

422
                if (skip)
614✔
423
                        continue;
56✔
424

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

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

432
                        /* Merge in what we've learned and continue onto the next version */
433

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

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

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

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

449
                        u->flags |= flags | extra_flags;
196✔
450

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

458
                        skip = true;
196✔
459
                        newest_found = true;
196✔
460
                        break;
196✔
461
                }
462

463
                if (skip)
558✔
464
                        continue;
196✔
465

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

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

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

481
                c->update_sets[c->n_update_sets++] = us;
362✔
482

483
                newest_found = true;
362✔
484

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

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

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

499
        return 0;
500
}
501

502
static int context_discover_update_sets(Context *c) {
90✔
503
        int r;
90✔
504

505
        assert(c);
90✔
506

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

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

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

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

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

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

529
        assert(c);
×
530

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

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

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

543
                color = update_set_flags_to_color(us->flags);
×
544

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

560
        return table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, arg_legend);
×
561
}
562

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

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

571
        return NULL;
572
}
573

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

585
        assert(c);
22✔
586
        assert(version);
22✔
587

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

758
                        have_sha256 = true;
×
759

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

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

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

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

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

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

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

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

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

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

842
                return 0;
843
        }
844
}
845

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

851
        size_t disabled_count = 0;
20✔
852
        int r, count = 0;
20✔
853

854
        assert(c);
20✔
855

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

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

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

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

872
                count = MAX(count, r);
84✔
873
        }
874

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

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

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

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

906
        return 0;
907
}
908

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

913
        assert(ret);
96✔
914

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

918
        context = context_new();
96✔
919
        if (!context)
96✔
920
                return log_oom();
×
921

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

926
        r = context_load_installed_instances(context);
96✔
927
        if (r < 0)
96✔
928
                return r;
929

930
        *ret = TAKE_PTR(context);
96✔
931
        return 0;
96✔
932
}
933

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

938
        assert(ret);
90✔
939

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

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

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

953
        r = context_discover_update_sets(context);
90✔
954
        if (r < 0)
90✔
955
                return r;
956

957
        *ret = TAKE_PTR(context);
90✔
958
        return 0;
90✔
959
}
960

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

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

972
        base = (100 * 100 * i) / n;
62✔
973
        scaled = (100 * percentage) / n;
62✔
974
        overall = (unsigned) ((base + scaled) / 100);
62✔
975
        assert(overall <= 100);
62✔
976

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

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

990
        UpdateSet *us = NULL;
18✔
991
        int r;
18✔
992

993
        assert(c);
18✔
994

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

1003
                        if (ret_applied)
×
1004
                                *ret_applied = NULL;
×
1005

1006
                        return 0;
×
1007
                }
1008

1009
                us = c->candidate;
1010
        }
1011

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

1017
                if (ret_applied)
×
1018
                        *ret_applied = NULL;
×
1019

1020
                return 0;
×
1021
        }
1022

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

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

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

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

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

1050
        if (arg_sync)
18✔
1051
                sync();
18✔
1052

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

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

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

1063
                assert(inst); /* ditto */
92✔
1064

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

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

1075
        if (arg_sync)
18✔
1076
                sync();
18✔
1077

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

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

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

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

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

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

1098
        if (ret_applied)
18✔
1099
                *ret_applied = us;
18✔
1100

1101
        return 1;
1102
}
1103

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

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

1113
        assert(ret_mounted_dir);
106✔
1114
        assert(ret_loop_device);
106✔
1115

1116
        if (!arg_image)
106✔
1117
                return 0;
1118

1119
        assert(!arg_root);
×
1120

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

1139
        arg_root = strdup(mounted_dir);
×
1140
        if (!arg_root)
×
1141
                return log_oom();
×
1142

1143
        *ret_mounted_dir = TAKE_PTR(mounted_dir);
×
1144
        *ret_loop_device = TAKE_PTR(loop_device);
×
1145

1146
        return 0;
×
1147
}
1148

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

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

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

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

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

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

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

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

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

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

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

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

1210
                return 0;
1211
        }
1212
}
1213

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

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

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

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

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

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

1243
                table = table_new_vertical();
2✔
1244
                if (!table)
2✔
1245
                        return log_oom();
×
1246

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1336
                r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_STRV("features", features));
×
1337
                if (r < 0)
×
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);
×
1341
                if (r < 0)
×
1342
                        return log_error_errno(r, "Failed to print JSON: %m");
×
1343
        }
1344

1345
        return 0;
×
1346
}
1347

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

1354
        assert(argc <= 1);
44✔
1355

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

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

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

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

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

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

1386
        return EXIT_SUCCESS;
1387
}
1388

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

1395
        assert(argc <= 1);
2✔
1396

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

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

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

1409
        return context_vacuum(context, 0, NULL);
2✔
1410
}
1411

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

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

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

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

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

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

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

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

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

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

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

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

1468
        return 0;
1469
}
1470

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

1476
        assert(argc == 1);
×
1477

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

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

1486
        log_info("Determining installed update sets%s", glyph(GLYPH_ELLIPSIS));
×
1487

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

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

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

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

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

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

1521
        return EXIT_SUCCESS;
1522
}
1523

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

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

1529
        if (isempty(c))
24✔
1530
                return false;
1531

1532
        if (string_has_cc(c, NULL))
12✔
1533
                return false;
1534

1535
        if (!utf8_is_valid(c))
12✔
1536
                return false;
1537

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

1542
        return filename_is_valid(j);
12✔
1543
}
1544

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

1552
        assert(argc <= 1);
10✔
1553

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

1558
        ConfFile **directories = NULL;
10✔
1559
        size_t n_directories = 0;
10✔
1560

1561
        CLEANUP_ARRAY(directories, n_directories, conf_file_free_many);
10✔
1562

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

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

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

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

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

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

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

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

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

1605
        strv_sort(z);
10✔
1606

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

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

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

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

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

1632
        return 0;
1633
}
1634

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

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

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

1683
        return 0;
1684
}
1685

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

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

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

1724
        int c, r;
106✔
1725

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

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

1731
                switch (c) {
128✔
1732

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

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

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

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

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

1752
                        break;
1753

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

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

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

1771
                        break;
1772

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

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

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

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

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

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

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

1811
                        break;
1812

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

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

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

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

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

1833
                        break;
1834

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

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

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

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

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

1852
        return 1;
1853
}
1854

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

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

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

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

1876
        log_setup();
106✔
1877

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

1882
        return sysupdate_main(argc, argv);
106✔
1883
}
1884

1885
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