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