HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/sysupdate/updatectl.c
Git blob: 8499ef46bf2445319bc8db820b628ab8112d77ed
Lines 1001–1755 of 1,755
if (r < 0)
return log_bus_error(r, NULL, op->target_id, "listen for PropertiesChanged");
TAKE_PTR(op); /* update_install_finished/update_interrupted take ownership of the data */
return 0;
}
static int update_install_finished(sd_bus_message *m, void *userdata, sd_bus_error *error) {
_cleanup_(operation_freep) Operation *op = ASSERT_PTR(userdata);
OrderedHashmap *map = ASSERT_PTR(op->userdata);
uint64_t id;
int r, status;
assert(m);
r = sd_bus_message_read(m, "toi", &id, NULL, &status);
if (r < 0) {
bus_log_parse_error_debug(r);
return 0;
}
if (id != op->job_id) {
TAKE_PTR(op);
return 0;
}
if (status == 0) /* success */
status = UPDATE_PROGRESS_DONE;
else if (status > 0) /* exit status without errno */
status = UPDATE_PROGRESS_FAILED; /* i.e. EXIT_FAILURE */
/* else errno */
r = ordered_hashmap_replace(map, op->target_id, INT_TO_PTR(status));
if (r < 0)
log_debug_errno(r, "Failed to update hashmap: %m");
return 0;
}
static int update_acquire_finished(sd_bus_message *m, void *userdata, sd_bus_error *error) {
_cleanup_(operation_freep) Operation *op = ASSERT_PTR(userdata);
OrderedHashmap *map = ASSERT_PTR(op->userdata);
uint64_t id;
int r, status;
assert(m);
r = sd_bus_message_read(m, "toi", &id, NULL, &status);
if (r < 0) {
bus_log_parse_error_debug(r);
return 0;
}
if (id != op->job_id) {
TAKE_PTR(op);
return 0;
}
if (status == 0) /* success */
status = UPDATE_PROGRESS_ACQUIRED;
else if (status > 0) /* exit status without errno */
status = UPDATE_PROGRESS_FAILED; /* i.e. EXIT_FAILURE */
/* else errno */
r = ordered_hashmap_replace(map, op->target_id, INT_TO_PTR(status));
if (r < 0)
log_debug_errno(r, "Failed to update hashmap: %m");
if (status != UPDATE_PROGRESS_ACQUIRED)
return 0;
/* Renew the JobRemoved notification for the Install() call instead. */
op->job_finished_slot = sd_bus_slot_unref(op->job_finished_slot);
r = bus_match_signal_async(
op->bus, &op->job_finished_slot, bus_sysupdate_mgr, "JobRemoved", update_install_finished, NULL, op);
if (r < 0)
return log_bus_error(r, NULL, op->target_id, "listen for JobRemoved");
/* With the Acquire() call finished, immediately call Install() to deploy the downloaded update.
* This reuses the same Operation struct so the progress reporting continues to be done in the same
* slot in the terminal. */
r = sd_bus_call_method_async(
op->bus,
NULL,
bus_sysupdate_mgr->destination,
op->target_path,
SYSUPDATE_TARGET_INTERFACE,
"Install",
update_install_started,
op,
"st",
op->requested_version,
0LU);
if (r < 0)
return log_bus_error(r, NULL, op->target_id, "call Install");
TAKE_PTR(op);
return 0;
}
static int update_interrupted(sd_event_source *source, void *userdata) {
/* Since the event loop is exiting, we will never receive the JobRemoved
* signal. So, we must free the userdata here. */
_cleanup_(operation_freep) Operation *op = ASSERT_PTR(userdata);
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
OrderedHashmap *map = ASSERT_PTR(op->userdata);
int r;
/* This call should work regardless of whether we’re cancelling the Acquire() call or the Install()
* call. */
r = sd_bus_call_method(op->bus,
bus_sysupdate_mgr->destination,
op->job_path,
"org.freedesktop.sysupdate1.Job",
"Cancel",
&error,
/* ret_reply= */ NULL,
NULL);
if (r < 0)
return log_bus_error(r, &error, NULL, "call Cancel");
r = ordered_hashmap_replace(map, op->target_id, INT_TO_PTR(-ECANCELED));
if (r < 0)
log_debug_errno(r, "Failed to update hashmap: %m");
return 0;
}
static int update_acquire_started(sd_bus_message *reply, void *userdata, sd_bus_error *ret_error) {
_cleanup_(operation_freep) Operation *op = ASSERT_PTR(userdata);
OrderedHashmap *map = ASSERT_PTR(op->userdata);
const sd_bus_error *e;
_cleanup_free_ char *key = NULL;
const char *new_version, *job_path;
int r;
assert(reply);
e = sd_bus_message_get_error(reply);
if (e) {
r = -sd_bus_error_get_errno(e);
key = strdup(op->target_id);
if (!key)
return log_oom();
r = ordered_hashmap_put(map, key, INT_TO_PTR(r));
if (r < 0)
return log_error_errno(r, "Failed to update hashmap: %m");
TAKE_PTR(key);
return 0;
}
r = sd_bus_message_read(reply, "sto", &new_version, &op->job_id, &job_path);
if (r < 0)
return bus_log_parse_error(r);
op->job_path = strdup(job_path);
if (!op->job_path)
return log_oom();
/* Store the version for the subsequent Install() call */
op->acquired_version = strdup(new_version);
if (!op->acquired_version)
return log_oom();
if (isempty(new_version))
new_version = "latest";
/* Register this job into the hashmap. This will give it a progress bar */
if (strchr(op->target_id, '@'))
key = strdup(op->target_id);
else
key = strjoin(op->target_id, "@", new_version);
if (!key)
return log_oom();
r = ordered_hashmap_put(map, key, INT_TO_PTR(0)); /* takes ownership of key */
if (r < 0)
return log_error_errno(r, "Failed to add target to tracking map: %m");
op->target_id = TAKE_PTR(key); /* just borrowing */
/* Cancel the job if the event loop exits */
r = sd_event_add_exit(op->event, &op->job_interrupt_source, update_interrupted, op);
if (r < 0)
return log_error_errno(r, "Failed to set up interrupt handler: %m");
/* We need to cancel the job before the final iteration of the renderer runs */
r = sd_event_source_set_priority(op->job_interrupt_source, SD_EVENT_PRIORITY_IMPORTANT);
if (r < 0)
return log_error_errno(r, "Failed to set interrupt priority: %m");
/* Register for progress notifications */
r = sd_bus_match_signal_async(
op->bus,
&op->job_properties_slot,
bus_sysupdate_mgr->destination,
job_path,
"org.freedesktop.DBus.Properties",
"PropertiesChanged",
update_properties_changed,
NULL,
op);
if (r < 0)
return log_bus_error(r, NULL, op->target_id, "listen for PropertiesChanged");
TAKE_PTR(op); /* update_acquire_finished/update_interrupted take ownership of the data */
return 0;
}
static int do_update(sd_bus *bus, char **targets) {
_cleanup_(sd_event_unrefp) sd_event *event = NULL;
_cleanup_(sd_event_source_unrefp) sd_event_source *render_exit = NULL;
_cleanup_ordered_hashmap_free_ OrderedHashmap *map = NULL;
_cleanup_strv_free_ char **versions = NULL, **target_paths = NULL;
size_t n;
unsigned remaining = 0;
void *p;
bool did_anything = false;
int r;
assert(bus);
assert(targets);
r = parse_targets(targets, &n, &target_paths, &versions);
if (r < 0)
return log_error_errno(r, "Could not parse targets: %m");
map = ordered_hashmap_new(&string_hash_ops_free);
if (!map)
return log_oom();
r = sd_event_default(&event);
if (r < 0)
return log_error_errno(r, "Failed to get event loop: %m");
r = sd_bus_attach_event(bus, event, 0);
if (r < 0)
return log_error_errno(r, "Failed to attach bus to event loop: %m");
r = sd_event_set_signal_exit(event, true);
if (r < 0)
return log_error_errno(r, "Failed to set up interrupt handler: %m");
for (size_t i = 0; i < n; i++) {
_cleanup_(operation_freep) Operation *op = NULL;
op = operation_new(map, bus, &remaining, target_paths[i], targets[i]);
if (!op)
return log_oom();
/* Sign up for notification when the associated job finishes */
r = bus_match_signal_async(
op->bus, &op->job_finished_slot, bus_sysupdate_mgr, "JobRemoved", update_acquire_finished, NULL, op);
if (r < 0)
return log_bus_error(r, NULL, op->target_id, "listen for JobRemoved");
r = sd_bus_call_method_async(
bus,
NULL,
bus_sysupdate_mgr->destination,
target_paths[i],
SYSUPDATE_TARGET_INTERFACE,
"Acquire",
update_acquire_started,
op,
"st",
versions[i],
0LU);
if (r < 0)
return log_bus_error(r, NULL, targets[i], "call Acquire");
op->requested_version = strdup(versions[i]);
if (!op->requested_version)
return log_oom();
TAKE_PTR(op);
remaining++;
}
/* Set up the rendering */
r = sd_event_add_post(event, NULL, update_render_progress, map);
if (r < 0)
return log_error_errno(r, "Failed to add progress rendering callback: %m");
r = sd_event_add_exit(event, &render_exit, update_render_progress, map);
if (r < 0)
return log_error_errno(r, "Failed to add exit callback: %m");
r = sd_event_source_set_priority(render_exit, SD_EVENT_PRIORITY_IDLE);
if (r < 0)
return log_error_errno(r, "Failed to set priority of update job");
r = sd_event_loop(event);
if (r < 0)
return log_error_errno(r, "Failed to start event loop");
ORDERED_HASHMAP_FOREACH(p, map) {
r = PTR_TO_INT(p);
if (r == -EALREADY)
continue;
if (r == UPDATE_PROGRESS_FAILED)
return EXIT_FAILURE;
if (r < 0)
return r;
did_anything = true;
}
return did_anything ? 1 : 0;
}
VERB(verb_update, "update", "[TARGET[@VERSION]...]\0", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY,
"Install updates");
static int verb_update(int argc, char *argv[], uintptr_t _data, void *userdata) {
sd_bus *bus = ASSERT_PTR(userdata);
_cleanup_strv_free_ char **targets = NULL;
bool did_anything = false;
int r;
r = ensure_targets(bus, argv + 1, &targets);
if (r < 0)
return log_error_errno(r, "Could not find targets: %m");
r = do_update(bus, targets);
if (r < 0)
return r;
if (r > 0)
did_anything = true;
if (!arg_reboot)
return 0;
if (did_anything)
return reboot_now();
log_info("Nothing was updated... skipping reboot.");
return 0;
}
static int do_vacuum(sd_bus *bus, const char *target, const char *path) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
uint32_t count, disabled;
int r;
r = sd_bus_call_method(bus, bus_sysupdate_mgr->destination, path, SYSUPDATE_TARGET_INTERFACE, "Vacuum", &error, &reply, NULL);
if (r < 0)
return log_bus_error(r, &error, target, "call Vacuum");
r = sd_bus_message_read(reply, "uu", &count, &disabled);
if (r < 0)
return bus_log_parse_error(r);
if (count > 0 && disabled > 0)
log_info("Deleted %u instance(s) and %u disabled transfer(s) of %s.",
count, disabled, target);
else if (count > 0)
log_info("Deleted %u instance(s) of %s.", count, target);
else if (disabled > 0)
log_info("Deleted %u disabled transfer(s) of %s.", disabled, target);
else
log_info("Found nothing to delete for %s.", target);
return count + disabled > 0 ? 1 : 0;
}
VERB(verb_vacuum, "vacuum", "[TARGET...]\0", VERB_ANY, VERB_ANY, VERB_ONLINE_ONLY,
"Clean up old updates");
static int verb_vacuum(int argc, char *argv[], uintptr_t _data, void *userdata) {
sd_bus *bus = ASSERT_PTR(userdata);
_cleanup_strv_free_ char **targets = NULL, **target_paths = NULL;
size_t n;
int r;
r = ensure_targets(bus, argv + 1, &targets);
if (r < 0)
return log_error_errno(r, "Failed to find targets: %m");
r = parse_targets(targets, &n, &target_paths, /* ret_versions= */ NULL);
if (r < 0)
return log_error_errno(r, "Failed to parse targets: %m");
for (size_t i = 0; i < n; i++) {
r = do_vacuum(bus, targets[i], target_paths[i]);
if (r < 0)
return r;
}
return 0;
}
typedef struct Feature {
char *id;
char *description;
bool enabled;
int suggested;
char **documentation;
char *appstream;
char **transfers;
} Feature;
#define FEATURE_NULL \
(Feature) { \
.suggested = -1, \
}
static void feature_done(Feature *f) {
assert(f);
f->id = mfree(f->id);
f->description = mfree(f->description);
f->documentation = strv_free(f->documentation);
f->appstream = mfree(f->appstream);
f->transfers = strv_free(f->transfers);
}
static int describe_feature(sd_bus *bus, const char *feature, Feature *ret) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
_cleanup_(feature_done) Feature f = FEATURE_NULL;
char *json;
int r;
static const sd_json_dispatch_field dispatch_table[] = {
{ "id", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(Feature, id), SD_JSON_MANDATORY },
{ "description", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(Feature, description), SD_JSON_NULLABLE },
{ "enabled", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(Feature, enabled), SD_JSON_MANDATORY },
{ "suggested", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_tristate, offsetof(Feature, suggested), SD_JSON_NULLABLE },
{ "documentation", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_strv, offsetof(Feature, documentation), SD_JSON_NULLABLE },
{ "appStream", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(Feature, appstream), SD_JSON_NULLABLE },
{ "transfers", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_strv, offsetof(Feature, transfers), SD_JSON_NULLABLE },
{}
};
assert(bus);
assert(feature);
assert(ret);
r = sd_bus_call_method(bus,
bus_sysupdate_mgr->destination,
SYSUPDATE_HOST_PATH,
SYSUPDATE_TARGET_INTERFACE,
"DescribeFeature",
&error,
&reply,
"st",
feature,
UINT64_C(0));
if (r < 0)
return log_bus_error(r, &error, "host", "lookup feature");
r = sd_bus_message_read_basic(reply, 's', &json);
if (r < 0)
return bus_log_parse_error(r);
r = sd_json_parse(json, 0, &v, NULL, NULL);
if (r < 0)
return log_error_errno(r, "Failed to parse JSON: %m");
r = sd_json_dispatch(v, dispatch_table, SD_JSON_ALLOW_EXTENSIONS, &f);
if (r < 0)
return log_error_errno(r, "Failed to dispatch JSON: %m");
*ret = TAKE_STRUCT(f);
return 0;
}
static int list_features(sd_bus *bus) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
_cleanup_strv_free_ char **features = NULL;
_cleanup_(table_unrefp) Table *table = NULL;
int r;
assert(bus);
table = table_new("", "feature", "description");
if (!table)
return log_oom();
(void) table_set_sort(table, 1);
r = sd_bus_call_method(bus,
bus_sysupdate_mgr->destination,
SYSUPDATE_HOST_PATH,
SYSUPDATE_TARGET_INTERFACE,
"ListFeatures",
&error,
&reply,
"t",
UINT64_C(0));
if (r < 0)
return log_bus_error(r, &error, "host", "lookup feature");
r = sd_bus_message_read_strv(reply, &features);
if (r < 0)
return bus_log_parse_error(r);
STRV_FOREACH(feature, features) {
_cleanup_(feature_done) Feature f = FEATURE_NULL;
_cleanup_free_ char *name_link = NULL;
r = describe_feature(bus, *feature, &f);
if (r < 0)
return r;
if (urlify_enabled() && !strv_isempty(f.documentation)) {
name_link = strjoin(f.id, glyph(GLYPH_EXTERNAL_LINK));
if (!name_link)
return log_oom();
}
r = table_add_many(table,
TABLE_BOOLEAN_CHECKMARK, f.enabled,
TABLE_SET_COLOR, ansi_highlight_green_red(f.enabled),
TABLE_STRING, name_link ?: f.id,
TABLE_SET_URL, strv_isempty(f.documentation) ? NULL : f.documentation[0],
TABLE_STRING, f.description);
if (r < 0)
return table_log_add_error(r);
}
return table_print_with_pager(table, SD_JSON_FORMAT_OFF, arg_pager_flags, arg_legend);
}
VERB(verb_features, "features", "[FEATURE]\0", VERB_ANY, 2, VERB_ONLINE_ONLY,
"List and inspect optional features on host OS");
static int verb_features(int argc, char *argv[], uintptr_t _data, void *userdata) {
sd_bus *bus = ASSERT_PTR(userdata);
_cleanup_(table_unrefp) Table *table = NULL;
_cleanup_(feature_done) Feature f = FEATURE_NULL;
int r;
if (argc == 1)
return list_features(bus);
table = table_new_vertical();
if (!table)
return log_oom();
r = describe_feature(bus, argv[1], &f);
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);
if (f.suggested >= 0) {
r = table_add_many(table,
TABLE_FIELD, "Suggested",
TABLE_BOOLEAN, f.suggested);
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(f.transfers)) {
r = table_add_many(table, TABLE_FIELD, "Transfers", TABLE_STRV_WRAPPED, f.transfers);
if (r < 0)
return table_log_add_error(r);
}
return table_print_with_pager(table, SD_JSON_FORMAT_OFF, arg_pager_flags, false);
}
VERB(verb_enable, "enable", "FEATURE...\0", 2, VERB_ANY, VERB_ONLINE_ONLY,
"Enable optional feature on host OS");
VERB(verb_enable, "disable", "FEATURE...\0", 2, VERB_ANY, VERB_ONLINE_ONLY,
"Disable optional feature on host OS");
static int verb_enable(int argc, char *argv[], uintptr_t _data, void *userdata) {
sd_bus *bus = ASSERT_PTR(userdata);
bool did_anything = false, enable;
char **features;
int r;
enable = streq(argv[0], "enable");
features = strv_skip(argv, 1);
STRV_FOREACH(feature, features) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
r = sd_bus_call_method(bus,
bus_sysupdate_mgr->destination,
SYSUPDATE_HOST_PATH,
SYSUPDATE_TARGET_INTERFACE,
"SetFeatureEnabled",
&error,
/* ret_reply= */ NULL,
"sit",
*feature,
(int) enable,
UINT64_C(0));
if (r < 0)
return log_bus_error(r, &error, "host", "call SetFeatureEnabled");
}
if (!arg_now) /* We weren't asked to apply the changes, so we're done! */
return 0;
if (enable) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
_cleanup_free_ char *target = NULL;
char *version = NULL;
/* We're downloading the new feature into the "current" version, which is either going to be
* the currently booted version or it's going to be a pending update that has already been
* installed and is just waiting for us to reboot into it. */
r = sd_bus_call_method(bus,
bus_sysupdate_mgr->destination,
SYSUPDATE_HOST_PATH,
SYSUPDATE_TARGET_INTERFACE,
"GetVersion",
&error,
&reply,
NULL);
if (r < 0)
return log_bus_error(r, &error, "host", "get current version");
r = sd_bus_message_read_basic(reply, 's', &version);
if (r < 0)
return bus_log_parse_error(r);
target = strjoin("host@", version);
if (!target)
return log_oom();
r = do_update(bus, STRV_MAKE(target));
} else
r = do_vacuum(bus, "host", SYSUPDATE_HOST_PATH);
if (r < 0)
return r;
if (r > 0)
did_anything = true;
if (arg_reboot && did_anything)
return reboot_now();
else if (did_anything)
log_info("Feature(s) %s.", enable ? "downloaded" : "deleted");
else
log_info("Nothing %s%s.",
enable ? "downloaded" : "deleted",
arg_reboot ? ", skipping reboot" :"");
return 0;
}
VERB_COMMON_HELP_AUTO_HIDDEN();
static int parse_argv(int argc, char *argv[], char ***ret_args) {
assert(argc >= 0);
assert(argv);
OptionParser opts = { argc, argv };
FOREACH_OPTION_OR_RETURN(c, &opts)
switch (c) {
OPTION_COMMON_HELP:
return command_print_help();
OPTION_COMMON_VERSION:
return version();
OPTION_LONG("reboot", NULL, "Reboot after updating to newer version"):
arg_reboot = true;
break;
OPTION_LONG("offline", NULL, "Do not fetch metadata from the network"):
arg_offline = true;
break;
OPTION_LONG("now", NULL, "Download/delete resources immediately"):
arg_now = true;
break;
OPTION_COMMON_HOST:
arg_transport = BUS_TRANSPORT_REMOTE;
arg_host = opts.arg;
break;
OPTION_COMMON_NO_PAGER:
arg_pager_flags |= PAGER_DISABLE;
break;
OPTION_COMMON_NO_LEGEND:
arg_legend = false;
break;
OPTION_COMMON_NO_ASK_PASSWORD:
arg_ask_password = false;
break;
OPTION_COMMON_INTROSPECT_CLI:
return introspect_cli(SD_JSON_FORMAT_OFF);
}
*ret_args = option_parser_get_args(&opts);
return 1;
}
static int run(int argc, char *argv[]) {
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
int r;
setlocale(LC_ALL, "");
log_setup();
(void) signal(SIGWINCH, columns_lines_cache_reset);
char **args = NULL;
r = parse_argv(argc, argv, &args);
if (r <= 0)
return r;
r = bus_connect_transport(arg_transport, arg_host, RUNTIME_SCOPE_SYSTEM, &bus);
if (r < 0)
return bus_log_connect_error(r, arg_transport, RUNTIME_SCOPE_SYSTEM);
(void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
(void) sd_bus_set_allow_interactive_authorization(bus, arg_ask_password);
return dispatch_verb(args, bus);
}
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);