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