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 void object_property_del_all(Object *obj) 360 { 361 while (!QTAILQ_EMPTY(&obj->properties)) { 362 ObjectProperty *prop = QTAILQ_FIRST(&obj->properties); 363 364 QTAILQ_REMOVE(&obj->properties, prop, node); 365 366 if (prop->release) { 367 prop->release(obj, prop->name, prop->opaque); 368 } 369 370 g_free(prop->name); 371 g_free(prop->type); 372 g_free(prop->description); 373 g_free(prop); 374 } 375 } 376 377 static void object_property_del_child(Object *obj, Object *child, Error **errp) 378 { 379 ObjectProperty *prop; 380 381 QTAILQ_FOREACH(prop, &obj->properties, node) { 382 if (object_property_is_child(prop) && prop->opaque == child) { 383 object_property_del(obj, prop->name, errp); 384 break; 385 } 386 } 387 } 388 389 void object_unparent(Object *obj) 390 { 391 if (obj->parent) { 392 object_property_del_child(obj->parent, obj, NULL); 393 } 394 } 395 396 static void object_deinit(Object *obj, TypeImpl *type) 397 { 398 if (type->instance_finalize) { 399 type->instance_finalize(obj); 400 } 401 402 if (type_has_parent(type)) { 403 object_deinit(obj, type_get_parent(type)); 404 } 405 } 406 407 static void object_finalize(void *data) 408 { 409 Object *obj = data; 410 TypeImpl *ti = obj->class->type; 411 412 object_property_del_all(obj); 413 object_deinit(obj, ti); 414 415 g_assert(obj->ref == 0); 416 if (obj->free) { 417 obj->free(obj); 418 } 419 } 420 421 Object *object_new_with_type(Type type) 422 { 423 Object *obj; 424 425 g_assert(type != NULL); 426 type_initialize(type); 427 428 obj = g_malloc(type->instance_size); 429 object_initialize_with_type(obj, type->instance_size, type); 430 obj->free = g_free; 431 432 return obj; 433 } 434 435 Object *object_new(const char *typename) 436 { 437 TypeImpl *ti = type_get_by_name(typename); 438 439 return object_new_with_type(ti); 440 } 441 442 Object *object_dynamic_cast(Object *obj, const char *typename) 443 { 444 if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) { 445 return obj; 446 } 447 448 return NULL; 449 } 450 451 Object *object_dynamic_cast_assert(Object *obj, const char *typename, 452 const char *file, int line, const char *func) 453 { 454 trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)", 455 typename, file, line, func); 456 457 #ifdef CONFIG_QOM_CAST_DEBUG 458 int i; 459 Object *inst; 460 461 for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) { 462 if (obj->class->object_cast_cache[i] == typename) { 463 goto out; 464 } 465 } 466 467 inst = object_dynamic_cast(obj, typename); 468 469 if (!inst && obj) { 470 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n", 471 file, line, func, obj, typename); 472 abort(); 473 } 474 475 assert(obj == inst); 476 477 if (obj && obj == inst) { 478 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) { 479 obj->class->object_cast_cache[i - 1] = 480 obj->class->object_cast_cache[i]; 481 } 482 obj->class->object_cast_cache[i - 1] = typename; 483 } 484 485 out: 486 #endif 487 return obj; 488 } 489 490 ObjectClass *object_class_dynamic_cast(ObjectClass *class, 491 const char *typename) 492 { 493 ObjectClass *ret = NULL; 494 TypeImpl *target_type; 495 TypeImpl *type; 496 497 if (!class) { 498 return NULL; 499 } 500 501 /* A simple fast path that can trigger a lot for leaf classes. */ 502 type = class->type; 503 if (type->name == typename) { 504 return class; 505 } 506 507 target_type = type_get_by_name(typename); 508 if (!target_type) { 509 /* target class type unknown, so fail the cast */ 510 return NULL; 511 } 512 513 if (type->class->interfaces && 514 type_is_ancestor(target_type, type_interface)) { 515 int found = 0; 516 GSList *i; 517 518 for (i = class->interfaces; i; i = i->next) { 519 ObjectClass *target_class = i->data; 520 521 if (type_is_ancestor(target_class->type, target_type)) { 522 ret = target_class; 523 found++; 524 } 525 } 526 527 /* The match was ambiguous, don't allow a cast */ 528 if (found > 1) { 529 ret = NULL; 530 } 531 } else if (type_is_ancestor(type, target_type)) { 532 ret = class; 533 } 534 535 return ret; 536 } 537 538 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class, 539 const char *typename, 540 const char *file, int line, 541 const char *func) 542 { 543 ObjectClass *ret; 544 545 trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)", 546 typename, file, line, func); 547 548 #ifdef CONFIG_QOM_CAST_DEBUG 549 int i; 550 551 for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) { 552 if (class->class_cast_cache[i] == typename) { 553 ret = class; 554 goto out; 555 } 556 } 557 #else 558 if (!class || !class->interfaces) { 559 return class; 560 } 561 #endif 562 563 ret = object_class_dynamic_cast(class, typename); 564 if (!ret && class) { 565 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n", 566 file, line, func, class, typename); 567 abort(); 568 } 569 570 #ifdef CONFIG_QOM_CAST_DEBUG 571 if (class && ret == class) { 572 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) { 573 class->class_cast_cache[i - 1] = class->class_cast_cache[i]; 574 } 575 class->class_cast_cache[i - 1] = typename; 576 } 577 out: 578 #endif 579 return ret; 580 } 581 582 const char *object_get_typename(Object *obj) 583 { 584 return obj->class->type->name; 585 } 586 587 ObjectClass *object_get_class(Object *obj) 588 { 589 return obj->class; 590 } 591 592 bool object_class_is_abstract(ObjectClass *klass) 593 { 594 return klass->type->abstract; 595 } 596 597 const char *object_class_get_name(ObjectClass *klass) 598 { 599 return klass->type->name; 600 } 601 602 ObjectClass *object_class_by_name(const char *typename) 603 { 604 TypeImpl *type = type_get_by_name(typename); 605 606 if (!type) { 607 return NULL; 608 } 609 610 type_initialize(type); 611 612 return type->class; 613 } 614 615 ObjectClass *object_class_get_parent(ObjectClass *class) 616 { 617 TypeImpl *type = type_get_parent(class->type); 618 619 if (!type) { 620 return NULL; 621 } 622 623 type_initialize(type); 624 625 return type->class; 626 } 627 628 typedef struct OCFData 629 { 630 void (*fn)(ObjectClass *klass, void *opaque); 631 const char *implements_type; 632 bool include_abstract; 633 void *opaque; 634 } OCFData; 635 636 static void object_class_foreach_tramp(gpointer key, gpointer value, 637 gpointer opaque) 638 { 639 OCFData *data = opaque; 640 TypeImpl *type = value; 641 ObjectClass *k; 642 643 type_initialize(type); 644 k = type->class; 645 646 if (!data->include_abstract && type->abstract) { 647 return; 648 } 649 650 if (data->implements_type && 651 !object_class_dynamic_cast(k, data->implements_type)) { 652 return; 653 } 654 655 data->fn(k, data->opaque); 656 } 657 658 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque), 659 const char *implements_type, bool include_abstract, 660 void *opaque) 661 { 662 OCFData data = { fn, implements_type, include_abstract, opaque }; 663 664 enumerating_types = true; 665 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data); 666 enumerating_types = false; 667 } 668 669 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque), 670 void *opaque) 671 { 672 ObjectProperty *prop, *next; 673 int ret = 0; 674 675 QTAILQ_FOREACH_SAFE(prop, &obj->properties, node, next) { 676 if (object_property_is_child(prop)) { 677 ret = fn(prop->opaque, opaque); 678 if (ret != 0) { 679 break; 680 } 681 } 682 } 683 return ret; 684 } 685 686 static void object_class_get_list_tramp(ObjectClass *klass, void *opaque) 687 { 688 GSList **list = opaque; 689 690 *list = g_slist_prepend(*list, klass); 691 } 692 693 GSList *object_class_get_list(const char *implements_type, 694 bool include_abstract) 695 { 696 GSList *list = NULL; 697 698 object_class_foreach(object_class_get_list_tramp, 699 implements_type, include_abstract, &list); 700 return list; 701 } 702 703 void object_ref(Object *obj) 704 { 705 if (!obj) { 706 return; 707 } 708 atomic_inc(&obj->ref); 709 } 710 711 void object_unref(Object *obj) 712 { 713 if (!obj) { 714 return; 715 } 716 g_assert(obj->ref > 0); 717 718 /* parent always holds a reference to its children */ 719 if (atomic_fetch_dec(&obj->ref) == 1) { 720 object_finalize(obj); 721 } 722 } 723 724 ObjectProperty * 725 object_property_add(Object *obj, const char *name, const char *type, 726 ObjectPropertyAccessor *get, 727 ObjectPropertyAccessor *set, 728 ObjectPropertyRelease *release, 729 void *opaque, Error **errp) 730 { 731 ObjectProperty *prop; 732 size_t name_len = strlen(name); 733 734 if (name_len >= 3 && !memcmp(name + name_len - 3, "[*]", 4)) { 735 int i; 736 ObjectProperty *ret; 737 char *name_no_array = g_strdup(name); 738 739 name_no_array[name_len - 3] = '\0'; 740 for (i = 0; ; ++i) { 741 char *full_name = g_strdup_printf("%s[%d]", name_no_array, i); 742 743 ret = object_property_add(obj, full_name, type, get, set, 744 release, opaque, NULL); 745 g_free(full_name); 746 if (ret) { 747 break; 748 } 749 } 750 g_free(name_no_array); 751 return ret; 752 } 753 754 QTAILQ_FOREACH(prop, &obj->properties, node) { 755 if (strcmp(prop->name, name) == 0) { 756 error_setg(errp, "attempt to add duplicate property '%s'" 757 " to object (type '%s')", name, 758 object_get_typename(obj)); 759 return NULL; 760 } 761 } 762 763 prop = g_malloc0(sizeof(*prop)); 764 765 prop->name = g_strdup(name); 766 prop->type = g_strdup(type); 767 768 prop->get = get; 769 prop->set = set; 770 prop->release = release; 771 prop->opaque = opaque; 772 773 QTAILQ_INSERT_TAIL(&obj->properties, prop, node); 774 return prop; 775 } 776 777 ObjectProperty *object_property_find(Object *obj, const char *name, 778 Error **errp) 779 { 780 ObjectProperty *prop; 781 782 QTAILQ_FOREACH(prop, &obj->properties, node) { 783 if (strcmp(prop->name, name) == 0) { 784 return prop; 785 } 786 } 787 788 error_setg(errp, "Property '.%s' not found", name); 789 return NULL; 790 } 791 792 void object_property_del(Object *obj, const char *name, Error **errp) 793 { 794 ObjectProperty *prop = object_property_find(obj, name, errp); 795 if (prop == NULL) { 796 return; 797 } 798 799 if (prop->release) { 800 prop->release(obj, name, prop->opaque); 801 } 802 803 QTAILQ_REMOVE(&obj->properties, prop, node); 804 805 g_free(prop->name); 806 g_free(prop->type); 807 g_free(prop->description); 808 g_free(prop); 809 } 810 811 void object_property_get(Object *obj, Visitor *v, const char *name, 812 Error **errp) 813 { 814 ObjectProperty *prop = object_property_find(obj, name, errp); 815 if (prop == NULL) { 816 return; 817 } 818 819 if (!prop->get) { 820 error_set(errp, QERR_PERMISSION_DENIED); 821 } else { 822 prop->get(obj, v, prop->opaque, name, errp); 823 } 824 } 825 826 void object_property_set(Object *obj, Visitor *v, const char *name, 827 Error **errp) 828 { 829 ObjectProperty *prop = object_property_find(obj, name, errp); 830 if (prop == NULL) { 831 return; 832 } 833 834 if (!prop->set) { 835 error_set(errp, QERR_PERMISSION_DENIED); 836 } else { 837 prop->set(obj, v, prop->opaque, name, errp); 838 } 839 } 840 841 void object_property_set_str(Object *obj, const char *value, 842 const char *name, Error **errp) 843 { 844 QString *qstr = qstring_from_str(value); 845 object_property_set_qobject(obj, QOBJECT(qstr), name, errp); 846 847 QDECREF(qstr); 848 } 849 850 char *object_property_get_str(Object *obj, const char *name, 851 Error **errp) 852 { 853 QObject *ret = object_property_get_qobject(obj, name, errp); 854 QString *qstring; 855 char *retval; 856 857 if (!ret) { 858 return NULL; 859 } 860 qstring = qobject_to_qstring(ret); 861 if (!qstring) { 862 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "string"); 863 retval = NULL; 864 } else { 865 retval = g_strdup(qstring_get_str(qstring)); 866 } 867 868 QDECREF(qstring); 869 return retval; 870 } 871 872 void object_property_set_link(Object *obj, Object *value, 873 const char *name, Error **errp) 874 { 875 gchar *path = object_get_canonical_path(value); 876 object_property_set_str(obj, path, name, errp); 877 g_free(path); 878 } 879 880 Object *object_property_get_link(Object *obj, const char *name, 881 Error **errp) 882 { 883 char *str = object_property_get_str(obj, name, errp); 884 Object *target = NULL; 885 886 if (str && *str) { 887 target = object_resolve_path(str, NULL); 888 if (!target) { 889 error_set(errp, QERR_DEVICE_NOT_FOUND, str); 890 } 891 } 892 893 g_free(str); 894 return target; 895 } 896 897 void object_property_set_bool(Object *obj, bool value, 898 const char *name, Error **errp) 899 { 900 QBool *qbool = qbool_from_int(value); 901 object_property_set_qobject(obj, QOBJECT(qbool), name, errp); 902 903 QDECREF(qbool); 904 } 905 906 bool object_property_get_bool(Object *obj, const char *name, 907 Error **errp) 908 { 909 QObject *ret = object_property_get_qobject(obj, name, errp); 910 QBool *qbool; 911 bool retval; 912 913 if (!ret) { 914 return false; 915 } 916 qbool = qobject_to_qbool(ret); 917 if (!qbool) { 918 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean"); 919 retval = false; 920 } else { 921 retval = qbool_get_int(qbool); 922 } 923 924 QDECREF(qbool); 925 return retval; 926 } 927 928 void object_property_set_int(Object *obj, int64_t value, 929 const char *name, Error **errp) 930 { 931 QInt *qint = qint_from_int(value); 932 object_property_set_qobject(obj, QOBJECT(qint), name, errp); 933 934 QDECREF(qint); 935 } 936 937 int64_t object_property_get_int(Object *obj, const char *name, 938 Error **errp) 939 { 940 QObject *ret = object_property_get_qobject(obj, name, errp); 941 QInt *qint; 942 int64_t retval; 943 944 if (!ret) { 945 return -1; 946 } 947 qint = qobject_to_qint(ret); 948 if (!qint) { 949 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "int"); 950 retval = -1; 951 } else { 952 retval = qint_get_int(qint); 953 } 954 955 QDECREF(qint); 956 return retval; 957 } 958 959 int object_property_get_enum(Object *obj, const char *name, 960 const char *strings[], Error **errp) 961 { 962 StringOutputVisitor *sov; 963 StringInputVisitor *siv; 964 char *str; 965 int ret; 966 967 sov = string_output_visitor_new(false); 968 object_property_get(obj, string_output_get_visitor(sov), name, errp); 969 str = string_output_get_string(sov); 970 siv = string_input_visitor_new(str); 971 string_output_visitor_cleanup(sov); 972 visit_type_enum(string_input_get_visitor(siv), 973 &ret, strings, NULL, name, errp); 974 975 g_free(str); 976 string_input_visitor_cleanup(siv); 977 978 return ret; 979 } 980 981 void object_property_get_uint16List(Object *obj, const char *name, 982 uint16List **list, Error **errp) 983 { 984 StringOutputVisitor *ov; 985 StringInputVisitor *iv; 986 char *str; 987 988 ov = string_output_visitor_new(false); 989 object_property_get(obj, string_output_get_visitor(ov), 990 name, errp); 991 str = string_output_get_string(ov); 992 iv = string_input_visitor_new(str); 993 visit_type_uint16List(string_input_get_visitor(iv), 994 list, NULL, errp); 995 996 g_free(str); 997 string_output_visitor_cleanup(ov); 998 string_input_visitor_cleanup(iv); 999 } 1000 1001 void object_property_parse(Object *obj, const char *string, 1002 const char *name, Error **errp) 1003 { 1004 StringInputVisitor *mi; 1005 mi = string_input_visitor_new(string); 1006 object_property_set(obj, string_input_get_visitor(mi), name, errp); 1007 1008 string_input_visitor_cleanup(mi); 1009 } 1010 1011 char *object_property_print(Object *obj, const char *name, bool human, 1012 Error **errp) 1013 { 1014 StringOutputVisitor *mo; 1015 char *string = NULL; 1016 Error *local_err = NULL; 1017 1018 mo = string_output_visitor_new(human); 1019 object_property_get(obj, string_output_get_visitor(mo), name, &local_err); 1020 if (local_err) { 1021 error_propagate(errp, local_err); 1022 goto out; 1023 } 1024 1025 string = string_output_get_string(mo); 1026 1027 out: 1028 string_output_visitor_cleanup(mo); 1029 return string; 1030 } 1031 1032 const char *object_property_get_type(Object *obj, const char *name, Error **errp) 1033 { 1034 ObjectProperty *prop = object_property_find(obj, name, errp); 1035 if (prop == NULL) { 1036 return NULL; 1037 } 1038 1039 return prop->type; 1040 } 1041 1042 Object *object_get_root(void) 1043 { 1044 static Object *root; 1045 1046 if (!root) { 1047 root = object_new("container"); 1048 } 1049 1050 return root; 1051 } 1052 1053 static void object_get_child_property(Object *obj, Visitor *v, void *opaque, 1054 const char *name, Error **errp) 1055 { 1056 Object *child = opaque; 1057 gchar *path; 1058 1059 path = object_get_canonical_path(child); 1060 visit_type_str(v, &path, name, errp); 1061 g_free(path); 1062 } 1063 1064 static Object *object_resolve_child_property(Object *parent, void *opaque, const gchar *part) 1065 { 1066 return opaque; 1067 } 1068 1069 static void object_finalize_child_property(Object *obj, const char *name, 1070 void *opaque) 1071 { 1072 Object *child = opaque; 1073 1074 if (child->class->unparent) { 1075 (child->class->unparent)(child); 1076 } 1077 child->parent = NULL; 1078 object_unref(child); 1079 } 1080 1081 void object_property_add_child(Object *obj, const char *name, 1082 Object *child, Error **errp) 1083 { 1084 Error *local_err = NULL; 1085 gchar *type; 1086 ObjectProperty *op; 1087 1088 type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child))); 1089 1090 op = object_property_add(obj, name, type, object_get_child_property, NULL, 1091 object_finalize_child_property, child, &local_err); 1092 if (local_err) { 1093 error_propagate(errp, local_err); 1094 goto out; 1095 } 1096 1097 op->resolve = object_resolve_child_property; 1098 object_ref(child); 1099 g_assert(child->parent == NULL); 1100 child->parent = obj; 1101 1102 out: 1103 g_free(type); 1104 } 1105 1106 void object_property_allow_set_link(Object *obj, const char *name, 1107 Object *val, Error **errp) 1108 { 1109 /* Allow the link to be set, always */ 1110 } 1111 1112 typedef struct { 1113 Object **child; 1114 void (*check)(Object *, const char *, Object *, Error **); 1115 ObjectPropertyLinkFlags flags; 1116 } LinkProperty; 1117 1118 static void object_get_link_property(Object *obj, Visitor *v, void *opaque, 1119 const char *name, Error **errp) 1120 { 1121 LinkProperty *lprop = opaque; 1122 Object **child = lprop->child; 1123 gchar *path; 1124 1125 if (*child) { 1126 path = object_get_canonical_path(*child); 1127 visit_type_str(v, &path, name, errp); 1128 g_free(path); 1129 } else { 1130 path = (gchar *)""; 1131 visit_type_str(v, &path, name, errp); 1132 } 1133 } 1134 1135 /* 1136 * object_resolve_link: 1137 * 1138 * Lookup an object and ensure its type matches the link property type. This 1139 * is similar to object_resolve_path() except type verification against the 1140 * link property is performed. 1141 * 1142 * Returns: The matched object or NULL on path lookup failures. 1143 */ 1144 static Object *object_resolve_link(Object *obj, const char *name, 1145 const char *path, Error **errp) 1146 { 1147 const char *type; 1148 gchar *target_type; 1149 bool ambiguous = false; 1150 Object *target; 1151 1152 /* Go from link<FOO> to FOO. */ 1153 type = object_property_get_type(obj, name, NULL); 1154 target_type = g_strndup(&type[5], strlen(type) - 6); 1155 target = object_resolve_path_type(path, target_type, &ambiguous); 1156 1157 if (ambiguous) { 1158 error_set(errp, ERROR_CLASS_GENERIC_ERROR, 1159 "Path '%s' does not uniquely identify an object", path); 1160 } else if (!target) { 1161 target = object_resolve_path(path, &ambiguous); 1162 if (target || ambiguous) { 1163 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type); 1164 } else { 1165 error_set(errp, QERR_DEVICE_NOT_FOUND, path); 1166 } 1167 target = NULL; 1168 } 1169 g_free(target_type); 1170 1171 return target; 1172 } 1173 1174 static void object_set_link_property(Object *obj, Visitor *v, void *opaque, 1175 const char *name, Error **errp) 1176 { 1177 Error *local_err = NULL; 1178 LinkProperty *prop = opaque; 1179 Object **child = prop->child; 1180 Object *old_target = *child; 1181 Object *new_target = NULL; 1182 char *path = NULL; 1183 1184 visit_type_str(v, &path, name, &local_err); 1185 1186 if (!local_err && strcmp(path, "") != 0) { 1187 new_target = object_resolve_link(obj, name, path, &local_err); 1188 } 1189 1190 g_free(path); 1191 if (local_err) { 1192 error_propagate(errp, local_err); 1193 return; 1194 } 1195 1196 prop->check(obj, name, new_target, &local_err); 1197 if (local_err) { 1198 error_propagate(errp, local_err); 1199 return; 1200 } 1201 1202 object_ref(new_target); 1203 *child = new_target; 1204 object_unref(old_target); 1205 } 1206 1207 static Object *object_resolve_link_property(Object *parent, void *opaque, const gchar *part) 1208 { 1209 LinkProperty *lprop = opaque; 1210 1211 return *lprop->child; 1212 } 1213 1214 static void object_release_link_property(Object *obj, const char *name, 1215 void *opaque) 1216 { 1217 LinkProperty *prop = opaque; 1218 1219 if ((prop->flags & OBJ_PROP_LINK_UNREF_ON_RELEASE) && *prop->child) { 1220 object_unref(*prop->child); 1221 } 1222 g_free(prop); 1223 } 1224 1225 void object_property_add_link(Object *obj, const char *name, 1226 const char *type, Object **child, 1227 void (*check)(Object *, const char *, 1228 Object *, Error **), 1229 ObjectPropertyLinkFlags flags, 1230 Error **errp) 1231 { 1232 Error *local_err = NULL; 1233 LinkProperty *prop = g_malloc(sizeof(*prop)); 1234 gchar *full_type; 1235 ObjectProperty *op; 1236 1237 prop->child = child; 1238 prop->check = check; 1239 prop->flags = flags; 1240 1241 full_type = g_strdup_printf("link<%s>", type); 1242 1243 op = object_property_add(obj, name, full_type, 1244 object_get_link_property, 1245 check ? object_set_link_property : NULL, 1246 object_release_link_property, 1247 prop, 1248 &local_err); 1249 if (local_err) { 1250 error_propagate(errp, local_err); 1251 g_free(prop); 1252 goto out; 1253 } 1254 1255 op->resolve = object_resolve_link_property; 1256 1257 out: 1258 g_free(full_type); 1259 } 1260 1261 gchar *object_get_canonical_path_component(Object *obj) 1262 { 1263 ObjectProperty *prop = NULL; 1264 1265 g_assert(obj); 1266 g_assert(obj->parent != NULL); 1267 1268 QTAILQ_FOREACH(prop, &obj->parent->properties, node) { 1269 if (!object_property_is_child(prop)) { 1270 continue; 1271 } 1272 1273 if (prop->opaque == obj) { 1274 return g_strdup(prop->name); 1275 } 1276 } 1277 1278 /* obj had a parent but was not a child, should never happen */ 1279 g_assert_not_reached(); 1280 return NULL; 1281 } 1282 1283 gchar *object_get_canonical_path(Object *obj) 1284 { 1285 Object *root = object_get_root(); 1286 char *newpath, *path = NULL; 1287 1288 while (obj != root) { 1289 char *component = object_get_canonical_path_component(obj); 1290 1291 if (path) { 1292 newpath = g_strdup_printf("%s/%s", component, path); 1293 g_free(component); 1294 g_free(path); 1295 path = newpath; 1296 } else { 1297 path = component; 1298 } 1299 1300 obj = obj->parent; 1301 } 1302 1303 newpath = g_strdup_printf("/%s", path ? path : ""); 1304 g_free(path); 1305 1306 return newpath; 1307 } 1308 1309 Object *object_resolve_path_component(Object *parent, const gchar *part) 1310 { 1311 ObjectProperty *prop = object_property_find(parent, part, NULL); 1312 if (prop == NULL) { 1313 return NULL; 1314 } 1315 1316 if (prop->resolve) { 1317 return prop->resolve(parent, prop->opaque, part); 1318 } else { 1319 return NULL; 1320 } 1321 } 1322 1323 static Object *object_resolve_abs_path(Object *parent, 1324 gchar **parts, 1325 const char *typename, 1326 int index) 1327 { 1328 Object *child; 1329 1330 if (parts[index] == NULL) { 1331 return object_dynamic_cast(parent, typename); 1332 } 1333 1334 if (strcmp(parts[index], "") == 0) { 1335 return object_resolve_abs_path(parent, parts, typename, index + 1); 1336 } 1337 1338 child = object_resolve_path_component(parent, parts[index]); 1339 if (!child) { 1340 return NULL; 1341 } 1342 1343 return object_resolve_abs_path(child, parts, typename, index + 1); 1344 } 1345 1346 static Object *object_resolve_partial_path(Object *parent, 1347 gchar **parts, 1348 const char *typename, 1349 bool *ambiguous) 1350 { 1351 Object *obj; 1352 ObjectProperty *prop; 1353 1354 obj = object_resolve_abs_path(parent, parts, typename, 0); 1355 1356 QTAILQ_FOREACH(prop, &parent->properties, node) { 1357 Object *found; 1358 1359 if (!object_property_is_child(prop)) { 1360 continue; 1361 } 1362 1363 found = object_resolve_partial_path(prop->opaque, parts, 1364 typename, ambiguous); 1365 if (found) { 1366 if (obj) { 1367 if (ambiguous) { 1368 *ambiguous = true; 1369 } 1370 return NULL; 1371 } 1372 obj = found; 1373 } 1374 1375 if (ambiguous && *ambiguous) { 1376 return NULL; 1377 } 1378 } 1379 1380 return obj; 1381 } 1382 1383 Object *object_resolve_path_type(const char *path, const char *typename, 1384 bool *ambiguous) 1385 { 1386 Object *obj; 1387 gchar **parts; 1388 1389 parts = g_strsplit(path, "/", 0); 1390 assert(parts); 1391 1392 if (parts[0] == NULL || strcmp(parts[0], "") != 0) { 1393 if (ambiguous) { 1394 *ambiguous = false; 1395 } 1396 obj = object_resolve_partial_path(object_get_root(), parts, 1397 typename, ambiguous); 1398 } else { 1399 obj = object_resolve_abs_path(object_get_root(), parts, typename, 1); 1400 } 1401 1402 g_strfreev(parts); 1403 1404 return obj; 1405 } 1406 1407 Object *object_resolve_path(const char *path, bool *ambiguous) 1408 { 1409 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous); 1410 } 1411 1412 typedef struct StringProperty 1413 { 1414 char *(*get)(Object *, Error **); 1415 void (*set)(Object *, const char *, Error **); 1416 } StringProperty; 1417 1418 static void property_get_str(Object *obj, Visitor *v, void *opaque, 1419 const char *name, Error **errp) 1420 { 1421 StringProperty *prop = opaque; 1422 char *value; 1423 1424 value = prop->get(obj, errp); 1425 if (value) { 1426 visit_type_str(v, &value, name, errp); 1427 g_free(value); 1428 } 1429 } 1430 1431 static void property_set_str(Object *obj, Visitor *v, void *opaque, 1432 const char *name, Error **errp) 1433 { 1434 StringProperty *prop = opaque; 1435 char *value; 1436 Error *local_err = NULL; 1437 1438 visit_type_str(v, &value, name, &local_err); 1439 if (local_err) { 1440 error_propagate(errp, local_err); 1441 return; 1442 } 1443 1444 prop->set(obj, value, errp); 1445 g_free(value); 1446 } 1447 1448 static void property_release_str(Object *obj, const char *name, 1449 void *opaque) 1450 { 1451 StringProperty *prop = opaque; 1452 g_free(prop); 1453 } 1454 1455 void object_property_add_str(Object *obj, const char *name, 1456 char *(*get)(Object *, Error **), 1457 void (*set)(Object *, const char *, Error **), 1458 Error **errp) 1459 { 1460 Error *local_err = NULL; 1461 StringProperty *prop = g_malloc0(sizeof(*prop)); 1462 1463 prop->get = get; 1464 prop->set = set; 1465 1466 object_property_add(obj, name, "string", 1467 get ? property_get_str : NULL, 1468 set ? property_set_str : NULL, 1469 property_release_str, 1470 prop, &local_err); 1471 if (local_err) { 1472 error_propagate(errp, local_err); 1473 g_free(prop); 1474 } 1475 } 1476 1477 typedef struct BoolProperty 1478 { 1479 bool (*get)(Object *, Error **); 1480 void (*set)(Object *, bool, Error **); 1481 } BoolProperty; 1482 1483 static void property_get_bool(Object *obj, Visitor *v, void *opaque, 1484 const char *name, Error **errp) 1485 { 1486 BoolProperty *prop = opaque; 1487 bool value; 1488 1489 value = prop->get(obj, errp); 1490 visit_type_bool(v, &value, name, errp); 1491 } 1492 1493 static void property_set_bool(Object *obj, Visitor *v, void *opaque, 1494 const char *name, Error **errp) 1495 { 1496 BoolProperty *prop = opaque; 1497 bool value; 1498 Error *local_err = NULL; 1499 1500 visit_type_bool(v, &value, name, &local_err); 1501 if (local_err) { 1502 error_propagate(errp, local_err); 1503 return; 1504 } 1505 1506 prop->set(obj, value, errp); 1507 } 1508 1509 static void property_release_bool(Object *obj, const char *name, 1510 void *opaque) 1511 { 1512 BoolProperty *prop = opaque; 1513 g_free(prop); 1514 } 1515 1516 void object_property_add_bool(Object *obj, const char *name, 1517 bool (*get)(Object *, Error **), 1518 void (*set)(Object *, bool, Error **), 1519 Error **errp) 1520 { 1521 Error *local_err = NULL; 1522 BoolProperty *prop = g_malloc0(sizeof(*prop)); 1523 1524 prop->get = get; 1525 prop->set = set; 1526 1527 object_property_add(obj, name, "bool", 1528 get ? property_get_bool : NULL, 1529 set ? property_set_bool : NULL, 1530 property_release_bool, 1531 prop, &local_err); 1532 if (local_err) { 1533 error_propagate(errp, local_err); 1534 g_free(prop); 1535 } 1536 } 1537 1538 static char *qdev_get_type(Object *obj, Error **errp) 1539 { 1540 return g_strdup(object_get_typename(obj)); 1541 } 1542 1543 static void property_get_uint8_ptr(Object *obj, Visitor *v, 1544 void *opaque, const char *name, 1545 Error **errp) 1546 { 1547 uint8_t value = *(uint8_t *)opaque; 1548 visit_type_uint8(v, &value, name, errp); 1549 } 1550 1551 static void property_get_uint16_ptr(Object *obj, Visitor *v, 1552 void *opaque, const char *name, 1553 Error **errp) 1554 { 1555 uint16_t value = *(uint16_t *)opaque; 1556 visit_type_uint16(v, &value, name, errp); 1557 } 1558 1559 static void property_get_uint32_ptr(Object *obj, Visitor *v, 1560 void *opaque, const char *name, 1561 Error **errp) 1562 { 1563 uint32_t value = *(uint32_t *)opaque; 1564 visit_type_uint32(v, &value, name, errp); 1565 } 1566 1567 static void property_get_uint64_ptr(Object *obj, Visitor *v, 1568 void *opaque, const char *name, 1569 Error **errp) 1570 { 1571 uint64_t value = *(uint64_t *)opaque; 1572 visit_type_uint64(v, &value, name, errp); 1573 } 1574 1575 void object_property_add_uint8_ptr(Object *obj, const char *name, 1576 const uint8_t *v, Error **errp) 1577 { 1578 object_property_add(obj, name, "uint8", property_get_uint8_ptr, 1579 NULL, NULL, (void *)v, errp); 1580 } 1581 1582 void object_property_add_uint16_ptr(Object *obj, const char *name, 1583 const uint16_t *v, Error **errp) 1584 { 1585 object_property_add(obj, name, "uint16", property_get_uint16_ptr, 1586 NULL, NULL, (void *)v, errp); 1587 } 1588 1589 void object_property_add_uint32_ptr(Object *obj, const char *name, 1590 const uint32_t *v, Error **errp) 1591 { 1592 object_property_add(obj, name, "uint32", property_get_uint32_ptr, 1593 NULL, NULL, (void *)v, errp); 1594 } 1595 1596 void object_property_add_uint64_ptr(Object *obj, const char *name, 1597 const uint64_t *v, Error **errp) 1598 { 1599 object_property_add(obj, name, "uint64", property_get_uint64_ptr, 1600 NULL, NULL, (void *)v, errp); 1601 } 1602 1603 typedef struct { 1604 Object *target_obj; 1605 const char *target_name; 1606 } AliasProperty; 1607 1608 static void property_get_alias(Object *obj, struct Visitor *v, void *opaque, 1609 const char *name, Error **errp) 1610 { 1611 AliasProperty *prop = opaque; 1612 1613 object_property_get(prop->target_obj, v, prop->target_name, errp); 1614 } 1615 1616 static void property_set_alias(Object *obj, struct Visitor *v, void *opaque, 1617 const char *name, Error **errp) 1618 { 1619 AliasProperty *prop = opaque; 1620 1621 object_property_set(prop->target_obj, v, prop->target_name, errp); 1622 } 1623 1624 static Object *property_resolve_alias(Object *obj, void *opaque, 1625 const gchar *part) 1626 { 1627 AliasProperty *prop = opaque; 1628 1629 return object_resolve_path_component(prop->target_obj, prop->target_name); 1630 } 1631 1632 static void property_release_alias(Object *obj, const char *name, void *opaque) 1633 { 1634 AliasProperty *prop = opaque; 1635 1636 g_free(prop); 1637 } 1638 1639 void object_property_add_alias(Object *obj, const char *name, 1640 Object *target_obj, const char *target_name, 1641 Error **errp) 1642 { 1643 AliasProperty *prop; 1644 ObjectProperty *op; 1645 ObjectProperty *target_prop; 1646 gchar *prop_type; 1647 Error *local_err = NULL; 1648 1649 target_prop = object_property_find(target_obj, target_name, errp); 1650 if (!target_prop) { 1651 return; 1652 } 1653 1654 if (object_property_is_child(target_prop)) { 1655 prop_type = g_strdup_printf("link%s", 1656 target_prop->type + strlen("child")); 1657 } else { 1658 prop_type = g_strdup(target_prop->type); 1659 } 1660 1661 prop = g_malloc(sizeof(*prop)); 1662 prop->target_obj = target_obj; 1663 prop->target_name = target_name; 1664 1665 op = object_property_add(obj, name, prop_type, 1666 property_get_alias, 1667 property_set_alias, 1668 property_release_alias, 1669 prop, &local_err); 1670 if (local_err) { 1671 error_propagate(errp, local_err); 1672 g_free(prop); 1673 goto out; 1674 } 1675 op->resolve = property_resolve_alias; 1676 1677 object_property_set_description(obj, name, 1678 target_prop->description, 1679 &error_abort); 1680 1681 out: 1682 g_free(prop_type); 1683 } 1684 1685 void object_property_set_description(Object *obj, const char *name, 1686 const char *description, Error **errp) 1687 { 1688 ObjectProperty *op; 1689 1690 op = object_property_find(obj, name, errp); 1691 if (!op) { 1692 return; 1693 } 1694 1695 g_free(op->description); 1696 op->description = g_strdup(description); 1697 } 1698 1699 static void object_instance_init(Object *obj) 1700 { 1701 object_property_add_str(obj, "type", qdev_get_type, NULL, NULL); 1702 } 1703 1704 static void register_types(void) 1705 { 1706 static TypeInfo interface_info = { 1707 .name = TYPE_INTERFACE, 1708 .class_size = sizeof(InterfaceClass), 1709 .abstract = true, 1710 }; 1711 1712 static TypeInfo object_info = { 1713 .name = TYPE_OBJECT, 1714 .instance_size = sizeof(Object), 1715 .instance_init = object_instance_init, 1716 .abstract = true, 1717 }; 1718 1719 type_interface = type_register_internal(&interface_info); 1720 type_register_internal(&object_info); 1721 } 1722 1723 type_init(register_types) 1724