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 "qom/object.h" 14 #include "qemu-common.h" 15 #include "qapi/visitor.h" 16 #include "qapi-visit.h" 17 #include "qapi/string-input-visitor.h" 18 #include "qapi/string-output-visitor.h" 19 #include "qapi/qmp/qerror.h" 20 #include "trace.h" 21 22 /* TODO: replace QObject with a simpler visitor to avoid a dependency 23 * of the QOM core on QObject? */ 24 #include "qom/qom-qobject.h" 25 #include "qapi/qmp/qobject.h" 26 #include "qapi/qmp/qbool.h" 27 #include "qapi/qmp/qint.h" 28 #include "qapi/qmp/qstring.h" 29 30 #define MAX_INTERFACES 32 31 32 typedef struct InterfaceImpl InterfaceImpl; 33 typedef struct TypeImpl TypeImpl; 34 35 struct InterfaceImpl 36 { 37 const char *typename; 38 }; 39 40 struct TypeImpl 41 { 42 const char *name; 43 44 size_t class_size; 45 46 size_t instance_size; 47 48 void (*class_init)(ObjectClass *klass, void *data); 49 void (*class_base_init)(ObjectClass *klass, void *data); 50 void (*class_finalize)(ObjectClass *klass, void *data); 51 52 void *class_data; 53 54 void (*instance_init)(Object *obj); 55 void (*instance_post_init)(Object *obj); 56 void (*instance_finalize)(Object *obj); 57 58 bool abstract; 59 60 const char *parent; 61 TypeImpl *parent_type; 62 63 ObjectClass *class; 64 65 int num_interfaces; 66 InterfaceImpl interfaces[MAX_INTERFACES]; 67 }; 68 69 static Type type_interface; 70 71 static GHashTable *type_table_get(void) 72 { 73 static GHashTable *type_table; 74 75 if (type_table == NULL) { 76 type_table = g_hash_table_new(g_str_hash, g_str_equal); 77 } 78 79 return type_table; 80 } 81 82 static bool enumerating_types; 83 84 static void type_table_add(TypeImpl *ti) 85 { 86 assert(!enumerating_types); 87 g_hash_table_insert(type_table_get(), (void *)ti->name, ti); 88 } 89 90 static TypeImpl *type_table_lookup(const char *name) 91 { 92 return g_hash_table_lookup(type_table_get(), name); 93 } 94 95 static TypeImpl *type_new(const TypeInfo *info) 96 { 97 TypeImpl *ti = g_malloc0(sizeof(*ti)); 98 int i; 99 100 g_assert(info->name != NULL); 101 102 if (type_table_lookup(info->name) != NULL) { 103 fprintf(stderr, "Registering `%s' which already exists\n", info->name); 104 abort(); 105 } 106 107 ti->name = g_strdup(info->name); 108 ti->parent = g_strdup(info->parent); 109 110 ti->class_size = info->class_size; 111 ti->instance_size = info->instance_size; 112 113 ti->class_init = info->class_init; 114 ti->class_base_init = info->class_base_init; 115 ti->class_finalize = info->class_finalize; 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 static TypeImpl *type_get_by_name(const char *name) 153 { 154 if (name == NULL) { 155 return NULL; 156 } 157 158 return type_table_lookup(name); 159 } 160 161 static TypeImpl *type_get_parent(TypeImpl *type) 162 { 163 if (!type->parent_type && type->parent) { 164 type->parent_type = type_get_by_name(type->parent); 165 g_assert(type->parent_type != NULL); 166 } 167 168 return type->parent_type; 169 } 170 171 static bool type_has_parent(TypeImpl *type) 172 { 173 return (type->parent != NULL); 174 } 175 176 static size_t type_class_get_size(TypeImpl *ti) 177 { 178 if (ti->class_size) { 179 return ti->class_size; 180 } 181 182 if (type_has_parent(ti)) { 183 return type_class_get_size(type_get_parent(ti)); 184 } 185 186 return sizeof(ObjectClass); 187 } 188 189 static size_t type_object_get_size(TypeImpl *ti) 190 { 191 if (ti->instance_size) { 192 return ti->instance_size; 193 } 194 195 if (type_has_parent(ti)) { 196 return type_object_get_size(type_get_parent(ti)); 197 } 198 199 return 0; 200 } 201 202 static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type) 203 { 204 assert(target_type); 205 206 /* Check if typename is a direct ancestor of type */ 207 while (type) { 208 if (type == target_type) { 209 return true; 210 } 211 212 type = type_get_parent(type); 213 } 214 215 return false; 216 } 217 218 static void type_initialize(TypeImpl *ti); 219 220 static void type_initialize_interface(TypeImpl *ti, TypeImpl *interface_type, 221 TypeImpl *parent_type) 222 { 223 InterfaceClass *new_iface; 224 TypeInfo info = { }; 225 TypeImpl *iface_impl; 226 227 info.parent = parent_type->name; 228 info.name = g_strdup_printf("%s::%s", ti->name, interface_type->name); 229 info.abstract = true; 230 231 iface_impl = type_new(&info); 232 iface_impl->parent_type = parent_type; 233 type_initialize(iface_impl); 234 g_free((char *)info.name); 235 236 new_iface = (InterfaceClass *)iface_impl->class; 237 new_iface->concrete_class = ti->class; 238 new_iface->interface_type = interface_type; 239 240 ti->class->interfaces = g_slist_append(ti->class->interfaces, 241 iface_impl->class); 242 } 243 244 static void type_initialize(TypeImpl *ti) 245 { 246 TypeImpl *parent; 247 248 if (ti->class) { 249 return; 250 } 251 252 ti->class_size = type_class_get_size(ti); 253 ti->instance_size = type_object_get_size(ti); 254 255 ti->class = g_malloc0(ti->class_size); 256 257 parent = type_get_parent(ti); 258 if (parent) { 259 type_initialize(parent); 260 GSList *e; 261 int i; 262 263 g_assert(parent->class_size <= ti->class_size); 264 memcpy(ti->class, parent->class, parent->class_size); 265 ti->class->interfaces = NULL; 266 267 for (e = parent->class->interfaces; e; e = e->next) { 268 InterfaceClass *iface = e->data; 269 ObjectClass *klass = OBJECT_CLASS(iface); 270 271 type_initialize_interface(ti, iface->interface_type, klass->type); 272 } 273 274 for (i = 0; i < ti->num_interfaces; i++) { 275 TypeImpl *t = type_get_by_name(ti->interfaces[i].typename); 276 for (e = ti->class->interfaces; e; e = e->next) { 277 TypeImpl *target_type = OBJECT_CLASS(e->data)->type; 278 279 if (type_is_ancestor(target_type, t)) { 280 break; 281 } 282 } 283 284 if (e) { 285 continue; 286 } 287 288 type_initialize_interface(ti, t, t); 289 } 290 } 291 292 ti->class->type = ti; 293 294 while (parent) { 295 if (parent->class_base_init) { 296 parent->class_base_init(ti->class, ti->class_data); 297 } 298 parent = type_get_parent(parent); 299 } 300 301 if (ti->class_init) { 302 ti->class_init(ti->class, ti->class_data); 303 } 304 } 305 306 static void object_init_with_type(Object *obj, TypeImpl *ti) 307 { 308 if (type_has_parent(ti)) { 309 object_init_with_type(obj, type_get_parent(ti)); 310 } 311 312 if (ti->instance_init) { 313 ti->instance_init(obj); 314 } 315 } 316 317 static void object_post_init_with_type(Object *obj, TypeImpl *ti) 318 { 319 if (ti->instance_post_init) { 320 ti->instance_post_init(obj); 321 } 322 323 if (type_has_parent(ti)) { 324 object_post_init_with_type(obj, type_get_parent(ti)); 325 } 326 } 327 328 void object_initialize_with_type(void *data, size_t size, TypeImpl *type) 329 { 330 Object *obj = data; 331 332 g_assert(type != NULL); 333 type_initialize(type); 334 335 g_assert(type->instance_size >= sizeof(Object)); 336 g_assert(type->abstract == false); 337 g_assert(size >= type->instance_size); 338 339 memset(obj, 0, type->instance_size); 340 obj->class = type->class; 341 object_ref(obj); 342 QTAILQ_INIT(&obj->properties); 343 object_init_with_type(obj, type); 344 object_post_init_with_type(obj, type); 345 } 346 347 void object_initialize(void *data, size_t size, const char *typename) 348 { 349 TypeImpl *type = type_get_by_name(typename); 350 351 object_initialize_with_type(data, size, type); 352 } 353 354 static inline bool object_property_is_child(ObjectProperty *prop) 355 { 356 return strstart(prop->type, "child<", NULL); 357 } 358 359 static inline bool object_property_is_link(ObjectProperty *prop) 360 { 361 return strstart(prop->type, "link<", NULL); 362 } 363 364 static void object_property_del_all(Object *obj) 365 { 366 while (!QTAILQ_EMPTY(&obj->properties)) { 367 ObjectProperty *prop = QTAILQ_FIRST(&obj->properties); 368 369 QTAILQ_REMOVE(&obj->properties, prop, node); 370 371 if (prop->release) { 372 prop->release(obj, prop->name, prop->opaque); 373 } 374 375 g_free(prop->name); 376 g_free(prop->type); 377 g_free(prop); 378 } 379 } 380 381 static void object_property_del_child(Object *obj, Object *child, Error **errp) 382 { 383 ObjectProperty *prop; 384 385 QTAILQ_FOREACH(prop, &obj->properties, node) { 386 if (object_property_is_child(prop) && prop->opaque == child) { 387 object_property_del(obj, prop->name, errp); 388 break; 389 } 390 } 391 } 392 393 void object_unparent(Object *obj) 394 { 395 if (!obj->parent) { 396 return; 397 } 398 399 object_ref(obj); 400 if (obj->class->unparent) { 401 (obj->class->unparent)(obj); 402 } 403 if (obj->parent) { 404 object_property_del_child(obj->parent, obj, NULL); 405 obj->parent = NULL; 406 } 407 object_unref(obj); 408 } 409 410 static void object_deinit(Object *obj, TypeImpl *type) 411 { 412 if (type->instance_finalize) { 413 type->instance_finalize(obj); 414 } 415 416 if (type_has_parent(type)) { 417 object_deinit(obj, type_get_parent(type)); 418 } 419 } 420 421 static void object_finalize(void *data) 422 { 423 Object *obj = data; 424 TypeImpl *ti = obj->class->type; 425 426 object_deinit(obj, ti); 427 object_property_del_all(obj); 428 429 g_assert(obj->ref == 0); 430 if (obj->free) { 431 obj->free(obj); 432 } 433 } 434 435 Object *object_new_with_type(Type type) 436 { 437 Object *obj; 438 439 g_assert(type != NULL); 440 type_initialize(type); 441 442 obj = g_malloc(type->instance_size); 443 object_initialize_with_type(obj, type->instance_size, type); 444 obj->free = g_free; 445 446 return obj; 447 } 448 449 Object *object_new(const char *typename) 450 { 451 TypeImpl *ti = type_get_by_name(typename); 452 453 return object_new_with_type(ti); 454 } 455 456 Object *object_dynamic_cast(Object *obj, const char *typename) 457 { 458 if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) { 459 return obj; 460 } 461 462 return NULL; 463 } 464 465 Object *object_dynamic_cast_assert(Object *obj, const char *typename, 466 const char *file, int line, const char *func) 467 { 468 trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)", 469 typename, file, line, func); 470 471 #ifdef CONFIG_QOM_CAST_DEBUG 472 int i; 473 Object *inst; 474 475 for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) { 476 if (obj->class->object_cast_cache[i] == typename) { 477 goto out; 478 } 479 } 480 481 inst = object_dynamic_cast(obj, typename); 482 483 if (!inst && obj) { 484 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n", 485 file, line, func, obj, typename); 486 abort(); 487 } 488 489 assert(obj == inst); 490 491 if (obj && obj == inst) { 492 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) { 493 obj->class->object_cast_cache[i - 1] = 494 obj->class->object_cast_cache[i]; 495 } 496 obj->class->object_cast_cache[i - 1] = typename; 497 } 498 499 out: 500 #endif 501 return obj; 502 } 503 504 ObjectClass *object_class_dynamic_cast(ObjectClass *class, 505 const char *typename) 506 { 507 ObjectClass *ret = NULL; 508 TypeImpl *target_type; 509 TypeImpl *type; 510 511 if (!class) { 512 return NULL; 513 } 514 515 /* A simple fast path that can trigger a lot for leaf classes. */ 516 type = class->type; 517 if (type->name == typename) { 518 return class; 519 } 520 521 target_type = type_get_by_name(typename); 522 if (!target_type) { 523 /* target class type unknown, so fail the cast */ 524 return NULL; 525 } 526 527 if (type->class->interfaces && 528 type_is_ancestor(target_type, type_interface)) { 529 int found = 0; 530 GSList *i; 531 532 for (i = class->interfaces; i; i = i->next) { 533 ObjectClass *target_class = i->data; 534 535 if (type_is_ancestor(target_class->type, target_type)) { 536 ret = target_class; 537 found++; 538 } 539 } 540 541 /* The match was ambiguous, don't allow a cast */ 542 if (found > 1) { 543 ret = NULL; 544 } 545 } else if (type_is_ancestor(type, target_type)) { 546 ret = class; 547 } 548 549 return ret; 550 } 551 552 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class, 553 const char *typename, 554 const char *file, int line, 555 const char *func) 556 { 557 ObjectClass *ret; 558 559 trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)", 560 typename, file, line, func); 561 562 #ifdef CONFIG_QOM_CAST_DEBUG 563 int i; 564 565 for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) { 566 if (class->class_cast_cache[i] == typename) { 567 ret = class; 568 goto out; 569 } 570 } 571 #else 572 if (!class || !class->interfaces) { 573 return class; 574 } 575 #endif 576 577 ret = object_class_dynamic_cast(class, typename); 578 if (!ret && class) { 579 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n", 580 file, line, func, class, typename); 581 abort(); 582 } 583 584 #ifdef CONFIG_QOM_CAST_DEBUG 585 if (class && ret == class) { 586 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) { 587 class->class_cast_cache[i - 1] = class->class_cast_cache[i]; 588 } 589 class->class_cast_cache[i - 1] = typename; 590 } 591 out: 592 #endif 593 return ret; 594 } 595 596 const char *object_get_typename(Object *obj) 597 { 598 return obj->class->type->name; 599 } 600 601 ObjectClass *object_get_class(Object *obj) 602 { 603 return obj->class; 604 } 605 606 bool object_class_is_abstract(ObjectClass *klass) 607 { 608 return klass->type->abstract; 609 } 610 611 const char *object_class_get_name(ObjectClass *klass) 612 { 613 return klass->type->name; 614 } 615 616 ObjectClass *object_class_by_name(const char *typename) 617 { 618 TypeImpl *type = type_get_by_name(typename); 619 620 if (!type) { 621 return NULL; 622 } 623 624 type_initialize(type); 625 626 return type->class; 627 } 628 629 ObjectClass *object_class_get_parent(ObjectClass *class) 630 { 631 TypeImpl *type = type_get_parent(class->type); 632 633 if (!type) { 634 return NULL; 635 } 636 637 type_initialize(type); 638 639 return type->class; 640 } 641 642 typedef struct OCFData 643 { 644 void (*fn)(ObjectClass *klass, void *opaque); 645 const char *implements_type; 646 bool include_abstract; 647 void *opaque; 648 } OCFData; 649 650 static void object_class_foreach_tramp(gpointer key, gpointer value, 651 gpointer opaque) 652 { 653 OCFData *data = opaque; 654 TypeImpl *type = value; 655 ObjectClass *k; 656 657 type_initialize(type); 658 k = type->class; 659 660 if (!data->include_abstract && type->abstract) { 661 return; 662 } 663 664 if (data->implements_type && 665 !object_class_dynamic_cast(k, data->implements_type)) { 666 return; 667 } 668 669 data->fn(k, data->opaque); 670 } 671 672 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque), 673 const char *implements_type, bool include_abstract, 674 void *opaque) 675 { 676 OCFData data = { fn, implements_type, include_abstract, opaque }; 677 678 enumerating_types = true; 679 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data); 680 enumerating_types = false; 681 } 682 683 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque), 684 void *opaque) 685 { 686 ObjectProperty *prop; 687 int ret = 0; 688 689 QTAILQ_FOREACH(prop, &obj->properties, node) { 690 if (object_property_is_child(prop)) { 691 ret = fn(prop->opaque, opaque); 692 if (ret != 0) { 693 break; 694 } 695 } 696 } 697 return ret; 698 } 699 700 static void object_class_get_list_tramp(ObjectClass *klass, void *opaque) 701 { 702 GSList **list = opaque; 703 704 *list = g_slist_prepend(*list, klass); 705 } 706 707 GSList *object_class_get_list(const char *implements_type, 708 bool include_abstract) 709 { 710 GSList *list = NULL; 711 712 object_class_foreach(object_class_get_list_tramp, 713 implements_type, include_abstract, &list); 714 return list; 715 } 716 717 void object_ref(Object *obj) 718 { 719 atomic_inc(&obj->ref); 720 } 721 722 void object_unref(Object *obj) 723 { 724 g_assert(obj->ref > 0); 725 726 /* parent always holds a reference to its children */ 727 if (atomic_fetch_dec(&obj->ref) == 1) { 728 object_finalize(obj); 729 } 730 } 731 732 void object_property_add(Object *obj, const char *name, const char *type, 733 ObjectPropertyAccessor *get, 734 ObjectPropertyAccessor *set, 735 ObjectPropertyRelease *release, 736 void *opaque, Error **errp) 737 { 738 ObjectProperty *prop; 739 740 QTAILQ_FOREACH(prop, &obj->properties, node) { 741 if (strcmp(prop->name, name) == 0) { 742 error_setg(errp, "attempt to add duplicate property '%s'" 743 " to object (type '%s')", name, 744 object_get_typename(obj)); 745 return; 746 } 747 } 748 749 prop = g_malloc0(sizeof(*prop)); 750 751 prop->name = g_strdup(name); 752 prop->type = g_strdup(type); 753 754 prop->get = get; 755 prop->set = set; 756 prop->release = release; 757 prop->opaque = opaque; 758 759 QTAILQ_INSERT_TAIL(&obj->properties, prop, node); 760 } 761 762 ObjectProperty *object_property_find(Object *obj, const char *name, 763 Error **errp) 764 { 765 ObjectProperty *prop; 766 767 QTAILQ_FOREACH(prop, &obj->properties, node) { 768 if (strcmp(prop->name, name) == 0) { 769 return prop; 770 } 771 } 772 773 error_setg(errp, "Property '.%s' not found", name); 774 return NULL; 775 } 776 777 void object_property_del(Object *obj, const char *name, Error **errp) 778 { 779 ObjectProperty *prop = object_property_find(obj, name, errp); 780 if (prop == NULL) { 781 return; 782 } 783 784 if (prop->release) { 785 prop->release(obj, name, prop->opaque); 786 } 787 788 QTAILQ_REMOVE(&obj->properties, prop, node); 789 790 g_free(prop->name); 791 g_free(prop->type); 792 g_free(prop); 793 } 794 795 void object_property_get(Object *obj, Visitor *v, const char *name, 796 Error **errp) 797 { 798 ObjectProperty *prop = object_property_find(obj, name, errp); 799 if (prop == NULL) { 800 return; 801 } 802 803 if (!prop->get) { 804 error_set(errp, QERR_PERMISSION_DENIED); 805 } else { 806 prop->get(obj, v, prop->opaque, name, errp); 807 } 808 } 809 810 void object_property_set(Object *obj, Visitor *v, const char *name, 811 Error **errp) 812 { 813 ObjectProperty *prop = object_property_find(obj, name, errp); 814 if (prop == NULL) { 815 return; 816 } 817 818 if (!prop->set) { 819 error_set(errp, QERR_PERMISSION_DENIED); 820 } else { 821 prop->set(obj, v, prop->opaque, name, errp); 822 } 823 } 824 825 void object_property_set_str(Object *obj, const char *value, 826 const char *name, Error **errp) 827 { 828 QString *qstr = qstring_from_str(value); 829 object_property_set_qobject(obj, QOBJECT(qstr), name, errp); 830 831 QDECREF(qstr); 832 } 833 834 char *object_property_get_str(Object *obj, const char *name, 835 Error **errp) 836 { 837 QObject *ret = object_property_get_qobject(obj, name, errp); 838 QString *qstring; 839 char *retval; 840 841 if (!ret) { 842 return NULL; 843 } 844 qstring = qobject_to_qstring(ret); 845 if (!qstring) { 846 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "string"); 847 retval = NULL; 848 } else { 849 retval = g_strdup(qstring_get_str(qstring)); 850 } 851 852 QDECREF(qstring); 853 return retval; 854 } 855 856 void object_property_set_link(Object *obj, Object *value, 857 const char *name, Error **errp) 858 { 859 gchar *path = object_get_canonical_path(value); 860 object_property_set_str(obj, path, name, errp); 861 g_free(path); 862 } 863 864 Object *object_property_get_link(Object *obj, const char *name, 865 Error **errp) 866 { 867 char *str = object_property_get_str(obj, name, errp); 868 Object *target = NULL; 869 870 if (str && *str) { 871 target = object_resolve_path(str, NULL); 872 if (!target) { 873 error_set(errp, QERR_DEVICE_NOT_FOUND, str); 874 } 875 } 876 877 g_free(str); 878 return target; 879 } 880 881 void object_property_set_bool(Object *obj, bool value, 882 const char *name, Error **errp) 883 { 884 QBool *qbool = qbool_from_int(value); 885 object_property_set_qobject(obj, QOBJECT(qbool), name, errp); 886 887 QDECREF(qbool); 888 } 889 890 bool object_property_get_bool(Object *obj, const char *name, 891 Error **errp) 892 { 893 QObject *ret = object_property_get_qobject(obj, name, errp); 894 QBool *qbool; 895 bool retval; 896 897 if (!ret) { 898 return false; 899 } 900 qbool = qobject_to_qbool(ret); 901 if (!qbool) { 902 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean"); 903 retval = false; 904 } else { 905 retval = qbool_get_int(qbool); 906 } 907 908 QDECREF(qbool); 909 return retval; 910 } 911 912 void object_property_set_int(Object *obj, int64_t value, 913 const char *name, Error **errp) 914 { 915 QInt *qint = qint_from_int(value); 916 object_property_set_qobject(obj, QOBJECT(qint), name, errp); 917 918 QDECREF(qint); 919 } 920 921 int64_t object_property_get_int(Object *obj, const char *name, 922 Error **errp) 923 { 924 QObject *ret = object_property_get_qobject(obj, name, errp); 925 QInt *qint; 926 int64_t retval; 927 928 if (!ret) { 929 return -1; 930 } 931 qint = qobject_to_qint(ret); 932 if (!qint) { 933 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "int"); 934 retval = -1; 935 } else { 936 retval = qint_get_int(qint); 937 } 938 939 QDECREF(qint); 940 return retval; 941 } 942 943 int object_property_get_enum(Object *obj, const char *name, 944 const char *strings[], Error **errp) 945 { 946 StringOutputVisitor *sov; 947 StringInputVisitor *siv; 948 int ret; 949 950 sov = string_output_visitor_new(false); 951 object_property_get(obj, string_output_get_visitor(sov), name, errp); 952 siv = string_input_visitor_new(string_output_get_string(sov)); 953 string_output_visitor_cleanup(sov); 954 visit_type_enum(string_input_get_visitor(siv), 955 &ret, strings, NULL, name, errp); 956 string_input_visitor_cleanup(siv); 957 958 return ret; 959 } 960 961 void object_property_get_uint16List(Object *obj, const char *name, 962 uint16List **list, Error **errp) 963 { 964 StringOutputVisitor *ov; 965 StringInputVisitor *iv; 966 967 ov = string_output_visitor_new(false); 968 object_property_get(obj, string_output_get_visitor(ov), 969 name, errp); 970 iv = string_input_visitor_new(string_output_get_string(ov)); 971 visit_type_uint16List(string_input_get_visitor(iv), 972 list, NULL, errp); 973 string_output_visitor_cleanup(ov); 974 string_input_visitor_cleanup(iv); 975 } 976 977 void object_property_parse(Object *obj, const char *string, 978 const char *name, Error **errp) 979 { 980 StringInputVisitor *mi; 981 mi = string_input_visitor_new(string); 982 object_property_set(obj, string_input_get_visitor(mi), name, errp); 983 984 string_input_visitor_cleanup(mi); 985 } 986 987 char *object_property_print(Object *obj, const char *name, bool human, 988 Error **errp) 989 { 990 StringOutputVisitor *mo; 991 char *string; 992 993 mo = string_output_visitor_new(human); 994 object_property_get(obj, string_output_get_visitor(mo), name, errp); 995 string = string_output_get_string(mo); 996 string_output_visitor_cleanup(mo); 997 return string; 998 } 999 1000 const char *object_property_get_type(Object *obj, const char *name, Error **errp) 1001 { 1002 ObjectProperty *prop = object_property_find(obj, name, errp); 1003 if (prop == NULL) { 1004 return NULL; 1005 } 1006 1007 return prop->type; 1008 } 1009 1010 Object *object_get_root(void) 1011 { 1012 static Object *root; 1013 1014 if (!root) { 1015 root = object_new("container"); 1016 } 1017 1018 return root; 1019 } 1020 1021 static void object_get_child_property(Object *obj, Visitor *v, void *opaque, 1022 const char *name, Error **errp) 1023 { 1024 Object *child = opaque; 1025 gchar *path; 1026 1027 path = object_get_canonical_path(child); 1028 visit_type_str(v, &path, name, errp); 1029 g_free(path); 1030 } 1031 1032 static void object_finalize_child_property(Object *obj, const char *name, 1033 void *opaque) 1034 { 1035 Object *child = opaque; 1036 1037 object_unref(child); 1038 } 1039 1040 void object_property_add_child(Object *obj, const char *name, 1041 Object *child, Error **errp) 1042 { 1043 Error *local_err = NULL; 1044 gchar *type; 1045 1046 type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child))); 1047 1048 object_property_add(obj, name, type, object_get_child_property, NULL, 1049 object_finalize_child_property, child, &local_err); 1050 if (local_err) { 1051 error_propagate(errp, local_err); 1052 goto out; 1053 } 1054 object_ref(child); 1055 g_assert(child->parent == NULL); 1056 child->parent = obj; 1057 1058 out: 1059 g_free(type); 1060 } 1061 1062 void object_property_allow_set_link(Object *obj, const char *name, 1063 Object *val, Error **errp) 1064 { 1065 /* Allow the link to be set, always */ 1066 } 1067 1068 typedef struct { 1069 Object **child; 1070 void (*check)(Object *, const char *, Object *, Error **); 1071 ObjectPropertyLinkFlags flags; 1072 } LinkProperty; 1073 1074 static void object_get_link_property(Object *obj, Visitor *v, void *opaque, 1075 const char *name, Error **errp) 1076 { 1077 LinkProperty *lprop = opaque; 1078 Object **child = lprop->child; 1079 gchar *path; 1080 1081 if (*child) { 1082 path = object_get_canonical_path(*child); 1083 visit_type_str(v, &path, name, errp); 1084 g_free(path); 1085 } else { 1086 path = (gchar *)""; 1087 visit_type_str(v, &path, name, errp); 1088 } 1089 } 1090 1091 /* 1092 * object_resolve_link: 1093 * 1094 * Lookup an object and ensure its type matches the link property type. This 1095 * is similar to object_resolve_path() except type verification against the 1096 * link property is performed. 1097 * 1098 * Returns: The matched object or NULL on path lookup failures. 1099 */ 1100 static Object *object_resolve_link(Object *obj, const char *name, 1101 const char *path, Error **errp) 1102 { 1103 const char *type; 1104 gchar *target_type; 1105 bool ambiguous = false; 1106 Object *target; 1107 1108 /* Go from link<FOO> to FOO. */ 1109 type = object_property_get_type(obj, name, NULL); 1110 target_type = g_strndup(&type[5], strlen(type) - 6); 1111 target = object_resolve_path_type(path, target_type, &ambiguous); 1112 1113 if (ambiguous) { 1114 error_set(errp, ERROR_CLASS_GENERIC_ERROR, 1115 "Path '%s' does not uniquely identify an object", path); 1116 } else if (!target) { 1117 target = object_resolve_path(path, &ambiguous); 1118 if (target || ambiguous) { 1119 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type); 1120 } else { 1121 error_set(errp, QERR_DEVICE_NOT_FOUND, path); 1122 } 1123 target = NULL; 1124 } 1125 g_free(target_type); 1126 1127 return target; 1128 } 1129 1130 static void object_set_link_property(Object *obj, Visitor *v, void *opaque, 1131 const char *name, Error **errp) 1132 { 1133 Error *local_err = NULL; 1134 LinkProperty *prop = opaque; 1135 Object **child = prop->child; 1136 Object *old_target = *child; 1137 Object *new_target = NULL; 1138 char *path = NULL; 1139 1140 visit_type_str(v, &path, name, &local_err); 1141 1142 if (!local_err && strcmp(path, "") != 0) { 1143 new_target = object_resolve_link(obj, name, path, &local_err); 1144 } 1145 1146 g_free(path); 1147 if (local_err) { 1148 error_propagate(errp, local_err); 1149 return; 1150 } 1151 1152 prop->check(obj, name, new_target, &local_err); 1153 if (local_err) { 1154 error_propagate(errp, local_err); 1155 return; 1156 } 1157 1158 if (new_target) { 1159 object_ref(new_target); 1160 } 1161 *child = new_target; 1162 if (old_target != NULL) { 1163 object_unref(old_target); 1164 } 1165 } 1166 1167 static void object_release_link_property(Object *obj, const char *name, 1168 void *opaque) 1169 { 1170 LinkProperty *prop = opaque; 1171 1172 if ((prop->flags & OBJ_PROP_LINK_UNREF_ON_RELEASE) && *prop->child) { 1173 object_unref(*prop->child); 1174 } 1175 g_free(prop); 1176 } 1177 1178 void object_property_add_link(Object *obj, const char *name, 1179 const char *type, Object **child, 1180 void (*check)(Object *, const char *, 1181 Object *, Error **), 1182 ObjectPropertyLinkFlags flags, 1183 Error **errp) 1184 { 1185 Error *local_err = NULL; 1186 LinkProperty *prop = g_malloc(sizeof(*prop)); 1187 gchar *full_type; 1188 1189 prop->child = child; 1190 prop->check = check; 1191 prop->flags = flags; 1192 1193 full_type = g_strdup_printf("link<%s>", type); 1194 1195 object_property_add(obj, name, full_type, 1196 object_get_link_property, 1197 check ? object_set_link_property : NULL, 1198 object_release_link_property, 1199 prop, 1200 &local_err); 1201 if (local_err) { 1202 error_propagate(errp, local_err); 1203 g_free(prop); 1204 } 1205 1206 g_free(full_type); 1207 } 1208 1209 gchar *object_get_canonical_path_component(Object *obj) 1210 { 1211 ObjectProperty *prop = NULL; 1212 1213 g_assert(obj); 1214 g_assert(obj->parent != NULL); 1215 1216 QTAILQ_FOREACH(prop, &obj->parent->properties, node) { 1217 if (!object_property_is_child(prop)) { 1218 continue; 1219 } 1220 1221 if (prop->opaque == obj) { 1222 return g_strdup(prop->name); 1223 } 1224 } 1225 1226 /* obj had a parent but was not a child, should never happen */ 1227 g_assert_not_reached(); 1228 return NULL; 1229 } 1230 1231 gchar *object_get_canonical_path(Object *obj) 1232 { 1233 Object *root = object_get_root(); 1234 char *newpath, *path = NULL; 1235 1236 while (obj != root) { 1237 char *component = object_get_canonical_path_component(obj); 1238 1239 if (path) { 1240 newpath = g_strdup_printf("%s/%s", component, path); 1241 g_free(component); 1242 g_free(path); 1243 path = newpath; 1244 } else { 1245 path = component; 1246 } 1247 1248 obj = obj->parent; 1249 } 1250 1251 newpath = g_strdup_printf("/%s", path ? path : ""); 1252 g_free(path); 1253 1254 return newpath; 1255 } 1256 1257 Object *object_resolve_path_component(Object *parent, const gchar *part) 1258 { 1259 ObjectProperty *prop = object_property_find(parent, part, NULL); 1260 if (prop == NULL) { 1261 return NULL; 1262 } 1263 1264 if (object_property_is_link(prop)) { 1265 LinkProperty *lprop = prop->opaque; 1266 return *lprop->child; 1267 } else if (object_property_is_child(prop)) { 1268 return prop->opaque; 1269 } else { 1270 return NULL; 1271 } 1272 } 1273 1274 static Object *object_resolve_abs_path(Object *parent, 1275 gchar **parts, 1276 const char *typename, 1277 int index) 1278 { 1279 Object *child; 1280 1281 if (parts[index] == NULL) { 1282 return object_dynamic_cast(parent, typename); 1283 } 1284 1285 if (strcmp(parts[index], "") == 0) { 1286 return object_resolve_abs_path(parent, parts, typename, index + 1); 1287 } 1288 1289 child = object_resolve_path_component(parent, parts[index]); 1290 if (!child) { 1291 return NULL; 1292 } 1293 1294 return object_resolve_abs_path(child, parts, typename, index + 1); 1295 } 1296 1297 static Object *object_resolve_partial_path(Object *parent, 1298 gchar **parts, 1299 const char *typename, 1300 bool *ambiguous) 1301 { 1302 Object *obj; 1303 ObjectProperty *prop; 1304 1305 obj = object_resolve_abs_path(parent, parts, typename, 0); 1306 1307 QTAILQ_FOREACH(prop, &parent->properties, node) { 1308 Object *found; 1309 1310 if (!object_property_is_child(prop)) { 1311 continue; 1312 } 1313 1314 found = object_resolve_partial_path(prop->opaque, parts, 1315 typename, ambiguous); 1316 if (found) { 1317 if (obj) { 1318 if (ambiguous) { 1319 *ambiguous = true; 1320 } 1321 return NULL; 1322 } 1323 obj = found; 1324 } 1325 1326 if (ambiguous && *ambiguous) { 1327 return NULL; 1328 } 1329 } 1330 1331 return obj; 1332 } 1333 1334 Object *object_resolve_path_type(const char *path, const char *typename, 1335 bool *ambiguous) 1336 { 1337 Object *obj; 1338 gchar **parts; 1339 1340 parts = g_strsplit(path, "/", 0); 1341 assert(parts); 1342 1343 if (parts[0] == NULL || strcmp(parts[0], "") != 0) { 1344 if (ambiguous) { 1345 *ambiguous = false; 1346 } 1347 obj = object_resolve_partial_path(object_get_root(), parts, 1348 typename, ambiguous); 1349 } else { 1350 obj = object_resolve_abs_path(object_get_root(), parts, typename, 1); 1351 } 1352 1353 g_strfreev(parts); 1354 1355 return obj; 1356 } 1357 1358 Object *object_resolve_path(const char *path, bool *ambiguous) 1359 { 1360 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous); 1361 } 1362 1363 typedef struct StringProperty 1364 { 1365 char *(*get)(Object *, Error **); 1366 void (*set)(Object *, const char *, Error **); 1367 } StringProperty; 1368 1369 static void property_get_str(Object *obj, Visitor *v, void *opaque, 1370 const char *name, Error **errp) 1371 { 1372 StringProperty *prop = opaque; 1373 char *value; 1374 1375 value = prop->get(obj, errp); 1376 if (value) { 1377 visit_type_str(v, &value, name, errp); 1378 g_free(value); 1379 } 1380 } 1381 1382 static void property_set_str(Object *obj, Visitor *v, void *opaque, 1383 const char *name, Error **errp) 1384 { 1385 StringProperty *prop = opaque; 1386 char *value; 1387 Error *local_err = NULL; 1388 1389 visit_type_str(v, &value, name, &local_err); 1390 if (local_err) { 1391 error_propagate(errp, local_err); 1392 return; 1393 } 1394 1395 prop->set(obj, value, errp); 1396 g_free(value); 1397 } 1398 1399 static void property_release_str(Object *obj, const char *name, 1400 void *opaque) 1401 { 1402 StringProperty *prop = opaque; 1403 g_free(prop); 1404 } 1405 1406 void object_property_add_str(Object *obj, const char *name, 1407 char *(*get)(Object *, Error **), 1408 void (*set)(Object *, const char *, Error **), 1409 Error **errp) 1410 { 1411 Error *local_err = NULL; 1412 StringProperty *prop = g_malloc0(sizeof(*prop)); 1413 1414 prop->get = get; 1415 prop->set = set; 1416 1417 object_property_add(obj, name, "string", 1418 get ? property_get_str : NULL, 1419 set ? property_set_str : NULL, 1420 property_release_str, 1421 prop, &local_err); 1422 if (local_err) { 1423 error_propagate(errp, local_err); 1424 g_free(prop); 1425 } 1426 } 1427 1428 typedef struct BoolProperty 1429 { 1430 bool (*get)(Object *, Error **); 1431 void (*set)(Object *, bool, Error **); 1432 } BoolProperty; 1433 1434 static void property_get_bool(Object *obj, Visitor *v, void *opaque, 1435 const char *name, Error **errp) 1436 { 1437 BoolProperty *prop = opaque; 1438 bool value; 1439 1440 value = prop->get(obj, errp); 1441 visit_type_bool(v, &value, name, errp); 1442 } 1443 1444 static void property_set_bool(Object *obj, Visitor *v, void *opaque, 1445 const char *name, Error **errp) 1446 { 1447 BoolProperty *prop = opaque; 1448 bool value; 1449 Error *local_err = NULL; 1450 1451 visit_type_bool(v, &value, name, &local_err); 1452 if (local_err) { 1453 error_propagate(errp, local_err); 1454 return; 1455 } 1456 1457 prop->set(obj, value, errp); 1458 } 1459 1460 static void property_release_bool(Object *obj, const char *name, 1461 void *opaque) 1462 { 1463 BoolProperty *prop = opaque; 1464 g_free(prop); 1465 } 1466 1467 void object_property_add_bool(Object *obj, const char *name, 1468 bool (*get)(Object *, Error **), 1469 void (*set)(Object *, bool, Error **), 1470 Error **errp) 1471 { 1472 Error *local_err = NULL; 1473 BoolProperty *prop = g_malloc0(sizeof(*prop)); 1474 1475 prop->get = get; 1476 prop->set = set; 1477 1478 object_property_add(obj, name, "bool", 1479 get ? property_get_bool : NULL, 1480 set ? property_set_bool : NULL, 1481 property_release_bool, 1482 prop, &local_err); 1483 if (local_err) { 1484 error_propagate(errp, local_err); 1485 g_free(prop); 1486 } 1487 } 1488 1489 static char *qdev_get_type(Object *obj, Error **errp) 1490 { 1491 return g_strdup(object_get_typename(obj)); 1492 } 1493 1494 static void property_get_uint8_ptr(Object *obj, Visitor *v, 1495 void *opaque, const char *name, 1496 Error **errp) 1497 { 1498 uint8_t value = *(uint8_t *)opaque; 1499 visit_type_uint8(v, &value, name, errp); 1500 } 1501 1502 static void property_get_uint16_ptr(Object *obj, Visitor *v, 1503 void *opaque, const char *name, 1504 Error **errp) 1505 { 1506 uint16_t value = *(uint16_t *)opaque; 1507 visit_type_uint16(v, &value, name, errp); 1508 } 1509 1510 static void property_get_uint32_ptr(Object *obj, Visitor *v, 1511 void *opaque, const char *name, 1512 Error **errp) 1513 { 1514 uint32_t value = *(uint32_t *)opaque; 1515 visit_type_uint32(v, &value, name, errp); 1516 } 1517 1518 static void property_get_uint64_ptr(Object *obj, Visitor *v, 1519 void *opaque, const char *name, 1520 Error **errp) 1521 { 1522 uint64_t value = *(uint64_t *)opaque; 1523 visit_type_uint64(v, &value, name, errp); 1524 } 1525 1526 void object_property_add_uint8_ptr(Object *obj, const char *name, 1527 const uint8_t *v, Error **errp) 1528 { 1529 object_property_add(obj, name, "uint8", property_get_uint8_ptr, 1530 NULL, NULL, (void *)v, errp); 1531 } 1532 1533 void object_property_add_uint16_ptr(Object *obj, const char *name, 1534 const uint16_t *v, Error **errp) 1535 { 1536 object_property_add(obj, name, "uint16", property_get_uint16_ptr, 1537 NULL, NULL, (void *)v, errp); 1538 } 1539 1540 void object_property_add_uint32_ptr(Object *obj, const char *name, 1541 const uint32_t *v, Error **errp) 1542 { 1543 object_property_add(obj, name, "uint32", property_get_uint32_ptr, 1544 NULL, NULL, (void *)v, errp); 1545 } 1546 1547 void object_property_add_uint64_ptr(Object *obj, const char *name, 1548 const uint64_t *v, Error **errp) 1549 { 1550 object_property_add(obj, name, "uint64", property_get_uint64_ptr, 1551 NULL, NULL, (void *)v, errp); 1552 } 1553 1554 static void object_instance_init(Object *obj) 1555 { 1556 object_property_add_str(obj, "type", qdev_get_type, NULL, NULL); 1557 } 1558 1559 static void register_types(void) 1560 { 1561 static TypeInfo interface_info = { 1562 .name = TYPE_INTERFACE, 1563 .class_size = sizeof(InterfaceClass), 1564 .abstract = true, 1565 }; 1566 1567 static TypeInfo object_info = { 1568 .name = TYPE_OBJECT, 1569 .instance_size = sizeof(Object), 1570 .instance_init = object_instance_init, 1571 .abstract = true, 1572 }; 1573 1574 type_interface = type_register_internal(&interface_info); 1575 type_register_internal(&object_info); 1576 } 1577 1578 type_init(register_types) 1579