blob: 60051dfed4b1778873c9779e52347b70aa9a3769 [file] [log] [blame]
Gerd Hoffmann7282a032009-07-31 12:25:35 +02001#include "qemu-common.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +01002#include "qemu/error-report.h"
3#include "qemu/option.h"
4#include "qemu/config-file.h"
Paolo Bonzinib4a42f82013-02-04 11:37:52 +01005#include "qapi/qmp/qerror.h"
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01006#include "hw/qdev.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +01007#include "qapi/error.h"
Amos Kong1f8f9872013-04-25 17:50:35 +08008#include "qmp-commands.h"
Gerd Hoffmann7282a032009-07-31 12:25:35 +02009
Paolo Bonzini4d454572012-11-26 16:03:42 +010010static QemuOptsList *vm_config_groups[32];
Amos Kong968854c2013-11-09 12:15:47 +080011static QemuOptsList *drive_config_groups[4];
Gerd Hoffmannd058fe02009-07-31 12:25:36 +020012
Luiz Capitulino2ac20612012-03-28 14:14:17 -030013static QemuOptsList *find_list(QemuOptsList **lists, const char *group,
14 Error **errp)
Gerd Hoffmannd058fe02009-07-31 12:25:36 +020015{
Gerd Hoffmannddc97852009-10-14 10:39:25 +020016 int i;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +020017
18 for (i = 0; lists[i] != NULL; i++) {
19 if (strcmp(lists[i]->name, group) == 0)
20 break;
21 }
22 if (lists[i] == NULL) {
Luiz Capitulino2ac20612012-03-28 14:14:17 -030023 error_set(errp, QERR_INVALID_OPTION_GROUP, group);
Gerd Hoffmannddc97852009-10-14 10:39:25 +020024 }
25 return lists[i];
26}
27
Kevin Wolf490b6482010-03-05 18:21:56 +010028QemuOptsList *qemu_find_opts(const char *group)
29{
Luiz Capitulino2ac20612012-03-28 14:14:17 -030030 QemuOptsList *ret;
31 Error *local_err = NULL;
32
33 ret = find_list(vm_config_groups, group, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +010034 if (local_err) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +010035 error_report("%s", error_get_pretty(local_err));
Luiz Capitulino2ac20612012-03-28 14:14:17 -030036 error_free(local_err);
37 }
38
39 return ret;
Kevin Wolf490b6482010-03-05 18:21:56 +010040}
41
Paolo Bonzinie96e5ae2014-03-06 10:39:24 +010042QemuOpts *qemu_find_opts_singleton(const char *group)
43{
44 QemuOptsList *list;
45 QemuOpts *opts;
46
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);
52 }
53 return opts;
54}
55
Amos Kong1f8f9872013-04-25 17:50:35 +080056static CommandLineParameterInfoList *query_option_descs(const QemuOptDesc *desc)
57{
58 CommandLineParameterInfoList *param_list = NULL, *entry;
59 CommandLineParameterInfo *info;
60 int i;
61
62 for (i = 0; desc[i].name != NULL; i++) {
63 info = g_malloc0(sizeof(*info));
64 info->name = g_strdup(desc[i].name);
65
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;
79 }
80
81 if (desc[i].help) {
82 info->has_help = true;
83 info->help = g_strdup(desc[i].help);
84 }
85
86 entry = g_malloc0(sizeof(*entry));
87 entry->value = info;
88 entry->next = param_list;
89 param_list = entry;
90 }
91
92 return param_list;
93}
94
Amos Kong968854c2013-11-09 12:15:47 +080095/* remove repeated entry from the info list */
96static void cleanup_infolist(CommandLineParameterInfoList *head)
97{
98 CommandLineParameterInfoList *pre_entry, *cur, *del_entry;
99
100 cur = head;
101 while (cur->next) {
102 pre_entry = head;
103 while (pre_entry != cur->next) {
104 if (!strcmp(pre_entry->value->name, cur->next->value->name)) {
105 del_entry = cur->next;
106 cur->next = cur->next->next;
107 g_free(del_entry);
108 break;
109 }
110 pre_entry = pre_entry->next;
111 }
112 cur = cur->next;
113 }
114}
115
116/* merge the description items of two parameter infolists */
117static void connect_infolist(CommandLineParameterInfoList *head,
118 CommandLineParameterInfoList *new)
119{
120 CommandLineParameterInfoList *cur;
121
122 cur = head;
123 while (cur->next) {
124 cur = cur->next;
125 }
126 cur->next = new;
127}
128
129/* access all the local QemuOptsLists for drive option */
130static CommandLineParameterInfoList *get_drive_infolist(void)
131{
132 CommandLineParameterInfoList *head = NULL, *cur;
133 int i;
134
135 for (i = 0; drive_config_groups[i] != NULL; i++) {
136 if (!head) {
137 head = query_option_descs(drive_config_groups[i]->desc);
138 } else {
139 cur = query_option_descs(drive_config_groups[i]->desc);
140 connect_infolist(head, cur);
141 }
142 }
143 cleanup_infolist(head);
144
145 return head;
146}
147
Amos Kong1f8f9872013-04-25 17:50:35 +0800148CommandLineOptionInfoList *qmp_query_command_line_options(bool has_option,
149 const char *option,
150 Error **errp)
151{
152 CommandLineOptionInfoList *conf_list = NULL, *entry;
153 CommandLineOptionInfo *info;
154 int i;
155
156 for (i = 0; vm_config_groups[i] != NULL; i++) {
157 if (!has_option || !strcmp(option, vm_config_groups[i]->name)) {
158 info = g_malloc0(sizeof(*info));
159 info->option = g_strdup(vm_config_groups[i]->name);
Amos Kong968854c2013-11-09 12:15:47 +0800160 if (!strcmp("drive", vm_config_groups[i]->name)) {
161 info->parameters = get_drive_infolist();
162 } else {
163 info->parameters =
164 query_option_descs(vm_config_groups[i]->desc);
165 }
Amos Kong1f8f9872013-04-25 17:50:35 +0800166 entry = g_malloc0(sizeof(*entry));
167 entry->value = info;
168 entry->next = conf_list;
169 conf_list = entry;
170 }
171 }
172
173 if (conf_list == NULL) {
174 error_setg(errp, "invalid option name: %s", option);
175 }
176
177 return conf_list;
178}
179
Luiz Capitulino60d56662012-03-28 14:16:37 -0300180QemuOptsList *qemu_find_opts_err(const char *group, Error **errp)
181{
182 return find_list(vm_config_groups, group, errp);
183}
184
Amos Kong968854c2013-11-09 12:15:47 +0800185void qemu_add_drive_opts(QemuOptsList *list)
186{
187 int entries, i;
188
189 entries = ARRAY_SIZE(drive_config_groups);
190 entries--; /* keep list NULL terminated */
191 for (i = 0; i < entries; i++) {
192 if (drive_config_groups[i] == NULL) {
193 drive_config_groups[i] = list;
194 return;
195 }
196 }
197 fprintf(stderr, "ran out of space in drive_config_groups");
198 abort();
199}
200
Gerd Hoffmanndfe795e2010-08-20 13:52:00 +0200201void qemu_add_opts(QemuOptsList *list)
202{
203 int entries, i;
204
205 entries = ARRAY_SIZE(vm_config_groups);
206 entries--; /* keep list NULL terminated */
207 for (i = 0; i < entries; i++) {
208 if (vm_config_groups[i] == NULL) {
209 vm_config_groups[i] = list;
210 return;
211 }
212 }
213 fprintf(stderr, "ran out of space in vm_config_groups");
214 abort();
215}
216
Gerd Hoffmannddc97852009-10-14 10:39:25 +0200217int qemu_set_option(const char *str)
218{
219 char group[64], id[64], arg[64];
220 QemuOptsList *list;
221 QemuOpts *opts;
222 int rc, offset;
223
224 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
225 if (rc < 3 || str[offset] != '=') {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100226 error_report("can't parse: \"%s\"", str);
Gerd Hoffmannd058fe02009-07-31 12:25:36 +0200227 return -1;
228 }
229
Markus Armbruster304329e2010-02-10 20:09:14 +0100230 list = qemu_find_opts(group);
Gerd Hoffmannddc97852009-10-14 10:39:25 +0200231 if (list == NULL) {
232 return -1;
233 }
234
235 opts = qemu_opts_find(list, id);
Gerd Hoffmannd058fe02009-07-31 12:25:36 +0200236 if (!opts) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100237 error_report("there is no %s \"%s\" defined",
238 list->name, id);
Gerd Hoffmannd058fe02009-07-31 12:25:36 +0200239 return -1;
240 }
241
Mark McLoughlin3df04ac2009-09-23 11:24:05 +0100242 if (qemu_opt_set(opts, arg, str+offset+1) == -1) {
Gerd Hoffmannd058fe02009-07-31 12:25:36 +0200243 return -1;
244 }
245 return 0;
246}
247
Gerd Hoffmann9d993392009-10-14 10:39:26 +0200248struct ConfigWriteData {
249 QemuOptsList *list;
250 FILE *fp;
251};
252
253static int config_write_opt(const char *name, const char *value, void *opaque)
254{
255 struct ConfigWriteData *data = opaque;
256
257 fprintf(data->fp, " %s = \"%s\"\n", name, value);
258 return 0;
259}
260
261static int config_write_opts(QemuOpts *opts, void *opaque)
262{
263 struct ConfigWriteData *data = opaque;
264 const char *id = qemu_opts_id(opts);
265
266 if (id) {
267 fprintf(data->fp, "[%s \"%s\"]\n", data->list->name, id);
268 } else {
269 fprintf(data->fp, "[%s]\n", data->list->name);
270 }
271 qemu_opt_foreach(opts, config_write_opt, data, 0);
272 fprintf(data->fp, "\n");
273 return 0;
274}
275
276void qemu_config_write(FILE *fp)
277{
278 struct ConfigWriteData data = { .fp = fp };
Kevin Wolf490b6482010-03-05 18:21:56 +0100279 QemuOptsList **lists = vm_config_groups;
Gerd Hoffmann9d993392009-10-14 10:39:26 +0200280 int i;
281
282 fprintf(fp, "# qemu config file\n\n");
283 for (i = 0; lists[i] != NULL; i++) {
284 data.list = lists[i];
285 qemu_opts_foreach(data.list, config_write_opts, &data, 0);
286 }
287}
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200288
Kevin Wolf490b6482010-03-05 18:21:56 +0100289int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname)
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200290{
291 char line[1024], group[64], id[64], arg[64], value[1024];
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100292 Location loc;
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200293 QemuOptsList *list = NULL;
Luiz Capitulino2ac20612012-03-28 14:14:17 -0300294 Error *local_err = NULL;
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200295 QemuOpts *opts = NULL;
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100296 int res = -1, lno = 0;
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200297
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100298 loc_push_none(&loc);
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200299 while (fgets(line, sizeof(line), fp) != NULL) {
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100300 loc_set_file(fname, ++lno);
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200301 if (line[0] == '\n') {
302 /* skip empty lines */
303 continue;
304 }
305 if (line[0] == '#') {
306 /* comment */
307 continue;
308 }
309 if (sscanf(line, "[%63s \"%63[^\"]\"]", group, id) == 2) {
310 /* group with id */
Luiz Capitulino2ac20612012-03-28 14:14:17 -0300311 list = find_list(lists, group, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100312 if (local_err) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100313 error_report("%s", error_get_pretty(local_err));
Luiz Capitulino2ac20612012-03-28 14:14:17 -0300314 error_free(local_err);
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100315 goto out;
Luiz Capitulino2ac20612012-03-28 14:14:17 -0300316 }
Luiz Capitulino8be7e7e2012-03-20 15:51:57 -0300317 opts = qemu_opts_create(list, id, 1, NULL);
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200318 continue;
319 }
320 if (sscanf(line, "[%63[^]]]", group) == 1) {
321 /* group without id */
Luiz Capitulino2ac20612012-03-28 14:14:17 -0300322 list = find_list(lists, group, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100323 if (local_err) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +0100324 error_report("%s", error_get_pretty(local_err));
Luiz Capitulino2ac20612012-03-28 14:14:17 -0300325 error_free(local_err);
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100326 goto out;
Luiz Capitulino2ac20612012-03-28 14:14:17 -0300327 }
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800328 opts = qemu_opts_create(list, NULL, 0, &error_abort);
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200329 continue;
330 }
331 if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) {
332 /* arg = value */
333 if (opts == NULL) {
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100334 error_report("no group defined");
335 goto out;
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200336 }
337 if (qemu_opt_set(opts, arg, value) != 0) {
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100338 goto out;
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200339 }
340 continue;
341 }
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100342 error_report("parse error");
343 goto out;
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200344 }
Markus Armbrusteref825162010-02-18 19:56:01 +0100345 if (ferror(fp)) {
346 error_report("error reading file");
347 goto out;
348 }
Markus Armbrustercf5a65a2010-02-18 19:48:33 +0100349 res = 0;
350out:
351 loc_pop(&loc);
352 return res;
Gerd Hoffmann42262ba2009-10-14 10:39:27 +0200353}
Kevin Wolfdcfb0932010-03-05 17:25:55 +0100354
355int qemu_read_config_file(const char *filename)
356{
357 FILE *f = fopen(filename, "r");
Kevin Wolf019e78b2010-05-17 10:36:47 +0200358 int ret;
359
Kevin Wolfdcfb0932010-03-05 17:25:55 +0100360 if (f == NULL) {
361 return -errno;
362 }
363
Kevin Wolf019e78b2010-05-17 10:36:47 +0200364 ret = qemu_config_parse(f, vm_config_groups, filename);
Kevin Wolfdcfb0932010-03-05 17:25:55 +0100365 fclose(f);
366
Kevin Wolf019e78b2010-05-17 10:36:47 +0200367 if (ret == 0) {
368 return 0;
369 } else {
370 return -EINVAL;
371 }
Kevin Wolfdcfb0932010-03-05 17:25:55 +0100372}
Max Reitzadf5c442013-12-20 19:28:05 +0100373
374static void config_parse_qdict_section(QDict *options, QemuOptsList *opts,
375 Error **errp)
376{
377 QemuOpts *subopts;
378 QDict *subqdict;
379 QList *list = NULL;
380 Error *local_err = NULL;
381 size_t orig_size, enum_size;
382 char *prefix;
383
384 prefix = g_strdup_printf("%s.", opts->name);
385 qdict_extract_subqdict(options, &subqdict, prefix);
386 g_free(prefix);
387 orig_size = qdict_size(subqdict);
388 if (!orig_size) {
389 goto out;
390 }
391
392 subopts = qemu_opts_create(opts, NULL, 0, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100393 if (local_err) {
Max Reitzadf5c442013-12-20 19:28:05 +0100394 error_propagate(errp, local_err);
395 goto out;
396 }
397
398 qemu_opts_absorb_qdict(subopts, subqdict, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100399 if (local_err) {
Max Reitzadf5c442013-12-20 19:28:05 +0100400 error_propagate(errp, local_err);
401 goto out;
402 }
403
404 enum_size = qdict_size(subqdict);
405 if (enum_size < orig_size && enum_size) {
406 error_setg(errp, "Unknown option '%s' for [%s]",
407 qdict_first(subqdict)->key, opts->name);
408 goto out;
409 }
410
411 if (enum_size) {
412 /* Multiple, enumerated sections */
413 QListEntry *list_entry;
414 unsigned i = 0;
415
416 /* Not required anymore */
417 qemu_opts_del(subopts);
418
419 qdict_array_split(subqdict, &list);
420 if (qdict_size(subqdict)) {
421 error_setg(errp, "Unused option '%s' for [%s]",
422 qdict_first(subqdict)->key, opts->name);
423 goto out;
424 }
425
426 QLIST_FOREACH_ENTRY(list, list_entry) {
427 QDict *section = qobject_to_qdict(qlist_entry_obj(list_entry));
428 char *opt_name;
429
Max Reitzae39c4b2014-02-21 19:11:39 +0100430 if (!section) {
431 error_setg(errp, "[%s] section (index %u) does not consist of "
432 "keys", opts->name, i);
433 goto out;
434 }
435
Max Reitzadf5c442013-12-20 19:28:05 +0100436 opt_name = g_strdup_printf("%s.%u", opts->name, i++);
437 subopts = qemu_opts_create(opts, opt_name, 1, &local_err);
438 g_free(opt_name);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100439 if (local_err) {
Max Reitzadf5c442013-12-20 19:28:05 +0100440 error_propagate(errp, local_err);
441 goto out;
442 }
443
444 qemu_opts_absorb_qdict(subopts, section, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100445 if (local_err) {
Max Reitzadf5c442013-12-20 19:28:05 +0100446 error_propagate(errp, local_err);
447 qemu_opts_del(subopts);
448 goto out;
449 }
450
451 if (qdict_size(section)) {
452 error_setg(errp, "[%s] section doesn't support the option '%s'",
453 opts->name, qdict_first(section)->key);
454 qemu_opts_del(subopts);
455 goto out;
456 }
457 }
458 }
459
460out:
461 QDECREF(subqdict);
462 QDECREF(list);
463}
464
465void qemu_config_parse_qdict(QDict *options, QemuOptsList **lists,
466 Error **errp)
467{
468 int i;
469 Error *local_err = NULL;
470
471 for (i = 0; lists[i]; i++) {
472 config_parse_qdict_section(options, lists[i], &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100473 if (local_err) {
Max Reitzadf5c442013-12-20 19:28:05 +0100474 error_propagate(errp, local_err);
475 return;
476 }
477 }
478}