blob: 2407b667d9f198452c54539a1e013f7037abc648 [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"
Hu Tao1f217722014-05-14 17:43:33 +080019#include "qapi-visit.h"
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +010020#include "qapi/string-input-visitor.h"
21#include "qapi/string-output-visitor.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/qobject.h"
29#include "qapi/qmp/qbool.h"
30#include "qapi/qmp/qint.h"
31#include "qapi/qmp/qstring.h"
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +010032
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060033#define MAX_INTERFACES 32
34
35typedef struct InterfaceImpl InterfaceImpl;
36typedef struct TypeImpl TypeImpl;
37
38struct InterfaceImpl
39{
Anthony Liguori33e95c62012-08-10 13:16:10 +100040 const char *typename;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060041};
42
43struct TypeImpl
44{
45 const char *name;
46
47 size_t class_size;
48
49 size_t instance_size;
50
51 void (*class_init)(ObjectClass *klass, void *data);
Paolo Bonzini3b50e312012-05-02 13:30:55 +020052 void (*class_base_init)(ObjectClass *klass, void *data);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060053 void (*class_finalize)(ObjectClass *klass, void *data);
54
55 void *class_data;
56
57 void (*instance_init)(Object *obj);
Eduardo Habkost8231c2d2013-07-10 17:08:41 -030058 void (*instance_post_init)(Object *obj);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060059 void (*instance_finalize)(Object *obj);
60
61 bool abstract;
62
63 const char *parent;
64 TypeImpl *parent_type;
65
66 ObjectClass *class;
67
68 int num_interfaces;
69 InterfaceImpl interfaces[MAX_INTERFACES];
70};
71
Paolo Bonzini9970bd82012-02-03 11:51:39 +010072static Type type_interface;
73
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060074static GHashTable *type_table_get(void)
75{
76 static GHashTable *type_table;
77
78 if (type_table == NULL) {
79 type_table = g_hash_table_new(g_str_hash, g_str_equal);
80 }
81
82 return type_table;
83}
84
Hervé Poussineauf54c19c2013-12-03 16:42:00 +010085static bool enumerating_types;
86
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060087static void type_table_add(TypeImpl *ti)
88{
Hervé Poussineauf54c19c2013-12-03 16:42:00 +010089 assert(!enumerating_types);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060090 g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
91}
92
93static TypeImpl *type_table_lookup(const char *name)
94{
95 return g_hash_table_lookup(type_table_get(), name);
96}
97
Paolo Bonzinib061dc42013-12-03 16:41:59 +010098static TypeImpl *type_new(const TypeInfo *info)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060099{
100 TypeImpl *ti = g_malloc0(sizeof(*ti));
Anthony Liguori33e95c62012-08-10 13:16:10 +1000101 int i;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600102
103 g_assert(info->name != NULL);
104
Anthony Liguori73093352012-01-25 13:37:36 -0600105 if (type_table_lookup(info->name) != NULL) {
106 fprintf(stderr, "Registering `%s' which already exists\n", info->name);
107 abort();
108 }
109
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600110 ti->name = g_strdup(info->name);
111 ti->parent = g_strdup(info->parent);
112
113 ti->class_size = info->class_size;
114 ti->instance_size = info->instance_size;
115
116 ti->class_init = info->class_init;
Paolo Bonzini3b50e312012-05-02 13:30:55 +0200117 ti->class_base_init = info->class_base_init;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600118 ti->class_finalize = info->class_finalize;
119 ti->class_data = info->class_data;
120
121 ti->instance_init = info->instance_init;
Eduardo Habkost8231c2d2013-07-10 17:08:41 -0300122 ti->instance_post_init = info->instance_post_init;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600123 ti->instance_finalize = info->instance_finalize;
124
125 ti->abstract = info->abstract;
126
Anthony Liguori33e95c62012-08-10 13:16:10 +1000127 for (i = 0; info->interfaces && info->interfaces[i].type; i++) {
128 ti->interfaces[i].typename = g_strdup(info->interfaces[i].type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600129 }
Anthony Liguori33e95c62012-08-10 13:16:10 +1000130 ti->num_interfaces = i;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600131
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100132 return ti;
133}
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600134
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100135static TypeImpl *type_register_internal(const TypeInfo *info)
136{
137 TypeImpl *ti;
138 ti = type_new(info);
139
140 type_table_add(ti);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600141 return ti;
142}
143
Paolo Bonzini049cb3c2012-04-04 15:58:40 +0200144TypeImpl *type_register(const TypeInfo *info)
145{
146 assert(info->parent);
147 return type_register_internal(info);
148}
149
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600150TypeImpl *type_register_static(const TypeInfo *info)
151{
152 return type_register(info);
153}
154
155static TypeImpl *type_get_by_name(const char *name)
156{
157 if (name == NULL) {
158 return NULL;
159 }
160
161 return type_table_lookup(name);
162}
163
164static TypeImpl *type_get_parent(TypeImpl *type)
165{
166 if (!type->parent_type && type->parent) {
167 type->parent_type = type_get_by_name(type->parent);
168 g_assert(type->parent_type != NULL);
169 }
170
171 return type->parent_type;
172}
173
174static bool type_has_parent(TypeImpl *type)
175{
176 return (type->parent != NULL);
177}
178
179static size_t type_class_get_size(TypeImpl *ti)
180{
181 if (ti->class_size) {
182 return ti->class_size;
183 }
184
185 if (type_has_parent(ti)) {
186 return type_class_get_size(type_get_parent(ti));
187 }
188
189 return sizeof(ObjectClass);
190}
191
Igor Mitsyankoaca59af2012-02-28 15:57:10 +0400192static size_t type_object_get_size(TypeImpl *ti)
193{
194 if (ti->instance_size) {
195 return ti->instance_size;
196 }
197
198 if (type_has_parent(ti)) {
199 return type_object_get_size(type_get_parent(ti));
200 }
201
202 return 0;
203}
204
Bharata B Rao3f97b532016-06-10 06:29:00 +0530205size_t object_type_get_instance_size(const char *typename)
206{
207 TypeImpl *type = type_get_by_name(typename);
208
209 g_assert(type != NULL);
210 return type_object_get_size(type);
211}
212
Anthony Liguori33e95c62012-08-10 13:16:10 +1000213static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600214{
Anthony Liguori33e95c62012-08-10 13:16:10 +1000215 assert(target_type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600216
Cao jinb30d8052015-11-03 10:36:42 +0800217 /* Check if target_type is a direct ancestor of type */
Anthony Liguori33e95c62012-08-10 13:16:10 +1000218 while (type) {
219 if (type == target_type) {
220 return true;
221 }
222
223 type = type_get_parent(type);
224 }
225
226 return false;
227}
228
229static void type_initialize(TypeImpl *ti);
230
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100231static void type_initialize_interface(TypeImpl *ti, TypeImpl *interface_type,
232 TypeImpl *parent_type)
Anthony Liguori33e95c62012-08-10 13:16:10 +1000233{
234 InterfaceClass *new_iface;
235 TypeInfo info = { };
236 TypeImpl *iface_impl;
237
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100238 info.parent = parent_type->name;
239 info.name = g_strdup_printf("%s::%s", ti->name, interface_type->name);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000240 info.abstract = true;
241
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100242 iface_impl = type_new(&info);
243 iface_impl->parent_type = parent_type;
Anthony Liguori33e95c62012-08-10 13:16:10 +1000244 type_initialize(iface_impl);
245 g_free((char *)info.name);
246
247 new_iface = (InterfaceClass *)iface_impl->class;
248 new_iface->concrete_class = ti->class;
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100249 new_iface->interface_type = interface_type;
Anthony Liguori33e95c62012-08-10 13:16:10 +1000250
251 ti->class->interfaces = g_slist_append(ti->class->interfaces,
252 iface_impl->class);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600253}
254
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +0100255static void object_property_free(gpointer data)
256{
257 ObjectProperty *prop = data;
258
259 g_free(prop->name);
260 g_free(prop->type);
261 g_free(prop->description);
262 g_free(prop);
263}
264
Igor Mitsyankoac451032012-02-28 15:57:11 +0400265static void type_initialize(TypeImpl *ti)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600266{
Paolo Bonzini745549c2012-03-31 16:45:54 +0200267 TypeImpl *parent;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600268
269 if (ti->class) {
270 return;
271 }
272
273 ti->class_size = type_class_get_size(ti);
Igor Mitsyankoaca59af2012-02-28 15:57:10 +0400274 ti->instance_size = type_object_get_size(ti);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600275
276 ti->class = g_malloc0(ti->class_size);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600277
Paolo Bonzini745549c2012-03-31 16:45:54 +0200278 parent = type_get_parent(ti);
279 if (parent) {
Igor Mitsyankoac451032012-02-28 15:57:11 +0400280 type_initialize(parent);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000281 GSList *e;
282 int i;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600283
Andreas Färber8438a132015-11-16 17:49:20 +0100284 g_assert_cmpint(parent->class_size, <=, ti->class_size);
Paolo Bonzini745549c2012-03-31 16:45:54 +0200285 memcpy(ti->class, parent->class, parent->class_size);
Peter Crosthwaite3e407de2013-02-19 14:02:09 +1000286 ti->class->interfaces = NULL;
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +0100287 ti->class->properties = g_hash_table_new_full(
288 g_str_hash, g_str_equal, g_free, object_property_free);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000289
290 for (e = parent->class->interfaces; e; e = e->next) {
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100291 InterfaceClass *iface = e->data;
292 ObjectClass *klass = OBJECT_CLASS(iface);
293
294 type_initialize_interface(ti, iface->interface_type, klass->type);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000295 }
296
297 for (i = 0; i < ti->num_interfaces; i++) {
298 TypeImpl *t = type_get_by_name(ti->interfaces[i].typename);
299 for (e = ti->class->interfaces; e; e = e->next) {
300 TypeImpl *target_type = OBJECT_CLASS(e->data)->type;
301
302 if (type_is_ancestor(target_type, t)) {
303 break;
304 }
305 }
306
307 if (e) {
308 continue;
309 }
310
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100311 type_initialize_interface(ti, t, t);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000312 }
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +0100313 } else {
314 ti->class->properties = g_hash_table_new_full(
315 g_str_hash, g_str_equal, g_free, object_property_free);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600316 }
317
Paolo Bonzini745549c2012-03-31 16:45:54 +0200318 ti->class->type = ti;
319
320 while (parent) {
321 if (parent->class_base_init) {
322 parent->class_base_init(ti->class, ti->class_data);
323 }
324 parent = type_get_parent(parent);
325 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600326
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600327 if (ti->class_init) {
328 ti->class_init(ti->class, ti->class_data);
329 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600330}
331
332static void object_init_with_type(Object *obj, TypeImpl *ti)
333{
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600334 if (type_has_parent(ti)) {
335 object_init_with_type(obj, type_get_parent(ti));
336 }
337
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600338 if (ti->instance_init) {
339 ti->instance_init(obj);
340 }
341}
342
Eduardo Habkost8231c2d2013-07-10 17:08:41 -0300343static void object_post_init_with_type(Object *obj, TypeImpl *ti)
344{
345 if (ti->instance_post_init) {
346 ti->instance_post_init(obj);
347 }
348
349 if (type_has_parent(ti)) {
350 object_post_init_with_type(obj, type_get_parent(ti));
351 }
352}
353
Andreas Färber5b9237f2013-08-30 18:28:37 +0200354void object_initialize_with_type(void *data, size_t size, TypeImpl *type)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600355{
356 Object *obj = data;
357
358 g_assert(type != NULL);
Igor Mitsyankoac451032012-02-28 15:57:11 +0400359 type_initialize(type);
Igor Mitsyankoaca59af2012-02-28 15:57:10 +0400360
Andreas Färber8438a132015-11-16 17:49:20 +0100361 g_assert_cmpint(type->instance_size, >=, sizeof(Object));
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600362 g_assert(type->abstract == false);
Andreas Färber8438a132015-11-16 17:49:20 +0100363 g_assert_cmpint(size, >=, type->instance_size);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600364
365 memset(obj, 0, type->instance_size);
366 obj->class = type->class;
Paolo Bonzini764b6312012-11-23 09:47:12 +0100367 object_ref(obj);
Pavel Fedinb604a852015-10-13 13:37:45 +0100368 obj->properties = g_hash_table_new_full(g_str_hash, g_str_equal,
369 NULL, object_property_free);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600370 object_init_with_type(obj, type);
Eduardo Habkost8231c2d2013-07-10 17:08:41 -0300371 object_post_init_with_type(obj, type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600372}
373
Andreas Färber213f0c42013-08-23 19:37:12 +0200374void object_initialize(void *data, size_t size, const char *typename)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600375{
376 TypeImpl *type = type_get_by_name(typename);
377
Andreas Färber5b9237f2013-08-30 18:28:37 +0200378 object_initialize_with_type(data, size, type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600379}
380
Andreas Färber5d9d3f42012-05-27 00:32:40 +0200381static inline bool object_property_is_child(ObjectProperty *prop)
382{
383 return strstart(prop->type, "child<", NULL);
384}
385
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600386static void object_property_del_all(Object *obj)
387{
Pavel Fedinb604a852015-10-13 13:37:45 +0100388 ObjectProperty *prop;
389 GHashTableIter iter;
390 gpointer key, value;
391 bool released;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600392
Pavel Fedinb604a852015-10-13 13:37:45 +0100393 do {
394 released = false;
395 g_hash_table_iter_init(&iter, obj->properties);
396 while (g_hash_table_iter_next(&iter, &key, &value)) {
397 prop = value;
398 if (prop->release) {
399 prop->release(obj, prop->name, prop->opaque);
400 prop->release = NULL;
401 released = true;
402 break;
403 }
404 g_hash_table_iter_remove(&iter);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600405 }
Pavel Fedinb604a852015-10-13 13:37:45 +0100406 } while (released);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600407
Pavel Fedinb604a852015-10-13 13:37:45 +0100408 g_hash_table_unref(obj->properties);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600409}
410
411static void object_property_del_child(Object *obj, Object *child, Error **errp)
412{
413 ObjectProperty *prop;
Pavel Fedinb604a852015-10-13 13:37:45 +0100414 GHashTableIter iter;
415 gpointer key, value;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600416
Pavel Fedinb604a852015-10-13 13:37:45 +0100417 g_hash_table_iter_init(&iter, obj->properties);
418 while (g_hash_table_iter_next(&iter, &key, &value)) {
419 prop = value;
Andreas Färber5d9d3f42012-05-27 00:32:40 +0200420 if (object_property_is_child(prop) && prop->opaque == child) {
Pavel Fedinb604a852015-10-13 13:37:45 +0100421 if (prop->release) {
422 prop->release(obj, prop->name, prop->opaque);
423 prop->release = NULL;
424 }
425 break;
426 }
427 }
428 g_hash_table_iter_init(&iter, obj->properties);
429 while (g_hash_table_iter_next(&iter, &key, &value)) {
430 prop = value;
431 if (object_property_is_child(prop) && prop->opaque == child) {
432 g_hash_table_iter_remove(&iter);
Paolo Bonzini6c1fdcf2012-02-28 09:54:15 +0100433 break;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600434 }
435 }
436}
437
438void object_unparent(Object *obj)
439{
Michael S. Tsirkine998fa82013-03-18 21:01:37 +0200440 if (obj->parent) {
441 object_property_del_child(obj->parent, obj, NULL);
442 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600443}
444
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600445static void object_deinit(Object *obj, TypeImpl *type)
446{
447 if (type->instance_finalize) {
448 type->instance_finalize(obj);
449 }
450
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600451 if (type_has_parent(type)) {
452 object_deinit(obj, type_get_parent(type));
453 }
454}
455
Paolo Bonzini339c2702012-11-23 09:47:16 +0100456static void object_finalize(void *data)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600457{
458 Object *obj = data;
459 TypeImpl *ti = obj->class->type;
460
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600461 object_property_del_all(obj);
Paolo Bonzini76a6e1c2014-06-11 11:58:30 +0200462 object_deinit(obj, ti);
Anthony Liguoridb85b572011-12-23 08:47:39 -0600463
Andreas Färber8438a132015-11-16 17:49:20 +0100464 g_assert_cmpint(obj->ref, ==, 0);
Paolo Bonzinifde9bf42012-11-23 09:47:14 +0100465 if (obj->free) {
466 obj->free(obj);
467 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600468}
469
470Object *object_new_with_type(Type type)
471{
472 Object *obj;
473
474 g_assert(type != NULL);
Igor Mitsyankoac451032012-02-28 15:57:11 +0400475 type_initialize(type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600476
477 obj = g_malloc(type->instance_size);
Andreas Färber5b9237f2013-08-30 18:28:37 +0200478 object_initialize_with_type(obj, type->instance_size, type);
Paolo Bonzinifde9bf42012-11-23 09:47:14 +0100479 obj->free = g_free;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600480
481 return obj;
482}
483
484Object *object_new(const char *typename)
485{
486 TypeImpl *ti = type_get_by_name(typename);
487
488 return object_new_with_type(ti);
489}
490
Daniel P. Berrangea31bdae2015-05-13 17:14:06 +0100491
492Object *object_new_with_props(const char *typename,
493 Object *parent,
494 const char *id,
495 Error **errp,
496 ...)
497{
498 va_list vargs;
499 Object *obj;
500
501 va_start(vargs, errp);
502 obj = object_new_with_propv(typename, parent, id, errp, vargs);
503 va_end(vargs);
504
505 return obj;
506}
507
508
509Object *object_new_with_propv(const char *typename,
510 Object *parent,
511 const char *id,
512 Error **errp,
513 va_list vargs)
514{
515 Object *obj;
516 ObjectClass *klass;
517 Error *local_err = NULL;
518
519 klass = object_class_by_name(typename);
520 if (!klass) {
521 error_setg(errp, "invalid object type: %s", typename);
522 return NULL;
523 }
524
525 if (object_class_is_abstract(klass)) {
526 error_setg(errp, "object type '%s' is abstract", typename);
527 return NULL;
528 }
529 obj = object_new(typename);
530
531 if (object_set_propv(obj, &local_err, vargs) < 0) {
532 goto error;
533 }
534
535 object_property_add_child(parent, id, obj, &local_err);
536 if (local_err) {
537 goto error;
538 }
539
540 if (object_dynamic_cast(obj, TYPE_USER_CREATABLE)) {
541 user_creatable_complete(obj, &local_err);
542 if (local_err) {
543 object_unparent(obj);
544 goto error;
545 }
546 }
547
548 object_unref(OBJECT(obj));
549 return obj;
550
551 error:
Eduardo Habkost621ff942016-06-13 18:57:56 -0300552 error_propagate(errp, local_err);
Daniel P. Berrangea31bdae2015-05-13 17:14:06 +0100553 object_unref(obj);
554 return NULL;
555}
556
557
558int object_set_props(Object *obj,
559 Error **errp,
560 ...)
561{
562 va_list vargs;
563 int ret;
564
565 va_start(vargs, errp);
566 ret = object_set_propv(obj, errp, vargs);
567 va_end(vargs);
568
569 return ret;
570}
571
572
573int object_set_propv(Object *obj,
574 Error **errp,
575 va_list vargs)
576{
577 const char *propname;
578 Error *local_err = NULL;
579
580 propname = va_arg(vargs, char *);
581 while (propname != NULL) {
582 const char *value = va_arg(vargs, char *);
583
584 g_assert(value != NULL);
585 object_property_parse(obj, value, propname, &local_err);
586 if (local_err) {
587 error_propagate(errp, local_err);
588 return -1;
589 }
590 propname = va_arg(vargs, char *);
591 }
592
593 return 0;
594}
595
596
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600597Object *object_dynamic_cast(Object *obj, const char *typename)
598{
Paolo Bonzinib7f43fe2012-11-23 16:56:17 +0100599 if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) {
Paolo Bonziniacc4af32012-02-03 11:57:23 +0100600 return obj;
601 }
602
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600603 return NULL;
604}
605
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200606Object *object_dynamic_cast_assert(Object *obj, const char *typename,
607 const char *file, int line, const char *func)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600608{
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200609 trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)",
610 typename, file, line, func);
611
Paolo Bonzini3556c232013-05-10 14:16:40 +0200612#ifdef CONFIG_QOM_CAST_DEBUG
Anthony Liguori03587322013-05-13 15:22:24 -0500613 int i;
614 Object *inst;
615
Peter Crosthwaite95916ab2013-05-22 11:19:16 +1000616 for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) {
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800617 if (obj->class->object_cast_cache[i] == typename) {
Anthony Liguori03587322013-05-13 15:22:24 -0500618 goto out;
619 }
620 }
621
622 inst = object_dynamic_cast(obj, typename);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600623
Paolo Bonzinib7f43fe2012-11-23 16:56:17 +0100624 if (!inst && obj) {
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200625 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
626 file, line, func, obj, typename);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600627 abort();
628 }
629
Paolo Bonzini3556c232013-05-10 14:16:40 +0200630 assert(obj == inst);
Anthony Liguori03587322013-05-13 15:22:24 -0500631
Peter Crosthwaite95916ab2013-05-22 11:19:16 +1000632 if (obj && obj == inst) {
Anthony Liguori03587322013-05-13 15:22:24 -0500633 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800634 obj->class->object_cast_cache[i - 1] =
635 obj->class->object_cast_cache[i];
Anthony Liguori03587322013-05-13 15:22:24 -0500636 }
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800637 obj->class->object_cast_cache[i - 1] = typename;
Anthony Liguori03587322013-05-13 15:22:24 -0500638 }
639
640out:
Paolo Bonzini3556c232013-05-10 14:16:40 +0200641#endif
642 return obj;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600643}
644
645ObjectClass *object_class_dynamic_cast(ObjectClass *class,
646 const char *typename)
647{
Anthony Liguori33e95c62012-08-10 13:16:10 +1000648 ObjectClass *ret = NULL;
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200649 TypeImpl *target_type;
650 TypeImpl *type;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600651
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200652 if (!class) {
653 return NULL;
654 }
655
Paolo Bonzini793c96b2013-05-10 14:16:37 +0200656 /* A simple fast path that can trigger a lot for leaf classes. */
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200657 type = class->type;
Paolo Bonzini793c96b2013-05-10 14:16:37 +0200658 if (type->name == typename) {
659 return class;
660 }
661
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200662 target_type = type_get_by_name(typename);
Alexander Graf9ab880b2013-04-30 15:02:16 +0200663 if (!target_type) {
664 /* target class type unknown, so fail the cast */
665 return NULL;
666 }
667
Peter Crosthwaite00e2cea2013-02-19 14:02:10 +1000668 if (type->class->interfaces &&
669 type_is_ancestor(target_type, type_interface)) {
Anthony Liguori33e95c62012-08-10 13:16:10 +1000670 int found = 0;
671 GSList *i;
672
673 for (i = class->interfaces; i; i = i->next) {
674 ObjectClass *target_class = i->data;
675
676 if (type_is_ancestor(target_class->type, target_type)) {
677 ret = target_class;
678 found++;
679 }
680 }
681
682 /* The match was ambiguous, don't allow a cast */
683 if (found > 1) {
684 ret = NULL;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600685 }
Anthony Liguori33e95c62012-08-10 13:16:10 +1000686 } else if (type_is_ancestor(type, target_type)) {
687 ret = class;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600688 }
689
Anthony Liguori33e95c62012-08-10 13:16:10 +1000690 return ret;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600691}
692
693ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200694 const char *typename,
695 const char *file, int line,
696 const char *func)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600697{
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200698 ObjectClass *ret;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600699
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200700 trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)",
701 typename, file, line, func);
702
Anthony Liguori03587322013-05-13 15:22:24 -0500703#ifdef CONFIG_QOM_CAST_DEBUG
704 int i;
705
Peter Crosthwaite9d6a3d52013-06-18 19:18:59 +1000706 for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) {
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800707 if (class->class_cast_cache[i] == typename) {
Anthony Liguori03587322013-05-13 15:22:24 -0500708 ret = class;
709 goto out;
710 }
711 }
712#else
Peter Crosthwaite9d6a3d52013-06-18 19:18:59 +1000713 if (!class || !class->interfaces) {
Paolo Bonzini3556c232013-05-10 14:16:40 +0200714 return class;
715 }
716#endif
717
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200718 ret = object_class_dynamic_cast(class, typename);
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200719 if (!ret && class) {
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200720 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
721 file, line, func, class, typename);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600722 abort();
723 }
724
Anthony Liguori03587322013-05-13 15:22:24 -0500725#ifdef CONFIG_QOM_CAST_DEBUG
Peter Crosthwaite9d6a3d52013-06-18 19:18:59 +1000726 if (class && ret == class) {
Anthony Liguori03587322013-05-13 15:22:24 -0500727 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800728 class->class_cast_cache[i - 1] = class->class_cast_cache[i];
Anthony Liguori03587322013-05-13 15:22:24 -0500729 }
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800730 class->class_cast_cache[i - 1] = typename;
Anthony Liguori03587322013-05-13 15:22:24 -0500731 }
732out:
733#endif
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600734 return ret;
735}
736
737const char *object_get_typename(Object *obj)
738{
739 return obj->class->type->name;
740}
741
742ObjectClass *object_get_class(Object *obj)
743{
744 return obj->class;
745}
746
Andreas Färber17862372013-01-23 12:20:18 +0100747bool object_class_is_abstract(ObjectClass *klass)
748{
749 return klass->type->abstract;
750}
751
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600752const char *object_class_get_name(ObjectClass *klass)
753{
754 return klass->type->name;
755}
756
757ObjectClass *object_class_by_name(const char *typename)
758{
759 TypeImpl *type = type_get_by_name(typename);
760
761 if (!type) {
762 return NULL;
763 }
764
Igor Mitsyankoac451032012-02-28 15:57:11 +0400765 type_initialize(type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600766
767 return type->class;
768}
769
Paolo Bonzinie7cce672012-05-02 13:30:54 +0200770ObjectClass *object_class_get_parent(ObjectClass *class)
771{
772 TypeImpl *type = type_get_parent(class->type);
773
774 if (!type) {
775 return NULL;
776 }
777
778 type_initialize(type);
779
780 return type->class;
781}
782
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600783typedef struct OCFData
784{
785 void (*fn)(ObjectClass *klass, void *opaque);
Anthony Liguori93c511a2011-12-22 14:11:53 -0600786 const char *implements_type;
787 bool include_abstract;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600788 void *opaque;
789} OCFData;
790
791static void object_class_foreach_tramp(gpointer key, gpointer value,
792 gpointer opaque)
793{
794 OCFData *data = opaque;
795 TypeImpl *type = value;
Anthony Liguori93c511a2011-12-22 14:11:53 -0600796 ObjectClass *k;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600797
Igor Mitsyankoac451032012-02-28 15:57:11 +0400798 type_initialize(type);
Anthony Liguori93c511a2011-12-22 14:11:53 -0600799 k = type->class;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600800
Anthony Liguori93c511a2011-12-22 14:11:53 -0600801 if (!data->include_abstract && type->abstract) {
802 return;
803 }
804
805 if (data->implements_type &&
806 !object_class_dynamic_cast(k, data->implements_type)) {
807 return;
808 }
809
810 data->fn(k, data->opaque);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600811}
812
813void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
Anthony Liguori93c511a2011-12-22 14:11:53 -0600814 const char *implements_type, bool include_abstract,
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600815 void *opaque)
816{
Anthony Liguori93c511a2011-12-22 14:11:53 -0600817 OCFData data = { fn, implements_type, include_abstract, opaque };
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600818
Hervé Poussineauf54c19c2013-12-03 16:42:00 +0100819 enumerating_types = true;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600820 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
Hervé Poussineauf54c19c2013-12-03 16:42:00 +0100821 enumerating_types = false;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600822}
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600823
Peter Crosthwaited714b8d2015-09-08 17:38:43 +0100824static int do_object_child_foreach(Object *obj,
825 int (*fn)(Object *child, void *opaque),
826 void *opaque, bool recurse)
Paolo Bonzini32efc532012-04-11 23:30:20 +0200827{
Pavel Fedinb604a852015-10-13 13:37:45 +0100828 GHashTableIter iter;
829 ObjectProperty *prop;
Paolo Bonzini32efc532012-04-11 23:30:20 +0200830 int ret = 0;
831
Pavel Fedinb604a852015-10-13 13:37:45 +0100832 g_hash_table_iter_init(&iter, obj->properties);
833 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
Paolo Bonzini32efc532012-04-11 23:30:20 +0200834 if (object_property_is_child(prop)) {
Peter Crosthwaited714b8d2015-09-08 17:38:43 +0100835 Object *child = prop->opaque;
836
837 ret = fn(child, opaque);
Paolo Bonzini32efc532012-04-11 23:30:20 +0200838 if (ret != 0) {
839 break;
840 }
Peter Crosthwaited714b8d2015-09-08 17:38:43 +0100841 if (recurse) {
842 do_object_child_foreach(child, fn, opaque, true);
843 }
Paolo Bonzini32efc532012-04-11 23:30:20 +0200844 }
845 }
846 return ret;
847}
848
Peter Crosthwaited714b8d2015-09-08 17:38:43 +0100849int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
850 void *opaque)
851{
852 return do_object_child_foreach(obj, fn, opaque, false);
853}
854
855int object_child_foreach_recursive(Object *obj,
856 int (*fn)(Object *child, void *opaque),
857 void *opaque)
858{
859 return do_object_child_foreach(obj, fn, opaque, true);
860}
861
Andreas Färber418ba9e2012-02-25 23:07:34 +0100862static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
863{
864 GSList **list = opaque;
865
866 *list = g_slist_prepend(*list, klass);
867}
868
869GSList *object_class_get_list(const char *implements_type,
870 bool include_abstract)
871{
872 GSList *list = NULL;
873
874 object_class_foreach(object_class_get_list_tramp,
875 implements_type, include_abstract, &list);
876 return list;
877}
878
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600879void object_ref(Object *obj)
880{
Peter Crosthwaite8ffad852014-06-05 23:13:36 -0700881 if (!obj) {
882 return;
883 }
Andreas Färber8438a132015-11-16 17:49:20 +0100884 atomic_inc(&obj->ref);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600885}
886
887void object_unref(Object *obj)
888{
Peter Crosthwaite8ffad852014-06-05 23:13:36 -0700889 if (!obj) {
890 return;
891 }
Andreas Färber8438a132015-11-16 17:49:20 +0100892 g_assert_cmpint(obj->ref, >, 0);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600893
894 /* parent always holds a reference to its children */
Jan Kiszkaf08c03f2013-07-02 11:36:39 +0200895 if (atomic_fetch_dec(&obj->ref) == 1) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600896 object_finalize(obj);
897 }
898}
899
Paolo Bonzini64607d02014-06-05 13:11:51 +0200900ObjectProperty *
901object_property_add(Object *obj, const char *name, const char *type,
902 ObjectPropertyAccessor *get,
903 ObjectPropertyAccessor *set,
904 ObjectPropertyRelease *release,
905 void *opaque, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600906{
Peter Maydell54852b02013-03-25 13:15:13 +0000907 ObjectProperty *prop;
Peter Crosthwaite33965902014-08-19 23:55:52 -0700908 size_t name_len = strlen(name);
909
910 if (name_len >= 3 && !memcmp(name + name_len - 3, "[*]", 4)) {
911 int i;
912 ObjectProperty *ret;
913 char *name_no_array = g_strdup(name);
914
915 name_no_array[name_len - 3] = '\0';
916 for (i = 0; ; ++i) {
917 char *full_name = g_strdup_printf("%s[%d]", name_no_array, i);
918
919 ret = object_property_add(obj, full_name, type, get, set,
920 release, opaque, NULL);
921 g_free(full_name);
922 if (ret) {
923 break;
924 }
925 }
926 g_free(name_no_array);
927 return ret;
928 }
Peter Maydell54852b02013-03-25 13:15:13 +0000929
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +0100930 if (object_property_find(obj, name, NULL) != NULL) {
Pavel Fedinb604a852015-10-13 13:37:45 +0100931 error_setg(errp, "attempt to add duplicate property '%s'"
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +0100932 " to object (type '%s')", name,
933 object_get_typename(obj));
Pavel Fedinb604a852015-10-13 13:37:45 +0100934 return NULL;
Peter Maydell54852b02013-03-25 13:15:13 +0000935 }
936
937 prop = g_malloc0(sizeof(*prop));
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600938
939 prop->name = g_strdup(name);
940 prop->type = g_strdup(type);
941
942 prop->get = get;
943 prop->set = set;
944 prop->release = release;
945 prop->opaque = opaque;
946
Pavel Fedinb604a852015-10-13 13:37:45 +0100947 g_hash_table_insert(obj->properties, prop->name, prop);
Paolo Bonzini64607d02014-06-05 13:11:51 +0200948 return prop;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600949}
950
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +0100951ObjectProperty *
952object_class_property_add(ObjectClass *klass,
953 const char *name,
954 const char *type,
955 ObjectPropertyAccessor *get,
956 ObjectPropertyAccessor *set,
957 ObjectPropertyRelease *release,
958 void *opaque,
959 Error **errp)
960{
961 ObjectProperty *prop;
962
963 if (object_class_property_find(klass, name, NULL) != NULL) {
964 error_setg(errp, "attempt to add duplicate property '%s'"
965 " to object (type '%s')", name,
966 object_class_get_name(klass));
967 return NULL;
968 }
969
970 prop = g_malloc0(sizeof(*prop));
971
972 prop->name = g_strdup(name);
973 prop->type = g_strdup(type);
974
975 prop->get = get;
976 prop->set = set;
977 prop->release = release;
978 prop->opaque = opaque;
979
980 g_hash_table_insert(klass->properties, g_strdup(name), prop);
981
982 return prop;
983}
984
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200985ObjectProperty *object_property_find(Object *obj, const char *name,
986 Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600987{
988 ObjectProperty *prop;
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +0100989 ObjectClass *klass = object_get_class(obj);
990
991 prop = object_class_property_find(klass, name, NULL);
992 if (prop) {
993 return prop;
994 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600995
Pavel Fedinb604a852015-10-13 13:37:45 +0100996 prop = g_hash_table_lookup(obj->properties, name);
997 if (prop) {
998 return prop;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600999 }
1000
Cole Robinsonf231b882014-03-21 19:42:26 -04001001 error_setg(errp, "Property '.%s' not found", name);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001002 return NULL;
1003}
1004
Daniel P. Berrange7746abd2015-12-09 12:34:02 +00001005void object_property_iter_init(ObjectPropertyIterator *iter,
1006 Object *obj)
Daniel P. Berrangea00c9482015-10-13 13:37:40 +01001007{
Daniel P. Berrange7746abd2015-12-09 12:34:02 +00001008 g_hash_table_iter_init(&iter->iter, obj->properties);
1009 iter->nextclass = object_get_class(obj);
Daniel P. Berrangea00c9482015-10-13 13:37:40 +01001010}
1011
1012ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter)
1013{
Pavel Fedinb604a852015-10-13 13:37:45 +01001014 gpointer key, val;
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001015 while (!g_hash_table_iter_next(&iter->iter, &key, &val)) {
1016 if (!iter->nextclass) {
1017 return NULL;
1018 }
1019 g_hash_table_iter_init(&iter->iter, iter->nextclass->properties);
1020 iter->nextclass = object_class_get_parent(iter->nextclass);
Daniel P. Berrangea00c9482015-10-13 13:37:40 +01001021 }
Pavel Fedinb604a852015-10-13 13:37:45 +01001022 return val;
Daniel P. Berrangea00c9482015-10-13 13:37:40 +01001023}
1024
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001025ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name,
1026 Error **errp)
1027{
1028 ObjectProperty *prop;
1029 ObjectClass *parent_klass;
1030
1031 parent_klass = object_class_get_parent(klass);
1032 if (parent_klass) {
1033 prop = object_class_property_find(parent_klass, name, NULL);
1034 if (prop) {
1035 return prop;
1036 }
1037 }
1038
1039 prop = g_hash_table_lookup(klass->properties, name);
1040 if (!prop) {
1041 error_setg(errp, "Property '.%s' not found", name);
1042 }
1043 return prop;
1044}
1045
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001046void object_property_del(Object *obj, const char *name, Error **errp)
1047{
Pavel Fedinb604a852015-10-13 13:37:45 +01001048 ObjectProperty *prop = g_hash_table_lookup(obj->properties, name);
1049
1050 if (!prop) {
1051 error_setg(errp, "Property '.%s' not found", name);
Anthony Liguori0866aca2011-12-23 15:34:39 -06001052 return;
1053 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001054
Anthony Liguori0866aca2011-12-23 15:34:39 -06001055 if (prop->release) {
1056 prop->release(obj, name, prop->opaque);
1057 }
Pavel Fedinb604a852015-10-13 13:37:45 +01001058 g_hash_table_remove(obj->properties, name);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001059}
1060
1061void object_property_get(Object *obj, Visitor *v, const char *name,
1062 Error **errp)
1063{
Paolo Bonzini89bfe002012-04-12 18:00:18 +02001064 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001065 if (prop == NULL) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001066 return;
1067 }
1068
1069 if (!prop->get) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001070 error_setg(errp, QERR_PERMISSION_DENIED);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001071 } else {
Eric Blaked7bce992016-01-29 06:48:55 -07001072 prop->get(obj, v, name, prop->opaque, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001073 }
1074}
1075
1076void object_property_set(Object *obj, Visitor *v, const char *name,
1077 Error **errp)
1078{
Paolo Bonzini89bfe002012-04-12 18:00:18 +02001079 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001080 if (prop == NULL) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001081 return;
1082 }
1083
1084 if (!prop->set) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001085 error_setg(errp, QERR_PERMISSION_DENIED);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001086 } else {
Eric Blaked7bce992016-01-29 06:48:55 -07001087 prop->set(obj, v, name, prop->opaque, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001088 }
1089}
1090
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001091void object_property_set_str(Object *obj, const char *value,
1092 const char *name, Error **errp)
1093{
1094 QString *qstr = qstring_from_str(value);
1095 object_property_set_qobject(obj, QOBJECT(qstr), name, errp);
1096
1097 QDECREF(qstr);
1098}
1099
1100char *object_property_get_str(Object *obj, const char *name,
1101 Error **errp)
1102{
1103 QObject *ret = object_property_get_qobject(obj, name, errp);
1104 QString *qstring;
1105 char *retval;
1106
1107 if (!ret) {
1108 return NULL;
1109 }
1110 qstring = qobject_to_qstring(ret);
1111 if (!qstring) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001112 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001113 retval = NULL;
1114 } else {
1115 retval = g_strdup(qstring_get_str(qstring));
1116 }
1117
1118 QDECREF(qstring);
1119 return retval;
1120}
1121
Paolo Bonzini1d9c5a12012-02-02 10:51:57 +01001122void object_property_set_link(Object *obj, Object *value,
1123 const char *name, Error **errp)
1124{
Peter Crosthwaited3c49312014-09-25 22:19:19 -07001125 if (value) {
1126 gchar *path = object_get_canonical_path(value);
1127 object_property_set_str(obj, path, name, errp);
1128 g_free(path);
1129 } else {
1130 object_property_set_str(obj, "", name, errp);
1131 }
Paolo Bonzini1d9c5a12012-02-02 10:51:57 +01001132}
1133
1134Object *object_property_get_link(Object *obj, const char *name,
1135 Error **errp)
1136{
1137 char *str = object_property_get_str(obj, name, errp);
1138 Object *target = NULL;
1139
1140 if (str && *str) {
1141 target = object_resolve_path(str, NULL);
1142 if (!target) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01001143 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1144 "Device '%s' not found", str);
Paolo Bonzini1d9c5a12012-02-02 10:51:57 +01001145 }
1146 }
1147
1148 g_free(str);
1149 return target;
1150}
1151
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001152void object_property_set_bool(Object *obj, bool value,
1153 const char *name, Error **errp)
1154{
Eric Blakefc48ffc2015-05-15 16:24:59 -06001155 QBool *qbool = qbool_from_bool(value);
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001156 object_property_set_qobject(obj, QOBJECT(qbool), name, errp);
1157
1158 QDECREF(qbool);
1159}
1160
1161bool object_property_get_bool(Object *obj, const char *name,
1162 Error **errp)
1163{
1164 QObject *ret = object_property_get_qobject(obj, name, errp);
1165 QBool *qbool;
1166 bool retval;
1167
1168 if (!ret) {
1169 return false;
1170 }
1171 qbool = qobject_to_qbool(ret);
1172 if (!qbool) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001173 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001174 retval = false;
1175 } else {
Eric Blakefc48ffc2015-05-15 16:24:59 -06001176 retval = qbool_get_bool(qbool);
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001177 }
1178
1179 QDECREF(qbool);
1180 return retval;
1181}
1182
1183void object_property_set_int(Object *obj, int64_t value,
1184 const char *name, Error **errp)
1185{
1186 QInt *qint = qint_from_int(value);
1187 object_property_set_qobject(obj, QOBJECT(qint), name, errp);
1188
1189 QDECREF(qint);
1190}
1191
1192int64_t object_property_get_int(Object *obj, const char *name,
1193 Error **errp)
1194{
1195 QObject *ret = object_property_get_qobject(obj, name, errp);
1196 QInt *qint;
1197 int64_t retval;
1198
1199 if (!ret) {
1200 return -1;
1201 }
1202 qint = qobject_to_qint(ret);
1203 if (!qint) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001204 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001205 retval = -1;
1206 } else {
1207 retval = qint_get_int(qint);
1208 }
1209
1210 QDECREF(qint);
1211 return retval;
1212}
1213
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01001214typedef struct EnumProperty {
1215 const char * const *strings;
1216 int (*get)(Object *, Error **);
1217 void (*set)(Object *, int, Error **);
1218} EnumProperty;
1219
Hu Tao1f217722014-05-14 17:43:33 +08001220int object_property_get_enum(Object *obj, const char *name,
Daniel P. Berrangea3590da2015-05-27 16:07:56 +01001221 const char *typename, Error **errp)
Hu Tao1f217722014-05-14 17:43:33 +08001222{
Markus Armbruster4715d422015-08-25 20:00:45 +02001223 Error *err = NULL;
Hu Tao1f217722014-05-14 17:43:33 +08001224 StringOutputVisitor *sov;
Eric Blake7a0525c2016-06-09 10:48:37 -06001225 Visitor *v;
Chen Fan976620a2014-08-18 14:46:34 +08001226 char *str;
Hu Tao1f217722014-05-14 17:43:33 +08001227 int ret;
Daniel P. Berrangea3590da2015-05-27 16:07:56 +01001228 ObjectProperty *prop = object_property_find(obj, name, errp);
1229 EnumProperty *enumprop;
1230
1231 if (prop == NULL) {
1232 return 0;
1233 }
1234
1235 if (!g_str_equal(prop->type, typename)) {
1236 error_setg(errp, "Property %s on %s is not '%s' enum type",
1237 name, object_class_get_name(
1238 object_get_class(obj)), typename);
1239 return 0;
1240 }
1241
1242 enumprop = prop->opaque;
Hu Tao1f217722014-05-14 17:43:33 +08001243
1244 sov = string_output_visitor_new(false);
Eric Blakee7ca5652016-06-09 10:48:39 -06001245 v = string_output_get_visitor(sov);
1246 object_property_get(obj, v, name, &err);
Markus Armbruster4715d422015-08-25 20:00:45 +02001247 if (err) {
1248 error_propagate(errp, err);
Eric Blakee7ca5652016-06-09 10:48:39 -06001249 visit_free(v);
Markus Armbruster4715d422015-08-25 20:00:45 +02001250 return 0;
1251 }
Chen Fan976620a2014-08-18 14:46:34 +08001252 str = string_output_get_string(sov);
Eric Blakee7ca5652016-06-09 10:48:39 -06001253 visit_free(v);
Eric Blake7a0525c2016-06-09 10:48:37 -06001254 v = string_input_visitor_new(str);
1255 visit_type_enum(v, name, &ret, enumprop->strings, errp);
Chen Fan976620a2014-08-18 14:46:34 +08001256
1257 g_free(str);
Eric Blake7a0525c2016-06-09 10:48:37 -06001258 visit_free(v);
Hu Tao1f217722014-05-14 17:43:33 +08001259
1260 return ret;
1261}
1262
1263void object_property_get_uint16List(Object *obj, const char *name,
1264 uint16List **list, Error **errp)
1265{
Markus Armbruster4715d422015-08-25 20:00:45 +02001266 Error *err = NULL;
Hu Tao1f217722014-05-14 17:43:33 +08001267 StringOutputVisitor *ov;
Eric Blake7a0525c2016-06-09 10:48:37 -06001268 Visitor *v;
Chen Fan976620a2014-08-18 14:46:34 +08001269 char *str;
Hu Tao1f217722014-05-14 17:43:33 +08001270
1271 ov = string_output_visitor_new(false);
1272 object_property_get(obj, string_output_get_visitor(ov),
Markus Armbruster4715d422015-08-25 20:00:45 +02001273 name, &err);
1274 if (err) {
1275 error_propagate(errp, err);
1276 goto out;
1277 }
Chen Fan976620a2014-08-18 14:46:34 +08001278 str = string_output_get_string(ov);
Eric Blake7a0525c2016-06-09 10:48:37 -06001279 v = string_input_visitor_new(str);
1280 visit_type_uint16List(v, NULL, list, errp);
Chen Fan976620a2014-08-18 14:46:34 +08001281
1282 g_free(str);
Eric Blake7a0525c2016-06-09 10:48:37 -06001283 visit_free(v);
Markus Armbruster4715d422015-08-25 20:00:45 +02001284out:
Eric Blakee7ca5652016-06-09 10:48:39 -06001285 visit_free(string_output_get_visitor(ov));
Hu Tao1f217722014-05-14 17:43:33 +08001286}
1287
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001288void object_property_parse(Object *obj, const char *string,
1289 const char *name, Error **errp)
1290{
Eric Blake7a0525c2016-06-09 10:48:37 -06001291 Visitor *v = string_input_visitor_new(string);
1292 object_property_set(obj, v, name, errp);
1293 visit_free(v);
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001294}
1295
Paolo Bonzini0b7593e2014-02-08 11:01:50 +01001296char *object_property_print(Object *obj, const char *name, bool human,
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001297 Error **errp)
1298{
Eric Blakef8b7f1a2015-09-29 16:21:09 -06001299 StringOutputVisitor *sov;
Gonglei3a530092014-09-27 13:13:55 +08001300 char *string = NULL;
1301 Error *local_err = NULL;
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001302
Eric Blakef8b7f1a2015-09-29 16:21:09 -06001303 sov = string_output_visitor_new(human);
1304 object_property_get(obj, string_output_get_visitor(sov), name, &local_err);
Gonglei3a530092014-09-27 13:13:55 +08001305 if (local_err) {
1306 error_propagate(errp, local_err);
1307 goto out;
1308 }
1309
Eric Blakef8b7f1a2015-09-29 16:21:09 -06001310 string = string_output_get_string(sov);
Gonglei3a530092014-09-27 13:13:55 +08001311
1312out:
Eric Blakee7ca5652016-06-09 10:48:39 -06001313 visit_free(string_output_get_visitor(sov));
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001314 return string;
1315}
1316
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001317const char *object_property_get_type(Object *obj, const char *name, Error **errp)
1318{
Paolo Bonzini89bfe002012-04-12 18:00:18 +02001319 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001320 if (prop == NULL) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001321 return NULL;
1322 }
1323
1324 return prop->type;
1325}
1326
1327Object *object_get_root(void)
1328{
Anthony Liguori8b45d442011-12-23 09:08:05 -06001329 static Object *root;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001330
Anthony Liguori8b45d442011-12-23 09:08:05 -06001331 if (!root) {
1332 root = object_new("container");
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001333 }
1334
Anthony Liguori8b45d442011-12-23 09:08:05 -06001335 return root;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001336}
1337
Daniel P. Berrangebc2256c2015-05-13 17:14:05 +01001338Object *object_get_objects_root(void)
1339{
1340 return container_get(object_get_root(), "/objects");
1341}
1342
Eric Blaked7bce992016-01-29 06:48:55 -07001343static void object_get_child_property(Object *obj, Visitor *v,
1344 const char *name, void *opaque,
1345 Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001346{
1347 Object *child = opaque;
1348 gchar *path;
1349
1350 path = object_get_canonical_path(child);
Eric Blake51e72bc2016-01-29 06:48:54 -07001351 visit_type_str(v, name, &path, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001352 g_free(path);
1353}
1354
Paolo Bonzini64607d02014-06-05 13:11:51 +02001355static Object *object_resolve_child_property(Object *parent, void *opaque, const gchar *part)
1356{
1357 return opaque;
1358}
1359
Anthony Liguoridb85b572011-12-23 08:47:39 -06001360static void object_finalize_child_property(Object *obj, const char *name,
1361 void *opaque)
1362{
1363 Object *child = opaque;
1364
Paolo Bonzinibffc6872014-06-11 11:57:38 +02001365 if (child->class->unparent) {
1366 (child->class->unparent)(child);
1367 }
1368 child->parent = NULL;
Anthony Liguoridb85b572011-12-23 08:47:39 -06001369 object_unref(child);
1370}
1371
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001372void object_property_add_child(Object *obj, const char *name,
1373 Object *child, Error **errp)
1374{
Paolo Bonzinib0ed5e92013-12-20 23:21:08 +01001375 Error *local_err = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001376 gchar *type;
Paolo Bonzini64607d02014-06-05 13:11:51 +02001377 ObjectProperty *op;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001378
Peter Crosthwaite8faa2f82014-09-25 22:19:52 -07001379 if (child->parent != NULL) {
1380 error_setg(errp, "child object is already parented");
1381 return;
1382 }
1383
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001384 type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child)));
1385
Paolo Bonzini64607d02014-06-05 13:11:51 +02001386 op = object_property_add(obj, name, type, object_get_child_property, NULL,
1387 object_finalize_child_property, child, &local_err);
Paolo Bonzinib0ed5e92013-12-20 23:21:08 +01001388 if (local_err) {
1389 error_propagate(errp, local_err);
1390 goto out;
1391 }
Paolo Bonzini64607d02014-06-05 13:11:51 +02001392
1393 op->resolve = object_resolve_child_property;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001394 object_ref(child);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001395 child->parent = obj;
1396
Paolo Bonzinib0ed5e92013-12-20 23:21:08 +01001397out:
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001398 g_free(type);
1399}
1400
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001401void object_property_allow_set_link(Object *obj, const char *name,
1402 Object *val, Error **errp)
1403{
1404 /* Allow the link to be set, always */
1405}
1406
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001407typedef struct {
1408 Object **child;
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001409 void (*check)(Object *, const char *, Object *, Error **);
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001410 ObjectPropertyLinkFlags flags;
1411} LinkProperty;
1412
Eric Blaked7bce992016-01-29 06:48:55 -07001413static void object_get_link_property(Object *obj, Visitor *v,
1414 const char *name, void *opaque,
1415 Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001416{
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001417 LinkProperty *lprop = opaque;
1418 Object **child = lprop->child;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001419 gchar *path;
1420
1421 if (*child) {
1422 path = object_get_canonical_path(*child);
Eric Blake51e72bc2016-01-29 06:48:54 -07001423 visit_type_str(v, name, &path, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001424 g_free(path);
1425 } else {
1426 path = (gchar *)"";
Eric Blake51e72bc2016-01-29 06:48:54 -07001427 visit_type_str(v, name, &path, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001428 }
1429}
1430
Stefan Hajnoczif5ec6702014-03-19 08:58:53 +01001431/*
1432 * object_resolve_link:
1433 *
1434 * Lookup an object and ensure its type matches the link property type. This
1435 * is similar to object_resolve_path() except type verification against the
1436 * link property is performed.
1437 *
1438 * Returns: The matched object or NULL on path lookup failures.
1439 */
1440static Object *object_resolve_link(Object *obj, const char *name,
1441 const char *path, Error **errp)
1442{
1443 const char *type;
1444 gchar *target_type;
1445 bool ambiguous = false;
1446 Object *target;
1447
1448 /* Go from link<FOO> to FOO. */
1449 type = object_property_get_type(obj, name, NULL);
1450 target_type = g_strndup(&type[5], strlen(type) - 6);
1451 target = object_resolve_path_type(path, target_type, &ambiguous);
1452
1453 if (ambiguous) {
Eric Blake455b0fd2015-11-10 23:51:20 -07001454 error_setg(errp, "Path '%s' does not uniquely identify an object",
1455 path);
Stefan Hajnoczif5ec6702014-03-19 08:58:53 +01001456 } else if (!target) {
1457 target = object_resolve_path(path, &ambiguous);
1458 if (target || ambiguous) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001459 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
Stefan Hajnoczif5ec6702014-03-19 08:58:53 +01001460 } else {
Markus Armbruster75158eb2015-03-16 08:57:47 +01001461 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1462 "Device '%s' not found", path);
Stefan Hajnoczif5ec6702014-03-19 08:58:53 +01001463 }
1464 target = NULL;
1465 }
1466 g_free(target_type);
1467
1468 return target;
1469}
1470
Eric Blaked7bce992016-01-29 06:48:55 -07001471static void object_set_link_property(Object *obj, Visitor *v,
1472 const char *name, void *opaque,
1473 Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001474{
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001475 Error *local_err = NULL;
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001476 LinkProperty *prop = opaque;
1477 Object **child = prop->child;
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001478 Object *old_target = *child;
1479 Object *new_target = NULL;
1480 char *path = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001481
Eric Blake51e72bc2016-01-29 06:48:54 -07001482 visit_type_str(v, name, &path, &local_err);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001483
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001484 if (!local_err && strcmp(path, "") != 0) {
1485 new_target = object_resolve_link(obj, name, path, &local_err);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001486 }
1487
1488 g_free(path);
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001489 if (local_err) {
1490 error_propagate(errp, local_err);
1491 return;
1492 }
Alexander Barabashf0cdc962012-02-22 19:22:26 +02001493
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001494 prop->check(obj, name, new_target, &local_err);
1495 if (local_err) {
1496 error_propagate(errp, local_err);
1497 return;
1498 }
1499
Peter Crosthwaite8ffad852014-06-05 23:13:36 -07001500 object_ref(new_target);
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001501 *child = new_target;
Peter Crosthwaite8ffad852014-06-05 23:13:36 -07001502 object_unref(old_target);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001503}
1504
Paolo Bonzini64607d02014-06-05 13:11:51 +02001505static Object *object_resolve_link_property(Object *parent, void *opaque, const gchar *part)
1506{
1507 LinkProperty *lprop = opaque;
1508
1509 return *lprop->child;
1510}
1511
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001512static void object_release_link_property(Object *obj, const char *name,
1513 void *opaque)
1514{
1515 LinkProperty *prop = opaque;
1516
1517 if ((prop->flags & OBJ_PROP_LINK_UNREF_ON_RELEASE) && *prop->child) {
1518 object_unref(*prop->child);
1519 }
1520 g_free(prop);
1521}
1522
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001523void object_property_add_link(Object *obj, const char *name,
1524 const char *type, Object **child,
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001525 void (*check)(Object *, const char *,
1526 Object *, Error **),
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001527 ObjectPropertyLinkFlags flags,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001528 Error **errp)
1529{
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001530 Error *local_err = NULL;
1531 LinkProperty *prop = g_malloc(sizeof(*prop));
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001532 gchar *full_type;
Paolo Bonzini64607d02014-06-05 13:11:51 +02001533 ObjectProperty *op;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001534
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001535 prop->child = child;
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001536 prop->check = check;
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001537 prop->flags = flags;
1538
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001539 full_type = g_strdup_printf("link<%s>", type);
1540
Paolo Bonzini64607d02014-06-05 13:11:51 +02001541 op = object_property_add(obj, name, full_type,
1542 object_get_link_property,
1543 check ? object_set_link_property : NULL,
1544 object_release_link_property,
1545 prop,
1546 &local_err);
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001547 if (local_err) {
1548 error_propagate(errp, local_err);
1549 g_free(prop);
Paolo Bonzini64607d02014-06-05 13:11:51 +02001550 goto out;
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001551 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001552
Paolo Bonzini64607d02014-06-05 13:11:51 +02001553 op->resolve = object_resolve_link_property;
1554
1555out:
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001556 g_free(full_type);
1557}
1558
Paolo Bonzinifb9e7e32015-05-05 18:29:00 +02001559void object_property_add_const_link(Object *obj, const char *name,
1560 Object *target, Error **errp)
1561{
1562 char *link_type;
1563 ObjectProperty *op;
1564
1565 link_type = g_strdup_printf("link<%s>", object_get_typename(target));
1566 op = object_property_add(obj, name, link_type,
1567 object_get_child_property, NULL,
1568 NULL, target, errp);
1569 if (op != NULL) {
1570 op->resolve = object_resolve_child_property;
1571 }
1572 g_free(link_type);
1573}
1574
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001575gchar *object_get_canonical_path_component(Object *obj)
1576{
1577 ObjectProperty *prop = NULL;
Pavel Fedinb604a852015-10-13 13:37:45 +01001578 GHashTableIter iter;
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001579
1580 g_assert(obj);
1581 g_assert(obj->parent != NULL);
1582
Pavel Fedinb604a852015-10-13 13:37:45 +01001583 g_hash_table_iter_init(&iter, obj->parent->properties);
1584 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001585 if (!object_property_is_child(prop)) {
1586 continue;
1587 }
1588
1589 if (prop->opaque == obj) {
1590 return g_strdup(prop->name);
1591 }
1592 }
1593
1594 /* obj had a parent but was not a child, should never happen */
1595 g_assert_not_reached();
1596 return NULL;
1597}
1598
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001599gchar *object_get_canonical_path(Object *obj)
1600{
1601 Object *root = object_get_root();
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001602 char *newpath, *path = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001603
1604 while (obj != root) {
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001605 char *component = object_get_canonical_path_component(obj);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001606
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001607 if (path) {
1608 newpath = g_strdup_printf("%s/%s", component, path);
1609 g_free(component);
1610 g_free(path);
1611 path = newpath;
1612 } else {
1613 path = component;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001614 }
1615
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001616 obj = obj->parent;
1617 }
1618
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001619 newpath = g_strdup_printf("/%s", path ? path : "");
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001620 g_free(path);
1621
1622 return newpath;
1623}
1624
Andreas Färber3e84b482013-01-15 02:55:10 +01001625Object *object_resolve_path_component(Object *parent, const gchar *part)
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001626{
Paolo Bonzini89bfe002012-04-12 18:00:18 +02001627 ObjectProperty *prop = object_property_find(parent, part, NULL);
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001628 if (prop == NULL) {
1629 return NULL;
1630 }
1631
Paolo Bonzini64607d02014-06-05 13:11:51 +02001632 if (prop->resolve) {
1633 return prop->resolve(parent, prop->opaque, part);
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001634 } else {
1635 return NULL;
1636 }
1637}
1638
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001639static Object *object_resolve_abs_path(Object *parent,
1640 gchar **parts,
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001641 const char *typename,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001642 int index)
1643{
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001644 Object *child;
1645
1646 if (parts[index] == NULL) {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001647 return object_dynamic_cast(parent, typename);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001648 }
1649
1650 if (strcmp(parts[index], "") == 0) {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001651 return object_resolve_abs_path(parent, parts, typename, index + 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001652 }
1653
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001654 child = object_resolve_path_component(parent, parts[index]);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001655 if (!child) {
1656 return NULL;
1657 }
1658
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001659 return object_resolve_abs_path(child, parts, typename, index + 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001660}
1661
1662static Object *object_resolve_partial_path(Object *parent,
1663 gchar **parts,
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001664 const char *typename,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001665 bool *ambiguous)
1666{
1667 Object *obj;
Pavel Fedinb604a852015-10-13 13:37:45 +01001668 GHashTableIter iter;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001669 ObjectProperty *prop;
1670
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001671 obj = object_resolve_abs_path(parent, parts, typename, 0);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001672
Pavel Fedinb604a852015-10-13 13:37:45 +01001673 g_hash_table_iter_init(&iter, parent->properties);
1674 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001675 Object *found;
1676
Andreas Färber5d9d3f42012-05-27 00:32:40 +02001677 if (!object_property_is_child(prop)) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001678 continue;
1679 }
1680
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001681 found = object_resolve_partial_path(prop->opaque, parts,
1682 typename, ambiguous);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001683 if (found) {
1684 if (obj) {
1685 if (ambiguous) {
1686 *ambiguous = true;
1687 }
1688 return NULL;
1689 }
1690 obj = found;
1691 }
1692
1693 if (ambiguous && *ambiguous) {
1694 return NULL;
1695 }
1696 }
1697
1698 return obj;
1699}
1700
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001701Object *object_resolve_path_type(const char *path, const char *typename,
1702 bool *ambiguous)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001703{
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001704 Object *obj;
1705 gchar **parts;
1706
1707 parts = g_strsplit(path, "/", 0);
Paolo Bonzini2e1103f2013-04-18 18:44:02 +02001708 assert(parts);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001709
Paolo Bonzini2e1103f2013-04-18 18:44:02 +02001710 if (parts[0] == NULL || strcmp(parts[0], "") != 0) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001711 if (ambiguous) {
1712 *ambiguous = false;
1713 }
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001714 obj = object_resolve_partial_path(object_get_root(), parts,
1715 typename, ambiguous);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001716 } else {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001717 obj = object_resolve_abs_path(object_get_root(), parts, typename, 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001718 }
1719
1720 g_strfreev(parts);
1721
1722 return obj;
1723}
1724
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001725Object *object_resolve_path(const char *path, bool *ambiguous)
1726{
1727 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
1728}
1729
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001730typedef struct StringProperty
1731{
1732 char *(*get)(Object *, Error **);
1733 void (*set)(Object *, const char *, Error **);
1734} StringProperty;
1735
Eric Blaked7bce992016-01-29 06:48:55 -07001736static void property_get_str(Object *obj, Visitor *v, const char *name,
1737 void *opaque, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001738{
1739 StringProperty *prop = opaque;
1740 char *value;
Markus Armbrustere1c82372015-08-25 20:00:46 +02001741 Error *err = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001742
Markus Armbrustere1c82372015-08-25 20:00:46 +02001743 value = prop->get(obj, &err);
1744 if (err) {
1745 error_propagate(errp, err);
1746 return;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001747 }
Markus Armbrustere1c82372015-08-25 20:00:46 +02001748
Eric Blake51e72bc2016-01-29 06:48:54 -07001749 visit_type_str(v, name, &value, errp);
Markus Armbrustere1c82372015-08-25 20:00:46 +02001750 g_free(value);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001751}
1752
Eric Blaked7bce992016-01-29 06:48:55 -07001753static void property_set_str(Object *obj, Visitor *v, const char *name,
1754 void *opaque, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001755{
1756 StringProperty *prop = opaque;
1757 char *value;
1758 Error *local_err = NULL;
1759
Eric Blake51e72bc2016-01-29 06:48:54 -07001760 visit_type_str(v, name, &value, &local_err);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001761 if (local_err) {
1762 error_propagate(errp, local_err);
1763 return;
1764 }
1765
1766 prop->set(obj, value, errp);
1767 g_free(value);
1768}
1769
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001770static void property_release_str(Object *obj, const char *name,
1771 void *opaque)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001772{
1773 StringProperty *prop = opaque;
1774 g_free(prop);
1775}
1776
1777void object_property_add_str(Object *obj, const char *name,
1778 char *(*get)(Object *, Error **),
1779 void (*set)(Object *, const char *, Error **),
1780 Error **errp)
1781{
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01001782 Error *local_err = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001783 StringProperty *prop = g_malloc0(sizeof(*prop));
1784
1785 prop->get = get;
1786 prop->set = set;
1787
1788 object_property_add(obj, name, "string",
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001789 get ? property_get_str : NULL,
1790 set ? property_set_str : NULL,
1791 property_release_str,
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01001792 prop, &local_err);
1793 if (local_err) {
1794 error_propagate(errp, local_err);
1795 g_free(prop);
1796 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001797}
Paolo Bonzini745549c2012-03-31 16:45:54 +02001798
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001799void object_class_property_add_str(ObjectClass *klass, const char *name,
1800 char *(*get)(Object *, Error **),
1801 void (*set)(Object *, const char *,
1802 Error **),
1803 Error **errp)
1804{
1805 Error *local_err = NULL;
1806 StringProperty *prop = g_malloc0(sizeof(*prop));
1807
1808 prop->get = get;
1809 prop->set = set;
1810
1811 object_class_property_add(klass, name, "string",
1812 get ? property_get_str : NULL,
1813 set ? property_set_str : NULL,
1814 property_release_str,
1815 prop, &local_err);
1816 if (local_err) {
1817 error_propagate(errp, local_err);
1818 g_free(prop);
1819 }
1820}
1821
Anthony Liguori0e558842012-06-25 10:32:46 -05001822typedef struct BoolProperty
1823{
1824 bool (*get)(Object *, Error **);
1825 void (*set)(Object *, bool, Error **);
1826} BoolProperty;
1827
Eric Blaked7bce992016-01-29 06:48:55 -07001828static void property_get_bool(Object *obj, Visitor *v, const char *name,
1829 void *opaque, Error **errp)
Anthony Liguori0e558842012-06-25 10:32:46 -05001830{
1831 BoolProperty *prop = opaque;
1832 bool value;
Markus Armbruster4715d422015-08-25 20:00:45 +02001833 Error *err = NULL;
Anthony Liguori0e558842012-06-25 10:32:46 -05001834
Markus Armbruster4715d422015-08-25 20:00:45 +02001835 value = prop->get(obj, &err);
1836 if (err) {
1837 error_propagate(errp, err);
1838 return;
1839 }
1840
Eric Blake51e72bc2016-01-29 06:48:54 -07001841 visit_type_bool(v, name, &value, errp);
Anthony Liguori0e558842012-06-25 10:32:46 -05001842}
1843
Eric Blaked7bce992016-01-29 06:48:55 -07001844static void property_set_bool(Object *obj, Visitor *v, const char *name,
1845 void *opaque, Error **errp)
Anthony Liguori0e558842012-06-25 10:32:46 -05001846{
1847 BoolProperty *prop = opaque;
1848 bool value;
1849 Error *local_err = NULL;
1850
Eric Blake51e72bc2016-01-29 06:48:54 -07001851 visit_type_bool(v, name, &value, &local_err);
Anthony Liguori0e558842012-06-25 10:32:46 -05001852 if (local_err) {
1853 error_propagate(errp, local_err);
1854 return;
1855 }
1856
1857 prop->set(obj, value, errp);
1858}
1859
1860static void property_release_bool(Object *obj, const char *name,
1861 void *opaque)
1862{
1863 BoolProperty *prop = opaque;
1864 g_free(prop);
1865}
1866
1867void object_property_add_bool(Object *obj, const char *name,
1868 bool (*get)(Object *, Error **),
1869 void (*set)(Object *, bool, Error **),
1870 Error **errp)
1871{
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01001872 Error *local_err = NULL;
Anthony Liguori0e558842012-06-25 10:32:46 -05001873 BoolProperty *prop = g_malloc0(sizeof(*prop));
1874
1875 prop->get = get;
1876 prop->set = set;
1877
1878 object_property_add(obj, name, "bool",
1879 get ? property_get_bool : NULL,
1880 set ? property_set_bool : NULL,
1881 property_release_bool,
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01001882 prop, &local_err);
1883 if (local_err) {
1884 error_propagate(errp, local_err);
1885 g_free(prop);
1886 }
Anthony Liguori0e558842012-06-25 10:32:46 -05001887}
1888
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001889void object_class_property_add_bool(ObjectClass *klass, const char *name,
1890 bool (*get)(Object *, Error **),
1891 void (*set)(Object *, bool, Error **),
1892 Error **errp)
1893{
1894 Error *local_err = NULL;
1895 BoolProperty *prop = g_malloc0(sizeof(*prop));
1896
1897 prop->get = get;
1898 prop->set = set;
1899
1900 object_class_property_add(klass, name, "bool",
1901 get ? property_get_bool : NULL,
1902 set ? property_set_bool : NULL,
1903 property_release_bool,
1904 prop, &local_err);
1905 if (local_err) {
1906 error_propagate(errp, local_err);
1907 g_free(prop);
1908 }
1909}
1910
Eric Blaked7bce992016-01-29 06:48:55 -07001911static void property_get_enum(Object *obj, Visitor *v, const char *name,
1912 void *opaque, Error **errp)
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01001913{
1914 EnumProperty *prop = opaque;
1915 int value;
Markus Armbruster4715d422015-08-25 20:00:45 +02001916 Error *err = NULL;
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01001917
Markus Armbruster4715d422015-08-25 20:00:45 +02001918 value = prop->get(obj, &err);
1919 if (err) {
1920 error_propagate(errp, err);
1921 return;
1922 }
1923
Eric Blake337283d2016-01-29 06:48:57 -07001924 visit_type_enum(v, name, &value, prop->strings, errp);
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01001925}
1926
Eric Blaked7bce992016-01-29 06:48:55 -07001927static void property_set_enum(Object *obj, Visitor *v, const char *name,
1928 void *opaque, Error **errp)
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01001929{
1930 EnumProperty *prop = opaque;
1931 int value;
Markus Armbruster4715d422015-08-25 20:00:45 +02001932 Error *err = NULL;
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01001933
Eric Blake337283d2016-01-29 06:48:57 -07001934 visit_type_enum(v, name, &value, prop->strings, &err);
Markus Armbruster4715d422015-08-25 20:00:45 +02001935 if (err) {
1936 error_propagate(errp, err);
1937 return;
1938 }
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01001939 prop->set(obj, value, errp);
1940}
1941
1942static void property_release_enum(Object *obj, const char *name,
1943 void *opaque)
1944{
1945 EnumProperty *prop = opaque;
1946 g_free(prop);
1947}
1948
1949void object_property_add_enum(Object *obj, const char *name,
1950 const char *typename,
1951 const char * const *strings,
1952 int (*get)(Object *, Error **),
1953 void (*set)(Object *, int, Error **),
1954 Error **errp)
1955{
1956 Error *local_err = NULL;
1957 EnumProperty *prop = g_malloc(sizeof(*prop));
1958
1959 prop->strings = strings;
1960 prop->get = get;
1961 prop->set = set;
1962
1963 object_property_add(obj, name, typename,
1964 get ? property_get_enum : NULL,
1965 set ? property_set_enum : NULL,
1966 property_release_enum,
1967 prop, &local_err);
1968 if (local_err) {
1969 error_propagate(errp, local_err);
1970 g_free(prop);
1971 }
1972}
1973
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001974void object_class_property_add_enum(ObjectClass *klass, const char *name,
1975 const char *typename,
1976 const char * const *strings,
1977 int (*get)(Object *, Error **),
1978 void (*set)(Object *, int, Error **),
1979 Error **errp)
1980{
1981 Error *local_err = NULL;
1982 EnumProperty *prop = g_malloc(sizeof(*prop));
1983
1984 prop->strings = strings;
1985 prop->get = get;
1986 prop->set = set;
1987
1988 object_class_property_add(klass, name, typename,
1989 get ? property_get_enum : NULL,
1990 set ? property_set_enum : NULL,
1991 property_release_enum,
1992 prop, &local_err);
1993 if (local_err) {
1994 error_propagate(errp, local_err);
1995 g_free(prop);
1996 }
1997}
1998
David Gibson8e099d12015-02-06 14:55:45 +11001999typedef struct TMProperty {
2000 void (*get)(Object *, struct tm *, Error **);
2001} TMProperty;
2002
Eric Blaked7bce992016-01-29 06:48:55 -07002003static void property_get_tm(Object *obj, Visitor *v, const char *name,
2004 void *opaque, Error **errp)
David Gibson8e099d12015-02-06 14:55:45 +11002005{
2006 TMProperty *prop = opaque;
2007 Error *err = NULL;
2008 struct tm value;
2009
2010 prop->get(obj, &value, &err);
2011 if (err) {
2012 goto out;
2013 }
2014
Eric Blake337283d2016-01-29 06:48:57 -07002015 visit_start_struct(v, name, NULL, 0, &err);
David Gibson8e099d12015-02-06 14:55:45 +11002016 if (err) {
2017 goto out;
2018 }
Eric Blake51e72bc2016-01-29 06:48:54 -07002019 visit_type_int32(v, "tm_year", &value.tm_year, &err);
David Gibson8e099d12015-02-06 14:55:45 +11002020 if (err) {
2021 goto out_end;
2022 }
Eric Blake51e72bc2016-01-29 06:48:54 -07002023 visit_type_int32(v, "tm_mon", &value.tm_mon, &err);
David Gibson8e099d12015-02-06 14:55:45 +11002024 if (err) {
2025 goto out_end;
2026 }
Eric Blake51e72bc2016-01-29 06:48:54 -07002027 visit_type_int32(v, "tm_mday", &value.tm_mday, &err);
David Gibson8e099d12015-02-06 14:55:45 +11002028 if (err) {
2029 goto out_end;
2030 }
Eric Blake51e72bc2016-01-29 06:48:54 -07002031 visit_type_int32(v, "tm_hour", &value.tm_hour, &err);
David Gibson8e099d12015-02-06 14:55:45 +11002032 if (err) {
2033 goto out_end;
2034 }
Eric Blake51e72bc2016-01-29 06:48:54 -07002035 visit_type_int32(v, "tm_min", &value.tm_min, &err);
David Gibson8e099d12015-02-06 14:55:45 +11002036 if (err) {
2037 goto out_end;
2038 }
Eric Blake51e72bc2016-01-29 06:48:54 -07002039 visit_type_int32(v, "tm_sec", &value.tm_sec, &err);
David Gibson8e099d12015-02-06 14:55:45 +11002040 if (err) {
2041 goto out_end;
2042 }
Eric Blake15c2f662016-04-28 15:45:27 -06002043 visit_check_struct(v, &err);
David Gibson8e099d12015-02-06 14:55:45 +11002044out_end:
Eric Blake1158bb22016-06-09 10:48:34 -06002045 visit_end_struct(v, NULL);
David Gibson8e099d12015-02-06 14:55:45 +11002046out:
2047 error_propagate(errp, err);
2048
2049}
2050
2051static void property_release_tm(Object *obj, const char *name,
2052 void *opaque)
2053{
2054 TMProperty *prop = opaque;
2055 g_free(prop);
2056}
2057
2058void object_property_add_tm(Object *obj, const char *name,
2059 void (*get)(Object *, struct tm *, Error **),
2060 Error **errp)
2061{
2062 Error *local_err = NULL;
2063 TMProperty *prop = g_malloc0(sizeof(*prop));
2064
2065 prop->get = get;
2066
2067 object_property_add(obj, name, "struct tm",
2068 get ? property_get_tm : NULL, NULL,
2069 property_release_tm,
2070 prop, &local_err);
2071 if (local_err) {
2072 error_propagate(errp, local_err);
2073 g_free(prop);
2074 }
2075}
2076
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002077void object_class_property_add_tm(ObjectClass *klass, const char *name,
2078 void (*get)(Object *, struct tm *, Error **),
2079 Error **errp)
2080{
2081 Error *local_err = NULL;
2082 TMProperty *prop = g_malloc0(sizeof(*prop));
2083
2084 prop->get = get;
2085
2086 object_class_property_add(klass, name, "struct tm",
2087 get ? property_get_tm : NULL, NULL,
2088 property_release_tm,
2089 prop, &local_err);
2090 if (local_err) {
2091 error_propagate(errp, local_err);
2092 g_free(prop);
2093 }
2094}
2095
Paolo Bonzini2f262e02012-04-02 17:33:51 +02002096static char *qdev_get_type(Object *obj, Error **errp)
2097{
2098 return g_strdup(object_get_typename(obj));
2099}
2100
Eric Blaked7bce992016-01-29 06:48:55 -07002101static void property_get_uint8_ptr(Object *obj, Visitor *v, const char *name,
2102 void *opaque, Error **errp)
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002103{
2104 uint8_t value = *(uint8_t *)opaque;
Eric Blake51e72bc2016-01-29 06:48:54 -07002105 visit_type_uint8(v, name, &value, errp);
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002106}
2107
Eric Blaked7bce992016-01-29 06:48:55 -07002108static void property_get_uint16_ptr(Object *obj, Visitor *v, const char *name,
2109 void *opaque, Error **errp)
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002110{
2111 uint16_t value = *(uint16_t *)opaque;
Eric Blake51e72bc2016-01-29 06:48:54 -07002112 visit_type_uint16(v, name, &value, errp);
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002113}
2114
Eric Blaked7bce992016-01-29 06:48:55 -07002115static void property_get_uint32_ptr(Object *obj, Visitor *v, const char *name,
2116 void *opaque, Error **errp)
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002117{
2118 uint32_t value = *(uint32_t *)opaque;
Eric Blake51e72bc2016-01-29 06:48:54 -07002119 visit_type_uint32(v, name, &value, errp);
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002120}
2121
Eric Blaked7bce992016-01-29 06:48:55 -07002122static void property_get_uint64_ptr(Object *obj, Visitor *v, const char *name,
2123 void *opaque, Error **errp)
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002124{
2125 uint64_t value = *(uint64_t *)opaque;
Eric Blake51e72bc2016-01-29 06:48:54 -07002126 visit_type_uint64(v, name, &value, errp);
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002127}
2128
2129void object_property_add_uint8_ptr(Object *obj, const char *name,
2130 const uint8_t *v, Error **errp)
2131{
2132 object_property_add(obj, name, "uint8", property_get_uint8_ptr,
2133 NULL, NULL, (void *)v, errp);
2134}
2135
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002136void object_class_property_add_uint8_ptr(ObjectClass *klass, const char *name,
2137 const uint8_t *v, Error **errp)
2138{
2139 object_class_property_add(klass, name, "uint8", property_get_uint8_ptr,
2140 NULL, NULL, (void *)v, errp);
2141}
2142
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002143void object_property_add_uint16_ptr(Object *obj, const char *name,
2144 const uint16_t *v, Error **errp)
2145{
2146 object_property_add(obj, name, "uint16", property_get_uint16_ptr,
2147 NULL, NULL, (void *)v, errp);
2148}
2149
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002150void object_class_property_add_uint16_ptr(ObjectClass *klass, const char *name,
2151 const uint16_t *v, Error **errp)
2152{
2153 object_class_property_add(klass, name, "uint16", property_get_uint16_ptr,
2154 NULL, NULL, (void *)v, errp);
2155}
2156
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002157void object_property_add_uint32_ptr(Object *obj, const char *name,
2158 const uint32_t *v, Error **errp)
2159{
2160 object_property_add(obj, name, "uint32", property_get_uint32_ptr,
2161 NULL, NULL, (void *)v, errp);
2162}
2163
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002164void object_class_property_add_uint32_ptr(ObjectClass *klass, const char *name,
2165 const uint32_t *v, Error **errp)
2166{
2167 object_class_property_add(klass, name, "uint32", property_get_uint32_ptr,
2168 NULL, NULL, (void *)v, errp);
2169}
2170
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002171void object_property_add_uint64_ptr(Object *obj, const char *name,
2172 const uint64_t *v, Error **errp)
2173{
2174 object_property_add(obj, name, "uint64", property_get_uint64_ptr,
2175 NULL, NULL, (void *)v, errp);
2176}
2177
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002178void object_class_property_add_uint64_ptr(ObjectClass *klass, const char *name,
2179 const uint64_t *v, Error **errp)
2180{
2181 object_class_property_add(klass, name, "uint64", property_get_uint64_ptr,
2182 NULL, NULL, (void *)v, errp);
2183}
2184
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002185typedef struct {
2186 Object *target_obj;
Eduardo Habkost1590d262015-04-09 16:57:29 -03002187 char *target_name;
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002188} AliasProperty;
2189
Eric Blaked7bce992016-01-29 06:48:55 -07002190static void property_get_alias(Object *obj, Visitor *v, const char *name,
2191 void *opaque, Error **errp)
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002192{
2193 AliasProperty *prop = opaque;
2194
2195 object_property_get(prop->target_obj, v, prop->target_name, errp);
2196}
2197
Eric Blaked7bce992016-01-29 06:48:55 -07002198static void property_set_alias(Object *obj, Visitor *v, const char *name,
2199 void *opaque, Error **errp)
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002200{
2201 AliasProperty *prop = opaque;
2202
2203 object_property_set(prop->target_obj, v, prop->target_name, errp);
2204}
2205
Paolo Bonzini64607d02014-06-05 13:11:51 +02002206static Object *property_resolve_alias(Object *obj, void *opaque,
2207 const gchar *part)
2208{
2209 AliasProperty *prop = opaque;
2210
2211 return object_resolve_path_component(prop->target_obj, prop->target_name);
2212}
2213
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002214static void property_release_alias(Object *obj, const char *name, void *opaque)
2215{
2216 AliasProperty *prop = opaque;
2217
Eduardo Habkost1590d262015-04-09 16:57:29 -03002218 g_free(prop->target_name);
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002219 g_free(prop);
2220}
2221
2222void object_property_add_alias(Object *obj, const char *name,
2223 Object *target_obj, const char *target_name,
2224 Error **errp)
2225{
2226 AliasProperty *prop;
Paolo Bonzini64607d02014-06-05 13:11:51 +02002227 ObjectProperty *op;
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002228 ObjectProperty *target_prop;
Paolo Bonzinid1906982014-06-10 11:17:35 +02002229 gchar *prop_type;
Gonglei8ae9a9e2014-09-27 13:13:56 +08002230 Error *local_err = NULL;
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002231
2232 target_prop = object_property_find(target_obj, target_name, errp);
2233 if (!target_prop) {
2234 return;
2235 }
2236
Paolo Bonzinid1906982014-06-10 11:17:35 +02002237 if (object_property_is_child(target_prop)) {
2238 prop_type = g_strdup_printf("link%s",
2239 target_prop->type + strlen("child"));
2240 } else {
2241 prop_type = g_strdup(target_prop->type);
2242 }
2243
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002244 prop = g_malloc(sizeof(*prop));
2245 prop->target_obj = target_obj;
Eduardo Habkost1590d262015-04-09 16:57:29 -03002246 prop->target_name = g_strdup(target_name);
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002247
Paolo Bonzinid1906982014-06-10 11:17:35 +02002248 op = object_property_add(obj, name, prop_type,
Paolo Bonzini64607d02014-06-05 13:11:51 +02002249 property_get_alias,
2250 property_set_alias,
2251 property_release_alias,
Gonglei8ae9a9e2014-09-27 13:13:56 +08002252 prop, &local_err);
2253 if (local_err) {
2254 error_propagate(errp, local_err);
2255 g_free(prop);
2256 goto out;
2257 }
Paolo Bonzini64607d02014-06-05 13:11:51 +02002258 op->resolve = property_resolve_alias;
Paolo Bonzinid1906982014-06-10 11:17:35 +02002259
Andreas Färbera18bb412015-03-27 17:34:10 +01002260 object_property_set_description(obj, op->name,
Gonglei80742642014-10-07 14:33:21 +08002261 target_prop->description,
2262 &error_abort);
2263
Gonglei8ae9a9e2014-09-27 13:13:56 +08002264out:
Paolo Bonzinid1906982014-06-10 11:17:35 +02002265 g_free(prop_type);
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002266}
2267
Gonglei80742642014-10-07 14:33:21 +08002268void object_property_set_description(Object *obj, const char *name,
2269 const char *description, Error **errp)
2270{
2271 ObjectProperty *op;
2272
2273 op = object_property_find(obj, name, errp);
2274 if (!op) {
2275 return;
2276 }
2277
2278 g_free(op->description);
2279 op->description = g_strdup(description);
2280}
2281
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002282void object_class_property_set_description(ObjectClass *klass,
2283 const char *name,
2284 const char *description,
2285 Error **errp)
2286{
2287 ObjectProperty *op;
2288
2289 op = g_hash_table_lookup(klass->properties, name);
2290 if (!op) {
2291 error_setg(errp, "Property '.%s' not found", name);
2292 return;
2293 }
2294
2295 g_free(op->description);
2296 op->description = g_strdup(description);
2297}
2298
Paolo Bonzini2f262e02012-04-02 17:33:51 +02002299static void object_instance_init(Object *obj)
2300{
2301 object_property_add_str(obj, "type", qdev_get_type, NULL, NULL);
2302}
2303
Paolo Bonzini745549c2012-03-31 16:45:54 +02002304static void register_types(void)
2305{
2306 static TypeInfo interface_info = {
2307 .name = TYPE_INTERFACE,
Anthony Liguori33e95c62012-08-10 13:16:10 +10002308 .class_size = sizeof(InterfaceClass),
Paolo Bonzini745549c2012-03-31 16:45:54 +02002309 .abstract = true,
2310 };
2311
2312 static TypeInfo object_info = {
2313 .name = TYPE_OBJECT,
2314 .instance_size = sizeof(Object),
Paolo Bonzini2f262e02012-04-02 17:33:51 +02002315 .instance_init = object_instance_init,
Paolo Bonzini745549c2012-03-31 16:45:54 +02002316 .abstract = true,
2317 };
2318
Paolo Bonzini049cb3c2012-04-04 15:58:40 +02002319 type_interface = type_register_internal(&interface_info);
2320 type_register_internal(&object_info);
Paolo Bonzini745549c2012-03-31 16:45:54 +02002321}
2322
2323type_init(register_types)