HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/sysupdate/sysupdate.c
Git blob: 53c690b9bc5a42764e46b95cc8a991b63a15dadc
Lines 2001–3000 of 3,595
_cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
_cleanup_strv_free_ char **versions = NULL;
const char *current = NULL;
bool current_is_pending = false;
FOREACH_ARRAY(update_set, context.update_sets, context.n_update_sets) {
UpdateSet *us = *update_set;
if (FLAGS_SET(us->flags, UPDATE_INSTALLED) &&
FLAGS_SET(us->flags, UPDATE_NEWEST)) {
current = us->version;
current_is_pending = FLAGS_SET(us->flags, UPDATE_PENDING);
}
r = strv_extend(&versions, us->version);
if (r < 0)
return log_oom();
}
FOREACH_ARRAY(tr, context.transfers, context.n_transfers)
STRV_FOREACH(appstream_url, (*tr)->appstream) {
/* Avoid duplicates */
if (strv_contains(appstream_urls, *appstream_url))
continue;
r = strv_extend(&appstream_urls, *appstream_url);
if (r < 0)
return log_oom();
}
r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_STRING(current_is_pending ? "current+pending" : "current", current),
SD_JSON_BUILD_PAIR_STRV("all", versions),
SD_JSON_BUILD_PAIR_STRV("appstreamUrls", appstream_urls));
if (r < 0)
return log_error_errno(r, "Failed to create JSON: %m");
r = sd_json_variant_dump(json, arg_json_format_flags, stdout, NULL);
if (r < 0)
return log_error_errno(r, "Failed to print JSON: %m");
return 0;
}
}
static int feature_to_json(Context *context, const Feature *f, sd_json_variant **ret);
VERB(verb_features, "features", "[FEATURE]\0", VERB_ANY, 2, 0,
"Show optional features");
static int verb_features(int argc, char *argv[], uintptr_t _data, void *userdata) {
_cleanup_(context_done) Context context = CONTEXT_NULL;
_cleanup_(table_unrefp) Table *table = NULL;
const char *feature_id;
Feature *f;
int r;
assert(argc <= 2);
feature_id = argc >= 2 ? argv[1] : NULL;
r = context_from_cmdline(&context);
if (r < 0)
return r;
if (context.feature_select != SELECT_EXPLICIT)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--feature-all/--feature-suggested is not supported for '%s'.", argv[0]);
if (context.component_select != SELECT_EXPLICIT)
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "--component-all/--component-suggested currently not supported for '%s'.", argv[0]);
r = context_load_offline(
&context,
PROCESS_IMAGE_READ_ONLY,
READ_DEFINITIONS_REQUIRES_ANY_TRANSFERS);
if (r < 0)
return r;
if (feature_id) {
_cleanup_strv_free_ char **transfers = NULL;
f = hashmap_get(context.features, feature_id);
if (!f)
return log_error_errno(SYNTHETIC_ERRNO(ENOENT),
"Optional feature not found: %s",
feature_id);
if (sd_json_format_enabled(arg_json_format_flags)) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
r = feature_to_json(&context, f, &json);
if (r < 0)
return r;
r = sd_json_variant_dump(json, arg_json_format_flags, stdout, NULL);
if (r < 0)
return log_error_errno(r, "Failed to print JSON: %m");
return 0;
}
table = table_new_vertical();
if (!table)
return log_oom();
r = context_get_transfers_for_feature(&context, f->id, &transfers);
if (r < 0)
return r;
r = table_add_many(table,
TABLE_FIELD, "Name",
TABLE_STRING, f->id,
TABLE_FIELD, "Enabled",
TABLE_BOOLEAN, f->enabled);
if (r < 0)
return table_log_add_error(r);
r = feature_is_suggested(f);
if (r < 0) {
errno = -r; /* Let's make %m below show this error */
_cleanup_free_ char *k = asprintf_safe("error (%m)");
r = table_add_many(table,
TABLE_FIELD, "Suggested",
TABLE_STRING, k);
} else
r = table_add_many(table,
TABLE_FIELD, "Suggested",
TABLE_BOOLEAN, r);
if (r < 0)
return table_log_add_error(r);
if (f->description) {
r = table_add_many(table, TABLE_FIELD, "Description", TABLE_STRING, f->description);
if (r < 0)
return table_log_add_error(r);
}
if (!strv_isempty(f->documentation)) {
r = table_add_many(table,
TABLE_FIELD, "Documentation",
TABLE_STRV_WRAPPED, f->documentation);
if (r < 0)
return table_log_add_error(r);
}
if (f->appstream) {
r = table_add_many(table,
TABLE_FIELD, "AppStream",
TABLE_STRING, f->appstream,
TABLE_SET_URL, f->appstream);
if (r < 0)
return table_log_add_error(r);
}
if (!strv_isempty(transfers)) {
r = table_add_many(table, TABLE_FIELD, "Transfers", TABLE_STRV_WRAPPED, transfers);
if (r < 0)
return table_log_add_error(r);
}
return table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
} else if (FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) {
table = table_new("enabled", "suggested", "feature", "description", "documentation");
if (!table)
return log_oom();
table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
HASHMAP_FOREACH(f, context.features) {
r = table_add_many(table,
TABLE_BOOLEAN_CHECKMARK, f->enabled,
TABLE_SET_COLOR, ansi_highlight_green_red(f->enabled));
if (r < 0)
return table_log_add_error(r);
r = feature_is_suggested(f);
if (r < 0)
r = table_add_many(table, TABLE_EMPTY);
else
r = table_add_many(table, TABLE_BOOLEAN_CHECKMARK, r);
if (r < 0)
return table_log_add_error(r);
r = table_add_many(table,
TABLE_STRING, f->id,
TABLE_STRING, f->description,
TABLE_STRV_WRAPPED, f->documentation);
if (r < 0)
return table_log_add_error(r);
}
r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
if (r < 0)
return r;
if (arg_legend) {
if (table_isempty(table))
log_info("No features.");
else
printf("\n%zu features listed.\n", table_get_rows(table) - 1);
}
} else {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
_cleanup_strv_free_ char **features = NULL;
HASHMAP_FOREACH(f, context.features) {
r = strv_extend(&features, f->id);
if (r < 0)
return log_oom();
}
r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_STRV("features", features));
if (r < 0)
return log_error_errno(r, "Failed to create JSON: %m");
r = sd_json_variant_dump(json, arg_json_format_flags, stdout, NULL);
if (r < 0)
return log_error_errno(r, "Failed to print JSON: %m");
}
return 0;
}
static int make_dropin_dir(Context *c, char **ret) {
_cleanup_free_ char *dir = NULL;
assert(c);
assert(ret);
/* Returns the (writable) directory where feature definitions live, so that we can drop our
* 'Enabled=' override right next to them. This mirrors the directory logic in
* context_read_definitions(), but settles on a single writable location below /etc. */
if (c->definitions)
dir = strdup(c->definitions); /* --root= is not supported for this for now */
else if (c->component) {
_cleanup_free_ char *n = strjoin("sysupdate.", c->component, ".d");
if (!n)
return log_oom();
dir = path_join(c->root, SYSCONF_DIR, n);
} else
dir = path_join(c->root, SYSCONF_DIR "/sysupdate.d");
if (!dir)
return log_oom();
*ret = TAKE_PTR(dir);
return 0;
}
static const char *context_component_display(const Context *c) {
assert(c);
return c->component ?: "<default>";
}
static int context_enable_feature(
Context *c,
const char *argv0,
bool enable,
char **features) {
int r;
assert(c);
assert(argv0);
_cleanup_free_ char **l = NULL;
switch (c->feature_select) {
case SELECT_EXPLICIT:
break;
case SELECT_ALL: {
assert(!features);
Feature *f;
HASHMAP_FOREACH(f, c->features)
if (strv_push(&l, f->id) < 0)
return log_oom();
features = l;
break;
}
case SELECT_SUGGESTED: {
assert(!features);
Feature *f;
HASHMAP_FOREACH(f, c->features) {
r = feature_is_suggested(f);
if (r < 0) {
log_warning_errno(r, "Failed to determine if feature '%s' of component '%s' is suggested, skipping: %m", f->id, context_component_display(c));
continue;
}
if (r == 0) {
log_debug("Feature '%s' of component '%s' is not suggested, skipping.", f->id, context_component_display(c));
continue;
}
log_info("%s feature '%s' of component '%s'.", enable ? "Enabling" : "Disabling", f->id, context_component_display(c));
if (strv_push(&l, f->id) < 0)
return log_oom();
}
features = l;
break;
}
default:
assert_not_reached();
}
if (strv_isempty(features)) {
log_debug("No features selected.");
return 0;
}
_cleanup_free_ char *dropin_dir = NULL;
r = make_dropin_dir(c, &dropin_dir);
if (r < 0)
return r;
int ret = 0;
STRV_FOREACH(name, features) {
if (!hashmap_contains(c->features, *name)) {
RET_GATHER(ret, log_error_errno(SYNTHETIC_ERRNO(ENOENT), "Optional feature not found in component '%s': %s", context_component_display(c), *name));
continue;
}
_cleanup_free_ char *fname = strjoin(*name, ".feature");
if (!fname)
return log_oom();
/* We assume that no sysadmin will name their config 50-systemd-sysupdate-enabled.conf */
r = write_drop_in_format(
dropin_dir,
fname,
50,
"systemd-sysupdate-enabled",
"# Generated via 'systemd-sysupdate %s'\n\n"
"[Feature]\n"
"Enabled=%s\n",
argv0,
yes_no(enable));
if (r < 0) {
RET_GATHER(ret, log_error_errno(r, "Failed to write drop-in for feature '%s': %m", *name));
continue;
}
log_info("Feature '%s' %s.", *name, enabled_disabled(enable));
}
return ret;
}
VERB(verb_enable_feature, "enable-feature", "FEATURE…\0", 1, VERB_ANY, 0,
"Enable optional feature");
VERB(verb_enable_feature, "disable-feature", "FEATURE…\0", 1, VERB_ANY, 0,
"Disable optional feature");
static int verb_enable_feature(int argc, char *argv[], uintptr_t _data, void *userdata) {
bool enable = streq(argv[0], "enable-feature");
int r;
_cleanup_(context_done) Context context = CONTEXT_NULL;
r = context_from_cmdline(&context);
if (r < 0)
return r;
if (!IN_SET(context.component_select, SELECT_EXPLICIT, SELECT_ALL))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--component-suggested is not supported for '%s'.", argv[0]);
if (context.definitions)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"The --definitions= switch may not be combined with '%s'.", argv[0]);
char **features;
if (argc > 1 && context.feature_select == SELECT_EXPLICIT) {
features = strv_skip(argv, 1);
STRV_FOREACH(name, features)
if (!feature_name_valid(*name))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Feature name invalid: %s", *name);
} else if (argc <= 1 && context.feature_select != SELECT_EXPLICIT)
features = NULL;
else
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Either specify features as positional parameter or via --feature-all/--feature-suggested, not both.");
r = context_load_offline(
&context,
/* process_image_flags= */ 0,
/* read_definitions_flags= */ 0);
if (r < 0)
return r;
switch (context.component_select) {
case SELECT_EXPLICIT:
return context_enable_feature(&context, argv[0], enable, features);
case SELECT_ALL: {
_cleanup_strv_free_ char **component_names = NULL;
bool has_default_component;
r = context_list_components(&context, &component_names, &has_default_component);
if (r < 0)
return r;
if (strv_isempty(component_names) && !has_default_component) {
log_debug("No components selected.");
return 0;
}
int ret = 0;
if (has_default_component)
RET_GATHER(ret, context_enable_feature(&context, argv[0], enable, features));
STRV_FOREACH(name, component_names) {
_cleanup_(context_done) Context cc = CONTEXT_NULL;
r = context_from_base_with_component(&context, *name, &cc);
if (r < 0) {
RET_GATHER(ret, r);
continue;
}
r = context_load_offline(
&cc,
/* process_image_flags= */ 0,
/* read_definitions_flags= */ 0);
if (r < 0) {
RET_GATHER(ret, r);
continue;
}
RET_GATHER(ret, context_enable_feature(&cc, argv[0], enable, features));
}
return ret;
}
default:
assert_not_reached();
}
}
static int feature_to_json(Context *context, const Feature *f, sd_json_variant **ret) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
_cleanup_strv_free_ char **transfers = NULL;
int suggested;
int r;
assert(context);
assert(f);
assert(ret);
r = context_get_transfers_for_feature(context, f->id, &transfers);
if (r < 0)
return r;
suggested = feature_is_suggested(f);
if (suggested < 0)
log_debug_errno(suggested,
"Failed to determine whether feature '%s' is suggested, omitting field: %m",
f->id);
r = sd_json_variant_merge_objectbo(
&v,
SD_JSON_BUILD_PAIR_STRING("id", f->id),
JSON_BUILD_PAIR_STRING_NON_EMPTY("description", f->description),
JSON_BUILD_PAIR_STRV_NON_EMPTY("documentation", f->documentation),
JSON_BUILD_PAIR_STRING_NON_EMPTY("appStream", f->appstream),
SD_JSON_BUILD_PAIR_BOOLEAN("enabled", f->enabled),
SD_JSON_BUILD_PAIR_CONDITION(suggested >= 0, "suggested", SD_JSON_BUILD_BOOLEAN(suggested > 0)),
JSON_BUILD_PAIR_STRV_NON_EMPTY("transfers", transfers));
if (r < 0)
return log_oom();
*ret = TAKE_PTR(v);
return 1;
}
static int vl_method_list_features(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) {
_cleanup_(context_done) Context context = CONTEXT_NULL;
Feature *f;
int r;
assert(link);
static const sd_json_dispatch_field dispatch_table[] = {
{ "target", SD_JSON_VARIANT_OBJECT, dispatch_target_identifier, voffsetof(context, target_identifier), SD_JSON_MANDATORY },
{},
};
r = sd_varlink_dispatch(link, parameters, dispatch_table, &context);
if (r != 0)
return r;
/* Listing features doesn’t require a polkit check */
if (getenv_bool("SYSTEMD_SYSUPDATE_NO_VERIFY") > 0)
context.verify = 0;
/* ListFeatures is always offline */
context.offline = true;
r = context_load_offline_from_target(
&context,
PROCESS_IMAGE_READ_ONLY,
READ_DEFINITIONS_REQUIRES_ANY_TRANSFERS);
if (r == -ENOENT)
return sd_varlink_error(link, "io.systemd.SysUpdate.NoSuchTarget", NULL);
if (r < 0)
return r;
_cleanup_(sd_json_variant_unrefp) sd_json_variant *l = NULL;
HASHMAP_FOREACH(f, context.features) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
r = feature_to_json(&context, f, &v);
if (r < 0)
return r;
r = sd_json_variant_append_array(&l, v);
if (r < 0)
return r;
}
if (!l) {
r = sd_json_variant_new_array(&l, NULL, 0);
if (r < 0)
return r;
}
return sd_varlink_replybo(link,
SD_JSON_BUILD_PAIR_VARIANT("features", l));
}
VERB_NOARG(verb_check_new, "check-new",
"Check if there's a new version available");
static int verb_check_new(int argc, char *argv[], uintptr_t _data, void *userdata) {
int r;
assert(argc <= 1);
_cleanup_(context_done) Context context = CONTEXT_NULL;
r = context_from_cmdline(&context);
if (r < 0)
return r;
if (context.feature_select != SELECT_EXPLICIT)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--feature-all/--feature-suggested is not supported for '%s'.", argv[0]);
if (context.component_select != SELECT_EXPLICIT)
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "--component-all/--component-suggested currently not supported for '%s'.", argv[0]);
r = context_load_online(
&context,
PROCESS_IMAGE_READ_ONLY,
READ_DEFINITIONS_REQUIRES_ENABLED_TRANSFERS|
READ_DEFINITIONS_REQUIRES_ANY_TRANSFERS);
if (r < 0)
return r;
if (!sd_json_format_enabled(arg_json_format_flags)) {
if (!context.candidate) {
log_debug("No candidate found.");
return EXIT_FAILURE;
}
puts(context.candidate->version);
} else {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
if (context.candidate)
r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_STRING("available", context.candidate->version));
else
r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_NULL("available"));
if (r < 0)
return log_error_errno(r, "Failed to create JSON: %m");
r = sd_json_variant_dump(json, arg_json_format_flags, stdout, NULL);
if (r < 0)
return log_error_errno(r, "Failed to print JSON: %m");
if (!context.candidate)
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
static int vl_method_check_new(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) {
_cleanup_(context_done) Context context = CONTEXT_NULL;
int r;
assert(link);
static const sd_json_dispatch_field dispatch_table[] = {
{ "target", SD_JSON_VARIANT_OBJECT, dispatch_target_identifier, voffsetof(context, target_identifier), SD_JSON_MANDATORY },
VARLINK_DISPATCH_POLKIT_FIELD,
{},
};
r = sd_varlink_dispatch(link, parameters, dispatch_table, &context);
if (r != 0)
return r;
r = verify_polkit(&context, link, "org.freedesktop.sysupdate1.check",
(const char**) STRV_MAKE(
"class", target_class_to_string(context.target_identifier.class),
"offline", "0",
context.target_identifier.name ? "name" : NULL, context.target_identifier.name));
if (r <= 0)
return r;
if (getenv_bool("SYSTEMD_SYSUPDATE_NO_VERIFY") > 0)
context.verify = 0;
/* CheckNew is always online */
context.offline = false;
r = context_load_online_from_target(
&context,
PROCESS_IMAGE_READ_ONLY,
READ_DEFINITIONS_REQUIRES_ENABLED_TRANSFERS|
READ_DEFINITIONS_REQUIRES_ANY_TRANSFERS);
if (r == -ENOENT)
return sd_varlink_error(link, "io.systemd.SysUpdate.NoSuchTarget", NULL);
if (r < 0)
return r;
if (context.candidate)
r = sd_varlink_replybo(link, SD_JSON_BUILD_PAIR_STRING("available", context.candidate->version));
else
r = sd_varlink_error(link, "io.systemd.SysUpdate.NoUpdateNeeded", NULL);
if (r < 0)
return r;
return 0;
}
typedef enum {
UPDATE_ACTION_ACQUIRE = 1 << 0,
UPDATE_ACTION_INSTALL = 1 << 1,
} UpdateActionFlags;
static int context_update(
Context *c,
const char *version,
const char *booted_version,
UpdateActionFlags action_flags) {
UpdateSet *applied = NULL;
bool installed = false;
int r, ret = 0;
assert(c);
r = context_load_online(
c,
/* process_image_flags= */ 0,
READ_DEFINITIONS_REQUIRES_ENABLED_TRANSFERS|
READ_DEFINITIONS_REQUIRES_ANY_TRANSFERS|
READ_DEFINITIONS_REQUIRES_ENABLED_COMPONENT);
if (r < 0) {
if (r != -ENOENT)
return r;
/* No transfer files found. In that case, still do the installdb cleanup below */
RET_GATHER(ret, r);
} else {
if (FLAGS_SET(action_flags, UPDATE_ACTION_ACQUIRE))
r = context_acquire(c, version);
else
r = context_process_partial_and_pending(c, version);
if (r < 0)
return r;
if (FLAGS_SET(action_flags, UPDATE_ACTION_INSTALL) && r > 0) { /* installation of update indicated */
r = context_install(c, version, &applied);
if (r < 0)
return r;
installed = r > 0;
}
/* context_install() returns > 0 (and emits a notification) only if it actually applied an update. If
* nothing was applied but SYSTEMD_SYSUPDATE_FORCE_NOTIFY=1 is set, still notify subscribers (without a
* resource list), so e.g. a kernel/policy refresh can be triggered unconditionally. */
if (FLAGS_SET(action_flags, UPDATE_ACTION_INSTALL) && !installed) {
int f = secure_getenv_bool("SYSTEMD_SYSUPDATE_FORCE_NOTIFY");
if (f < 0 && f != -ENXIO)
log_debug_errno(f, "Failed to parse $SYSTEMD_SYSUPDATE_FORCE_NOTIFY, ignoring: %m");
if (f > 0)
(void) context_notify_subscribers(c, /* us= */ NULL);
}
}
if (c->cleanup > 0)
RET_GATHER(ret, installdb_cleanup_component(c));
if (installed) {
/* We installed something, yay */
if (c->reboot) {
assert(applied);
assert(booted_version);
if (strverscmp_improved(applied->version, booted_version) > 0) {
log_notice("Newly installed version is newer than booted version, rebooting.");
RET_GATHER(ret, reboot_now());
} else if (strverscmp_improved(applied->version, booted_version) == 0 &&
FLAGS_SET(applied->flags, UPDATE_INCOMPLETE)) {
log_notice("Currently booted version was incomplete and has been repaired, rebooting.");
RET_GATHER(ret, reboot_now());
} else
log_info("Booted version is newer or identical to newly installed version, not rebooting.");
}
}
return ret;
}
static int verb_update_impl(int argc, char **argv, UpdateActionFlags action_flags) {
const char *version;
int r;
assert(argc <= 2);
version = argc >= 2 ? argv[1] : NULL;
_cleanup_(context_done) Context context = CONTEXT_NULL;
r = context_from_cmdline(&context);
if (r < 0)
return r;
if (context.feature_select != SELECT_EXPLICIT)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--feature-all/--feature-suggested is not supported for '%s'.", argv[0]);
if (!IN_SET(context.component_select, SELECT_EXPLICIT, SELECT_ALL))
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "--component-suggested currently not supported for '%s'.", argv[0]);
if (context.instances_max < 2)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"The --instances-max= argument must be >= 2 while updating");
_cleanup_free_ char *booted_version = NULL;
if (context.reboot) {
/* We already checked in parse_argv() that --component=/--component-all/--component-suggested
* are not combined with --reboot */
assert(!context.component);
assert(context.component_select == SELECT_EXPLICIT);
/* If automatic reboot on completion is requested, let's first determine the currently booted image */
r = parse_os_release(context.root, "IMAGE_VERSION", &booted_version);
if (r < 0)
return log_error_errno(r, "Failed to parse /etc/os-release: %m");
if (!booted_version)
return log_error_errno(SYNTHETIC_ERRNO(ENODATA), "/etc/os-release lacks IMAGE_VERSION field.");
}
switch (context.component_select) {
case SELECT_EXPLICIT:
return context_update(&context, version, booted_version, action_flags);
case SELECT_ALL: {
int ret = 0;
/* Update the default, component-less installation first (if any). Running it before the
* enumeration below also ensures the image (if any) is mounted and context.root is set, so
* that we enumerate the components inside the image rather than on the host. A missing
* default installation (ENOENT) is not an error in this mode. */
r = context_update(&context, version, booted_version, action_flags);
if (r != -ENOENT)
RET_GATHER(ret, r);
_cleanup_strv_free_ char **component_names = NULL;
r = context_list_components(&context, &component_names, /* ret_has_default_component= */ NULL);
if (r < 0) {
RET_GATHER(ret, r);
return ret;
}
STRV_FOREACH(name, component_names) {
_cleanup_(context_done) Context cc = CONTEXT_NULL;
r = context_from_base_with_component(&context, *name, &cc);
if (r < 0) {
RET_GATHER(ret, r);
continue;
}
r = context_update(&cc, version, booted_version, action_flags);
if (r == -EHOSTDOWN) /* Component disabled → skip it in the "all" case. */
continue;
RET_GATHER(ret, r);
}
return ret;
}
default:
assert_not_reached();
}
}
VERB(verb_update, "update", "[VERSION]\0", VERB_ANY, 2, 0,
"Install new version now");
static int verb_update(int argc, char *argv[], uintptr_t _data, void *userdata) {
UpdateActionFlags flags = UPDATE_ACTION_INSTALL;
if (!arg_offline)
flags |= UPDATE_ACTION_ACQUIRE;
return verb_update_impl(argc, argv, flags);
}
VERB(verb_acquire, "acquire", "[VERSION]\0", VERB_ANY, 2, 0,
"Acquire (download) new version now");
static int verb_acquire(int argc, char *argv[], uintptr_t _data, void *userdata) {
return verb_update_impl(argc, argv, UPDATE_ACTION_ACQUIRE);
}
VERB_NOARG(verb_vacuum, "vacuum",
"Make room, by deleting old versions");
static int verb_vacuum(int argc, char *argv[], uintptr_t _data, void *userdata) {
_cleanup_(context_done) Context context = CONTEXT_NULL;
int r;
assert(argc <= 1);
r = context_from_cmdline(&context);
if (r < 0)
return r;
if (context.feature_select != SELECT_EXPLICIT)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--feature-all/--feature-suggested is not supported for '%s'.", argv[0]);
if (context.component_select != SELECT_EXPLICIT)
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "--component-all/--component-suggested currently not supported for '%s'.", argv[0]);
if (context.instances_max < 1)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"The --instances-max= argument must be >= 1 while vacuuming");
r = context_load_offline(
&context,
/* process_image_flags= */ 0,
READ_DEFINITIONS_REQUIRES_ANY_TRANSFERS);
if (r < 0)
return r;
return context_vacuum(&context, 0, NULL);
}
VERB_NOARG(verb_cleanup, "cleanup", "Clean up orphaned files");
static int verb_cleanup(int argc, char *argv[], uintptr_t _data, void *userdata) {
_cleanup_(context_done) Context context = CONTEXT_NULL;
int r;
assert(argc <= 1);
r = context_from_cmdline(&context);
if (r < 0)
return r;
if (context.cleanup == 0)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invocation of 'cleanup' with --cleanup=no is contradictory, refusing.");
if (context.feature_select != SELECT_EXPLICIT)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--feature-all/--feature-suggested is not supported for '%s'.", argv[0]);
if (!IN_SET(context.component_select, SELECT_EXPLICIT, SELECT_ALL))
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "--component-suggested currently not supported for '%s'.", argv[0]);
r = context_load_offline(
&context,
/* process_image_flags= */ 0,
/* read_definitions_flags= */ 0);
if (r < 0)
return r;
int ret = 0;
RET_GATHER(ret, installdb_cleanup_component(&context));
if (context.component_select == SELECT_ALL) {
_cleanup_strv_free_ char **z = NULL;
r = installdb_list_components(&context, &z);
if (r < 0)
return log_error_errno(r, "Failed to enumerate components: %m");
STRV_FOREACH(i, z) {
_cleanup_(context_done) Context component_context = CONTEXT_NULL;
r = context_from_cmdline(&component_context);
if (r < 0)
return r;
/* Override the component with our iter. This needs to be done in a fresh Context
* as the installdb_fd and other state are specific to the component. */
r = free_and_strdup_warn(&component_context.component, *i);
if (r < 0)
return r;
r = context_load_offline(
&component_context,
/* process_image_flags= */ 0,
/* read_definitions_flags= */ 0);
if (r < 0)
return r;
RET_GATHER(ret, installdb_cleanup_component(&component_context));
}
}
return ret;
}
VERB(verb_pending_or_reboot, "pending", NULL, 1, 1, 0,
"Report whether a newer version is installed than currently booted");
VERB(verb_pending_or_reboot, "reboot", NULL, 1, 1, 0,
"Reboot if a newer version is installed than booted");
static int verb_pending_or_reboot(int argc, char *argv[], uintptr_t _data, void *userdata) {
_cleanup_(context_done) Context context = CONTEXT_NULL;
_cleanup_free_ char *booted_version = NULL;
int r;
assert(argc == 1);
r = context_from_cmdline(&context);
if (r < 0)
return r;
if (context.image || context.root)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"The --root=/--image= switches may not be combined with the '%s' operation.", argv[0]);
if (context.feature_select != SELECT_EXPLICIT)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "--feature-all/--feature-suggested is not supported for '%s'.", argv[0]);
if (context.component || context.component_select != SELECT_EXPLICIT)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"The --component=, --component-all and --component-suggested switches may not be combined with the '%s' operation, which only applies to the booted OS version.", argv[0]);
r = context_load_offline(
&context,
/* process_image_flags= */ 0,
READ_DEFINITIONS_REQUIRES_ENABLED_TRANSFERS|
READ_DEFINITIONS_REQUIRES_ANY_TRANSFERS|
READ_DEFINITIONS_REQUIRES_ENABLED_COMPONENT);
if (r < 0)
return r;
log_info("Determining installed update sets%s", glyph(GLYPH_ELLIPSIS));
r = context_discover_update_sets_by_flag(&context, UPDATE_INSTALLED);
if (r < 0)
return r;
if (!context.newest_installed)
return log_error_errno(SYNTHETIC_ERRNO(ENODATA), "Couldn't find any suitable installed versions.");
r = parse_os_release(context.root, "IMAGE_VERSION", &booted_version);
if (r < 0) /* yes, context.root is NULL here, but we have to pass something, and it's a lot more readable
* if we see what the first argument is about */
return log_error_errno(r, "Failed to parse /etc/os-release: %m");
if (!booted_version)
return log_error_errno(SYNTHETIC_ERRNO(ENODATA), "/etc/os-release lacks IMAGE_VERSION= field.");
r = strverscmp_improved(context.newest_installed->version, booted_version);
if (r > 0) {
log_notice("Newest installed version '%s' is newer than booted version '%s'.%s",
context.newest_installed->version, booted_version,
streq(argv[0], "pending") ? " Reboot recommended." : "");
if (streq(argv[0], "reboot"))
return reboot_now();
return EXIT_SUCCESS;
} else if (r == 0)
log_info("Newest installed version '%s' matches booted version '%s'.",
context.newest_installed->version, booted_version);
else
log_warning("Newest installed version '%s' is older than booted version '%s'.",
context.newest_installed->version, booted_version);
if (streq(argv[0], "pending")) /* When called as 'pending' tell the caller via failure exit code that there's nothing newer installed */
return EXIT_FAILURE;
return EXIT_SUCCESS;
}
static int context_list_components(Context *context, char ***ret_component_names, bool *ret_has_default_component) {
int r;
assert(context);
_cleanup_strv_free_ char **z = NULL;
r = get_component_list(context->root, &z);
if (r < 0)
return r;
if (ret_component_names)
*ret_component_names = TAKE_PTR(z);
/* Does the system have at least one transfer file in sysupdate.d, which can be considered the
* default component-less installation? */