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