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