128 lines
6.5 KiB
C
128 lines
6.5 KiB
C
/* SPDX-License-Identifier: GPL-2.0-or-later
|
|
* Called from GTask worker threads only. No privileged subprocess or state file.
|
|
*/
|
|
#define ARCH_BUS "org.archipelago.System1"
|
|
#define ARCH_PATH "/org/archipelago/System1"
|
|
#define ARCH_MANAGER ARCH_BUS ".Manager"
|
|
#define ARCH_TRANSACTION ARCH_BUS ".Transaction"
|
|
|
|
static const gchar *bus_string(GVariant *map, const gchar *key)
|
|
{
|
|
const gchar *s = "";
|
|
if (map) g_variant_lookup(map, key, "&s", &s);
|
|
return s;
|
|
}
|
|
static GVariant *bus_options(gboolean interactive)
|
|
{
|
|
GVariantBuilder b;
|
|
g_autofree gchar *id = g_uuid_string_random();
|
|
g_variant_builder_init(&b, G_VARIANT_TYPE_VARDICT);
|
|
g_variant_builder_add(&b, "{sv}", "request_id", g_variant_new_string(id));
|
|
g_variant_builder_add(&b, "{sv}", "interactive", g_variant_new_boolean(interactive));
|
|
return g_variant_builder_end(&b);
|
|
}
|
|
static GVariant *bus_call(GDBusConnection *bus, const gchar *path, const gchar *iface,
|
|
const gchar *method, GVariant *body, gboolean interactive, GError **error)
|
|
{
|
|
return g_dbus_connection_call_sync(bus, ARCH_BUS, path, iface, method, body, NULL,
|
|
interactive ? G_DBUS_CALL_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION : G_DBUS_CALL_FLAGS_NONE,
|
|
120000, NULL, error);
|
|
}
|
|
static GVariant *bus_operation(GDBusConnection *bus, const gchar *method, const gchar *target,
|
|
gboolean interactive, GCancellable *cancellable, GError **error)
|
|
{
|
|
g_autoptr(GVariant) admitted = bus_call(bus, ARCH_PATH, ARCH_MANAGER, method,
|
|
target ? g_variant_new("(s@a{sv})", target, bus_options(interactive))
|
|
: g_variant_new("(@a{sv})", bus_options(interactive)), interactive, error);
|
|
if (!admitted) return NULL;
|
|
const gchar *path;
|
|
g_variant_get(admitted, "(&o)", &path);
|
|
gboolean cancel_sent = FALSE;
|
|
guint failures = 0;
|
|
gint64 deadline = g_get_monotonic_time() + 4 * G_TIME_SPAN_HOUR;
|
|
for (;;) {
|
|
g_autoptr(GError) transient = NULL;
|
|
g_autoptr(GVariant) snapshot = bus_call(bus, path, ARCH_TRANSACTION, "GetSnapshot", NULL, FALSE, &transient);
|
|
if (!snapshot) {
|
|
/* Re-expose the durable object after daemon restart. Never replay Stage. */
|
|
const gchar *id = strrchr(path, '/') + 4;
|
|
g_autoptr(GVariant) exposed = bus_call(bus, ARCH_PATH, ARCH_MANAGER, "GetTransaction",
|
|
g_variant_new("(s)", id), FALSE, NULL);
|
|
if (++failures > 20) { g_propagate_error(error, g_steal_pointer(&transient)); return NULL; }
|
|
g_usleep(500000);
|
|
continue;
|
|
}
|
|
failures = 0;
|
|
g_autoptr(GVariant) fields = g_variant_get_child_value(snapshot, 0);
|
|
const gchar *state = bus_string(fields, "State");
|
|
if (g_str_equal(state, "succeeded"))
|
|
return g_variant_lookup_value(fields, "Result", G_VARIANT_TYPE_VARDICT);
|
|
if (g_str_equal(state, "failed") || g_str_equal(state, "cancelled") || g_str_equal(state, "interrupted")) {
|
|
g_autoptr(GVariant) detail = g_variant_lookup_value(fields, "Error", G_VARIANT_TYPE_VARDICT);
|
|
g_set_error(error, G_IO_ERROR, G_IO_ERROR_FAILED, "Operation %s: %s", path, bus_string(detail, "message"));
|
|
return NULL;
|
|
}
|
|
gboolean can_cancel = FALSE;
|
|
g_variant_lookup(fields, "CanCancel", "b", &can_cancel);
|
|
if (cancellable && g_cancellable_is_cancelled(cancellable) && can_cancel && !cancel_sent) {
|
|
GVariantBuilder options;
|
|
g_variant_builder_init(&options, G_VARIANT_TYPE_VARDICT);
|
|
g_variant_builder_add(&options, "{sv}", "interactive", g_variant_new_boolean(interactive));
|
|
g_autoptr(GVariant) cancelled = bus_call(bus, path, ARCH_TRANSACTION, "Cancel",
|
|
g_variant_new("(a{sv})", &options), interactive, error);
|
|
if (!cancelled) return NULL;
|
|
cancel_sent = TRUE;
|
|
}
|
|
if (g_get_monotonic_time() > deadline) {
|
|
g_set_error(error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT, "Operation %s remains owned by the system service", path);
|
|
return NULL;
|
|
}
|
|
g_usleep(250000);
|
|
}
|
|
}
|
|
static JsonObject *bus_status(GError **error)
|
|
{
|
|
g_autoptr(GDBusConnection) bus = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, error);
|
|
if (!bus) return NULL;
|
|
g_autoptr(GVariant) reply = bus_call(bus, ARCH_PATH, ARCH_MANAGER, "GetStatus", NULL, FALSE, error);
|
|
if (!reply) return NULL;
|
|
g_autoptr(GVariant) status = g_variant_get_child_value(reply, 0);
|
|
g_autoptr(GVariant) pending = g_variant_lookup_value(status, "pending", G_VARIANT_TYPE_VARDICT);
|
|
g_autoptr(GVariant) booted = g_variant_lookup_value(status, "booted", G_VARIANT_TYPE_VARDICT);
|
|
g_autoptr(GVariant) update = g_variant_lookup_value(status, "update", G_VARIANT_TYPE_VARDICT);
|
|
JsonObject *record = json_object_new();
|
|
gboolean reconciling = FALSE;
|
|
g_variant_lookup(status, "reconciling", "b", &reconciling);
|
|
if (reconciling) {
|
|
json_object_set_string_member(record, "state", "error");
|
|
json_object_set_string_member(record, "error", "System state is being reconciled; retry when ready.");
|
|
} else if (*bus_string(pending, "deployment_id")) {
|
|
json_object_set_string_member(record, "state", "pending");
|
|
json_object_set_string_member(record, "pending_id", bus_string(pending, "deployment_id"));
|
|
json_object_set_string_member(record, "pending_checksum", bus_string(pending, "checksum"));
|
|
} else {
|
|
const gchar *state = bus_string(update, "state");
|
|
json_object_set_string_member(record, "state", *state ? state : "unknown");
|
|
json_object_set_string_member(record, "target", bus_string(update, "target"));
|
|
JsonObject *plan = json_object_new();
|
|
json_object_set_string_member(plan, "base", bus_string(booted, "checksum"));
|
|
json_object_set_string_member(plan, "target", bus_string(update, "target"));
|
|
g_autoptr(GVariant) details = update ? g_variant_lookup_value(update, "details", G_VARIANT_TYPE_VARDICT) : NULL;
|
|
g_autoptr(GVariant) changes = details ? g_variant_lookup_value(details, "package_changes", G_VARIANT_TYPE("aa{sv}")) : NULL;
|
|
JsonArray *array = json_array_new();
|
|
if (changes) for (gsize i = 0; i < g_variant_n_children(changes); i++) {
|
|
g_autoptr(GVariant) item = g_variant_get_child_value(changes, i);
|
|
JsonObject *row = json_object_new();
|
|
const gchar *keys[] = {"name", "old_version", "new_version"};
|
|
for (guint k = 0; k < G_N_ELEMENTS(keys); k++) {
|
|
const gchar *value = bus_string(item, keys[k]);
|
|
if (*value) json_object_set_string_member(row, keys[k], value);
|
|
else json_object_set_null_member(row, keys[k]);
|
|
}
|
|
json_array_add_object_element(array, row);
|
|
}
|
|
json_object_set_array_member(plan, "package_changes", array);
|
|
json_object_set_object_member(record, "plan", plan);
|
|
}
|
|
return record;
|
|
}
|