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