hw/virtio: Rename virtio_device_find() -> qmp_find_virtio_device()
[qemu.git] / util / qemu-config.c
blobd63f27438dc4c5b351945295cd3cba0710ce357e
1 #include "qemu/osdep.h"
2 #include "block/qdict.h" /* for qdict_extract_subqdict() */
3 #include "qapi/error.h"
4 #include "qapi/qapi-commands-misc.h"
5 #include "qapi/qmp/qerror.h"
6 #include "qapi/qmp/qdict.h"
7 #include "qapi/qmp/qlist.h"
8 #include "qemu/error-report.h"
9 #include "qemu/option.h"
10 #include "qemu/config-file.h"
11 #include "hw/boards.h"
13 static QemuOptsList *vm_config_groups[48];
14 static QemuOptsList *drive_config_groups[5];
16 static QemuOptsList *find_list(QemuOptsList **lists, const char *group,
17 Error **errp)
19 int i;
21 qemu_load_module_for_opts(group);
22 for (i = 0; lists[i] != NULL; i++) {
23 if (strcmp(lists[i]->name, group) == 0)
24 break;
26 if (lists[i] == NULL) {
27 error_setg(errp, "There is no option group '%s'", group);
29 return lists[i];
32 QemuOptsList *qemu_find_opts(const char *group)
34 QemuOptsList *ret;
35 Error *local_err = NULL;
37 ret = find_list(vm_config_groups, group, &local_err);
38 if (local_err) {
39 error_report_err(local_err);
42 return ret;
45 QemuOpts *qemu_find_opts_singleton(const char *group)
47 QemuOptsList *list;
48 QemuOpts *opts;
50 list = qemu_find_opts(group);
51 assert(list);
52 opts = qemu_opts_find(list, NULL);
53 if (!opts) {
54 opts = qemu_opts_create(list, NULL, 0, &error_abort);
56 return opts;
59 static CommandLineParameterInfoList *query_option_descs(const QemuOptDesc *desc)
61 CommandLineParameterInfoList *param_list = NULL;
62 CommandLineParameterInfo *info;
63 int i;
65 for (i = 0; desc[i].name != NULL; i++) {
66 info = g_malloc0(sizeof(*info));
67 info->name = g_strdup(desc[i].name);
69 switch (desc[i].type) {
70 case QEMU_OPT_STRING:
71 info->type = COMMAND_LINE_PARAMETER_TYPE_STRING;
72 break;
73 case QEMU_OPT_BOOL:
74 info->type = COMMAND_LINE_PARAMETER_TYPE_BOOLEAN;
75 break;
76 case QEMU_OPT_NUMBER:
77 info->type = COMMAND_LINE_PARAMETER_TYPE_NUMBER;
78 break;
79 case QEMU_OPT_SIZE:
80 info->type = COMMAND_LINE_PARAMETER_TYPE_SIZE;
81 break;
84 info->help = g_strdup(desc[i].help);
85 info->q_default = g_strdup(desc[i].def_value_str);
87 QAPI_LIST_PREPEND(param_list, info);
90 return param_list;
93 /* remove repeated entry from the info list */
94 static void cleanup_infolist(CommandLineParameterInfoList *head)
96 CommandLineParameterInfoList *pre_entry, *cur, *del_entry;
98 cur = head;
99 while (cur->next) {
100 pre_entry = head;
101 while (pre_entry != cur->next) {
102 if (!strcmp(pre_entry->value->name, cur->next->value->name)) {
103 del_entry = cur->next;
104 cur->next = cur->next->next;
105 del_entry->next = NULL;
106 qapi_free_CommandLineParameterInfoList(del_entry);
107 break;
109 pre_entry = pre_entry->next;
111 cur = cur->next;
115 /* merge the description items of two parameter infolists */
116 static void connect_infolist(CommandLineParameterInfoList *head,
117 CommandLineParameterInfoList *new)
119 CommandLineParameterInfoList *cur;
121 cur = head;
122 while (cur->next) {
123 cur = cur->next;
125 cur->next = new;
128 /* access all the local QemuOptsLists for drive option */
129 static CommandLineParameterInfoList *get_drive_infolist(void)
131 CommandLineParameterInfoList *head = NULL, *cur;
132 int i;
134 for (i = 0; drive_config_groups[i] != NULL; i++) {
135 if (!head) {
136 head = query_option_descs(drive_config_groups[i]->desc);
137 } else {
138 cur = query_option_descs(drive_config_groups[i]->desc);
139 connect_infolist(head, cur);
142 cleanup_infolist(head);
144 return head;
147 static CommandLineParameterInfo *objprop_to_cmdline_prop(ObjectProperty *prop)
149 CommandLineParameterInfo *info;
151 info = g_malloc0(sizeof(*info));
152 info->name = g_strdup(prop->name);
154 if (g_str_equal(prop->type, "bool") || g_str_equal(prop->type, "OnOffAuto")) {
155 info->type = COMMAND_LINE_PARAMETER_TYPE_BOOLEAN;
156 } else if (g_str_equal(prop->type, "int")) {
157 info->type = COMMAND_LINE_PARAMETER_TYPE_NUMBER;
158 } else if (g_str_equal(prop->type, "size")) {
159 info->type = COMMAND_LINE_PARAMETER_TYPE_SIZE;
160 } else {
161 info->type = COMMAND_LINE_PARAMETER_TYPE_STRING;
164 if (prop->description) {
165 info->help = g_strdup(prop->description);
168 return info;
171 static CommandLineParameterInfoList *query_all_machine_properties(void)
173 CommandLineParameterInfoList *params = NULL, *clpiter;
174 CommandLineParameterInfo *info;
175 GSList *machines, *curr_mach;
176 ObjectPropertyIterator op_iter;
177 ObjectProperty *prop;
178 bool is_new;
180 machines = object_class_get_list(TYPE_MACHINE, false);
181 assert(machines);
183 /* Loop over all machine classes */
184 for (curr_mach = machines; curr_mach; curr_mach = curr_mach->next) {
185 object_class_property_iter_init(&op_iter, curr_mach->data);
186 /* ... and over the properties of each machine: */
187 while ((prop = object_property_iter_next(&op_iter))) {
188 if (!prop->set) {
189 continue;
192 * Check whether the property has already been put into the list
193 * (via another machine class)
195 is_new = true;
196 for (clpiter = params; clpiter != NULL; clpiter = clpiter->next) {
197 if (g_str_equal(clpiter->value->name, prop->name)) {
198 is_new = false;
199 break;
202 /* If it hasn't been added before, add it now to the list */
203 if (is_new) {
204 info = objprop_to_cmdline_prop(prop);
205 QAPI_LIST_PREPEND(params, info);
210 g_slist_free(machines);
212 /* Add entry for the "type" parameter */
213 info = g_malloc0(sizeof(*info));
214 info->name = g_strdup("type");
215 info->type = COMMAND_LINE_PARAMETER_TYPE_STRING;
216 info->help = g_strdup("machine type");
217 QAPI_LIST_PREPEND(params, info);
219 return params;
222 CommandLineOptionInfoList *qmp_query_command_line_options(const char *option,
223 Error **errp)
225 CommandLineOptionInfoList *conf_list = NULL;
226 CommandLineOptionInfo *info;
227 int i;
229 for (i = 0; vm_config_groups[i] != NULL; i++) {
230 if (!option || !strcmp(option, vm_config_groups[i]->name)) {
231 info = g_malloc0(sizeof(*info));
232 info->option = g_strdup(vm_config_groups[i]->name);
233 if (!strcmp("drive", vm_config_groups[i]->name)) {
234 info->parameters = get_drive_infolist();
235 } else {
236 info->parameters =
237 query_option_descs(vm_config_groups[i]->desc);
239 QAPI_LIST_PREPEND(conf_list, info);
243 if (!option || !strcmp(option, "machine")) {
244 info = g_malloc0(sizeof(*info));
245 info->option = g_strdup("machine");
246 info->parameters = query_all_machine_properties();
247 QAPI_LIST_PREPEND(conf_list, info);
250 if (conf_list == NULL) {
251 error_setg(errp, "invalid option name: %s", option);
254 return conf_list;
257 QemuOptsList *qemu_find_opts_err(const char *group, Error **errp)
259 return find_list(vm_config_groups, group, errp);
262 void qemu_add_drive_opts(QemuOptsList *list)
264 int entries, i;
266 entries = ARRAY_SIZE(drive_config_groups);
267 entries--; /* keep list NULL terminated */
268 for (i = 0; i < entries; i++) {
269 if (drive_config_groups[i] == NULL) {
270 drive_config_groups[i] = list;
271 return;
274 fprintf(stderr, "ran out of space in drive_config_groups");
275 abort();
278 void qemu_add_opts(QemuOptsList *list)
280 int entries, i;
282 entries = ARRAY_SIZE(vm_config_groups);
283 entries--; /* keep list NULL terminated */
284 for (i = 0; i < entries; i++) {
285 if (vm_config_groups[i] == NULL) {
286 vm_config_groups[i] = list;
287 return;
290 fprintf(stderr, "ran out of space in vm_config_groups");
291 abort();
294 /* Returns number of config groups on success, -errno on error */
295 static int qemu_config_foreach(FILE *fp, QEMUConfigCB *cb, void *opaque,
296 const char *fname, Error **errp)
298 ERRP_GUARD();
299 char line[1024], prev_group[64], group[64], arg[64], value[1024];
300 Location loc;
301 QDict *qdict = NULL;
302 int res = -EINVAL, lno = 0;
303 int count = 0;
305 loc_push_none(&loc);
306 while (fgets(line, sizeof(line), fp) != NULL) {
307 ++lno;
308 if (line[0] == '\n') {
309 /* skip empty lines */
310 continue;
312 if (line[0] == '#') {
313 /* comment */
314 continue;
316 if (line[0] == '[') {
317 QDict *prev = qdict;
318 if (sscanf(line, "[%63s \"%63[^\"]\"]", group, value) == 2) {
319 qdict = qdict_new();
320 qdict_put_str(qdict, "id", value);
321 count++;
322 } else if (sscanf(line, "[%63[^]]]", group) == 1) {
323 qdict = qdict_new();
324 count++;
326 if (qdict != prev) {
327 if (prev) {
328 cb(prev_group, prev, opaque, errp);
329 qobject_unref(prev);
330 if (*errp) {
331 goto out;
334 strcpy(prev_group, group);
335 continue;
338 loc_set_file(fname, lno);
339 value[0] = '\0';
340 if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2 ||
341 sscanf(line, " %63s = \"\"", arg) == 1) {
342 /* arg = value */
343 if (qdict == NULL) {
344 error_setg(errp, "no group defined");
345 goto out;
347 qdict_put_str(qdict, arg, value);
348 continue;
350 error_setg(errp, "parse error");
351 goto out;
353 if (ferror(fp)) {
354 loc_pop(&loc);
355 error_setg_errno(errp, errno, "Cannot read config file");
356 goto out_no_loc;
358 res = count;
359 if (qdict) {
360 cb(group, qdict, opaque, errp);
362 out:
363 loc_pop(&loc);
364 out_no_loc:
365 qobject_unref(qdict);
366 return res;
369 void qemu_config_do_parse(const char *group, QDict *qdict, void *opaque, Error **errp)
371 QemuOptsList **lists = opaque;
372 QemuOptsList *list;
374 list = find_list(lists, group, errp);
375 if (!list) {
376 return;
379 qemu_opts_from_qdict(list, qdict, errp);
382 int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname, Error **errp)
384 return qemu_config_foreach(fp, qemu_config_do_parse, lists, fname, errp);
387 int qemu_read_config_file(const char *filename, QEMUConfigCB *cb, Error **errp)
389 FILE *f = fopen(filename, "r");
390 int ret;
392 if (f == NULL) {
393 error_setg_file_open(errp, errno, filename);
394 return -errno;
397 ret = qemu_config_foreach(f, cb, vm_config_groups, filename, errp);
398 fclose(f);
399 return ret;
402 static bool config_parse_qdict_section(QDict *options, QemuOptsList *opts,
403 Error **errp)
405 QemuOpts *subopts;
406 g_autoptr(QDict) subqdict = NULL;
407 g_autoptr(QList) list = NULL;
408 size_t orig_size, enum_size;
409 char *prefix;
411 prefix = g_strdup_printf("%s.", opts->name);
412 qdict_extract_subqdict(options, &subqdict, prefix);
413 g_free(prefix);
414 orig_size = qdict_size(subqdict);
415 if (!orig_size) {
416 return true;
419 subopts = qemu_opts_create(opts, NULL, 0, errp);
420 if (!subopts) {
421 return false;
424 if (!qemu_opts_absorb_qdict(subopts, subqdict, errp)) {
425 return false;
428 enum_size = qdict_size(subqdict);
429 if (enum_size < orig_size && enum_size) {
430 error_setg(errp, "Unknown option '%s' for [%s]",
431 qdict_first(subqdict)->key, opts->name);
432 return false;
435 if (enum_size) {
436 /* Multiple, enumerated sections */
437 QListEntry *list_entry;
438 unsigned i = 0;
440 /* Not required anymore */
441 qemu_opts_del(subopts);
443 qdict_array_split(subqdict, &list);
444 if (qdict_size(subqdict)) {
445 error_setg(errp, "Unused option '%s' for [%s]",
446 qdict_first(subqdict)->key, opts->name);
447 return false;
450 QLIST_FOREACH_ENTRY(list, list_entry) {
451 QDict *section = qobject_to(QDict, qlist_entry_obj(list_entry));
452 char *opt_name;
454 if (!section) {
455 error_setg(errp, "[%s] section (index %u) does not consist of "
456 "keys", opts->name, i);
457 return false;
460 opt_name = g_strdup_printf("%s.%u", opts->name, i++);
461 subopts = qemu_opts_create(opts, opt_name, 1, errp);
462 g_free(opt_name);
463 if (!subopts) {
464 return false;
467 if (!qemu_opts_absorb_qdict(subopts, section, errp)) {
468 qemu_opts_del(subopts);
469 return false;
472 if (qdict_size(section)) {
473 error_setg(errp, "[%s] section doesn't support the option '%s'",
474 opts->name, qdict_first(section)->key);
475 qemu_opts_del(subopts);
476 return false;
481 return true;
484 bool qemu_config_parse_qdict(QDict *options, QemuOptsList **lists,
485 Error **errp)
487 int i;
489 for (i = 0; lists[i]; i++) {
490 if (!config_parse_qdict_section(options, lists[i], errp)) {
491 return false;
495 return true;