4 * Copyright (C) 2019 Red Hat Inc
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "qemu/units.h"
15 #include "qemu/dbus.h"
16 #include "qemu/error-report.h"
17 #include "qapi/error.h"
18 #include "qom/object_interfaces.h"
19 #include "qapi/qmp/qerror.h"
20 #include "migration/vmstate.h"
22 #include "qom/object.h"
24 typedef struct DBusVMState DBusVMState;
25 typedef struct DBusVMStateClass DBusVMStateClass;
27 #define TYPE_DBUS_VMSTATE "dbus-vmstate"
28 #define DBUS_VMSTATE(obj) \
29 OBJECT_CHECK(DBusVMState, (obj), TYPE_DBUS_VMSTATE)
30 #define DBUS_VMSTATE_GET_CLASS(obj) \
31 OBJECT_GET_CLASS(DBusVMStateClass, (obj), TYPE_DBUS_VMSTATE)
32 #define DBUS_VMSTATE_CLASS(klass) \
33 OBJECT_CLASS_CHECK(DBusVMStateClass, (klass), TYPE_DBUS_VMSTATE)
35 struct DBusVMStateClass {
36 ObjectClass parent_class;
50 static const GDBusPropertyInfo vmstate_property_info[] = {
51 { -1, (char *) "Id", (char *) "s",
52 G_DBUS_PROPERTY_INFO_FLAGS_READABLE, NULL },
55 static const GDBusPropertyInfo * const vmstate_property_info_pointers[] = {
56 &vmstate_property_info[0],
60 static const GDBusInterfaceInfo vmstate1_interface_info = {
62 (char *) "org.qemu.VMState1",
63 (GDBusMethodInfo **) NULL,
64 (GDBusSignalInfo **) NULL,
65 (GDBusPropertyInfo **) &vmstate_property_info_pointers,
69 #define DBUS_VMSTATE_SIZE_LIMIT (1 * MiB)
72 get_id_list_set(DBusVMState *self)
74 g_auto(GStrv) ids = NULL;
75 g_autoptr(GHashTable) set = NULL;
82 ids = g_strsplit(self->id_list, ",", -1);
83 set = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
84 for (i = 0; ids[i]; i++) {
85 g_hash_table_add(set, ids[i]);
89 return g_steal_pointer(&set);
93 dbus_get_proxies(DBusVMState *self, GError **err)
95 g_autoptr(GHashTable) proxies = NULL;
96 g_autoptr(GHashTable) ids = NULL;
97 g_auto(GStrv) names = NULL;
101 ids = get_id_list_set(self);
102 proxies = g_hash_table_new_full(g_str_hash, g_str_equal,
103 g_free, g_object_unref);
105 names = qemu_dbus_get_queued_owners(self->bus, "org.qemu.VMState1", &error);
107 g_set_error(err, G_IO_ERROR, G_IO_ERROR_FAILED, "%s",
108 error_get_pretty(error));
113 for (i = 0; names[i]; i++) {
114 g_autoptr(GDBusProxy) proxy = NULL;
115 g_autoptr(GVariant) result = NULL;
116 g_autofree char *id = NULL;
119 proxy = g_dbus_proxy_new_sync(self->bus, G_DBUS_PROXY_FLAGS_NONE,
120 (GDBusInterfaceInfo *) &vmstate1_interface_info,
122 "/org/qemu/VMState1",
129 result = g_dbus_proxy_get_cached_property(proxy, "Id");
131 g_set_error_literal(err, G_IO_ERROR, G_IO_ERROR_FAILED,
132 "VMState Id property is missing.");
136 id = g_variant_dup_string(result, &size);
137 if (ids && !g_hash_table_remove(ids, id)) {
138 g_clear_pointer(&id, g_free);
139 g_clear_object(&proxy);
142 if (size == 0 || size >= 256) {
143 g_set_error(err, G_IO_ERROR, G_IO_ERROR_FAILED,
144 "VMState Id '%s' is invalid.", id);
148 if (!g_hash_table_insert(proxies, id, proxy)) {
149 g_set_error(err, G_IO_ERROR, G_IO_ERROR_FAILED,
150 "Duplicated VMState Id '%s'", id);
156 g_clear_pointer(&result, g_variant_unref);
160 g_autofree char **left = NULL;
162 left = (char **)g_hash_table_get_keys_as_array(ids, NULL);
164 g_autofree char *leftids = g_strjoinv(",", left);
165 g_set_error(err, G_IO_ERROR, G_IO_ERROR_FAILED,
166 "Required VMState Id are missing: %s", leftids);
171 return g_steal_pointer(&proxies);
175 dbus_load_state_proxy(GDBusProxy *proxy, const uint8_t *data, size_t size)
177 g_autoptr(GError) err = NULL;
178 g_autoptr(GVariant) result = NULL;
179 g_autoptr(GVariant) value = NULL;
181 value = g_variant_new_fixed_array(G_VARIANT_TYPE_BYTE,
182 data, size, sizeof(char));
183 result = g_dbus_proxy_call_sync(proxy, "Load",
184 g_variant_new("(@ay)",
185 g_steal_pointer(&value)),
186 G_DBUS_CALL_FLAGS_NO_AUTO_START,
189 error_report("%s: Failed to Load: %s", __func__, err->message);
196 static int dbus_vmstate_post_load(void *opaque, int version_id)
198 DBusVMState *self = DBUS_VMSTATE(opaque);
199 g_autoptr(GInputStream) m = NULL;
200 g_autoptr(GDataInputStream) s = NULL;
201 g_autoptr(GError) err = NULL;
202 g_autoptr(GHashTable) proxies = NULL;
205 trace_dbus_vmstate_post_load(version_id);
207 proxies = dbus_get_proxies(self, &err);
209 error_report("%s: Failed to get proxies: %s", __func__, err->message);
213 m = g_memory_input_stream_new_from_data(self->data, self->data_size, NULL);
214 s = g_data_input_stream_new(m);
215 g_data_input_stream_set_byte_order(s, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
217 nelem = g_data_input_stream_read_uint32(s, NULL, &err);
223 GDBusProxy *proxy = NULL;
225 gsize bytes_read, avail;
228 len = g_data_input_stream_read_uint32(s, NULL, &err);
233 error_report("%s: Invalid DBus vmstate proxy name %u",
237 if (!g_input_stream_read_all(G_INPUT_STREAM(s), id, len,
238 &bytes_read, NULL, &err)) {
241 g_return_val_if_fail(bytes_read == len, -1);
244 trace_dbus_vmstate_loading(id);
246 proxy = g_hash_table_lookup(proxies, id);
248 error_report("%s: Failed to find proxy Id '%s'", __func__, id);
252 len = g_data_input_stream_read_uint32(s, NULL, &err);
253 avail = g_buffered_input_stream_get_available(
254 G_BUFFERED_INPUT_STREAM(s));
256 if (len > DBUS_VMSTATE_SIZE_LIMIT || len > avail) {
257 error_report("%s: Invalid vmstate size: %u", __func__, len);
261 if (dbus_load_state_proxy(proxy,
262 g_buffered_input_stream_peek_buffer(G_BUFFERED_INPUT_STREAM(s),
265 error_report("%s: Failed to restore Id '%s'", __func__, id);
269 if (!g_seekable_seek(G_SEEKABLE(s), len, G_SEEK_CUR, NULL, &err)) {
279 error_report("%s: Failed to read from stream: %s", __func__, err->message);
284 dbus_save_state_proxy(gpointer key,
288 GDataOutputStream *s = user_data;
289 const char *id = key;
290 GDBusProxy *proxy = value;
291 g_autoptr(GVariant) result = NULL;
292 g_autoptr(GVariant) child = NULL;
293 g_autoptr(GError) err = NULL;
297 trace_dbus_vmstate_saving(id);
299 result = g_dbus_proxy_call_sync(proxy, "Save",
300 NULL, G_DBUS_CALL_FLAGS_NO_AUTO_START,
303 error_report("%s: Failed to Save: %s", __func__, err->message);
307 child = g_variant_get_child_value(result, 0);
308 data = g_variant_get_fixed_array(child, &size, sizeof(char));
310 error_report("%s: Failed to Save: not a byte array", __func__);
313 if (size > DBUS_VMSTATE_SIZE_LIMIT) {
314 error_report("%s: Too large vmstate data to save: %zu",
315 __func__, (size_t)size);
319 if (!g_data_output_stream_put_uint32(s, strlen(id), NULL, &err) ||
320 !g_data_output_stream_put_string(s, id, NULL, &err) ||
321 !g_data_output_stream_put_uint32(s, size, NULL, &err) ||
322 !g_output_stream_write_all(G_OUTPUT_STREAM(s),
323 data, size, NULL, NULL, &err)) {
324 error_report("%s: Failed to write to stream: %s",
325 __func__, err->message);
329 static int dbus_vmstate_pre_save(void *opaque)
331 DBusVMState *self = DBUS_VMSTATE(opaque);
332 g_autoptr(GOutputStream) m = NULL;
333 g_autoptr(GDataOutputStream) s = NULL;
334 g_autoptr(GHashTable) proxies = NULL;
335 g_autoptr(GError) err = NULL;
337 trace_dbus_vmstate_pre_save();
339 proxies = dbus_get_proxies(self, &err);
341 error_report("%s: Failed to get proxies: %s", __func__, err->message);
345 m = g_memory_output_stream_new_resizable();
346 s = g_data_output_stream_new(m);
347 g_data_output_stream_set_byte_order(s, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
349 if (!g_data_output_stream_put_uint32(s, g_hash_table_size(proxies),
351 error_report("%s: Failed to write to stream: %s",
352 __func__, err->message);
356 g_hash_table_foreach(proxies, dbus_save_state_proxy, s);
358 if (g_memory_output_stream_get_size(G_MEMORY_OUTPUT_STREAM(m))
360 error_report("%s: DBus vmstate buffer is too large", __func__);
364 if (!g_output_stream_close(G_OUTPUT_STREAM(m), NULL, &err)) {
365 error_report("%s: Failed to close stream: %s", __func__, err->message);
371 g_memory_output_stream_get_size(G_MEMORY_OUTPUT_STREAM(m));
373 g_memory_output_stream_steal_data(G_MEMORY_OUTPUT_STREAM(m));
378 static const VMStateDescription dbus_vmstate = {
379 .name = TYPE_DBUS_VMSTATE,
381 .pre_save = dbus_vmstate_pre_save,
382 .post_load = dbus_vmstate_post_load,
383 .fields = (VMStateField[]) {
384 VMSTATE_UINT32(data_size, DBusVMState),
385 VMSTATE_VBUFFER_ALLOC_UINT32(data, DBusVMState, 0, 0, data_size),
386 VMSTATE_END_OF_LIST()
391 dbus_vmstate_complete(UserCreatable *uc, Error **errp)
393 DBusVMState *self = DBUS_VMSTATE(uc);
394 g_autoptr(GError) err = NULL;
396 if (!object_resolve_path_type("", TYPE_DBUS_VMSTATE, NULL)) {
397 error_setg(errp, "There is already an instance of %s",
402 if (!self->dbus_addr) {
403 error_setg(errp, QERR_MISSING_PARAMETER, "addr");
407 self->bus = g_dbus_connection_new_for_address_sync(self->dbus_addr,
408 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
409 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
412 error_setg(errp, "failed to connect to DBus: '%s'", err->message);
416 if (vmstate_register(VMSTATE_IF(self), VMSTATE_INSTANCE_ID_ANY,
417 &dbus_vmstate, self) < 0) {
418 error_setg(errp, "Failed to register vmstate");
423 dbus_vmstate_finalize(Object *o)
425 DBusVMState *self = DBUS_VMSTATE(o);
427 vmstate_unregister(VMSTATE_IF(self), &dbus_vmstate, self);
429 g_clear_object(&self->bus);
430 g_free(self->dbus_addr);
431 g_free(self->id_list);
436 get_dbus_addr(Object *o, Error **errp)
438 DBusVMState *self = DBUS_VMSTATE(o);
440 return g_strdup(self->dbus_addr);
444 set_dbus_addr(Object *o, const char *str, Error **errp)
446 DBusVMState *self = DBUS_VMSTATE(o);
448 g_free(self->dbus_addr);
449 self->dbus_addr = g_strdup(str);
453 get_id_list(Object *o, Error **errp)
455 DBusVMState *self = DBUS_VMSTATE(o);
457 return g_strdup(self->id_list);
461 set_id_list(Object *o, const char *str, Error **errp)
463 DBusVMState *self = DBUS_VMSTATE(o);
465 g_free(self->id_list);
466 self->id_list = g_strdup(str);
470 dbus_vmstate_get_id(VMStateIf *vmif)
472 return g_strdup(TYPE_DBUS_VMSTATE);
476 dbus_vmstate_class_init(ObjectClass *oc, void *data)
478 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
479 VMStateIfClass *vc = VMSTATE_IF_CLASS(oc);
481 ucc->complete = dbus_vmstate_complete;
482 vc->get_id = dbus_vmstate_get_id;
484 object_class_property_add_str(oc, "addr",
485 get_dbus_addr, set_dbus_addr);
486 object_class_property_add_str(oc, "id-list",
487 get_id_list, set_id_list);
490 static const TypeInfo dbus_vmstate_info = {
491 .name = TYPE_DBUS_VMSTATE,
492 .parent = TYPE_OBJECT,
493 .instance_size = sizeof(DBusVMState),
494 .instance_finalize = dbus_vmstate_finalize,
495 .class_size = sizeof(DBusVMStateClass),
496 .class_init = dbus_vmstate_class_init,
497 .interfaces = (InterfaceInfo[]) {
498 { TYPE_USER_CREATABLE },
507 type_register_static(&dbus_vmstate_info);
510 type_init(register_types);