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