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