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