exec: Protect map_client_list with mutex
[qemu/ar7.git] / util / qemu-config.c
blob2d32ce7e91edc60edb9c69abc2acde6504a8ff1c
1 #include "qemu-common.h"
2 #include "qemu/error-report.h"
3 #include "qemu/option.h"
4 #include "qemu/config-file.h"
5 #include "qapi/qmp/qerror.h"
6 #include "hw/qdev.h"
7 #include "qapi/error.h"
8 #include "qmp-commands.h"
9 #include "hw/i386/pc.h"
11 static QemuOptsList *vm_config_groups[32];
12 static QemuOptsList *drive_config_groups[4];
14 static QemuOptsList *find_list(QemuOptsList **lists, const char *group,
15 Error **errp)
17 int i;
19 for (i = 0; lists[i] != NULL; i++) {
20 if (strcmp(lists[i]->name, group) == 0)
21 break;
23 if (lists[i] == NULL) {
24 error_setg(errp, "There is no option group '%s'", group);
26 return lists[i];
29 QemuOptsList *qemu_find_opts(const char *group)
31 QemuOptsList *ret;
32 Error *local_err = NULL;
34 ret = find_list(vm_config_groups, group, &local_err);
35 if (local_err) {
36 error_report_err(local_err);
39 return ret;
42 QemuOpts *qemu_find_opts_singleton(const char *group)
44 QemuOptsList *list;
45 QemuOpts *opts;
47 list = qemu_find_opts(group);
48 assert(list);
49 opts = qemu_opts_find(list, NULL);
50 if (!opts) {
51 opts = qemu_opts_create(list, NULL, 0, &error_abort);
53 return opts;
56 static CommandLineParameterInfoList *query_option_descs(const QemuOptDesc *desc)
58 CommandLineParameterInfoList *param_list = NULL, *entry;
59 CommandLineParameterInfo *info;
60 int i;
62 for (i = 0; desc[i].name != NULL; i++) {
63 info = g_malloc0(sizeof(*info));
64 info->name = g_strdup(desc[i].name);
66 switch (desc[i].type) {
67 case QEMU_OPT_STRING:
68 info->type = COMMAND_LINE_PARAMETER_TYPE_STRING;
69 break;
70 case QEMU_OPT_BOOL:
71 info->type = COMMAND_LINE_PARAMETER_TYPE_BOOLEAN;
72 break;
73 case QEMU_OPT_NUMBER:
74 info->type = COMMAND_LINE_PARAMETER_TYPE_NUMBER;
75 break;
76 case QEMU_OPT_SIZE:
77 info->type = COMMAND_LINE_PARAMETER_TYPE_SIZE;
78 break;
81 if (desc[i].help) {
82 info->has_help = true;
83 info->help = g_strdup(desc[i].help);
85 if (desc[i].def_value_str) {
86 info->has_q_default = true;
87 info->q_default = g_strdup(desc[i].def_value_str);
90 entry = g_malloc0(sizeof(*entry));
91 entry->value = info;
92 entry->next = param_list;
93 param_list = entry;
96 return param_list;
99 /* remove repeated entry from the info list */
100 static void cleanup_infolist(CommandLineParameterInfoList *head)
102 CommandLineParameterInfoList *pre_entry, *cur, *del_entry;
104 cur = head;
105 while (cur->next) {
106 pre_entry = head;
107 while (pre_entry != cur->next) {
108 if (!strcmp(pre_entry->value->name, cur->next->value->name)) {
109 del_entry = cur->next;
110 cur->next = cur->next->next;
111 g_free(del_entry);
112 break;
114 pre_entry = pre_entry->next;
116 cur = cur->next;
120 /* merge the description items of two parameter infolists */
121 static void connect_infolist(CommandLineParameterInfoList *head,
122 CommandLineParameterInfoList *new)
124 CommandLineParameterInfoList *cur;
126 cur = head;
127 while (cur->next) {
128 cur = cur->next;
130 cur->next = new;
133 /* access all the local QemuOptsLists for drive option */
134 static CommandLineParameterInfoList *get_drive_infolist(void)
136 CommandLineParameterInfoList *head = NULL, *cur;
137 int i;
139 for (i = 0; drive_config_groups[i] != NULL; i++) {
140 if (!head) {
141 head = query_option_descs(drive_config_groups[i]->desc);
142 } else {
143 cur = query_option_descs(drive_config_groups[i]->desc);
144 connect_infolist(head, cur);
147 cleanup_infolist(head);
149 return head;
152 /* restore machine options that are now machine's properties */
153 static QemuOptsList machine_opts = {
154 .merge_lists = true,
155 .head = QTAILQ_HEAD_INITIALIZER(machine_opts.head),
156 .desc = {
158 .name = "type",
159 .type = QEMU_OPT_STRING,
160 .help = "emulated machine"
162 .name = "accel",
163 .type = QEMU_OPT_STRING,
164 .help = "accelerator list",
166 .name = "kernel_irqchip",
167 .type = QEMU_OPT_BOOL,
168 .help = "use KVM in-kernel irqchip",
170 .name = "kvm_shadow_mem",
171 .type = QEMU_OPT_SIZE,
172 .help = "KVM shadow MMU size",
174 .name = "kernel",
175 .type = QEMU_OPT_STRING,
176 .help = "Linux kernel image file",
178 .name = "initrd",
179 .type = QEMU_OPT_STRING,
180 .help = "Linux initial ramdisk file",
182 .name = "append",
183 .type = QEMU_OPT_STRING,
184 .help = "Linux kernel command line",
186 .name = "dtb",
187 .type = QEMU_OPT_STRING,
188 .help = "Linux kernel device tree file",
190 .name = "dumpdtb",
191 .type = QEMU_OPT_STRING,
192 .help = "Dump current dtb to a file and quit",
194 .name = "phandle_start",
195 .type = QEMU_OPT_NUMBER,
196 .help = "The first phandle ID we may generate dynamically",
198 .name = "dt_compatible",
199 .type = QEMU_OPT_STRING,
200 .help = "Overrides the \"compatible\" property of the dt root node",
202 .name = "dump-guest-core",
203 .type = QEMU_OPT_BOOL,
204 .help = "Include guest memory in a core dump",
206 .name = "mem-merge",
207 .type = QEMU_OPT_BOOL,
208 .help = "enable/disable memory merge support",
210 .name = "usb",
211 .type = QEMU_OPT_BOOL,
212 .help = "Set on/off to enable/disable usb",
214 .name = "firmware",
215 .type = QEMU_OPT_STRING,
216 .help = "firmware image",
218 .name = "iommu",
219 .type = QEMU_OPT_BOOL,
220 .help = "Set on/off to enable/disable Intel IOMMU (VT-d)",
222 .name = "suppress-vmdesc",
223 .type = QEMU_OPT_BOOL,
224 .help = "Set on to disable self-describing migration",
226 { /* End of list */ }
230 CommandLineOptionInfoList *qmp_query_command_line_options(bool has_option,
231 const char *option,
232 Error **errp)
234 CommandLineOptionInfoList *conf_list = NULL, *entry;
235 CommandLineOptionInfo *info;
236 int i;
238 for (i = 0; vm_config_groups[i] != NULL; i++) {
239 if (!has_option || !strcmp(option, vm_config_groups[i]->name)) {
240 info = g_malloc0(sizeof(*info));
241 info->option = g_strdup(vm_config_groups[i]->name);
242 if (!strcmp("drive", vm_config_groups[i]->name)) {
243 info->parameters = get_drive_infolist();
244 } else if (!strcmp("machine", vm_config_groups[i]->name)) {
245 info->parameters = query_option_descs(machine_opts.desc);
246 } else {
247 info->parameters =
248 query_option_descs(vm_config_groups[i]->desc);
250 entry = g_malloc0(sizeof(*entry));
251 entry->value = info;
252 entry->next = conf_list;
253 conf_list = entry;
257 if (conf_list == NULL) {
258 error_setg(errp, "invalid option name: %s", option);
261 return conf_list;
264 QemuOptsList *qemu_find_opts_err(const char *group, Error **errp)
266 return find_list(vm_config_groups, group, errp);
269 void qemu_add_drive_opts(QemuOptsList *list)
271 int entries, i;
273 entries = ARRAY_SIZE(drive_config_groups);
274 entries--; /* keep list NULL terminated */
275 for (i = 0; i < entries; i++) {
276 if (drive_config_groups[i] == NULL) {
277 drive_config_groups[i] = list;
278 return;
281 fprintf(stderr, "ran out of space in drive_config_groups");
282 abort();
285 void qemu_add_opts(QemuOptsList *list)
287 int entries, i;
289 entries = ARRAY_SIZE(vm_config_groups);
290 entries--; /* keep list NULL terminated */
291 for (i = 0; i < entries; i++) {
292 if (vm_config_groups[i] == NULL) {
293 vm_config_groups[i] = list;
294 return;
297 fprintf(stderr, "ran out of space in vm_config_groups");
298 abort();
301 int qemu_set_option(const char *str)
303 Error *local_err = NULL;
304 char group[64], id[64], arg[64];
305 QemuOptsList *list;
306 QemuOpts *opts;
307 int rc, offset;
309 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
310 if (rc < 3 || str[offset] != '=') {
311 error_report("can't parse: \"%s\"", str);
312 return -1;
315 list = qemu_find_opts(group);
316 if (list == NULL) {
317 return -1;
320 opts = qemu_opts_find(list, id);
321 if (!opts) {
322 error_report("there is no %s \"%s\" defined",
323 list->name, id);
324 return -1;
327 qemu_opt_set(opts, arg, str + offset + 1, &local_err);
328 if (local_err) {
329 error_report_err(local_err);
330 return -1;
332 return 0;
335 struct ConfigWriteData {
336 QemuOptsList *list;
337 FILE *fp;
340 static int config_write_opt(const char *name, const char *value, void *opaque)
342 struct ConfigWriteData *data = opaque;
344 fprintf(data->fp, " %s = \"%s\"\n", name, value);
345 return 0;
348 static int config_write_opts(QemuOpts *opts, void *opaque)
350 struct ConfigWriteData *data = opaque;
351 const char *id = qemu_opts_id(opts);
353 if (id) {
354 fprintf(data->fp, "[%s \"%s\"]\n", data->list->name, id);
355 } else {
356 fprintf(data->fp, "[%s]\n", data->list->name);
358 qemu_opt_foreach(opts, config_write_opt, data, 0);
359 fprintf(data->fp, "\n");
360 return 0;
363 void qemu_config_write(FILE *fp)
365 struct ConfigWriteData data = { .fp = fp };
366 QemuOptsList **lists = vm_config_groups;
367 int i;
369 fprintf(fp, "# qemu config file\n\n");
370 for (i = 0; lists[i] != NULL; i++) {
371 data.list = lists[i];
372 qemu_opts_foreach(data.list, config_write_opts, &data, 0);
376 int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname)
378 char line[1024], group[64], id[64], arg[64], value[1024];
379 Location loc;
380 QemuOptsList *list = NULL;
381 Error *local_err = NULL;
382 QemuOpts *opts = NULL;
383 int res = -1, lno = 0;
385 loc_push_none(&loc);
386 while (fgets(line, sizeof(line), fp) != NULL) {
387 loc_set_file(fname, ++lno);
388 if (line[0] == '\n') {
389 /* skip empty lines */
390 continue;
392 if (line[0] == '#') {
393 /* comment */
394 continue;
396 if (sscanf(line, "[%63s \"%63[^\"]\"]", group, id) == 2) {
397 /* group with id */
398 list = find_list(lists, group, &local_err);
399 if (local_err) {
400 error_report_err(local_err);
401 goto out;
403 opts = qemu_opts_create(list, id, 1, NULL);
404 continue;
406 if (sscanf(line, "[%63[^]]]", group) == 1) {
407 /* group without id */
408 list = find_list(lists, group, &local_err);
409 if (local_err) {
410 error_report_err(local_err);
411 goto out;
413 opts = qemu_opts_create(list, NULL, 0, &error_abort);
414 continue;
416 if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) {
417 /* arg = value */
418 if (opts == NULL) {
419 error_report("no group defined");
420 goto out;
422 qemu_opt_set(opts, arg, value, &local_err);
423 if (local_err) {
424 error_report_err(local_err);
425 goto out;
427 continue;
429 error_report("parse error");
430 goto out;
432 if (ferror(fp)) {
433 error_report("error reading file");
434 goto out;
436 res = 0;
437 out:
438 loc_pop(&loc);
439 return res;
442 int qemu_read_config_file(const char *filename)
444 FILE *f = fopen(filename, "r");
445 int ret;
447 if (f == NULL) {
448 return -errno;
451 ret = qemu_config_parse(f, vm_config_groups, filename);
452 fclose(f);
454 if (ret == 0) {
455 return 0;
456 } else {
457 return -EINVAL;
461 static void config_parse_qdict_section(QDict *options, QemuOptsList *opts,
462 Error **errp)
464 QemuOpts *subopts;
465 QDict *subqdict;
466 QList *list = NULL;
467 Error *local_err = NULL;
468 size_t orig_size, enum_size;
469 char *prefix;
471 prefix = g_strdup_printf("%s.", opts->name);
472 qdict_extract_subqdict(options, &subqdict, prefix);
473 g_free(prefix);
474 orig_size = qdict_size(subqdict);
475 if (!orig_size) {
476 goto out;
479 subopts = qemu_opts_create(opts, NULL, 0, &local_err);
480 if (local_err) {
481 error_propagate(errp, local_err);
482 goto out;
485 qemu_opts_absorb_qdict(subopts, subqdict, &local_err);
486 if (local_err) {
487 error_propagate(errp, local_err);
488 goto out;
491 enum_size = qdict_size(subqdict);
492 if (enum_size < orig_size && enum_size) {
493 error_setg(errp, "Unknown option '%s' for [%s]",
494 qdict_first(subqdict)->key, opts->name);
495 goto out;
498 if (enum_size) {
499 /* Multiple, enumerated sections */
500 QListEntry *list_entry;
501 unsigned i = 0;
503 /* Not required anymore */
504 qemu_opts_del(subopts);
506 qdict_array_split(subqdict, &list);
507 if (qdict_size(subqdict)) {
508 error_setg(errp, "Unused option '%s' for [%s]",
509 qdict_first(subqdict)->key, opts->name);
510 goto out;
513 QLIST_FOREACH_ENTRY(list, list_entry) {
514 QDict *section = qobject_to_qdict(qlist_entry_obj(list_entry));
515 char *opt_name;
517 if (!section) {
518 error_setg(errp, "[%s] section (index %u) does not consist of "
519 "keys", opts->name, i);
520 goto out;
523 opt_name = g_strdup_printf("%s.%u", opts->name, i++);
524 subopts = qemu_opts_create(opts, opt_name, 1, &local_err);
525 g_free(opt_name);
526 if (local_err) {
527 error_propagate(errp, local_err);
528 goto out;
531 qemu_opts_absorb_qdict(subopts, section, &local_err);
532 if (local_err) {
533 error_propagate(errp, local_err);
534 qemu_opts_del(subopts);
535 goto out;
538 if (qdict_size(section)) {
539 error_setg(errp, "[%s] section doesn't support the option '%s'",
540 opts->name, qdict_first(section)->key);
541 qemu_opts_del(subopts);
542 goto out;
547 out:
548 QDECREF(subqdict);
549 QDECREF(list);
552 void qemu_config_parse_qdict(QDict *options, QemuOptsList **lists,
553 Error **errp)
555 int i;
556 Error *local_err = NULL;
558 for (i = 0; lists[i]; i++) {
559 config_parse_qdict_section(options, lists[i], &local_err);
560 if (local_err) {
561 error_propagate(errp, local_err);
562 return;