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(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(type->instance_size >= sizeof(Object)); 376 g_assert(type->abstract == false); 377 g_assert(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(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 static gint object_class_cmp(gconstpointer a, gconstpointer b) 895 { 896 return strcasecmp(object_class_get_name((ObjectClass *)a), 897 object_class_get_name((ObjectClass *)b)); 898 } 899 900 GSList *object_class_get_list_sorted(const char *implements_type, 901 bool include_abstract) 902 { 903 return g_slist_sort(object_class_get_list(implements_type, include_abstract), 904 object_class_cmp); 905 } 906 907 void object_ref(Object *obj) 908 { 909 if (!obj) { 910 return; 911 } 912 atomic_inc(&obj->ref); 913 } 914 915 void object_unref(Object *obj) 916 { 917 if (!obj) { 918 return; 919 } 920 g_assert(obj->ref > 0); 921 922 /* parent always holds a reference to its children */ 923 if (atomic_fetch_dec(&obj->ref) == 1) { 924 object_finalize(obj); 925 } 926 } 927 928 ObjectProperty * 929 object_property_add(Object *obj, const char *name, const char *type, 930 ObjectPropertyAccessor *get, 931 ObjectPropertyAccessor *set, 932 ObjectPropertyRelease *release, 933 void *opaque, Error **errp) 934 { 935 ObjectProperty *prop; 936 size_t name_len = strlen(name); 937 938 if (name_len >= 3 && !memcmp(name + name_len - 3, "[*]", 4)) { 939 int i; 940 ObjectProperty *ret; 941 char *name_no_array = g_strdup(name); 942 943 name_no_array[name_len - 3] = '\0'; 944 for (i = 0; ; ++i) { 945 char *full_name = g_strdup_printf("%s[%d]", name_no_array, i); 946 947 ret = object_property_add(obj, full_name, type, get, set, 948 release, opaque, NULL); 949 g_free(full_name); 950 if (ret) { 951 break; 952 } 953 } 954 g_free(name_no_array); 955 return ret; 956 } 957 958 if (object_property_find(obj, name, NULL) != NULL) { 959 error_setg(errp, "attempt to add duplicate property '%s'" 960 " to object (type '%s')", name, 961 object_get_typename(obj)); 962 return NULL; 963 } 964 965 prop = g_malloc0(sizeof(*prop)); 966 967 prop->name = g_strdup(name); 968 prop->type = g_strdup(type); 969 970 prop->get = get; 971 prop->set = set; 972 prop->release = release; 973 prop->opaque = opaque; 974 975 g_hash_table_insert(obj->properties, prop->name, prop); 976 return prop; 977 } 978 979 ObjectProperty * 980 object_class_property_add(ObjectClass *klass, 981 const char *name, 982 const char *type, 983 ObjectPropertyAccessor *get, 984 ObjectPropertyAccessor *set, 985 ObjectPropertyRelease *release, 986 void *opaque, 987 Error **errp) 988 { 989 ObjectProperty *prop; 990 991 if (object_class_property_find(klass, name, NULL) != NULL) { 992 error_setg(errp, "attempt to add duplicate property '%s'" 993 " to object (type '%s')", name, 994 object_class_get_name(klass)); 995 return NULL; 996 } 997 998 prop = g_malloc0(sizeof(*prop)); 999 1000 prop->name = g_strdup(name); 1001 prop->type = g_strdup(type); 1002 1003 prop->get = get; 1004 prop->set = set; 1005 prop->release = release; 1006 prop->opaque = opaque; 1007 1008 g_hash_table_insert(klass->properties, g_strdup(name), prop); 1009 1010 return prop; 1011 } 1012 1013 ObjectProperty *object_property_find(Object *obj, const char *name, 1014 Error **errp) 1015 { 1016 ObjectProperty *prop; 1017 ObjectClass *klass = object_get_class(obj); 1018 1019 prop = object_class_property_find(klass, name, NULL); 1020 if (prop) { 1021 return prop; 1022 } 1023 1024 prop = g_hash_table_lookup(obj->properties, name); 1025 if (prop) { 1026 return prop; 1027 } 1028 1029 error_setg(errp, "Property '.%s' not found", name); 1030 return NULL; 1031 } 1032 1033 void object_property_iter_init(ObjectPropertyIterator *iter, 1034 Object *obj) 1035 { 1036 g_hash_table_iter_init(&iter->iter, obj->properties); 1037 iter->nextclass = object_get_class(obj); 1038 } 1039 1040 ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter) 1041 { 1042 gpointer key, val; 1043 while (!g_hash_table_iter_next(&iter->iter, &key, &val)) { 1044 if (!iter->nextclass) { 1045 return NULL; 1046 } 1047 g_hash_table_iter_init(&iter->iter, iter->nextclass->properties); 1048 iter->nextclass = object_class_get_parent(iter->nextclass); 1049 } 1050 return val; 1051 } 1052 1053 void object_class_property_iter_init(ObjectPropertyIterator *iter, 1054 ObjectClass *klass) 1055 { 1056 g_hash_table_iter_init(&iter->iter, klass->properties); 1057 iter->nextclass = klass; 1058 } 1059 1060 ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name, 1061 Error **errp) 1062 { 1063 ObjectProperty *prop; 1064 ObjectClass *parent_klass; 1065 1066 parent_klass = object_class_get_parent(klass); 1067 if (parent_klass) { 1068 prop = object_class_property_find(parent_klass, name, NULL); 1069 if (prop) { 1070 return prop; 1071 } 1072 } 1073 1074 prop = g_hash_table_lookup(klass->properties, name); 1075 if (!prop) { 1076 error_setg(errp, "Property '.%s' not found", name); 1077 } 1078 return prop; 1079 } 1080 1081 void object_property_del(Object *obj, const char *name, Error **errp) 1082 { 1083 ObjectProperty *prop = g_hash_table_lookup(obj->properties, name); 1084 1085 if (!prop) { 1086 error_setg(errp, "Property '.%s' not found", name); 1087 return; 1088 } 1089 1090 if (prop->release) { 1091 prop->release(obj, name, prop->opaque); 1092 } 1093 g_hash_table_remove(obj->properties, name); 1094 } 1095 1096 void object_property_get(Object *obj, Visitor *v, const char *name, 1097 Error **errp) 1098 { 1099 ObjectProperty *prop = object_property_find(obj, name, errp); 1100 if (prop == NULL) { 1101 return; 1102 } 1103 1104 if (!prop->get) { 1105 error_setg(errp, QERR_PERMISSION_DENIED); 1106 } else { 1107 prop->get(obj, v, name, prop->opaque, errp); 1108 } 1109 } 1110 1111 void object_property_set(Object *obj, Visitor *v, const char *name, 1112 Error **errp) 1113 { 1114 ObjectProperty *prop = object_property_find(obj, name, errp); 1115 if (prop == NULL) { 1116 return; 1117 } 1118 1119 if (!prop->set) { 1120 error_setg(errp, QERR_PERMISSION_DENIED); 1121 } else { 1122 prop->set(obj, v, name, prop->opaque, errp); 1123 } 1124 } 1125 1126 void object_property_set_str(Object *obj, const char *value, 1127 const char *name, Error **errp) 1128 { 1129 QString *qstr = qstring_from_str(value); 1130 object_property_set_qobject(obj, QOBJECT(qstr), name, errp); 1131 1132 qobject_unref(qstr); 1133 } 1134 1135 char *object_property_get_str(Object *obj, const char *name, 1136 Error **errp) 1137 { 1138 QObject *ret = object_property_get_qobject(obj, name, errp); 1139 char *retval; 1140 1141 if (!ret) { 1142 return NULL; 1143 } 1144 1145 retval = g_strdup(qobject_get_try_str(ret)); 1146 if (!retval) { 1147 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "string"); 1148 } 1149 1150 qobject_unref(ret); 1151 return retval; 1152 } 1153 1154 void object_property_set_link(Object *obj, Object *value, 1155 const char *name, Error **errp) 1156 { 1157 if (value) { 1158 gchar *path = object_get_canonical_path(value); 1159 object_property_set_str(obj, path, name, errp); 1160 g_free(path); 1161 } else { 1162 object_property_set_str(obj, "", name, errp); 1163 } 1164 } 1165 1166 Object *object_property_get_link(Object *obj, const char *name, 1167 Error **errp) 1168 { 1169 char *str = object_property_get_str(obj, name, errp); 1170 Object *target = NULL; 1171 1172 if (str && *str) { 1173 target = object_resolve_path(str, NULL); 1174 if (!target) { 1175 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 1176 "Device '%s' not found", str); 1177 } 1178 } 1179 1180 g_free(str); 1181 return target; 1182 } 1183 1184 void object_property_set_bool(Object *obj, bool value, 1185 const char *name, Error **errp) 1186 { 1187 QBool *qbool = qbool_from_bool(value); 1188 object_property_set_qobject(obj, QOBJECT(qbool), name, errp); 1189 1190 qobject_unref(qbool); 1191 } 1192 1193 bool object_property_get_bool(Object *obj, const char *name, 1194 Error **errp) 1195 { 1196 QObject *ret = object_property_get_qobject(obj, name, errp); 1197 QBool *qbool; 1198 bool retval; 1199 1200 if (!ret) { 1201 return false; 1202 } 1203 qbool = qobject_to(QBool, ret); 1204 if (!qbool) { 1205 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean"); 1206 retval = false; 1207 } else { 1208 retval = qbool_get_bool(qbool); 1209 } 1210 1211 qobject_unref(ret); 1212 return retval; 1213 } 1214 1215 void object_property_set_int(Object *obj, int64_t value, 1216 const char *name, Error **errp) 1217 { 1218 QNum *qnum = qnum_from_int(value); 1219 object_property_set_qobject(obj, QOBJECT(qnum), name, errp); 1220 1221 qobject_unref(qnum); 1222 } 1223 1224 int64_t object_property_get_int(Object *obj, const char *name, 1225 Error **errp) 1226 { 1227 QObject *ret = object_property_get_qobject(obj, name, errp); 1228 QNum *qnum; 1229 int64_t retval; 1230 1231 if (!ret) { 1232 return -1; 1233 } 1234 1235 qnum = qobject_to(QNum, ret); 1236 if (!qnum || !qnum_get_try_int(qnum, &retval)) { 1237 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "int"); 1238 retval = -1; 1239 } 1240 1241 qobject_unref(ret); 1242 return retval; 1243 } 1244 1245 void object_property_set_uint(Object *obj, uint64_t value, 1246 const char *name, Error **errp) 1247 { 1248 QNum *qnum = qnum_from_uint(value); 1249 1250 object_property_set_qobject(obj, QOBJECT(qnum), name, errp); 1251 qobject_unref(qnum); 1252 } 1253 1254 uint64_t object_property_get_uint(Object *obj, const char *name, 1255 Error **errp) 1256 { 1257 QObject *ret = object_property_get_qobject(obj, name, errp); 1258 QNum *qnum; 1259 uint64_t retval; 1260 1261 if (!ret) { 1262 return 0; 1263 } 1264 qnum = qobject_to(QNum, ret); 1265 if (!qnum || !qnum_get_try_uint(qnum, &retval)) { 1266 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "uint"); 1267 retval = 0; 1268 } 1269 1270 qobject_unref(ret); 1271 return retval; 1272 } 1273 1274 typedef struct EnumProperty { 1275 const QEnumLookup *lookup; 1276 int (*get)(Object *, Error **); 1277 void (*set)(Object *, int, Error **); 1278 } EnumProperty; 1279 1280 int object_property_get_enum(Object *obj, const char *name, 1281 const char *typename, Error **errp) 1282 { 1283 Error *err = NULL; 1284 Visitor *v; 1285 char *str; 1286 int ret; 1287 ObjectProperty *prop = object_property_find(obj, name, errp); 1288 EnumProperty *enumprop; 1289 1290 if (prop == NULL) { 1291 return 0; 1292 } 1293 1294 if (!g_str_equal(prop->type, typename)) { 1295 error_setg(errp, "Property %s on %s is not '%s' enum type", 1296 name, object_class_get_name( 1297 object_get_class(obj)), typename); 1298 return 0; 1299 } 1300 1301 enumprop = prop->opaque; 1302 1303 v = string_output_visitor_new(false, &str); 1304 object_property_get(obj, v, name, &err); 1305 if (err) { 1306 error_propagate(errp, err); 1307 visit_free(v); 1308 return 0; 1309 } 1310 visit_complete(v, &str); 1311 visit_free(v); 1312 v = string_input_visitor_new(str); 1313 visit_type_enum(v, name, &ret, enumprop->lookup, errp); 1314 1315 g_free(str); 1316 visit_free(v); 1317 1318 return ret; 1319 } 1320 1321 void object_property_get_uint16List(Object *obj, const char *name, 1322 uint16List **list, Error **errp) 1323 { 1324 Error *err = NULL; 1325 Visitor *v; 1326 char *str; 1327 1328 v = string_output_visitor_new(false, &str); 1329 object_property_get(obj, v, name, &err); 1330 if (err) { 1331 error_propagate(errp, err); 1332 goto out; 1333 } 1334 visit_complete(v, &str); 1335 visit_free(v); 1336 v = string_input_visitor_new(str); 1337 visit_type_uint16List(v, NULL, list, errp); 1338 1339 g_free(str); 1340 out: 1341 visit_free(v); 1342 } 1343 1344 void object_property_parse(Object *obj, const char *string, 1345 const char *name, Error **errp) 1346 { 1347 Visitor *v = string_input_visitor_new(string); 1348 object_property_set(obj, v, name, errp); 1349 visit_free(v); 1350 } 1351 1352 char *object_property_print(Object *obj, const char *name, bool human, 1353 Error **errp) 1354 { 1355 Visitor *v; 1356 char *string = NULL; 1357 Error *local_err = NULL; 1358 1359 v = string_output_visitor_new(human, &string); 1360 object_property_get(obj, v, name, &local_err); 1361 if (local_err) { 1362 error_propagate(errp, local_err); 1363 goto out; 1364 } 1365 1366 visit_complete(v, &string); 1367 1368 out: 1369 visit_free(v); 1370 return string; 1371 } 1372 1373 const char *object_property_get_type(Object *obj, const char *name, Error **errp) 1374 { 1375 ObjectProperty *prop = object_property_find(obj, name, errp); 1376 if (prop == NULL) { 1377 return NULL; 1378 } 1379 1380 return prop->type; 1381 } 1382 1383 Object *object_get_root(void) 1384 { 1385 static Object *root; 1386 1387 if (!root) { 1388 root = object_new("container"); 1389 } 1390 1391 return root; 1392 } 1393 1394 Object *object_get_objects_root(void) 1395 { 1396 return container_get(object_get_root(), "/objects"); 1397 } 1398 1399 Object *object_get_internal_root(void) 1400 { 1401 static Object *internal_root; 1402 1403 if (!internal_root) { 1404 internal_root = object_new("container"); 1405 } 1406 1407 return internal_root; 1408 } 1409 1410 static void object_get_child_property(Object *obj, Visitor *v, 1411 const char *name, void *opaque, 1412 Error **errp) 1413 { 1414 Object *child = opaque; 1415 gchar *path; 1416 1417 path = object_get_canonical_path(child); 1418 visit_type_str(v, name, &path, errp); 1419 g_free(path); 1420 } 1421 1422 static Object *object_resolve_child_property(Object *parent, void *opaque, const gchar *part) 1423 { 1424 return opaque; 1425 } 1426 1427 static void object_finalize_child_property(Object *obj, const char *name, 1428 void *opaque) 1429 { 1430 Object *child = opaque; 1431 1432 if (child->class->unparent) { 1433 (child->class->unparent)(child); 1434 } 1435 child->parent = NULL; 1436 object_unref(child); 1437 } 1438 1439 void object_property_add_child(Object *obj, const char *name, 1440 Object *child, Error **errp) 1441 { 1442 Error *local_err = NULL; 1443 gchar *type; 1444 ObjectProperty *op; 1445 1446 if (child->parent != NULL) { 1447 error_setg(errp, "child object is already parented"); 1448 return; 1449 } 1450 1451 type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child))); 1452 1453 op = object_property_add(obj, name, type, object_get_child_property, NULL, 1454 object_finalize_child_property, child, &local_err); 1455 if (local_err) { 1456 error_propagate(errp, local_err); 1457 goto out; 1458 } 1459 1460 op->resolve = object_resolve_child_property; 1461 object_ref(child); 1462 child->parent = obj; 1463 1464 out: 1465 g_free(type); 1466 } 1467 1468 void object_property_allow_set_link(const Object *obj, const char *name, 1469 Object *val, Error **errp) 1470 { 1471 /* Allow the link to be set, always */ 1472 } 1473 1474 typedef struct { 1475 Object **child; 1476 void (*check)(const Object *, const char *, Object *, Error **); 1477 ObjectPropertyLinkFlags flags; 1478 } LinkProperty; 1479 1480 static void object_get_link_property(Object *obj, Visitor *v, 1481 const char *name, void *opaque, 1482 Error **errp) 1483 { 1484 LinkProperty *lprop = opaque; 1485 Object **child = lprop->child; 1486 gchar *path; 1487 1488 if (*child) { 1489 path = object_get_canonical_path(*child); 1490 visit_type_str(v, name, &path, errp); 1491 g_free(path); 1492 } else { 1493 path = (gchar *)""; 1494 visit_type_str(v, name, &path, errp); 1495 } 1496 } 1497 1498 /* 1499 * object_resolve_link: 1500 * 1501 * Lookup an object and ensure its type matches the link property type. This 1502 * is similar to object_resolve_path() except type verification against the 1503 * link property is performed. 1504 * 1505 * Returns: The matched object or NULL on path lookup failures. 1506 */ 1507 static Object *object_resolve_link(Object *obj, const char *name, 1508 const char *path, Error **errp) 1509 { 1510 const char *type; 1511 gchar *target_type; 1512 bool ambiguous = false; 1513 Object *target; 1514 1515 /* Go from link<FOO> to FOO. */ 1516 type = object_property_get_type(obj, name, NULL); 1517 target_type = g_strndup(&type[5], strlen(type) - 6); 1518 target = object_resolve_path_type(path, target_type, &ambiguous); 1519 1520 if (ambiguous) { 1521 error_setg(errp, "Path '%s' does not uniquely identify an object", 1522 path); 1523 } else if (!target) { 1524 target = object_resolve_path(path, &ambiguous); 1525 if (target || ambiguous) { 1526 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type); 1527 } else { 1528 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 1529 "Device '%s' not found", path); 1530 } 1531 target = NULL; 1532 } 1533 g_free(target_type); 1534 1535 return target; 1536 } 1537 1538 static void object_set_link_property(Object *obj, Visitor *v, 1539 const char *name, void *opaque, 1540 Error **errp) 1541 { 1542 Error *local_err = NULL; 1543 LinkProperty *prop = opaque; 1544 Object **child = prop->child; 1545 Object *old_target = *child; 1546 Object *new_target = NULL; 1547 char *path = NULL; 1548 1549 visit_type_str(v, name, &path, &local_err); 1550 1551 if (!local_err && strcmp(path, "") != 0) { 1552 new_target = object_resolve_link(obj, name, path, &local_err); 1553 } 1554 1555 g_free(path); 1556 if (local_err) { 1557 error_propagate(errp, local_err); 1558 return; 1559 } 1560 1561 prop->check(obj, name, new_target, &local_err); 1562 if (local_err) { 1563 error_propagate(errp, local_err); 1564 return; 1565 } 1566 1567 *child = new_target; 1568 if (prop->flags == OBJ_PROP_LINK_STRONG) { 1569 object_ref(new_target); 1570 object_unref(old_target); 1571 } 1572 } 1573 1574 static Object *object_resolve_link_property(Object *parent, void *opaque, const gchar *part) 1575 { 1576 LinkProperty *lprop = opaque; 1577 1578 return *lprop->child; 1579 } 1580 1581 static void object_release_link_property(Object *obj, const char *name, 1582 void *opaque) 1583 { 1584 LinkProperty *prop = opaque; 1585 1586 if ((prop->flags & OBJ_PROP_LINK_STRONG) && *prop->child) { 1587 object_unref(*prop->child); 1588 } 1589 g_free(prop); 1590 } 1591 1592 void object_property_add_link(Object *obj, const char *name, 1593 const char *type, Object **child, 1594 void (*check)(const Object *, const char *, 1595 Object *, Error **), 1596 ObjectPropertyLinkFlags flags, 1597 Error **errp) 1598 { 1599 Error *local_err = NULL; 1600 LinkProperty *prop = g_malloc(sizeof(*prop)); 1601 gchar *full_type; 1602 ObjectProperty *op; 1603 1604 prop->child = child; 1605 prop->check = check; 1606 prop->flags = flags; 1607 1608 full_type = g_strdup_printf("link<%s>", type); 1609 1610 op = object_property_add(obj, name, full_type, 1611 object_get_link_property, 1612 check ? object_set_link_property : NULL, 1613 object_release_link_property, 1614 prop, 1615 &local_err); 1616 if (local_err) { 1617 error_propagate(errp, local_err); 1618 g_free(prop); 1619 goto out; 1620 } 1621 1622 op->resolve = object_resolve_link_property; 1623 1624 out: 1625 g_free(full_type); 1626 } 1627 1628 void object_property_add_const_link(Object *obj, const char *name, 1629 Object *target, Error **errp) 1630 { 1631 char *link_type; 1632 ObjectProperty *op; 1633 1634 link_type = g_strdup_printf("link<%s>", object_get_typename(target)); 1635 op = object_property_add(obj, name, link_type, 1636 object_get_child_property, NULL, 1637 NULL, target, errp); 1638 if (op != NULL) { 1639 op->resolve = object_resolve_child_property; 1640 } 1641 g_free(link_type); 1642 } 1643 1644 gchar *object_get_canonical_path_component(Object *obj) 1645 { 1646 ObjectProperty *prop = NULL; 1647 GHashTableIter iter; 1648 1649 if (obj->parent == NULL) { 1650 return NULL; 1651 } 1652 1653 g_hash_table_iter_init(&iter, obj->parent->properties); 1654 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) { 1655 if (!object_property_is_child(prop)) { 1656 continue; 1657 } 1658 1659 if (prop->opaque == obj) { 1660 return g_strdup(prop->name); 1661 } 1662 } 1663 1664 /* obj had a parent but was not a child, should never happen */ 1665 g_assert_not_reached(); 1666 return NULL; 1667 } 1668 1669 gchar *object_get_canonical_path(Object *obj) 1670 { 1671 Object *root = object_get_root(); 1672 char *newpath, *path = NULL; 1673 1674 if (obj == root) { 1675 return g_strdup("/"); 1676 } 1677 1678 do { 1679 char *component = object_get_canonical_path_component(obj); 1680 1681 if (!component) { 1682 /* A canonical path must be complete, so discard what was 1683 * collected so far. 1684 */ 1685 g_free(path); 1686 return NULL; 1687 } 1688 1689 newpath = g_strdup_printf("/%s%s", component, path ? path : ""); 1690 g_free(path); 1691 g_free(component); 1692 path = newpath; 1693 obj = obj->parent; 1694 } while (obj != root); 1695 1696 return path; 1697 } 1698 1699 Object *object_resolve_path_component(Object *parent, const gchar *part) 1700 { 1701 ObjectProperty *prop = object_property_find(parent, part, NULL); 1702 if (prop == NULL) { 1703 return NULL; 1704 } 1705 1706 if (prop->resolve) { 1707 return prop->resolve(parent, prop->opaque, part); 1708 } else { 1709 return NULL; 1710 } 1711 } 1712 1713 static Object *object_resolve_abs_path(Object *parent, 1714 gchar **parts, 1715 const char *typename, 1716 int index) 1717 { 1718 Object *child; 1719 1720 if (parts[index] == NULL) { 1721 return object_dynamic_cast(parent, typename); 1722 } 1723 1724 if (strcmp(parts[index], "") == 0) { 1725 return object_resolve_abs_path(parent, parts, typename, index + 1); 1726 } 1727 1728 child = object_resolve_path_component(parent, parts[index]); 1729 if (!child) { 1730 return NULL; 1731 } 1732 1733 return object_resolve_abs_path(child, parts, typename, index + 1); 1734 } 1735 1736 static Object *object_resolve_partial_path(Object *parent, 1737 gchar **parts, 1738 const char *typename, 1739 bool *ambiguous) 1740 { 1741 Object *obj; 1742 GHashTableIter iter; 1743 ObjectProperty *prop; 1744 1745 obj = object_resolve_abs_path(parent, parts, typename, 0); 1746 1747 g_hash_table_iter_init(&iter, parent->properties); 1748 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) { 1749 Object *found; 1750 1751 if (!object_property_is_child(prop)) { 1752 continue; 1753 } 1754 1755 found = object_resolve_partial_path(prop->opaque, parts, 1756 typename, ambiguous); 1757 if (found) { 1758 if (obj) { 1759 *ambiguous = true; 1760 return NULL; 1761 } 1762 obj = found; 1763 } 1764 1765 if (*ambiguous) { 1766 return NULL; 1767 } 1768 } 1769 1770 return obj; 1771 } 1772 1773 Object *object_resolve_path_type(const char *path, const char *typename, 1774 bool *ambiguousp) 1775 { 1776 Object *obj; 1777 gchar **parts; 1778 1779 parts = g_strsplit(path, "/", 0); 1780 assert(parts); 1781 1782 if (parts[0] == NULL || strcmp(parts[0], "") != 0) { 1783 bool ambiguous = false; 1784 obj = object_resolve_partial_path(object_get_root(), parts, 1785 typename, &ambiguous); 1786 if (ambiguousp) { 1787 *ambiguousp = ambiguous; 1788 } 1789 } else { 1790 obj = object_resolve_abs_path(object_get_root(), parts, typename, 1); 1791 } 1792 1793 g_strfreev(parts); 1794 1795 return obj; 1796 } 1797 1798 Object *object_resolve_path(const char *path, bool *ambiguous) 1799 { 1800 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous); 1801 } 1802 1803 typedef struct StringProperty 1804 { 1805 char *(*get)(Object *, Error **); 1806 void (*set)(Object *, const char *, Error **); 1807 } StringProperty; 1808 1809 static void property_get_str(Object *obj, Visitor *v, const char *name, 1810 void *opaque, Error **errp) 1811 { 1812 StringProperty *prop = opaque; 1813 char *value; 1814 Error *err = NULL; 1815 1816 value = prop->get(obj, &err); 1817 if (err) { 1818 error_propagate(errp, err); 1819 return; 1820 } 1821 1822 visit_type_str(v, name, &value, errp); 1823 g_free(value); 1824 } 1825 1826 static void property_set_str(Object *obj, Visitor *v, const char *name, 1827 void *opaque, Error **errp) 1828 { 1829 StringProperty *prop = opaque; 1830 char *value; 1831 Error *local_err = NULL; 1832 1833 visit_type_str(v, name, &value, &local_err); 1834 if (local_err) { 1835 error_propagate(errp, local_err); 1836 return; 1837 } 1838 1839 prop->set(obj, value, errp); 1840 g_free(value); 1841 } 1842 1843 static void property_release_str(Object *obj, const char *name, 1844 void *opaque) 1845 { 1846 StringProperty *prop = opaque; 1847 g_free(prop); 1848 } 1849 1850 void object_property_add_str(Object *obj, const char *name, 1851 char *(*get)(Object *, Error **), 1852 void (*set)(Object *, const char *, Error **), 1853 Error **errp) 1854 { 1855 Error *local_err = NULL; 1856 StringProperty *prop = g_malloc0(sizeof(*prop)); 1857 1858 prop->get = get; 1859 prop->set = set; 1860 1861 object_property_add(obj, name, "string", 1862 get ? property_get_str : NULL, 1863 set ? property_set_str : NULL, 1864 property_release_str, 1865 prop, &local_err); 1866 if (local_err) { 1867 error_propagate(errp, local_err); 1868 g_free(prop); 1869 } 1870 } 1871 1872 void object_class_property_add_str(ObjectClass *klass, const char *name, 1873 char *(*get)(Object *, Error **), 1874 void (*set)(Object *, const char *, 1875 Error **), 1876 Error **errp) 1877 { 1878 Error *local_err = NULL; 1879 StringProperty *prop = g_malloc0(sizeof(*prop)); 1880 1881 prop->get = get; 1882 prop->set = set; 1883 1884 object_class_property_add(klass, name, "string", 1885 get ? property_get_str : NULL, 1886 set ? property_set_str : NULL, 1887 property_release_str, 1888 prop, &local_err); 1889 if (local_err) { 1890 error_propagate(errp, local_err); 1891 g_free(prop); 1892 } 1893 } 1894 1895 typedef struct BoolProperty 1896 { 1897 bool (*get)(Object *, Error **); 1898 void (*set)(Object *, bool, Error **); 1899 } BoolProperty; 1900 1901 static void property_get_bool(Object *obj, Visitor *v, const char *name, 1902 void *opaque, Error **errp) 1903 { 1904 BoolProperty *prop = opaque; 1905 bool value; 1906 Error *err = NULL; 1907 1908 value = prop->get(obj, &err); 1909 if (err) { 1910 error_propagate(errp, err); 1911 return; 1912 } 1913 1914 visit_type_bool(v, name, &value, errp); 1915 } 1916 1917 static void property_set_bool(Object *obj, Visitor *v, const char *name, 1918 void *opaque, Error **errp) 1919 { 1920 BoolProperty *prop = opaque; 1921 bool value; 1922 Error *local_err = NULL; 1923 1924 visit_type_bool(v, name, &value, &local_err); 1925 if (local_err) { 1926 error_propagate(errp, local_err); 1927 return; 1928 } 1929 1930 prop->set(obj, value, errp); 1931 } 1932 1933 static void property_release_bool(Object *obj, const char *name, 1934 void *opaque) 1935 { 1936 BoolProperty *prop = opaque; 1937 g_free(prop); 1938 } 1939 1940 void object_property_add_bool(Object *obj, const char *name, 1941 bool (*get)(Object *, Error **), 1942 void (*set)(Object *, bool, Error **), 1943 Error **errp) 1944 { 1945 Error *local_err = NULL; 1946 BoolProperty *prop = g_malloc0(sizeof(*prop)); 1947 1948 prop->get = get; 1949 prop->set = set; 1950 1951 object_property_add(obj, name, "bool", 1952 get ? property_get_bool : NULL, 1953 set ? property_set_bool : NULL, 1954 property_release_bool, 1955 prop, &local_err); 1956 if (local_err) { 1957 error_propagate(errp, local_err); 1958 g_free(prop); 1959 } 1960 } 1961 1962 void object_class_property_add_bool(ObjectClass *klass, const char *name, 1963 bool (*get)(Object *, Error **), 1964 void (*set)(Object *, bool, Error **), 1965 Error **errp) 1966 { 1967 Error *local_err = NULL; 1968 BoolProperty *prop = g_malloc0(sizeof(*prop)); 1969 1970 prop->get = get; 1971 prop->set = set; 1972 1973 object_class_property_add(klass, name, "bool", 1974 get ? property_get_bool : NULL, 1975 set ? property_set_bool : NULL, 1976 property_release_bool, 1977 prop, &local_err); 1978 if (local_err) { 1979 error_propagate(errp, local_err); 1980 g_free(prop); 1981 } 1982 } 1983 1984 static void property_get_enum(Object *obj, Visitor *v, const char *name, 1985 void *opaque, Error **errp) 1986 { 1987 EnumProperty *prop = opaque; 1988 int value; 1989 Error *err = NULL; 1990 1991 value = prop->get(obj, &err); 1992 if (err) { 1993 error_propagate(errp, err); 1994 return; 1995 } 1996 1997 visit_type_enum(v, name, &value, prop->lookup, errp); 1998 } 1999 2000 static void property_set_enum(Object *obj, Visitor *v, const char *name, 2001 void *opaque, Error **errp) 2002 { 2003 EnumProperty *prop = opaque; 2004 int value; 2005 Error *err = NULL; 2006 2007 visit_type_enum(v, name, &value, prop->lookup, &err); 2008 if (err) { 2009 error_propagate(errp, err); 2010 return; 2011 } 2012 prop->set(obj, value, errp); 2013 } 2014 2015 static void property_release_enum(Object *obj, const char *name, 2016 void *opaque) 2017 { 2018 EnumProperty *prop = opaque; 2019 g_free(prop); 2020 } 2021 2022 void object_property_add_enum(Object *obj, const char *name, 2023 const char *typename, 2024 const QEnumLookup *lookup, 2025 int (*get)(Object *, Error **), 2026 void (*set)(Object *, int, Error **), 2027 Error **errp) 2028 { 2029 Error *local_err = NULL; 2030 EnumProperty *prop = g_malloc(sizeof(*prop)); 2031 2032 prop->lookup = lookup; 2033 prop->get = get; 2034 prop->set = set; 2035 2036 object_property_add(obj, name, typename, 2037 get ? property_get_enum : NULL, 2038 set ? property_set_enum : NULL, 2039 property_release_enum, 2040 prop, &local_err); 2041 if (local_err) { 2042 error_propagate(errp, local_err); 2043 g_free(prop); 2044 } 2045 } 2046 2047 void object_class_property_add_enum(ObjectClass *klass, const char *name, 2048 const char *typename, 2049 const QEnumLookup *lookup, 2050 int (*get)(Object *, Error **), 2051 void (*set)(Object *, int, Error **), 2052 Error **errp) 2053 { 2054 Error *local_err = NULL; 2055 EnumProperty *prop = g_malloc(sizeof(*prop)); 2056 2057 prop->lookup = lookup; 2058 prop->get = get; 2059 prop->set = set; 2060 2061 object_class_property_add(klass, name, typename, 2062 get ? property_get_enum : NULL, 2063 set ? property_set_enum : NULL, 2064 property_release_enum, 2065 prop, &local_err); 2066 if (local_err) { 2067 error_propagate(errp, local_err); 2068 g_free(prop); 2069 } 2070 } 2071 2072 typedef struct TMProperty { 2073 void (*get)(Object *, struct tm *, Error **); 2074 } TMProperty; 2075 2076 static void property_get_tm(Object *obj, Visitor *v, const char *name, 2077 void *opaque, Error **errp) 2078 { 2079 TMProperty *prop = opaque; 2080 Error *err = NULL; 2081 struct tm value; 2082 2083 prop->get(obj, &value, &err); 2084 if (err) { 2085 goto out; 2086 } 2087 2088 visit_start_struct(v, name, NULL, 0, &err); 2089 if (err) { 2090 goto out; 2091 } 2092 visit_type_int32(v, "tm_year", &value.tm_year, &err); 2093 if (err) { 2094 goto out_end; 2095 } 2096 visit_type_int32(v, "tm_mon", &value.tm_mon, &err); 2097 if (err) { 2098 goto out_end; 2099 } 2100 visit_type_int32(v, "tm_mday", &value.tm_mday, &err); 2101 if (err) { 2102 goto out_end; 2103 } 2104 visit_type_int32(v, "tm_hour", &value.tm_hour, &err); 2105 if (err) { 2106 goto out_end; 2107 } 2108 visit_type_int32(v, "tm_min", &value.tm_min, &err); 2109 if (err) { 2110 goto out_end; 2111 } 2112 visit_type_int32(v, "tm_sec", &value.tm_sec, &err); 2113 if (err) { 2114 goto out_end; 2115 } 2116 visit_check_struct(v, &err); 2117 out_end: 2118 visit_end_struct(v, NULL); 2119 out: 2120 error_propagate(errp, err); 2121 2122 } 2123 2124 static void property_release_tm(Object *obj, const char *name, 2125 void *opaque) 2126 { 2127 TMProperty *prop = opaque; 2128 g_free(prop); 2129 } 2130 2131 void object_property_add_tm(Object *obj, const char *name, 2132 void (*get)(Object *, struct tm *, Error **), 2133 Error **errp) 2134 { 2135 Error *local_err = NULL; 2136 TMProperty *prop = g_malloc0(sizeof(*prop)); 2137 2138 prop->get = get; 2139 2140 object_property_add(obj, name, "struct tm", 2141 get ? property_get_tm : NULL, NULL, 2142 property_release_tm, 2143 prop, &local_err); 2144 if (local_err) { 2145 error_propagate(errp, local_err); 2146 g_free(prop); 2147 } 2148 } 2149 2150 void object_class_property_add_tm(ObjectClass *klass, const char *name, 2151 void (*get)(Object *, struct tm *, Error **), 2152 Error **errp) 2153 { 2154 Error *local_err = NULL; 2155 TMProperty *prop = g_malloc0(sizeof(*prop)); 2156 2157 prop->get = get; 2158 2159 object_class_property_add(klass, name, "struct tm", 2160 get ? property_get_tm : NULL, NULL, 2161 property_release_tm, 2162 prop, &local_err); 2163 if (local_err) { 2164 error_propagate(errp, local_err); 2165 g_free(prop); 2166 } 2167 } 2168 2169 static char *qdev_get_type(Object *obj, Error **errp) 2170 { 2171 return g_strdup(object_get_typename(obj)); 2172 } 2173 2174 static void property_get_uint8_ptr(Object *obj, Visitor *v, const char *name, 2175 void *opaque, Error **errp) 2176 { 2177 uint8_t value = *(uint8_t *)opaque; 2178 visit_type_uint8(v, name, &value, errp); 2179 } 2180 2181 static void property_get_uint16_ptr(Object *obj, Visitor *v, const char *name, 2182 void *opaque, Error **errp) 2183 { 2184 uint16_t value = *(uint16_t *)opaque; 2185 visit_type_uint16(v, name, &value, errp); 2186 } 2187 2188 static void property_get_uint32_ptr(Object *obj, Visitor *v, const char *name, 2189 void *opaque, Error **errp) 2190 { 2191 uint32_t value = *(uint32_t *)opaque; 2192 visit_type_uint32(v, name, &value, errp); 2193 } 2194 2195 static void property_get_uint64_ptr(Object *obj, Visitor *v, const char *name, 2196 void *opaque, Error **errp) 2197 { 2198 uint64_t value = *(uint64_t *)opaque; 2199 visit_type_uint64(v, name, &value, errp); 2200 } 2201 2202 void object_property_add_uint8_ptr(Object *obj, const char *name, 2203 const uint8_t *v, Error **errp) 2204 { 2205 object_property_add(obj, name, "uint8", property_get_uint8_ptr, 2206 NULL, NULL, (void *)v, errp); 2207 } 2208 2209 void object_class_property_add_uint8_ptr(ObjectClass *klass, const char *name, 2210 const uint8_t *v, Error **errp) 2211 { 2212 object_class_property_add(klass, name, "uint8", property_get_uint8_ptr, 2213 NULL, NULL, (void *)v, errp); 2214 } 2215 2216 void object_property_add_uint16_ptr(Object *obj, const char *name, 2217 const uint16_t *v, Error **errp) 2218 { 2219 object_property_add(obj, name, "uint16", property_get_uint16_ptr, 2220 NULL, NULL, (void *)v, errp); 2221 } 2222 2223 void object_class_property_add_uint16_ptr(ObjectClass *klass, const char *name, 2224 const uint16_t *v, Error **errp) 2225 { 2226 object_class_property_add(klass, name, "uint16", property_get_uint16_ptr, 2227 NULL, NULL, (void *)v, errp); 2228 } 2229 2230 void object_property_add_uint32_ptr(Object *obj, const char *name, 2231 const uint32_t *v, Error **errp) 2232 { 2233 object_property_add(obj, name, "uint32", property_get_uint32_ptr, 2234 NULL, NULL, (void *)v, errp); 2235 } 2236 2237 void object_class_property_add_uint32_ptr(ObjectClass *klass, const char *name, 2238 const uint32_t *v, Error **errp) 2239 { 2240 object_class_property_add(klass, name, "uint32", property_get_uint32_ptr, 2241 NULL, NULL, (void *)v, errp); 2242 } 2243 2244 void object_property_add_uint64_ptr(Object *obj, const char *name, 2245 const uint64_t *v, Error **errp) 2246 { 2247 object_property_add(obj, name, "uint64", property_get_uint64_ptr, 2248 NULL, NULL, (void *)v, errp); 2249 } 2250 2251 void object_class_property_add_uint64_ptr(ObjectClass *klass, const char *name, 2252 const uint64_t *v, Error **errp) 2253 { 2254 object_class_property_add(klass, name, "uint64", property_get_uint64_ptr, 2255 NULL, NULL, (void *)v, errp); 2256 } 2257 2258 typedef struct { 2259 Object *target_obj; 2260 char *target_name; 2261 } AliasProperty; 2262 2263 static void property_get_alias(Object *obj, Visitor *v, const char *name, 2264 void *opaque, Error **errp) 2265 { 2266 AliasProperty *prop = opaque; 2267 2268 object_property_get(prop->target_obj, v, prop->target_name, errp); 2269 } 2270 2271 static void property_set_alias(Object *obj, Visitor *v, const char *name, 2272 void *opaque, Error **errp) 2273 { 2274 AliasProperty *prop = opaque; 2275 2276 object_property_set(prop->target_obj, v, prop->target_name, errp); 2277 } 2278 2279 static Object *property_resolve_alias(Object *obj, void *opaque, 2280 const gchar *part) 2281 { 2282 AliasProperty *prop = opaque; 2283 2284 return object_resolve_path_component(prop->target_obj, prop->target_name); 2285 } 2286 2287 static void property_release_alias(Object *obj, const char *name, void *opaque) 2288 { 2289 AliasProperty *prop = opaque; 2290 2291 g_free(prop->target_name); 2292 g_free(prop); 2293 } 2294 2295 void object_property_add_alias(Object *obj, const char *name, 2296 Object *target_obj, const char *target_name, 2297 Error **errp) 2298 { 2299 AliasProperty *prop; 2300 ObjectProperty *op; 2301 ObjectProperty *target_prop; 2302 gchar *prop_type; 2303 Error *local_err = NULL; 2304 2305 target_prop = object_property_find(target_obj, target_name, errp); 2306 if (!target_prop) { 2307 return; 2308 } 2309 2310 if (object_property_is_child(target_prop)) { 2311 prop_type = g_strdup_printf("link%s", 2312 target_prop->type + strlen("child")); 2313 } else { 2314 prop_type = g_strdup(target_prop->type); 2315 } 2316 2317 prop = g_malloc(sizeof(*prop)); 2318 prop->target_obj = target_obj; 2319 prop->target_name = g_strdup(target_name); 2320 2321 op = object_property_add(obj, name, prop_type, 2322 property_get_alias, 2323 property_set_alias, 2324 property_release_alias, 2325 prop, &local_err); 2326 if (local_err) { 2327 error_propagate(errp, local_err); 2328 g_free(prop); 2329 goto out; 2330 } 2331 op->resolve = property_resolve_alias; 2332 2333 object_property_set_description(obj, op->name, 2334 target_prop->description, 2335 &error_abort); 2336 2337 out: 2338 g_free(prop_type); 2339 } 2340 2341 void object_property_set_description(Object *obj, const char *name, 2342 const char *description, Error **errp) 2343 { 2344 ObjectProperty *op; 2345 2346 op = object_property_find(obj, name, errp); 2347 if (!op) { 2348 return; 2349 } 2350 2351 g_free(op->description); 2352 op->description = g_strdup(description); 2353 } 2354 2355 void object_class_property_set_description(ObjectClass *klass, 2356 const char *name, 2357 const char *description, 2358 Error **errp) 2359 { 2360 ObjectProperty *op; 2361 2362 op = g_hash_table_lookup(klass->properties, name); 2363 if (!op) { 2364 error_setg(errp, "Property '.%s' not found", name); 2365 return; 2366 } 2367 2368 g_free(op->description); 2369 op->description = g_strdup(description); 2370 } 2371 2372 static void object_instance_init(Object *obj) 2373 { 2374 object_property_add_str(obj, "type", qdev_get_type, NULL, NULL); 2375 } 2376 2377 static void register_types(void) 2378 { 2379 static TypeInfo interface_info = { 2380 .name = TYPE_INTERFACE, 2381 .class_size = sizeof(InterfaceClass), 2382 .abstract = true, 2383 }; 2384 2385 static TypeInfo object_info = { 2386 .name = TYPE_OBJECT, 2387 .instance_size = sizeof(Object), 2388 .instance_init = object_instance_init, 2389 .abstract = true, 2390 }; 2391 2392 type_interface = type_register_internal(&interface_info); 2393 type_register_internal(&object_info); 2394 } 2395 2396 type_init(register_types) 2397