1 #include "qemu/osdep.h" 2 #include "qapi/error.h" 3 #include "qom/object_interfaces.h" 4 #include "qemu/module.h" 5 #include "qapi-visit.h" 6 #include "qapi/opts-visitor.h" 7 8 void user_creatable_complete(Object *obj, Error **errp) 9 { 10 11 UserCreatableClass *ucc; 12 UserCreatable *uc = 13 (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE); 14 15 if (!uc) { 16 return; 17 } 18 19 ucc = USER_CREATABLE_GET_CLASS(uc); 20 if (ucc->complete) { 21 ucc->complete(uc, errp); 22 } 23 } 24 25 bool user_creatable_can_be_deleted(UserCreatable *uc, Error **errp) 26 { 27 28 UserCreatableClass *ucc = USER_CREATABLE_GET_CLASS(uc); 29 30 if (ucc->can_be_deleted) { 31 return ucc->can_be_deleted(uc, errp); 32 } else { 33 return true; 34 } 35 } 36 37 Object *user_creatable_add_type(const char *type, const char *id, 38 const QDict *qdict, 39 Visitor *v, Error **errp) 40 { 41 Object *obj; 42 ObjectClass *klass; 43 const QDictEntry *e; 44 Error *local_err = NULL; 45 46 klass = object_class_by_name(type); 47 if (!klass) { 48 error_setg(errp, "invalid object type: %s", type); 49 return NULL; 50 } 51 52 if (!object_class_dynamic_cast(klass, TYPE_USER_CREATABLE)) { 53 error_setg(errp, "object type '%s' isn't supported by object-add", 54 type); 55 return NULL; 56 } 57 58 if (object_class_is_abstract(klass)) { 59 error_setg(errp, "object type '%s' is abstract", type); 60 return NULL; 61 } 62 63 assert(qdict); 64 obj = object_new(type); 65 if (object_property_find(obj, "id", NULL)) { 66 object_property_set_str(obj, id, "id", &local_err); 67 if (local_err) { 68 goto out; 69 } 70 } 71 visit_start_struct(v, NULL, NULL, 0, &local_err); 72 if (local_err) { 73 goto out; 74 } 75 for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) { 76 object_property_set(obj, v, e->key, &local_err); 77 if (local_err) { 78 break; 79 } 80 } 81 if (!local_err) { 82 visit_check_struct(v, &local_err); 83 } 84 visit_end_struct(v, NULL); 85 if (local_err) { 86 goto out; 87 } 88 89 object_property_add_child(object_get_objects_root(), 90 id, obj, &local_err); 91 if (local_err) { 92 goto out; 93 } 94 95 user_creatable_complete(obj, &local_err); 96 if (local_err) { 97 object_property_del(object_get_objects_root(), 98 id, &error_abort); 99 goto out; 100 } 101 out: 102 if (local_err) { 103 error_propagate(errp, local_err); 104 object_unref(obj); 105 return NULL; 106 } 107 return obj; 108 } 109 110 111 Object *user_creatable_add_opts(QemuOpts *opts, Error **errp) 112 { 113 Visitor *v; 114 QDict *pdict; 115 Object *obj; 116 const char *id = qemu_opts_id(opts); 117 const char *type = qemu_opt_get(opts, "qom-type"); 118 119 if (!type) { 120 error_setg(errp, QERR_MISSING_PARAMETER, "qom-type"); 121 return NULL; 122 } 123 if (!id) { 124 error_setg(errp, QERR_MISSING_PARAMETER, "id"); 125 return NULL; 126 } 127 128 pdict = qemu_opts_to_qdict(opts, NULL); 129 qdict_del(pdict, "qom-type"); 130 qdict_del(pdict, "id"); 131 132 v = opts_visitor_new(opts); 133 obj = user_creatable_add_type(type, id, pdict, v, errp); 134 visit_free(v); 135 136 QDECREF(pdict); 137 return obj; 138 } 139 140 141 int user_creatable_add_opts_foreach(void *opaque, QemuOpts *opts, Error **errp) 142 { 143 bool (*type_predicate)(const char *) = opaque; 144 Object *obj = NULL; 145 Error *err = NULL; 146 const char *type; 147 148 type = qemu_opt_get(opts, "qom-type"); 149 if (type && type_predicate && 150 !type_predicate(type)) { 151 return 0; 152 } 153 154 obj = user_creatable_add_opts(opts, &err); 155 if (!obj) { 156 error_report_err(err); 157 return -1; 158 } 159 object_unref(obj); 160 return 0; 161 } 162 163 164 void user_creatable_del(const char *id, Error **errp) 165 { 166 Object *container; 167 Object *obj; 168 169 container = object_get_objects_root(); 170 obj = object_resolve_path_component(container, id); 171 if (!obj) { 172 error_setg(errp, "object '%s' not found", id); 173 return; 174 } 175 176 if (!user_creatable_can_be_deleted(USER_CREATABLE(obj), errp)) { 177 error_setg(errp, "object '%s' is in use, can not be deleted", id); 178 return; 179 } 180 object_unparent(obj); 181 } 182 183 static void register_types(void) 184 { 185 static const TypeInfo uc_interface_info = { 186 .name = TYPE_USER_CREATABLE, 187 .parent = TYPE_INTERFACE, 188 .class_size = sizeof(UserCreatableClass), 189 }; 190 191 type_register_static(&uc_interface_info); 192 } 193 194 type_init(register_types) 195