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