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