blob: 3bc8a009bbec98f9870169f931df08d34dbf9c97 [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
Anthony Liguori33e95c62012-08-10 13:16:10 +1000205static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600206{
Anthony Liguori33e95c62012-08-10 13:16:10 +1000207 assert(target_type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600208
Cao jinb30d8052015-11-03 10:36:42 +0800209 /* Check if target_type is a direct ancestor of type */
Anthony Liguori33e95c62012-08-10 13:16:10 +1000210 while (type) {
211 if (type == target_type) {
212 return true;
213 }
214
215 type = type_get_parent(type);
216 }
217
218 return false;
219}
220
221static void type_initialize(TypeImpl *ti);
222
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100223static void type_initialize_interface(TypeImpl *ti, TypeImpl *interface_type,
224 TypeImpl *parent_type)
Anthony Liguori33e95c62012-08-10 13:16:10 +1000225{
226 InterfaceClass *new_iface;
227 TypeInfo info = { };
228 TypeImpl *iface_impl;
229
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100230 info.parent = parent_type->name;
231 info.name = g_strdup_printf("%s::%s", ti->name, interface_type->name);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000232 info.abstract = true;
233
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100234 iface_impl = type_new(&info);
235 iface_impl->parent_type = parent_type;
Anthony Liguori33e95c62012-08-10 13:16:10 +1000236 type_initialize(iface_impl);
237 g_free((char *)info.name);
238
239 new_iface = (InterfaceClass *)iface_impl->class;
240 new_iface->concrete_class = ti->class;
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100241 new_iface->interface_type = interface_type;
Anthony Liguori33e95c62012-08-10 13:16:10 +1000242
243 ti->class->interfaces = g_slist_append(ti->class->interfaces,
244 iface_impl->class);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600245}
246
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +0100247static void object_property_free(gpointer data)
248{
249 ObjectProperty *prop = data;
250
251 g_free(prop->name);
252 g_free(prop->type);
253 g_free(prop->description);
254 g_free(prop);
255}
256
Igor Mitsyankoac451032012-02-28 15:57:11 +0400257static void type_initialize(TypeImpl *ti)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600258{
Paolo Bonzini745549c2012-03-31 16:45:54 +0200259 TypeImpl *parent;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600260
261 if (ti->class) {
262 return;
263 }
264
265 ti->class_size = type_class_get_size(ti);
Igor Mitsyankoaca59af2012-02-28 15:57:10 +0400266 ti->instance_size = type_object_get_size(ti);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600267
268 ti->class = g_malloc0(ti->class_size);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600269
Paolo Bonzini745549c2012-03-31 16:45:54 +0200270 parent = type_get_parent(ti);
271 if (parent) {
Igor Mitsyankoac451032012-02-28 15:57:11 +0400272 type_initialize(parent);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000273 GSList *e;
274 int i;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600275
Andreas Färber8438a132015-11-16 17:49:20 +0100276 g_assert_cmpint(parent->class_size, <=, ti->class_size);
Paolo Bonzini745549c2012-03-31 16:45:54 +0200277 memcpy(ti->class, parent->class, parent->class_size);
Peter Crosthwaite3e407de2013-02-19 14:02:09 +1000278 ti->class->interfaces = NULL;
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +0100279 ti->class->properties = g_hash_table_new_full(
280 g_str_hash, g_str_equal, g_free, object_property_free);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000281
282 for (e = parent->class->interfaces; e; e = e->next) {
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100283 InterfaceClass *iface = e->data;
284 ObjectClass *klass = OBJECT_CLASS(iface);
285
286 type_initialize_interface(ti, iface->interface_type, klass->type);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000287 }
288
289 for (i = 0; i < ti->num_interfaces; i++) {
290 TypeImpl *t = type_get_by_name(ti->interfaces[i].typename);
291 for (e = ti->class->interfaces; e; e = e->next) {
292 TypeImpl *target_type = OBJECT_CLASS(e->data)->type;
293
294 if (type_is_ancestor(target_type, t)) {
295 break;
296 }
297 }
298
299 if (e) {
300 continue;
301 }
302
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100303 type_initialize_interface(ti, t, t);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000304 }
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +0100305 } else {
306 ti->class->properties = g_hash_table_new_full(
307 g_str_hash, g_str_equal, g_free, object_property_free);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600308 }
309
Paolo Bonzini745549c2012-03-31 16:45:54 +0200310 ti->class->type = ti;
311
312 while (parent) {
313 if (parent->class_base_init) {
314 parent->class_base_init(ti->class, ti->class_data);
315 }
316 parent = type_get_parent(parent);
317 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600318
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600319 if (ti->class_init) {
320 ti->class_init(ti->class, ti->class_data);
321 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600322}
323
324static void object_init_with_type(Object *obj, TypeImpl *ti)
325{
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600326 if (type_has_parent(ti)) {
327 object_init_with_type(obj, type_get_parent(ti));
328 }
329
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600330 if (ti->instance_init) {
331 ti->instance_init(obj);
332 }
333}
334
Eduardo Habkost8231c2d2013-07-10 17:08:41 -0300335static void object_post_init_with_type(Object *obj, TypeImpl *ti)
336{
337 if (ti->instance_post_init) {
338 ti->instance_post_init(obj);
339 }
340
341 if (type_has_parent(ti)) {
342 object_post_init_with_type(obj, type_get_parent(ti));
343 }
344}
345
Andreas Färber5b9237f2013-08-30 18:28:37 +0200346void object_initialize_with_type(void *data, size_t size, TypeImpl *type)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600347{
348 Object *obj = data;
349
350 g_assert(type != NULL);
Igor Mitsyankoac451032012-02-28 15:57:11 +0400351 type_initialize(type);
Igor Mitsyankoaca59af2012-02-28 15:57:10 +0400352
Andreas Färber8438a132015-11-16 17:49:20 +0100353 g_assert_cmpint(type->instance_size, >=, sizeof(Object));
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600354 g_assert(type->abstract == false);
Andreas Färber8438a132015-11-16 17:49:20 +0100355 g_assert_cmpint(size, >=, type->instance_size);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600356
357 memset(obj, 0, type->instance_size);
358 obj->class = type->class;
Paolo Bonzini764b6312012-11-23 09:47:12 +0100359 object_ref(obj);
Pavel Fedinb604a852015-10-13 13:37:45 +0100360 obj->properties = g_hash_table_new_full(g_str_hash, g_str_equal,
361 NULL, object_property_free);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600362 object_init_with_type(obj, type);
Eduardo Habkost8231c2d2013-07-10 17:08:41 -0300363 object_post_init_with_type(obj, type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600364}
365
Andreas Färber213f0c42013-08-23 19:37:12 +0200366void object_initialize(void *data, size_t size, const char *typename)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600367{
368 TypeImpl *type = type_get_by_name(typename);
369
Andreas Färber5b9237f2013-08-30 18:28:37 +0200370 object_initialize_with_type(data, size, type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600371}
372
Andreas Färber5d9d3f42012-05-27 00:32:40 +0200373static inline bool object_property_is_child(ObjectProperty *prop)
374{
375 return strstart(prop->type, "child<", NULL);
376}
377
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600378static void object_property_del_all(Object *obj)
379{
Pavel Fedinb604a852015-10-13 13:37:45 +0100380 ObjectProperty *prop;
381 GHashTableIter iter;
382 gpointer key, value;
383 bool released;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600384
Pavel Fedinb604a852015-10-13 13:37:45 +0100385 do {
386 released = false;
387 g_hash_table_iter_init(&iter, obj->properties);
388 while (g_hash_table_iter_next(&iter, &key, &value)) {
389 prop = value;
390 if (prop->release) {
391 prop->release(obj, prop->name, prop->opaque);
392 prop->release = NULL;
393 released = true;
394 break;
395 }
396 g_hash_table_iter_remove(&iter);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600397 }
Pavel Fedinb604a852015-10-13 13:37:45 +0100398 } while (released);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600399
Pavel Fedinb604a852015-10-13 13:37:45 +0100400 g_hash_table_unref(obj->properties);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600401}
402
403static void object_property_del_child(Object *obj, Object *child, Error **errp)
404{
405 ObjectProperty *prop;
Pavel Fedinb604a852015-10-13 13:37:45 +0100406 GHashTableIter iter;
407 gpointer key, value;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600408
Pavel Fedinb604a852015-10-13 13:37:45 +0100409 g_hash_table_iter_init(&iter, obj->properties);
410 while (g_hash_table_iter_next(&iter, &key, &value)) {
411 prop = value;
Andreas Färber5d9d3f42012-05-27 00:32:40 +0200412 if (object_property_is_child(prop) && prop->opaque == child) {
Pavel Fedinb604a852015-10-13 13:37:45 +0100413 if (prop->release) {
414 prop->release(obj, prop->name, prop->opaque);
415 prop->release = NULL;
416 }
417 break;
418 }
419 }
420 g_hash_table_iter_init(&iter, obj->properties);
421 while (g_hash_table_iter_next(&iter, &key, &value)) {
422 prop = value;
423 if (object_property_is_child(prop) && prop->opaque == child) {
424 g_hash_table_iter_remove(&iter);
Paolo Bonzini6c1fdcf2012-02-28 09:54:15 +0100425 break;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600426 }
427 }
428}
429
430void object_unparent(Object *obj)
431{
Michael S. Tsirkine998fa82013-03-18 21:01:37 +0200432 if (obj->parent) {
433 object_property_del_child(obj->parent, obj, NULL);
434 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600435}
436
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600437static void object_deinit(Object *obj, TypeImpl *type)
438{
439 if (type->instance_finalize) {
440 type->instance_finalize(obj);
441 }
442
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600443 if (type_has_parent(type)) {
444 object_deinit(obj, type_get_parent(type));
445 }
446}
447
Paolo Bonzini339c2702012-11-23 09:47:16 +0100448static void object_finalize(void *data)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600449{
450 Object *obj = data;
451 TypeImpl *ti = obj->class->type;
452
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600453 object_property_del_all(obj);
Paolo Bonzini76a6e1c2014-06-11 11:58:30 +0200454 object_deinit(obj, ti);
Anthony Liguoridb85b572011-12-23 08:47:39 -0600455
Andreas Färber8438a132015-11-16 17:49:20 +0100456 g_assert_cmpint(obj->ref, ==, 0);
Paolo Bonzinifde9bf42012-11-23 09:47:14 +0100457 if (obj->free) {
458 obj->free(obj);
459 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600460}
461
462Object *object_new_with_type(Type type)
463{
464 Object *obj;
465
466 g_assert(type != NULL);
Igor Mitsyankoac451032012-02-28 15:57:11 +0400467 type_initialize(type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600468
469 obj = g_malloc(type->instance_size);
Andreas Färber5b9237f2013-08-30 18:28:37 +0200470 object_initialize_with_type(obj, type->instance_size, type);
Paolo Bonzinifde9bf42012-11-23 09:47:14 +0100471 obj->free = g_free;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600472
473 return obj;
474}
475
476Object *object_new(const char *typename)
477{
478 TypeImpl *ti = type_get_by_name(typename);
479
480 return object_new_with_type(ti);
481}
482
Daniel P. Berrangea31bdae2015-05-13 17:14:06 +0100483
484Object *object_new_with_props(const char *typename,
485 Object *parent,
486 const char *id,
487 Error **errp,
488 ...)
489{
490 va_list vargs;
491 Object *obj;
492
493 va_start(vargs, errp);
494 obj = object_new_with_propv(typename, parent, id, errp, vargs);
495 va_end(vargs);
496
497 return obj;
498}
499
500
501Object *object_new_with_propv(const char *typename,
502 Object *parent,
503 const char *id,
504 Error **errp,
505 va_list vargs)
506{
507 Object *obj;
508 ObjectClass *klass;
509 Error *local_err = NULL;
510
511 klass = object_class_by_name(typename);
512 if (!klass) {
513 error_setg(errp, "invalid object type: %s", typename);
514 return NULL;
515 }
516
517 if (object_class_is_abstract(klass)) {
518 error_setg(errp, "object type '%s' is abstract", typename);
519 return NULL;
520 }
521 obj = object_new(typename);
522
523 if (object_set_propv(obj, &local_err, vargs) < 0) {
524 goto error;
525 }
526
527 object_property_add_child(parent, id, obj, &local_err);
528 if (local_err) {
529 goto error;
530 }
531
532 if (object_dynamic_cast(obj, TYPE_USER_CREATABLE)) {
533 user_creatable_complete(obj, &local_err);
534 if (local_err) {
535 object_unparent(obj);
536 goto error;
537 }
538 }
539
540 object_unref(OBJECT(obj));
541 return obj;
542
543 error:
544 if (local_err) {
545 error_propagate(errp, local_err);
546 }
547 object_unref(obj);
548 return NULL;
549}
550
551
552int object_set_props(Object *obj,
553 Error **errp,
554 ...)
555{
556 va_list vargs;
557 int ret;
558
559 va_start(vargs, errp);
560 ret = object_set_propv(obj, errp, vargs);
561 va_end(vargs);
562
563 return ret;
564}
565
566
567int object_set_propv(Object *obj,
568 Error **errp,
569 va_list vargs)
570{
571 const char *propname;
572 Error *local_err = NULL;
573
574 propname = va_arg(vargs, char *);
575 while (propname != NULL) {
576 const char *value = va_arg(vargs, char *);
577
578 g_assert(value != NULL);
579 object_property_parse(obj, value, propname, &local_err);
580 if (local_err) {
581 error_propagate(errp, local_err);
582 return -1;
583 }
584 propname = va_arg(vargs, char *);
585 }
586
587 return 0;
588}
589
590
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600591Object *object_dynamic_cast(Object *obj, const char *typename)
592{
Paolo Bonzinib7f43fe2012-11-23 16:56:17 +0100593 if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) {
Paolo Bonziniacc4af32012-02-03 11:57:23 +0100594 return obj;
595 }
596
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600597 return NULL;
598}
599
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200600Object *object_dynamic_cast_assert(Object *obj, const char *typename,
601 const char *file, int line, const char *func)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600602{
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200603 trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)",
604 typename, file, line, func);
605
Paolo Bonzini3556c232013-05-10 14:16:40 +0200606#ifdef CONFIG_QOM_CAST_DEBUG
Anthony Liguori03587322013-05-13 15:22:24 -0500607 int i;
608 Object *inst;
609
Peter Crosthwaite95916ab2013-05-22 11:19:16 +1000610 for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) {
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800611 if (obj->class->object_cast_cache[i] == typename) {
Anthony Liguori03587322013-05-13 15:22:24 -0500612 goto out;
613 }
614 }
615
616 inst = object_dynamic_cast(obj, typename);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600617
Paolo Bonzinib7f43fe2012-11-23 16:56:17 +0100618 if (!inst && obj) {
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200619 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
620 file, line, func, obj, typename);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600621 abort();
622 }
623
Paolo Bonzini3556c232013-05-10 14:16:40 +0200624 assert(obj == inst);
Anthony Liguori03587322013-05-13 15:22:24 -0500625
Peter Crosthwaite95916ab2013-05-22 11:19:16 +1000626 if (obj && obj == inst) {
Anthony Liguori03587322013-05-13 15:22:24 -0500627 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800628 obj->class->object_cast_cache[i - 1] =
629 obj->class->object_cast_cache[i];
Anthony Liguori03587322013-05-13 15:22:24 -0500630 }
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800631 obj->class->object_cast_cache[i - 1] = typename;
Anthony Liguori03587322013-05-13 15:22:24 -0500632 }
633
634out:
Paolo Bonzini3556c232013-05-10 14:16:40 +0200635#endif
636 return obj;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600637}
638
639ObjectClass *object_class_dynamic_cast(ObjectClass *class,
640 const char *typename)
641{
Anthony Liguori33e95c62012-08-10 13:16:10 +1000642 ObjectClass *ret = NULL;
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200643 TypeImpl *target_type;
644 TypeImpl *type;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600645
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200646 if (!class) {
647 return NULL;
648 }
649
Paolo Bonzini793c96b2013-05-10 14:16:37 +0200650 /* A simple fast path that can trigger a lot for leaf classes. */
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200651 type = class->type;
Paolo Bonzini793c96b2013-05-10 14:16:37 +0200652 if (type->name == typename) {
653 return class;
654 }
655
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200656 target_type = type_get_by_name(typename);
Alexander Graf9ab880b2013-04-30 15:02:16 +0200657 if (!target_type) {
658 /* target class type unknown, so fail the cast */
659 return NULL;
660 }
661
Peter Crosthwaite00e2cea2013-02-19 14:02:10 +1000662 if (type->class->interfaces &&
663 type_is_ancestor(target_type, type_interface)) {
Anthony Liguori33e95c62012-08-10 13:16:10 +1000664 int found = 0;
665 GSList *i;
666
667 for (i = class->interfaces; i; i = i->next) {
668 ObjectClass *target_class = i->data;
669
670 if (type_is_ancestor(target_class->type, target_type)) {
671 ret = target_class;
672 found++;
673 }
674 }
675
676 /* The match was ambiguous, don't allow a cast */
677 if (found > 1) {
678 ret = NULL;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600679 }
Anthony Liguori33e95c62012-08-10 13:16:10 +1000680 } else if (type_is_ancestor(type, target_type)) {
681 ret = class;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600682 }
683
Anthony Liguori33e95c62012-08-10 13:16:10 +1000684 return ret;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600685}
686
687ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200688 const char *typename,
689 const char *file, int line,
690 const char *func)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600691{
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200692 ObjectClass *ret;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600693
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200694 trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)",
695 typename, file, line, func);
696
Anthony Liguori03587322013-05-13 15:22:24 -0500697#ifdef CONFIG_QOM_CAST_DEBUG
698 int i;
699
Peter Crosthwaite9d6a3d52013-06-18 19:18:59 +1000700 for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) {
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800701 if (class->class_cast_cache[i] == typename) {
Anthony Liguori03587322013-05-13 15:22:24 -0500702 ret = class;
703 goto out;
704 }
705 }
706#else
Peter Crosthwaite9d6a3d52013-06-18 19:18:59 +1000707 if (!class || !class->interfaces) {
Paolo Bonzini3556c232013-05-10 14:16:40 +0200708 return class;
709 }
710#endif
711
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200712 ret = object_class_dynamic_cast(class, typename);
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200713 if (!ret && class) {
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200714 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
715 file, line, func, class, typename);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600716 abort();
717 }
718
Anthony Liguori03587322013-05-13 15:22:24 -0500719#ifdef CONFIG_QOM_CAST_DEBUG
Peter Crosthwaite9d6a3d52013-06-18 19:18:59 +1000720 if (class && ret == class) {
Anthony Liguori03587322013-05-13 15:22:24 -0500721 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800722 class->class_cast_cache[i - 1] = class->class_cast_cache[i];
Anthony Liguori03587322013-05-13 15:22:24 -0500723 }
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800724 class->class_cast_cache[i - 1] = typename;
Anthony Liguori03587322013-05-13 15:22:24 -0500725 }
726out:
727#endif
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600728 return ret;
729}
730
731const char *object_get_typename(Object *obj)
732{
733 return obj->class->type->name;
734}
735
736ObjectClass *object_get_class(Object *obj)
737{
738 return obj->class;
739}
740
Andreas Färber17862372013-01-23 12:20:18 +0100741bool object_class_is_abstract(ObjectClass *klass)
742{
743 return klass->type->abstract;
744}
745
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600746const char *object_class_get_name(ObjectClass *klass)
747{
748 return klass->type->name;
749}
750
751ObjectClass *object_class_by_name(const char *typename)
752{
753 TypeImpl *type = type_get_by_name(typename);
754
755 if (!type) {
756 return NULL;
757 }
758
Igor Mitsyankoac451032012-02-28 15:57:11 +0400759 type_initialize(type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600760
761 return type->class;
762}
763
Paolo Bonzinie7cce672012-05-02 13:30:54 +0200764ObjectClass *object_class_get_parent(ObjectClass *class)
765{
766 TypeImpl *type = type_get_parent(class->type);
767
768 if (!type) {
769 return NULL;
770 }
771
772 type_initialize(type);
773
774 return type->class;
775}
776
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600777typedef struct OCFData
778{
779 void (*fn)(ObjectClass *klass, void *opaque);
Anthony Liguori93c511a2011-12-22 14:11:53 -0600780 const char *implements_type;
781 bool include_abstract;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600782 void *opaque;
783} OCFData;
784
785static void object_class_foreach_tramp(gpointer key, gpointer value,
786 gpointer opaque)
787{
788 OCFData *data = opaque;
789 TypeImpl *type = value;
Anthony Liguori93c511a2011-12-22 14:11:53 -0600790 ObjectClass *k;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600791
Igor Mitsyankoac451032012-02-28 15:57:11 +0400792 type_initialize(type);
Anthony Liguori93c511a2011-12-22 14:11:53 -0600793 k = type->class;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600794
Anthony Liguori93c511a2011-12-22 14:11:53 -0600795 if (!data->include_abstract && type->abstract) {
796 return;
797 }
798
799 if (data->implements_type &&
800 !object_class_dynamic_cast(k, data->implements_type)) {
801 return;
802 }
803
804 data->fn(k, data->opaque);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600805}
806
807void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
Anthony Liguori93c511a2011-12-22 14:11:53 -0600808 const char *implements_type, bool include_abstract,
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600809 void *opaque)
810{
Anthony Liguori93c511a2011-12-22 14:11:53 -0600811 OCFData data = { fn, implements_type, include_abstract, opaque };
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600812
Hervé Poussineauf54c19c2013-12-03 16:42:00 +0100813 enumerating_types = true;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600814 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
Hervé Poussineauf54c19c2013-12-03 16:42:00 +0100815 enumerating_types = false;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600816}
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600817
Peter Crosthwaited714b8d2015-09-08 17:38:43 +0100818static int do_object_child_foreach(Object *obj,
819 int (*fn)(Object *child, void *opaque),
820 void *opaque, bool recurse)
Paolo Bonzini32efc532012-04-11 23:30:20 +0200821{
Pavel Fedinb604a852015-10-13 13:37:45 +0100822 GHashTableIter iter;
823 ObjectProperty *prop;
Paolo Bonzini32efc532012-04-11 23:30:20 +0200824 int ret = 0;
825
Pavel Fedinb604a852015-10-13 13:37:45 +0100826 g_hash_table_iter_init(&iter, obj->properties);
827 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
Paolo Bonzini32efc532012-04-11 23:30:20 +0200828 if (object_property_is_child(prop)) {
Peter Crosthwaited714b8d2015-09-08 17:38:43 +0100829 Object *child = prop->opaque;
830
831 ret = fn(child, opaque);
Paolo Bonzini32efc532012-04-11 23:30:20 +0200832 if (ret != 0) {
833 break;
834 }
Peter Crosthwaited714b8d2015-09-08 17:38:43 +0100835 if (recurse) {
836 do_object_child_foreach(child, fn, opaque, true);
837 }
Paolo Bonzini32efc532012-04-11 23:30:20 +0200838 }
839 }
840 return ret;
841}
842
Peter Crosthwaited714b8d2015-09-08 17:38:43 +0100843int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
844 void *opaque)
845{
846 return do_object_child_foreach(obj, fn, opaque, false);
847}
848
849int object_child_foreach_recursive(Object *obj,
850 int (*fn)(Object *child, void *opaque),
851 void *opaque)
852{
853 return do_object_child_foreach(obj, fn, opaque, true);
854}
855
Andreas Färber418ba9e2012-02-25 23:07:34 +0100856static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
857{
858 GSList **list = opaque;
859
860 *list = g_slist_prepend(*list, klass);
861}
862
863GSList *object_class_get_list(const char *implements_type,
864 bool include_abstract)
865{
866 GSList *list = NULL;
867
868 object_class_foreach(object_class_get_list_tramp,
869 implements_type, include_abstract, &list);
870 return list;
871}
872
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600873void object_ref(Object *obj)
874{
Peter Crosthwaite8ffad852014-06-05 23:13:36 -0700875 if (!obj) {
876 return;
877 }
Andreas Färber8438a132015-11-16 17:49:20 +0100878 atomic_inc(&obj->ref);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600879}
880
881void object_unref(Object *obj)
882{
Peter Crosthwaite8ffad852014-06-05 23:13:36 -0700883 if (!obj) {
884 return;
885 }
Andreas Färber8438a132015-11-16 17:49:20 +0100886 g_assert_cmpint(obj->ref, >, 0);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600887
888 /* parent always holds a reference to its children */
Jan Kiszkaf08c03f2013-07-02 11:36:39 +0200889 if (atomic_fetch_dec(&obj->ref) == 1) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600890 object_finalize(obj);
891 }
892}
893
Paolo Bonzini64607d02014-06-05 13:11:51 +0200894ObjectProperty *
895object_property_add(Object *obj, const char *name, const char *type,
896 ObjectPropertyAccessor *get,
897 ObjectPropertyAccessor *set,
898 ObjectPropertyRelease *release,
899 void *opaque, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600900{
Peter Maydell54852b02013-03-25 13:15:13 +0000901 ObjectProperty *prop;
Peter Crosthwaite33965902014-08-19 23:55:52 -0700902 size_t name_len = strlen(name);
903
904 if (name_len >= 3 && !memcmp(name + name_len - 3, "[*]", 4)) {
905 int i;
906 ObjectProperty *ret;
907 char *name_no_array = g_strdup(name);
908
909 name_no_array[name_len - 3] = '\0';
910 for (i = 0; ; ++i) {
911 char *full_name = g_strdup_printf("%s[%d]", name_no_array, i);
912
913 ret = object_property_add(obj, full_name, type, get, set,
914 release, opaque, NULL);
915 g_free(full_name);
916 if (ret) {
917 break;
918 }
919 }
920 g_free(name_no_array);
921 return ret;
922 }
Peter Maydell54852b02013-03-25 13:15:13 +0000923
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +0100924 if (object_property_find(obj, name, NULL) != NULL) {
Pavel Fedinb604a852015-10-13 13:37:45 +0100925 error_setg(errp, "attempt to add duplicate property '%s'"
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +0100926 " to object (type '%s')", name,
927 object_get_typename(obj));
Pavel Fedinb604a852015-10-13 13:37:45 +0100928 return NULL;
Peter Maydell54852b02013-03-25 13:15:13 +0000929 }
930
931 prop = g_malloc0(sizeof(*prop));
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600932
933 prop->name = g_strdup(name);
934 prop->type = g_strdup(type);
935
936 prop->get = get;
937 prop->set = set;
938 prop->release = release;
939 prop->opaque = opaque;
940
Pavel Fedinb604a852015-10-13 13:37:45 +0100941 g_hash_table_insert(obj->properties, prop->name, prop);
Paolo Bonzini64607d02014-06-05 13:11:51 +0200942 return prop;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600943}
944
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +0100945ObjectProperty *
946object_class_property_add(ObjectClass *klass,
947 const char *name,
948 const char *type,
949 ObjectPropertyAccessor *get,
950 ObjectPropertyAccessor *set,
951 ObjectPropertyRelease *release,
952 void *opaque,
953 Error **errp)
954{
955 ObjectProperty *prop;
956
957 if (object_class_property_find(klass, name, NULL) != NULL) {
958 error_setg(errp, "attempt to add duplicate property '%s'"
959 " to object (type '%s')", name,
960 object_class_get_name(klass));
961 return NULL;
962 }
963
964 prop = g_malloc0(sizeof(*prop));
965
966 prop->name = g_strdup(name);
967 prop->type = g_strdup(type);
968
969 prop->get = get;
970 prop->set = set;
971 prop->release = release;
972 prop->opaque = opaque;
973
974 g_hash_table_insert(klass->properties, g_strdup(name), prop);
975
976 return prop;
977}
978
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200979ObjectProperty *object_property_find(Object *obj, const char *name,
980 Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600981{
982 ObjectProperty *prop;
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +0100983 ObjectClass *klass = object_get_class(obj);
984
985 prop = object_class_property_find(klass, name, NULL);
986 if (prop) {
987 return prop;
988 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600989
Pavel Fedinb604a852015-10-13 13:37:45 +0100990 prop = g_hash_table_lookup(obj->properties, name);
991 if (prop) {
992 return prop;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600993 }
994
Cole Robinsonf231b882014-03-21 19:42:26 -0400995 error_setg(errp, "Property '.%s' not found", name);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600996 return NULL;
997}
998
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000999void object_property_iter_init(ObjectPropertyIterator *iter,
1000 Object *obj)
Daniel P. Berrangea00c9482015-10-13 13:37:40 +01001001{
Daniel P. Berrange7746abd2015-12-09 12:34:02 +00001002 g_hash_table_iter_init(&iter->iter, obj->properties);
1003 iter->nextclass = object_get_class(obj);
Daniel P. Berrangea00c9482015-10-13 13:37:40 +01001004}
1005
1006ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter)
1007{
Pavel Fedinb604a852015-10-13 13:37:45 +01001008 gpointer key, val;
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001009 while (!g_hash_table_iter_next(&iter->iter, &key, &val)) {
1010 if (!iter->nextclass) {
1011 return NULL;
1012 }
1013 g_hash_table_iter_init(&iter->iter, iter->nextclass->properties);
1014 iter->nextclass = object_class_get_parent(iter->nextclass);
Daniel P. Berrangea00c9482015-10-13 13:37:40 +01001015 }
Pavel Fedinb604a852015-10-13 13:37:45 +01001016 return val;
Daniel P. Berrangea00c9482015-10-13 13:37:40 +01001017}
1018
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001019ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name,
1020 Error **errp)
1021{
1022 ObjectProperty *prop;
1023 ObjectClass *parent_klass;
1024
1025 parent_klass = object_class_get_parent(klass);
1026 if (parent_klass) {
1027 prop = object_class_property_find(parent_klass, name, NULL);
1028 if (prop) {
1029 return prop;
1030 }
1031 }
1032
1033 prop = g_hash_table_lookup(klass->properties, name);
1034 if (!prop) {
1035 error_setg(errp, "Property '.%s' not found", name);
1036 }
1037 return prop;
1038}
1039
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001040void object_property_del(Object *obj, const char *name, Error **errp)
1041{
Pavel Fedinb604a852015-10-13 13:37:45 +01001042 ObjectProperty *prop = g_hash_table_lookup(obj->properties, name);
1043
1044 if (!prop) {
1045 error_setg(errp, "Property '.%s' not found", name);
Anthony Liguori0866aca2011-12-23 15:34:39 -06001046 return;
1047 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001048
Anthony Liguori0866aca2011-12-23 15:34:39 -06001049 if (prop->release) {
1050 prop->release(obj, name, prop->opaque);
1051 }
Pavel Fedinb604a852015-10-13 13:37:45 +01001052 g_hash_table_remove(obj->properties, name);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001053}
1054
1055void object_property_get(Object *obj, Visitor *v, const char *name,
1056 Error **errp)
1057{
Paolo Bonzini89bfe002012-04-12 18:00:18 +02001058 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001059 if (prop == NULL) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001060 return;
1061 }
1062
1063 if (!prop->get) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001064 error_setg(errp, QERR_PERMISSION_DENIED);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001065 } else {
Eric Blaked7bce992016-01-29 06:48:55 -07001066 prop->get(obj, v, name, prop->opaque, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001067 }
1068}
1069
1070void object_property_set(Object *obj, Visitor *v, const char *name,
1071 Error **errp)
1072{
Paolo Bonzini89bfe002012-04-12 18:00:18 +02001073 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001074 if (prop == NULL) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001075 return;
1076 }
1077
1078 if (!prop->set) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001079 error_setg(errp, QERR_PERMISSION_DENIED);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001080 } else {
Eric Blaked7bce992016-01-29 06:48:55 -07001081 prop->set(obj, v, name, prop->opaque, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001082 }
1083}
1084
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001085void object_property_set_str(Object *obj, const char *value,
1086 const char *name, Error **errp)
1087{
1088 QString *qstr = qstring_from_str(value);
1089 object_property_set_qobject(obj, QOBJECT(qstr), name, errp);
1090
1091 QDECREF(qstr);
1092}
1093
1094char *object_property_get_str(Object *obj, const char *name,
1095 Error **errp)
1096{
1097 QObject *ret = object_property_get_qobject(obj, name, errp);
1098 QString *qstring;
1099 char *retval;
1100
1101 if (!ret) {
1102 return NULL;
1103 }
1104 qstring = qobject_to_qstring(ret);
1105 if (!qstring) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001106 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001107 retval = NULL;
1108 } else {
1109 retval = g_strdup(qstring_get_str(qstring));
1110 }
1111
1112 QDECREF(qstring);
1113 return retval;
1114}
1115
Paolo Bonzini1d9c5a12012-02-02 10:51:57 +01001116void object_property_set_link(Object *obj, Object *value,
1117 const char *name, Error **errp)
1118{
Peter Crosthwaited3c49312014-09-25 22:19:19 -07001119 if (value) {
1120 gchar *path = object_get_canonical_path(value);
1121 object_property_set_str(obj, path, name, errp);
1122 g_free(path);
1123 } else {
1124 object_property_set_str(obj, "", name, errp);
1125 }
Paolo Bonzini1d9c5a12012-02-02 10:51:57 +01001126}
1127
1128Object *object_property_get_link(Object *obj, const char *name,
1129 Error **errp)
1130{
1131 char *str = object_property_get_str(obj, name, errp);
1132 Object *target = NULL;
1133
1134 if (str && *str) {
1135 target = object_resolve_path(str, NULL);
1136 if (!target) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01001137 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1138 "Device '%s' not found", str);
Paolo Bonzini1d9c5a12012-02-02 10:51:57 +01001139 }
1140 }
1141
1142 g_free(str);
1143 return target;
1144}
1145
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001146void object_property_set_bool(Object *obj, bool value,
1147 const char *name, Error **errp)
1148{
Eric Blakefc48ffc2015-05-15 16:24:59 -06001149 QBool *qbool = qbool_from_bool(value);
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001150 object_property_set_qobject(obj, QOBJECT(qbool), name, errp);
1151
1152 QDECREF(qbool);
1153}
1154
1155bool object_property_get_bool(Object *obj, const char *name,
1156 Error **errp)
1157{
1158 QObject *ret = object_property_get_qobject(obj, name, errp);
1159 QBool *qbool;
1160 bool retval;
1161
1162 if (!ret) {
1163 return false;
1164 }
1165 qbool = qobject_to_qbool(ret);
1166 if (!qbool) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001167 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001168 retval = false;
1169 } else {
Eric Blakefc48ffc2015-05-15 16:24:59 -06001170 retval = qbool_get_bool(qbool);
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001171 }
1172
1173 QDECREF(qbool);
1174 return retval;
1175}
1176
1177void object_property_set_int(Object *obj, int64_t value,
1178 const char *name, Error **errp)
1179{
1180 QInt *qint = qint_from_int(value);
1181 object_property_set_qobject(obj, QOBJECT(qint), name, errp);
1182
1183 QDECREF(qint);
1184}
1185
1186int64_t object_property_get_int(Object *obj, const char *name,
1187 Error **errp)
1188{
1189 QObject *ret = object_property_get_qobject(obj, name, errp);
1190 QInt *qint;
1191 int64_t retval;
1192
1193 if (!ret) {
1194 return -1;
1195 }
1196 qint = qobject_to_qint(ret);
1197 if (!qint) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001198 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001199 retval = -1;
1200 } else {
1201 retval = qint_get_int(qint);
1202 }
1203
1204 QDECREF(qint);
1205 return retval;
1206}
1207
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01001208typedef struct EnumProperty {
1209 const char * const *strings;
1210 int (*get)(Object *, Error **);
1211 void (*set)(Object *, int, Error **);
1212} EnumProperty;
1213
Hu Tao1f217722014-05-14 17:43:33 +08001214int object_property_get_enum(Object *obj, const char *name,
Daniel P. Berrangea3590da2015-05-27 16:07:56 +01001215 const char *typename, Error **errp)
Hu Tao1f217722014-05-14 17:43:33 +08001216{
Markus Armbruster4715d422015-08-25 20:00:45 +02001217 Error *err = NULL;
Hu Tao1f217722014-05-14 17:43:33 +08001218 StringOutputVisitor *sov;
1219 StringInputVisitor *siv;
Chen Fan976620a2014-08-18 14:46:34 +08001220 char *str;
Hu Tao1f217722014-05-14 17:43:33 +08001221 int ret;
Daniel P. Berrangea3590da2015-05-27 16:07:56 +01001222 ObjectProperty *prop = object_property_find(obj, name, errp);
1223 EnumProperty *enumprop;
1224
1225 if (prop == NULL) {
1226 return 0;
1227 }
1228
1229 if (!g_str_equal(prop->type, typename)) {
1230 error_setg(errp, "Property %s on %s is not '%s' enum type",
1231 name, object_class_get_name(
1232 object_get_class(obj)), typename);
1233 return 0;
1234 }
1235
1236 enumprop = prop->opaque;
Hu Tao1f217722014-05-14 17:43:33 +08001237
1238 sov = string_output_visitor_new(false);
Markus Armbruster4715d422015-08-25 20:00:45 +02001239 object_property_get(obj, string_output_get_visitor(sov), name, &err);
1240 if (err) {
1241 error_propagate(errp, err);
1242 string_output_visitor_cleanup(sov);
1243 return 0;
1244 }
Chen Fan976620a2014-08-18 14:46:34 +08001245 str = string_output_get_string(sov);
1246 siv = string_input_visitor_new(str);
Hu Tao1f217722014-05-14 17:43:33 +08001247 string_output_visitor_cleanup(sov);
Eric Blake51e72bc2016-01-29 06:48:54 -07001248 visit_type_enum(string_input_get_visitor(siv), name, &ret,
Eric Blake337283d2016-01-29 06:48:57 -07001249 enumprop->strings, errp);
Chen Fan976620a2014-08-18 14:46:34 +08001250
1251 g_free(str);
Hu Tao1f217722014-05-14 17:43:33 +08001252 string_input_visitor_cleanup(siv);
1253
1254 return ret;
1255}
1256
1257void object_property_get_uint16List(Object *obj, const char *name,
1258 uint16List **list, Error **errp)
1259{
Markus Armbruster4715d422015-08-25 20:00:45 +02001260 Error *err = NULL;
Hu Tao1f217722014-05-14 17:43:33 +08001261 StringOutputVisitor *ov;
1262 StringInputVisitor *iv;
Chen Fan976620a2014-08-18 14:46:34 +08001263 char *str;
Hu Tao1f217722014-05-14 17:43:33 +08001264
1265 ov = string_output_visitor_new(false);
1266 object_property_get(obj, string_output_get_visitor(ov),
Markus Armbruster4715d422015-08-25 20:00:45 +02001267 name, &err);
1268 if (err) {
1269 error_propagate(errp, err);
1270 goto out;
1271 }
Chen Fan976620a2014-08-18 14:46:34 +08001272 str = string_output_get_string(ov);
1273 iv = string_input_visitor_new(str);
Eric Blake51e72bc2016-01-29 06:48:54 -07001274 visit_type_uint16List(string_input_get_visitor(iv), NULL, list, errp);
Chen Fan976620a2014-08-18 14:46:34 +08001275
1276 g_free(str);
Hu Tao1f217722014-05-14 17:43:33 +08001277 string_input_visitor_cleanup(iv);
Markus Armbruster4715d422015-08-25 20:00:45 +02001278out:
1279 string_output_visitor_cleanup(ov);
Hu Tao1f217722014-05-14 17:43:33 +08001280}
1281
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001282void object_property_parse(Object *obj, const char *string,
1283 const char *name, Error **errp)
1284{
Eric Blakef8b7f1a2015-09-29 16:21:09 -06001285 StringInputVisitor *siv;
1286 siv = string_input_visitor_new(string);
1287 object_property_set(obj, string_input_get_visitor(siv), name, errp);
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001288
Eric Blakef8b7f1a2015-09-29 16:21:09 -06001289 string_input_visitor_cleanup(siv);
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001290}
1291
Paolo Bonzini0b7593e2014-02-08 11:01:50 +01001292char *object_property_print(Object *obj, const char *name, bool human,
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001293 Error **errp)
1294{
Eric Blakef8b7f1a2015-09-29 16:21:09 -06001295 StringOutputVisitor *sov;
Gonglei3a530092014-09-27 13:13:55 +08001296 char *string = NULL;
1297 Error *local_err = NULL;
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001298
Eric Blakef8b7f1a2015-09-29 16:21:09 -06001299 sov = string_output_visitor_new(human);
1300 object_property_get(obj, string_output_get_visitor(sov), name, &local_err);
Gonglei3a530092014-09-27 13:13:55 +08001301 if (local_err) {
1302 error_propagate(errp, local_err);
1303 goto out;
1304 }
1305
Eric Blakef8b7f1a2015-09-29 16:21:09 -06001306 string = string_output_get_string(sov);
Gonglei3a530092014-09-27 13:13:55 +08001307
1308out:
Eric Blakef8b7f1a2015-09-29 16:21:09 -06001309 string_output_visitor_cleanup(sov);
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001310 return string;
1311}
1312
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001313const char *object_property_get_type(Object *obj, const char *name, Error **errp)
1314{
Paolo Bonzini89bfe002012-04-12 18:00:18 +02001315 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001316 if (prop == NULL) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001317 return NULL;
1318 }
1319
1320 return prop->type;
1321}
1322
1323Object *object_get_root(void)
1324{
Anthony Liguori8b45d442011-12-23 09:08:05 -06001325 static Object *root;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001326
Anthony Liguori8b45d442011-12-23 09:08:05 -06001327 if (!root) {
1328 root = object_new("container");
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001329 }
1330
Anthony Liguori8b45d442011-12-23 09:08:05 -06001331 return root;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001332}
1333
Daniel P. Berrangebc2256c2015-05-13 17:14:05 +01001334Object *object_get_objects_root(void)
1335{
1336 return container_get(object_get_root(), "/objects");
1337}
1338
Eric Blaked7bce992016-01-29 06:48:55 -07001339static void object_get_child_property(Object *obj, Visitor *v,
1340 const char *name, void *opaque,
1341 Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001342{
1343 Object *child = opaque;
1344 gchar *path;
1345
1346 path = object_get_canonical_path(child);
Eric Blake51e72bc2016-01-29 06:48:54 -07001347 visit_type_str(v, name, &path, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001348 g_free(path);
1349}
1350
Paolo Bonzini64607d02014-06-05 13:11:51 +02001351static Object *object_resolve_child_property(Object *parent, void *opaque, const gchar *part)
1352{
1353 return opaque;
1354}
1355
Anthony Liguoridb85b572011-12-23 08:47:39 -06001356static void object_finalize_child_property(Object *obj, const char *name,
1357 void *opaque)
1358{
1359 Object *child = opaque;
1360
Paolo Bonzinibffc6872014-06-11 11:57:38 +02001361 if (child->class->unparent) {
1362 (child->class->unparent)(child);
1363 }
1364 child->parent = NULL;
Anthony Liguoridb85b572011-12-23 08:47:39 -06001365 object_unref(child);
1366}
1367
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001368void object_property_add_child(Object *obj, const char *name,
1369 Object *child, Error **errp)
1370{
Paolo Bonzinib0ed5e92013-12-20 23:21:08 +01001371 Error *local_err = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001372 gchar *type;
Paolo Bonzini64607d02014-06-05 13:11:51 +02001373 ObjectProperty *op;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001374
Peter Crosthwaite8faa2f82014-09-25 22:19:52 -07001375 if (child->parent != NULL) {
1376 error_setg(errp, "child object is already parented");
1377 return;
1378 }
1379
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001380 type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child)));
1381
Paolo Bonzini64607d02014-06-05 13:11:51 +02001382 op = object_property_add(obj, name, type, object_get_child_property, NULL,
1383 object_finalize_child_property, child, &local_err);
Paolo Bonzinib0ed5e92013-12-20 23:21:08 +01001384 if (local_err) {
1385 error_propagate(errp, local_err);
1386 goto out;
1387 }
Paolo Bonzini64607d02014-06-05 13:11:51 +02001388
1389 op->resolve = object_resolve_child_property;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001390 object_ref(child);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001391 child->parent = obj;
1392
Paolo Bonzinib0ed5e92013-12-20 23:21:08 +01001393out:
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001394 g_free(type);
1395}
1396
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001397void object_property_allow_set_link(Object *obj, const char *name,
1398 Object *val, Error **errp)
1399{
1400 /* Allow the link to be set, always */
1401}
1402
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001403typedef struct {
1404 Object **child;
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001405 void (*check)(Object *, const char *, Object *, Error **);
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001406 ObjectPropertyLinkFlags flags;
1407} LinkProperty;
1408
Eric Blaked7bce992016-01-29 06:48:55 -07001409static void object_get_link_property(Object *obj, Visitor *v,
1410 const char *name, void *opaque,
1411 Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001412{
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001413 LinkProperty *lprop = opaque;
1414 Object **child = lprop->child;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001415 gchar *path;
1416
1417 if (*child) {
1418 path = object_get_canonical_path(*child);
Eric Blake51e72bc2016-01-29 06:48:54 -07001419 visit_type_str(v, name, &path, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001420 g_free(path);
1421 } else {
1422 path = (gchar *)"";
Eric Blake51e72bc2016-01-29 06:48:54 -07001423 visit_type_str(v, name, &path, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001424 }
1425}
1426
Stefan Hajnoczif5ec6702014-03-19 08:58:53 +01001427/*
1428 * object_resolve_link:
1429 *
1430 * Lookup an object and ensure its type matches the link property type. This
1431 * is similar to object_resolve_path() except type verification against the
1432 * link property is performed.
1433 *
1434 * Returns: The matched object or NULL on path lookup failures.
1435 */
1436static Object *object_resolve_link(Object *obj, const char *name,
1437 const char *path, Error **errp)
1438{
1439 const char *type;
1440 gchar *target_type;
1441 bool ambiguous = false;
1442 Object *target;
1443
1444 /* Go from link<FOO> to FOO. */
1445 type = object_property_get_type(obj, name, NULL);
1446 target_type = g_strndup(&type[5], strlen(type) - 6);
1447 target = object_resolve_path_type(path, target_type, &ambiguous);
1448
1449 if (ambiguous) {
Eric Blake455b0fd2015-11-10 23:51:20 -07001450 error_setg(errp, "Path '%s' does not uniquely identify an object",
1451 path);
Stefan Hajnoczif5ec6702014-03-19 08:58:53 +01001452 } else if (!target) {
1453 target = object_resolve_path(path, &ambiguous);
1454 if (target || ambiguous) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001455 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
Stefan Hajnoczif5ec6702014-03-19 08:58:53 +01001456 } else {
Markus Armbruster75158eb2015-03-16 08:57:47 +01001457 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1458 "Device '%s' not found", path);
Stefan Hajnoczif5ec6702014-03-19 08:58:53 +01001459 }
1460 target = NULL;
1461 }
1462 g_free(target_type);
1463
1464 return target;
1465}
1466
Eric Blaked7bce992016-01-29 06:48:55 -07001467static void object_set_link_property(Object *obj, Visitor *v,
1468 const char *name, void *opaque,
1469 Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001470{
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001471 Error *local_err = NULL;
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001472 LinkProperty *prop = opaque;
1473 Object **child = prop->child;
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001474 Object *old_target = *child;
1475 Object *new_target = NULL;
1476 char *path = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001477
Eric Blake51e72bc2016-01-29 06:48:54 -07001478 visit_type_str(v, name, &path, &local_err);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001479
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001480 if (!local_err && strcmp(path, "") != 0) {
1481 new_target = object_resolve_link(obj, name, path, &local_err);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001482 }
1483
1484 g_free(path);
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001485 if (local_err) {
1486 error_propagate(errp, local_err);
1487 return;
1488 }
Alexander Barabashf0cdc962012-02-22 19:22:26 +02001489
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001490 prop->check(obj, name, new_target, &local_err);
1491 if (local_err) {
1492 error_propagate(errp, local_err);
1493 return;
1494 }
1495
Peter Crosthwaite8ffad852014-06-05 23:13:36 -07001496 object_ref(new_target);
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001497 *child = new_target;
Peter Crosthwaite8ffad852014-06-05 23:13:36 -07001498 object_unref(old_target);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001499}
1500
Paolo Bonzini64607d02014-06-05 13:11:51 +02001501static Object *object_resolve_link_property(Object *parent, void *opaque, const gchar *part)
1502{
1503 LinkProperty *lprop = opaque;
1504
1505 return *lprop->child;
1506}
1507
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001508static void object_release_link_property(Object *obj, const char *name,
1509 void *opaque)
1510{
1511 LinkProperty *prop = opaque;
1512
1513 if ((prop->flags & OBJ_PROP_LINK_UNREF_ON_RELEASE) && *prop->child) {
1514 object_unref(*prop->child);
1515 }
1516 g_free(prop);
1517}
1518
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001519void object_property_add_link(Object *obj, const char *name,
1520 const char *type, Object **child,
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001521 void (*check)(Object *, const char *,
1522 Object *, Error **),
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001523 ObjectPropertyLinkFlags flags,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001524 Error **errp)
1525{
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001526 Error *local_err = NULL;
1527 LinkProperty *prop = g_malloc(sizeof(*prop));
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001528 gchar *full_type;
Paolo Bonzini64607d02014-06-05 13:11:51 +02001529 ObjectProperty *op;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001530
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001531 prop->child = child;
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001532 prop->check = check;
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001533 prop->flags = flags;
1534
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001535 full_type = g_strdup_printf("link<%s>", type);
1536
Paolo Bonzini64607d02014-06-05 13:11:51 +02001537 op = object_property_add(obj, name, full_type,
1538 object_get_link_property,
1539 check ? object_set_link_property : NULL,
1540 object_release_link_property,
1541 prop,
1542 &local_err);
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001543 if (local_err) {
1544 error_propagate(errp, local_err);
1545 g_free(prop);
Paolo Bonzini64607d02014-06-05 13:11:51 +02001546 goto out;
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001547 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001548
Paolo Bonzini64607d02014-06-05 13:11:51 +02001549 op->resolve = object_resolve_link_property;
1550
1551out:
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001552 g_free(full_type);
1553}
1554
Paolo Bonzinifb9e7e32015-05-05 18:29:00 +02001555void object_property_add_const_link(Object *obj, const char *name,
1556 Object *target, Error **errp)
1557{
1558 char *link_type;
1559 ObjectProperty *op;
1560
1561 link_type = g_strdup_printf("link<%s>", object_get_typename(target));
1562 op = object_property_add(obj, name, link_type,
1563 object_get_child_property, NULL,
1564 NULL, target, errp);
1565 if (op != NULL) {
1566 op->resolve = object_resolve_child_property;
1567 }
1568 g_free(link_type);
1569}
1570
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001571gchar *object_get_canonical_path_component(Object *obj)
1572{
1573 ObjectProperty *prop = NULL;
Pavel Fedinb604a852015-10-13 13:37:45 +01001574 GHashTableIter iter;
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001575
1576 g_assert(obj);
1577 g_assert(obj->parent != NULL);
1578
Pavel Fedinb604a852015-10-13 13:37:45 +01001579 g_hash_table_iter_init(&iter, obj->parent->properties);
1580 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001581 if (!object_property_is_child(prop)) {
1582 continue;
1583 }
1584
1585 if (prop->opaque == obj) {
1586 return g_strdup(prop->name);
1587 }
1588 }
1589
1590 /* obj had a parent but was not a child, should never happen */
1591 g_assert_not_reached();
1592 return NULL;
1593}
1594
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001595gchar *object_get_canonical_path(Object *obj)
1596{
1597 Object *root = object_get_root();
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001598 char *newpath, *path = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001599
1600 while (obj != root) {
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001601 char *component = object_get_canonical_path_component(obj);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001602
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001603 if (path) {
1604 newpath = g_strdup_printf("%s/%s", component, path);
1605 g_free(component);
1606 g_free(path);
1607 path = newpath;
1608 } else {
1609 path = component;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001610 }
1611
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001612 obj = obj->parent;
1613 }
1614
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001615 newpath = g_strdup_printf("/%s", path ? path : "");
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001616 g_free(path);
1617
1618 return newpath;
1619}
1620
Andreas Färber3e84b482013-01-15 02:55:10 +01001621Object *object_resolve_path_component(Object *parent, const gchar *part)
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001622{
Paolo Bonzini89bfe002012-04-12 18:00:18 +02001623 ObjectProperty *prop = object_property_find(parent, part, NULL);
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001624 if (prop == NULL) {
1625 return NULL;
1626 }
1627
Paolo Bonzini64607d02014-06-05 13:11:51 +02001628 if (prop->resolve) {
1629 return prop->resolve(parent, prop->opaque, part);
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001630 } else {
1631 return NULL;
1632 }
1633}
1634
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001635static Object *object_resolve_abs_path(Object *parent,
1636 gchar **parts,
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001637 const char *typename,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001638 int index)
1639{
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001640 Object *child;
1641
1642 if (parts[index] == NULL) {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001643 return object_dynamic_cast(parent, typename);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001644 }
1645
1646 if (strcmp(parts[index], "") == 0) {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001647 return object_resolve_abs_path(parent, parts, typename, index + 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001648 }
1649
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001650 child = object_resolve_path_component(parent, parts[index]);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001651 if (!child) {
1652 return NULL;
1653 }
1654
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001655 return object_resolve_abs_path(child, parts, typename, index + 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001656}
1657
1658static Object *object_resolve_partial_path(Object *parent,
1659 gchar **parts,
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001660 const char *typename,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001661 bool *ambiguous)
1662{
1663 Object *obj;
Pavel Fedinb604a852015-10-13 13:37:45 +01001664 GHashTableIter iter;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001665 ObjectProperty *prop;
1666
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001667 obj = object_resolve_abs_path(parent, parts, typename, 0);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001668
Pavel Fedinb604a852015-10-13 13:37:45 +01001669 g_hash_table_iter_init(&iter, parent->properties);
1670 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001671 Object *found;
1672
Andreas Färber5d9d3f42012-05-27 00:32:40 +02001673 if (!object_property_is_child(prop)) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001674 continue;
1675 }
1676
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001677 found = object_resolve_partial_path(prop->opaque, parts,
1678 typename, ambiguous);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001679 if (found) {
1680 if (obj) {
1681 if (ambiguous) {
1682 *ambiguous = true;
1683 }
1684 return NULL;
1685 }
1686 obj = found;
1687 }
1688
1689 if (ambiguous && *ambiguous) {
1690 return NULL;
1691 }
1692 }
1693
1694 return obj;
1695}
1696
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001697Object *object_resolve_path_type(const char *path, const char *typename,
1698 bool *ambiguous)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001699{
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001700 Object *obj;
1701 gchar **parts;
1702
1703 parts = g_strsplit(path, "/", 0);
Paolo Bonzini2e1103f2013-04-18 18:44:02 +02001704 assert(parts);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001705
Paolo Bonzini2e1103f2013-04-18 18:44:02 +02001706 if (parts[0] == NULL || strcmp(parts[0], "") != 0) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001707 if (ambiguous) {
1708 *ambiguous = false;
1709 }
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001710 obj = object_resolve_partial_path(object_get_root(), parts,
1711 typename, ambiguous);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001712 } else {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001713 obj = object_resolve_abs_path(object_get_root(), parts, typename, 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001714 }
1715
1716 g_strfreev(parts);
1717
1718 return obj;
1719}
1720
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001721Object *object_resolve_path(const char *path, bool *ambiguous)
1722{
1723 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
1724}
1725
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001726typedef struct StringProperty
1727{
1728 char *(*get)(Object *, Error **);
1729 void (*set)(Object *, const char *, Error **);
1730} StringProperty;
1731
Eric Blaked7bce992016-01-29 06:48:55 -07001732static void property_get_str(Object *obj, Visitor *v, const char *name,
1733 void *opaque, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001734{
1735 StringProperty *prop = opaque;
1736 char *value;
Markus Armbrustere1c82372015-08-25 20:00:46 +02001737 Error *err = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001738
Markus Armbrustere1c82372015-08-25 20:00:46 +02001739 value = prop->get(obj, &err);
1740 if (err) {
1741 error_propagate(errp, err);
1742 return;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001743 }
Markus Armbrustere1c82372015-08-25 20:00:46 +02001744
Eric Blake51e72bc2016-01-29 06:48:54 -07001745 visit_type_str(v, name, &value, errp);
Markus Armbrustere1c82372015-08-25 20:00:46 +02001746 g_free(value);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001747}
1748
Eric Blaked7bce992016-01-29 06:48:55 -07001749static void property_set_str(Object *obj, Visitor *v, const char *name,
1750 void *opaque, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001751{
1752 StringProperty *prop = opaque;
1753 char *value;
1754 Error *local_err = NULL;
1755
Eric Blake51e72bc2016-01-29 06:48:54 -07001756 visit_type_str(v, name, &value, &local_err);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001757 if (local_err) {
1758 error_propagate(errp, local_err);
1759 return;
1760 }
1761
1762 prop->set(obj, value, errp);
1763 g_free(value);
1764}
1765
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001766static void property_release_str(Object *obj, const char *name,
1767 void *opaque)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001768{
1769 StringProperty *prop = opaque;
1770 g_free(prop);
1771}
1772
1773void object_property_add_str(Object *obj, const char *name,
1774 char *(*get)(Object *, Error **),
1775 void (*set)(Object *, const char *, Error **),
1776 Error **errp)
1777{
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01001778 Error *local_err = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001779 StringProperty *prop = g_malloc0(sizeof(*prop));
1780
1781 prop->get = get;
1782 prop->set = set;
1783
1784 object_property_add(obj, name, "string",
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001785 get ? property_get_str : NULL,
1786 set ? property_set_str : NULL,
1787 property_release_str,
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01001788 prop, &local_err);
1789 if (local_err) {
1790 error_propagate(errp, local_err);
1791 g_free(prop);
1792 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001793}
Paolo Bonzini745549c2012-03-31 16:45:54 +02001794
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001795void object_class_property_add_str(ObjectClass *klass, const char *name,
1796 char *(*get)(Object *, Error **),
1797 void (*set)(Object *, const char *,
1798 Error **),
1799 Error **errp)
1800{
1801 Error *local_err = NULL;
1802 StringProperty *prop = g_malloc0(sizeof(*prop));
1803
1804 prop->get = get;
1805 prop->set = set;
1806
1807 object_class_property_add(klass, name, "string",
1808 get ? property_get_str : NULL,
1809 set ? property_set_str : NULL,
1810 property_release_str,
1811 prop, &local_err);
1812 if (local_err) {
1813 error_propagate(errp, local_err);
1814 g_free(prop);
1815 }
1816}
1817
Anthony Liguori0e558842012-06-25 10:32:46 -05001818typedef struct BoolProperty
1819{
1820 bool (*get)(Object *, Error **);
1821 void (*set)(Object *, bool, Error **);
1822} BoolProperty;
1823
Eric Blaked7bce992016-01-29 06:48:55 -07001824static void property_get_bool(Object *obj, Visitor *v, const char *name,
1825 void *opaque, Error **errp)
Anthony Liguori0e558842012-06-25 10:32:46 -05001826{
1827 BoolProperty *prop = opaque;
1828 bool value;
Markus Armbruster4715d422015-08-25 20:00:45 +02001829 Error *err = NULL;
Anthony Liguori0e558842012-06-25 10:32:46 -05001830
Markus Armbruster4715d422015-08-25 20:00:45 +02001831 value = prop->get(obj, &err);
1832 if (err) {
1833 error_propagate(errp, err);
1834 return;
1835 }
1836
Eric Blake51e72bc2016-01-29 06:48:54 -07001837 visit_type_bool(v, name, &value, errp);
Anthony Liguori0e558842012-06-25 10:32:46 -05001838}
1839
Eric Blaked7bce992016-01-29 06:48:55 -07001840static void property_set_bool(Object *obj, Visitor *v, const char *name,
1841 void *opaque, Error **errp)
Anthony Liguori0e558842012-06-25 10:32:46 -05001842{
1843 BoolProperty *prop = opaque;
1844 bool value;
1845 Error *local_err = NULL;
1846
Eric Blake51e72bc2016-01-29 06:48:54 -07001847 visit_type_bool(v, name, &value, &local_err);
Anthony Liguori0e558842012-06-25 10:32:46 -05001848 if (local_err) {
1849 error_propagate(errp, local_err);
1850 return;
1851 }
1852
1853 prop->set(obj, value, errp);
1854}
1855
1856static void property_release_bool(Object *obj, const char *name,
1857 void *opaque)
1858{
1859 BoolProperty *prop = opaque;
1860 g_free(prop);
1861}
1862
1863void object_property_add_bool(Object *obj, const char *name,
1864 bool (*get)(Object *, Error **),
1865 void (*set)(Object *, bool, Error **),
1866 Error **errp)
1867{
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01001868 Error *local_err = NULL;
Anthony Liguori0e558842012-06-25 10:32:46 -05001869 BoolProperty *prop = g_malloc0(sizeof(*prop));
1870
1871 prop->get = get;
1872 prop->set = set;
1873
1874 object_property_add(obj, name, "bool",
1875 get ? property_get_bool : NULL,
1876 set ? property_set_bool : NULL,
1877 property_release_bool,
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01001878 prop, &local_err);
1879 if (local_err) {
1880 error_propagate(errp, local_err);
1881 g_free(prop);
1882 }
Anthony Liguori0e558842012-06-25 10:32:46 -05001883}
1884
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001885void object_class_property_add_bool(ObjectClass *klass, const char *name,
1886 bool (*get)(Object *, Error **),
1887 void (*set)(Object *, bool, Error **),
1888 Error **errp)
1889{
1890 Error *local_err = NULL;
1891 BoolProperty *prop = g_malloc0(sizeof(*prop));
1892
1893 prop->get = get;
1894 prop->set = set;
1895
1896 object_class_property_add(klass, name, "bool",
1897 get ? property_get_bool : NULL,
1898 set ? property_set_bool : NULL,
1899 property_release_bool,
1900 prop, &local_err);
1901 if (local_err) {
1902 error_propagate(errp, local_err);
1903 g_free(prop);
1904 }
1905}
1906
Eric Blaked7bce992016-01-29 06:48:55 -07001907static void property_get_enum(Object *obj, Visitor *v, const char *name,
1908 void *opaque, Error **errp)
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01001909{
1910 EnumProperty *prop = opaque;
1911 int value;
Markus Armbruster4715d422015-08-25 20:00:45 +02001912 Error *err = NULL;
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01001913
Markus Armbruster4715d422015-08-25 20:00:45 +02001914 value = prop->get(obj, &err);
1915 if (err) {
1916 error_propagate(errp, err);
1917 return;
1918 }
1919
Eric Blake337283d2016-01-29 06:48:57 -07001920 visit_type_enum(v, name, &value, prop->strings, errp);
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01001921}
1922
Eric Blaked7bce992016-01-29 06:48:55 -07001923static void property_set_enum(Object *obj, Visitor *v, const char *name,
1924 void *opaque, Error **errp)
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01001925{
1926 EnumProperty *prop = opaque;
1927 int value;
Markus Armbruster4715d422015-08-25 20:00:45 +02001928 Error *err = NULL;
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01001929
Eric Blake337283d2016-01-29 06:48:57 -07001930 visit_type_enum(v, name, &value, prop->strings, &err);
Markus Armbruster4715d422015-08-25 20:00:45 +02001931 if (err) {
1932 error_propagate(errp, err);
1933 return;
1934 }
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01001935 prop->set(obj, value, errp);
1936}
1937
1938static void property_release_enum(Object *obj, const char *name,
1939 void *opaque)
1940{
1941 EnumProperty *prop = opaque;
1942 g_free(prop);
1943}
1944
1945void object_property_add_enum(Object *obj, const char *name,
1946 const char *typename,
1947 const char * const *strings,
1948 int (*get)(Object *, Error **),
1949 void (*set)(Object *, int, Error **),
1950 Error **errp)
1951{
1952 Error *local_err = NULL;
1953 EnumProperty *prop = g_malloc(sizeof(*prop));
1954
1955 prop->strings = strings;
1956 prop->get = get;
1957 prop->set = set;
1958
1959 object_property_add(obj, name, typename,
1960 get ? property_get_enum : NULL,
1961 set ? property_set_enum : NULL,
1962 property_release_enum,
1963 prop, &local_err);
1964 if (local_err) {
1965 error_propagate(errp, local_err);
1966 g_free(prop);
1967 }
1968}
1969
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001970void object_class_property_add_enum(ObjectClass *klass, const char *name,
1971 const char *typename,
1972 const char * const *strings,
1973 int (*get)(Object *, Error **),
1974 void (*set)(Object *, int, Error **),
1975 Error **errp)
1976{
1977 Error *local_err = NULL;
1978 EnumProperty *prop = g_malloc(sizeof(*prop));
1979
1980 prop->strings = strings;
1981 prop->get = get;
1982 prop->set = set;
1983
1984 object_class_property_add(klass, name, typename,
1985 get ? property_get_enum : NULL,
1986 set ? property_set_enum : NULL,
1987 property_release_enum,
1988 prop, &local_err);
1989 if (local_err) {
1990 error_propagate(errp, local_err);
1991 g_free(prop);
1992 }
1993}
1994
David Gibson8e099d12015-02-06 14:55:45 +11001995typedef struct TMProperty {
1996 void (*get)(Object *, struct tm *, Error **);
1997} TMProperty;
1998
Eric Blaked7bce992016-01-29 06:48:55 -07001999static void property_get_tm(Object *obj, Visitor *v, const char *name,
2000 void *opaque, Error **errp)
David Gibson8e099d12015-02-06 14:55:45 +11002001{
2002 TMProperty *prop = opaque;
2003 Error *err = NULL;
2004 struct tm value;
2005
2006 prop->get(obj, &value, &err);
2007 if (err) {
2008 goto out;
2009 }
2010
Eric Blake337283d2016-01-29 06:48:57 -07002011 visit_start_struct(v, name, NULL, 0, &err);
David Gibson8e099d12015-02-06 14:55:45 +11002012 if (err) {
2013 goto out;
2014 }
Eric Blake51e72bc2016-01-29 06:48:54 -07002015 visit_type_int32(v, "tm_year", &value.tm_year, &err);
David Gibson8e099d12015-02-06 14:55:45 +11002016 if (err) {
2017 goto out_end;
2018 }
Eric Blake51e72bc2016-01-29 06:48:54 -07002019 visit_type_int32(v, "tm_mon", &value.tm_mon, &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_mday", &value.tm_mday, &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_hour", &value.tm_hour, &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_min", &value.tm_min, &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_sec", &value.tm_sec, &err);
David Gibson8e099d12015-02-06 14:55:45 +11002036 if (err) {
2037 goto out_end;
2038 }
Eric Blake15c2f662016-04-28 15:45:27 -06002039 visit_check_struct(v, &err);
David Gibson8e099d12015-02-06 14:55:45 +11002040out_end:
Eric Blake15c2f662016-04-28 15:45:27 -06002041 visit_end_struct(v);
David Gibson8e099d12015-02-06 14:55:45 +11002042out:
2043 error_propagate(errp, err);
2044
2045}
2046
2047static void property_release_tm(Object *obj, const char *name,
2048 void *opaque)
2049{
2050 TMProperty *prop = opaque;
2051 g_free(prop);
2052}
2053
2054void object_property_add_tm(Object *obj, const char *name,
2055 void (*get)(Object *, struct tm *, Error **),
2056 Error **errp)
2057{
2058 Error *local_err = NULL;
2059 TMProperty *prop = g_malloc0(sizeof(*prop));
2060
2061 prop->get = get;
2062
2063 object_property_add(obj, name, "struct tm",
2064 get ? property_get_tm : NULL, NULL,
2065 property_release_tm,
2066 prop, &local_err);
2067 if (local_err) {
2068 error_propagate(errp, local_err);
2069 g_free(prop);
2070 }
2071}
2072
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002073void object_class_property_add_tm(ObjectClass *klass, const char *name,
2074 void (*get)(Object *, struct tm *, Error **),
2075 Error **errp)
2076{
2077 Error *local_err = NULL;
2078 TMProperty *prop = g_malloc0(sizeof(*prop));
2079
2080 prop->get = get;
2081
2082 object_class_property_add(klass, name, "struct tm",
2083 get ? property_get_tm : NULL, NULL,
2084 property_release_tm,
2085 prop, &local_err);
2086 if (local_err) {
2087 error_propagate(errp, local_err);
2088 g_free(prop);
2089 }
2090}
2091
Paolo Bonzini2f262e02012-04-02 17:33:51 +02002092static char *qdev_get_type(Object *obj, Error **errp)
2093{
2094 return g_strdup(object_get_typename(obj));
2095}
2096
Eric Blaked7bce992016-01-29 06:48:55 -07002097static void property_get_uint8_ptr(Object *obj, Visitor *v, const char *name,
2098 void *opaque, Error **errp)
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002099{
2100 uint8_t value = *(uint8_t *)opaque;
Eric Blake51e72bc2016-01-29 06:48:54 -07002101 visit_type_uint8(v, name, &value, errp);
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002102}
2103
Eric Blaked7bce992016-01-29 06:48:55 -07002104static void property_get_uint16_ptr(Object *obj, Visitor *v, const char *name,
2105 void *opaque, Error **errp)
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002106{
2107 uint16_t value = *(uint16_t *)opaque;
Eric Blake51e72bc2016-01-29 06:48:54 -07002108 visit_type_uint16(v, name, &value, errp);
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002109}
2110
Eric Blaked7bce992016-01-29 06:48:55 -07002111static void property_get_uint32_ptr(Object *obj, Visitor *v, const char *name,
2112 void *opaque, Error **errp)
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002113{
2114 uint32_t value = *(uint32_t *)opaque;
Eric Blake51e72bc2016-01-29 06:48:54 -07002115 visit_type_uint32(v, name, &value, errp);
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002116}
2117
Eric Blaked7bce992016-01-29 06:48:55 -07002118static void property_get_uint64_ptr(Object *obj, Visitor *v, const char *name,
2119 void *opaque, Error **errp)
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002120{
2121 uint64_t value = *(uint64_t *)opaque;
Eric Blake51e72bc2016-01-29 06:48:54 -07002122 visit_type_uint64(v, name, &value, errp);
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002123}
2124
2125void object_property_add_uint8_ptr(Object *obj, const char *name,
2126 const uint8_t *v, Error **errp)
2127{
2128 object_property_add(obj, name, "uint8", property_get_uint8_ptr,
2129 NULL, NULL, (void *)v, errp);
2130}
2131
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002132void object_class_property_add_uint8_ptr(ObjectClass *klass, const char *name,
2133 const uint8_t *v, Error **errp)
2134{
2135 object_class_property_add(klass, name, "uint8", property_get_uint8_ptr,
2136 NULL, NULL, (void *)v, errp);
2137}
2138
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002139void object_property_add_uint16_ptr(Object *obj, const char *name,
2140 const uint16_t *v, Error **errp)
2141{
2142 object_property_add(obj, name, "uint16", property_get_uint16_ptr,
2143 NULL, NULL, (void *)v, errp);
2144}
2145
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002146void object_class_property_add_uint16_ptr(ObjectClass *klass, const char *name,
2147 const uint16_t *v, Error **errp)
2148{
2149 object_class_property_add(klass, name, "uint16", property_get_uint16_ptr,
2150 NULL, NULL, (void *)v, errp);
2151}
2152
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002153void object_property_add_uint32_ptr(Object *obj, const char *name,
2154 const uint32_t *v, Error **errp)
2155{
2156 object_property_add(obj, name, "uint32", property_get_uint32_ptr,
2157 NULL, NULL, (void *)v, errp);
2158}
2159
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002160void object_class_property_add_uint32_ptr(ObjectClass *klass, const char *name,
2161 const uint32_t *v, Error **errp)
2162{
2163 object_class_property_add(klass, name, "uint32", property_get_uint32_ptr,
2164 NULL, NULL, (void *)v, errp);
2165}
2166
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002167void object_property_add_uint64_ptr(Object *obj, const char *name,
2168 const uint64_t *v, Error **errp)
2169{
2170 object_property_add(obj, name, "uint64", property_get_uint64_ptr,
2171 NULL, NULL, (void *)v, errp);
2172}
2173
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002174void object_class_property_add_uint64_ptr(ObjectClass *klass, const char *name,
2175 const uint64_t *v, Error **errp)
2176{
2177 object_class_property_add(klass, name, "uint64", property_get_uint64_ptr,
2178 NULL, NULL, (void *)v, errp);
2179}
2180
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002181typedef struct {
2182 Object *target_obj;
Eduardo Habkost1590d262015-04-09 16:57:29 -03002183 char *target_name;
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002184} AliasProperty;
2185
Eric Blaked7bce992016-01-29 06:48:55 -07002186static void property_get_alias(Object *obj, Visitor *v, const char *name,
2187 void *opaque, Error **errp)
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002188{
2189 AliasProperty *prop = opaque;
2190
2191 object_property_get(prop->target_obj, v, prop->target_name, errp);
2192}
2193
Eric Blaked7bce992016-01-29 06:48:55 -07002194static void property_set_alias(Object *obj, Visitor *v, const char *name,
2195 void *opaque, Error **errp)
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002196{
2197 AliasProperty *prop = opaque;
2198
2199 object_property_set(prop->target_obj, v, prop->target_name, errp);
2200}
2201
Paolo Bonzini64607d02014-06-05 13:11:51 +02002202static Object *property_resolve_alias(Object *obj, void *opaque,
2203 const gchar *part)
2204{
2205 AliasProperty *prop = opaque;
2206
2207 return object_resolve_path_component(prop->target_obj, prop->target_name);
2208}
2209
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002210static void property_release_alias(Object *obj, const char *name, void *opaque)
2211{
2212 AliasProperty *prop = opaque;
2213
Eduardo Habkost1590d262015-04-09 16:57:29 -03002214 g_free(prop->target_name);
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002215 g_free(prop);
2216}
2217
2218void object_property_add_alias(Object *obj, const char *name,
2219 Object *target_obj, const char *target_name,
2220 Error **errp)
2221{
2222 AliasProperty *prop;
Paolo Bonzini64607d02014-06-05 13:11:51 +02002223 ObjectProperty *op;
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002224 ObjectProperty *target_prop;
Paolo Bonzinid1906982014-06-10 11:17:35 +02002225 gchar *prop_type;
Gonglei8ae9a9e2014-09-27 13:13:56 +08002226 Error *local_err = NULL;
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002227
2228 target_prop = object_property_find(target_obj, target_name, errp);
2229 if (!target_prop) {
2230 return;
2231 }
2232
Paolo Bonzinid1906982014-06-10 11:17:35 +02002233 if (object_property_is_child(target_prop)) {
2234 prop_type = g_strdup_printf("link%s",
2235 target_prop->type + strlen("child"));
2236 } else {
2237 prop_type = g_strdup(target_prop->type);
2238 }
2239
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002240 prop = g_malloc(sizeof(*prop));
2241 prop->target_obj = target_obj;
Eduardo Habkost1590d262015-04-09 16:57:29 -03002242 prop->target_name = g_strdup(target_name);
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002243
Paolo Bonzinid1906982014-06-10 11:17:35 +02002244 op = object_property_add(obj, name, prop_type,
Paolo Bonzini64607d02014-06-05 13:11:51 +02002245 property_get_alias,
2246 property_set_alias,
2247 property_release_alias,
Gonglei8ae9a9e2014-09-27 13:13:56 +08002248 prop, &local_err);
2249 if (local_err) {
2250 error_propagate(errp, local_err);
2251 g_free(prop);
2252 goto out;
2253 }
Paolo Bonzini64607d02014-06-05 13:11:51 +02002254 op->resolve = property_resolve_alias;
Paolo Bonzinid1906982014-06-10 11:17:35 +02002255
Andreas Färbera18bb412015-03-27 17:34:10 +01002256 object_property_set_description(obj, op->name,
Gonglei80742642014-10-07 14:33:21 +08002257 target_prop->description,
2258 &error_abort);
2259
Gonglei8ae9a9e2014-09-27 13:13:56 +08002260out:
Paolo Bonzinid1906982014-06-10 11:17:35 +02002261 g_free(prop_type);
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002262}
2263
Gonglei80742642014-10-07 14:33:21 +08002264void object_property_set_description(Object *obj, const char *name,
2265 const char *description, Error **errp)
2266{
2267 ObjectProperty *op;
2268
2269 op = object_property_find(obj, name, errp);
2270 if (!op) {
2271 return;
2272 }
2273
2274 g_free(op->description);
2275 op->description = g_strdup(description);
2276}
2277
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002278void object_class_property_set_description(ObjectClass *klass,
2279 const char *name,
2280 const char *description,
2281 Error **errp)
2282{
2283 ObjectProperty *op;
2284
2285 op = g_hash_table_lookup(klass->properties, name);
2286 if (!op) {
2287 error_setg(errp, "Property '.%s' not found", name);
2288 return;
2289 }
2290
2291 g_free(op->description);
2292 op->description = g_strdup(description);
2293}
2294
Paolo Bonzini2f262e02012-04-02 17:33:51 +02002295static void object_instance_init(Object *obj)
2296{
2297 object_property_add_str(obj, "type", qdev_get_type, NULL, NULL);
2298}
2299
Paolo Bonzini745549c2012-03-31 16:45:54 +02002300static void register_types(void)
2301{
2302 static TypeInfo interface_info = {
2303 .name = TYPE_INTERFACE,
Anthony Liguori33e95c62012-08-10 13:16:10 +10002304 .class_size = sizeof(InterfaceClass),
Paolo Bonzini745549c2012-03-31 16:45:54 +02002305 .abstract = true,
2306 };
2307
2308 static TypeInfo object_info = {
2309 .name = TYPE_OBJECT,
2310 .instance_size = sizeof(Object),
Paolo Bonzini2f262e02012-04-02 17:33:51 +02002311 .instance_init = object_instance_init,
Paolo Bonzini745549c2012-03-31 16:45:54 +02002312 .abstract = true,
2313 };
2314
Paolo Bonzini049cb3c2012-04-04 15:58:40 +02002315 type_interface = type_register_internal(&interface_info);
2316 type_register_internal(&object_info);
Paolo Bonzini745549c2012-03-31 16:45:54 +02002317}
2318
2319type_init(register_types)