xref: /openbmc/qemu/qom/object.c (revision 98e56ae6)
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/osdep.h"
14 #include "qapi/error.h"
15 #include "qom/object.h"
16 #include "qom/object_interfaces.h"
17 #include "qemu/cutils.h"
18 #include "qapi/visitor.h"
19 #include "qapi/string-input-visitor.h"
20 #include "qapi/string-output-visitor.h"
21 #include "qapi/qapi-builtin-visit.h"
22 #include "qapi/qmp/qerror.h"
23 #include "trace.h"
24 
25 /* TODO: replace QObject with a simpler visitor to avoid a dependency
26  * of the QOM core on QObject?  */
27 #include "qom/qom-qobject.h"
28 #include "qapi/qmp/qbool.h"
29 #include "qapi/qmp/qnum.h"
30 #include "qapi/qmp/qstring.h"
31 
32 #define MAX_INTERFACES 32
33 
34 typedef struct InterfaceImpl InterfaceImpl;
35 typedef struct TypeImpl TypeImpl;
36 
37 struct InterfaceImpl
38 {
39     const char *typename;
40 };
41 
42 struct TypeImpl
43 {
44     const char *name;
45 
46     size_t class_size;
47 
48     size_t instance_size;
49 
50     void (*class_init)(ObjectClass *klass, void *data);
51     void (*class_base_init)(ObjectClass *klass, void *data);
52 
53     void *class_data;
54 
55     void (*instance_init)(Object *obj);
56     void (*instance_post_init)(Object *obj);
57     void (*instance_finalize)(Object *obj);
58 
59     bool abstract;
60 
61     const char *parent;
62     TypeImpl *parent_type;
63 
64     ObjectClass *class;
65 
66     int num_interfaces;
67     InterfaceImpl interfaces[MAX_INTERFACES];
68 };
69 
70 static Type type_interface;
71 
72 static GHashTable *type_table_get(void)
73 {
74     static GHashTable *type_table;
75 
76     if (type_table == NULL) {
77         type_table = g_hash_table_new(g_str_hash, g_str_equal);
78     }
79 
80     return type_table;
81 }
82 
83 static bool enumerating_types;
84 
85 static void type_table_add(TypeImpl *ti)
86 {
87     assert(!enumerating_types);
88     g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
89 }
90 
91 static TypeImpl *type_table_lookup(const char *name)
92 {
93     return g_hash_table_lookup(type_table_get(), name);
94 }
95 
96 static TypeImpl *type_new(const TypeInfo *info)
97 {
98     TypeImpl *ti = g_malloc0(sizeof(*ti));
99     int i;
100 
101     g_assert(info->name != NULL);
102 
103     if (type_table_lookup(info->name) != NULL) {
104         fprintf(stderr, "Registering `%s' which already exists\n", info->name);
105         abort();
106     }
107 
108     ti->name = g_strdup(info->name);
109     ti->parent = g_strdup(info->parent);
110 
111     ti->class_size = info->class_size;
112     ti->instance_size = info->instance_size;
113 
114     ti->class_init = info->class_init;
115     ti->class_base_init = info->class_base_init;
116     ti->class_data = info->class_data;
117 
118     ti->instance_init = info->instance_init;
119     ti->instance_post_init = info->instance_post_init;
120     ti->instance_finalize = info->instance_finalize;
121 
122     ti->abstract = info->abstract;
123 
124     for (i = 0; info->interfaces && info->interfaces[i].type; i++) {
125         ti->interfaces[i].typename = g_strdup(info->interfaces[i].type);
126     }
127     ti->num_interfaces = i;
128 
129     return ti;
130 }
131 
132 static TypeImpl *type_register_internal(const TypeInfo *info)
133 {
134     TypeImpl *ti;
135     ti = type_new(info);
136 
137     type_table_add(ti);
138     return ti;
139 }
140 
141 TypeImpl *type_register(const TypeInfo *info)
142 {
143     assert(info->parent);
144     return type_register_internal(info);
145 }
146 
147 TypeImpl *type_register_static(const TypeInfo *info)
148 {
149     return type_register(info);
150 }
151 
152 void type_register_static_array(const TypeInfo *infos, int nr_infos)
153 {
154     int i;
155 
156     for (i = 0; i < nr_infos; i++) {
157         type_register_static(&infos[i]);
158     }
159 }
160 
161 static TypeImpl *type_get_by_name(const char *name)
162 {
163     if (name == NULL) {
164         return NULL;
165     }
166 
167     return type_table_lookup(name);
168 }
169 
170 static TypeImpl *type_get_parent(TypeImpl *type)
171 {
172     if (!type->parent_type && type->parent) {
173         type->parent_type = type_get_by_name(type->parent);
174         g_assert(type->parent_type != NULL);
175     }
176 
177     return type->parent_type;
178 }
179 
180 static bool type_has_parent(TypeImpl *type)
181 {
182     return (type->parent != NULL);
183 }
184 
185 static size_t type_class_get_size(TypeImpl *ti)
186 {
187     if (ti->class_size) {
188         return ti->class_size;
189     }
190 
191     if (type_has_parent(ti)) {
192         return type_class_get_size(type_get_parent(ti));
193     }
194 
195     return sizeof(ObjectClass);
196 }
197 
198 static size_t type_object_get_size(TypeImpl *ti)
199 {
200     if (ti->instance_size) {
201         return ti->instance_size;
202     }
203 
204     if (type_has_parent(ti)) {
205         return type_object_get_size(type_get_parent(ti));
206     }
207 
208     return 0;
209 }
210 
211 size_t object_type_get_instance_size(const char *typename)
212 {
213     TypeImpl *type = type_get_by_name(typename);
214 
215     g_assert(type != NULL);
216     return type_object_get_size(type);
217 }
218 
219 static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
220 {
221     assert(target_type);
222 
223     /* Check if target_type is a direct ancestor of type */
224     while (type) {
225         if (type == target_type) {
226             return true;
227         }
228 
229         type = type_get_parent(type);
230     }
231 
232     return false;
233 }
234 
235 static void type_initialize(TypeImpl *ti);
236 
237 static void type_initialize_interface(TypeImpl *ti, TypeImpl *interface_type,
238                                       TypeImpl *parent_type)
239 {
240     InterfaceClass *new_iface;
241     TypeInfo info = { };
242     TypeImpl *iface_impl;
243 
244     info.parent = parent_type->name;
245     info.name = g_strdup_printf("%s::%s", ti->name, interface_type->name);
246     info.abstract = true;
247 
248     iface_impl = type_new(&info);
249     iface_impl->parent_type = parent_type;
250     type_initialize(iface_impl);
251     g_free((char *)info.name);
252 
253     new_iface = (InterfaceClass *)iface_impl->class;
254     new_iface->concrete_class = ti->class;
255     new_iface->interface_type = interface_type;
256 
257     ti->class->interfaces = g_slist_append(ti->class->interfaces,
258                                            iface_impl->class);
259 }
260 
261 static void object_property_free(gpointer data)
262 {
263     ObjectProperty *prop = data;
264 
265     g_free(prop->name);
266     g_free(prop->type);
267     g_free(prop->description);
268     g_free(prop);
269 }
270 
271 static void type_initialize(TypeImpl *ti)
272 {
273     TypeImpl *parent;
274 
275     if (ti->class) {
276         return;
277     }
278 
279     ti->class_size = type_class_get_size(ti);
280     ti->instance_size = type_object_get_size(ti);
281     /* Any type with zero instance_size is implicitly abstract.
282      * This means interface types are all abstract.
283      */
284     if (ti->instance_size == 0) {
285         ti->abstract = true;
286     }
287     if (type_is_ancestor(ti, type_interface)) {
288         assert(ti->instance_size == 0);
289         assert(ti->abstract);
290         assert(!ti->instance_init);
291         assert(!ti->instance_post_init);
292         assert(!ti->instance_finalize);
293         assert(!ti->num_interfaces);
294     }
295     ti->class = g_malloc0(ti->class_size);
296 
297     parent = type_get_parent(ti);
298     if (parent) {
299         type_initialize(parent);
300         GSList *e;
301         int i;
302 
303         g_assert(parent->class_size <= ti->class_size);
304         memcpy(ti->class, parent->class, parent->class_size);
305         ti->class->interfaces = NULL;
306         ti->class->properties = g_hash_table_new_full(
307             g_str_hash, g_str_equal, g_free, object_property_free);
308 
309         for (e = parent->class->interfaces; e; e = e->next) {
310             InterfaceClass *iface = e->data;
311             ObjectClass *klass = OBJECT_CLASS(iface);
312 
313             type_initialize_interface(ti, iface->interface_type, klass->type);
314         }
315 
316         for (i = 0; i < ti->num_interfaces; i++) {
317             TypeImpl *t = type_get_by_name(ti->interfaces[i].typename);
318             for (e = ti->class->interfaces; e; e = e->next) {
319                 TypeImpl *target_type = OBJECT_CLASS(e->data)->type;
320 
321                 if (type_is_ancestor(target_type, t)) {
322                     break;
323                 }
324             }
325 
326             if (e) {
327                 continue;
328             }
329 
330             type_initialize_interface(ti, t, t);
331         }
332     } else {
333         ti->class->properties = g_hash_table_new_full(
334             g_str_hash, g_str_equal, g_free, object_property_free);
335     }
336 
337     ti->class->type = ti;
338 
339     while (parent) {
340         if (parent->class_base_init) {
341             parent->class_base_init(ti->class, ti->class_data);
342         }
343         parent = type_get_parent(parent);
344     }
345 
346     if (ti->class_init) {
347         ti->class_init(ti->class, ti->class_data);
348     }
349 }
350 
351 static void object_init_with_type(Object *obj, TypeImpl *ti)
352 {
353     if (type_has_parent(ti)) {
354         object_init_with_type(obj, type_get_parent(ti));
355     }
356 
357     if (ti->instance_init) {
358         ti->instance_init(obj);
359     }
360 }
361 
362 static void object_post_init_with_type(Object *obj, TypeImpl *ti)
363 {
364     if (ti->instance_post_init) {
365         ti->instance_post_init(obj);
366     }
367 
368     if (type_has_parent(ti)) {
369         object_post_init_with_type(obj, type_get_parent(ti));
370     }
371 }
372 
373 void object_apply_global_props(Object *obj, const GPtrArray *props, Error **errp)
374 {
375     Error *err = NULL;
376     int i;
377 
378     if (!props) {
379         return;
380     }
381 
382     for (i = 0; i < props->len; i++) {
383         GlobalProperty *p = g_ptr_array_index(props, i);
384 
385         if (object_dynamic_cast(obj, p->driver) == NULL) {
386             continue;
387         }
388         p->used = true;
389         object_property_parse(obj, p->value, p->property, &err);
390         if (err != NULL) {
391             error_prepend(&err, "can't apply global %s.%s=%s: ",
392                           p->driver, p->property, p->value);
393             /*
394              * If errp != NULL, propagate error and return.
395              * If errp == NULL, report a warning, but keep going
396              * with the remaining globals.
397              */
398             if (errp) {
399                 error_propagate(errp, err);
400                 return;
401             } else {
402                 warn_report_err(err);
403             }
404         }
405     }
406 }
407 
408 static void object_initialize_with_type(void *data, size_t size, TypeImpl *type)
409 {
410     Object *obj = data;
411 
412     g_assert(type != NULL);
413     type_initialize(type);
414 
415     g_assert(type->instance_size >= sizeof(Object));
416     g_assert(type->abstract == false);
417     g_assert(size >= type->instance_size);
418 
419     memset(obj, 0, type->instance_size);
420     obj->class = type->class;
421     object_ref(obj);
422     obj->properties = g_hash_table_new_full(g_str_hash, g_str_equal,
423                                             NULL, object_property_free);
424     object_init_with_type(obj, type);
425     object_post_init_with_type(obj, type);
426 }
427 
428 void object_initialize(void *data, size_t size, const char *typename)
429 {
430     TypeImpl *type = type_get_by_name(typename);
431 
432     object_initialize_with_type(data, size, type);
433 }
434 
435 void object_initialize_child(Object *parentobj, const char *propname,
436                              void *childobj, size_t size, const char *type,
437                              Error **errp, ...)
438 {
439     va_list vargs;
440 
441     va_start(vargs, errp);
442     object_initialize_childv(parentobj, propname, childobj, size, type, errp,
443                              vargs);
444     va_end(vargs);
445 }
446 
447 void object_initialize_childv(Object *parentobj, const char *propname,
448                               void *childobj, size_t size, const char *type,
449                               Error **errp, va_list vargs)
450 {
451     Error *local_err = NULL;
452     Object *obj;
453     UserCreatable *uc;
454 
455     object_initialize(childobj, size, type);
456     obj = OBJECT(childobj);
457 
458     object_set_propv(obj, &local_err, vargs);
459     if (local_err) {
460         goto out;
461     }
462 
463     object_property_add_child(parentobj, propname, obj, &local_err);
464     if (local_err) {
465         goto out;
466     }
467 
468     uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
469     if (uc) {
470         user_creatable_complete(uc, &local_err);
471         if (local_err) {
472             object_unparent(obj);
473             goto out;
474         }
475     }
476 
477     /*
478      * Since object_property_add_child added a reference to the child object,
479      * we can drop the reference added by object_initialize(), so the child
480      * property will own the only reference to the object.
481      */
482     object_unref(obj);
483 
484 out:
485     if (local_err) {
486         error_propagate(errp, local_err);
487         object_unref(obj);
488     }
489 }
490 
491 static inline bool object_property_is_child(ObjectProperty *prop)
492 {
493     return strstart(prop->type, "child<", NULL);
494 }
495 
496 static void object_property_del_all(Object *obj)
497 {
498     ObjectProperty *prop;
499     GHashTableIter iter;
500     gpointer key, value;
501     bool released;
502 
503     do {
504         released = false;
505         g_hash_table_iter_init(&iter, obj->properties);
506         while (g_hash_table_iter_next(&iter, &key, &value)) {
507             prop = value;
508             if (prop->release) {
509                 prop->release(obj, prop->name, prop->opaque);
510                 prop->release = NULL;
511                 released = true;
512                 break;
513             }
514             g_hash_table_iter_remove(&iter);
515         }
516     } while (released);
517 
518     g_hash_table_unref(obj->properties);
519 }
520 
521 static void object_property_del_child(Object *obj, Object *child, Error **errp)
522 {
523     ObjectProperty *prop;
524     GHashTableIter iter;
525     gpointer key, value;
526 
527     g_hash_table_iter_init(&iter, obj->properties);
528     while (g_hash_table_iter_next(&iter, &key, &value)) {
529         prop = value;
530         if (object_property_is_child(prop) && prop->opaque == child) {
531             if (prop->release) {
532                 prop->release(obj, prop->name, prop->opaque);
533                 prop->release = NULL;
534             }
535             break;
536         }
537     }
538     g_hash_table_iter_init(&iter, obj->properties);
539     while (g_hash_table_iter_next(&iter, &key, &value)) {
540         prop = value;
541         if (object_property_is_child(prop) && prop->opaque == child) {
542             g_hash_table_iter_remove(&iter);
543             break;
544         }
545     }
546 }
547 
548 void object_unparent(Object *obj)
549 {
550     if (obj->parent) {
551         object_property_del_child(obj->parent, obj, NULL);
552     }
553 }
554 
555 static void object_deinit(Object *obj, TypeImpl *type)
556 {
557     if (type->instance_finalize) {
558         type->instance_finalize(obj);
559     }
560 
561     if (type_has_parent(type)) {
562         object_deinit(obj, type_get_parent(type));
563     }
564 }
565 
566 static void object_finalize(void *data)
567 {
568     Object *obj = data;
569     TypeImpl *ti = obj->class->type;
570 
571     object_property_del_all(obj);
572     object_deinit(obj, ti);
573 
574     g_assert(obj->ref == 0);
575     if (obj->free) {
576         obj->free(obj);
577     }
578 }
579 
580 static Object *object_new_with_type(Type type)
581 {
582     Object *obj;
583 
584     g_assert(type != NULL);
585     type_initialize(type);
586 
587     obj = g_malloc(type->instance_size);
588     object_initialize_with_type(obj, type->instance_size, type);
589     obj->free = g_free;
590 
591     return obj;
592 }
593 
594 Object *object_new(const char *typename)
595 {
596     TypeImpl *ti = type_get_by_name(typename);
597 
598     return object_new_with_type(ti);
599 }
600 
601 
602 Object *object_new_with_props(const char *typename,
603                               Object *parent,
604                               const char *id,
605                               Error **errp,
606                               ...)
607 {
608     va_list vargs;
609     Object *obj;
610 
611     va_start(vargs, errp);
612     obj = object_new_with_propv(typename, parent, id, errp, vargs);
613     va_end(vargs);
614 
615     return obj;
616 }
617 
618 
619 Object *object_new_with_propv(const char *typename,
620                               Object *parent,
621                               const char *id,
622                               Error **errp,
623                               va_list vargs)
624 {
625     Object *obj;
626     ObjectClass *klass;
627     Error *local_err = NULL;
628     UserCreatable *uc;
629 
630     klass = object_class_by_name(typename);
631     if (!klass) {
632         error_setg(errp, "invalid object type: %s", typename);
633         return NULL;
634     }
635 
636     if (object_class_is_abstract(klass)) {
637         error_setg(errp, "object type '%s' is abstract", typename);
638         return NULL;
639     }
640     obj = object_new(typename);
641 
642     if (object_set_propv(obj, &local_err, vargs) < 0) {
643         goto error;
644     }
645 
646     object_property_add_child(parent, id, obj, &local_err);
647     if (local_err) {
648         goto error;
649     }
650 
651     uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
652     if (uc) {
653         user_creatable_complete(uc, &local_err);
654         if (local_err) {
655             object_unparent(obj);
656             goto error;
657         }
658     }
659 
660     object_unref(OBJECT(obj));
661     return obj;
662 
663  error:
664     error_propagate(errp, local_err);
665     object_unref(obj);
666     return NULL;
667 }
668 
669 
670 int object_set_props(Object *obj,
671                      Error **errp,
672                      ...)
673 {
674     va_list vargs;
675     int ret;
676 
677     va_start(vargs, errp);
678     ret = object_set_propv(obj, errp, vargs);
679     va_end(vargs);
680 
681     return ret;
682 }
683 
684 
685 int object_set_propv(Object *obj,
686                      Error **errp,
687                      va_list vargs)
688 {
689     const char *propname;
690     Error *local_err = NULL;
691 
692     propname = va_arg(vargs, char *);
693     while (propname != NULL) {
694         const char *value = va_arg(vargs, char *);
695 
696         g_assert(value != NULL);
697         object_property_parse(obj, value, propname, &local_err);
698         if (local_err) {
699             error_propagate(errp, local_err);
700             return -1;
701         }
702         propname = va_arg(vargs, char *);
703     }
704 
705     return 0;
706 }
707 
708 
709 Object *object_dynamic_cast(Object *obj, const char *typename)
710 {
711     if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) {
712         return obj;
713     }
714 
715     return NULL;
716 }
717 
718 Object *object_dynamic_cast_assert(Object *obj, const char *typename,
719                                    const char *file, int line, const char *func)
720 {
721     trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)",
722                                      typename, file, line, func);
723 
724 #ifdef CONFIG_QOM_CAST_DEBUG
725     int i;
726     Object *inst;
727 
728     for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) {
729         if (atomic_read(&obj->class->object_cast_cache[i]) == typename) {
730             goto out;
731         }
732     }
733 
734     inst = object_dynamic_cast(obj, typename);
735 
736     if (!inst && obj) {
737         fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
738                 file, line, func, obj, typename);
739         abort();
740     }
741 
742     assert(obj == inst);
743 
744     if (obj && obj == inst) {
745         for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
746             atomic_set(&obj->class->object_cast_cache[i - 1],
747                        atomic_read(&obj->class->object_cast_cache[i]));
748         }
749         atomic_set(&obj->class->object_cast_cache[i - 1], typename);
750     }
751 
752 out:
753 #endif
754     return obj;
755 }
756 
757 ObjectClass *object_class_dynamic_cast(ObjectClass *class,
758                                        const char *typename)
759 {
760     ObjectClass *ret = NULL;
761     TypeImpl *target_type;
762     TypeImpl *type;
763 
764     if (!class) {
765         return NULL;
766     }
767 
768     /* A simple fast path that can trigger a lot for leaf classes.  */
769     type = class->type;
770     if (type->name == typename) {
771         return class;
772     }
773 
774     target_type = type_get_by_name(typename);
775     if (!target_type) {
776         /* target class type unknown, so fail the cast */
777         return NULL;
778     }
779 
780     if (type->class->interfaces &&
781             type_is_ancestor(target_type, type_interface)) {
782         int found = 0;
783         GSList *i;
784 
785         for (i = class->interfaces; i; i = i->next) {
786             ObjectClass *target_class = i->data;
787 
788             if (type_is_ancestor(target_class->type, target_type)) {
789                 ret = target_class;
790                 found++;
791             }
792          }
793 
794         /* The match was ambiguous, don't allow a cast */
795         if (found > 1) {
796             ret = NULL;
797         }
798     } else if (type_is_ancestor(type, target_type)) {
799         ret = class;
800     }
801 
802     return ret;
803 }
804 
805 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
806                                               const char *typename,
807                                               const char *file, int line,
808                                               const char *func)
809 {
810     ObjectClass *ret;
811 
812     trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)",
813                                            typename, file, line, func);
814 
815 #ifdef CONFIG_QOM_CAST_DEBUG
816     int i;
817 
818     for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) {
819         if (atomic_read(&class->class_cast_cache[i]) == typename) {
820             ret = class;
821             goto out;
822         }
823     }
824 #else
825     if (!class || !class->interfaces) {
826         return class;
827     }
828 #endif
829 
830     ret = object_class_dynamic_cast(class, typename);
831     if (!ret && class) {
832         fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
833                 file, line, func, class, typename);
834         abort();
835     }
836 
837 #ifdef CONFIG_QOM_CAST_DEBUG
838     if (class && ret == class) {
839         for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
840             atomic_set(&class->class_cast_cache[i - 1],
841                        atomic_read(&class->class_cast_cache[i]));
842         }
843         atomic_set(&class->class_cast_cache[i - 1], typename);
844     }
845 out:
846 #endif
847     return ret;
848 }
849 
850 const char *object_get_typename(const Object *obj)
851 {
852     return obj->class->type->name;
853 }
854 
855 ObjectClass *object_get_class(Object *obj)
856 {
857     return obj->class;
858 }
859 
860 bool object_class_is_abstract(ObjectClass *klass)
861 {
862     return klass->type->abstract;
863 }
864 
865 const char *object_class_get_name(ObjectClass *klass)
866 {
867     return klass->type->name;
868 }
869 
870 ObjectClass *object_class_by_name(const char *typename)
871 {
872     TypeImpl *type = type_get_by_name(typename);
873 
874     if (!type) {
875         return NULL;
876     }
877 
878     type_initialize(type);
879 
880     return type->class;
881 }
882 
883 ObjectClass *object_class_get_parent(ObjectClass *class)
884 {
885     TypeImpl *type = type_get_parent(class->type);
886 
887     if (!type) {
888         return NULL;
889     }
890 
891     type_initialize(type);
892 
893     return type->class;
894 }
895 
896 typedef struct OCFData
897 {
898     void (*fn)(ObjectClass *klass, void *opaque);
899     const char *implements_type;
900     bool include_abstract;
901     void *opaque;
902 } OCFData;
903 
904 static void object_class_foreach_tramp(gpointer key, gpointer value,
905                                        gpointer opaque)
906 {
907     OCFData *data = opaque;
908     TypeImpl *type = value;
909     ObjectClass *k;
910 
911     type_initialize(type);
912     k = type->class;
913 
914     if (!data->include_abstract && type->abstract) {
915         return;
916     }
917 
918     if (data->implements_type &&
919         !object_class_dynamic_cast(k, data->implements_type)) {
920         return;
921     }
922 
923     data->fn(k, data->opaque);
924 }
925 
926 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
927                           const char *implements_type, bool include_abstract,
928                           void *opaque)
929 {
930     OCFData data = { fn, implements_type, include_abstract, opaque };
931 
932     enumerating_types = true;
933     g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
934     enumerating_types = false;
935 }
936 
937 static int do_object_child_foreach(Object *obj,
938                                    int (*fn)(Object *child, void *opaque),
939                                    void *opaque, bool recurse)
940 {
941     GHashTableIter iter;
942     ObjectProperty *prop;
943     int ret = 0;
944 
945     g_hash_table_iter_init(&iter, obj->properties);
946     while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
947         if (object_property_is_child(prop)) {
948             Object *child = prop->opaque;
949 
950             ret = fn(child, opaque);
951             if (ret != 0) {
952                 break;
953             }
954             if (recurse) {
955                 do_object_child_foreach(child, fn, opaque, true);
956             }
957         }
958     }
959     return ret;
960 }
961 
962 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
963                          void *opaque)
964 {
965     return do_object_child_foreach(obj, fn, opaque, false);
966 }
967 
968 int object_child_foreach_recursive(Object *obj,
969                                    int (*fn)(Object *child, void *opaque),
970                                    void *opaque)
971 {
972     return do_object_child_foreach(obj, fn, opaque, true);
973 }
974 
975 static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
976 {
977     GSList **list = opaque;
978 
979     *list = g_slist_prepend(*list, klass);
980 }
981 
982 GSList *object_class_get_list(const char *implements_type,
983                               bool include_abstract)
984 {
985     GSList *list = NULL;
986 
987     object_class_foreach(object_class_get_list_tramp,
988                          implements_type, include_abstract, &list);
989     return list;
990 }
991 
992 static gint object_class_cmp(gconstpointer a, gconstpointer b)
993 {
994     return strcasecmp(object_class_get_name((ObjectClass *)a),
995                       object_class_get_name((ObjectClass *)b));
996 }
997 
998 GSList *object_class_get_list_sorted(const char *implements_type,
999                                      bool include_abstract)
1000 {
1001     return g_slist_sort(object_class_get_list(implements_type, include_abstract),
1002                         object_class_cmp);
1003 }
1004 
1005 void object_ref(Object *obj)
1006 {
1007     if (!obj) {
1008         return;
1009     }
1010     atomic_inc(&obj->ref);
1011 }
1012 
1013 void object_unref(Object *obj)
1014 {
1015     if (!obj) {
1016         return;
1017     }
1018     g_assert(obj->ref > 0);
1019 
1020     /* parent always holds a reference to its children */
1021     if (atomic_fetch_dec(&obj->ref) == 1) {
1022         object_finalize(obj);
1023     }
1024 }
1025 
1026 ObjectProperty *
1027 object_property_add(Object *obj, const char *name, const char *type,
1028                     ObjectPropertyAccessor *get,
1029                     ObjectPropertyAccessor *set,
1030                     ObjectPropertyRelease *release,
1031                     void *opaque, Error **errp)
1032 {
1033     ObjectProperty *prop;
1034     size_t name_len = strlen(name);
1035 
1036     if (name_len >= 3 && !memcmp(name + name_len - 3, "[*]", 4)) {
1037         int i;
1038         ObjectProperty *ret;
1039         char *name_no_array = g_strdup(name);
1040 
1041         name_no_array[name_len - 3] = '\0';
1042         for (i = 0; ; ++i) {
1043             char *full_name = g_strdup_printf("%s[%d]", name_no_array, i);
1044 
1045             ret = object_property_add(obj, full_name, type, get, set,
1046                                       release, opaque, NULL);
1047             g_free(full_name);
1048             if (ret) {
1049                 break;
1050             }
1051         }
1052         g_free(name_no_array);
1053         return ret;
1054     }
1055 
1056     if (object_property_find(obj, name, NULL) != NULL) {
1057         error_setg(errp, "attempt to add duplicate property '%s'"
1058                    " to object (type '%s')", name,
1059                    object_get_typename(obj));
1060         return NULL;
1061     }
1062 
1063     prop = g_malloc0(sizeof(*prop));
1064 
1065     prop->name = g_strdup(name);
1066     prop->type = g_strdup(type);
1067 
1068     prop->get = get;
1069     prop->set = set;
1070     prop->release = release;
1071     prop->opaque = opaque;
1072 
1073     g_hash_table_insert(obj->properties, prop->name, prop);
1074     return prop;
1075 }
1076 
1077 ObjectProperty *
1078 object_class_property_add(ObjectClass *klass,
1079                           const char *name,
1080                           const char *type,
1081                           ObjectPropertyAccessor *get,
1082                           ObjectPropertyAccessor *set,
1083                           ObjectPropertyRelease *release,
1084                           void *opaque,
1085                           Error **errp)
1086 {
1087     ObjectProperty *prop;
1088 
1089     if (object_class_property_find(klass, name, NULL) != NULL) {
1090         error_setg(errp, "attempt to add duplicate property '%s'"
1091                    " to object (type '%s')", name,
1092                    object_class_get_name(klass));
1093         return NULL;
1094     }
1095 
1096     prop = g_malloc0(sizeof(*prop));
1097 
1098     prop->name = g_strdup(name);
1099     prop->type = g_strdup(type);
1100 
1101     prop->get = get;
1102     prop->set = set;
1103     prop->release = release;
1104     prop->opaque = opaque;
1105 
1106     g_hash_table_insert(klass->properties, g_strdup(name), prop);
1107 
1108     return prop;
1109 }
1110 
1111 ObjectProperty *object_property_find(Object *obj, const char *name,
1112                                      Error **errp)
1113 {
1114     ObjectProperty *prop;
1115     ObjectClass *klass = object_get_class(obj);
1116 
1117     prop = object_class_property_find(klass, name, NULL);
1118     if (prop) {
1119         return prop;
1120     }
1121 
1122     prop = g_hash_table_lookup(obj->properties, name);
1123     if (prop) {
1124         return prop;
1125     }
1126 
1127     error_setg(errp, "Property '.%s' not found", name);
1128     return NULL;
1129 }
1130 
1131 void object_property_iter_init(ObjectPropertyIterator *iter,
1132                                Object *obj)
1133 {
1134     g_hash_table_iter_init(&iter->iter, obj->properties);
1135     iter->nextclass = object_get_class(obj);
1136 }
1137 
1138 ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter)
1139 {
1140     gpointer key, val;
1141     while (!g_hash_table_iter_next(&iter->iter, &key, &val)) {
1142         if (!iter->nextclass) {
1143             return NULL;
1144         }
1145         g_hash_table_iter_init(&iter->iter, iter->nextclass->properties);
1146         iter->nextclass = object_class_get_parent(iter->nextclass);
1147     }
1148     return val;
1149 }
1150 
1151 void object_class_property_iter_init(ObjectPropertyIterator *iter,
1152                                      ObjectClass *klass)
1153 {
1154     g_hash_table_iter_init(&iter->iter, klass->properties);
1155     iter->nextclass = object_class_get_parent(klass);
1156 }
1157 
1158 ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name,
1159                                            Error **errp)
1160 {
1161     ObjectProperty *prop;
1162     ObjectClass *parent_klass;
1163 
1164     parent_klass = object_class_get_parent(klass);
1165     if (parent_klass) {
1166         prop = object_class_property_find(parent_klass, name, NULL);
1167         if (prop) {
1168             return prop;
1169         }
1170     }
1171 
1172     prop = g_hash_table_lookup(klass->properties, name);
1173     if (!prop) {
1174         error_setg(errp, "Property '.%s' not found", name);
1175     }
1176     return prop;
1177 }
1178 
1179 void object_property_del(Object *obj, const char *name, Error **errp)
1180 {
1181     ObjectProperty *prop = g_hash_table_lookup(obj->properties, name);
1182 
1183     if (!prop) {
1184         error_setg(errp, "Property '.%s' not found", name);
1185         return;
1186     }
1187 
1188     if (prop->release) {
1189         prop->release(obj, name, prop->opaque);
1190     }
1191     g_hash_table_remove(obj->properties, name);
1192 }
1193 
1194 void object_property_get(Object *obj, Visitor *v, const char *name,
1195                          Error **errp)
1196 {
1197     ObjectProperty *prop = object_property_find(obj, name, errp);
1198     if (prop == NULL) {
1199         return;
1200     }
1201 
1202     if (!prop->get) {
1203         error_setg(errp, QERR_PERMISSION_DENIED);
1204     } else {
1205         prop->get(obj, v, name, prop->opaque, errp);
1206     }
1207 }
1208 
1209 void object_property_set(Object *obj, Visitor *v, const char *name,
1210                          Error **errp)
1211 {
1212     ObjectProperty *prop = object_property_find(obj, name, errp);
1213     if (prop == NULL) {
1214         return;
1215     }
1216 
1217     if (!prop->set) {
1218         error_setg(errp, QERR_PERMISSION_DENIED);
1219     } else {
1220         prop->set(obj, v, name, prop->opaque, errp);
1221     }
1222 }
1223 
1224 void object_property_set_str(Object *obj, const char *value,
1225                              const char *name, Error **errp)
1226 {
1227     QString *qstr = qstring_from_str(value);
1228     object_property_set_qobject(obj, QOBJECT(qstr), name, errp);
1229 
1230     qobject_unref(qstr);
1231 }
1232 
1233 char *object_property_get_str(Object *obj, const char *name,
1234                               Error **errp)
1235 {
1236     QObject *ret = object_property_get_qobject(obj, name, errp);
1237     char *retval;
1238 
1239     if (!ret) {
1240         return NULL;
1241     }
1242 
1243     retval = g_strdup(qobject_get_try_str(ret));
1244     if (!retval) {
1245         error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
1246     }
1247 
1248     qobject_unref(ret);
1249     return retval;
1250 }
1251 
1252 void object_property_set_link(Object *obj, Object *value,
1253                               const char *name, Error **errp)
1254 {
1255     if (value) {
1256         gchar *path = object_get_canonical_path(value);
1257         object_property_set_str(obj, path, name, errp);
1258         g_free(path);
1259     } else {
1260         object_property_set_str(obj, "", name, errp);
1261     }
1262 }
1263 
1264 Object *object_property_get_link(Object *obj, const char *name,
1265                                  Error **errp)
1266 {
1267     char *str = object_property_get_str(obj, name, errp);
1268     Object *target = NULL;
1269 
1270     if (str && *str) {
1271         target = object_resolve_path(str, NULL);
1272         if (!target) {
1273             error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1274                       "Device '%s' not found", str);
1275         }
1276     }
1277 
1278     g_free(str);
1279     return target;
1280 }
1281 
1282 void object_property_set_bool(Object *obj, bool value,
1283                               const char *name, Error **errp)
1284 {
1285     QBool *qbool = qbool_from_bool(value);
1286     object_property_set_qobject(obj, QOBJECT(qbool), name, errp);
1287 
1288     qobject_unref(qbool);
1289 }
1290 
1291 bool object_property_get_bool(Object *obj, const char *name,
1292                               Error **errp)
1293 {
1294     QObject *ret = object_property_get_qobject(obj, name, errp);
1295     QBool *qbool;
1296     bool retval;
1297 
1298     if (!ret) {
1299         return false;
1300     }
1301     qbool = qobject_to(QBool, ret);
1302     if (!qbool) {
1303         error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
1304         retval = false;
1305     } else {
1306         retval = qbool_get_bool(qbool);
1307     }
1308 
1309     qobject_unref(ret);
1310     return retval;
1311 }
1312 
1313 void object_property_set_int(Object *obj, int64_t value,
1314                              const char *name, Error **errp)
1315 {
1316     QNum *qnum = qnum_from_int(value);
1317     object_property_set_qobject(obj, QOBJECT(qnum), name, errp);
1318 
1319     qobject_unref(qnum);
1320 }
1321 
1322 int64_t object_property_get_int(Object *obj, const char *name,
1323                                 Error **errp)
1324 {
1325     QObject *ret = object_property_get_qobject(obj, name, errp);
1326     QNum *qnum;
1327     int64_t retval;
1328 
1329     if (!ret) {
1330         return -1;
1331     }
1332 
1333     qnum = qobject_to(QNum, ret);
1334     if (!qnum || !qnum_get_try_int(qnum, &retval)) {
1335         error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
1336         retval = -1;
1337     }
1338 
1339     qobject_unref(ret);
1340     return retval;
1341 }
1342 
1343 void object_property_set_uint(Object *obj, uint64_t value,
1344                               const char *name, Error **errp)
1345 {
1346     QNum *qnum = qnum_from_uint(value);
1347 
1348     object_property_set_qobject(obj, QOBJECT(qnum), name, errp);
1349     qobject_unref(qnum);
1350 }
1351 
1352 uint64_t object_property_get_uint(Object *obj, const char *name,
1353                                   Error **errp)
1354 {
1355     QObject *ret = object_property_get_qobject(obj, name, errp);
1356     QNum *qnum;
1357     uint64_t retval;
1358 
1359     if (!ret) {
1360         return 0;
1361     }
1362     qnum = qobject_to(QNum, ret);
1363     if (!qnum || !qnum_get_try_uint(qnum, &retval)) {
1364         error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "uint");
1365         retval = 0;
1366     }
1367 
1368     qobject_unref(ret);
1369     return retval;
1370 }
1371 
1372 typedef struct EnumProperty {
1373     const QEnumLookup *lookup;
1374     int (*get)(Object *, Error **);
1375     void (*set)(Object *, int, Error **);
1376 } EnumProperty;
1377 
1378 int object_property_get_enum(Object *obj, const char *name,
1379                              const char *typename, Error **errp)
1380 {
1381     Error *err = NULL;
1382     Visitor *v;
1383     char *str;
1384     int ret;
1385     ObjectProperty *prop = object_property_find(obj, name, errp);
1386     EnumProperty *enumprop;
1387 
1388     if (prop == NULL) {
1389         return 0;
1390     }
1391 
1392     if (!g_str_equal(prop->type, typename)) {
1393         error_setg(errp, "Property %s on %s is not '%s' enum type",
1394                    name, object_class_get_name(
1395                        object_get_class(obj)), typename);
1396         return 0;
1397     }
1398 
1399     enumprop = prop->opaque;
1400 
1401     v = string_output_visitor_new(false, &str);
1402     object_property_get(obj, v, name, &err);
1403     if (err) {
1404         error_propagate(errp, err);
1405         visit_free(v);
1406         return 0;
1407     }
1408     visit_complete(v, &str);
1409     visit_free(v);
1410     v = string_input_visitor_new(str);
1411     visit_type_enum(v, name, &ret, enumprop->lookup, errp);
1412 
1413     g_free(str);
1414     visit_free(v);
1415 
1416     return ret;
1417 }
1418 
1419 void object_property_get_uint16List(Object *obj, const char *name,
1420                                     uint16List **list, Error **errp)
1421 {
1422     Error *err = NULL;
1423     Visitor *v;
1424     char *str;
1425 
1426     v = string_output_visitor_new(false, &str);
1427     object_property_get(obj, v, name, &err);
1428     if (err) {
1429         error_propagate(errp, err);
1430         goto out;
1431     }
1432     visit_complete(v, &str);
1433     visit_free(v);
1434     v = string_input_visitor_new(str);
1435     visit_type_uint16List(v, NULL, list, errp);
1436 
1437     g_free(str);
1438 out:
1439     visit_free(v);
1440 }
1441 
1442 void object_property_parse(Object *obj, const char *string,
1443                            const char *name, Error **errp)
1444 {
1445     Visitor *v = string_input_visitor_new(string);
1446     object_property_set(obj, v, name, errp);
1447     visit_free(v);
1448 }
1449 
1450 char *object_property_print(Object *obj, const char *name, bool human,
1451                             Error **errp)
1452 {
1453     Visitor *v;
1454     char *string = NULL;
1455     Error *local_err = NULL;
1456 
1457     v = string_output_visitor_new(human, &string);
1458     object_property_get(obj, v, name, &local_err);
1459     if (local_err) {
1460         error_propagate(errp, local_err);
1461         goto out;
1462     }
1463 
1464     visit_complete(v, &string);
1465 
1466 out:
1467     visit_free(v);
1468     return string;
1469 }
1470 
1471 const char *object_property_get_type(Object *obj, const char *name, Error **errp)
1472 {
1473     ObjectProperty *prop = object_property_find(obj, name, errp);
1474     if (prop == NULL) {
1475         return NULL;
1476     }
1477 
1478     return prop->type;
1479 }
1480 
1481 Object *object_get_root(void)
1482 {
1483     static Object *root;
1484 
1485     if (!root) {
1486         root = object_new("container");
1487     }
1488 
1489     return root;
1490 }
1491 
1492 Object *object_get_objects_root(void)
1493 {
1494     return container_get(object_get_root(), "/objects");
1495 }
1496 
1497 Object *object_get_internal_root(void)
1498 {
1499     static Object *internal_root;
1500 
1501     if (!internal_root) {
1502         internal_root = object_new("container");
1503     }
1504 
1505     return internal_root;
1506 }
1507 
1508 static void object_get_child_property(Object *obj, Visitor *v,
1509                                       const char *name, void *opaque,
1510                                       Error **errp)
1511 {
1512     Object *child = opaque;
1513     gchar *path;
1514 
1515     path = object_get_canonical_path(child);
1516     visit_type_str(v, name, &path, errp);
1517     g_free(path);
1518 }
1519 
1520 static Object *object_resolve_child_property(Object *parent, void *opaque, const gchar *part)
1521 {
1522     return opaque;
1523 }
1524 
1525 static void object_finalize_child_property(Object *obj, const char *name,
1526                                            void *opaque)
1527 {
1528     Object *child = opaque;
1529 
1530     if (child->class->unparent) {
1531         (child->class->unparent)(child);
1532     }
1533     child->parent = NULL;
1534     object_unref(child);
1535 }
1536 
1537 void object_property_add_child(Object *obj, const char *name,
1538                                Object *child, Error **errp)
1539 {
1540     Error *local_err = NULL;
1541     gchar *type;
1542     ObjectProperty *op;
1543 
1544     if (child->parent != NULL) {
1545         error_setg(errp, "child object is already parented");
1546         return;
1547     }
1548 
1549     type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child)));
1550 
1551     op = object_property_add(obj, name, type, object_get_child_property, NULL,
1552                              object_finalize_child_property, child, &local_err);
1553     if (local_err) {
1554         error_propagate(errp, local_err);
1555         goto out;
1556     }
1557 
1558     op->resolve = object_resolve_child_property;
1559     object_ref(child);
1560     child->parent = obj;
1561 
1562 out:
1563     g_free(type);
1564 }
1565 
1566 void object_property_allow_set_link(const Object *obj, const char *name,
1567                                     Object *val, Error **errp)
1568 {
1569     /* Allow the link to be set, always */
1570 }
1571 
1572 typedef struct {
1573     Object **child;
1574     void (*check)(const Object *, const char *, Object *, Error **);
1575     ObjectPropertyLinkFlags flags;
1576 } LinkProperty;
1577 
1578 static void object_get_link_property(Object *obj, Visitor *v,
1579                                      const char *name, void *opaque,
1580                                      Error **errp)
1581 {
1582     LinkProperty *lprop = opaque;
1583     Object **child = lprop->child;
1584     gchar *path;
1585 
1586     if (*child) {
1587         path = object_get_canonical_path(*child);
1588         visit_type_str(v, name, &path, errp);
1589         g_free(path);
1590     } else {
1591         path = (gchar *)"";
1592         visit_type_str(v, name, &path, errp);
1593     }
1594 }
1595 
1596 /*
1597  * object_resolve_link:
1598  *
1599  * Lookup an object and ensure its type matches the link property type.  This
1600  * is similar to object_resolve_path() except type verification against the
1601  * link property is performed.
1602  *
1603  * Returns: The matched object or NULL on path lookup failures.
1604  */
1605 static Object *object_resolve_link(Object *obj, const char *name,
1606                                    const char *path, Error **errp)
1607 {
1608     const char *type;
1609     gchar *target_type;
1610     bool ambiguous = false;
1611     Object *target;
1612 
1613     /* Go from link<FOO> to FOO.  */
1614     type = object_property_get_type(obj, name, NULL);
1615     target_type = g_strndup(&type[5], strlen(type) - 6);
1616     target = object_resolve_path_type(path, target_type, &ambiguous);
1617 
1618     if (ambiguous) {
1619         error_setg(errp, "Path '%s' does not uniquely identify an object",
1620                    path);
1621     } else if (!target) {
1622         target = object_resolve_path(path, &ambiguous);
1623         if (target || ambiguous) {
1624             error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
1625         } else {
1626             error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1627                       "Device '%s' not found", path);
1628         }
1629         target = NULL;
1630     }
1631     g_free(target_type);
1632 
1633     return target;
1634 }
1635 
1636 static void object_set_link_property(Object *obj, Visitor *v,
1637                                      const char *name, void *opaque,
1638                                      Error **errp)
1639 {
1640     Error *local_err = NULL;
1641     LinkProperty *prop = opaque;
1642     Object **child = prop->child;
1643     Object *old_target = *child;
1644     Object *new_target = NULL;
1645     char *path = NULL;
1646 
1647     visit_type_str(v, name, &path, &local_err);
1648 
1649     if (!local_err && strcmp(path, "") != 0) {
1650         new_target = object_resolve_link(obj, name, path, &local_err);
1651     }
1652 
1653     g_free(path);
1654     if (local_err) {
1655         error_propagate(errp, local_err);
1656         return;
1657     }
1658 
1659     prop->check(obj, name, new_target, &local_err);
1660     if (local_err) {
1661         error_propagate(errp, local_err);
1662         return;
1663     }
1664 
1665     *child = new_target;
1666     if (prop->flags == OBJ_PROP_LINK_STRONG) {
1667         object_ref(new_target);
1668         object_unref(old_target);
1669     }
1670 }
1671 
1672 static Object *object_resolve_link_property(Object *parent, void *opaque, const gchar *part)
1673 {
1674     LinkProperty *lprop = opaque;
1675 
1676     return *lprop->child;
1677 }
1678 
1679 static void object_release_link_property(Object *obj, const char *name,
1680                                          void *opaque)
1681 {
1682     LinkProperty *prop = opaque;
1683 
1684     if ((prop->flags & OBJ_PROP_LINK_STRONG) && *prop->child) {
1685         object_unref(*prop->child);
1686     }
1687     g_free(prop);
1688 }
1689 
1690 void object_property_add_link(Object *obj, const char *name,
1691                               const char *type, Object **child,
1692                               void (*check)(const Object *, const char *,
1693                                             Object *, Error **),
1694                               ObjectPropertyLinkFlags flags,
1695                               Error **errp)
1696 {
1697     Error *local_err = NULL;
1698     LinkProperty *prop = g_malloc(sizeof(*prop));
1699     gchar *full_type;
1700     ObjectProperty *op;
1701 
1702     prop->child = child;
1703     prop->check = check;
1704     prop->flags = flags;
1705 
1706     full_type = g_strdup_printf("link<%s>", type);
1707 
1708     op = object_property_add(obj, name, full_type,
1709                              object_get_link_property,
1710                              check ? object_set_link_property : NULL,
1711                              object_release_link_property,
1712                              prop,
1713                              &local_err);
1714     if (local_err) {
1715         error_propagate(errp, local_err);
1716         g_free(prop);
1717         goto out;
1718     }
1719 
1720     op->resolve = object_resolve_link_property;
1721 
1722 out:
1723     g_free(full_type);
1724 }
1725 
1726 void object_property_add_const_link(Object *obj, const char *name,
1727                                     Object *target, Error **errp)
1728 {
1729     char *link_type;
1730     ObjectProperty *op;
1731 
1732     link_type = g_strdup_printf("link<%s>", object_get_typename(target));
1733     op = object_property_add(obj, name, link_type,
1734                              object_get_child_property, NULL,
1735                              NULL, target, errp);
1736     if (op != NULL) {
1737         op->resolve = object_resolve_child_property;
1738     }
1739     g_free(link_type);
1740 }
1741 
1742 gchar *object_get_canonical_path_component(Object *obj)
1743 {
1744     ObjectProperty *prop = NULL;
1745     GHashTableIter iter;
1746 
1747     if (obj->parent == NULL) {
1748         return NULL;
1749     }
1750 
1751     g_hash_table_iter_init(&iter, obj->parent->properties);
1752     while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
1753         if (!object_property_is_child(prop)) {
1754             continue;
1755         }
1756 
1757         if (prop->opaque == obj) {
1758             return g_strdup(prop->name);
1759         }
1760     }
1761 
1762     /* obj had a parent but was not a child, should never happen */
1763     g_assert_not_reached();
1764     return NULL;
1765 }
1766 
1767 gchar *object_get_canonical_path(Object *obj)
1768 {
1769     Object *root = object_get_root();
1770     char *newpath, *path = NULL;
1771 
1772     if (obj == root) {
1773         return g_strdup("/");
1774     }
1775 
1776     do {
1777         char *component = object_get_canonical_path_component(obj);
1778 
1779         if (!component) {
1780             /* A canonical path must be complete, so discard what was
1781              * collected so far.
1782              */
1783             g_free(path);
1784             return NULL;
1785         }
1786 
1787         newpath = g_strdup_printf("/%s%s", component, path ? path : "");
1788         g_free(path);
1789         g_free(component);
1790         path = newpath;
1791         obj = obj->parent;
1792     } while (obj != root);
1793 
1794     return path;
1795 }
1796 
1797 Object *object_resolve_path_component(Object *parent, const gchar *part)
1798 {
1799     ObjectProperty *prop = object_property_find(parent, part, NULL);
1800     if (prop == NULL) {
1801         return NULL;
1802     }
1803 
1804     if (prop->resolve) {
1805         return prop->resolve(parent, prop->opaque, part);
1806     } else {
1807         return NULL;
1808     }
1809 }
1810 
1811 static Object *object_resolve_abs_path(Object *parent,
1812                                           gchar **parts,
1813                                           const char *typename,
1814                                           int index)
1815 {
1816     Object *child;
1817 
1818     if (parts[index] == NULL) {
1819         return object_dynamic_cast(parent, typename);
1820     }
1821 
1822     if (strcmp(parts[index], "") == 0) {
1823         return object_resolve_abs_path(parent, parts, typename, index + 1);
1824     }
1825 
1826     child = object_resolve_path_component(parent, parts[index]);
1827     if (!child) {
1828         return NULL;
1829     }
1830 
1831     return object_resolve_abs_path(child, parts, typename, index + 1);
1832 }
1833 
1834 static Object *object_resolve_partial_path(Object *parent,
1835                                               gchar **parts,
1836                                               const char *typename,
1837                                               bool *ambiguous)
1838 {
1839     Object *obj;
1840     GHashTableIter iter;
1841     ObjectProperty *prop;
1842 
1843     obj = object_resolve_abs_path(parent, parts, typename, 0);
1844 
1845     g_hash_table_iter_init(&iter, parent->properties);
1846     while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
1847         Object *found;
1848 
1849         if (!object_property_is_child(prop)) {
1850             continue;
1851         }
1852 
1853         found = object_resolve_partial_path(prop->opaque, parts,
1854                                             typename, ambiguous);
1855         if (found) {
1856             if (obj) {
1857                 *ambiguous = true;
1858                 return NULL;
1859             }
1860             obj = found;
1861         }
1862 
1863         if (*ambiguous) {
1864             return NULL;
1865         }
1866     }
1867 
1868     return obj;
1869 }
1870 
1871 Object *object_resolve_path_type(const char *path, const char *typename,
1872                                  bool *ambiguousp)
1873 {
1874     Object *obj;
1875     gchar **parts;
1876 
1877     parts = g_strsplit(path, "/", 0);
1878     assert(parts);
1879 
1880     if (parts[0] == NULL || strcmp(parts[0], "") != 0) {
1881         bool ambiguous = false;
1882         obj = object_resolve_partial_path(object_get_root(), parts,
1883                                           typename, &ambiguous);
1884         if (ambiguousp) {
1885             *ambiguousp = ambiguous;
1886         }
1887     } else {
1888         obj = object_resolve_abs_path(object_get_root(), parts, typename, 1);
1889     }
1890 
1891     g_strfreev(parts);
1892 
1893     return obj;
1894 }
1895 
1896 Object *object_resolve_path(const char *path, bool *ambiguous)
1897 {
1898     return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
1899 }
1900 
1901 typedef struct StringProperty
1902 {
1903     char *(*get)(Object *, Error **);
1904     void (*set)(Object *, const char *, Error **);
1905 } StringProperty;
1906 
1907 static void property_get_str(Object *obj, Visitor *v, const char *name,
1908                              void *opaque, Error **errp)
1909 {
1910     StringProperty *prop = opaque;
1911     char *value;
1912     Error *err = NULL;
1913 
1914     value = prop->get(obj, &err);
1915     if (err) {
1916         error_propagate(errp, err);
1917         return;
1918     }
1919 
1920     visit_type_str(v, name, &value, errp);
1921     g_free(value);
1922 }
1923 
1924 static void property_set_str(Object *obj, Visitor *v, const char *name,
1925                              void *opaque, Error **errp)
1926 {
1927     StringProperty *prop = opaque;
1928     char *value;
1929     Error *local_err = NULL;
1930 
1931     visit_type_str(v, name, &value, &local_err);
1932     if (local_err) {
1933         error_propagate(errp, local_err);
1934         return;
1935     }
1936 
1937     prop->set(obj, value, errp);
1938     g_free(value);
1939 }
1940 
1941 static void property_release_str(Object *obj, const char *name,
1942                                  void *opaque)
1943 {
1944     StringProperty *prop = opaque;
1945     g_free(prop);
1946 }
1947 
1948 void object_property_add_str(Object *obj, const char *name,
1949                            char *(*get)(Object *, Error **),
1950                            void (*set)(Object *, const char *, Error **),
1951                            Error **errp)
1952 {
1953     Error *local_err = NULL;
1954     StringProperty *prop = g_malloc0(sizeof(*prop));
1955 
1956     prop->get = get;
1957     prop->set = set;
1958 
1959     object_property_add(obj, name, "string",
1960                         get ? property_get_str : NULL,
1961                         set ? property_set_str : NULL,
1962                         property_release_str,
1963                         prop, &local_err);
1964     if (local_err) {
1965         error_propagate(errp, local_err);
1966         g_free(prop);
1967     }
1968 }
1969 
1970 void object_class_property_add_str(ObjectClass *klass, const char *name,
1971                                    char *(*get)(Object *, Error **),
1972                                    void (*set)(Object *, const char *,
1973                                                Error **),
1974                                    Error **errp)
1975 {
1976     Error *local_err = NULL;
1977     StringProperty *prop = g_malloc0(sizeof(*prop));
1978 
1979     prop->get = get;
1980     prop->set = set;
1981 
1982     object_class_property_add(klass, name, "string",
1983                               get ? property_get_str : NULL,
1984                               set ? property_set_str : NULL,
1985                               property_release_str,
1986                               prop, &local_err);
1987     if (local_err) {
1988         error_propagate(errp, local_err);
1989         g_free(prop);
1990     }
1991 }
1992 
1993 typedef struct BoolProperty
1994 {
1995     bool (*get)(Object *, Error **);
1996     void (*set)(Object *, bool, Error **);
1997 } BoolProperty;
1998 
1999 static void property_get_bool(Object *obj, Visitor *v, const char *name,
2000                               void *opaque, Error **errp)
2001 {
2002     BoolProperty *prop = opaque;
2003     bool value;
2004     Error *err = NULL;
2005 
2006     value = prop->get(obj, &err);
2007     if (err) {
2008         error_propagate(errp, err);
2009         return;
2010     }
2011 
2012     visit_type_bool(v, name, &value, errp);
2013 }
2014 
2015 static void property_set_bool(Object *obj, Visitor *v, const char *name,
2016                               void *opaque, Error **errp)
2017 {
2018     BoolProperty *prop = opaque;
2019     bool value;
2020     Error *local_err = NULL;
2021 
2022     visit_type_bool(v, name, &value, &local_err);
2023     if (local_err) {
2024         error_propagate(errp, local_err);
2025         return;
2026     }
2027 
2028     prop->set(obj, value, errp);
2029 }
2030 
2031 static void property_release_bool(Object *obj, const char *name,
2032                                   void *opaque)
2033 {
2034     BoolProperty *prop = opaque;
2035     g_free(prop);
2036 }
2037 
2038 void object_property_add_bool(Object *obj, const char *name,
2039                               bool (*get)(Object *, Error **),
2040                               void (*set)(Object *, bool, Error **),
2041                               Error **errp)
2042 {
2043     Error *local_err = NULL;
2044     BoolProperty *prop = g_malloc0(sizeof(*prop));
2045 
2046     prop->get = get;
2047     prop->set = set;
2048 
2049     object_property_add(obj, name, "bool",
2050                         get ? property_get_bool : NULL,
2051                         set ? property_set_bool : NULL,
2052                         property_release_bool,
2053                         prop, &local_err);
2054     if (local_err) {
2055         error_propagate(errp, local_err);
2056         g_free(prop);
2057     }
2058 }
2059 
2060 void object_class_property_add_bool(ObjectClass *klass, const char *name,
2061                                     bool (*get)(Object *, Error **),
2062                                     void (*set)(Object *, bool, Error **),
2063                                     Error **errp)
2064 {
2065     Error *local_err = NULL;
2066     BoolProperty *prop = g_malloc0(sizeof(*prop));
2067 
2068     prop->get = get;
2069     prop->set = set;
2070 
2071     object_class_property_add(klass, name, "bool",
2072                               get ? property_get_bool : NULL,
2073                               set ? property_set_bool : NULL,
2074                               property_release_bool,
2075                               prop, &local_err);
2076     if (local_err) {
2077         error_propagate(errp, local_err);
2078         g_free(prop);
2079     }
2080 }
2081 
2082 static void property_get_enum(Object *obj, Visitor *v, const char *name,
2083                               void *opaque, Error **errp)
2084 {
2085     EnumProperty *prop = opaque;
2086     int value;
2087     Error *err = NULL;
2088 
2089     value = prop->get(obj, &err);
2090     if (err) {
2091         error_propagate(errp, err);
2092         return;
2093     }
2094 
2095     visit_type_enum(v, name, &value, prop->lookup, errp);
2096 }
2097 
2098 static void property_set_enum(Object *obj, Visitor *v, const char *name,
2099                               void *opaque, Error **errp)
2100 {
2101     EnumProperty *prop = opaque;
2102     int value;
2103     Error *err = NULL;
2104 
2105     visit_type_enum(v, name, &value, prop->lookup, &err);
2106     if (err) {
2107         error_propagate(errp, err);
2108         return;
2109     }
2110     prop->set(obj, value, errp);
2111 }
2112 
2113 static void property_release_enum(Object *obj, const char *name,
2114                                   void *opaque)
2115 {
2116     EnumProperty *prop = opaque;
2117     g_free(prop);
2118 }
2119 
2120 void object_property_add_enum(Object *obj, const char *name,
2121                               const char *typename,
2122                               const QEnumLookup *lookup,
2123                               int (*get)(Object *, Error **),
2124                               void (*set)(Object *, int, Error **),
2125                               Error **errp)
2126 {
2127     Error *local_err = NULL;
2128     EnumProperty *prop = g_malloc(sizeof(*prop));
2129 
2130     prop->lookup = lookup;
2131     prop->get = get;
2132     prop->set = set;
2133 
2134     object_property_add(obj, name, typename,
2135                         get ? property_get_enum : NULL,
2136                         set ? property_set_enum : NULL,
2137                         property_release_enum,
2138                         prop, &local_err);
2139     if (local_err) {
2140         error_propagate(errp, local_err);
2141         g_free(prop);
2142     }
2143 }
2144 
2145 void object_class_property_add_enum(ObjectClass *klass, const char *name,
2146                                     const char *typename,
2147                                     const QEnumLookup *lookup,
2148                                     int (*get)(Object *, Error **),
2149                                     void (*set)(Object *, int, Error **),
2150                                     Error **errp)
2151 {
2152     Error *local_err = NULL;
2153     EnumProperty *prop = g_malloc(sizeof(*prop));
2154 
2155     prop->lookup = lookup;
2156     prop->get = get;
2157     prop->set = set;
2158 
2159     object_class_property_add(klass, name, typename,
2160                               get ? property_get_enum : NULL,
2161                               set ? property_set_enum : NULL,
2162                               property_release_enum,
2163                               prop, &local_err);
2164     if (local_err) {
2165         error_propagate(errp, local_err);
2166         g_free(prop);
2167     }
2168 }
2169 
2170 typedef struct TMProperty {
2171     void (*get)(Object *, struct tm *, Error **);
2172 } TMProperty;
2173 
2174 static void property_get_tm(Object *obj, Visitor *v, const char *name,
2175                             void *opaque, Error **errp)
2176 {
2177     TMProperty *prop = opaque;
2178     Error *err = NULL;
2179     struct tm value;
2180 
2181     prop->get(obj, &value, &err);
2182     if (err) {
2183         goto out;
2184     }
2185 
2186     visit_start_struct(v, name, NULL, 0, &err);
2187     if (err) {
2188         goto out;
2189     }
2190     visit_type_int32(v, "tm_year", &value.tm_year, &err);
2191     if (err) {
2192         goto out_end;
2193     }
2194     visit_type_int32(v, "tm_mon", &value.tm_mon, &err);
2195     if (err) {
2196         goto out_end;
2197     }
2198     visit_type_int32(v, "tm_mday", &value.tm_mday, &err);
2199     if (err) {
2200         goto out_end;
2201     }
2202     visit_type_int32(v, "tm_hour", &value.tm_hour, &err);
2203     if (err) {
2204         goto out_end;
2205     }
2206     visit_type_int32(v, "tm_min", &value.tm_min, &err);
2207     if (err) {
2208         goto out_end;
2209     }
2210     visit_type_int32(v, "tm_sec", &value.tm_sec, &err);
2211     if (err) {
2212         goto out_end;
2213     }
2214     visit_check_struct(v, &err);
2215 out_end:
2216     visit_end_struct(v, NULL);
2217 out:
2218     error_propagate(errp, err);
2219 
2220 }
2221 
2222 static void property_release_tm(Object *obj, const char *name,
2223                                 void *opaque)
2224 {
2225     TMProperty *prop = opaque;
2226     g_free(prop);
2227 }
2228 
2229 void object_property_add_tm(Object *obj, const char *name,
2230                             void (*get)(Object *, struct tm *, Error **),
2231                             Error **errp)
2232 {
2233     Error *local_err = NULL;
2234     TMProperty *prop = g_malloc0(sizeof(*prop));
2235 
2236     prop->get = get;
2237 
2238     object_property_add(obj, name, "struct tm",
2239                         get ? property_get_tm : NULL, NULL,
2240                         property_release_tm,
2241                         prop, &local_err);
2242     if (local_err) {
2243         error_propagate(errp, local_err);
2244         g_free(prop);
2245     }
2246 }
2247 
2248 void object_class_property_add_tm(ObjectClass *klass, const char *name,
2249                                   void (*get)(Object *, struct tm *, Error **),
2250                                   Error **errp)
2251 {
2252     Error *local_err = NULL;
2253     TMProperty *prop = g_malloc0(sizeof(*prop));
2254 
2255     prop->get = get;
2256 
2257     object_class_property_add(klass, name, "struct tm",
2258                               get ? property_get_tm : NULL, NULL,
2259                               property_release_tm,
2260                               prop, &local_err);
2261     if (local_err) {
2262         error_propagate(errp, local_err);
2263         g_free(prop);
2264     }
2265 }
2266 
2267 static char *qdev_get_type(Object *obj, Error **errp)
2268 {
2269     return g_strdup(object_get_typename(obj));
2270 }
2271 
2272 static void property_get_uint8_ptr(Object *obj, Visitor *v, const char *name,
2273                                    void *opaque, Error **errp)
2274 {
2275     uint8_t value = *(uint8_t *)opaque;
2276     visit_type_uint8(v, name, &value, errp);
2277 }
2278 
2279 static void property_get_uint16_ptr(Object *obj, Visitor *v, const char *name,
2280                                     void *opaque, Error **errp)
2281 {
2282     uint16_t value = *(uint16_t *)opaque;
2283     visit_type_uint16(v, name, &value, errp);
2284 }
2285 
2286 static void property_get_uint32_ptr(Object *obj, Visitor *v, const char *name,
2287                                     void *opaque, Error **errp)
2288 {
2289     uint32_t value = *(uint32_t *)opaque;
2290     visit_type_uint32(v, name, &value, errp);
2291 }
2292 
2293 static void property_get_uint64_ptr(Object *obj, Visitor *v, const char *name,
2294                                     void *opaque, Error **errp)
2295 {
2296     uint64_t value = *(uint64_t *)opaque;
2297     visit_type_uint64(v, name, &value, errp);
2298 }
2299 
2300 void object_property_add_uint8_ptr(Object *obj, const char *name,
2301                                    const uint8_t *v, Error **errp)
2302 {
2303     object_property_add(obj, name, "uint8", property_get_uint8_ptr,
2304                         NULL, NULL, (void *)v, errp);
2305 }
2306 
2307 void object_class_property_add_uint8_ptr(ObjectClass *klass, const char *name,
2308                                          const uint8_t *v, Error **errp)
2309 {
2310     object_class_property_add(klass, name, "uint8", property_get_uint8_ptr,
2311                               NULL, NULL, (void *)v, errp);
2312 }
2313 
2314 void object_property_add_uint16_ptr(Object *obj, const char *name,
2315                                     const uint16_t *v, Error **errp)
2316 {
2317     object_property_add(obj, name, "uint16", property_get_uint16_ptr,
2318                         NULL, NULL, (void *)v, errp);
2319 }
2320 
2321 void object_class_property_add_uint16_ptr(ObjectClass *klass, const char *name,
2322                                           const uint16_t *v, Error **errp)
2323 {
2324     object_class_property_add(klass, name, "uint16", property_get_uint16_ptr,
2325                               NULL, NULL, (void *)v, errp);
2326 }
2327 
2328 void object_property_add_uint32_ptr(Object *obj, const char *name,
2329                                     const uint32_t *v, Error **errp)
2330 {
2331     object_property_add(obj, name, "uint32", property_get_uint32_ptr,
2332                         NULL, NULL, (void *)v, errp);
2333 }
2334 
2335 void object_class_property_add_uint32_ptr(ObjectClass *klass, const char *name,
2336                                           const uint32_t *v, Error **errp)
2337 {
2338     object_class_property_add(klass, name, "uint32", property_get_uint32_ptr,
2339                               NULL, NULL, (void *)v, errp);
2340 }
2341 
2342 void object_property_add_uint64_ptr(Object *obj, const char *name,
2343                                     const uint64_t *v, Error **errp)
2344 {
2345     object_property_add(obj, name, "uint64", property_get_uint64_ptr,
2346                         NULL, NULL, (void *)v, errp);
2347 }
2348 
2349 void object_class_property_add_uint64_ptr(ObjectClass *klass, const char *name,
2350                                           const uint64_t *v, Error **errp)
2351 {
2352     object_class_property_add(klass, name, "uint64", property_get_uint64_ptr,
2353                               NULL, NULL, (void *)v, errp);
2354 }
2355 
2356 typedef struct {
2357     Object *target_obj;
2358     char *target_name;
2359 } AliasProperty;
2360 
2361 static void property_get_alias(Object *obj, Visitor *v, const char *name,
2362                                void *opaque, Error **errp)
2363 {
2364     AliasProperty *prop = opaque;
2365 
2366     object_property_get(prop->target_obj, v, prop->target_name, errp);
2367 }
2368 
2369 static void property_set_alias(Object *obj, Visitor *v, const char *name,
2370                                void *opaque, Error **errp)
2371 {
2372     AliasProperty *prop = opaque;
2373 
2374     object_property_set(prop->target_obj, v, prop->target_name, errp);
2375 }
2376 
2377 static Object *property_resolve_alias(Object *obj, void *opaque,
2378                                       const gchar *part)
2379 {
2380     AliasProperty *prop = opaque;
2381 
2382     return object_resolve_path_component(prop->target_obj, prop->target_name);
2383 }
2384 
2385 static void property_release_alias(Object *obj, const char *name, void *opaque)
2386 {
2387     AliasProperty *prop = opaque;
2388 
2389     g_free(prop->target_name);
2390     g_free(prop);
2391 }
2392 
2393 void object_property_add_alias(Object *obj, const char *name,
2394                                Object *target_obj, const char *target_name,
2395                                Error **errp)
2396 {
2397     AliasProperty *prop;
2398     ObjectProperty *op;
2399     ObjectProperty *target_prop;
2400     gchar *prop_type;
2401     Error *local_err = NULL;
2402 
2403     target_prop = object_property_find(target_obj, target_name, errp);
2404     if (!target_prop) {
2405         return;
2406     }
2407 
2408     if (object_property_is_child(target_prop)) {
2409         prop_type = g_strdup_printf("link%s",
2410                                     target_prop->type + strlen("child"));
2411     } else {
2412         prop_type = g_strdup(target_prop->type);
2413     }
2414 
2415     prop = g_malloc(sizeof(*prop));
2416     prop->target_obj = target_obj;
2417     prop->target_name = g_strdup(target_name);
2418 
2419     op = object_property_add(obj, name, prop_type,
2420                              property_get_alias,
2421                              property_set_alias,
2422                              property_release_alias,
2423                              prop, &local_err);
2424     if (local_err) {
2425         error_propagate(errp, local_err);
2426         g_free(prop);
2427         goto out;
2428     }
2429     op->resolve = property_resolve_alias;
2430 
2431     object_property_set_description(obj, op->name,
2432                                     target_prop->description,
2433                                     &error_abort);
2434 
2435 out:
2436     g_free(prop_type);
2437 }
2438 
2439 void object_property_set_description(Object *obj, const char *name,
2440                                      const char *description, Error **errp)
2441 {
2442     ObjectProperty *op;
2443 
2444     op = object_property_find(obj, name, errp);
2445     if (!op) {
2446         return;
2447     }
2448 
2449     g_free(op->description);
2450     op->description = g_strdup(description);
2451 }
2452 
2453 void object_class_property_set_description(ObjectClass *klass,
2454                                            const char *name,
2455                                            const char *description,
2456                                            Error **errp)
2457 {
2458     ObjectProperty *op;
2459 
2460     op = g_hash_table_lookup(klass->properties, name);
2461     if (!op) {
2462         error_setg(errp, "Property '.%s' not found", name);
2463         return;
2464     }
2465 
2466     g_free(op->description);
2467     op->description = g_strdup(description);
2468 }
2469 
2470 static void object_class_init(ObjectClass *klass, void *data)
2471 {
2472     object_class_property_add_str(klass, "type", qdev_get_type,
2473                                   NULL, &error_abort);
2474 }
2475 
2476 static void register_types(void)
2477 {
2478     static TypeInfo interface_info = {
2479         .name = TYPE_INTERFACE,
2480         .class_size = sizeof(InterfaceClass),
2481         .abstract = true,
2482     };
2483 
2484     static TypeInfo object_info = {
2485         .name = TYPE_OBJECT,
2486         .instance_size = sizeof(Object),
2487         .class_init = object_class_init,
2488         .abstract = true,
2489     };
2490 
2491     type_interface = type_register_internal(&interface_info);
2492     type_register_internal(&object_info);
2493 }
2494 
2495 type_init(register_types)
2496