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
,
21 qemu_load_module_for_opts(group
);
22 for (i
= 0; lists
[i
] != NULL
; i
++) {
23 if (strcmp(lists
[i
]->name
, group
) == 0)
26 if (lists
[i
] == NULL
) {
27 error_setg(errp
, "There is no option group '%s'", group
);
32 QemuOptsList
*qemu_find_opts(const char *group
)
35 Error
*local_err
= NULL
;
37 ret
= find_list(vm_config_groups
, group
, &local_err
);
39 error_report_err(local_err
);
45 QemuOpts
*qemu_find_opts_singleton(const char *group
)
50 list
= qemu_find_opts(group
);
52 opts
= qemu_opts_find(list
, NULL
);
54 opts
= qemu_opts_create(list
, NULL
, 0, &error_abort
);
59 static CommandLineParameterInfoList
*query_option_descs(const QemuOptDesc
*desc
)
61 CommandLineParameterInfoList
*param_list
= NULL
;
62 CommandLineParameterInfo
*info
;
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
) {
71 info
->type
= COMMAND_LINE_PARAMETER_TYPE_STRING
;
74 info
->type
= COMMAND_LINE_PARAMETER_TYPE_BOOLEAN
;
77 info
->type
= COMMAND_LINE_PARAMETER_TYPE_NUMBER
;
80 info
->type
= COMMAND_LINE_PARAMETER_TYPE_SIZE
;
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
);
93 /* remove repeated entry from the info list */
94 static void cleanup_infolist(CommandLineParameterInfoList
*head
)
96 CommandLineParameterInfoList
*pre_entry
, *cur
, *del_entry
;
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
);
109 pre_entry
= pre_entry
->next
;
115 /* merge the description items of two parameter infolists */
116 static void connect_infolist(CommandLineParameterInfoList
*head
,
117 CommandLineParameterInfoList
*new)
119 CommandLineParameterInfoList
*cur
;
128 /* access all the local QemuOptsLists for drive option */
129 static CommandLineParameterInfoList
*get_drive_infolist(void)
131 CommandLineParameterInfoList
*head
= NULL
, *cur
;
134 for (i
= 0; drive_config_groups
[i
] != NULL
; i
++) {
136 head
= query_option_descs(drive_config_groups
[i
]->desc
);
138 cur
= query_option_descs(drive_config_groups
[i
]->desc
);
139 connect_infolist(head
, cur
);
142 cleanup_infolist(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
;
161 info
->type
= COMMAND_LINE_PARAMETER_TYPE_STRING
;
164 if (prop
->description
) {
165 info
->help
= g_strdup(prop
->description
);
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
;
180 machines
= object_class_get_list(TYPE_MACHINE
, false);
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
))) {
192 * Check whether the property has already been put into the list
193 * (via another machine class)
196 for (clpiter
= params
; clpiter
!= NULL
; clpiter
= clpiter
->next
) {
197 if (g_str_equal(clpiter
->value
->name
, prop
->name
)) {
202 /* If it hasn't been added before, add it now to the list */
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
);
222 CommandLineOptionInfoList
*qmp_query_command_line_options(const char *option
,
225 CommandLineOptionInfoList
*conf_list
= NULL
;
226 CommandLineOptionInfo
*info
;
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();
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
);
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
)
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
;
274 fprintf(stderr
, "ran out of space in drive_config_groups");
278 void qemu_add_opts(QemuOptsList
*list
)
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
;
290 fprintf(stderr
, "ran out of space in vm_config_groups");
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
)
299 char line
[1024], prev_group
[64], group
[64], arg
[64], value
[1024];
302 int res
= -EINVAL
, lno
= 0;
306 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
308 if (line
[0] == '\n') {
309 /* skip empty lines */
312 if (line
[0] == '#') {
316 if (line
[0] == '[') {
318 if (sscanf(line
, "[%63s \"%63[^\"]\"]", group
, value
) == 2) {
320 qdict_put_str(qdict
, "id", value
);
322 } else if (sscanf(line
, "[%63[^]]]", group
) == 1) {
328 cb(prev_group
, prev
, opaque
, errp
);
334 strcpy(prev_group
, group
);
338 loc_set_file(fname
, lno
);
340 if (sscanf(line
, " %63s = \"%1023[^\"]\"", arg
, value
) == 2 ||
341 sscanf(line
, " %63s = \"\"", arg
) == 1) {
344 error_setg(errp
, "no group defined");
347 qdict_put_str(qdict
, arg
, value
);
350 error_setg(errp
, "parse error");
355 error_setg_errno(errp
, errno
, "Cannot read config file");
360 cb(group
, qdict
, opaque
, errp
);
365 qobject_unref(qdict
);
369 void qemu_config_do_parse(const char *group
, QDict
*qdict
, void *opaque
, Error
**errp
)
371 QemuOptsList
**lists
= opaque
;
374 list
= find_list(lists
, group
, errp
);
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");
393 error_setg_file_open(errp
, errno
, filename
);
397 ret
= qemu_config_foreach(f
, cb
, vm_config_groups
, filename
, errp
);
402 static bool config_parse_qdict_section(QDict
*options
, QemuOptsList
*opts
,
406 g_autoptr(QDict
) subqdict
= NULL
;
407 g_autoptr(QList
) list
= NULL
;
408 size_t orig_size
, enum_size
;
411 prefix
= g_strdup_printf("%s.", opts
->name
);
412 qdict_extract_subqdict(options
, &subqdict
, prefix
);
414 orig_size
= qdict_size(subqdict
);
419 subopts
= qemu_opts_create(opts
, NULL
, 0, errp
);
424 if (!qemu_opts_absorb_qdict(subopts
, subqdict
, errp
)) {
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
);
436 /* Multiple, enumerated sections */
437 QListEntry
*list_entry
;
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
);
450 QLIST_FOREACH_ENTRY(list
, list_entry
) {
451 QDict
*section
= qobject_to(QDict
, qlist_entry_obj(list_entry
));
455 error_setg(errp
, "[%s] section (index %u) does not consist of "
456 "keys", opts
->name
, i
);
460 opt_name
= g_strdup_printf("%s.%u", opts
->name
, i
++);
461 subopts
= qemu_opts_create(opts
, opt_name
, 1, errp
);
467 if (!qemu_opts_absorb_qdict(subopts
, section
, errp
)) {
468 qemu_opts_del(subopts
);
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
);
484 bool qemu_config_parse_qdict(QDict
*options
, QemuOptsList
**lists
,
489 for (i
= 0; lists
[i
]; i
++) {
490 if (!config_parse_qdict_section(options
, lists
[i
], errp
)) {