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