blob: 75d1d48944384492a01a43a0371f29dcc9222ed4 [file] [log] [blame]
Anthony Liguori2f28d2f2011-12-03 17:10:08 -06001/*
2 * QEMU Object Model
3 *
4 * Copyright IBM, Corp. 2011
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
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.
11 */
12
Peter Maydell9bbc8532016-01-29 17:50:02 +000013#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010014#include "qapi/error.h"
Paolo Bonzini14cccb62012-12-17 18:19:50 +010015#include "qom/object.h"
Daniel P. Berrangea31bdae2015-05-13 17:14:06 +010016#include "qom/object_interfaces.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020017#include "qemu/cutils.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010018#include "qapi/visitor.h"
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +010019#include "qapi/string-input-visitor.h"
20#include "qapi/string-output-visitor.h"
Markus Armbrustereb815e22018-02-11 10:36:05 +010021#include "qapi/qapi-builtin-visit.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010022#include "qapi/qmp/qerror.h"
Paolo Bonzinifa131d92013-05-10 14:16:39 +020023#include "trace.h"
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060024
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +010025/* TODO: replace QObject with a simpler visitor to avoid a dependency
26 * of the QOM core on QObject? */
Paolo Bonzini14cccb62012-12-17 18:19:50 +010027#include "qom/qom-qobject.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010028#include "qapi/qmp/qbool.h"
Markus Armbruster15280c32018-02-01 12:18:36 +010029#include "qapi/qmp/qnum.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010030#include "qapi/qmp/qstring.h"
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +010031
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060032#define MAX_INTERFACES 32
33
34typedef struct InterfaceImpl InterfaceImpl;
35typedef struct TypeImpl TypeImpl;
36
37struct InterfaceImpl
38{
Anthony Liguori33e95c62012-08-10 13:16:10 +100039 const char *typename;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060040};
41
42struct TypeImpl
43{
44 const char *name;
45
46 size_t class_size;
47
48 size_t instance_size;
49
50 void (*class_init)(ObjectClass *klass, void *data);
Paolo Bonzini3b50e312012-05-02 13:30:55 +020051 void (*class_base_init)(ObjectClass *klass, void *data);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060052 void (*class_finalize)(ObjectClass *klass, void *data);
53
54 void *class_data;
55
56 void (*instance_init)(Object *obj);
Eduardo Habkost8231c2d2013-07-10 17:08:41 -030057 void (*instance_post_init)(Object *obj);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060058 void (*instance_finalize)(Object *obj);
59
60 bool abstract;
61
62 const char *parent;
63 TypeImpl *parent_type;
64
65 ObjectClass *class;
66
67 int num_interfaces;
68 InterfaceImpl interfaces[MAX_INTERFACES];
69};
70
Paolo Bonzini9970bd82012-02-03 11:51:39 +010071static Type type_interface;
72
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060073static GHashTable *type_table_get(void)
74{
75 static GHashTable *type_table;
76
77 if (type_table == NULL) {
78 type_table = g_hash_table_new(g_str_hash, g_str_equal);
79 }
80
81 return type_table;
82}
83
Hervé Poussineauf54c19c2013-12-03 16:42:00 +010084static bool enumerating_types;
85
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060086static void type_table_add(TypeImpl *ti)
87{
Hervé Poussineauf54c19c2013-12-03 16:42:00 +010088 assert(!enumerating_types);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060089 g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
90}
91
92static TypeImpl *type_table_lookup(const char *name)
93{
94 return g_hash_table_lookup(type_table_get(), name);
95}
96
Paolo Bonzinib061dc42013-12-03 16:41:59 +010097static TypeImpl *type_new(const TypeInfo *info)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060098{
99 TypeImpl *ti = g_malloc0(sizeof(*ti));
Anthony Liguori33e95c62012-08-10 13:16:10 +1000100 int i;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600101
102 g_assert(info->name != NULL);
103
Anthony Liguori73093352012-01-25 13:37:36 -0600104 if (type_table_lookup(info->name) != NULL) {
105 fprintf(stderr, "Registering `%s' which already exists\n", info->name);
106 abort();
107 }
108
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600109 ti->name = g_strdup(info->name);
110 ti->parent = g_strdup(info->parent);
111
112 ti->class_size = info->class_size;
113 ti->instance_size = info->instance_size;
114
115 ti->class_init = info->class_init;
Paolo Bonzini3b50e312012-05-02 13:30:55 +0200116 ti->class_base_init = info->class_base_init;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600117 ti->class_finalize = info->class_finalize;
118 ti->class_data = info->class_data;
119
120 ti->instance_init = info->instance_init;
Eduardo Habkost8231c2d2013-07-10 17:08:41 -0300121 ti->instance_post_init = info->instance_post_init;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600122 ti->instance_finalize = info->instance_finalize;
123
124 ti->abstract = info->abstract;
125
Anthony Liguori33e95c62012-08-10 13:16:10 +1000126 for (i = 0; info->interfaces && info->interfaces[i].type; i++) {
127 ti->interfaces[i].typename = g_strdup(info->interfaces[i].type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600128 }
Anthony Liguori33e95c62012-08-10 13:16:10 +1000129 ti->num_interfaces = i;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600130
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100131 return ti;
132}
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600133
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100134static TypeImpl *type_register_internal(const TypeInfo *info)
135{
136 TypeImpl *ti;
137 ti = type_new(info);
138
139 type_table_add(ti);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600140 return ti;
141}
142
Paolo Bonzini049cb3c2012-04-04 15:58:40 +0200143TypeImpl *type_register(const TypeInfo *info)
144{
145 assert(info->parent);
146 return type_register_internal(info);
147}
148
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600149TypeImpl *type_register_static(const TypeInfo *info)
150{
151 return type_register(info);
152}
153
Igor Mammedovaa04c9d22017-10-09 21:50:49 +0200154void type_register_static_array(const TypeInfo *infos, int nr_infos)
155{
156 int i;
157
158 for (i = 0; i < nr_infos; i++) {
159 type_register_static(&infos[i]);
160 }
161}
162
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600163static TypeImpl *type_get_by_name(const char *name)
164{
165 if (name == NULL) {
166 return NULL;
167 }
168
169 return type_table_lookup(name);
170}
171
172static TypeImpl *type_get_parent(TypeImpl *type)
173{
174 if (!type->parent_type && type->parent) {
175 type->parent_type = type_get_by_name(type->parent);
176 g_assert(type->parent_type != NULL);
177 }
178
179 return type->parent_type;
180}
181
182static bool type_has_parent(TypeImpl *type)
183{
184 return (type->parent != NULL);
185}
186
187static size_t type_class_get_size(TypeImpl *ti)
188{
189 if (ti->class_size) {
190 return ti->class_size;
191 }
192
193 if (type_has_parent(ti)) {
194 return type_class_get_size(type_get_parent(ti));
195 }
196
197 return sizeof(ObjectClass);
198}
199
Igor Mitsyankoaca59af2012-02-28 15:57:10 +0400200static size_t type_object_get_size(TypeImpl *ti)
201{
202 if (ti->instance_size) {
203 return ti->instance_size;
204 }
205
206 if (type_has_parent(ti)) {
207 return type_object_get_size(type_get_parent(ti));
208 }
209
210 return 0;
211}
212
Bharata B Rao3f97b532016-06-10 06:29:00 +0530213size_t object_type_get_instance_size(const char *typename)
214{
215 TypeImpl *type = type_get_by_name(typename);
216
217 g_assert(type != NULL);
218 return type_object_get_size(type);
219}
220
Anthony Liguori33e95c62012-08-10 13:16:10 +1000221static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600222{
Anthony Liguori33e95c62012-08-10 13:16:10 +1000223 assert(target_type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600224
Cao jinb30d8052015-11-03 10:36:42 +0800225 /* Check if target_type is a direct ancestor of type */
Anthony Liguori33e95c62012-08-10 13:16:10 +1000226 while (type) {
227 if (type == target_type) {
228 return true;
229 }
230
231 type = type_get_parent(type);
232 }
233
234 return false;
235}
236
237static void type_initialize(TypeImpl *ti);
238
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100239static void type_initialize_interface(TypeImpl *ti, TypeImpl *interface_type,
240 TypeImpl *parent_type)
Anthony Liguori33e95c62012-08-10 13:16:10 +1000241{
242 InterfaceClass *new_iface;
243 TypeInfo info = { };
244 TypeImpl *iface_impl;
245
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100246 info.parent = parent_type->name;
247 info.name = g_strdup_printf("%s::%s", ti->name, interface_type->name);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000248 info.abstract = true;
249
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100250 iface_impl = type_new(&info);
251 iface_impl->parent_type = parent_type;
Anthony Liguori33e95c62012-08-10 13:16:10 +1000252 type_initialize(iface_impl);
253 g_free((char *)info.name);
254
255 new_iface = (InterfaceClass *)iface_impl->class;
256 new_iface->concrete_class = ti->class;
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100257 new_iface->interface_type = interface_type;
Anthony Liguori33e95c62012-08-10 13:16:10 +1000258
259 ti->class->interfaces = g_slist_append(ti->class->interfaces,
260 iface_impl->class);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600261}
262
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +0100263static void object_property_free(gpointer data)
264{
265 ObjectProperty *prop = data;
266
267 g_free(prop->name);
268 g_free(prop->type);
269 g_free(prop->description);
270 g_free(prop);
271}
272
Igor Mitsyankoac451032012-02-28 15:57:11 +0400273static void type_initialize(TypeImpl *ti)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600274{
Paolo Bonzini745549c2012-03-31 16:45:54 +0200275 TypeImpl *parent;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600276
277 if (ti->class) {
278 return;
279 }
280
281 ti->class_size = type_class_get_size(ti);
Igor Mitsyankoaca59af2012-02-28 15:57:10 +0400282 ti->instance_size = type_object_get_size(ti);
Eduardo Habkost1c6d75d2016-12-12 16:31:01 -0200283 /* Any type with zero instance_size is implicitly abstract.
284 * This means interface types are all abstract.
285 */
286 if (ti->instance_size == 0) {
287 ti->abstract = true;
288 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600289
290 ti->class = g_malloc0(ti->class_size);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600291
Paolo Bonzini745549c2012-03-31 16:45:54 +0200292 parent = type_get_parent(ti);
293 if (parent) {
Igor Mitsyankoac451032012-02-28 15:57:11 +0400294 type_initialize(parent);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000295 GSList *e;
296 int i;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600297
Markus Armbruster719a3072018-06-08 19:02:31 +0200298 g_assert(parent->class_size <= ti->class_size);
Paolo Bonzini745549c2012-03-31 16:45:54 +0200299 memcpy(ti->class, parent->class, parent->class_size);
Peter Crosthwaite3e407de2013-02-19 14:02:09 +1000300 ti->class->interfaces = NULL;
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +0100301 ti->class->properties = g_hash_table_new_full(
302 g_str_hash, g_str_equal, g_free, object_property_free);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000303
304 for (e = parent->class->interfaces; e; e = e->next) {
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100305 InterfaceClass *iface = e->data;
306 ObjectClass *klass = OBJECT_CLASS(iface);
307
308 type_initialize_interface(ti, iface->interface_type, klass->type);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000309 }
310
311 for (i = 0; i < ti->num_interfaces; i++) {
312 TypeImpl *t = type_get_by_name(ti->interfaces[i].typename);
313 for (e = ti->class->interfaces; e; e = e->next) {
314 TypeImpl *target_type = OBJECT_CLASS(e->data)->type;
315
316 if (type_is_ancestor(target_type, t)) {
317 break;
318 }
319 }
320
321 if (e) {
322 continue;
323 }
324
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100325 type_initialize_interface(ti, t, t);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000326 }
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +0100327 } else {
328 ti->class->properties = g_hash_table_new_full(
329 g_str_hash, g_str_equal, g_free, object_property_free);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600330 }
331
Paolo Bonzini745549c2012-03-31 16:45:54 +0200332 ti->class->type = ti;
333
334 while (parent) {
335 if (parent->class_base_init) {
336 parent->class_base_init(ti->class, ti->class_data);
337 }
338 parent = type_get_parent(parent);
339 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600340
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600341 if (ti->class_init) {
342 ti->class_init(ti->class, ti->class_data);
343 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600344}
345
346static void object_init_with_type(Object *obj, TypeImpl *ti)
347{
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600348 if (type_has_parent(ti)) {
349 object_init_with_type(obj, type_get_parent(ti));
350 }
351
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600352 if (ti->instance_init) {
353 ti->instance_init(obj);
354 }
355}
356
Eduardo Habkost8231c2d2013-07-10 17:08:41 -0300357static void object_post_init_with_type(Object *obj, TypeImpl *ti)
358{
359 if (ti->instance_post_init) {
360 ti->instance_post_init(obj);
361 }
362
363 if (type_has_parent(ti)) {
364 object_post_init_with_type(obj, type_get_parent(ti));
365 }
366}
367
Marc-André Lureau63f7b102016-12-12 20:31:51 +0300368static void object_initialize_with_type(void *data, size_t size, TypeImpl *type)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600369{
370 Object *obj = data;
371
372 g_assert(type != NULL);
Igor Mitsyankoac451032012-02-28 15:57:11 +0400373 type_initialize(type);
Igor Mitsyankoaca59af2012-02-28 15:57:10 +0400374
Markus Armbruster719a3072018-06-08 19:02:31 +0200375 g_assert(type->instance_size >= sizeof(Object));
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600376 g_assert(type->abstract == false);
Markus Armbruster719a3072018-06-08 19:02:31 +0200377 g_assert(size >= type->instance_size);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600378
379 memset(obj, 0, type->instance_size);
380 obj->class = type->class;
Paolo Bonzini764b6312012-11-23 09:47:12 +0100381 object_ref(obj);
Pavel Fedinb604a852015-10-13 13:37:45 +0100382 obj->properties = g_hash_table_new_full(g_str_hash, g_str_equal,
383 NULL, object_property_free);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600384 object_init_with_type(obj, type);
Eduardo Habkost8231c2d2013-07-10 17:08:41 -0300385 object_post_init_with_type(obj, type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600386}
387
Andreas Färber213f0c42013-08-23 19:37:12 +0200388void object_initialize(void *data, size_t size, const char *typename)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600389{
390 TypeImpl *type = type_get_by_name(typename);
391
Andreas Färber5b9237f2013-08-30 18:28:37 +0200392 object_initialize_with_type(data, size, type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600393}
394
Thomas Huth0210b392018-07-16 14:59:18 +0200395void object_initialize_child(Object *parentobj, const char *propname,
396 void *childobj, size_t size, const char *type,
397 Error **errp, ...)
398{
399 va_list vargs;
400
401 va_start(vargs, errp);
402 object_initialize_childv(parentobj, propname, childobj, size, type, errp,
403 vargs);
404 va_end(vargs);
405}
406
407void object_initialize_childv(Object *parentobj, const char *propname,
408 void *childobj, size_t size, const char *type,
409 Error **errp, va_list vargs)
410{
411 Error *local_err = NULL;
412 Object *obj;
413
414 object_initialize(childobj, size, type);
415 obj = OBJECT(childobj);
416
417 object_set_propv(obj, &local_err, vargs);
418 if (local_err) {
419 goto out;
420 }
421
422 object_property_add_child(parentobj, propname, obj, &local_err);
423 if (local_err) {
424 goto out;
425 }
426
427 if (object_dynamic_cast(obj, TYPE_USER_CREATABLE)) {
428 user_creatable_complete(obj, &local_err);
429 if (local_err) {
430 object_unparent(obj);
431 goto out;
432 }
433 }
434
435 /*
436 * Since object_property_add_child added a reference to the child object,
437 * we can drop the reference added by object_initialize(), so the child
438 * property will own the only reference to the object.
439 */
440 object_unref(obj);
441
442out:
443 if (local_err) {
444 error_propagate(errp, local_err);
445 object_unref(obj);
446 }
447}
448
Andreas Färber5d9d3f42012-05-27 00:32:40 +0200449static inline bool object_property_is_child(ObjectProperty *prop)
450{
451 return strstart(prop->type, "child<", NULL);
452}
453
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600454static void object_property_del_all(Object *obj)
455{
Pavel Fedinb604a852015-10-13 13:37:45 +0100456 ObjectProperty *prop;
457 GHashTableIter iter;
458 gpointer key, value;
459 bool released;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600460
Pavel Fedinb604a852015-10-13 13:37:45 +0100461 do {
462 released = false;
463 g_hash_table_iter_init(&iter, obj->properties);
464 while (g_hash_table_iter_next(&iter, &key, &value)) {
465 prop = value;
466 if (prop->release) {
467 prop->release(obj, prop->name, prop->opaque);
468 prop->release = NULL;
469 released = true;
470 break;
471 }
472 g_hash_table_iter_remove(&iter);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600473 }
Pavel Fedinb604a852015-10-13 13:37:45 +0100474 } while (released);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600475
Pavel Fedinb604a852015-10-13 13:37:45 +0100476 g_hash_table_unref(obj->properties);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600477}
478
479static void object_property_del_child(Object *obj, Object *child, Error **errp)
480{
481 ObjectProperty *prop;
Pavel Fedinb604a852015-10-13 13:37:45 +0100482 GHashTableIter iter;
483 gpointer key, value;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600484
Pavel Fedinb604a852015-10-13 13:37:45 +0100485 g_hash_table_iter_init(&iter, obj->properties);
486 while (g_hash_table_iter_next(&iter, &key, &value)) {
487 prop = value;
Andreas Färber5d9d3f42012-05-27 00:32:40 +0200488 if (object_property_is_child(prop) && prop->opaque == child) {
Pavel Fedinb604a852015-10-13 13:37:45 +0100489 if (prop->release) {
490 prop->release(obj, prop->name, prop->opaque);
491 prop->release = NULL;
492 }
493 break;
494 }
495 }
496 g_hash_table_iter_init(&iter, obj->properties);
497 while (g_hash_table_iter_next(&iter, &key, &value)) {
498 prop = value;
499 if (object_property_is_child(prop) && prop->opaque == child) {
500 g_hash_table_iter_remove(&iter);
Paolo Bonzini6c1fdcf2012-02-28 09:54:15 +0100501 break;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600502 }
503 }
504}
505
506void object_unparent(Object *obj)
507{
Michael S. Tsirkine998fa82013-03-18 21:01:37 +0200508 if (obj->parent) {
509 object_property_del_child(obj->parent, obj, NULL);
510 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600511}
512
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600513static void object_deinit(Object *obj, TypeImpl *type)
514{
515 if (type->instance_finalize) {
516 type->instance_finalize(obj);
517 }
518
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600519 if (type_has_parent(type)) {
520 object_deinit(obj, type_get_parent(type));
521 }
522}
523
Paolo Bonzini339c2702012-11-23 09:47:16 +0100524static void object_finalize(void *data)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600525{
526 Object *obj = data;
527 TypeImpl *ti = obj->class->type;
528
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600529 object_property_del_all(obj);
Paolo Bonzini76a6e1c2014-06-11 11:58:30 +0200530 object_deinit(obj, ti);
Anthony Liguoridb85b572011-12-23 08:47:39 -0600531
Markus Armbruster719a3072018-06-08 19:02:31 +0200532 g_assert(obj->ref == 0);
Paolo Bonzinifde9bf42012-11-23 09:47:14 +0100533 if (obj->free) {
534 obj->free(obj);
535 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600536}
537
Marc-André Lureau63f7b102016-12-12 20:31:51 +0300538static Object *object_new_with_type(Type type)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600539{
540 Object *obj;
541
542 g_assert(type != NULL);
Igor Mitsyankoac451032012-02-28 15:57:11 +0400543 type_initialize(type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600544
545 obj = g_malloc(type->instance_size);
Andreas Färber5b9237f2013-08-30 18:28:37 +0200546 object_initialize_with_type(obj, type->instance_size, type);
Paolo Bonzinifde9bf42012-11-23 09:47:14 +0100547 obj->free = g_free;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600548
549 return obj;
550}
551
552Object *object_new(const char *typename)
553{
554 TypeImpl *ti = type_get_by_name(typename);
555
556 return object_new_with_type(ti);
557}
558
Daniel P. Berrangea31bdae2015-05-13 17:14:06 +0100559
560Object *object_new_with_props(const char *typename,
561 Object *parent,
562 const char *id,
563 Error **errp,
564 ...)
565{
566 va_list vargs;
567 Object *obj;
568
569 va_start(vargs, errp);
570 obj = object_new_with_propv(typename, parent, id, errp, vargs);
571 va_end(vargs);
572
573 return obj;
574}
575
576
577Object *object_new_with_propv(const char *typename,
578 Object *parent,
579 const char *id,
580 Error **errp,
581 va_list vargs)
582{
583 Object *obj;
584 ObjectClass *klass;
585 Error *local_err = NULL;
586
587 klass = object_class_by_name(typename);
588 if (!klass) {
589 error_setg(errp, "invalid object type: %s", typename);
590 return NULL;
591 }
592
593 if (object_class_is_abstract(klass)) {
594 error_setg(errp, "object type '%s' is abstract", typename);
595 return NULL;
596 }
597 obj = object_new(typename);
598
599 if (object_set_propv(obj, &local_err, vargs) < 0) {
600 goto error;
601 }
602
603 object_property_add_child(parent, id, obj, &local_err);
604 if (local_err) {
605 goto error;
606 }
607
608 if (object_dynamic_cast(obj, TYPE_USER_CREATABLE)) {
609 user_creatable_complete(obj, &local_err);
610 if (local_err) {
611 object_unparent(obj);
612 goto error;
613 }
614 }
615
616 object_unref(OBJECT(obj));
617 return obj;
618
619 error:
Eduardo Habkost621ff942016-06-13 18:57:56 -0300620 error_propagate(errp, local_err);
Daniel P. Berrangea31bdae2015-05-13 17:14:06 +0100621 object_unref(obj);
622 return NULL;
623}
624
625
626int object_set_props(Object *obj,
627 Error **errp,
628 ...)
629{
630 va_list vargs;
631 int ret;
632
633 va_start(vargs, errp);
634 ret = object_set_propv(obj, errp, vargs);
635 va_end(vargs);
636
637 return ret;
638}
639
640
641int object_set_propv(Object *obj,
642 Error **errp,
643 va_list vargs)
644{
645 const char *propname;
646 Error *local_err = NULL;
647
648 propname = va_arg(vargs, char *);
649 while (propname != NULL) {
650 const char *value = va_arg(vargs, char *);
651
652 g_assert(value != NULL);
653 object_property_parse(obj, value, propname, &local_err);
654 if (local_err) {
655 error_propagate(errp, local_err);
656 return -1;
657 }
658 propname = va_arg(vargs, char *);
659 }
660
661 return 0;
662}
663
664
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600665Object *object_dynamic_cast(Object *obj, const char *typename)
666{
Paolo Bonzinib7f43fe2012-11-23 16:56:17 +0100667 if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) {
Paolo Bonziniacc4af32012-02-03 11:57:23 +0100668 return obj;
669 }
670
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600671 return NULL;
672}
673
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200674Object *object_dynamic_cast_assert(Object *obj, const char *typename,
675 const char *file, int line, const char *func)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600676{
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200677 trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)",
678 typename, file, line, func);
679
Paolo Bonzini3556c232013-05-10 14:16:40 +0200680#ifdef CONFIG_QOM_CAST_DEBUG
Anthony Liguori03587322013-05-13 15:22:24 -0500681 int i;
682 Object *inst;
683
Peter Crosthwaite95916ab2013-05-22 11:19:16 +1000684 for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) {
Alex Bennéeb6b3ccf2016-09-30 22:30:57 +0100685 if (atomic_read(&obj->class->object_cast_cache[i]) == typename) {
Anthony Liguori03587322013-05-13 15:22:24 -0500686 goto out;
687 }
688 }
689
690 inst = object_dynamic_cast(obj, typename);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600691
Paolo Bonzinib7f43fe2012-11-23 16:56:17 +0100692 if (!inst && obj) {
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200693 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
694 file, line, func, obj, typename);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600695 abort();
696 }
697
Paolo Bonzini3556c232013-05-10 14:16:40 +0200698 assert(obj == inst);
Anthony Liguori03587322013-05-13 15:22:24 -0500699
Peter Crosthwaite95916ab2013-05-22 11:19:16 +1000700 if (obj && obj == inst) {
Anthony Liguori03587322013-05-13 15:22:24 -0500701 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
Alex Bennéeb6b3ccf2016-09-30 22:30:57 +0100702 atomic_set(&obj->class->object_cast_cache[i - 1],
703 atomic_read(&obj->class->object_cast_cache[i]));
Anthony Liguori03587322013-05-13 15:22:24 -0500704 }
Alex Bennéeb6b3ccf2016-09-30 22:30:57 +0100705 atomic_set(&obj->class->object_cast_cache[i - 1], typename);
Anthony Liguori03587322013-05-13 15:22:24 -0500706 }
707
708out:
Paolo Bonzini3556c232013-05-10 14:16:40 +0200709#endif
710 return obj;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600711}
712
713ObjectClass *object_class_dynamic_cast(ObjectClass *class,
714 const char *typename)
715{
Anthony Liguori33e95c62012-08-10 13:16:10 +1000716 ObjectClass *ret = NULL;
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200717 TypeImpl *target_type;
718 TypeImpl *type;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600719
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200720 if (!class) {
721 return NULL;
722 }
723
Paolo Bonzini793c96b2013-05-10 14:16:37 +0200724 /* A simple fast path that can trigger a lot for leaf classes. */
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200725 type = class->type;
Paolo Bonzini793c96b2013-05-10 14:16:37 +0200726 if (type->name == typename) {
727 return class;
728 }
729
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200730 target_type = type_get_by_name(typename);
Alexander Graf9ab880b2013-04-30 15:02:16 +0200731 if (!target_type) {
732 /* target class type unknown, so fail the cast */
733 return NULL;
734 }
735
Peter Crosthwaite00e2cea2013-02-19 14:02:10 +1000736 if (type->class->interfaces &&
737 type_is_ancestor(target_type, type_interface)) {
Anthony Liguori33e95c62012-08-10 13:16:10 +1000738 int found = 0;
739 GSList *i;
740
741 for (i = class->interfaces; i; i = i->next) {
742 ObjectClass *target_class = i->data;
743
744 if (type_is_ancestor(target_class->type, target_type)) {
745 ret = target_class;
746 found++;
747 }
748 }
749
750 /* The match was ambiguous, don't allow a cast */
751 if (found > 1) {
752 ret = NULL;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600753 }
Anthony Liguori33e95c62012-08-10 13:16:10 +1000754 } else if (type_is_ancestor(type, target_type)) {
755 ret = class;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600756 }
757
Anthony Liguori33e95c62012-08-10 13:16:10 +1000758 return ret;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600759}
760
761ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200762 const char *typename,
763 const char *file, int line,
764 const char *func)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600765{
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200766 ObjectClass *ret;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600767
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200768 trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)",
769 typename, file, line, func);
770
Anthony Liguori03587322013-05-13 15:22:24 -0500771#ifdef CONFIG_QOM_CAST_DEBUG
772 int i;
773
Peter Crosthwaite9d6a3d52013-06-18 19:18:59 +1000774 for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) {
Alex Bennéeb6b3ccf2016-09-30 22:30:57 +0100775 if (atomic_read(&class->class_cast_cache[i]) == typename) {
Anthony Liguori03587322013-05-13 15:22:24 -0500776 ret = class;
777 goto out;
778 }
779 }
780#else
Peter Crosthwaite9d6a3d52013-06-18 19:18:59 +1000781 if (!class || !class->interfaces) {
Paolo Bonzini3556c232013-05-10 14:16:40 +0200782 return class;
783 }
784#endif
785
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200786 ret = object_class_dynamic_cast(class, typename);
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200787 if (!ret && class) {
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200788 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
789 file, line, func, class, typename);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600790 abort();
791 }
792
Anthony Liguori03587322013-05-13 15:22:24 -0500793#ifdef CONFIG_QOM_CAST_DEBUG
Peter Crosthwaite9d6a3d52013-06-18 19:18:59 +1000794 if (class && ret == class) {
Anthony Liguori03587322013-05-13 15:22:24 -0500795 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
Alex Bennéeb6b3ccf2016-09-30 22:30:57 +0100796 atomic_set(&class->class_cast_cache[i - 1],
797 atomic_read(&class->class_cast_cache[i]));
Anthony Liguori03587322013-05-13 15:22:24 -0500798 }
Alex Bennéeb6b3ccf2016-09-30 22:30:57 +0100799 atomic_set(&class->class_cast_cache[i - 1], typename);
Anthony Liguori03587322013-05-13 15:22:24 -0500800 }
801out:
802#endif
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600803 return ret;
804}
805
Igor Mammedov8f5d58e2017-07-14 10:14:50 +0800806const char *object_get_typename(const Object *obj)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600807{
808 return obj->class->type->name;
809}
810
811ObjectClass *object_get_class(Object *obj)
812{
813 return obj->class;
814}
815
Andreas Färber17862372013-01-23 12:20:18 +0100816bool object_class_is_abstract(ObjectClass *klass)
817{
818 return klass->type->abstract;
819}
820
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600821const char *object_class_get_name(ObjectClass *klass)
822{
823 return klass->type->name;
824}
825
826ObjectClass *object_class_by_name(const char *typename)
827{
828 TypeImpl *type = type_get_by_name(typename);
829
830 if (!type) {
831 return NULL;
832 }
833
Igor Mitsyankoac451032012-02-28 15:57:11 +0400834 type_initialize(type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600835
836 return type->class;
837}
838
Paolo Bonzinie7cce672012-05-02 13:30:54 +0200839ObjectClass *object_class_get_parent(ObjectClass *class)
840{
841 TypeImpl *type = type_get_parent(class->type);
842
843 if (!type) {
844 return NULL;
845 }
846
847 type_initialize(type);
848
849 return type->class;
850}
851
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600852typedef struct OCFData
853{
854 void (*fn)(ObjectClass *klass, void *opaque);
Anthony Liguori93c511a2011-12-22 14:11:53 -0600855 const char *implements_type;
856 bool include_abstract;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600857 void *opaque;
858} OCFData;
859
860static void object_class_foreach_tramp(gpointer key, gpointer value,
861 gpointer opaque)
862{
863 OCFData *data = opaque;
864 TypeImpl *type = value;
Anthony Liguori93c511a2011-12-22 14:11:53 -0600865 ObjectClass *k;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600866
Igor Mitsyankoac451032012-02-28 15:57:11 +0400867 type_initialize(type);
Anthony Liguori93c511a2011-12-22 14:11:53 -0600868 k = type->class;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600869
Anthony Liguori93c511a2011-12-22 14:11:53 -0600870 if (!data->include_abstract && type->abstract) {
871 return;
872 }
873
874 if (data->implements_type &&
875 !object_class_dynamic_cast(k, data->implements_type)) {
876 return;
877 }
878
879 data->fn(k, data->opaque);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600880}
881
882void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
Anthony Liguori93c511a2011-12-22 14:11:53 -0600883 const char *implements_type, bool include_abstract,
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600884 void *opaque)
885{
Anthony Liguori93c511a2011-12-22 14:11:53 -0600886 OCFData data = { fn, implements_type, include_abstract, opaque };
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600887
Hervé Poussineauf54c19c2013-12-03 16:42:00 +0100888 enumerating_types = true;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600889 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
Hervé Poussineauf54c19c2013-12-03 16:42:00 +0100890 enumerating_types = false;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600891}
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600892
Peter Crosthwaited714b8d2015-09-08 17:38:43 +0100893static int do_object_child_foreach(Object *obj,
894 int (*fn)(Object *child, void *opaque),
895 void *opaque, bool recurse)
Paolo Bonzini32efc532012-04-11 23:30:20 +0200896{
Pavel Fedinb604a852015-10-13 13:37:45 +0100897 GHashTableIter iter;
898 ObjectProperty *prop;
Paolo Bonzini32efc532012-04-11 23:30:20 +0200899 int ret = 0;
900
Pavel Fedinb604a852015-10-13 13:37:45 +0100901 g_hash_table_iter_init(&iter, obj->properties);
902 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
Paolo Bonzini32efc532012-04-11 23:30:20 +0200903 if (object_property_is_child(prop)) {
Peter Crosthwaited714b8d2015-09-08 17:38:43 +0100904 Object *child = prop->opaque;
905
906 ret = fn(child, opaque);
Paolo Bonzini32efc532012-04-11 23:30:20 +0200907 if (ret != 0) {
908 break;
909 }
Peter Crosthwaited714b8d2015-09-08 17:38:43 +0100910 if (recurse) {
911 do_object_child_foreach(child, fn, opaque, true);
912 }
Paolo Bonzini32efc532012-04-11 23:30:20 +0200913 }
914 }
915 return ret;
916}
917
Peter Crosthwaited714b8d2015-09-08 17:38:43 +0100918int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
919 void *opaque)
920{
921 return do_object_child_foreach(obj, fn, opaque, false);
922}
923
924int object_child_foreach_recursive(Object *obj,
925 int (*fn)(Object *child, void *opaque),
926 void *opaque)
927{
928 return do_object_child_foreach(obj, fn, opaque, true);
929}
930
Andreas Färber418ba9e2012-02-25 23:07:34 +0100931static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
932{
933 GSList **list = opaque;
934
935 *list = g_slist_prepend(*list, klass);
936}
937
938GSList *object_class_get_list(const char *implements_type,
939 bool include_abstract)
940{
941 GSList *list = NULL;
942
943 object_class_foreach(object_class_get_list_tramp,
944 implements_type, include_abstract, &list);
945 return list;
946}
947
Paolo Bonzini47c66002018-03-03 08:33:10 +0100948static gint object_class_cmp(gconstpointer a, gconstpointer b)
949{
950 return strcasecmp(object_class_get_name((ObjectClass *)a),
951 object_class_get_name((ObjectClass *)b));
952}
953
954GSList *object_class_get_list_sorted(const char *implements_type,
955 bool include_abstract)
956{
957 return g_slist_sort(object_class_get_list(implements_type, include_abstract),
958 object_class_cmp);
959}
960
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600961void object_ref(Object *obj)
962{
Peter Crosthwaite8ffad852014-06-05 23:13:36 -0700963 if (!obj) {
964 return;
965 }
Andreas Färber8438a132015-11-16 17:49:20 +0100966 atomic_inc(&obj->ref);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600967}
968
969void object_unref(Object *obj)
970{
Peter Crosthwaite8ffad852014-06-05 23:13:36 -0700971 if (!obj) {
972 return;
973 }
Markus Armbruster719a3072018-06-08 19:02:31 +0200974 g_assert(obj->ref > 0);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600975
976 /* parent always holds a reference to its children */
Jan Kiszkaf08c03f2013-07-02 11:36:39 +0200977 if (atomic_fetch_dec(&obj->ref) == 1) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600978 object_finalize(obj);
979 }
980}
981
Paolo Bonzini64607d02014-06-05 13:11:51 +0200982ObjectProperty *
983object_property_add(Object *obj, const char *name, const char *type,
984 ObjectPropertyAccessor *get,
985 ObjectPropertyAccessor *set,
986 ObjectPropertyRelease *release,
987 void *opaque, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600988{
Peter Maydell54852b02013-03-25 13:15:13 +0000989 ObjectProperty *prop;
Peter Crosthwaite33965902014-08-19 23:55:52 -0700990 size_t name_len = strlen(name);
991
992 if (name_len >= 3 && !memcmp(name + name_len - 3, "[*]", 4)) {
993 int i;
994 ObjectProperty *ret;
995 char *name_no_array = g_strdup(name);
996
997 name_no_array[name_len - 3] = '\0';
998 for (i = 0; ; ++i) {
999 char *full_name = g_strdup_printf("%s[%d]", name_no_array, i);
1000
1001 ret = object_property_add(obj, full_name, type, get, set,
1002 release, opaque, NULL);
1003 g_free(full_name);
1004 if (ret) {
1005 break;
1006 }
1007 }
1008 g_free(name_no_array);
1009 return ret;
1010 }
Peter Maydell54852b02013-03-25 13:15:13 +00001011
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001012 if (object_property_find(obj, name, NULL) != NULL) {
Pavel Fedinb604a852015-10-13 13:37:45 +01001013 error_setg(errp, "attempt to add duplicate property '%s'"
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001014 " to object (type '%s')", name,
1015 object_get_typename(obj));
Pavel Fedinb604a852015-10-13 13:37:45 +01001016 return NULL;
Peter Maydell54852b02013-03-25 13:15:13 +00001017 }
1018
1019 prop = g_malloc0(sizeof(*prop));
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001020
1021 prop->name = g_strdup(name);
1022 prop->type = g_strdup(type);
1023
1024 prop->get = get;
1025 prop->set = set;
1026 prop->release = release;
1027 prop->opaque = opaque;
1028
Pavel Fedinb604a852015-10-13 13:37:45 +01001029 g_hash_table_insert(obj->properties, prop->name, prop);
Paolo Bonzini64607d02014-06-05 13:11:51 +02001030 return prop;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001031}
1032
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001033ObjectProperty *
1034object_class_property_add(ObjectClass *klass,
1035 const char *name,
1036 const char *type,
1037 ObjectPropertyAccessor *get,
1038 ObjectPropertyAccessor *set,
1039 ObjectPropertyRelease *release,
1040 void *opaque,
1041 Error **errp)
1042{
1043 ObjectProperty *prop;
1044
1045 if (object_class_property_find(klass, name, NULL) != NULL) {
1046 error_setg(errp, "attempt to add duplicate property '%s'"
1047 " to object (type '%s')", name,
1048 object_class_get_name(klass));
1049 return NULL;
1050 }
1051
1052 prop = g_malloc0(sizeof(*prop));
1053
1054 prop->name = g_strdup(name);
1055 prop->type = g_strdup(type);
1056
1057 prop->get = get;
1058 prop->set = set;
1059 prop->release = release;
1060 prop->opaque = opaque;
1061
1062 g_hash_table_insert(klass->properties, g_strdup(name), prop);
1063
1064 return prop;
1065}
1066
Paolo Bonzini89bfe002012-04-12 18:00:18 +02001067ObjectProperty *object_property_find(Object *obj, const char *name,
1068 Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001069{
1070 ObjectProperty *prop;
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001071 ObjectClass *klass = object_get_class(obj);
1072
1073 prop = object_class_property_find(klass, name, NULL);
1074 if (prop) {
1075 return prop;
1076 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001077
Pavel Fedinb604a852015-10-13 13:37:45 +01001078 prop = g_hash_table_lookup(obj->properties, name);
1079 if (prop) {
1080 return prop;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001081 }
1082
Cole Robinsonf231b882014-03-21 19:42:26 -04001083 error_setg(errp, "Property '.%s' not found", name);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001084 return NULL;
1085}
1086
Daniel P. Berrange7746abd2015-12-09 12:34:02 +00001087void object_property_iter_init(ObjectPropertyIterator *iter,
1088 Object *obj)
Daniel P. Berrangea00c9482015-10-13 13:37:40 +01001089{
Daniel P. Berrange7746abd2015-12-09 12:34:02 +00001090 g_hash_table_iter_init(&iter->iter, obj->properties);
1091 iter->nextclass = object_get_class(obj);
Daniel P. Berrangea00c9482015-10-13 13:37:40 +01001092}
1093
1094ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter)
1095{
Pavel Fedinb604a852015-10-13 13:37:45 +01001096 gpointer key, val;
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001097 while (!g_hash_table_iter_next(&iter->iter, &key, &val)) {
1098 if (!iter->nextclass) {
1099 return NULL;
1100 }
1101 g_hash_table_iter_init(&iter->iter, iter->nextclass->properties);
1102 iter->nextclass = object_class_get_parent(iter->nextclass);
Daniel P. Berrangea00c9482015-10-13 13:37:40 +01001103 }
Pavel Fedinb604a852015-10-13 13:37:45 +01001104 return val;
Daniel P. Berrangea00c9482015-10-13 13:37:40 +01001105}
1106
Alexey Kardashevskiy961c47b2018-03-02 00:09:39 +11001107void object_class_property_iter_init(ObjectPropertyIterator *iter,
1108 ObjectClass *klass)
1109{
1110 g_hash_table_iter_init(&iter->iter, klass->properties);
1111 iter->nextclass = klass;
1112}
1113
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001114ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name,
1115 Error **errp)
1116{
1117 ObjectProperty *prop;
1118 ObjectClass *parent_klass;
1119
1120 parent_klass = object_class_get_parent(klass);
1121 if (parent_klass) {
1122 prop = object_class_property_find(parent_klass, name, NULL);
1123 if (prop) {
1124 return prop;
1125 }
1126 }
1127
1128 prop = g_hash_table_lookup(klass->properties, name);
1129 if (!prop) {
1130 error_setg(errp, "Property '.%s' not found", name);
1131 }
1132 return prop;
1133}
1134
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001135void object_property_del(Object *obj, const char *name, Error **errp)
1136{
Pavel Fedinb604a852015-10-13 13:37:45 +01001137 ObjectProperty *prop = g_hash_table_lookup(obj->properties, name);
1138
1139 if (!prop) {
1140 error_setg(errp, "Property '.%s' not found", name);
Anthony Liguori0866aca2011-12-23 15:34:39 -06001141 return;
1142 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001143
Anthony Liguori0866aca2011-12-23 15:34:39 -06001144 if (prop->release) {
1145 prop->release(obj, name, prop->opaque);
1146 }
Pavel Fedinb604a852015-10-13 13:37:45 +01001147 g_hash_table_remove(obj->properties, name);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001148}
1149
1150void object_property_get(Object *obj, Visitor *v, const char *name,
1151 Error **errp)
1152{
Paolo Bonzini89bfe002012-04-12 18:00:18 +02001153 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001154 if (prop == NULL) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001155 return;
1156 }
1157
1158 if (!prop->get) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001159 error_setg(errp, QERR_PERMISSION_DENIED);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001160 } else {
Eric Blaked7bce992016-01-29 06:48:55 -07001161 prop->get(obj, v, name, prop->opaque, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001162 }
1163}
1164
1165void object_property_set(Object *obj, Visitor *v, const char *name,
1166 Error **errp)
1167{
Paolo Bonzini89bfe002012-04-12 18:00:18 +02001168 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001169 if (prop == NULL) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001170 return;
1171 }
1172
1173 if (!prop->set) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001174 error_setg(errp, QERR_PERMISSION_DENIED);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001175 } else {
Eric Blaked7bce992016-01-29 06:48:55 -07001176 prop->set(obj, v, name, prop->opaque, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001177 }
1178}
1179
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001180void object_property_set_str(Object *obj, const char *value,
1181 const char *name, Error **errp)
1182{
1183 QString *qstr = qstring_from_str(value);
1184 object_property_set_qobject(obj, QOBJECT(qstr), name, errp);
1185
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001186 qobject_unref(qstr);
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001187}
1188
1189char *object_property_get_str(Object *obj, const char *name,
1190 Error **errp)
1191{
1192 QObject *ret = object_property_get_qobject(obj, name, errp);
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001193 char *retval;
1194
1195 if (!ret) {
1196 return NULL;
1197 }
Peter Xuaafb21a2018-03-09 16:59:47 +08001198
1199 retval = g_strdup(qobject_get_try_str(ret));
1200 if (!retval) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001201 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001202 }
1203
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001204 qobject_unref(ret);
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001205 return retval;
1206}
1207
Paolo Bonzini1d9c5a12012-02-02 10:51:57 +01001208void object_property_set_link(Object *obj, Object *value,
1209 const char *name, Error **errp)
1210{
Peter Crosthwaited3c49312014-09-25 22:19:19 -07001211 if (value) {
1212 gchar *path = object_get_canonical_path(value);
1213 object_property_set_str(obj, path, name, errp);
1214 g_free(path);
1215 } else {
1216 object_property_set_str(obj, "", name, errp);
1217 }
Paolo Bonzini1d9c5a12012-02-02 10:51:57 +01001218}
1219
1220Object *object_property_get_link(Object *obj, const char *name,
1221 Error **errp)
1222{
1223 char *str = object_property_get_str(obj, name, errp);
1224 Object *target = NULL;
1225
1226 if (str && *str) {
1227 target = object_resolve_path(str, NULL);
1228 if (!target) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01001229 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1230 "Device '%s' not found", str);
Paolo Bonzini1d9c5a12012-02-02 10:51:57 +01001231 }
1232 }
1233
1234 g_free(str);
1235 return target;
1236}
1237
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001238void object_property_set_bool(Object *obj, bool value,
1239 const char *name, Error **errp)
1240{
Eric Blakefc48ffc2015-05-15 16:24:59 -06001241 QBool *qbool = qbool_from_bool(value);
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001242 object_property_set_qobject(obj, QOBJECT(qbool), name, errp);
1243
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001244 qobject_unref(qbool);
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001245}
1246
1247bool object_property_get_bool(Object *obj, const char *name,
1248 Error **errp)
1249{
1250 QObject *ret = object_property_get_qobject(obj, name, errp);
1251 QBool *qbool;
1252 bool retval;
1253
1254 if (!ret) {
1255 return false;
1256 }
Max Reitz7dc847e2018-02-24 16:40:29 +01001257 qbool = qobject_to(QBool, ret);
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001258 if (!qbool) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001259 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001260 retval = false;
1261 } else {
Eric Blakefc48ffc2015-05-15 16:24:59 -06001262 retval = qbool_get_bool(qbool);
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001263 }
1264
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001265 qobject_unref(ret);
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001266 return retval;
1267}
1268
1269void object_property_set_int(Object *obj, int64_t value,
1270 const char *name, Error **errp)
1271{
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +04001272 QNum *qnum = qnum_from_int(value);
1273 object_property_set_qobject(obj, QOBJECT(qnum), name, errp);
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001274
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001275 qobject_unref(qnum);
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001276}
1277
1278int64_t object_property_get_int(Object *obj, const char *name,
1279 Error **errp)
1280{
1281 QObject *ret = object_property_get_qobject(obj, name, errp);
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +04001282 QNum *qnum;
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001283 int64_t retval;
1284
1285 if (!ret) {
1286 return -1;
1287 }
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +04001288
Max Reitz7dc847e2018-02-24 16:40:29 +01001289 qnum = qobject_to(QNum, ret);
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +04001290 if (!qnum || !qnum_get_try_int(qnum, &retval)) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001291 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001292 retval = -1;
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001293 }
1294
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001295 qobject_unref(ret);
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001296 return retval;
1297}
1298
Marc-André Lureau31527792017-06-07 20:36:04 +04001299void object_property_set_uint(Object *obj, uint64_t value,
1300 const char *name, Error **errp)
1301{
1302 QNum *qnum = qnum_from_uint(value);
1303
1304 object_property_set_qobject(obj, QOBJECT(qnum), name, errp);
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001305 qobject_unref(qnum);
Marc-André Lureau31527792017-06-07 20:36:04 +04001306}
1307
1308uint64_t object_property_get_uint(Object *obj, const char *name,
1309 Error **errp)
1310{
1311 QObject *ret = object_property_get_qobject(obj, name, errp);
1312 QNum *qnum;
1313 uint64_t retval;
1314
1315 if (!ret) {
1316 return 0;
1317 }
Max Reitz7dc847e2018-02-24 16:40:29 +01001318 qnum = qobject_to(QNum, ret);
Marc-André Lureau31527792017-06-07 20:36:04 +04001319 if (!qnum || !qnum_get_try_uint(qnum, &retval)) {
1320 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "uint");
1321 retval = 0;
1322 }
1323
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +02001324 qobject_unref(ret);
Marc-André Lureau31527792017-06-07 20:36:04 +04001325 return retval;
1326}
1327
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01001328typedef struct EnumProperty {
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +02001329 const QEnumLookup *lookup;
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01001330 int (*get)(Object *, Error **);
1331 void (*set)(Object *, int, Error **);
1332} EnumProperty;
1333
Hu Tao1f217722014-05-14 17:43:33 +08001334int object_property_get_enum(Object *obj, const char *name,
Daniel P. Berrangea3590da2015-05-27 16:07:56 +01001335 const char *typename, Error **errp)
Hu Tao1f217722014-05-14 17:43:33 +08001336{
Markus Armbruster4715d422015-08-25 20:00:45 +02001337 Error *err = NULL;
Eric Blake7a0525c2016-06-09 10:48:37 -06001338 Visitor *v;
Chen Fan976620a2014-08-18 14:46:34 +08001339 char *str;
Hu Tao1f217722014-05-14 17:43:33 +08001340 int ret;
Daniel P. Berrangea3590da2015-05-27 16:07:56 +01001341 ObjectProperty *prop = object_property_find(obj, name, errp);
1342 EnumProperty *enumprop;
1343
1344 if (prop == NULL) {
1345 return 0;
1346 }
1347
1348 if (!g_str_equal(prop->type, typename)) {
1349 error_setg(errp, "Property %s on %s is not '%s' enum type",
1350 name, object_class_get_name(
1351 object_get_class(obj)), typename);
1352 return 0;
1353 }
1354
1355 enumprop = prop->opaque;
Hu Tao1f217722014-05-14 17:43:33 +08001356
Eric Blake3b098d52016-06-09 10:48:43 -06001357 v = string_output_visitor_new(false, &str);
Eric Blakee7ca5652016-06-09 10:48:39 -06001358 object_property_get(obj, v, name, &err);
Markus Armbruster4715d422015-08-25 20:00:45 +02001359 if (err) {
1360 error_propagate(errp, err);
Eric Blakee7ca5652016-06-09 10:48:39 -06001361 visit_free(v);
Markus Armbruster4715d422015-08-25 20:00:45 +02001362 return 0;
1363 }
Eric Blake3b098d52016-06-09 10:48:43 -06001364 visit_complete(v, &str);
Eric Blakee7ca5652016-06-09 10:48:39 -06001365 visit_free(v);
Eric Blake7a0525c2016-06-09 10:48:37 -06001366 v = string_input_visitor_new(str);
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +02001367 visit_type_enum(v, name, &ret, enumprop->lookup, errp);
Chen Fan976620a2014-08-18 14:46:34 +08001368
1369 g_free(str);
Eric Blake7a0525c2016-06-09 10:48:37 -06001370 visit_free(v);
Hu Tao1f217722014-05-14 17:43:33 +08001371
1372 return ret;
1373}
1374
1375void object_property_get_uint16List(Object *obj, const char *name,
1376 uint16List **list, Error **errp)
1377{
Markus Armbruster4715d422015-08-25 20:00:45 +02001378 Error *err = NULL;
Eric Blake7a0525c2016-06-09 10:48:37 -06001379 Visitor *v;
Chen Fan976620a2014-08-18 14:46:34 +08001380 char *str;
Hu Tao1f217722014-05-14 17:43:33 +08001381
Eric Blake3b098d52016-06-09 10:48:43 -06001382 v = string_output_visitor_new(false, &str);
1383 object_property_get(obj, v, name, &err);
Markus Armbruster4715d422015-08-25 20:00:45 +02001384 if (err) {
1385 error_propagate(errp, err);
1386 goto out;
1387 }
Eric Blake3b098d52016-06-09 10:48:43 -06001388 visit_complete(v, &str);
1389 visit_free(v);
Eric Blake7a0525c2016-06-09 10:48:37 -06001390 v = string_input_visitor_new(str);
1391 visit_type_uint16List(v, NULL, list, errp);
Chen Fan976620a2014-08-18 14:46:34 +08001392
1393 g_free(str);
Markus Armbruster4715d422015-08-25 20:00:45 +02001394out:
Eric Blake3b098d52016-06-09 10:48:43 -06001395 visit_free(v);
Hu Tao1f217722014-05-14 17:43:33 +08001396}
1397
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001398void object_property_parse(Object *obj, const char *string,
1399 const char *name, Error **errp)
1400{
Eric Blake7a0525c2016-06-09 10:48:37 -06001401 Visitor *v = string_input_visitor_new(string);
1402 object_property_set(obj, v, name, errp);
1403 visit_free(v);
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001404}
1405
Paolo Bonzini0b7593e2014-02-08 11:01:50 +01001406char *object_property_print(Object *obj, const char *name, bool human,
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001407 Error **errp)
1408{
Eric Blake3b098d52016-06-09 10:48:43 -06001409 Visitor *v;
Gonglei3a530092014-09-27 13:13:55 +08001410 char *string = NULL;
1411 Error *local_err = NULL;
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001412
Eric Blake3b098d52016-06-09 10:48:43 -06001413 v = string_output_visitor_new(human, &string);
1414 object_property_get(obj, v, name, &local_err);
Gonglei3a530092014-09-27 13:13:55 +08001415 if (local_err) {
1416 error_propagate(errp, local_err);
1417 goto out;
1418 }
1419
Eric Blake3b098d52016-06-09 10:48:43 -06001420 visit_complete(v, &string);
Gonglei3a530092014-09-27 13:13:55 +08001421
1422out:
Eric Blake3b098d52016-06-09 10:48:43 -06001423 visit_free(v);
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001424 return string;
1425}
1426
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001427const char *object_property_get_type(Object *obj, const char *name, Error **errp)
1428{
Paolo Bonzini89bfe002012-04-12 18:00:18 +02001429 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001430 if (prop == NULL) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001431 return NULL;
1432 }
1433
1434 return prop->type;
1435}
1436
1437Object *object_get_root(void)
1438{
Anthony Liguori8b45d442011-12-23 09:08:05 -06001439 static Object *root;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001440
Anthony Liguori8b45d442011-12-23 09:08:05 -06001441 if (!root) {
1442 root = object_new("container");
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001443 }
1444
Anthony Liguori8b45d442011-12-23 09:08:05 -06001445 return root;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001446}
1447
Daniel P. Berrangebc2256c2015-05-13 17:14:05 +01001448Object *object_get_objects_root(void)
1449{
1450 return container_get(object_get_root(), "/objects");
1451}
1452
Peter Xu7c47c4e2017-09-28 10:59:54 +08001453Object *object_get_internal_root(void)
1454{
1455 static Object *internal_root;
1456
1457 if (!internal_root) {
1458 internal_root = object_new("container");
1459 }
1460
1461 return internal_root;
1462}
1463
Eric Blaked7bce992016-01-29 06:48:55 -07001464static void object_get_child_property(Object *obj, Visitor *v,
1465 const char *name, void *opaque,
1466 Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001467{
1468 Object *child = opaque;
1469 gchar *path;
1470
1471 path = object_get_canonical_path(child);
Eric Blake51e72bc2016-01-29 06:48:54 -07001472 visit_type_str(v, name, &path, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001473 g_free(path);
1474}
1475
Paolo Bonzini64607d02014-06-05 13:11:51 +02001476static Object *object_resolve_child_property(Object *parent, void *opaque, const gchar *part)
1477{
1478 return opaque;
1479}
1480
Anthony Liguoridb85b572011-12-23 08:47:39 -06001481static void object_finalize_child_property(Object *obj, const char *name,
1482 void *opaque)
1483{
1484 Object *child = opaque;
1485
Paolo Bonzinibffc6872014-06-11 11:57:38 +02001486 if (child->class->unparent) {
1487 (child->class->unparent)(child);
1488 }
1489 child->parent = NULL;
Anthony Liguoridb85b572011-12-23 08:47:39 -06001490 object_unref(child);
1491}
1492
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001493void object_property_add_child(Object *obj, const char *name,
1494 Object *child, Error **errp)
1495{
Paolo Bonzinib0ed5e92013-12-20 23:21:08 +01001496 Error *local_err = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001497 gchar *type;
Paolo Bonzini64607d02014-06-05 13:11:51 +02001498 ObjectProperty *op;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001499
Peter Crosthwaite8faa2f82014-09-25 22:19:52 -07001500 if (child->parent != NULL) {
1501 error_setg(errp, "child object is already parented");
1502 return;
1503 }
1504
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001505 type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child)));
1506
Paolo Bonzini64607d02014-06-05 13:11:51 +02001507 op = object_property_add(obj, name, type, object_get_child_property, NULL,
1508 object_finalize_child_property, child, &local_err);
Paolo Bonzinib0ed5e92013-12-20 23:21:08 +01001509 if (local_err) {
1510 error_propagate(errp, local_err);
1511 goto out;
1512 }
Paolo Bonzini64607d02014-06-05 13:11:51 +02001513
1514 op->resolve = object_resolve_child_property;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001515 object_ref(child);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001516 child->parent = obj;
1517
Paolo Bonzinib0ed5e92013-12-20 23:21:08 +01001518out:
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001519 g_free(type);
1520}
1521
Igor Mammedov8f5d58e2017-07-14 10:14:50 +08001522void object_property_allow_set_link(const Object *obj, const char *name,
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001523 Object *val, Error **errp)
1524{
1525 /* Allow the link to be set, always */
1526}
1527
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001528typedef struct {
1529 Object **child;
Igor Mammedov8f5d58e2017-07-14 10:14:50 +08001530 void (*check)(const Object *, const char *, Object *, Error **);
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001531 ObjectPropertyLinkFlags flags;
1532} LinkProperty;
1533
Eric Blaked7bce992016-01-29 06:48:55 -07001534static void object_get_link_property(Object *obj, Visitor *v,
1535 const char *name, void *opaque,
1536 Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001537{
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001538 LinkProperty *lprop = opaque;
1539 Object **child = lprop->child;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001540 gchar *path;
1541
1542 if (*child) {
1543 path = object_get_canonical_path(*child);
Eric Blake51e72bc2016-01-29 06:48:54 -07001544 visit_type_str(v, name, &path, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001545 g_free(path);
1546 } else {
1547 path = (gchar *)"";
Eric Blake51e72bc2016-01-29 06:48:54 -07001548 visit_type_str(v, name, &path, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001549 }
1550}
1551
Stefan Hajnoczif5ec6702014-03-19 08:58:53 +01001552/*
1553 * object_resolve_link:
1554 *
1555 * Lookup an object and ensure its type matches the link property type. This
1556 * is similar to object_resolve_path() except type verification against the
1557 * link property is performed.
1558 *
1559 * Returns: The matched object or NULL on path lookup failures.
1560 */
1561static Object *object_resolve_link(Object *obj, const char *name,
1562 const char *path, Error **errp)
1563{
1564 const char *type;
1565 gchar *target_type;
1566 bool ambiguous = false;
1567 Object *target;
1568
1569 /* Go from link<FOO> to FOO. */
1570 type = object_property_get_type(obj, name, NULL);
1571 target_type = g_strndup(&type[5], strlen(type) - 6);
1572 target = object_resolve_path_type(path, target_type, &ambiguous);
1573
1574 if (ambiguous) {
Eric Blake455b0fd2015-11-10 23:51:20 -07001575 error_setg(errp, "Path '%s' does not uniquely identify an object",
1576 path);
Stefan Hajnoczif5ec6702014-03-19 08:58:53 +01001577 } else if (!target) {
1578 target = object_resolve_path(path, &ambiguous);
1579 if (target || ambiguous) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001580 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
Stefan Hajnoczif5ec6702014-03-19 08:58:53 +01001581 } else {
Markus Armbruster75158eb2015-03-16 08:57:47 +01001582 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1583 "Device '%s' not found", path);
Stefan Hajnoczif5ec6702014-03-19 08:58:53 +01001584 }
1585 target = NULL;
1586 }
1587 g_free(target_type);
1588
1589 return target;
1590}
1591
Eric Blaked7bce992016-01-29 06:48:55 -07001592static void object_set_link_property(Object *obj, Visitor *v,
1593 const char *name, void *opaque,
1594 Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001595{
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001596 Error *local_err = NULL;
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001597 LinkProperty *prop = opaque;
1598 Object **child = prop->child;
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001599 Object *old_target = *child;
1600 Object *new_target = NULL;
1601 char *path = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001602
Eric Blake51e72bc2016-01-29 06:48:54 -07001603 visit_type_str(v, name, &path, &local_err);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001604
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001605 if (!local_err && strcmp(path, "") != 0) {
1606 new_target = object_resolve_link(obj, name, path, &local_err);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001607 }
1608
1609 g_free(path);
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001610 if (local_err) {
1611 error_propagate(errp, local_err);
1612 return;
1613 }
Alexander Barabashf0cdc962012-02-22 19:22:26 +02001614
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001615 prop->check(obj, name, new_target, &local_err);
1616 if (local_err) {
1617 error_propagate(errp, local_err);
1618 return;
1619 }
1620
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001621 *child = new_target;
Marc-André Lureau265b5782018-05-31 21:51:17 +02001622 if (prop->flags == OBJ_PROP_LINK_STRONG) {
1623 object_ref(new_target);
1624 object_unref(old_target);
1625 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001626}
1627
Paolo Bonzini64607d02014-06-05 13:11:51 +02001628static Object *object_resolve_link_property(Object *parent, void *opaque, const gchar *part)
1629{
1630 LinkProperty *lprop = opaque;
1631
1632 return *lprop->child;
1633}
1634
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001635static void object_release_link_property(Object *obj, const char *name,
1636 void *opaque)
1637{
1638 LinkProperty *prop = opaque;
1639
Marc-André Lureau265b5782018-05-31 21:51:17 +02001640 if ((prop->flags & OBJ_PROP_LINK_STRONG) && *prop->child) {
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001641 object_unref(*prop->child);
1642 }
1643 g_free(prop);
1644}
1645
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001646void object_property_add_link(Object *obj, const char *name,
1647 const char *type, Object **child,
Igor Mammedov8f5d58e2017-07-14 10:14:50 +08001648 void (*check)(const Object *, const char *,
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001649 Object *, Error **),
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001650 ObjectPropertyLinkFlags flags,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001651 Error **errp)
1652{
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001653 Error *local_err = NULL;
1654 LinkProperty *prop = g_malloc(sizeof(*prop));
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001655 gchar *full_type;
Paolo Bonzini64607d02014-06-05 13:11:51 +02001656 ObjectProperty *op;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001657
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001658 prop->child = child;
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001659 prop->check = check;
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001660 prop->flags = flags;
1661
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001662 full_type = g_strdup_printf("link<%s>", type);
1663
Paolo Bonzini64607d02014-06-05 13:11:51 +02001664 op = object_property_add(obj, name, full_type,
1665 object_get_link_property,
1666 check ? object_set_link_property : NULL,
1667 object_release_link_property,
1668 prop,
1669 &local_err);
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001670 if (local_err) {
1671 error_propagate(errp, local_err);
1672 g_free(prop);
Paolo Bonzini64607d02014-06-05 13:11:51 +02001673 goto out;
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001674 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001675
Paolo Bonzini64607d02014-06-05 13:11:51 +02001676 op->resolve = object_resolve_link_property;
1677
1678out:
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001679 g_free(full_type);
1680}
1681
Paolo Bonzinifb9e7e32015-05-05 18:29:00 +02001682void object_property_add_const_link(Object *obj, const char *name,
1683 Object *target, Error **errp)
1684{
1685 char *link_type;
1686 ObjectProperty *op;
1687
1688 link_type = g_strdup_printf("link<%s>", object_get_typename(target));
1689 op = object_property_add(obj, name, link_type,
1690 object_get_child_property, NULL,
1691 NULL, target, errp);
1692 if (op != NULL) {
1693 op->resolve = object_resolve_child_property;
1694 }
1695 g_free(link_type);
1696}
1697
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001698gchar *object_get_canonical_path_component(Object *obj)
1699{
1700 ObjectProperty *prop = NULL;
Pavel Fedinb604a852015-10-13 13:37:45 +01001701 GHashTableIter iter;
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001702
Paolo Bonzini770dec22018-04-30 11:44:17 +02001703 if (obj->parent == NULL) {
1704 return NULL;
1705 }
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001706
Pavel Fedinb604a852015-10-13 13:37:45 +01001707 g_hash_table_iter_init(&iter, obj->parent->properties);
1708 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001709 if (!object_property_is_child(prop)) {
1710 continue;
1711 }
1712
1713 if (prop->opaque == obj) {
1714 return g_strdup(prop->name);
1715 }
1716 }
1717
1718 /* obj had a parent but was not a child, should never happen */
1719 g_assert_not_reached();
1720 return NULL;
1721}
1722
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001723gchar *object_get_canonical_path(Object *obj)
1724{
1725 Object *root = object_get_root();
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001726 char *newpath, *path = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001727
Paolo Bonzinie40077f2018-05-30 18:16:04 +02001728 if (obj == root) {
1729 return g_strdup("/");
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001730 }
1731
Paolo Bonzinie40077f2018-05-30 18:16:04 +02001732 do {
1733 char *component = object_get_canonical_path_component(obj);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001734
Paolo Bonzinie40077f2018-05-30 18:16:04 +02001735 if (!component) {
1736 /* A canonical path must be complete, so discard what was
1737 * collected so far.
1738 */
1739 g_free(path);
1740 return NULL;
1741 }
1742
1743 newpath = g_strdup_printf("/%s%s", component, path ? path : "");
1744 g_free(path);
1745 g_free(component);
1746 path = newpath;
1747 obj = obj->parent;
1748 } while (obj != root);
1749
1750 return path;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001751}
1752
Andreas Färber3e84b482013-01-15 02:55:10 +01001753Object *object_resolve_path_component(Object *parent, const gchar *part)
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001754{
Paolo Bonzini89bfe002012-04-12 18:00:18 +02001755 ObjectProperty *prop = object_property_find(parent, part, NULL);
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001756 if (prop == NULL) {
1757 return NULL;
1758 }
1759
Paolo Bonzini64607d02014-06-05 13:11:51 +02001760 if (prop->resolve) {
1761 return prop->resolve(parent, prop->opaque, part);
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001762 } else {
1763 return NULL;
1764 }
1765}
1766
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001767static Object *object_resolve_abs_path(Object *parent,
1768 gchar **parts,
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001769 const char *typename,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001770 int index)
1771{
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001772 Object *child;
1773
1774 if (parts[index] == NULL) {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001775 return object_dynamic_cast(parent, typename);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001776 }
1777
1778 if (strcmp(parts[index], "") == 0) {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001779 return object_resolve_abs_path(parent, parts, typename, index + 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001780 }
1781
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001782 child = object_resolve_path_component(parent, parts[index]);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001783 if (!child) {
1784 return NULL;
1785 }
1786
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001787 return object_resolve_abs_path(child, parts, typename, index + 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001788}
1789
1790static Object *object_resolve_partial_path(Object *parent,
1791 gchar **parts,
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001792 const char *typename,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001793 bool *ambiguous)
1794{
1795 Object *obj;
Pavel Fedinb604a852015-10-13 13:37:45 +01001796 GHashTableIter iter;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001797 ObjectProperty *prop;
1798
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001799 obj = object_resolve_abs_path(parent, parts, typename, 0);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001800
Pavel Fedinb604a852015-10-13 13:37:45 +01001801 g_hash_table_iter_init(&iter, parent->properties);
1802 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001803 Object *found;
1804
Andreas Färber5d9d3f42012-05-27 00:32:40 +02001805 if (!object_property_is_child(prop)) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001806 continue;
1807 }
1808
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001809 found = object_resolve_partial_path(prop->opaque, parts,
1810 typename, ambiguous);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001811 if (found) {
1812 if (obj) {
Eduardo Habkostebcc4792017-07-07 18:30:52 -03001813 *ambiguous = true;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001814 return NULL;
1815 }
1816 obj = found;
1817 }
1818
Eduardo Habkostebcc4792017-07-07 18:30:52 -03001819 if (*ambiguous) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001820 return NULL;
1821 }
1822 }
1823
1824 return obj;
1825}
1826
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001827Object *object_resolve_path_type(const char *path, const char *typename,
Eduardo Habkostebcc4792017-07-07 18:30:52 -03001828 bool *ambiguousp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001829{
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001830 Object *obj;
1831 gchar **parts;
1832
1833 parts = g_strsplit(path, "/", 0);
Paolo Bonzini2e1103f2013-04-18 18:44:02 +02001834 assert(parts);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001835
Paolo Bonzini2e1103f2013-04-18 18:44:02 +02001836 if (parts[0] == NULL || strcmp(parts[0], "") != 0) {
Eduardo Habkostebcc4792017-07-07 18:30:52 -03001837 bool ambiguous = false;
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001838 obj = object_resolve_partial_path(object_get_root(), parts,
Eduardo Habkostebcc4792017-07-07 18:30:52 -03001839 typename, &ambiguous);
1840 if (ambiguousp) {
1841 *ambiguousp = ambiguous;
1842 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001843 } else {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001844 obj = object_resolve_abs_path(object_get_root(), parts, typename, 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001845 }
1846
1847 g_strfreev(parts);
1848
1849 return obj;
1850}
1851
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001852Object *object_resolve_path(const char *path, bool *ambiguous)
1853{
1854 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
1855}
1856
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001857typedef struct StringProperty
1858{
1859 char *(*get)(Object *, Error **);
1860 void (*set)(Object *, const char *, Error **);
1861} StringProperty;
1862
Eric Blaked7bce992016-01-29 06:48:55 -07001863static void property_get_str(Object *obj, Visitor *v, const char *name,
1864 void *opaque, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001865{
1866 StringProperty *prop = opaque;
1867 char *value;
Markus Armbrustere1c82372015-08-25 20:00:46 +02001868 Error *err = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001869
Markus Armbrustere1c82372015-08-25 20:00:46 +02001870 value = prop->get(obj, &err);
1871 if (err) {
1872 error_propagate(errp, err);
1873 return;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001874 }
Markus Armbrustere1c82372015-08-25 20:00:46 +02001875
Eric Blake51e72bc2016-01-29 06:48:54 -07001876 visit_type_str(v, name, &value, errp);
Markus Armbrustere1c82372015-08-25 20:00:46 +02001877 g_free(value);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001878}
1879
Eric Blaked7bce992016-01-29 06:48:55 -07001880static void property_set_str(Object *obj, Visitor *v, const char *name,
1881 void *opaque, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001882{
1883 StringProperty *prop = opaque;
1884 char *value;
1885 Error *local_err = NULL;
1886
Eric Blake51e72bc2016-01-29 06:48:54 -07001887 visit_type_str(v, name, &value, &local_err);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001888 if (local_err) {
1889 error_propagate(errp, local_err);
1890 return;
1891 }
1892
1893 prop->set(obj, value, errp);
1894 g_free(value);
1895}
1896
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001897static void property_release_str(Object *obj, const char *name,
1898 void *opaque)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001899{
1900 StringProperty *prop = opaque;
1901 g_free(prop);
1902}
1903
1904void object_property_add_str(Object *obj, const char *name,
1905 char *(*get)(Object *, Error **),
1906 void (*set)(Object *, const char *, Error **),
1907 Error **errp)
1908{
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01001909 Error *local_err = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001910 StringProperty *prop = g_malloc0(sizeof(*prop));
1911
1912 prop->get = get;
1913 prop->set = set;
1914
1915 object_property_add(obj, name, "string",
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001916 get ? property_get_str : NULL,
1917 set ? property_set_str : NULL,
1918 property_release_str,
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01001919 prop, &local_err);
1920 if (local_err) {
1921 error_propagate(errp, local_err);
1922 g_free(prop);
1923 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001924}
Paolo Bonzini745549c2012-03-31 16:45:54 +02001925
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001926void object_class_property_add_str(ObjectClass *klass, const char *name,
1927 char *(*get)(Object *, Error **),
1928 void (*set)(Object *, const char *,
1929 Error **),
1930 Error **errp)
1931{
1932 Error *local_err = NULL;
1933 StringProperty *prop = g_malloc0(sizeof(*prop));
1934
1935 prop->get = get;
1936 prop->set = set;
1937
1938 object_class_property_add(klass, name, "string",
1939 get ? property_get_str : NULL,
1940 set ? property_set_str : NULL,
1941 property_release_str,
1942 prop, &local_err);
1943 if (local_err) {
1944 error_propagate(errp, local_err);
1945 g_free(prop);
1946 }
1947}
1948
Anthony Liguori0e558842012-06-25 10:32:46 -05001949typedef struct BoolProperty
1950{
1951 bool (*get)(Object *, Error **);
1952 void (*set)(Object *, bool, Error **);
1953} BoolProperty;
1954
Eric Blaked7bce992016-01-29 06:48:55 -07001955static void property_get_bool(Object *obj, Visitor *v, const char *name,
1956 void *opaque, Error **errp)
Anthony Liguori0e558842012-06-25 10:32:46 -05001957{
1958 BoolProperty *prop = opaque;
1959 bool value;
Markus Armbruster4715d422015-08-25 20:00:45 +02001960 Error *err = NULL;
Anthony Liguori0e558842012-06-25 10:32:46 -05001961
Markus Armbruster4715d422015-08-25 20:00:45 +02001962 value = prop->get(obj, &err);
1963 if (err) {
1964 error_propagate(errp, err);
1965 return;
1966 }
1967
Eric Blake51e72bc2016-01-29 06:48:54 -07001968 visit_type_bool(v, name, &value, errp);
Anthony Liguori0e558842012-06-25 10:32:46 -05001969}
1970
Eric Blaked7bce992016-01-29 06:48:55 -07001971static void property_set_bool(Object *obj, Visitor *v, const char *name,
1972 void *opaque, Error **errp)
Anthony Liguori0e558842012-06-25 10:32:46 -05001973{
1974 BoolProperty *prop = opaque;
1975 bool value;
1976 Error *local_err = NULL;
1977
Eric Blake51e72bc2016-01-29 06:48:54 -07001978 visit_type_bool(v, name, &value, &local_err);
Anthony Liguori0e558842012-06-25 10:32:46 -05001979 if (local_err) {
1980 error_propagate(errp, local_err);
1981 return;
1982 }
1983
1984 prop->set(obj, value, errp);
1985}
1986
1987static void property_release_bool(Object *obj, const char *name,
1988 void *opaque)
1989{
1990 BoolProperty *prop = opaque;
1991 g_free(prop);
1992}
1993
1994void object_property_add_bool(Object *obj, const char *name,
1995 bool (*get)(Object *, Error **),
1996 void (*set)(Object *, bool, Error **),
1997 Error **errp)
1998{
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01001999 Error *local_err = NULL;
Anthony Liguori0e558842012-06-25 10:32:46 -05002000 BoolProperty *prop = g_malloc0(sizeof(*prop));
2001
2002 prop->get = get;
2003 prop->set = set;
2004
2005 object_property_add(obj, name, "bool",
2006 get ? property_get_bool : NULL,
2007 set ? property_set_bool : NULL,
2008 property_release_bool,
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01002009 prop, &local_err);
2010 if (local_err) {
2011 error_propagate(errp, local_err);
2012 g_free(prop);
2013 }
Anthony Liguori0e558842012-06-25 10:32:46 -05002014}
2015
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002016void object_class_property_add_bool(ObjectClass *klass, const char *name,
2017 bool (*get)(Object *, Error **),
2018 void (*set)(Object *, bool, Error **),
2019 Error **errp)
2020{
2021 Error *local_err = NULL;
2022 BoolProperty *prop = g_malloc0(sizeof(*prop));
2023
2024 prop->get = get;
2025 prop->set = set;
2026
2027 object_class_property_add(klass, name, "bool",
2028 get ? property_get_bool : NULL,
2029 set ? property_set_bool : NULL,
2030 property_release_bool,
2031 prop, &local_err);
2032 if (local_err) {
2033 error_propagate(errp, local_err);
2034 g_free(prop);
2035 }
2036}
2037
Eric Blaked7bce992016-01-29 06:48:55 -07002038static void property_get_enum(Object *obj, Visitor *v, const char *name,
2039 void *opaque, Error **errp)
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01002040{
2041 EnumProperty *prop = opaque;
2042 int value;
Markus Armbruster4715d422015-08-25 20:00:45 +02002043 Error *err = NULL;
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01002044
Markus Armbruster4715d422015-08-25 20:00:45 +02002045 value = prop->get(obj, &err);
2046 if (err) {
2047 error_propagate(errp, err);
2048 return;
2049 }
2050
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +02002051 visit_type_enum(v, name, &value, prop->lookup, errp);
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01002052}
2053
Eric Blaked7bce992016-01-29 06:48:55 -07002054static void property_set_enum(Object *obj, Visitor *v, const char *name,
2055 void *opaque, Error **errp)
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01002056{
2057 EnumProperty *prop = opaque;
2058 int value;
Markus Armbruster4715d422015-08-25 20:00:45 +02002059 Error *err = NULL;
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01002060
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +02002061 visit_type_enum(v, name, &value, prop->lookup, &err);
Markus Armbruster4715d422015-08-25 20:00:45 +02002062 if (err) {
2063 error_propagate(errp, err);
2064 return;
2065 }
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01002066 prop->set(obj, value, errp);
2067}
2068
2069static void property_release_enum(Object *obj, const char *name,
2070 void *opaque)
2071{
2072 EnumProperty *prop = opaque;
2073 g_free(prop);
2074}
2075
2076void object_property_add_enum(Object *obj, const char *name,
2077 const char *typename,
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +02002078 const QEnumLookup *lookup,
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01002079 int (*get)(Object *, Error **),
2080 void (*set)(Object *, int, Error **),
2081 Error **errp)
2082{
2083 Error *local_err = NULL;
2084 EnumProperty *prop = g_malloc(sizeof(*prop));
2085
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +02002086 prop->lookup = lookup;
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01002087 prop->get = get;
2088 prop->set = set;
2089
2090 object_property_add(obj, name, typename,
2091 get ? property_get_enum : NULL,
2092 set ? property_set_enum : NULL,
2093 property_release_enum,
2094 prop, &local_err);
2095 if (local_err) {
2096 error_propagate(errp, local_err);
2097 g_free(prop);
2098 }
2099}
2100
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002101void object_class_property_add_enum(ObjectClass *klass, const char *name,
2102 const char *typename,
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +02002103 const QEnumLookup *lookup,
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002104 int (*get)(Object *, Error **),
2105 void (*set)(Object *, int, Error **),
2106 Error **errp)
2107{
2108 Error *local_err = NULL;
2109 EnumProperty *prop = g_malloc(sizeof(*prop));
2110
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +02002111 prop->lookup = lookup;
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002112 prop->get = get;
2113 prop->set = set;
2114
2115 object_class_property_add(klass, name, typename,
2116 get ? property_get_enum : NULL,
2117 set ? property_set_enum : NULL,
2118 property_release_enum,
2119 prop, &local_err);
2120 if (local_err) {
2121 error_propagate(errp, local_err);
2122 g_free(prop);
2123 }
2124}
2125
David Gibson8e099d12015-02-06 14:55:45 +11002126typedef struct TMProperty {
2127 void (*get)(Object *, struct tm *, Error **);
2128} TMProperty;
2129
Eric Blaked7bce992016-01-29 06:48:55 -07002130static void property_get_tm(Object *obj, Visitor *v, const char *name,
2131 void *opaque, Error **errp)
David Gibson8e099d12015-02-06 14:55:45 +11002132{
2133 TMProperty *prop = opaque;
2134 Error *err = NULL;
2135 struct tm value;
2136
2137 prop->get(obj, &value, &err);
2138 if (err) {
2139 goto out;
2140 }
2141
Eric Blake337283d2016-01-29 06:48:57 -07002142 visit_start_struct(v, name, NULL, 0, &err);
David Gibson8e099d12015-02-06 14:55:45 +11002143 if (err) {
2144 goto out;
2145 }
Eric Blake51e72bc2016-01-29 06:48:54 -07002146 visit_type_int32(v, "tm_year", &value.tm_year, &err);
David Gibson8e099d12015-02-06 14:55:45 +11002147 if (err) {
2148 goto out_end;
2149 }
Eric Blake51e72bc2016-01-29 06:48:54 -07002150 visit_type_int32(v, "tm_mon", &value.tm_mon, &err);
David Gibson8e099d12015-02-06 14:55:45 +11002151 if (err) {
2152 goto out_end;
2153 }
Eric Blake51e72bc2016-01-29 06:48:54 -07002154 visit_type_int32(v, "tm_mday", &value.tm_mday, &err);
David Gibson8e099d12015-02-06 14:55:45 +11002155 if (err) {
2156 goto out_end;
2157 }
Eric Blake51e72bc2016-01-29 06:48:54 -07002158 visit_type_int32(v, "tm_hour", &value.tm_hour, &err);
David Gibson8e099d12015-02-06 14:55:45 +11002159 if (err) {
2160 goto out_end;
2161 }
Eric Blake51e72bc2016-01-29 06:48:54 -07002162 visit_type_int32(v, "tm_min", &value.tm_min, &err);
David Gibson8e099d12015-02-06 14:55:45 +11002163 if (err) {
2164 goto out_end;
2165 }
Eric Blake51e72bc2016-01-29 06:48:54 -07002166 visit_type_int32(v, "tm_sec", &value.tm_sec, &err);
David Gibson8e099d12015-02-06 14:55:45 +11002167 if (err) {
2168 goto out_end;
2169 }
Eric Blake15c2f662016-04-28 15:45:27 -06002170 visit_check_struct(v, &err);
David Gibson8e099d12015-02-06 14:55:45 +11002171out_end:
Eric Blake1158bb22016-06-09 10:48:34 -06002172 visit_end_struct(v, NULL);
David Gibson8e099d12015-02-06 14:55:45 +11002173out:
2174 error_propagate(errp, err);
2175
2176}
2177
2178static void property_release_tm(Object *obj, const char *name,
2179 void *opaque)
2180{
2181 TMProperty *prop = opaque;
2182 g_free(prop);
2183}
2184
2185void object_property_add_tm(Object *obj, const char *name,
2186 void (*get)(Object *, struct tm *, Error **),
2187 Error **errp)
2188{
2189 Error *local_err = NULL;
2190 TMProperty *prop = g_malloc0(sizeof(*prop));
2191
2192 prop->get = get;
2193
2194 object_property_add(obj, name, "struct tm",
2195 get ? property_get_tm : NULL, NULL,
2196 property_release_tm,
2197 prop, &local_err);
2198 if (local_err) {
2199 error_propagate(errp, local_err);
2200 g_free(prop);
2201 }
2202}
2203
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002204void object_class_property_add_tm(ObjectClass *klass, const char *name,
2205 void (*get)(Object *, struct tm *, Error **),
2206 Error **errp)
2207{
2208 Error *local_err = NULL;
2209 TMProperty *prop = g_malloc0(sizeof(*prop));
2210
2211 prop->get = get;
2212
2213 object_class_property_add(klass, name, "struct tm",
2214 get ? property_get_tm : NULL, NULL,
2215 property_release_tm,
2216 prop, &local_err);
2217 if (local_err) {
2218 error_propagate(errp, local_err);
2219 g_free(prop);
2220 }
2221}
2222
Paolo Bonzini2f262e02012-04-02 17:33:51 +02002223static char *qdev_get_type(Object *obj, Error **errp)
2224{
2225 return g_strdup(object_get_typename(obj));
2226}
2227
Eric Blaked7bce992016-01-29 06:48:55 -07002228static void property_get_uint8_ptr(Object *obj, Visitor *v, const char *name,
2229 void *opaque, Error **errp)
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002230{
2231 uint8_t value = *(uint8_t *)opaque;
Eric Blake51e72bc2016-01-29 06:48:54 -07002232 visit_type_uint8(v, name, &value, errp);
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002233}
2234
Eric Blaked7bce992016-01-29 06:48:55 -07002235static void property_get_uint16_ptr(Object *obj, Visitor *v, const char *name,
2236 void *opaque, Error **errp)
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002237{
2238 uint16_t value = *(uint16_t *)opaque;
Eric Blake51e72bc2016-01-29 06:48:54 -07002239 visit_type_uint16(v, name, &value, errp);
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002240}
2241
Eric Blaked7bce992016-01-29 06:48:55 -07002242static void property_get_uint32_ptr(Object *obj, Visitor *v, const char *name,
2243 void *opaque, Error **errp)
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002244{
2245 uint32_t value = *(uint32_t *)opaque;
Eric Blake51e72bc2016-01-29 06:48:54 -07002246 visit_type_uint32(v, name, &value, errp);
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002247}
2248
Eric Blaked7bce992016-01-29 06:48:55 -07002249static void property_get_uint64_ptr(Object *obj, Visitor *v, const char *name,
2250 void *opaque, Error **errp)
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002251{
2252 uint64_t value = *(uint64_t *)opaque;
Eric Blake51e72bc2016-01-29 06:48:54 -07002253 visit_type_uint64(v, name, &value, errp);
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002254}
2255
2256void object_property_add_uint8_ptr(Object *obj, const char *name,
2257 const uint8_t *v, Error **errp)
2258{
2259 object_property_add(obj, name, "uint8", property_get_uint8_ptr,
2260 NULL, NULL, (void *)v, errp);
2261}
2262
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002263void object_class_property_add_uint8_ptr(ObjectClass *klass, const char *name,
2264 const uint8_t *v, Error **errp)
2265{
2266 object_class_property_add(klass, name, "uint8", property_get_uint8_ptr,
2267 NULL, NULL, (void *)v, errp);
2268}
2269
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002270void object_property_add_uint16_ptr(Object *obj, const char *name,
2271 const uint16_t *v, Error **errp)
2272{
2273 object_property_add(obj, name, "uint16", property_get_uint16_ptr,
2274 NULL, NULL, (void *)v, errp);
2275}
2276
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002277void object_class_property_add_uint16_ptr(ObjectClass *klass, const char *name,
2278 const uint16_t *v, Error **errp)
2279{
2280 object_class_property_add(klass, name, "uint16", property_get_uint16_ptr,
2281 NULL, NULL, (void *)v, errp);
2282}
2283
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002284void object_property_add_uint32_ptr(Object *obj, const char *name,
2285 const uint32_t *v, Error **errp)
2286{
2287 object_property_add(obj, name, "uint32", property_get_uint32_ptr,
2288 NULL, NULL, (void *)v, errp);
2289}
2290
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002291void object_class_property_add_uint32_ptr(ObjectClass *klass, const char *name,
2292 const uint32_t *v, Error **errp)
2293{
2294 object_class_property_add(klass, name, "uint32", property_get_uint32_ptr,
2295 NULL, NULL, (void *)v, errp);
2296}
2297
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002298void object_property_add_uint64_ptr(Object *obj, const char *name,
2299 const uint64_t *v, Error **errp)
2300{
2301 object_property_add(obj, name, "uint64", property_get_uint64_ptr,
2302 NULL, NULL, (void *)v, errp);
2303}
2304
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002305void object_class_property_add_uint64_ptr(ObjectClass *klass, const char *name,
2306 const uint64_t *v, Error **errp)
2307{
2308 object_class_property_add(klass, name, "uint64", property_get_uint64_ptr,
2309 NULL, NULL, (void *)v, errp);
2310}
2311
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002312typedef struct {
2313 Object *target_obj;
Eduardo Habkost1590d262015-04-09 16:57:29 -03002314 char *target_name;
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002315} AliasProperty;
2316
Eric Blaked7bce992016-01-29 06:48:55 -07002317static void property_get_alias(Object *obj, Visitor *v, const char *name,
2318 void *opaque, Error **errp)
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002319{
2320 AliasProperty *prop = opaque;
2321
2322 object_property_get(prop->target_obj, v, prop->target_name, errp);
2323}
2324
Eric Blaked7bce992016-01-29 06:48:55 -07002325static void property_set_alias(Object *obj, Visitor *v, const char *name,
2326 void *opaque, Error **errp)
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002327{
2328 AliasProperty *prop = opaque;
2329
2330 object_property_set(prop->target_obj, v, prop->target_name, errp);
2331}
2332
Paolo Bonzini64607d02014-06-05 13:11:51 +02002333static Object *property_resolve_alias(Object *obj, void *opaque,
2334 const gchar *part)
2335{
2336 AliasProperty *prop = opaque;
2337
2338 return object_resolve_path_component(prop->target_obj, prop->target_name);
2339}
2340
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002341static void property_release_alias(Object *obj, const char *name, void *opaque)
2342{
2343 AliasProperty *prop = opaque;
2344
Eduardo Habkost1590d262015-04-09 16:57:29 -03002345 g_free(prop->target_name);
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002346 g_free(prop);
2347}
2348
2349void object_property_add_alias(Object *obj, const char *name,
2350 Object *target_obj, const char *target_name,
2351 Error **errp)
2352{
2353 AliasProperty *prop;
Paolo Bonzini64607d02014-06-05 13:11:51 +02002354 ObjectProperty *op;
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002355 ObjectProperty *target_prop;
Paolo Bonzinid1906982014-06-10 11:17:35 +02002356 gchar *prop_type;
Gonglei8ae9a9e2014-09-27 13:13:56 +08002357 Error *local_err = NULL;
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002358
2359 target_prop = object_property_find(target_obj, target_name, errp);
2360 if (!target_prop) {
2361 return;
2362 }
2363
Paolo Bonzinid1906982014-06-10 11:17:35 +02002364 if (object_property_is_child(target_prop)) {
2365 prop_type = g_strdup_printf("link%s",
2366 target_prop->type + strlen("child"));
2367 } else {
2368 prop_type = g_strdup(target_prop->type);
2369 }
2370
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002371 prop = g_malloc(sizeof(*prop));
2372 prop->target_obj = target_obj;
Eduardo Habkost1590d262015-04-09 16:57:29 -03002373 prop->target_name = g_strdup(target_name);
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002374
Paolo Bonzinid1906982014-06-10 11:17:35 +02002375 op = object_property_add(obj, name, prop_type,
Paolo Bonzini64607d02014-06-05 13:11:51 +02002376 property_get_alias,
2377 property_set_alias,
2378 property_release_alias,
Gonglei8ae9a9e2014-09-27 13:13:56 +08002379 prop, &local_err);
2380 if (local_err) {
2381 error_propagate(errp, local_err);
2382 g_free(prop);
2383 goto out;
2384 }
Paolo Bonzini64607d02014-06-05 13:11:51 +02002385 op->resolve = property_resolve_alias;
Paolo Bonzinid1906982014-06-10 11:17:35 +02002386
Andreas Färbera18bb412015-03-27 17:34:10 +01002387 object_property_set_description(obj, op->name,
Gonglei80742642014-10-07 14:33:21 +08002388 target_prop->description,
2389 &error_abort);
2390
Gonglei8ae9a9e2014-09-27 13:13:56 +08002391out:
Paolo Bonzinid1906982014-06-10 11:17:35 +02002392 g_free(prop_type);
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002393}
2394
Gonglei80742642014-10-07 14:33:21 +08002395void object_property_set_description(Object *obj, const char *name,
2396 const char *description, Error **errp)
2397{
2398 ObjectProperty *op;
2399
2400 op = object_property_find(obj, name, errp);
2401 if (!op) {
2402 return;
2403 }
2404
2405 g_free(op->description);
2406 op->description = g_strdup(description);
2407}
2408
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002409void object_class_property_set_description(ObjectClass *klass,
2410 const char *name,
2411 const char *description,
2412 Error **errp)
2413{
2414 ObjectProperty *op;
2415
2416 op = g_hash_table_lookup(klass->properties, name);
2417 if (!op) {
2418 error_setg(errp, "Property '.%s' not found", name);
2419 return;
2420 }
2421
2422 g_free(op->description);
2423 op->description = g_strdup(description);
2424}
2425
Paolo Bonzini2f262e02012-04-02 17:33:51 +02002426static void object_instance_init(Object *obj)
2427{
2428 object_property_add_str(obj, "type", qdev_get_type, NULL, NULL);
2429}
2430
Paolo Bonzini745549c2012-03-31 16:45:54 +02002431static void register_types(void)
2432{
2433 static TypeInfo interface_info = {
2434 .name = TYPE_INTERFACE,
Anthony Liguori33e95c62012-08-10 13:16:10 +10002435 .class_size = sizeof(InterfaceClass),
Paolo Bonzini745549c2012-03-31 16:45:54 +02002436 .abstract = true,
2437 };
2438
2439 static TypeInfo object_info = {
2440 .name = TYPE_OBJECT,
2441 .instance_size = sizeof(Object),
Paolo Bonzini2f262e02012-04-02 17:33:51 +02002442 .instance_init = object_instance_init,
Paolo Bonzini745549c2012-03-31 16:45:54 +02002443 .abstract = true,
2444 };
2445
Paolo Bonzini049cb3c2012-04-04 15:58:40 +02002446 type_interface = type_register_internal(&interface_info);
2447 type_register_internal(&object_info);
Paolo Bonzini745549c2012-03-31 16:45:54 +02002448}
2449
2450type_init(register_types)