]> git.proxmox.com Git - qemu.git/blob - qom/object.c
qom: Assert that public types have a non-NULL parent field
[qemu.git] / qom / object.c
1 /*
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
13 #include "qemu/object.h"
14 #include "qemu-common.h"
15 #include "qapi/qapi-visit-core.h"
16 #include "qapi/string-input-visitor.h"
17 #include "qapi/string-output-visitor.h"
18
19 /* TODO: replace QObject with a simpler visitor to avoid a dependency
20 * of the QOM core on QObject? */
21 #include "qemu/qom-qobject.h"
22 #include "qobject.h"
23 #include "qbool.h"
24 #include "qint.h"
25 #include "qstring.h"
26
27 #define MAX_INTERFACES 32
28
29 typedef struct InterfaceImpl InterfaceImpl;
30 typedef struct TypeImpl TypeImpl;
31
32 struct InterfaceImpl
33 {
34 const char *parent;
35 void (*interface_initfn)(ObjectClass *class, void *data);
36 TypeImpl *type;
37 };
38
39 struct TypeImpl
40 {
41 const char *name;
42
43 size_t class_size;
44
45 size_t instance_size;
46
47 void (*class_init)(ObjectClass *klass, void *data);
48 void (*class_base_init)(ObjectClass *klass, void *data);
49 void (*class_finalize)(ObjectClass *klass, void *data);
50
51 void *class_data;
52
53 void (*instance_init)(Object *obj);
54 void (*instance_finalize)(Object *obj);
55
56 bool abstract;
57
58 const char *parent;
59 TypeImpl *parent_type;
60
61 ObjectClass *class;
62
63 int num_interfaces;
64 InterfaceImpl interfaces[MAX_INTERFACES];
65 };
66
67 typedef struct Interface
68 {
69 Object parent;
70 Object *obj;
71 } Interface;
72
73 #define INTERFACE(obj) OBJECT_CHECK(Interface, obj, TYPE_INTERFACE)
74
75 static Type type_interface;
76
77 static GHashTable *type_table_get(void)
78 {
79 static GHashTable *type_table;
80
81 if (type_table == NULL) {
82 type_table = g_hash_table_new(g_str_hash, g_str_equal);
83 }
84
85 return type_table;
86 }
87
88 static void type_table_add(TypeImpl *ti)
89 {
90 g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
91 }
92
93 static TypeImpl *type_table_lookup(const char *name)
94 {
95 return g_hash_table_lookup(type_table_get(), name);
96 }
97
98 static TypeImpl *type_register_internal(const TypeInfo *info)
99 {
100 TypeImpl *ti = g_malloc0(sizeof(*ti));
101
102 g_assert(info->name != NULL);
103
104 if (type_table_lookup(info->name) != NULL) {
105 fprintf(stderr, "Registering `%s' which already exists\n", info->name);
106 abort();
107 }
108
109 ti->name = g_strdup(info->name);
110 ti->parent = g_strdup(info->parent);
111
112 ti->class_size = info->class_size;
113 ti->instance_size = info->instance_size;
114
115 ti->class_init = info->class_init;
116 ti->class_base_init = info->class_base_init;
117 ti->class_finalize = info->class_finalize;
118 ti->class_data = info->class_data;
119
120 ti->instance_init = info->instance_init;
121 ti->instance_finalize = info->instance_finalize;
122
123 ti->abstract = info->abstract;
124
125 if (info->interfaces) {
126 int i;
127
128 for (i = 0; info->interfaces[i].type; i++) {
129 ti->interfaces[i].parent = info->interfaces[i].type;
130 ti->interfaces[i].interface_initfn = info->interfaces[i].interface_initfn;
131 ti->num_interfaces++;
132 }
133 }
134
135 type_table_add(ti);
136
137 return ti;
138 }
139
140 TypeImpl *type_register(const TypeInfo *info)
141 {
142 assert(info->parent);
143 return type_register_internal(info);
144 }
145
146 TypeImpl *type_register_static(const TypeInfo *info)
147 {
148 return type_register(info);
149 }
150
151 static TypeImpl *type_get_by_name(const char *name)
152 {
153 if (name == NULL) {
154 return NULL;
155 }
156
157 return type_table_lookup(name);
158 }
159
160 static TypeImpl *type_get_parent(TypeImpl *type)
161 {
162 if (!type->parent_type && type->parent) {
163 type->parent_type = type_get_by_name(type->parent);
164 g_assert(type->parent_type != NULL);
165 }
166
167 return type->parent_type;
168 }
169
170 static bool type_has_parent(TypeImpl *type)
171 {
172 return (type->parent != NULL);
173 }
174
175 static size_t type_class_get_size(TypeImpl *ti)
176 {
177 if (ti->class_size) {
178 return ti->class_size;
179 }
180
181 if (type_has_parent(ti)) {
182 return type_class_get_size(type_get_parent(ti));
183 }
184
185 return sizeof(ObjectClass);
186 }
187
188 static size_t type_object_get_size(TypeImpl *ti)
189 {
190 if (ti->instance_size) {
191 return ti->instance_size;
192 }
193
194 if (type_has_parent(ti)) {
195 return type_object_get_size(type_get_parent(ti));
196 }
197
198 return 0;
199 }
200
201 static void type_class_interface_init(TypeImpl *ti, InterfaceImpl *iface)
202 {
203 TypeInfo info = {
204 .instance_size = sizeof(Interface),
205 .parent = iface->parent,
206 .class_size = sizeof(InterfaceClass),
207 .class_init = iface->interface_initfn,
208 .abstract = true,
209 };
210 char *name = g_strdup_printf("<%s::%s>", ti->name, iface->parent);
211
212 info.name = name;
213 iface->type = type_register_internal(&info);
214 g_free(name);
215 }
216
217 static void type_initialize(TypeImpl *ti)
218 {
219 TypeImpl *parent;
220 int i;
221
222 if (ti->class) {
223 return;
224 }
225
226 ti->class_size = type_class_get_size(ti);
227 ti->instance_size = type_object_get_size(ti);
228
229 ti->class = g_malloc0(ti->class_size);
230
231 parent = type_get_parent(ti);
232 if (parent) {
233 type_initialize(parent);
234
235 g_assert(parent->class_size <= ti->class_size);
236 memcpy(ti->class, parent->class, parent->class_size);
237 }
238
239 ti->class->type = ti;
240
241 while (parent) {
242 if (parent->class_base_init) {
243 parent->class_base_init(ti->class, ti->class_data);
244 }
245 parent = type_get_parent(parent);
246 }
247
248 for (i = 0; i < ti->num_interfaces; i++) {
249 type_class_interface_init(ti, &ti->interfaces[i]);
250 }
251
252 if (ti->class_init) {
253 ti->class_init(ti->class, ti->class_data);
254 }
255 }
256
257 static void object_interface_init(Object *obj, InterfaceImpl *iface)
258 {
259 TypeImpl *ti = iface->type;
260 Interface *iface_obj;
261
262 iface_obj = INTERFACE(object_new(ti->name));
263 iface_obj->obj = obj;
264
265 obj->interfaces = g_slist_prepend(obj->interfaces, iface_obj);
266 }
267
268 static void object_init_with_type(Object *obj, TypeImpl *ti)
269 {
270 int i;
271
272 if (type_has_parent(ti)) {
273 object_init_with_type(obj, type_get_parent(ti));
274 }
275
276 for (i = 0; i < ti->num_interfaces; i++) {
277 object_interface_init(obj, &ti->interfaces[i]);
278 }
279
280 if (ti->instance_init) {
281 ti->instance_init(obj);
282 }
283 }
284
285 void object_initialize_with_type(void *data, TypeImpl *type)
286 {
287 Object *obj = data;
288
289 g_assert(type != NULL);
290 type_initialize(type);
291
292 g_assert(type->instance_size >= sizeof(Object));
293 g_assert(type->abstract == false);
294
295 memset(obj, 0, type->instance_size);
296 obj->class = type->class;
297 QTAILQ_INIT(&obj->properties);
298 object_init_with_type(obj, type);
299 }
300
301 void object_initialize(void *data, const char *typename)
302 {
303 TypeImpl *type = type_get_by_name(typename);
304
305 object_initialize_with_type(data, type);
306 }
307
308 static inline bool object_property_is_child(ObjectProperty *prop)
309 {
310 return strstart(prop->type, "child<", NULL);
311 }
312
313 static inline bool object_property_is_link(ObjectProperty *prop)
314 {
315 return strstart(prop->type, "link<", NULL);
316 }
317
318 static void object_property_del_all(Object *obj)
319 {
320 while (!QTAILQ_EMPTY(&obj->properties)) {
321 ObjectProperty *prop = QTAILQ_FIRST(&obj->properties);
322
323 QTAILQ_REMOVE(&obj->properties, prop, node);
324
325 if (prop->release) {
326 prop->release(obj, prop->name, prop->opaque);
327 }
328
329 g_free(prop->name);
330 g_free(prop->type);
331 g_free(prop);
332 }
333 }
334
335 static void object_property_del_child(Object *obj, Object *child, Error **errp)
336 {
337 ObjectProperty *prop;
338
339 QTAILQ_FOREACH(prop, &obj->properties, node) {
340 if (object_property_is_child(prop) && prop->opaque == child) {
341 object_property_del(obj, prop->name, errp);
342 break;
343 }
344 }
345 }
346
347 void object_unparent(Object *obj)
348 {
349 if (obj->parent) {
350 object_property_del_child(obj->parent, obj, NULL);
351 }
352 }
353
354 static void object_deinit(Object *obj, TypeImpl *type)
355 {
356 if (type->instance_finalize) {
357 type->instance_finalize(obj);
358 }
359
360 while (obj->interfaces) {
361 Interface *iface_obj = obj->interfaces->data;
362 obj->interfaces = g_slist_delete_link(obj->interfaces, obj->interfaces);
363 object_delete(OBJECT(iface_obj));
364 }
365
366 if (type_has_parent(type)) {
367 object_deinit(obj, type_get_parent(type));
368 }
369
370 object_unparent(obj);
371 }
372
373 void object_finalize(void *data)
374 {
375 Object *obj = data;
376 TypeImpl *ti = obj->class->type;
377
378 object_deinit(obj, ti);
379 object_property_del_all(obj);
380
381 g_assert(obj->ref == 0);
382 }
383
384 Object *object_new_with_type(Type type)
385 {
386 Object *obj;
387
388 g_assert(type != NULL);
389 type_initialize(type);
390
391 obj = g_malloc(type->instance_size);
392 object_initialize_with_type(obj, type);
393 object_ref(obj);
394
395 return obj;
396 }
397
398 Object *object_new(const char *typename)
399 {
400 TypeImpl *ti = type_get_by_name(typename);
401
402 return object_new_with_type(ti);
403 }
404
405 void object_delete(Object *obj)
406 {
407 object_unref(obj);
408 g_assert(obj->ref == 0);
409 g_free(obj);
410 }
411
412 static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
413 {
414 assert(target_type);
415
416 /* Check if typename is a direct ancestor of type */
417 while (type) {
418 if (type == target_type) {
419 return true;
420 }
421
422 type = type_get_parent(type);
423 }
424
425 return false;
426 }
427
428 static bool object_is_type(Object *obj, TypeImpl *target_type)
429 {
430 return !target_type || type_is_ancestor(obj->class->type, target_type);
431 }
432
433 Object *object_dynamic_cast(Object *obj, const char *typename)
434 {
435 TypeImpl *target_type = type_get_by_name(typename);
436 GSList *i;
437
438 /* Check if typename is a direct ancestor. Special-case TYPE_OBJECT,
439 * we want to go back from interfaces to the parent.
440 */
441 if (target_type && object_is_type(obj, target_type)) {
442 return obj;
443 }
444
445 /* Check if obj is an interface and its containing object is a direct
446 * ancestor of typename. In principle we could do this test at the very
447 * beginning of object_dynamic_cast, avoiding a second call to
448 * object_is_type. However, casting between interfaces is relatively
449 * rare, and object_is_type(obj, type_interface) would fail almost always.
450 *
451 * Perhaps we could add a magic value to the object header for increased
452 * (run-time) type safety and to speed up tests like this one. If we ever
453 * do that we can revisit the order here.
454 */
455 if (object_is_type(obj, type_interface)) {
456 assert(!obj->interfaces);
457 obj = INTERFACE(obj)->obj;
458 if (object_is_type(obj, target_type)) {
459 return obj;
460 }
461 }
462
463 if (!target_type) {
464 return obj;
465 }
466
467 /* Check if obj has an interface of typename */
468 for (i = obj->interfaces; i; i = i->next) {
469 Interface *iface = i->data;
470
471 if (object_is_type(OBJECT(iface), target_type)) {
472 return OBJECT(iface);
473 }
474 }
475
476 return NULL;
477 }
478
479
480 Object *object_dynamic_cast_assert(Object *obj, const char *typename)
481 {
482 Object *inst;
483
484 inst = object_dynamic_cast(obj, typename);
485
486 if (!inst) {
487 fprintf(stderr, "Object %p is not an instance of type %s\n",
488 obj, typename);
489 abort();
490 }
491
492 return inst;
493 }
494
495 ObjectClass *object_class_dynamic_cast(ObjectClass *class,
496 const char *typename)
497 {
498 TypeImpl *target_type = type_get_by_name(typename);
499 TypeImpl *type = class->type;
500
501 while (type) {
502 if (type == target_type) {
503 return class;
504 }
505
506 type = type_get_parent(type);
507 }
508
509 return NULL;
510 }
511
512 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
513 const char *typename)
514 {
515 ObjectClass *ret = object_class_dynamic_cast(class, typename);
516
517 if (!ret) {
518 fprintf(stderr, "Object %p is not an instance of type %s\n",
519 class, typename);
520 abort();
521 }
522
523 return ret;
524 }
525
526 const char *object_get_typename(Object *obj)
527 {
528 return obj->class->type->name;
529 }
530
531 ObjectClass *object_get_class(Object *obj)
532 {
533 return obj->class;
534 }
535
536 const char *object_class_get_name(ObjectClass *klass)
537 {
538 return klass->type->name;
539 }
540
541 ObjectClass *object_class_by_name(const char *typename)
542 {
543 TypeImpl *type = type_get_by_name(typename);
544
545 if (!type) {
546 return NULL;
547 }
548
549 type_initialize(type);
550
551 return type->class;
552 }
553
554 ObjectClass *object_class_get_parent(ObjectClass *class)
555 {
556 TypeImpl *type = type_get_parent(class->type);
557
558 if (!type) {
559 return NULL;
560 }
561
562 type_initialize(type);
563
564 return type->class;
565 }
566
567 typedef struct OCFData
568 {
569 void (*fn)(ObjectClass *klass, void *opaque);
570 const char *implements_type;
571 bool include_abstract;
572 void *opaque;
573 } OCFData;
574
575 static void object_class_foreach_tramp(gpointer key, gpointer value,
576 gpointer opaque)
577 {
578 OCFData *data = opaque;
579 TypeImpl *type = value;
580 ObjectClass *k;
581
582 type_initialize(type);
583 k = type->class;
584
585 if (!data->include_abstract && type->abstract) {
586 return;
587 }
588
589 if (data->implements_type &&
590 !object_class_dynamic_cast(k, data->implements_type)) {
591 return;
592 }
593
594 data->fn(k, data->opaque);
595 }
596
597 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
598 const char *implements_type, bool include_abstract,
599 void *opaque)
600 {
601 OCFData data = { fn, implements_type, include_abstract, opaque };
602
603 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
604 }
605
606 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
607 void *opaque)
608 {
609 ObjectProperty *prop;
610 int ret = 0;
611
612 QTAILQ_FOREACH(prop, &obj->properties, node) {
613 if (object_property_is_child(prop)) {
614 ret = fn(prop->opaque, opaque);
615 if (ret != 0) {
616 break;
617 }
618 }
619 }
620 return ret;
621 }
622
623 static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
624 {
625 GSList **list = opaque;
626
627 *list = g_slist_prepend(*list, klass);
628 }
629
630 GSList *object_class_get_list(const char *implements_type,
631 bool include_abstract)
632 {
633 GSList *list = NULL;
634
635 object_class_foreach(object_class_get_list_tramp,
636 implements_type, include_abstract, &list);
637 return list;
638 }
639
640 void object_ref(Object *obj)
641 {
642 obj->ref++;
643 }
644
645 void object_unref(Object *obj)
646 {
647 g_assert(obj->ref > 0);
648 obj->ref--;
649
650 /* parent always holds a reference to its children */
651 if (obj->ref == 0) {
652 object_finalize(obj);
653 }
654 }
655
656 void object_property_add(Object *obj, const char *name, const char *type,
657 ObjectPropertyAccessor *get,
658 ObjectPropertyAccessor *set,
659 ObjectPropertyRelease *release,
660 void *opaque, Error **errp)
661 {
662 ObjectProperty *prop = g_malloc0(sizeof(*prop));
663
664 prop->name = g_strdup(name);
665 prop->type = g_strdup(type);
666
667 prop->get = get;
668 prop->set = set;
669 prop->release = release;
670 prop->opaque = opaque;
671
672 QTAILQ_INSERT_TAIL(&obj->properties, prop, node);
673 }
674
675 static ObjectProperty *object_property_find(Object *obj, const char *name)
676 {
677 ObjectProperty *prop;
678
679 QTAILQ_FOREACH(prop, &obj->properties, node) {
680 if (strcmp(prop->name, name) == 0) {
681 return prop;
682 }
683 }
684
685 return NULL;
686 }
687
688 void object_property_del(Object *obj, const char *name, Error **errp)
689 {
690 ObjectProperty *prop = object_property_find(obj, name);
691
692 QTAILQ_REMOVE(&obj->properties, prop, node);
693
694 prop->release(obj, prop->name, prop->opaque);
695
696 g_free(prop->name);
697 g_free(prop->type);
698 g_free(prop);
699 }
700
701 void object_property_get(Object *obj, Visitor *v, const char *name,
702 Error **errp)
703 {
704 ObjectProperty *prop = object_property_find(obj, name);
705
706 if (prop == NULL) {
707 error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name);
708 return;
709 }
710
711 if (!prop->get) {
712 error_set(errp, QERR_PERMISSION_DENIED);
713 } else {
714 prop->get(obj, v, prop->opaque, name, errp);
715 }
716 }
717
718 void object_property_set(Object *obj, Visitor *v, const char *name,
719 Error **errp)
720 {
721 ObjectProperty *prop = object_property_find(obj, name);
722
723 if (prop == NULL) {
724 error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name);
725 return;
726 }
727
728 if (!prop->set) {
729 error_set(errp, QERR_PERMISSION_DENIED);
730 } else {
731 prop->set(obj, v, prop->opaque, name, errp);
732 }
733 }
734
735 void object_property_set_str(Object *obj, const char *value,
736 const char *name, Error **errp)
737 {
738 QString *qstr = qstring_from_str(value);
739 object_property_set_qobject(obj, QOBJECT(qstr), name, errp);
740
741 QDECREF(qstr);
742 }
743
744 char *object_property_get_str(Object *obj, const char *name,
745 Error **errp)
746 {
747 QObject *ret = object_property_get_qobject(obj, name, errp);
748 QString *qstring;
749 char *retval;
750
751 if (!ret) {
752 return NULL;
753 }
754 qstring = qobject_to_qstring(ret);
755 if (!qstring) {
756 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
757 retval = NULL;
758 } else {
759 retval = g_strdup(qstring_get_str(qstring));
760 }
761
762 QDECREF(qstring);
763 return retval;
764 }
765
766 void object_property_set_link(Object *obj, Object *value,
767 const char *name, Error **errp)
768 {
769 object_property_set_str(obj, object_get_canonical_path(value),
770 name, errp);
771 }
772
773 Object *object_property_get_link(Object *obj, const char *name,
774 Error **errp)
775 {
776 char *str = object_property_get_str(obj, name, errp);
777 Object *target = NULL;
778
779 if (str && *str) {
780 target = object_resolve_path(str, NULL);
781 if (!target) {
782 error_set(errp, QERR_DEVICE_NOT_FOUND, str);
783 }
784 }
785
786 g_free(str);
787 return target;
788 }
789
790 void object_property_set_bool(Object *obj, bool value,
791 const char *name, Error **errp)
792 {
793 QBool *qbool = qbool_from_int(value);
794 object_property_set_qobject(obj, QOBJECT(qbool), name, errp);
795
796 QDECREF(qbool);
797 }
798
799 bool object_property_get_bool(Object *obj, const char *name,
800 Error **errp)
801 {
802 QObject *ret = object_property_get_qobject(obj, name, errp);
803 QBool *qbool;
804 bool retval;
805
806 if (!ret) {
807 return false;
808 }
809 qbool = qobject_to_qbool(ret);
810 if (!qbool) {
811 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
812 retval = false;
813 } else {
814 retval = qbool_get_int(qbool);
815 }
816
817 QDECREF(qbool);
818 return retval;
819 }
820
821 void object_property_set_int(Object *obj, int64_t value,
822 const char *name, Error **errp)
823 {
824 QInt *qint = qint_from_int(value);
825 object_property_set_qobject(obj, QOBJECT(qint), name, errp);
826
827 QDECREF(qint);
828 }
829
830 int64_t object_property_get_int(Object *obj, const char *name,
831 Error **errp)
832 {
833 QObject *ret = object_property_get_qobject(obj, name, errp);
834 QInt *qint;
835 int64_t retval;
836
837 if (!ret) {
838 return -1;
839 }
840 qint = qobject_to_qint(ret);
841 if (!qint) {
842 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
843 retval = -1;
844 } else {
845 retval = qint_get_int(qint);
846 }
847
848 QDECREF(qint);
849 return retval;
850 }
851
852 void object_property_parse(Object *obj, const char *string,
853 const char *name, Error **errp)
854 {
855 StringInputVisitor *mi;
856 mi = string_input_visitor_new(string);
857 object_property_set(obj, string_input_get_visitor(mi), name, errp);
858
859 string_input_visitor_cleanup(mi);
860 }
861
862 char *object_property_print(Object *obj, const char *name,
863 Error **errp)
864 {
865 StringOutputVisitor *mo;
866 char *string;
867
868 mo = string_output_visitor_new();
869 object_property_get(obj, string_output_get_visitor(mo), name, errp);
870 string = string_output_get_string(mo);
871 string_output_visitor_cleanup(mo);
872 return string;
873 }
874
875 const char *object_property_get_type(Object *obj, const char *name, Error **errp)
876 {
877 ObjectProperty *prop = object_property_find(obj, name);
878
879 if (prop == NULL) {
880 error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name);
881 return NULL;
882 }
883
884 return prop->type;
885 }
886
887 Object *object_get_root(void)
888 {
889 static Object *root;
890
891 if (!root) {
892 root = object_new("container");
893 }
894
895 return root;
896 }
897
898 static void object_get_child_property(Object *obj, Visitor *v, void *opaque,
899 const char *name, Error **errp)
900 {
901 Object *child = opaque;
902 gchar *path;
903
904 path = object_get_canonical_path(child);
905 visit_type_str(v, &path, name, errp);
906 g_free(path);
907 }
908
909 static void object_finalize_child_property(Object *obj, const char *name,
910 void *opaque)
911 {
912 Object *child = opaque;
913
914 object_unref(child);
915 }
916
917 void object_property_add_child(Object *obj, const char *name,
918 Object *child, Error **errp)
919 {
920 gchar *type;
921
922 /* Registering an interface object in the composition tree will mightily
923 * confuse object_get_canonical_path (which, on the other hand, knows how
924 * to get the canonical path of an interface object).
925 */
926 assert(!object_is_type(obj, type_interface));
927
928 type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child)));
929
930 object_property_add(obj, name, type, object_get_child_property,
931 NULL, object_finalize_child_property, child, errp);
932
933 object_ref(child);
934 g_assert(child->parent == NULL);
935 child->parent = obj;
936
937 g_free(type);
938 }
939
940 static void object_get_link_property(Object *obj, Visitor *v, void *opaque,
941 const char *name, Error **errp)
942 {
943 Object **child = opaque;
944 gchar *path;
945
946 if (*child) {
947 path = object_get_canonical_path(*child);
948 visit_type_str(v, &path, name, errp);
949 g_free(path);
950 } else {
951 path = (gchar *)"";
952 visit_type_str(v, &path, name, errp);
953 }
954 }
955
956 static void object_set_link_property(Object *obj, Visitor *v, void *opaque,
957 const char *name, Error **errp)
958 {
959 Object **child = opaque;
960 Object *old_target;
961 bool ambiguous = false;
962 const char *type;
963 char *path;
964 gchar *target_type;
965
966 type = object_property_get_type(obj, name, NULL);
967
968 visit_type_str(v, &path, name, errp);
969
970 old_target = *child;
971 *child = NULL;
972
973 if (strcmp(path, "") != 0) {
974 Object *target;
975
976 /* Go from link<FOO> to FOO. */
977 target_type = g_strndup(&type[5], strlen(type) - 6);
978 target = object_resolve_path_type(path, target_type, &ambiguous);
979
980 if (ambiguous) {
981 error_set(errp, QERR_AMBIGUOUS_PATH, path);
982 } else if (target) {
983 object_ref(target);
984 *child = target;
985 } else {
986 target = object_resolve_path(path, &ambiguous);
987 if (target || ambiguous) {
988 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
989 } else {
990 error_set(errp, QERR_DEVICE_NOT_FOUND, path);
991 }
992 }
993 g_free(target_type);
994 }
995
996 g_free(path);
997
998 if (old_target != NULL) {
999 object_unref(old_target);
1000 }
1001 }
1002
1003 void object_property_add_link(Object *obj, const char *name,
1004 const char *type, Object **child,
1005 Error **errp)
1006 {
1007 gchar *full_type;
1008
1009 full_type = g_strdup_printf("link<%s>", type);
1010
1011 object_property_add(obj, name, full_type,
1012 object_get_link_property,
1013 object_set_link_property,
1014 NULL, child, errp);
1015
1016 g_free(full_type);
1017 }
1018
1019 gchar *object_get_canonical_path(Object *obj)
1020 {
1021 Object *root = object_get_root();
1022 char *newpath = NULL, *path = NULL;
1023
1024 if (object_is_type(obj, type_interface)) {
1025 obj = INTERFACE(obj)->obj;
1026 }
1027
1028 while (obj != root) {
1029 ObjectProperty *prop = NULL;
1030
1031 g_assert(obj->parent != NULL);
1032
1033 QTAILQ_FOREACH(prop, &obj->parent->properties, node) {
1034 if (!object_property_is_child(prop)) {
1035 continue;
1036 }
1037
1038 if (prop->opaque == obj) {
1039 if (path) {
1040 newpath = g_strdup_printf("%s/%s", prop->name, path);
1041 g_free(path);
1042 path = newpath;
1043 } else {
1044 path = g_strdup(prop->name);
1045 }
1046 break;
1047 }
1048 }
1049
1050 g_assert(prop != NULL);
1051
1052 obj = obj->parent;
1053 }
1054
1055 newpath = g_strdup_printf("/%s", path);
1056 g_free(path);
1057
1058 return newpath;
1059 }
1060
1061 Object *object_resolve_path_component(Object *parent, gchar *part)
1062 {
1063 ObjectProperty *prop = object_property_find(parent, part);
1064 if (prop == NULL) {
1065 return NULL;
1066 }
1067
1068 if (object_property_is_link(prop)) {
1069 return *(Object **)prop->opaque;
1070 } else if (object_property_is_child(prop)) {
1071 return prop->opaque;
1072 } else {
1073 return NULL;
1074 }
1075 }
1076
1077 static Object *object_resolve_abs_path(Object *parent,
1078 gchar **parts,
1079 const char *typename,
1080 int index)
1081 {
1082 Object *child;
1083
1084 if (parts[index] == NULL) {
1085 return object_dynamic_cast(parent, typename);
1086 }
1087
1088 if (strcmp(parts[index], "") == 0) {
1089 return object_resolve_abs_path(parent, parts, typename, index + 1);
1090 }
1091
1092 child = object_resolve_path_component(parent, parts[index]);
1093 if (!child) {
1094 return NULL;
1095 }
1096
1097 return object_resolve_abs_path(child, parts, typename, index + 1);
1098 }
1099
1100 static Object *object_resolve_partial_path(Object *parent,
1101 gchar **parts,
1102 const char *typename,
1103 bool *ambiguous)
1104 {
1105 Object *obj;
1106 ObjectProperty *prop;
1107
1108 obj = object_resolve_abs_path(parent, parts, typename, 0);
1109
1110 QTAILQ_FOREACH(prop, &parent->properties, node) {
1111 Object *found;
1112
1113 if (!object_property_is_child(prop)) {
1114 continue;
1115 }
1116
1117 found = object_resolve_partial_path(prop->opaque, parts,
1118 typename, ambiguous);
1119 if (found) {
1120 if (obj) {
1121 if (ambiguous) {
1122 *ambiguous = true;
1123 }
1124 return NULL;
1125 }
1126 obj = found;
1127 }
1128
1129 if (ambiguous && *ambiguous) {
1130 return NULL;
1131 }
1132 }
1133
1134 return obj;
1135 }
1136
1137 Object *object_resolve_path_type(const char *path, const char *typename,
1138 bool *ambiguous)
1139 {
1140 bool partial_path = true;
1141 Object *obj;
1142 gchar **parts;
1143
1144 parts = g_strsplit(path, "/", 0);
1145 if (parts == NULL || parts[0] == NULL) {
1146 g_strfreev(parts);
1147 return object_get_root();
1148 }
1149
1150 if (strcmp(parts[0], "") == 0) {
1151 partial_path = false;
1152 }
1153
1154 if (partial_path) {
1155 if (ambiguous) {
1156 *ambiguous = false;
1157 }
1158 obj = object_resolve_partial_path(object_get_root(), parts,
1159 typename, ambiguous);
1160 } else {
1161 obj = object_resolve_abs_path(object_get_root(), parts, typename, 1);
1162 }
1163
1164 g_strfreev(parts);
1165
1166 return obj;
1167 }
1168
1169 Object *object_resolve_path(const char *path, bool *ambiguous)
1170 {
1171 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
1172 }
1173
1174 typedef struct StringProperty
1175 {
1176 char *(*get)(Object *, Error **);
1177 void (*set)(Object *, const char *, Error **);
1178 } StringProperty;
1179
1180 static void property_get_str(Object *obj, Visitor *v, void *opaque,
1181 const char *name, Error **errp)
1182 {
1183 StringProperty *prop = opaque;
1184 char *value;
1185
1186 value = prop->get(obj, errp);
1187 if (value) {
1188 visit_type_str(v, &value, name, errp);
1189 g_free(value);
1190 }
1191 }
1192
1193 static void property_set_str(Object *obj, Visitor *v, void *opaque,
1194 const char *name, Error **errp)
1195 {
1196 StringProperty *prop = opaque;
1197 char *value;
1198 Error *local_err = NULL;
1199
1200 visit_type_str(v, &value, name, &local_err);
1201 if (local_err) {
1202 error_propagate(errp, local_err);
1203 return;
1204 }
1205
1206 prop->set(obj, value, errp);
1207 g_free(value);
1208 }
1209
1210 static void property_release_str(Object *obj, const char *name,
1211 void *opaque)
1212 {
1213 StringProperty *prop = opaque;
1214 g_free(prop);
1215 }
1216
1217 void object_property_add_str(Object *obj, const char *name,
1218 char *(*get)(Object *, Error **),
1219 void (*set)(Object *, const char *, Error **),
1220 Error **errp)
1221 {
1222 StringProperty *prop = g_malloc0(sizeof(*prop));
1223
1224 prop->get = get;
1225 prop->set = set;
1226
1227 object_property_add(obj, name, "string",
1228 get ? property_get_str : NULL,
1229 set ? property_set_str : NULL,
1230 property_release_str,
1231 prop, errp);
1232 }
1233
1234 static void register_types(void)
1235 {
1236 static TypeInfo interface_info = {
1237 .name = TYPE_INTERFACE,
1238 .instance_size = sizeof(Interface),
1239 .abstract = true,
1240 };
1241
1242 static TypeInfo object_info = {
1243 .name = TYPE_OBJECT,
1244 .instance_size = sizeof(Object),
1245 .abstract = true,
1246 };
1247
1248 type_interface = type_register_internal(&interface_info);
1249 type_register_internal(&object_info);
1250 }
1251
1252 type_init(register_types)