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