HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/sysupdate/sysupdate.c
Git blob: 53c690b9bc5a42764e46b95cc8a991b63a15dadc
Lines 1001–2000 of 3,595
if (!have_read_only)
(void) table_hide_column_from_display(t, 10);
if (!have_growfs)
(void) table_hide_column_from_display(t, 11);
if (!have_sha256)
(void) table_hide_column_from_display(t, 12);
if (!sd_json_format_enabled(arg_json_format_flags)) {
printf("%s%s%s Version: %s\n"
" State: %s%s%s\n"
"Installed: %s%s%s%s%s%s%s\n"
"Available: %s%s\n"
"Protected: %s%s%s\n"
" Obsolete: %s%s%s\n",
strempty(update_set_flags_to_color(us->flags)), update_set_flags_to_glyph(us->flags), ansi_normal(), us->version,
strempty(update_set_flags_to_color(us->flags)), update_set_flags_to_string(us->flags), ansi_normal(),
yes_no(us->flags & UPDATE_INSTALLED), FLAGS_SET(us->flags, UPDATE_INSTALLED|UPDATE_NEWEST) ? " (newest)" : "",
FLAGS_SET(us->flags, UPDATE_INCOMPLETE) ? ansi_highlight_yellow() : "", FLAGS_SET(us->flags, UPDATE_INCOMPLETE) ? " (incomplete)" : "", ansi_normal(),
FLAGS_SET(us->flags, UPDATE_INSTALLED|UPDATE_PENDING) ? " (pending)" : "", FLAGS_SET(us->flags, UPDATE_INSTALLED|UPDATE_PARTIAL) ? " (partial)" : "",
yes_no(us->flags & UPDATE_AVAILABLE), (us->flags & (UPDATE_INSTALLED|UPDATE_AVAILABLE|UPDATE_NEWEST)) == (UPDATE_AVAILABLE|UPDATE_NEWEST) ? " (newest)" : "",
FLAGS_SET(us->flags, UPDATE_INSTALLED|UPDATE_PROTECTED) ? ansi_highlight() : "", yes_no(FLAGS_SET(us->flags, UPDATE_INSTALLED|UPDATE_PROTECTED)), ansi_normal(),
us->flags & UPDATE_OBSOLETE ? ansi_highlight_red() : "", yes_no(us->flags & UPDATE_OBSOLETE), ansi_normal());
STRV_FOREACH(url, changelog_urls) {
_cleanup_free_ char *changelog_link = NULL;
r = terminal_urlify(*url, NULL, &changelog_link);
if (r < 0)
return log_oom();
printf("ChangeLog: %s\n", changelog_link);
}
printf("\n");
return table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, arg_legend);
} else {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *t_json = NULL;
r = table_to_json(t, &t_json);
if (r < 0)
return log_error_errno(r, "failed to convert table to JSON: %m");
r = sd_json_buildo(&json, SD_JSON_BUILD_PAIR_STRING("version", us->version),
SD_JSON_BUILD_PAIR_BOOLEAN("newest", FLAGS_SET(us->flags, UPDATE_NEWEST)),
SD_JSON_BUILD_PAIR_BOOLEAN("available", FLAGS_SET(us->flags, UPDATE_AVAILABLE)),
SD_JSON_BUILD_PAIR_BOOLEAN("installed", FLAGS_SET(us->flags, UPDATE_INSTALLED)),
SD_JSON_BUILD_PAIR_BOOLEAN("partial", FLAGS_SET(us->flags, UPDATE_PARTIAL)),
SD_JSON_BUILD_PAIR_BOOLEAN("pending", FLAGS_SET(us->flags, UPDATE_PENDING)),
SD_JSON_BUILD_PAIR_BOOLEAN("obsolete", FLAGS_SET(us->flags, UPDATE_OBSOLETE)),
SD_JSON_BUILD_PAIR_BOOLEAN("protected", FLAGS_SET(us->flags, UPDATE_PROTECTED)),
SD_JSON_BUILD_PAIR_BOOLEAN("incomplete", FLAGS_SET(us->flags, UPDATE_INCOMPLETE)),
SD_JSON_BUILD_PAIR_STRV("changelogUrls", changelog_urls),
SD_JSON_BUILD_PAIR_VARIANT("contents", t_json));
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 context_vacuum(
Context *c,
uint64_t space,
const char *extra_protected_version) {
size_t disabled_count = 0;
int r, count = 0;
assert(c);
if (space == 0)
log_info("Making room%s", glyph(GLYPH_ELLIPSIS));
else
log_info("Making room for %" PRIu64 " updates%s", space, glyph(GLYPH_ELLIPSIS));
FOREACH_ARRAY(tr, c->transfers, c->n_transfers) {
Transfer *t = *tr;
/* Don't bother clearing out space if we're not going to be downloading anything */
if (extra_protected_version && resource_find_instance(&t->target, extra_protected_version))
continue;
r = transfer_vacuum(t, space, extra_protected_version);
if (r < 0)
return r;
count = MAX(count, r);
}
FOREACH_ARRAY(tr, c->disabled_transfers, c->n_disabled_transfers) {
r = transfer_vacuum(*tr, UINT64_MAX /* wipe all instances */, NULL);
if (r < 0)
return r;
if (r > 0)
disabled_count++;
}
if (!sd_json_format_enabled(arg_json_format_flags)) {
if (count > 0 && disabled_count > 0)
log_info("Removed %i instances, and %zu disabled transfers.", count, disabled_count);
else if (count > 0)
log_info("Removed %i instances.", count);
else if (disabled_count > 0)
log_info("Removed %zu disabled transfers.", disabled_count);
else
log_info("Found nothing to remove.");
} else {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL;
r = sd_json_buildo(&json,
SD_JSON_BUILD_PAIR_INTEGER("removed", count),
SD_JSON_BUILD_PAIR_UNSIGNED("disabledTransfers", disabled_count));
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;
}
typedef enum ProcessImageFlags {
PROCESS_IMAGE_READ_ONLY = 1 << 0,
} ProcessImageFlags;
static int context_process_image(
Context *c,
ProcessImageFlags flags) {
_cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
_cleanup_(umount_and_rmdir_and_freep) char *mounted_dir = NULL;
int r;
assert(c);
if (c->root || !c->image) /* Idempotent or nothing to do */
return 0;
assert(!c->mounted_dir);
assert(!c->loop_device);
r = mount_image_privately_interactively(
c->image,
c->image_policy,
(FLAGS_SET(flags, PROCESS_IMAGE_READ_ONLY) ? DISSECT_IMAGE_READ_ONLY : 0) |
DISSECT_IMAGE_FSCK |
DISSECT_IMAGE_MKDIR |
DISSECT_IMAGE_GROWFS |
DISSECT_IMAGE_RELAX_VAR_CHECK |
DISSECT_IMAGE_USR_NO_ROOT |
DISSECT_IMAGE_GENERIC_ROOT |
DISSECT_IMAGE_REQUIRE_ROOT |
DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
&mounted_dir,
/* ret_dir_fd= */ NULL,
&loop_device);
if (r < 0)
return r;
c->root = strdup(mounted_dir);
if (!c->root)
return log_oom();
c->mounted_dir = TAKE_PTR(mounted_dir);
c->loop_device = TAKE_PTR(loop_device);
return 0;
}
static int context_list_components(Context *context, char ***ret_component_names, bool *ret_has_default_component);
static int context_load_offline(
Context *context,
ProcessImageFlags process_image_flags,
ReadDefinitionsFlags read_definitions_flags) {
int r;
assert(context);
/* Sets up a context object and initializes everything we can initialize offline, i.e. without
* checking on the update source (i.e. the Internet) what versions are available */
r = context_process_image(context, process_image_flags);
if (r < 0)
return r;
r = context_read_definitions(context, context->loop_device ? context->loop_device->node : NULL, read_definitions_flags);
if (r < 0)
return r;
r = context_load_installed_instances(context);
if (r < 0)
return r;
return 0;
}
static int context_load_online(
Context *context,
ProcessImageFlags process_image_flags,
ReadDefinitionsFlags read_definitions_flags) {
int r;
assert(context);
/* Like context_load_offline(), but also communicates with the update source looking for new
* versions (as long as --offline is not specified on the command line). */
r = context_load_offline(
context,
process_image_flags,
read_definitions_flags);
if (r < 0)
return r;
if (!context->offline) {
r = context_load_available_instances(context);
if (r < 0)
return r;
}
r = context_discover_update_sets(context);
if (r < 0)
return r;
return 0;
}
static bool image_type_can_sysupdate(ImageType image_type) {
/* systemd-sysupdate doesn't support mstack images yet */
return IN_SET(image_type, IMAGE_DIRECTORY, IMAGE_SUBVOLUME, IMAGE_RAW, IMAGE_BLOCK);
}
static int context_load_paths_from_image(Context *context, Image *image) {
assert(context);
assert(image);
assert(!context->root);
assert(!context->image);
switch (image->type) {
case IMAGE_DIRECTORY:
case IMAGE_SUBVOLUME:
context->root = strdup(image->path);
if (!context->root)
return log_oom();
return 0;
case IMAGE_RAW:
case IMAGE_BLOCK:
context->image = strdup(image->path);
if (!context->image)
return log_oom();
return 0;
default:
assert_not_reached();
}
}
static void target_identifier_hash_func(const TargetIdentifier *t, struct siphash *state) {
assert(t);
siphash24_compress_typesafe(t->class, state);
siphash24_compress_string(t->name, state);
}
static int target_identifier_compare_func(const TargetIdentifier *x, const TargetIdentifier *y) {
int r;
assert(x);
assert(y);
r = CMP(x->class, y->class);
if (r != 0)
return r;
return strcmp(x->name, y->name);
}
DEFINE_PRIVATE_HASH_OPS_WITH_VALUE_DESTRUCTOR(target_identifier_hash_ops,
TargetIdentifier, target_identifier_hash_func, target_identifier_compare_func,
TargetIdentifier, target_identifier_free);
static int enumerate_image_class(Context *context, RuntimeScope runtime_scope, TargetClass class, Set **targets) {
_cleanup_hashmap_free_ Hashmap *images = NULL;
Image *image;
int r;
assert(context);
assert(runtime_scope >= 0 && runtime_scope < _RUNTIME_SCOPE_MAX && runtime_scope != RUNTIME_SCOPE_GLOBAL);
assert(class >= 0 && class < _TARGET_CLASS_MAX);
r = image_discover(runtime_scope, (ImageClass) class, NULL, &images);
if (r < 0)
return r;
HASHMAP_FOREACH(image, images) {
_cleanup_(target_identifier_freep) TargetIdentifier *t = NULL;
bool have = false;
_cleanup_(context_done) Context image_context = CONTEXT_NULL;
if (image_is_host(image))
continue; /* We already enroll the host ourselves */
if (!image_type_can_sysupdate(image->type))
continue;
r = context_load_paths_from_image(&image_context, image);
if (r < 0)
return r;
image_context.verify = context->verify;
image_context.offline = true;
if (context->image_policy) {
image_context.image_policy = image_policy_copy(context->image_policy);
if (!image_context.image_policy)
return log_oom();
}
/* Load the components in a separate Context specific to the given Image before
* committing to loading that state to the main Context. */
r = context_load_offline(
&image_context,
PROCESS_IMAGE_READ_ONLY,
/* read_definitions_flags= */ 0);
if (r < 0)
return r;
r = context_list_components(&image_context, /* ret_component_names= */ NULL, &have);
if (r < 0)
return r;
if (!have) {
log_debug("Skipping %s because it has no default component", image->path);
continue;
}
r = target_identifier_new(class, image->name, &t);
if (r < 0)
return r;
r = set_ensure_consume(targets, &target_identifier_hash_ops, TAKE_PTR(t));
if (r < 0)
return r;
}
return 0;
}
static int context_enumerate_components(Context *context, Set **targets) {
_cleanup_strv_free_ char **component_names = NULL;
bool have_default_component;
int r;
assert(context);
r = context_list_components(context, &component_names, &have_default_component);
if (r < 0)
return r;
if (have_default_component) {
_cleanup_(target_identifier_freep) TargetIdentifier *t = NULL;
r = target_identifier_new(TARGET_HOST, "host", &t);
if (r < 0)
return r;
r = set_ensure_consume(targets, &target_identifier_hash_ops, TAKE_PTR(t));
if (r < 0)
return r;
}
STRV_FOREACH(component, component_names) {
_cleanup_(target_identifier_freep) TargetIdentifier *t = NULL;
r = target_identifier_new(TARGET_COMPONENT, *component, &t);
if (r < 0)
return r;
r = set_ensure_consume(targets, &target_identifier_hash_ops, TAKE_PTR(t));
if (r < 0)
return r;
}
return 0;
}
static int context_enumerate_targets(Context *context, Set **targets) {
static const TargetClass discoverable_classes[] = {
TARGET_MACHINE,
TARGET_PORTABLE,
TARGET_SYSEXT,
TARGET_CONFEXT,
};
int r;
assert(context);
FOREACH_ARRAY(class, discoverable_classes, ELEMENTSOF(discoverable_classes)) {
r = enumerate_image_class(context, RUNTIME_SCOPE_SYSTEM, *class, targets);
if (r < 0)
return r;
}
r = context_enumerate_components(context, targets);
if (r < 0)
return r;
return 0;
}
static int context_load_paths_from_target(Context *context) {
int r;
assert(context);
assert(context->target_identifier.class != _TARGET_CLASS_INVALID);
/* These shouldn’t have been set up some other way first */
assert(!context->component);
assert(!context->root);
assert(!context->image);
switch (context->target_identifier.class) {
case TARGET_MACHINE:
case TARGET_PORTABLE:
case TARGET_SYSEXT:
case TARGET_CONFEXT: {
_cleanup_hashmap_free_ Hashmap *images = NULL;
Image *image, *selected_image = NULL;
/* These are all image-based target classes, so first find the corresponding image. */
r = image_discover(RUNTIME_SCOPE_SYSTEM, (ImageClass) context->target_identifier.class, NULL, &images);
if (r < 0)
return r;
HASHMAP_FOREACH(image, images) {
bool have = false;
_cleanup_(context_done) Context image_context = CONTEXT_NULL;
if (image_is_host(image))
continue; /* We already enroll the host ourselves */
if (!image_type_can_sysupdate(image->type))
continue;
if (!streq(image->name, context->target_identifier.name))
continue;
r = context_load_paths_from_image(&image_context, image);
if (r < 0)
return r;
image_context.verify = context->verify;
image_context.offline = true;
if (context->image_policy) {
image_context.image_policy = image_policy_copy(context->image_policy);
if (!image_context.image_policy)
return log_oom();
}
/* Load the components in a separate Context specific to the given Image before
* committing to loading that state to the main Context. */
r = context_load_offline(&image_context, 0, 0);
if (r < 0)
return r;
r = context_list_components(&image_context, /* ret_component_names= */ NULL, &have);
if (r < 0)
return r;
if (!have) {
log_debug("Skipping %s because it has no default component", image->path);
continue;
}
/* This is the match we were looking for */
selected_image = image;
break;
}
if (!selected_image)
return -ENOENT;
r = context_load_paths_from_image(context, selected_image);
if (r < 0)
return r;
break;
}
case TARGET_HOST:
/* No additional setup needed */
break;
case TARGET_COMPONENT: {
_cleanup_strv_free_ char **component_names = NULL;
r = context_list_components(context, &component_names, /* ret_has_default_component= */ NULL);
if (r < 0)
return r;
if (!strv_contains(component_names, context->target_identifier.name))
return -ENOENT;
context->component = strdup(context->target_identifier.name);
if (!context->component)
return log_oom();
break;
}
default:
assert_not_reached();
}
return 0;
}
/* Load a Context to point to the target given by the TargetIdentifier. The TargetIdentifier will have been
* syntactically validated by dispatch_target_identifier(), but might still point to components which don’t
* exist, images which the user isn’t privileged to access, etc. This function validates the TargetIdentifier
* against an enumerated list of known targets, which are safe to update without additional permissions. */
static int context_load_online_from_target(
Context *context,
ProcessImageFlags process_image_flags,
ReadDefinitionsFlags read_definitions_flags) {
int r;
assert(context);
assert(context->target_identifier.class != _TARGET_CLASS_INVALID);
r = context_load_paths_from_target(context);
if (r < 0)
return r;
return context_load_online(context, process_image_flags, read_definitions_flags);
}
static int context_load_offline_from_target(
Context *context,
ProcessImageFlags process_image_flags,
ReadDefinitionsFlags read_definitions_flags) {
int r;
assert(context);
assert(context->target_identifier.class != _TARGET_CLASS_INVALID);
r = context_load_paths_from_target(context);
if (r < 0)
return r;
return context_load_offline(context, process_image_flags, read_definitions_flags);
}
static int context_on_acquire_progress(const Transfer *t, const Instance *inst, unsigned percentage) {
const Context *c = ASSERT_PTR(t->context);
size_t i, n = c->n_transfers;
uint64_t base, scaled;
unsigned overall;
for (i = 0; i < n; i++)
if (c->transfers[i] == t)
break;
assert(i < n); /* We should have found the index */
base = (100 * 100 * i) / n;
scaled = (100 * percentage) / n;
overall = (unsigned) ((base + scaled) / 100);
assert(overall <= 100);
log_debug("Transfer %zu/%zu is %u%% complete (%u%% overall).", i+1, n, percentage, overall);
return sd_notifyf(/* unset_environment= */ false, "X_SYSUPDATE_PROGRESS=%u\n"
"X_SYSUPDATE_TRANSFERS_LEFT=%zu\n"
"X_SYSUPDATE_TRANSFERS_DONE=%zu\n"
"STATUS=Updating to '%s' (%u%% complete).",
overall, n - i, i, inst->metadata.version, overall);
}
static int context_process_partial_and_pending(Context *c, const char *version);
static int context_acquire(
Context *c,
const char *version) {
UpdateSet *us = NULL;
int r;
assert(c);
if (version) {
us = context_update_set_by_version(c, version);
if (!us)
return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "Update '%s' not found.", version);
} else {
if (!c->candidate) {
log_info("No update needed.");
return 0;
}
us = c->candidate;
}
if (FLAGS_SET(us->flags, UPDATE_INCOMPLETE))
log_info("Selected update '%s' is already installed, but incomplete. Repairing.", us->version);
else if (FLAGS_SET(us->flags, UPDATE_PARTIAL)) {
return log_error_errno(SYNTHETIC_ERRNO(EUCLEAN), "Selected update '%s' is already acquired and partially installed. Vacuum it to try installing again.", us->version);
} else if (FLAGS_SET(us->flags, UPDATE_PENDING)) {
log_info("Selected update '%s' is already acquired and pending installation.", us->version);
return context_process_partial_and_pending(c, version);
} else if (FLAGS_SET(us->flags, UPDATE_INSTALLED)) {
log_info("Selected update '%s' is already installed. Skipping update.", us->version);
return 0;
}
if (!FLAGS_SET(us->flags, UPDATE_AVAILABLE))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Selected update '%s' is not available, refusing.", us->version);
if (FLAGS_SET(us->flags, UPDATE_OBSOLETE))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Selected update '%s' is obsolete, refusing.", us->version);
if (!FLAGS_SET(us->flags, UPDATE_NEWEST))
log_notice("Selected update '%s' is not the newest, proceeding anyway.", us->version);
if (c->newest_installed && strverscmp_improved(c->newest_installed->version, us->version) > 0)
log_notice("Selected update '%s' is older than newest installed version, proceeding anyway.", us->version);
log_info("Selected update '%s' for install.", us->version);
_cleanup_free_ InstanceMetadata *metadata = new0(InstanceMetadata, c->n_transfers);
if (!metadata)
return log_oom();
/* Compute up the temporary paths before vacuuming so we don't vacuum anything if we fail to compute
* any paths because of failed validations (e.g. exceeding the gpt partition label size). */
for (size_t i = 0; i < c->n_transfers; i++) {
Instance *inst = us->instances[i];
Transfer *t = c->transfers[i];
assert(inst);
r = transfer_compute_temporary_paths(t, inst, metadata + i);
if (r < 0)
return r;
}
(void) sd_notifyf(/* unset_environment= */ false,
"READY=1\n"
"X_SYSUPDATE_VERSION=%s\n"
"STATUS=Making room for '%s'.", us->version, us->version);
/* Let's make some room. We make sure for each transfer we have one free space to fill. While
* removing stuff we'll protect the version we are trying to acquire. Why that? Maybe an earlier
* download succeeded already, in which case we shouldn't remove it just to acquire it again */
r = context_vacuum(
c,
/* space= */ 1,
/* extra_protected_version= */ us->version);
if (r < 0)
return r;
if (c->sync)
sync();
(void) sd_notifyf(/* unset_environment= */ false,
"STATUS=Updating to '%s'.", us->version);
/* There should now be one instance picked for each transfer, and the order is the same */
assert(us->n_instances == c->n_transfers);
for (size_t i = 0; i < c->n_transfers; i++) {
Instance *inst = us->instances[i];
Transfer *t = c->transfers[i];
assert(inst); /* ditto */
if (inst->resource == &t->target) { /* a present transfer in an incomplete installation */
assert(FLAGS_SET(us->flags, UPDATE_INCOMPLETE));
continue;
}
r = transfer_acquire_instance(t, inst, metadata + i, context_on_acquire_progress, c);
if (r < 0)
return r;
}
if (c->sync)
sync();
return 1;
}
/* Check to see if we have an update set acquired and pending installation. */
static int context_process_partial_and_pending(
Context *c,
const char *version) {
UpdateSet *us = NULL;
int r;
assert(c);
if (version) {
us = context_update_set_by_version(c, version);
if (!us)
return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "Update '%s' not found.", version);
} else {
if (!c->candidate) {
log_info("No update needed.");
return 0;
}
us = c->candidate;
}
if (FLAGS_SET(us->flags, UPDATE_INCOMPLETE))
log_info("Selected update '%s' is already installed, but incomplete. Repairing.", us->version);
else if ((us->flags & (UPDATE_PARTIAL|UPDATE_PENDING|UPDATE_INSTALLED)) == UPDATE_INSTALLED) {
log_info("Selected update '%s' is already installed. Skipping update.", us->version);
return 0;
}
if (FLAGS_SET(us->flags, UPDATE_PARTIAL))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Selected update '%s' is only partially downloaded, refusing.", us->version);
if (!FLAGS_SET(us->flags, UPDATE_PENDING))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Selected update '%s' is not pending installation, refusing.", us->version);
if (FLAGS_SET(us->flags, UPDATE_OBSOLETE))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Selected update '%s' is obsolete, refusing.", us->version);
if (!FLAGS_SET(us->flags, UPDATE_NEWEST))
log_notice("Selected update '%s' is not the newest, proceeding anyway.", us->version);
if (c->newest_installed && strverscmp_improved(c->newest_installed->version, us->version) > 0)
log_notice("Selected update '%s' is older than newest installed version, proceeding anyway.", us->version);
log_info("Selected update '%s' for install.", us->version);
/* There should now be one instance picked for each transfer, and the order is the same */
assert(us->n_instances == c->n_transfers);
for (size_t i = 0; i < c->n_transfers; i++) {
Instance *inst = us->instances[i];
Transfer *t = c->transfers[i];
assert(inst);
r = transfer_process_partial_and_pending_instance(t, inst);
if (r < 0)
return r;
}
return 1;
}
static int notify_subscribers_reply(
sd_varlink *link,
sd_json_variant *reply,
const char *error_id,
sd_varlink_reply_flags_t flags,
void *userdata) {
assert(link);
if (error_id)
log_warning("Notification subscriber '%s' returned error, ignoring: %s",
strna(sd_varlink_get_description(link)), error_id);
return 0;
}
static int context_notify_subscribers(Context *c, UpdateSet *us) {
int r;
assert(c);
/* 'us' is NULL when we are forced to notify even though no update was applied (via
* SYSTEMD_SYSUPDATE_FORCE_NOTIFY=1). In that case we send neither a version nor a resource list. */
_cleanup_(sd_json_variant_unrefp) sd_json_variant *resources = NULL;
if (us)
for (size_t i = 0; i < c->n_transfers; i++) {
Instance *inst = us->instances[i];
Transfer *t = c->transfers[i];
if (inst->resource == &t->target &&
!inst->is_pending)
continue;
/* Report where the resource was installed *to* (not the source it came from): the
* final on-disk path for filesystem targets, the partition device node for partition
* targets. */
const char *target_path =
RESOURCE_IS_FILESYSTEM(t->target.type) ? t->final_path :
t->target.type == RESOURCE_PARTITION ? t->partition_info.device :
NULL;
r = sd_json_variant_append_arraybo(
&resources,
SD_JSON_BUILD_PAIR_STRING("transfer", t->id),
SD_JSON_BUILD_PAIR_CONDITION(!!target_path, "path", SD_JSON_BUILD_STRING(target_path)));
if (r < 0)
return log_warning_errno(r, "Failed to build sysupdate notify resources list, skipping notification: %m");
}
_cleanup_(sd_json_variant_unrefp) sd_json_variant *params = NULL;
r = sd_json_buildo(
¶ms,
SD_JSON_BUILD_PAIR_CONDITION(!!c->component, "component", SD_JSON_BUILD_STRING(c->component)),
SD_JSON_BUILD_PAIR_CONDITION(!!us, "version", SD_JSON_BUILD_STRING(us ? us->version : NULL)),
SD_JSON_BUILD_PAIR_CONDITION(!!resources, "resources", SD_JSON_BUILD_VARIANT(resources)));
if (r < 0)
return log_warning_errno(r, "Failed to build sysupdate notify parameters, skipping notification: %m");
ssize_t n = varlink_execute_directory(
VARLINK_DIR_SYSUPDATE_NOTIFY_HOOK,
"io.systemd.SysUpdate.Notify.OnCompletedUpdate",
params,
/* more= */ false,
/* timeout_usec= */ 5 * USEC_PER_MINUTE,
notify_subscribers_reply,
/* userdata= */ NULL);
if (n < 0)
log_debug_errno(n, "Failed to dispatch sysupdate notification to %s, ignoring: %m",
VARLINK_DIR_SYSUPDATE_NOTIFY_HOOK);
else if (n > 0)
log_debug("Dispatched sysupdate notification to %zi subscribers in %s.", n, VARLINK_DIR_SYSUPDATE_NOTIFY_HOOK);
return 0;
}
static int context_install(
Context *c,
const char *version,
UpdateSet **ret_applied) {
UpdateSet *us = NULL;
int r;
assert(c);
if (version) {
us = context_update_set_by_version(c, version);
if (!us)
return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "Update '%s' not found.", version);
} else {
if (!c->candidate) {
log_info("No update needed.");
return 0;
}
us = c->candidate;
}
(void) sd_notifyf(/* unset_environment=*/ false,
"READY=1\n"
"X_SYSUPDATE_VERSION=%s\n"
"STATUS=Installing '%s'.", us->version, us->version);
for (size_t i = 0; i < c->n_transfers; i++) {
Instance *inst = us->instances[i];
Transfer *t = c->transfers[i];
if (inst->resource == &t->target &&
!inst->is_pending)
continue;
r = transfer_install_instance(t, inst, c->root);
if (r < 0)
return r;
}
log_info("%s Successfully installed update '%s'.", glyph(GLYPH_SPARKLES), us->version);
if (!c->root)
(void) context_notify_subscribers(c, us);
(void) sd_notifyf(/* unset_environment= */ false,
"STATUS=Installed '%s'.", us->version);
if (ret_applied)
*ret_applied = us;
return 1;
}
static int context_get_transfers_for_feature(Context *context, const char *feature_id, char ***ret_transfers) {
int r;
assert(context);
assert(feature_id);
FOREACH_ARRAY(tr, context->transfers, context->n_transfers) {
Transfer *t = *tr;
if (!strv_contains(t->features, feature_id) && !strv_contains(t->requisite_features, feature_id))
continue;
r = strv_extend(ret_transfers, t->id);
if (r < 0)
return log_oom();
}
FOREACH_ARRAY(tr, context->disabled_transfers, context->n_disabled_transfers) {
Transfer *t = *tr;
if (!strv_contains(t->features, feature_id) && !strv_contains(t->requisite_features, feature_id))
continue;
r = strv_extend(ret_transfers, t->id);
if (r < 0)
return log_oom();
}
return 0;
}
static JSON_DISPATCH_ENUM_DEFINE(dispatch_target_class, TargetClass, target_class_from_string);
static int dispatch_target_identifier(const char *name, sd_json_variant *variant, sd_json_dispatch_flags_t flags, void *userdata) {
TargetIdentifier *t = ASSERT_PTR(userdata);
static const sd_json_dispatch_field dispatch[] = {
{ "class", SD_JSON_VARIANT_STRING, dispatch_target_class, voffsetof(*t, class), SD_JSON_MANDATORY },
{ "name", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, voffsetof(*t, name), SD_JSON_NULLABLE },
{}
};
int r;
r = sd_json_dispatch(variant, dispatch, flags, t);
if (r < 0)
return r;
/* Name is mandatory unless class is `host` */
if ((t->class == TARGET_HOST) != (!t->name))
return json_log(variant, flags, SYNTHETIC_ERRNO(ENXIO), "Target name does not match class.");
if (t->class == TARGET_COMPONENT && !component_name_valid(t->name))
return json_log(variant, flags, SYNTHETIC_ERRNO(EINVAL), "Component name invalid: %s", t->name);
return 0;
}
static int verify_polkit(Context *context, sd_varlink *link, const char *action, const char **details) {
int r;
Server *s = ASSERT_PTR(sd_varlink_get_userdata(ASSERT_PTR(link)));
assert(context);
if (!s->system_bus) {
r = sd_bus_open_system_with_description(&s->system_bus, "sysupdate-system");
if (r < 0)
return log_error_errno(r, "Failed to get system bus connection: %m");
r = sd_bus_attach_event(s->system_bus, sd_varlink_get_event(link), SD_EVENT_PRIORITY_NORMAL);
if (r < 0)
return log_error_errno(r, "Failed to attach system bus to event loop: %m");
}
return varlink_verify_polkit_async(link,
s->system_bus,
action,
details,
&s->polkit_registry);
}
VERB(verb_list, "list", "[VERSION]\0", VERB_ANY, 2, VERB_DEFAULT,
"Show installed and available versions");
static int verb_list(int argc, char *argv[], uintptr_t _data, void *userdata) {
_cleanup_(context_done) Context context = CONTEXT_NULL;
_cleanup_strv_free_ char **appstream_urls = NULL;
const char *version;
int r;
assert(argc <= 2);
version = 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_online(
&context,
PROCESS_IMAGE_READ_ONLY,
READ_DEFINITIONS_REQUIRES_ENABLED_TRANSFERS|
READ_DEFINITIONS_REQUIRES_ANY_TRANSFERS);
if (r < 0)
return r;
if (version)
return context_show_version(&context, version);
else if (!sd_json_format_enabled(arg_json_format_flags))
return context_show_table(&context);
else {