blob: eb4bc924ffa8ce73fc659b7a599e01edf4706f8e [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);
Eduardo Habkost1c6d75d2016-12-12 16:31:01 -0200275 /* Any type with zero instance_size is implicitly abstract.
276 * This means interface types are all abstract.
277 */
278 if (ti->instance_size == 0) {
279 ti->abstract = true;
280 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600281
282 ti->class = g_malloc0(ti->class_size);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600283
Paolo Bonzini745549c2012-03-31 16:45:54 +0200284 parent = type_get_parent(ti);
285 if (parent) {
Igor Mitsyankoac451032012-02-28 15:57:11 +0400286 type_initialize(parent);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000287 GSList *e;
288 int i;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600289
Andreas Färber8438a132015-11-16 17:49:20 +0100290 g_assert_cmpint(parent->class_size, <=, ti->class_size);
Paolo Bonzini745549c2012-03-31 16:45:54 +0200291 memcpy(ti->class, parent->class, parent->class_size);
Peter Crosthwaite3e407de2013-02-19 14:02:09 +1000292 ti->class->interfaces = NULL;
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +0100293 ti->class->properties = g_hash_table_new_full(
294 g_str_hash, g_str_equal, g_free, object_property_free);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000295
296 for (e = parent->class->interfaces; e; e = e->next) {
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100297 InterfaceClass *iface = e->data;
298 ObjectClass *klass = OBJECT_CLASS(iface);
299
300 type_initialize_interface(ti, iface->interface_type, klass->type);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000301 }
302
303 for (i = 0; i < ti->num_interfaces; i++) {
304 TypeImpl *t = type_get_by_name(ti->interfaces[i].typename);
305 for (e = ti->class->interfaces; e; e = e->next) {
306 TypeImpl *target_type = OBJECT_CLASS(e->data)->type;
307
308 if (type_is_ancestor(target_type, t)) {
309 break;
310 }
311 }
312
313 if (e) {
314 continue;
315 }
316
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100317 type_initialize_interface(ti, t, t);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000318 }
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +0100319 } else {
320 ti->class->properties = g_hash_table_new_full(
321 g_str_hash, g_str_equal, g_free, object_property_free);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600322 }
323
Paolo Bonzini745549c2012-03-31 16:45:54 +0200324 ti->class->type = ti;
325
326 while (parent) {
327 if (parent->class_base_init) {
328 parent->class_base_init(ti->class, ti->class_data);
329 }
330 parent = type_get_parent(parent);
331 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600332
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600333 if (ti->class_init) {
334 ti->class_init(ti->class, ti->class_data);
335 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600336}
337
338static void object_init_with_type(Object *obj, TypeImpl *ti)
339{
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600340 if (type_has_parent(ti)) {
341 object_init_with_type(obj, type_get_parent(ti));
342 }
343
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600344 if (ti->instance_init) {
345 ti->instance_init(obj);
346 }
347}
348
Eduardo Habkost8231c2d2013-07-10 17:08:41 -0300349static void object_post_init_with_type(Object *obj, TypeImpl *ti)
350{
351 if (ti->instance_post_init) {
352 ti->instance_post_init(obj);
353 }
354
355 if (type_has_parent(ti)) {
356 object_post_init_with_type(obj, type_get_parent(ti));
357 }
358}
359
Marc-André Lureau63f7b102016-12-12 20:31:51 +0300360static void object_initialize_with_type(void *data, size_t size, TypeImpl *type)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600361{
362 Object *obj = data;
363
364 g_assert(type != NULL);
Igor Mitsyankoac451032012-02-28 15:57:11 +0400365 type_initialize(type);
Igor Mitsyankoaca59af2012-02-28 15:57:10 +0400366
Andreas Färber8438a132015-11-16 17:49:20 +0100367 g_assert_cmpint(type->instance_size, >=, sizeof(Object));
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600368 g_assert(type->abstract == false);
Andreas Färber8438a132015-11-16 17:49:20 +0100369 g_assert_cmpint(size, >=, type->instance_size);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600370
371 memset(obj, 0, type->instance_size);
372 obj->class = type->class;
Paolo Bonzini764b6312012-11-23 09:47:12 +0100373 object_ref(obj);
Pavel Fedinb604a852015-10-13 13:37:45 +0100374 obj->properties = g_hash_table_new_full(g_str_hash, g_str_equal,
375 NULL, object_property_free);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600376 object_init_with_type(obj, type);
Eduardo Habkost8231c2d2013-07-10 17:08:41 -0300377 object_post_init_with_type(obj, type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600378}
379
Andreas Färber213f0c42013-08-23 19:37:12 +0200380void object_initialize(void *data, size_t size, const char *typename)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600381{
382 TypeImpl *type = type_get_by_name(typename);
383
Andreas Färber5b9237f2013-08-30 18:28:37 +0200384 object_initialize_with_type(data, size, type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600385}
386
Andreas Färber5d9d3f42012-05-27 00:32:40 +0200387static inline bool object_property_is_child(ObjectProperty *prop)
388{
389 return strstart(prop->type, "child<", NULL);
390}
391
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600392static void object_property_del_all(Object *obj)
393{
Pavel Fedinb604a852015-10-13 13:37:45 +0100394 ObjectProperty *prop;
395 GHashTableIter iter;
396 gpointer key, value;
397 bool released;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600398
Pavel Fedinb604a852015-10-13 13:37:45 +0100399 do {
400 released = false;
401 g_hash_table_iter_init(&iter, obj->properties);
402 while (g_hash_table_iter_next(&iter, &key, &value)) {
403 prop = value;
404 if (prop->release) {
405 prop->release(obj, prop->name, prop->opaque);
406 prop->release = NULL;
407 released = true;
408 break;
409 }
410 g_hash_table_iter_remove(&iter);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600411 }
Pavel Fedinb604a852015-10-13 13:37:45 +0100412 } while (released);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600413
Pavel Fedinb604a852015-10-13 13:37:45 +0100414 g_hash_table_unref(obj->properties);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600415}
416
417static void object_property_del_child(Object *obj, Object *child, Error **errp)
418{
419 ObjectProperty *prop;
Pavel Fedinb604a852015-10-13 13:37:45 +0100420 GHashTableIter iter;
421 gpointer key, value;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600422
Pavel Fedinb604a852015-10-13 13:37:45 +0100423 g_hash_table_iter_init(&iter, obj->properties);
424 while (g_hash_table_iter_next(&iter, &key, &value)) {
425 prop = value;
Andreas Färber5d9d3f42012-05-27 00:32:40 +0200426 if (object_property_is_child(prop) && prop->opaque == child) {
Pavel Fedinb604a852015-10-13 13:37:45 +0100427 if (prop->release) {
428 prop->release(obj, prop->name, prop->opaque);
429 prop->release = NULL;
430 }
431 break;
432 }
433 }
434 g_hash_table_iter_init(&iter, obj->properties);
435 while (g_hash_table_iter_next(&iter, &key, &value)) {
436 prop = value;
437 if (object_property_is_child(prop) && prop->opaque == child) {
438 g_hash_table_iter_remove(&iter);
Paolo Bonzini6c1fdcf2012-02-28 09:54:15 +0100439 break;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600440 }
441 }
442}
443
444void object_unparent(Object *obj)
445{
Michael S. Tsirkine998fa82013-03-18 21:01:37 +0200446 if (obj->parent) {
447 object_property_del_child(obj->parent, obj, NULL);
448 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600449}
450
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600451static void object_deinit(Object *obj, TypeImpl *type)
452{
453 if (type->instance_finalize) {
454 type->instance_finalize(obj);
455 }
456
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600457 if (type_has_parent(type)) {
458 object_deinit(obj, type_get_parent(type));
459 }
460}
461
Paolo Bonzini339c2702012-11-23 09:47:16 +0100462static void object_finalize(void *data)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600463{
464 Object *obj = data;
465 TypeImpl *ti = obj->class->type;
466
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600467 object_property_del_all(obj);
Paolo Bonzini76a6e1c2014-06-11 11:58:30 +0200468 object_deinit(obj, ti);
Anthony Liguoridb85b572011-12-23 08:47:39 -0600469
Andreas Färber8438a132015-11-16 17:49:20 +0100470 g_assert_cmpint(obj->ref, ==, 0);
Paolo Bonzinifde9bf42012-11-23 09:47:14 +0100471 if (obj->free) {
472 obj->free(obj);
473 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600474}
475
Marc-André Lureau63f7b102016-12-12 20:31:51 +0300476static Object *object_new_with_type(Type type)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600477{
478 Object *obj;
479
480 g_assert(type != NULL);
Igor Mitsyankoac451032012-02-28 15:57:11 +0400481 type_initialize(type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600482
483 obj = g_malloc(type->instance_size);
Andreas Färber5b9237f2013-08-30 18:28:37 +0200484 object_initialize_with_type(obj, type->instance_size, type);
Paolo Bonzinifde9bf42012-11-23 09:47:14 +0100485 obj->free = g_free;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600486
487 return obj;
488}
489
490Object *object_new(const char *typename)
491{
492 TypeImpl *ti = type_get_by_name(typename);
493
494 return object_new_with_type(ti);
495}
496
Daniel P. Berrangea31bdae2015-05-13 17:14:06 +0100497
498Object *object_new_with_props(const char *typename,
499 Object *parent,
500 const char *id,
501 Error **errp,
502 ...)
503{
504 va_list vargs;
505 Object *obj;
506
507 va_start(vargs, errp);
508 obj = object_new_with_propv(typename, parent, id, errp, vargs);
509 va_end(vargs);
510
511 return obj;
512}
513
514
515Object *object_new_with_propv(const char *typename,
516 Object *parent,
517 const char *id,
518 Error **errp,
519 va_list vargs)
520{
521 Object *obj;
522 ObjectClass *klass;
523 Error *local_err = NULL;
524
525 klass = object_class_by_name(typename);
526 if (!klass) {
527 error_setg(errp, "invalid object type: %s", typename);
528 return NULL;
529 }
530
531 if (object_class_is_abstract(klass)) {
532 error_setg(errp, "object type '%s' is abstract", typename);
533 return NULL;
534 }
535 obj = object_new(typename);
536
537 if (object_set_propv(obj, &local_err, vargs) < 0) {
538 goto error;
539 }
540
541 object_property_add_child(parent, id, obj, &local_err);
542 if (local_err) {
543 goto error;
544 }
545
546 if (object_dynamic_cast(obj, TYPE_USER_CREATABLE)) {
547 user_creatable_complete(obj, &local_err);
548 if (local_err) {
549 object_unparent(obj);
550 goto error;
551 }
552 }
553
554 object_unref(OBJECT(obj));
555 return obj;
556
557 error:
Eduardo Habkost621ff942016-06-13 18:57:56 -0300558 error_propagate(errp, local_err);
Daniel P. Berrangea31bdae2015-05-13 17:14:06 +0100559 object_unref(obj);
560 return NULL;
561}
562
563
564int object_set_props(Object *obj,
565 Error **errp,
566 ...)
567{
568 va_list vargs;
569 int ret;
570
571 va_start(vargs, errp);
572 ret = object_set_propv(obj, errp, vargs);
573 va_end(vargs);
574
575 return ret;
576}
577
578
579int object_set_propv(Object *obj,
580 Error **errp,
581 va_list vargs)
582{
583 const char *propname;
584 Error *local_err = NULL;
585
586 propname = va_arg(vargs, char *);
587 while (propname != NULL) {
588 const char *value = va_arg(vargs, char *);
589
590 g_assert(value != NULL);
591 object_property_parse(obj, value, propname, &local_err);
592 if (local_err) {
593 error_propagate(errp, local_err);
594 return -1;
595 }
596 propname = va_arg(vargs, char *);
597 }
598
599 return 0;
600}
601
602
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600603Object *object_dynamic_cast(Object *obj, const char *typename)
604{
Paolo Bonzinib7f43fe2012-11-23 16:56:17 +0100605 if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) {
Paolo Bonziniacc4af32012-02-03 11:57:23 +0100606 return obj;
607 }
608
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600609 return NULL;
610}
611
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200612Object *object_dynamic_cast_assert(Object *obj, const char *typename,
613 const char *file, int line, const char *func)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600614{
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200615 trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)",
616 typename, file, line, func);
617
Paolo Bonzini3556c232013-05-10 14:16:40 +0200618#ifdef CONFIG_QOM_CAST_DEBUG
Anthony Liguori03587322013-05-13 15:22:24 -0500619 int i;
620 Object *inst;
621
Peter Crosthwaite95916ab2013-05-22 11:19:16 +1000622 for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) {
Alex Bennéeb6b3ccf2016-09-30 22:30:57 +0100623 if (atomic_read(&obj->class->object_cast_cache[i]) == typename) {
Anthony Liguori03587322013-05-13 15:22:24 -0500624 goto out;
625 }
626 }
627
628 inst = object_dynamic_cast(obj, typename);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600629
Paolo Bonzinib7f43fe2012-11-23 16:56:17 +0100630 if (!inst && obj) {
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200631 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
632 file, line, func, obj, typename);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600633 abort();
634 }
635
Paolo Bonzini3556c232013-05-10 14:16:40 +0200636 assert(obj == inst);
Anthony Liguori03587322013-05-13 15:22:24 -0500637
Peter Crosthwaite95916ab2013-05-22 11:19:16 +1000638 if (obj && obj == inst) {
Anthony Liguori03587322013-05-13 15:22:24 -0500639 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
Alex Bennéeb6b3ccf2016-09-30 22:30:57 +0100640 atomic_set(&obj->class->object_cast_cache[i - 1],
641 atomic_read(&obj->class->object_cast_cache[i]));
Anthony Liguori03587322013-05-13 15:22:24 -0500642 }
Alex Bennéeb6b3ccf2016-09-30 22:30:57 +0100643 atomic_set(&obj->class->object_cast_cache[i - 1], typename);
Anthony Liguori03587322013-05-13 15:22:24 -0500644 }
645
646out:
Paolo Bonzini3556c232013-05-10 14:16:40 +0200647#endif
648 return obj;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600649}
650
651ObjectClass *object_class_dynamic_cast(ObjectClass *class,
652 const char *typename)
653{
Anthony Liguori33e95c62012-08-10 13:16:10 +1000654 ObjectClass *ret = NULL;
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200655 TypeImpl *target_type;
656 TypeImpl *type;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600657
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200658 if (!class) {
659 return NULL;
660 }
661
Paolo Bonzini793c96b2013-05-10 14:16:37 +0200662 /* A simple fast path that can trigger a lot for leaf classes. */
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200663 type = class->type;
Paolo Bonzini793c96b2013-05-10 14:16:37 +0200664 if (type->name == typename) {
665 return class;
666 }
667
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200668 target_type = type_get_by_name(typename);
Alexander Graf9ab880b2013-04-30 15:02:16 +0200669 if (!target_type) {
670 /* target class type unknown, so fail the cast */
671 return NULL;
672 }
673
Peter Crosthwaite00e2cea2013-02-19 14:02:10 +1000674 if (type->class->interfaces &&
675 type_is_ancestor(target_type, type_interface)) {
Anthony Liguori33e95c62012-08-10 13:16:10 +1000676 int found = 0;
677 GSList *i;
678
679 for (i = class->interfaces; i; i = i->next) {
680 ObjectClass *target_class = i->data;
681
682 if (type_is_ancestor(target_class->type, target_type)) {
683 ret = target_class;
684 found++;
685 }
686 }
687
688 /* The match was ambiguous, don't allow a cast */
689 if (found > 1) {
690 ret = NULL;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600691 }
Anthony Liguori33e95c62012-08-10 13:16:10 +1000692 } else if (type_is_ancestor(type, target_type)) {
693 ret = class;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600694 }
695
Anthony Liguori33e95c62012-08-10 13:16:10 +1000696 return ret;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600697}
698
699ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200700 const char *typename,
701 const char *file, int line,
702 const char *func)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600703{
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200704 ObjectClass *ret;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600705
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200706 trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)",
707 typename, file, line, func);
708
Anthony Liguori03587322013-05-13 15:22:24 -0500709#ifdef CONFIG_QOM_CAST_DEBUG
710 int i;
711
Peter Crosthwaite9d6a3d52013-06-18 19:18:59 +1000712 for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) {
Alex Bennéeb6b3ccf2016-09-30 22:30:57 +0100713 if (atomic_read(&class->class_cast_cache[i]) == typename) {
Anthony Liguori03587322013-05-13 15:22:24 -0500714 ret = class;
715 goto out;
716 }
717 }
718#else
Peter Crosthwaite9d6a3d52013-06-18 19:18:59 +1000719 if (!class || !class->interfaces) {
Paolo Bonzini3556c232013-05-10 14:16:40 +0200720 return class;
721 }
722#endif
723
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200724 ret = object_class_dynamic_cast(class, typename);
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200725 if (!ret && class) {
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200726 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
727 file, line, func, class, typename);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600728 abort();
729 }
730
Anthony Liguori03587322013-05-13 15:22:24 -0500731#ifdef CONFIG_QOM_CAST_DEBUG
Peter Crosthwaite9d6a3d52013-06-18 19:18:59 +1000732 if (class && ret == class) {
Anthony Liguori03587322013-05-13 15:22:24 -0500733 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
Alex Bennéeb6b3ccf2016-09-30 22:30:57 +0100734 atomic_set(&class->class_cast_cache[i - 1],
735 atomic_read(&class->class_cast_cache[i]));
Anthony Liguori03587322013-05-13 15:22:24 -0500736 }
Alex Bennéeb6b3ccf2016-09-30 22:30:57 +0100737 atomic_set(&class->class_cast_cache[i - 1], typename);
Anthony Liguori03587322013-05-13 15:22:24 -0500738 }
739out:
740#endif
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600741 return ret;
742}
743
744const char *object_get_typename(Object *obj)
745{
746 return obj->class->type->name;
747}
748
749ObjectClass *object_get_class(Object *obj)
750{
751 return obj->class;
752}
753
Andreas Färber17862372013-01-23 12:20:18 +0100754bool object_class_is_abstract(ObjectClass *klass)
755{
756 return klass->type->abstract;
757}
758
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600759const char *object_class_get_name(ObjectClass *klass)
760{
761 return klass->type->name;
762}
763
764ObjectClass *object_class_by_name(const char *typename)
765{
766 TypeImpl *type = type_get_by_name(typename);
767
768 if (!type) {
769 return NULL;
770 }
771
Igor Mitsyankoac451032012-02-28 15:57:11 +0400772 type_initialize(type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600773
774 return type->class;
775}
776
Paolo Bonzinie7cce672012-05-02 13:30:54 +0200777ObjectClass *object_class_get_parent(ObjectClass *class)
778{
779 TypeImpl *type = type_get_parent(class->type);
780
781 if (!type) {
782 return NULL;
783 }
784
785 type_initialize(type);
786
787 return type->class;
788}
789
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600790typedef struct OCFData
791{
792 void (*fn)(ObjectClass *klass, void *opaque);
Anthony Liguori93c511a2011-12-22 14:11:53 -0600793 const char *implements_type;
794 bool include_abstract;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600795 void *opaque;
796} OCFData;
797
798static void object_class_foreach_tramp(gpointer key, gpointer value,
799 gpointer opaque)
800{
801 OCFData *data = opaque;
802 TypeImpl *type = value;
Anthony Liguori93c511a2011-12-22 14:11:53 -0600803 ObjectClass *k;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600804
Igor Mitsyankoac451032012-02-28 15:57:11 +0400805 type_initialize(type);
Anthony Liguori93c511a2011-12-22 14:11:53 -0600806 k = type->class;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600807
Anthony Liguori93c511a2011-12-22 14:11:53 -0600808 if (!data->include_abstract && type->abstract) {
809 return;
810 }
811
812 if (data->implements_type &&
813 !object_class_dynamic_cast(k, data->implements_type)) {
814 return;
815 }
816
817 data->fn(k, data->opaque);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600818}
819
820void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
Anthony Liguori93c511a2011-12-22 14:11:53 -0600821 const char *implements_type, bool include_abstract,
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600822 void *opaque)
823{
Anthony Liguori93c511a2011-12-22 14:11:53 -0600824 OCFData data = { fn, implements_type, include_abstract, opaque };
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600825
Hervé Poussineauf54c19c2013-12-03 16:42:00 +0100826 enumerating_types = true;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600827 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
Hervé Poussineauf54c19c2013-12-03 16:42:00 +0100828 enumerating_types = false;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600829}
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600830
Peter Crosthwaited714b8d2015-09-08 17:38:43 +0100831static int do_object_child_foreach(Object *obj,
832 int (*fn)(Object *child, void *opaque),
833 void *opaque, bool recurse)
Paolo Bonzini32efc532012-04-11 23:30:20 +0200834{
Pavel Fedinb604a852015-10-13 13:37:45 +0100835 GHashTableIter iter;
836 ObjectProperty *prop;
Paolo Bonzini32efc532012-04-11 23:30:20 +0200837 int ret = 0;
838
Pavel Fedinb604a852015-10-13 13:37:45 +0100839 g_hash_table_iter_init(&iter, obj->properties);
840 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
Paolo Bonzini32efc532012-04-11 23:30:20 +0200841 if (object_property_is_child(prop)) {
Peter Crosthwaited714b8d2015-09-08 17:38:43 +0100842 Object *child = prop->opaque;
843
844 ret = fn(child, opaque);
Paolo Bonzini32efc532012-04-11 23:30:20 +0200845 if (ret != 0) {
846 break;
847 }
Peter Crosthwaited714b8d2015-09-08 17:38:43 +0100848 if (recurse) {
849 do_object_child_foreach(child, fn, opaque, true);
850 }
Paolo Bonzini32efc532012-04-11 23:30:20 +0200851 }
852 }
853 return ret;
854}
855
Peter Crosthwaited714b8d2015-09-08 17:38:43 +0100856int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
857 void *opaque)
858{
859 return do_object_child_foreach(obj, fn, opaque, false);
860}
861
862int object_child_foreach_recursive(Object *obj,
863 int (*fn)(Object *child, void *opaque),
864 void *opaque)
865{
866 return do_object_child_foreach(obj, fn, opaque, true);
867}
868
Andreas Färber418ba9e2012-02-25 23:07:34 +0100869static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
870{
871 GSList **list = opaque;
872
873 *list = g_slist_prepend(*list, klass);
874}
875
876GSList *object_class_get_list(const char *implements_type,
877 bool include_abstract)
878{
879 GSList *list = NULL;
880
881 object_class_foreach(object_class_get_list_tramp,
882 implements_type, include_abstract, &list);
883 return list;
884}
885
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600886void object_ref(Object *obj)
887{
Peter Crosthwaite8ffad852014-06-05 23:13:36 -0700888 if (!obj) {
889 return;
890 }
Andreas Färber8438a132015-11-16 17:49:20 +0100891 atomic_inc(&obj->ref);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600892}
893
894void object_unref(Object *obj)
895{
Peter Crosthwaite8ffad852014-06-05 23:13:36 -0700896 if (!obj) {
897 return;
898 }
Andreas Färber8438a132015-11-16 17:49:20 +0100899 g_assert_cmpint(obj->ref, >, 0);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600900
901 /* parent always holds a reference to its children */
Jan Kiszkaf08c03f2013-07-02 11:36:39 +0200902 if (atomic_fetch_dec(&obj->ref) == 1) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600903 object_finalize(obj);
904 }
905}
906
Paolo Bonzini64607d02014-06-05 13:11:51 +0200907ObjectProperty *
908object_property_add(Object *obj, const char *name, const char *type,
909 ObjectPropertyAccessor *get,
910 ObjectPropertyAccessor *set,
911 ObjectPropertyRelease *release,
912 void *opaque, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600913{
Peter Maydell54852b02013-03-25 13:15:13 +0000914 ObjectProperty *prop;
Peter Crosthwaite33965902014-08-19 23:55:52 -0700915 size_t name_len = strlen(name);
916
917 if (name_len >= 3 && !memcmp(name + name_len - 3, "[*]", 4)) {
918 int i;
919 ObjectProperty *ret;
920 char *name_no_array = g_strdup(name);
921
922 name_no_array[name_len - 3] = '\0';
923 for (i = 0; ; ++i) {
924 char *full_name = g_strdup_printf("%s[%d]", name_no_array, i);
925
926 ret = object_property_add(obj, full_name, type, get, set,
927 release, opaque, NULL);
928 g_free(full_name);
929 if (ret) {
930 break;
931 }
932 }
933 g_free(name_no_array);
934 return ret;
935 }
Peter Maydell54852b02013-03-25 13:15:13 +0000936
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +0100937 if (object_property_find(obj, name, NULL) != NULL) {
Pavel Fedinb604a852015-10-13 13:37:45 +0100938 error_setg(errp, "attempt to add duplicate property '%s'"
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +0100939 " to object (type '%s')", name,
940 object_get_typename(obj));
Pavel Fedinb604a852015-10-13 13:37:45 +0100941 return NULL;
Peter Maydell54852b02013-03-25 13:15:13 +0000942 }
943
944 prop = g_malloc0(sizeof(*prop));
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600945
946 prop->name = g_strdup(name);
947 prop->type = g_strdup(type);
948
949 prop->get = get;
950 prop->set = set;
951 prop->release = release;
952 prop->opaque = opaque;
953
Pavel Fedinb604a852015-10-13 13:37:45 +0100954 g_hash_table_insert(obj->properties, prop->name, prop);
Paolo Bonzini64607d02014-06-05 13:11:51 +0200955 return prop;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600956}
957
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +0100958ObjectProperty *
959object_class_property_add(ObjectClass *klass,
960 const char *name,
961 const char *type,
962 ObjectPropertyAccessor *get,
963 ObjectPropertyAccessor *set,
964 ObjectPropertyRelease *release,
965 void *opaque,
966 Error **errp)
967{
968 ObjectProperty *prop;
969
970 if (object_class_property_find(klass, name, NULL) != NULL) {
971 error_setg(errp, "attempt to add duplicate property '%s'"
972 " to object (type '%s')", name,
973 object_class_get_name(klass));
974 return NULL;
975 }
976
977 prop = g_malloc0(sizeof(*prop));
978
979 prop->name = g_strdup(name);
980 prop->type = g_strdup(type);
981
982 prop->get = get;
983 prop->set = set;
984 prop->release = release;
985 prop->opaque = opaque;
986
987 g_hash_table_insert(klass->properties, g_strdup(name), prop);
988
989 return prop;
990}
991
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200992ObjectProperty *object_property_find(Object *obj, const char *name,
993 Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600994{
995 ObjectProperty *prop;
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +0100996 ObjectClass *klass = object_get_class(obj);
997
998 prop = object_class_property_find(klass, name, NULL);
999 if (prop) {
1000 return prop;
1001 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001002
Pavel Fedinb604a852015-10-13 13:37:45 +01001003 prop = g_hash_table_lookup(obj->properties, name);
1004 if (prop) {
1005 return prop;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001006 }
1007
Cole Robinsonf231b882014-03-21 19:42:26 -04001008 error_setg(errp, "Property '.%s' not found", name);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001009 return NULL;
1010}
1011
Daniel P. Berrange7746abd2015-12-09 12:34:02 +00001012void object_property_iter_init(ObjectPropertyIterator *iter,
1013 Object *obj)
Daniel P. Berrangea00c9482015-10-13 13:37:40 +01001014{
Daniel P. Berrange7746abd2015-12-09 12:34:02 +00001015 g_hash_table_iter_init(&iter->iter, obj->properties);
1016 iter->nextclass = object_get_class(obj);
Daniel P. Berrangea00c9482015-10-13 13:37:40 +01001017}
1018
1019ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter)
1020{
Pavel Fedinb604a852015-10-13 13:37:45 +01001021 gpointer key, val;
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001022 while (!g_hash_table_iter_next(&iter->iter, &key, &val)) {
1023 if (!iter->nextclass) {
1024 return NULL;
1025 }
1026 g_hash_table_iter_init(&iter->iter, iter->nextclass->properties);
1027 iter->nextclass = object_class_get_parent(iter->nextclass);
Daniel P. Berrangea00c9482015-10-13 13:37:40 +01001028 }
Pavel Fedinb604a852015-10-13 13:37:45 +01001029 return val;
Daniel P. Berrangea00c9482015-10-13 13:37:40 +01001030}
1031
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001032ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name,
1033 Error **errp)
1034{
1035 ObjectProperty *prop;
1036 ObjectClass *parent_klass;
1037
1038 parent_klass = object_class_get_parent(klass);
1039 if (parent_klass) {
1040 prop = object_class_property_find(parent_klass, name, NULL);
1041 if (prop) {
1042 return prop;
1043 }
1044 }
1045
1046 prop = g_hash_table_lookup(klass->properties, name);
1047 if (!prop) {
1048 error_setg(errp, "Property '.%s' not found", name);
1049 }
1050 return prop;
1051}
1052
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001053void object_property_del(Object *obj, const char *name, Error **errp)
1054{
Pavel Fedinb604a852015-10-13 13:37:45 +01001055 ObjectProperty *prop = g_hash_table_lookup(obj->properties, name);
1056
1057 if (!prop) {
1058 error_setg(errp, "Property '.%s' not found", name);
Anthony Liguori0866aca2011-12-23 15:34:39 -06001059 return;
1060 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001061
Anthony Liguori0866aca2011-12-23 15:34:39 -06001062 if (prop->release) {
1063 prop->release(obj, name, prop->opaque);
1064 }
Pavel Fedinb604a852015-10-13 13:37:45 +01001065 g_hash_table_remove(obj->properties, name);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001066}
1067
1068void object_property_get(Object *obj, Visitor *v, const char *name,
1069 Error **errp)
1070{
Paolo Bonzini89bfe002012-04-12 18:00:18 +02001071 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001072 if (prop == NULL) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001073 return;
1074 }
1075
1076 if (!prop->get) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001077 error_setg(errp, QERR_PERMISSION_DENIED);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001078 } else {
Eric Blaked7bce992016-01-29 06:48:55 -07001079 prop->get(obj, v, name, prop->opaque, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001080 }
1081}
1082
1083void object_property_set(Object *obj, Visitor *v, const char *name,
1084 Error **errp)
1085{
Paolo Bonzini89bfe002012-04-12 18:00:18 +02001086 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001087 if (prop == NULL) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001088 return;
1089 }
1090
1091 if (!prop->set) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001092 error_setg(errp, QERR_PERMISSION_DENIED);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001093 } else {
Eric Blaked7bce992016-01-29 06:48:55 -07001094 prop->set(obj, v, name, prop->opaque, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001095 }
1096}
1097
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001098void object_property_set_str(Object *obj, const char *value,
1099 const char *name, Error **errp)
1100{
1101 QString *qstr = qstring_from_str(value);
1102 object_property_set_qobject(obj, QOBJECT(qstr), name, errp);
1103
1104 QDECREF(qstr);
1105}
1106
1107char *object_property_get_str(Object *obj, const char *name,
1108 Error **errp)
1109{
1110 QObject *ret = object_property_get_qobject(obj, name, errp);
1111 QString *qstring;
1112 char *retval;
1113
1114 if (!ret) {
1115 return NULL;
1116 }
1117 qstring = qobject_to_qstring(ret);
1118 if (!qstring) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001119 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001120 retval = NULL;
1121 } else {
1122 retval = g_strdup(qstring_get_str(qstring));
1123 }
1124
1125 QDECREF(qstring);
1126 return retval;
1127}
1128
Paolo Bonzini1d9c5a12012-02-02 10:51:57 +01001129void object_property_set_link(Object *obj, Object *value,
1130 const char *name, Error **errp)
1131{
Peter Crosthwaited3c49312014-09-25 22:19:19 -07001132 if (value) {
1133 gchar *path = object_get_canonical_path(value);
1134 object_property_set_str(obj, path, name, errp);
1135 g_free(path);
1136 } else {
1137 object_property_set_str(obj, "", name, errp);
1138 }
Paolo Bonzini1d9c5a12012-02-02 10:51:57 +01001139}
1140
1141Object *object_property_get_link(Object *obj, const char *name,
1142 Error **errp)
1143{
1144 char *str = object_property_get_str(obj, name, errp);
1145 Object *target = NULL;
1146
1147 if (str && *str) {
1148 target = object_resolve_path(str, NULL);
1149 if (!target) {
Markus Armbruster75158eb2015-03-16 08:57:47 +01001150 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1151 "Device '%s' not found", str);
Paolo Bonzini1d9c5a12012-02-02 10:51:57 +01001152 }
1153 }
1154
1155 g_free(str);
1156 return target;
1157}
1158
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001159void object_property_set_bool(Object *obj, bool value,
1160 const char *name, Error **errp)
1161{
Eric Blakefc48ffc2015-05-15 16:24:59 -06001162 QBool *qbool = qbool_from_bool(value);
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001163 object_property_set_qobject(obj, QOBJECT(qbool), name, errp);
1164
1165 QDECREF(qbool);
1166}
1167
1168bool object_property_get_bool(Object *obj, const char *name,
1169 Error **errp)
1170{
1171 QObject *ret = object_property_get_qobject(obj, name, errp);
1172 QBool *qbool;
1173 bool retval;
1174
1175 if (!ret) {
1176 return false;
1177 }
1178 qbool = qobject_to_qbool(ret);
1179 if (!qbool) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001180 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001181 retval = false;
1182 } else {
Eric Blakefc48ffc2015-05-15 16:24:59 -06001183 retval = qbool_get_bool(qbool);
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001184 }
1185
1186 QDECREF(qbool);
1187 return retval;
1188}
1189
1190void object_property_set_int(Object *obj, int64_t value,
1191 const char *name, Error **errp)
1192{
1193 QInt *qint = qint_from_int(value);
1194 object_property_set_qobject(obj, QOBJECT(qint), name, errp);
1195
1196 QDECREF(qint);
1197}
1198
1199int64_t object_property_get_int(Object *obj, const char *name,
1200 Error **errp)
1201{
1202 QObject *ret = object_property_get_qobject(obj, name, errp);
1203 QInt *qint;
1204 int64_t retval;
1205
1206 if (!ret) {
1207 return -1;
1208 }
1209 qint = qobject_to_qint(ret);
1210 if (!qint) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001211 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001212 retval = -1;
1213 } else {
1214 retval = qint_get_int(qint);
1215 }
1216
1217 QDECREF(qint);
1218 return retval;
1219}
1220
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01001221typedef struct EnumProperty {
1222 const char * const *strings;
1223 int (*get)(Object *, Error **);
1224 void (*set)(Object *, int, Error **);
1225} EnumProperty;
1226
Hu Tao1f217722014-05-14 17:43:33 +08001227int object_property_get_enum(Object *obj, const char *name,
Daniel P. Berrangea3590da2015-05-27 16:07:56 +01001228 const char *typename, Error **errp)
Hu Tao1f217722014-05-14 17:43:33 +08001229{
Markus Armbruster4715d422015-08-25 20:00:45 +02001230 Error *err = NULL;
Eric Blake7a0525c2016-06-09 10:48:37 -06001231 Visitor *v;
Chen Fan976620a2014-08-18 14:46:34 +08001232 char *str;
Hu Tao1f217722014-05-14 17:43:33 +08001233 int ret;
Daniel P. Berrangea3590da2015-05-27 16:07:56 +01001234 ObjectProperty *prop = object_property_find(obj, name, errp);
1235 EnumProperty *enumprop;
1236
1237 if (prop == NULL) {
1238 return 0;
1239 }
1240
1241 if (!g_str_equal(prop->type, typename)) {
1242 error_setg(errp, "Property %s on %s is not '%s' enum type",
1243 name, object_class_get_name(
1244 object_get_class(obj)), typename);
1245 return 0;
1246 }
1247
1248 enumprop = prop->opaque;
Hu Tao1f217722014-05-14 17:43:33 +08001249
Eric Blake3b098d52016-06-09 10:48:43 -06001250 v = string_output_visitor_new(false, &str);
Eric Blakee7ca5652016-06-09 10:48:39 -06001251 object_property_get(obj, v, name, &err);
Markus Armbruster4715d422015-08-25 20:00:45 +02001252 if (err) {
1253 error_propagate(errp, err);
Eric Blakee7ca5652016-06-09 10:48:39 -06001254 visit_free(v);
Markus Armbruster4715d422015-08-25 20:00:45 +02001255 return 0;
1256 }
Eric Blake3b098d52016-06-09 10:48:43 -06001257 visit_complete(v, &str);
Eric Blakee7ca5652016-06-09 10:48:39 -06001258 visit_free(v);
Eric Blake7a0525c2016-06-09 10:48:37 -06001259 v = string_input_visitor_new(str);
1260 visit_type_enum(v, name, &ret, enumprop->strings, errp);
Chen Fan976620a2014-08-18 14:46:34 +08001261
1262 g_free(str);
Eric Blake7a0525c2016-06-09 10:48:37 -06001263 visit_free(v);
Hu Tao1f217722014-05-14 17:43:33 +08001264
1265 return ret;
1266}
1267
1268void object_property_get_uint16List(Object *obj, const char *name,
1269 uint16List **list, Error **errp)
1270{
Markus Armbruster4715d422015-08-25 20:00:45 +02001271 Error *err = NULL;
Eric Blake7a0525c2016-06-09 10:48:37 -06001272 Visitor *v;
Chen Fan976620a2014-08-18 14:46:34 +08001273 char *str;
Hu Tao1f217722014-05-14 17:43:33 +08001274
Eric Blake3b098d52016-06-09 10:48:43 -06001275 v = string_output_visitor_new(false, &str);
1276 object_property_get(obj, v, name, &err);
Markus Armbruster4715d422015-08-25 20:00:45 +02001277 if (err) {
1278 error_propagate(errp, err);
1279 goto out;
1280 }
Eric Blake3b098d52016-06-09 10:48:43 -06001281 visit_complete(v, &str);
1282 visit_free(v);
Eric Blake7a0525c2016-06-09 10:48:37 -06001283 v = string_input_visitor_new(str);
1284 visit_type_uint16List(v, NULL, list, errp);
Chen Fan976620a2014-08-18 14:46:34 +08001285
1286 g_free(str);
Markus Armbruster4715d422015-08-25 20:00:45 +02001287out:
Eric Blake3b098d52016-06-09 10:48:43 -06001288 visit_free(v);
Hu Tao1f217722014-05-14 17:43:33 +08001289}
1290
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001291void object_property_parse(Object *obj, const char *string,
1292 const char *name, Error **errp)
1293{
Eric Blake7a0525c2016-06-09 10:48:37 -06001294 Visitor *v = string_input_visitor_new(string);
1295 object_property_set(obj, v, name, errp);
1296 visit_free(v);
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001297}
1298
Paolo Bonzini0b7593e2014-02-08 11:01:50 +01001299char *object_property_print(Object *obj, const char *name, bool human,
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001300 Error **errp)
1301{
Eric Blake3b098d52016-06-09 10:48:43 -06001302 Visitor *v;
Gonglei3a530092014-09-27 13:13:55 +08001303 char *string = NULL;
1304 Error *local_err = NULL;
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001305
Eric Blake3b098d52016-06-09 10:48:43 -06001306 v = string_output_visitor_new(human, &string);
1307 object_property_get(obj, v, name, &local_err);
Gonglei3a530092014-09-27 13:13:55 +08001308 if (local_err) {
1309 error_propagate(errp, local_err);
1310 goto out;
1311 }
1312
Eric Blake3b098d52016-06-09 10:48:43 -06001313 visit_complete(v, &string);
Gonglei3a530092014-09-27 13:13:55 +08001314
1315out:
Eric Blake3b098d52016-06-09 10:48:43 -06001316 visit_free(v);
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001317 return string;
1318}
1319
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001320const char *object_property_get_type(Object *obj, const char *name, Error **errp)
1321{
Paolo Bonzini89bfe002012-04-12 18:00:18 +02001322 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001323 if (prop == NULL) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001324 return NULL;
1325 }
1326
1327 return prop->type;
1328}
1329
1330Object *object_get_root(void)
1331{
Anthony Liguori8b45d442011-12-23 09:08:05 -06001332 static Object *root;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001333
Anthony Liguori8b45d442011-12-23 09:08:05 -06001334 if (!root) {
1335 root = object_new("container");
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001336 }
1337
Anthony Liguori8b45d442011-12-23 09:08:05 -06001338 return root;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001339}
1340
Daniel P. Berrangebc2256c2015-05-13 17:14:05 +01001341Object *object_get_objects_root(void)
1342{
1343 return container_get(object_get_root(), "/objects");
1344}
1345
Eric Blaked7bce992016-01-29 06:48:55 -07001346static void object_get_child_property(Object *obj, Visitor *v,
1347 const char *name, void *opaque,
1348 Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001349{
1350 Object *child = opaque;
1351 gchar *path;
1352
1353 path = object_get_canonical_path(child);
Eric Blake51e72bc2016-01-29 06:48:54 -07001354 visit_type_str(v, name, &path, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001355 g_free(path);
1356}
1357
Paolo Bonzini64607d02014-06-05 13:11:51 +02001358static Object *object_resolve_child_property(Object *parent, void *opaque, const gchar *part)
1359{
1360 return opaque;
1361}
1362
Anthony Liguoridb85b572011-12-23 08:47:39 -06001363static void object_finalize_child_property(Object *obj, const char *name,
1364 void *opaque)
1365{
1366 Object *child = opaque;
1367
Paolo Bonzinibffc6872014-06-11 11:57:38 +02001368 if (child->class->unparent) {
1369 (child->class->unparent)(child);
1370 }
1371 child->parent = NULL;
Anthony Liguoridb85b572011-12-23 08:47:39 -06001372 object_unref(child);
1373}
1374
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001375void object_property_add_child(Object *obj, const char *name,
1376 Object *child, Error **errp)
1377{
Paolo Bonzinib0ed5e92013-12-20 23:21:08 +01001378 Error *local_err = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001379 gchar *type;
Paolo Bonzini64607d02014-06-05 13:11:51 +02001380 ObjectProperty *op;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001381
Peter Crosthwaite8faa2f82014-09-25 22:19:52 -07001382 if (child->parent != NULL) {
1383 error_setg(errp, "child object is already parented");
1384 return;
1385 }
1386
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001387 type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child)));
1388
Paolo Bonzini64607d02014-06-05 13:11:51 +02001389 op = object_property_add(obj, name, type, object_get_child_property, NULL,
1390 object_finalize_child_property, child, &local_err);
Paolo Bonzinib0ed5e92013-12-20 23:21:08 +01001391 if (local_err) {
1392 error_propagate(errp, local_err);
1393 goto out;
1394 }
Paolo Bonzini64607d02014-06-05 13:11:51 +02001395
1396 op->resolve = object_resolve_child_property;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001397 object_ref(child);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001398 child->parent = obj;
1399
Paolo Bonzinib0ed5e92013-12-20 23:21:08 +01001400out:
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001401 g_free(type);
1402}
1403
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001404void object_property_allow_set_link(Object *obj, const char *name,
1405 Object *val, Error **errp)
1406{
1407 /* Allow the link to be set, always */
1408}
1409
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001410typedef struct {
1411 Object **child;
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001412 void (*check)(Object *, const char *, Object *, Error **);
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001413 ObjectPropertyLinkFlags flags;
1414} LinkProperty;
1415
Eric Blaked7bce992016-01-29 06:48:55 -07001416static void object_get_link_property(Object *obj, Visitor *v,
1417 const char *name, void *opaque,
1418 Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001419{
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001420 LinkProperty *lprop = opaque;
1421 Object **child = lprop->child;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001422 gchar *path;
1423
1424 if (*child) {
1425 path = object_get_canonical_path(*child);
Eric Blake51e72bc2016-01-29 06:48:54 -07001426 visit_type_str(v, name, &path, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001427 g_free(path);
1428 } else {
1429 path = (gchar *)"";
Eric Blake51e72bc2016-01-29 06:48:54 -07001430 visit_type_str(v, name, &path, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001431 }
1432}
1433
Stefan Hajnoczif5ec6702014-03-19 08:58:53 +01001434/*
1435 * object_resolve_link:
1436 *
1437 * Lookup an object and ensure its type matches the link property type. This
1438 * is similar to object_resolve_path() except type verification against the
1439 * link property is performed.
1440 *
1441 * Returns: The matched object or NULL on path lookup failures.
1442 */
1443static Object *object_resolve_link(Object *obj, const char *name,
1444 const char *path, Error **errp)
1445{
1446 const char *type;
1447 gchar *target_type;
1448 bool ambiguous = false;
1449 Object *target;
1450
1451 /* Go from link<FOO> to FOO. */
1452 type = object_property_get_type(obj, name, NULL);
1453 target_type = g_strndup(&type[5], strlen(type) - 6);
1454 target = object_resolve_path_type(path, target_type, &ambiguous);
1455
1456 if (ambiguous) {
Eric Blake455b0fd2015-11-10 23:51:20 -07001457 error_setg(errp, "Path '%s' does not uniquely identify an object",
1458 path);
Stefan Hajnoczif5ec6702014-03-19 08:58:53 +01001459 } else if (!target) {
1460 target = object_resolve_path(path, &ambiguous);
1461 if (target || ambiguous) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +01001462 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
Stefan Hajnoczif5ec6702014-03-19 08:58:53 +01001463 } else {
Markus Armbruster75158eb2015-03-16 08:57:47 +01001464 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1465 "Device '%s' not found", path);
Stefan Hajnoczif5ec6702014-03-19 08:58:53 +01001466 }
1467 target = NULL;
1468 }
1469 g_free(target_type);
1470
1471 return target;
1472}
1473
Eric Blaked7bce992016-01-29 06:48:55 -07001474static void object_set_link_property(Object *obj, Visitor *v,
1475 const char *name, void *opaque,
1476 Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001477{
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001478 Error *local_err = NULL;
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001479 LinkProperty *prop = opaque;
1480 Object **child = prop->child;
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001481 Object *old_target = *child;
1482 Object *new_target = NULL;
1483 char *path = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001484
Eric Blake51e72bc2016-01-29 06:48:54 -07001485 visit_type_str(v, name, &path, &local_err);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001486
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001487 if (!local_err && strcmp(path, "") != 0) {
1488 new_target = object_resolve_link(obj, name, path, &local_err);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001489 }
1490
1491 g_free(path);
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001492 if (local_err) {
1493 error_propagate(errp, local_err);
1494 return;
1495 }
Alexander Barabashf0cdc962012-02-22 19:22:26 +02001496
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001497 prop->check(obj, name, new_target, &local_err);
1498 if (local_err) {
1499 error_propagate(errp, local_err);
1500 return;
1501 }
1502
Peter Crosthwaite8ffad852014-06-05 23:13:36 -07001503 object_ref(new_target);
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001504 *child = new_target;
Peter Crosthwaite8ffad852014-06-05 23:13:36 -07001505 object_unref(old_target);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001506}
1507
Paolo Bonzini64607d02014-06-05 13:11:51 +02001508static Object *object_resolve_link_property(Object *parent, void *opaque, const gchar *part)
1509{
1510 LinkProperty *lprop = opaque;
1511
1512 return *lprop->child;
1513}
1514
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001515static void object_release_link_property(Object *obj, const char *name,
1516 void *opaque)
1517{
1518 LinkProperty *prop = opaque;
1519
1520 if ((prop->flags & OBJ_PROP_LINK_UNREF_ON_RELEASE) && *prop->child) {
1521 object_unref(*prop->child);
1522 }
1523 g_free(prop);
1524}
1525
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001526void object_property_add_link(Object *obj, const char *name,
1527 const char *type, Object **child,
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001528 void (*check)(Object *, const char *,
1529 Object *, Error **),
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001530 ObjectPropertyLinkFlags flags,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001531 Error **errp)
1532{
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001533 Error *local_err = NULL;
1534 LinkProperty *prop = g_malloc(sizeof(*prop));
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001535 gchar *full_type;
Paolo Bonzini64607d02014-06-05 13:11:51 +02001536 ObjectProperty *op;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001537
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001538 prop->child = child;
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001539 prop->check = check;
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001540 prop->flags = flags;
1541
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001542 full_type = g_strdup_printf("link<%s>", type);
1543
Paolo Bonzini64607d02014-06-05 13:11:51 +02001544 op = object_property_add(obj, name, full_type,
1545 object_get_link_property,
1546 check ? object_set_link_property : NULL,
1547 object_release_link_property,
1548 prop,
1549 &local_err);
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001550 if (local_err) {
1551 error_propagate(errp, local_err);
1552 g_free(prop);
Paolo Bonzini64607d02014-06-05 13:11:51 +02001553 goto out;
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001554 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001555
Paolo Bonzini64607d02014-06-05 13:11:51 +02001556 op->resolve = object_resolve_link_property;
1557
1558out:
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001559 g_free(full_type);
1560}
1561
Paolo Bonzinifb9e7e32015-05-05 18:29:00 +02001562void object_property_add_const_link(Object *obj, const char *name,
1563 Object *target, Error **errp)
1564{
1565 char *link_type;
1566 ObjectProperty *op;
1567
1568 link_type = g_strdup_printf("link<%s>", object_get_typename(target));
1569 op = object_property_add(obj, name, link_type,
1570 object_get_child_property, NULL,
1571 NULL, target, errp);
1572 if (op != NULL) {
1573 op->resolve = object_resolve_child_property;
1574 }
1575 g_free(link_type);
1576}
1577
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001578gchar *object_get_canonical_path_component(Object *obj)
1579{
1580 ObjectProperty *prop = NULL;
Pavel Fedinb604a852015-10-13 13:37:45 +01001581 GHashTableIter iter;
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001582
1583 g_assert(obj);
1584 g_assert(obj->parent != NULL);
1585
Pavel Fedinb604a852015-10-13 13:37:45 +01001586 g_hash_table_iter_init(&iter, obj->parent->properties);
1587 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001588 if (!object_property_is_child(prop)) {
1589 continue;
1590 }
1591
1592 if (prop->opaque == obj) {
1593 return g_strdup(prop->name);
1594 }
1595 }
1596
1597 /* obj had a parent but was not a child, should never happen */
1598 g_assert_not_reached();
1599 return NULL;
1600}
1601
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001602gchar *object_get_canonical_path(Object *obj)
1603{
1604 Object *root = object_get_root();
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001605 char *newpath, *path = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001606
1607 while (obj != root) {
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001608 char *component = object_get_canonical_path_component(obj);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001609
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001610 if (path) {
1611 newpath = g_strdup_printf("%s/%s", component, path);
1612 g_free(component);
1613 g_free(path);
1614 path = newpath;
1615 } else {
1616 path = component;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001617 }
1618
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001619 obj = obj->parent;
1620 }
1621
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001622 newpath = g_strdup_printf("/%s", path ? path : "");
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001623 g_free(path);
1624
1625 return newpath;
1626}
1627
Andreas Färber3e84b482013-01-15 02:55:10 +01001628Object *object_resolve_path_component(Object *parent, const gchar *part)
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001629{
Paolo Bonzini89bfe002012-04-12 18:00:18 +02001630 ObjectProperty *prop = object_property_find(parent, part, NULL);
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001631 if (prop == NULL) {
1632 return NULL;
1633 }
1634
Paolo Bonzini64607d02014-06-05 13:11:51 +02001635 if (prop->resolve) {
1636 return prop->resolve(parent, prop->opaque, part);
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001637 } else {
1638 return NULL;
1639 }
1640}
1641
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001642static Object *object_resolve_abs_path(Object *parent,
1643 gchar **parts,
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001644 const char *typename,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001645 int index)
1646{
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001647 Object *child;
1648
1649 if (parts[index] == NULL) {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001650 return object_dynamic_cast(parent, typename);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001651 }
1652
1653 if (strcmp(parts[index], "") == 0) {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001654 return object_resolve_abs_path(parent, parts, typename, index + 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001655 }
1656
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001657 child = object_resolve_path_component(parent, parts[index]);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001658 if (!child) {
1659 return NULL;
1660 }
1661
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001662 return object_resolve_abs_path(child, parts, typename, index + 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001663}
1664
1665static Object *object_resolve_partial_path(Object *parent,
1666 gchar **parts,
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001667 const char *typename,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001668 bool *ambiguous)
1669{
1670 Object *obj;
Pavel Fedinb604a852015-10-13 13:37:45 +01001671 GHashTableIter iter;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001672 ObjectProperty *prop;
1673
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001674 obj = object_resolve_abs_path(parent, parts, typename, 0);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001675
Pavel Fedinb604a852015-10-13 13:37:45 +01001676 g_hash_table_iter_init(&iter, parent->properties);
1677 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001678 Object *found;
1679
Andreas Färber5d9d3f42012-05-27 00:32:40 +02001680 if (!object_property_is_child(prop)) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001681 continue;
1682 }
1683
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001684 found = object_resolve_partial_path(prop->opaque, parts,
1685 typename, ambiguous);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001686 if (found) {
1687 if (obj) {
1688 if (ambiguous) {
1689 *ambiguous = true;
1690 }
1691 return NULL;
1692 }
1693 obj = found;
1694 }
1695
1696 if (ambiguous && *ambiguous) {
1697 return NULL;
1698 }
1699 }
1700
1701 return obj;
1702}
1703
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001704Object *object_resolve_path_type(const char *path, const char *typename,
1705 bool *ambiguous)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001706{
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001707 Object *obj;
1708 gchar **parts;
1709
1710 parts = g_strsplit(path, "/", 0);
Paolo Bonzini2e1103f2013-04-18 18:44:02 +02001711 assert(parts);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001712
Paolo Bonzini2e1103f2013-04-18 18:44:02 +02001713 if (parts[0] == NULL || strcmp(parts[0], "") != 0) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001714 if (ambiguous) {
1715 *ambiguous = false;
1716 }
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001717 obj = object_resolve_partial_path(object_get_root(), parts,
1718 typename, ambiguous);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001719 } else {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001720 obj = object_resolve_abs_path(object_get_root(), parts, typename, 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001721 }
1722
1723 g_strfreev(parts);
1724
1725 return obj;
1726}
1727
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001728Object *object_resolve_path(const char *path, bool *ambiguous)
1729{
1730 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
1731}
1732
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001733typedef struct StringProperty
1734{
1735 char *(*get)(Object *, Error **);
1736 void (*set)(Object *, const char *, Error **);
1737} StringProperty;
1738
Eric Blaked7bce992016-01-29 06:48:55 -07001739static void property_get_str(Object *obj, Visitor *v, const char *name,
1740 void *opaque, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001741{
1742 StringProperty *prop = opaque;
1743 char *value;
Markus Armbrustere1c82372015-08-25 20:00:46 +02001744 Error *err = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001745
Markus Armbrustere1c82372015-08-25 20:00:46 +02001746 value = prop->get(obj, &err);
1747 if (err) {
1748 error_propagate(errp, err);
1749 return;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001750 }
Markus Armbrustere1c82372015-08-25 20:00:46 +02001751
Eric Blake51e72bc2016-01-29 06:48:54 -07001752 visit_type_str(v, name, &value, errp);
Markus Armbrustere1c82372015-08-25 20:00:46 +02001753 g_free(value);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001754}
1755
Eric Blaked7bce992016-01-29 06:48:55 -07001756static void property_set_str(Object *obj, Visitor *v, const char *name,
1757 void *opaque, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001758{
1759 StringProperty *prop = opaque;
1760 char *value;
1761 Error *local_err = NULL;
1762
Eric Blake51e72bc2016-01-29 06:48:54 -07001763 visit_type_str(v, name, &value, &local_err);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001764 if (local_err) {
1765 error_propagate(errp, local_err);
1766 return;
1767 }
1768
1769 prop->set(obj, value, errp);
1770 g_free(value);
1771}
1772
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001773static void property_release_str(Object *obj, const char *name,
1774 void *opaque)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001775{
1776 StringProperty *prop = opaque;
1777 g_free(prop);
1778}
1779
1780void object_property_add_str(Object *obj, const char *name,
1781 char *(*get)(Object *, Error **),
1782 void (*set)(Object *, const char *, Error **),
1783 Error **errp)
1784{
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01001785 Error *local_err = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001786 StringProperty *prop = g_malloc0(sizeof(*prop));
1787
1788 prop->get = get;
1789 prop->set = set;
1790
1791 object_property_add(obj, name, "string",
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001792 get ? property_get_str : NULL,
1793 set ? property_set_str : NULL,
1794 property_release_str,
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01001795 prop, &local_err);
1796 if (local_err) {
1797 error_propagate(errp, local_err);
1798 g_free(prop);
1799 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001800}
Paolo Bonzini745549c2012-03-31 16:45:54 +02001801
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001802void object_class_property_add_str(ObjectClass *klass, const char *name,
1803 char *(*get)(Object *, Error **),
1804 void (*set)(Object *, const char *,
1805 Error **),
1806 Error **errp)
1807{
1808 Error *local_err = NULL;
1809 StringProperty *prop = g_malloc0(sizeof(*prop));
1810
1811 prop->get = get;
1812 prop->set = set;
1813
1814 object_class_property_add(klass, name, "string",
1815 get ? property_get_str : NULL,
1816 set ? property_set_str : NULL,
1817 property_release_str,
1818 prop, &local_err);
1819 if (local_err) {
1820 error_propagate(errp, local_err);
1821 g_free(prop);
1822 }
1823}
1824
Anthony Liguori0e558842012-06-25 10:32:46 -05001825typedef struct BoolProperty
1826{
1827 bool (*get)(Object *, Error **);
1828 void (*set)(Object *, bool, Error **);
1829} BoolProperty;
1830
Eric Blaked7bce992016-01-29 06:48:55 -07001831static void property_get_bool(Object *obj, Visitor *v, const char *name,
1832 void *opaque, Error **errp)
Anthony Liguori0e558842012-06-25 10:32:46 -05001833{
1834 BoolProperty *prop = opaque;
1835 bool value;
Markus Armbruster4715d422015-08-25 20:00:45 +02001836 Error *err = NULL;
Anthony Liguori0e558842012-06-25 10:32:46 -05001837
Markus Armbruster4715d422015-08-25 20:00:45 +02001838 value = prop->get(obj, &err);
1839 if (err) {
1840 error_propagate(errp, err);
1841 return;
1842 }
1843
Eric Blake51e72bc2016-01-29 06:48:54 -07001844 visit_type_bool(v, name, &value, errp);
Anthony Liguori0e558842012-06-25 10:32:46 -05001845}
1846
Eric Blaked7bce992016-01-29 06:48:55 -07001847static void property_set_bool(Object *obj, Visitor *v, const char *name,
1848 void *opaque, Error **errp)
Anthony Liguori0e558842012-06-25 10:32:46 -05001849{
1850 BoolProperty *prop = opaque;
1851 bool value;
1852 Error *local_err = NULL;
1853
Eric Blake51e72bc2016-01-29 06:48:54 -07001854 visit_type_bool(v, name, &value, &local_err);
Anthony Liguori0e558842012-06-25 10:32:46 -05001855 if (local_err) {
1856 error_propagate(errp, local_err);
1857 return;
1858 }
1859
1860 prop->set(obj, value, errp);
1861}
1862
1863static void property_release_bool(Object *obj, const char *name,
1864 void *opaque)
1865{
1866 BoolProperty *prop = opaque;
1867 g_free(prop);
1868}
1869
1870void object_property_add_bool(Object *obj, const char *name,
1871 bool (*get)(Object *, Error **),
1872 void (*set)(Object *, bool, Error **),
1873 Error **errp)
1874{
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01001875 Error *local_err = NULL;
Anthony Liguori0e558842012-06-25 10:32:46 -05001876 BoolProperty *prop = g_malloc0(sizeof(*prop));
1877
1878 prop->get = get;
1879 prop->set = set;
1880
1881 object_property_add(obj, name, "bool",
1882 get ? property_get_bool : NULL,
1883 set ? property_set_bool : NULL,
1884 property_release_bool,
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01001885 prop, &local_err);
1886 if (local_err) {
1887 error_propagate(errp, local_err);
1888 g_free(prop);
1889 }
Anthony Liguori0e558842012-06-25 10:32:46 -05001890}
1891
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001892void object_class_property_add_bool(ObjectClass *klass, const char *name,
1893 bool (*get)(Object *, Error **),
1894 void (*set)(Object *, bool, Error **),
1895 Error **errp)
1896{
1897 Error *local_err = NULL;
1898 BoolProperty *prop = g_malloc0(sizeof(*prop));
1899
1900 prop->get = get;
1901 prop->set = set;
1902
1903 object_class_property_add(klass, name, "bool",
1904 get ? property_get_bool : NULL,
1905 set ? property_set_bool : NULL,
1906 property_release_bool,
1907 prop, &local_err);
1908 if (local_err) {
1909 error_propagate(errp, local_err);
1910 g_free(prop);
1911 }
1912}
1913
Eric Blaked7bce992016-01-29 06:48:55 -07001914static void property_get_enum(Object *obj, Visitor *v, const char *name,
1915 void *opaque, Error **errp)
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01001916{
1917 EnumProperty *prop = opaque;
1918 int value;
Markus Armbruster4715d422015-08-25 20:00:45 +02001919 Error *err = NULL;
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01001920
Markus Armbruster4715d422015-08-25 20:00:45 +02001921 value = prop->get(obj, &err);
1922 if (err) {
1923 error_propagate(errp, err);
1924 return;
1925 }
1926
Eric Blake337283d2016-01-29 06:48:57 -07001927 visit_type_enum(v, name, &value, prop->strings, errp);
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01001928}
1929
Eric Blaked7bce992016-01-29 06:48:55 -07001930static void property_set_enum(Object *obj, Visitor *v, const char *name,
1931 void *opaque, Error **errp)
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01001932{
1933 EnumProperty *prop = opaque;
1934 int value;
Markus Armbruster4715d422015-08-25 20:00:45 +02001935 Error *err = NULL;
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01001936
Eric Blake337283d2016-01-29 06:48:57 -07001937 visit_type_enum(v, name, &value, prop->strings, &err);
Markus Armbruster4715d422015-08-25 20:00:45 +02001938 if (err) {
1939 error_propagate(errp, err);
1940 return;
1941 }
Daniel P. Berrangea8e3fbe2015-05-13 17:14:08 +01001942 prop->set(obj, value, errp);
1943}
1944
1945static void property_release_enum(Object *obj, const char *name,
1946 void *opaque)
1947{
1948 EnumProperty *prop = opaque;
1949 g_free(prop);
1950}
1951
1952void object_property_add_enum(Object *obj, const char *name,
1953 const char *typename,
1954 const char * const *strings,
1955 int (*get)(Object *, Error **),
1956 void (*set)(Object *, int, Error **),
1957 Error **errp)
1958{
1959 Error *local_err = NULL;
1960 EnumProperty *prop = g_malloc(sizeof(*prop));
1961
1962 prop->strings = strings;
1963 prop->get = get;
1964 prop->set = set;
1965
1966 object_property_add(obj, name, typename,
1967 get ? property_get_enum : NULL,
1968 set ? property_set_enum : NULL,
1969 property_release_enum,
1970 prop, &local_err);
1971 if (local_err) {
1972 error_propagate(errp, local_err);
1973 g_free(prop);
1974 }
1975}
1976
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01001977void object_class_property_add_enum(ObjectClass *klass, const char *name,
1978 const char *typename,
1979 const char * const *strings,
1980 int (*get)(Object *, Error **),
1981 void (*set)(Object *, int, Error **),
1982 Error **errp)
1983{
1984 Error *local_err = NULL;
1985 EnumProperty *prop = g_malloc(sizeof(*prop));
1986
1987 prop->strings = strings;
1988 prop->get = get;
1989 prop->set = set;
1990
1991 object_class_property_add(klass, name, typename,
1992 get ? property_get_enum : NULL,
1993 set ? property_set_enum : NULL,
1994 property_release_enum,
1995 prop, &local_err);
1996 if (local_err) {
1997 error_propagate(errp, local_err);
1998 g_free(prop);
1999 }
2000}
2001
David Gibson8e099d12015-02-06 14:55:45 +11002002typedef struct TMProperty {
2003 void (*get)(Object *, struct tm *, Error **);
2004} TMProperty;
2005
Eric Blaked7bce992016-01-29 06:48:55 -07002006static void property_get_tm(Object *obj, Visitor *v, const char *name,
2007 void *opaque, Error **errp)
David Gibson8e099d12015-02-06 14:55:45 +11002008{
2009 TMProperty *prop = opaque;
2010 Error *err = NULL;
2011 struct tm value;
2012
2013 prop->get(obj, &value, &err);
2014 if (err) {
2015 goto out;
2016 }
2017
Eric Blake337283d2016-01-29 06:48:57 -07002018 visit_start_struct(v, name, NULL, 0, &err);
David Gibson8e099d12015-02-06 14:55:45 +11002019 if (err) {
2020 goto out;
2021 }
Eric Blake51e72bc2016-01-29 06:48:54 -07002022 visit_type_int32(v, "tm_year", &value.tm_year, &err);
David Gibson8e099d12015-02-06 14:55:45 +11002023 if (err) {
2024 goto out_end;
2025 }
Eric Blake51e72bc2016-01-29 06:48:54 -07002026 visit_type_int32(v, "tm_mon", &value.tm_mon, &err);
David Gibson8e099d12015-02-06 14:55:45 +11002027 if (err) {
2028 goto out_end;
2029 }
Eric Blake51e72bc2016-01-29 06:48:54 -07002030 visit_type_int32(v, "tm_mday", &value.tm_mday, &err);
David Gibson8e099d12015-02-06 14:55:45 +11002031 if (err) {
2032 goto out_end;
2033 }
Eric Blake51e72bc2016-01-29 06:48:54 -07002034 visit_type_int32(v, "tm_hour", &value.tm_hour, &err);
David Gibson8e099d12015-02-06 14:55:45 +11002035 if (err) {
2036 goto out_end;
2037 }
Eric Blake51e72bc2016-01-29 06:48:54 -07002038 visit_type_int32(v, "tm_min", &value.tm_min, &err);
David Gibson8e099d12015-02-06 14:55:45 +11002039 if (err) {
2040 goto out_end;
2041 }
Eric Blake51e72bc2016-01-29 06:48:54 -07002042 visit_type_int32(v, "tm_sec", &value.tm_sec, &err);
David Gibson8e099d12015-02-06 14:55:45 +11002043 if (err) {
2044 goto out_end;
2045 }
Eric Blake15c2f662016-04-28 15:45:27 -06002046 visit_check_struct(v, &err);
David Gibson8e099d12015-02-06 14:55:45 +11002047out_end:
Eric Blake1158bb22016-06-09 10:48:34 -06002048 visit_end_struct(v, NULL);
David Gibson8e099d12015-02-06 14:55:45 +11002049out:
2050 error_propagate(errp, err);
2051
2052}
2053
2054static void property_release_tm(Object *obj, const char *name,
2055 void *opaque)
2056{
2057 TMProperty *prop = opaque;
2058 g_free(prop);
2059}
2060
2061void object_property_add_tm(Object *obj, const char *name,
2062 void (*get)(Object *, struct tm *, Error **),
2063 Error **errp)
2064{
2065 Error *local_err = NULL;
2066 TMProperty *prop = g_malloc0(sizeof(*prop));
2067
2068 prop->get = get;
2069
2070 object_property_add(obj, name, "struct tm",
2071 get ? property_get_tm : NULL, NULL,
2072 property_release_tm,
2073 prop, &local_err);
2074 if (local_err) {
2075 error_propagate(errp, local_err);
2076 g_free(prop);
2077 }
2078}
2079
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002080void object_class_property_add_tm(ObjectClass *klass, const char *name,
2081 void (*get)(Object *, struct tm *, Error **),
2082 Error **errp)
2083{
2084 Error *local_err = NULL;
2085 TMProperty *prop = g_malloc0(sizeof(*prop));
2086
2087 prop->get = get;
2088
2089 object_class_property_add(klass, name, "struct tm",
2090 get ? property_get_tm : NULL, NULL,
2091 property_release_tm,
2092 prop, &local_err);
2093 if (local_err) {
2094 error_propagate(errp, local_err);
2095 g_free(prop);
2096 }
2097}
2098
Paolo Bonzini2f262e02012-04-02 17:33:51 +02002099static char *qdev_get_type(Object *obj, Error **errp)
2100{
2101 return g_strdup(object_get_typename(obj));
2102}
2103
Eric Blaked7bce992016-01-29 06:48:55 -07002104static void property_get_uint8_ptr(Object *obj, Visitor *v, const char *name,
2105 void *opaque, Error **errp)
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002106{
2107 uint8_t value = *(uint8_t *)opaque;
Eric Blake51e72bc2016-01-29 06:48:54 -07002108 visit_type_uint8(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_uint16_ptr(Object *obj, Visitor *v, const char *name,
2112 void *opaque, Error **errp)
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002113{
2114 uint16_t value = *(uint16_t *)opaque;
Eric Blake51e72bc2016-01-29 06:48:54 -07002115 visit_type_uint16(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_uint32_ptr(Object *obj, Visitor *v, const char *name,
2119 void *opaque, Error **errp)
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002120{
2121 uint32_t value = *(uint32_t *)opaque;
Eric Blake51e72bc2016-01-29 06:48:54 -07002122 visit_type_uint32(v, name, &value, errp);
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002123}
2124
Eric Blaked7bce992016-01-29 06:48:55 -07002125static void property_get_uint64_ptr(Object *obj, Visitor *v, const char *name,
2126 void *opaque, Error **errp)
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002127{
2128 uint64_t value = *(uint64_t *)opaque;
Eric Blake51e72bc2016-01-29 06:48:54 -07002129 visit_type_uint64(v, name, &value, errp);
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002130}
2131
2132void object_property_add_uint8_ptr(Object *obj, const char *name,
2133 const uint8_t *v, Error **errp)
2134{
2135 object_property_add(obj, name, "uint8", property_get_uint8_ptr,
2136 NULL, NULL, (void *)v, errp);
2137}
2138
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002139void object_class_property_add_uint8_ptr(ObjectClass *klass, const char *name,
2140 const uint8_t *v, Error **errp)
2141{
2142 object_class_property_add(klass, name, "uint8", property_get_uint8_ptr,
2143 NULL, NULL, (void *)v, errp);
2144}
2145
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002146void object_property_add_uint16_ptr(Object *obj, const char *name,
2147 const uint16_t *v, Error **errp)
2148{
2149 object_property_add(obj, name, "uint16", property_get_uint16_ptr,
2150 NULL, NULL, (void *)v, errp);
2151}
2152
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002153void object_class_property_add_uint16_ptr(ObjectClass *klass, const char *name,
2154 const uint16_t *v, Error **errp)
2155{
2156 object_class_property_add(klass, name, "uint16", property_get_uint16_ptr,
2157 NULL, NULL, (void *)v, errp);
2158}
2159
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002160void object_property_add_uint32_ptr(Object *obj, const char *name,
2161 const uint32_t *v, Error **errp)
2162{
2163 object_property_add(obj, name, "uint32", property_get_uint32_ptr,
2164 NULL, NULL, (void *)v, errp);
2165}
2166
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002167void object_class_property_add_uint32_ptr(ObjectClass *klass, const char *name,
2168 const uint32_t *v, Error **errp)
2169{
2170 object_class_property_add(klass, name, "uint32", property_get_uint32_ptr,
2171 NULL, NULL, (void *)v, errp);
2172}
2173
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03002174void object_property_add_uint64_ptr(Object *obj, const char *name,
2175 const uint64_t *v, Error **errp)
2176{
2177 object_property_add(obj, name, "uint64", property_get_uint64_ptr,
2178 NULL, NULL, (void *)v, errp);
2179}
2180
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002181void object_class_property_add_uint64_ptr(ObjectClass *klass, const char *name,
2182 const uint64_t *v, Error **errp)
2183{
2184 object_class_property_add(klass, name, "uint64", property_get_uint64_ptr,
2185 NULL, NULL, (void *)v, errp);
2186}
2187
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002188typedef struct {
2189 Object *target_obj;
Eduardo Habkost1590d262015-04-09 16:57:29 -03002190 char *target_name;
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002191} AliasProperty;
2192
Eric Blaked7bce992016-01-29 06:48:55 -07002193static void property_get_alias(Object *obj, Visitor *v, const char *name,
2194 void *opaque, Error **errp)
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002195{
2196 AliasProperty *prop = opaque;
2197
2198 object_property_get(prop->target_obj, v, prop->target_name, errp);
2199}
2200
Eric Blaked7bce992016-01-29 06:48:55 -07002201static void property_set_alias(Object *obj, Visitor *v, const char *name,
2202 void *opaque, Error **errp)
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002203{
2204 AliasProperty *prop = opaque;
2205
2206 object_property_set(prop->target_obj, v, prop->target_name, errp);
2207}
2208
Paolo Bonzini64607d02014-06-05 13:11:51 +02002209static Object *property_resolve_alias(Object *obj, void *opaque,
2210 const gchar *part)
2211{
2212 AliasProperty *prop = opaque;
2213
2214 return object_resolve_path_component(prop->target_obj, prop->target_name);
2215}
2216
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002217static void property_release_alias(Object *obj, const char *name, void *opaque)
2218{
2219 AliasProperty *prop = opaque;
2220
Eduardo Habkost1590d262015-04-09 16:57:29 -03002221 g_free(prop->target_name);
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002222 g_free(prop);
2223}
2224
2225void object_property_add_alias(Object *obj, const char *name,
2226 Object *target_obj, const char *target_name,
2227 Error **errp)
2228{
2229 AliasProperty *prop;
Paolo Bonzini64607d02014-06-05 13:11:51 +02002230 ObjectProperty *op;
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002231 ObjectProperty *target_prop;
Paolo Bonzinid1906982014-06-10 11:17:35 +02002232 gchar *prop_type;
Gonglei8ae9a9e2014-09-27 13:13:56 +08002233 Error *local_err = NULL;
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002234
2235 target_prop = object_property_find(target_obj, target_name, errp);
2236 if (!target_prop) {
2237 return;
2238 }
2239
Paolo Bonzinid1906982014-06-10 11:17:35 +02002240 if (object_property_is_child(target_prop)) {
2241 prop_type = g_strdup_printf("link%s",
2242 target_prop->type + strlen("child"));
2243 } else {
2244 prop_type = g_strdup(target_prop->type);
2245 }
2246
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002247 prop = g_malloc(sizeof(*prop));
2248 prop->target_obj = target_obj;
Eduardo Habkost1590d262015-04-09 16:57:29 -03002249 prop->target_name = g_strdup(target_name);
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002250
Paolo Bonzinid1906982014-06-10 11:17:35 +02002251 op = object_property_add(obj, name, prop_type,
Paolo Bonzini64607d02014-06-05 13:11:51 +02002252 property_get_alias,
2253 property_set_alias,
2254 property_release_alias,
Gonglei8ae9a9e2014-09-27 13:13:56 +08002255 prop, &local_err);
2256 if (local_err) {
2257 error_propagate(errp, local_err);
2258 g_free(prop);
2259 goto out;
2260 }
Paolo Bonzini64607d02014-06-05 13:11:51 +02002261 op->resolve = property_resolve_alias;
Paolo Bonzinid1906982014-06-10 11:17:35 +02002262
Andreas Färbera18bb412015-03-27 17:34:10 +01002263 object_property_set_description(obj, op->name,
Gonglei80742642014-10-07 14:33:21 +08002264 target_prop->description,
2265 &error_abort);
2266
Gonglei8ae9a9e2014-09-27 13:13:56 +08002267out:
Paolo Bonzinid1906982014-06-10 11:17:35 +02002268 g_free(prop_type);
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08002269}
2270
Gonglei80742642014-10-07 14:33:21 +08002271void object_property_set_description(Object *obj, const char *name,
2272 const char *description, Error **errp)
2273{
2274 ObjectProperty *op;
2275
2276 op = object_property_find(obj, name, errp);
2277 if (!op) {
2278 return;
2279 }
2280
2281 g_free(op->description);
2282 op->description = g_strdup(description);
2283}
2284
Daniel P. Berrange16bf7f52015-10-13 13:37:46 +01002285void object_class_property_set_description(ObjectClass *klass,
2286 const char *name,
2287 const char *description,
2288 Error **errp)
2289{
2290 ObjectProperty *op;
2291
2292 op = g_hash_table_lookup(klass->properties, name);
2293 if (!op) {
2294 error_setg(errp, "Property '.%s' not found", name);
2295 return;
2296 }
2297
2298 g_free(op->description);
2299 op->description = g_strdup(description);
2300}
2301
Paolo Bonzini2f262e02012-04-02 17:33:51 +02002302static void object_instance_init(Object *obj)
2303{
2304 object_property_add_str(obj, "type", qdev_get_type, NULL, NULL);
2305}
2306
Paolo Bonzini745549c2012-03-31 16:45:54 +02002307static void register_types(void)
2308{
2309 static TypeInfo interface_info = {
2310 .name = TYPE_INTERFACE,
Anthony Liguori33e95c62012-08-10 13:16:10 +10002311 .class_size = sizeof(InterfaceClass),
Paolo Bonzini745549c2012-03-31 16:45:54 +02002312 .abstract = true,
2313 };
2314
2315 static TypeInfo object_info = {
2316 .name = TYPE_OBJECT,
2317 .instance_size = sizeof(Object),
Paolo Bonzini2f262e02012-04-02 17:33:51 +02002318 .instance_init = object_instance_init,
Paolo Bonzini745549c2012-03-31 16:45:54 +02002319 .abstract = true,
2320 };
2321
Paolo Bonzini049cb3c2012-04-04 15:58:40 +02002322 type_interface = type_register_internal(&interface_info);
2323 type_register_internal(&object_info);
Paolo Bonzini745549c2012-03-31 16:45:54 +02002324}
2325
2326type_init(register_types)