qemu-e2k/util/qemu-config.c

497 lines
14 KiB
C
Raw Normal View History

#include "qemu/osdep.h"
#include "block/qdict.h" /* for qdict_extract_subqdict() */
#include "qapi/error.h"
#include "qapi/qapi-commands-misc.h"
#include "qapi/qmp/qerror.h"
#include "qapi/qmp/qdict.h"
#include "qapi/qmp/qlist.h"
#include "qemu/error-report.h"
#include "qemu/option.h"
#include "qemu/config-file.h"
#include "hw/boards.h"
static QemuOptsList *vm_config_groups[48];
static QemuOptsList *drive_config_groups[5];
static QemuOptsList *find_list(QemuOptsList **lists, const char *group,
Error **errp)
{
int i;
qemu_load_module_for_opts(group);
for (i = 0; lists[i] != NULL; i++) {
if (strcmp(lists[i]->name, group) == 0)
break;
}
if (lists[i] == NULL) {
error_setg(errp, "There is no option group '%s'", group);
}
return lists[i];
}
QemuOptsList *qemu_find_opts(const char *group)
{
QemuOptsList *ret;
Error *local_err = NULL;
ret = find_list(vm_config_groups, group, &local_err);
if (local_err) {
error_report_err(local_err);
}
return ret;
}
QemuOpts *qemu_find_opts_singleton(const char *group)
{
QemuOptsList *list;
QemuOpts *opts;
list = qemu_find_opts(group);
assert(list);
opts = qemu_opts_find(list, NULL);
if (!opts) {
opts = qemu_opts_create(list, NULL, 0, &error_abort);
}
return opts;
}
static CommandLineParameterInfoList *query_option_descs(const QemuOptDesc *desc)
{
CommandLineParameterInfoList *param_list = NULL;
CommandLineParameterInfo *info;
int i;
for (i = 0; desc[i].name != NULL; i++) {
info = g_malloc0(sizeof(*info));
info->name = g_strdup(desc[i].name);
switch (desc[i].type) {
case QEMU_OPT_STRING:
info->type = COMMAND_LINE_PARAMETER_TYPE_STRING;
break;
case QEMU_OPT_BOOL:
info->type = COMMAND_LINE_PARAMETER_TYPE_BOOLEAN;
break;
case QEMU_OPT_NUMBER:
info->type = COMMAND_LINE_PARAMETER_TYPE_NUMBER;
break;
case QEMU_OPT_SIZE:
info->type = COMMAND_LINE_PARAMETER_TYPE_SIZE;
break;
}
info->help = g_strdup(desc[i].help);
info->q_default = g_strdup(desc[i].def_value_str);
QAPI_LIST_PREPEND(param_list, info);
}
return param_list;
}
/* remove repeated entry from the info list */
static void cleanup_infolist(CommandLineParameterInfoList *head)
{
CommandLineParameterInfoList *pre_entry, *cur, *del_entry;
cur = head;
while (cur->next) {
pre_entry = head;
while (pre_entry != cur->next) {
if (!strcmp(pre_entry->value->name, cur->next->value->name)) {
del_entry = cur->next;
cur->next = cur->next->next;
qemu-config: fix leak in query-command-line-options Direct leak of 160 byte(s) in 4 object(s) allocated from: #0 0x55ed7678cda8 in calloc (/home/elmarco/src/qq/build/x86_64-softmmu/qemu-system-x86_64+0x797da8) #1 0x7f3f5e725f75 in g_malloc0 /home/elmarco/src/gnome/glib/builddir/../glib/gmem.c:124 #2 0x55ed778aa3a7 in query_option_descs /home/elmarco/src/qq/util/qemu-config.c:60:16 #3 0x55ed778aa307 in get_drive_infolist /home/elmarco/src/qq/util/qemu-config.c:140:19 #4 0x55ed778a9f40 in qmp_query_command_line_options /home/elmarco/src/qq/util/qemu-config.c:254:36 #5 0x55ed76d4868c in qmp_marshal_query_command_line_options /home/elmarco/src/qq/build/qmp-marshal.c:3078:14 #6 0x55ed77855dd5 in do_qmp_dispatch /home/elmarco/src/qq/qapi/qmp-dispatch.c:104:5 #7 0x55ed778558cc in qmp_dispatch /home/elmarco/src/qq/qapi/qmp-dispatch.c:131:11 #8 0x55ed768b592f in handle_qmp_command /home/elmarco/src/qq/monitor.c:3840:11 #9 0x55ed7786ccfe in json_message_process_token /home/elmarco/src/qq/qobject/json-streamer.c:105:5 #10 0x55ed778fe37c in json_lexer_feed_char /home/elmarco/src/qq/qobject/json-lexer.c:323:13 #11 0x55ed778fdde6 in json_lexer_feed /home/elmarco/src/qq/qobject/json-lexer.c:373:15 #12 0x55ed7786cd83 in json_message_parser_feed /home/elmarco/src/qq/qobject/json-streamer.c:124:12 #13 0x55ed768b559e in monitor_qmp_read /home/elmarco/src/qq/monitor.c:3882:5 #14 0x55ed77714f29 in qemu_chr_be_write_impl /home/elmarco/src/qq/chardev/char.c:167:9 #15 0x55ed77714fde in qemu_chr_be_write /home/elmarco/src/qq/chardev/char.c:179:9 #16 0x55ed7772ffad in tcp_chr_read /home/elmarco/src/qq/chardev/char-socket.c:440:13 #17 0x55ed7777113b in qio_channel_fd_source_dispatch /home/elmarco/src/qq/io/channel-watch.c:84:12 #18 0x7f3f5e71d90b in g_main_dispatch /home/elmarco/src/gnome/glib/builddir/../glib/gmain.c:3182 #19 0x7f3f5e71e7ac in g_main_context_dispatch /home/elmarco/src/gnome/glib/builddir/../glib/gmain.c:3847 #20 0x55ed77886ffc in glib_pollfds_poll /home/elmarco/src/qq/util/main-loop.c:214:9 #21 0x55ed778865fd in os_host_main_loop_wait /home/elmarco/src/qq/util/main-loop.c:261:5 #22 0x55ed77886222 in main_loop_wait /home/elmarco/src/qq/util/main-loop.c:515:11 #23 0x55ed76d2a4df in main_loop /home/elmarco/src/qq/vl.c:1995:9 #24 0x55ed76d1cb4a in main /home/elmarco/src/qq/vl.c:4914:5 #25 0x7f3f555f6039 in __libc_start_main (/lib64/libc.so.6+0x21039) Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com> Reviewed-by: Eric Blake <eblake@redhat.com> Message-Id: <20180104160523.22995-14-marcandre.lureau@redhat.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2018-01-04 17:05:18 +01:00
del_entry->next = NULL;
qapi_free_CommandLineParameterInfoList(del_entry);
break;
}
pre_entry = pre_entry->next;
}
cur = cur->next;
}
}
/* merge the description items of two parameter infolists */
static void connect_infolist(CommandLineParameterInfoList *head,
CommandLineParameterInfoList *new)
{
CommandLineParameterInfoList *cur;
cur = head;
while (cur->next) {
cur = cur->next;
}
cur->next = new;
}
/* access all the local QemuOptsLists for drive option */
static CommandLineParameterInfoList *get_drive_infolist(void)
{
CommandLineParameterInfoList *head = NULL, *cur;
int i;
for (i = 0; drive_config_groups[i] != NULL; i++) {
if (!head) {
head = query_option_descs(drive_config_groups[i]->desc);
} else {
cur = query_option_descs(drive_config_groups[i]->desc);
connect_infolist(head, cur);
}
}
cleanup_infolist(head);
return head;
}
static CommandLineParameterInfo *objprop_to_cmdline_prop(ObjectProperty *prop)
{
CommandLineParameterInfo *info;
info = g_malloc0(sizeof(*info));
info->name = g_strdup(prop->name);
if (g_str_equal(prop->type, "bool") || g_str_equal(prop->type, "OnOffAuto")) {
info->type = COMMAND_LINE_PARAMETER_TYPE_BOOLEAN;
} else if (g_str_equal(prop->type, "int")) {
info->type = COMMAND_LINE_PARAMETER_TYPE_NUMBER;
} else if (g_str_equal(prop->type, "size")) {
info->type = COMMAND_LINE_PARAMETER_TYPE_SIZE;
} else {
info->type = COMMAND_LINE_PARAMETER_TYPE_STRING;
}
if (prop->description) {
info->help = g_strdup(prop->description);
}
return info;
}
static CommandLineParameterInfoList *query_all_machine_properties(void)
{
CommandLineParameterInfoList *params = NULL, *clpiter;
CommandLineParameterInfo *info;
GSList *machines, *curr_mach;
ObjectPropertyIterator op_iter;
ObjectProperty *prop;
bool is_new;
machines = object_class_get_list(TYPE_MACHINE, false);
assert(machines);
/* Loop over all machine classes */
for (curr_mach = machines; curr_mach; curr_mach = curr_mach->next) {
object_class_property_iter_init(&op_iter, curr_mach->data);
/* ... and over the properties of each machine: */
while ((prop = object_property_iter_next(&op_iter))) {
if (!prop->set) {
continue;
}
/*
* Check whether the property has already been put into the list
* (via another machine class)
*/
is_new = true;
for (clpiter = params; clpiter != NULL; clpiter = clpiter->next) {
if (g_str_equal(clpiter->value->name, prop->name)) {
is_new = false;
break;
}
}
/* If it hasn't been added before, add it now to the list */
if (is_new) {
info = objprop_to_cmdline_prop(prop);
QAPI_LIST_PREPEND(params, info);
}
}
}
g_slist_free(machines);
/* Add entry for the "type" parameter */
info = g_malloc0(sizeof(*info));
info->name = g_strdup("type");
info->type = COMMAND_LINE_PARAMETER_TYPE_STRING;
info->help = g_strdup("machine type");
QAPI_LIST_PREPEND(params, info);
return params;
}
CommandLineOptionInfoList *qmp_query_command_line_options(const char *option,
Error **errp)
{
CommandLineOptionInfoList *conf_list = NULL;
CommandLineOptionInfo *info;
int i;
for (i = 0; vm_config_groups[i] != NULL; i++) {
if (!option || !strcmp(option, vm_config_groups[i]->name)) {
info = g_malloc0(sizeof(*info));
info->option = g_strdup(vm_config_groups[i]->name);
if (!strcmp("drive", vm_config_groups[i]->name)) {
info->parameters = get_drive_infolist();
} else {
info->parameters =
query_option_descs(vm_config_groups[i]->desc);
}
QAPI_LIST_PREPEND(conf_list, info);
}
}
if (!option || !strcmp(option, "machine")) {
info = g_malloc0(sizeof(*info));
info->option = g_strdup("machine");
info->parameters = query_all_machine_properties();
QAPI_LIST_PREPEND(conf_list, info);
}
if (conf_list == NULL) {
error_setg(errp, "invalid option name: %s", option);
}
return conf_list;
}
QemuOptsList *qemu_find_opts_err(const char *group, Error **errp)
{
return find_list(vm_config_groups, group, errp);
}
void qemu_add_drive_opts(QemuOptsList *list)
{
int entries, i;
entries = ARRAY_SIZE(drive_config_groups);
entries--; /* keep list NULL terminated */
for (i = 0; i < entries; i++) {
if (drive_config_groups[i] == NULL) {
drive_config_groups[i] = list;
return;
}
}
fprintf(stderr, "ran out of space in drive_config_groups");
abort();
}
void qemu_add_opts(QemuOptsList *list)
{
int entries, i;
entries = ARRAY_SIZE(vm_config_groups);
entries--; /* keep list NULL terminated */
for (i = 0; i < entries; i++) {
if (vm_config_groups[i] == NULL) {
vm_config_groups[i] = list;
return;
}
}
fprintf(stderr, "ran out of space in vm_config_groups");
abort();
}
/* Returns number of config groups on success, -errno on error */
static int qemu_config_foreach(FILE *fp, QEMUConfigCB *cb, void *opaque,
const char *fname, Error **errp)
{
ERRP_GUARD();
char line[1024], prev_group[64], group[64], arg[64], value[1024];
Location loc;
QDict *qdict = NULL;
int res = -EINVAL, lno = 0;
int count = 0;
loc_push_none(&loc);
while (fgets(line, sizeof(line), fp) != NULL) {
++lno;
if (line[0] == '\n') {
/* skip empty lines */
continue;
}
if (line[0] == '#') {
/* comment */
continue;
}
if (line[0] == '[') {
QDict *prev = qdict;
if (sscanf(line, "[%63s \"%63[^\"]\"]", group, value) == 2) {
qdict = qdict_new();
qdict_put_str(qdict, "id", value);
count++;
} else if (sscanf(line, "[%63[^]]]", group) == 1) {
qdict = qdict_new();
count++;
}
if (qdict != prev) {
if (prev) {
cb(prev_group, prev, opaque, errp);
qobject_unref(prev);
if (*errp) {
goto out;
}
}
strcpy(prev_group, group);
continue;
}
}
loc_set_file(fname, lno);
value[0] = '\0';
if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2 ||
sscanf(line, " %63s = \"\"", arg) == 1) {
/* arg = value */
if (qdict == NULL) {
error_setg(errp, "no group defined");
goto out;
}
qdict_put_str(qdict, arg, value);
continue;
}
error_setg(errp, "parse error");
goto out;
}
if (ferror(fp)) {
loc_pop(&loc);
error_setg_errno(errp, errno, "Cannot read config file");
goto out_no_loc;
}
res = count;
if (qdict) {
cb(group, qdict, opaque, errp);
}
out:
loc_pop(&loc);
out_no_loc:
qobject_unref(qdict);
return res;
}
void qemu_config_do_parse(const char *group, QDict *qdict, void *opaque, Error **errp)
{
QemuOptsList **lists = opaque;
QemuOptsList *list;
list = find_list(lists, group, errp);
if (!list) {
return;
}
qemu_opts_from_qdict(list, qdict, errp);
}
int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname, Error **errp)
{
return qemu_config_foreach(fp, qemu_config_do_parse, lists, fname, errp);
}
int qemu_read_config_file(const char *filename, QEMUConfigCB *cb, Error **errp)
{
FILE *f = fopen(filename, "r");
int ret;
if (f == NULL) {
error_setg_file_open(errp, errno, filename);
return -errno;
}
ret = qemu_config_foreach(f, cb, vm_config_groups, filename, errp);
fclose(f);
return ret;
}
static bool config_parse_qdict_section(QDict *options, QemuOptsList *opts,
Error **errp)
{
QemuOpts *subopts;
g_autoptr(QDict) subqdict = NULL;
g_autoptr(QList) list = NULL;
size_t orig_size, enum_size;
char *prefix;
prefix = g_strdup_printf("%s.", opts->name);
qdict_extract_subqdict(options, &subqdict, prefix);
g_free(prefix);
orig_size = qdict_size(subqdict);
if (!orig_size) {
return true;
}
subopts = qemu_opts_create(opts, NULL, 0, errp);
if (!subopts) {
return false;
}
error: Eliminate error_propagate() with Coccinelle, part 1 When all we do with an Error we receive into a local variable is propagating to somewhere else, we can just as well receive it there right away. Convert if (!foo(..., &err)) { ... error_propagate(errp, err); ... return ... } to if (!foo(..., errp)) { ... ... return ... } where nothing else needs @err. Coccinelle script: @rule1 forall@ identifier fun, err, errp, lbl; expression list args, args2; binary operator op; constant c1, c2; symbol false; @@ if ( ( - fun(args, &err, args2) + fun(args, errp, args2) | - !fun(args, &err, args2) + !fun(args, errp, args2) | - fun(args, &err, args2) op c1 + fun(args, errp, args2) op c1 ) ) { ... when != err when != lbl: when strict - error_propagate(errp, err); ... when != err ( return; | return c2; | return false; ) } @rule2 forall@ identifier fun, err, errp, lbl; expression list args, args2; expression var; binary operator op; constant c1, c2; symbol false; @@ - var = fun(args, &err, args2); + var = fun(args, errp, args2); ... when != err if ( ( var | !var | var op c1 ) ) { ... when != err when != lbl: when strict - error_propagate(errp, err); ... when != err ( return; | return c2; | return false; | return var; ) } @depends on rule1 || rule2@ identifier err; @@ - Error *err = NULL; ... when != err Not exactly elegant, I'm afraid. The "when != lbl:" is necessary to avoid transforming if (fun(args, &err)) { goto out } ... out: error_propagate(errp, err); even though other paths to label out still need the error_propagate(). For an actual example, see sclp_realize(). Without the "when strict", Coccinelle transforms vfio_msix_setup(), incorrectly. I don't know what exactly "when strict" does, only that it helps here. The match of return is narrower than what I want, but I can't figure out how to express "return where the operand doesn't use @err". For an example where it's too narrow, see vfio_intx_enable(). Silently fails to convert hw/arm/armsse.c, because Coccinelle gets confused by ARMSSE being used both as typedef and function-like macro there. Converted manually. Line breaks tidied up manually. One nested declaration of @local_err deleted manually. Preexisting unwanted blank line dropped in hw/riscv/sifive_e.c. Signed-off-by: Markus Armbruster <armbru@redhat.com> Reviewed-by: Eric Blake <eblake@redhat.com> Message-Id: <20200707160613.848843-35-armbru@redhat.com>
2020-07-07 18:06:02 +02:00
if (!qemu_opts_absorb_qdict(subopts, subqdict, errp)) {
return false;
}
enum_size = qdict_size(subqdict);
if (enum_size < orig_size && enum_size) {
error_setg(errp, "Unknown option '%s' for [%s]",
qdict_first(subqdict)->key, opts->name);
return false;
}
if (enum_size) {
/* Multiple, enumerated sections */
QListEntry *list_entry;
unsigned i = 0;
/* Not required anymore */
qemu_opts_del(subopts);
qdict_array_split(subqdict, &list);
if (qdict_size(subqdict)) {
error_setg(errp, "Unused option '%s' for [%s]",
qdict_first(subqdict)->key, opts->name);
return false;
}
QLIST_FOREACH_ENTRY(list, list_entry) {
QDict *section = qobject_to(QDict, qlist_entry_obj(list_entry));
char *opt_name;
if (!section) {
error_setg(errp, "[%s] section (index %u) does not consist of "
"keys", opts->name, i);
return false;
}
opt_name = g_strdup_printf("%s.%u", opts->name, i++);
subopts = qemu_opts_create(opts, opt_name, 1, errp);
g_free(opt_name);
if (!subopts) {
return false;
}
error: Eliminate error_propagate() with Coccinelle, part 1 When all we do with an Error we receive into a local variable is propagating to somewhere else, we can just as well receive it there right away. Convert if (!foo(..., &err)) { ... error_propagate(errp, err); ... return ... } to if (!foo(..., errp)) { ... ... return ... } where nothing else needs @err. Coccinelle script: @rule1 forall@ identifier fun, err, errp, lbl; expression list args, args2; binary operator op; constant c1, c2; symbol false; @@ if ( ( - fun(args, &err, args2) + fun(args, errp, args2) | - !fun(args, &err, args2) + !fun(args, errp, args2) | - fun(args, &err, args2) op c1 + fun(args, errp, args2) op c1 ) ) { ... when != err when != lbl: when strict - error_propagate(errp, err); ... when != err ( return; | return c2; | return false; ) } @rule2 forall@ identifier fun, err, errp, lbl; expression list args, args2; expression var; binary operator op; constant c1, c2; symbol false; @@ - var = fun(args, &err, args2); + var = fun(args, errp, args2); ... when != err if ( ( var | !var | var op c1 ) ) { ... when != err when != lbl: when strict - error_propagate(errp, err); ... when != err ( return; | return c2; | return false; | return var; ) } @depends on rule1 || rule2@ identifier err; @@ - Error *err = NULL; ... when != err Not exactly elegant, I'm afraid. The "when != lbl:" is necessary to avoid transforming if (fun(args, &err)) { goto out } ... out: error_propagate(errp, err); even though other paths to label out still need the error_propagate(). For an actual example, see sclp_realize(). Without the "when strict", Coccinelle transforms vfio_msix_setup(), incorrectly. I don't know what exactly "when strict" does, only that it helps here. The match of return is narrower than what I want, but I can't figure out how to express "return where the operand doesn't use @err". For an example where it's too narrow, see vfio_intx_enable(). Silently fails to convert hw/arm/armsse.c, because Coccinelle gets confused by ARMSSE being used both as typedef and function-like macro there. Converted manually. Line breaks tidied up manually. One nested declaration of @local_err deleted manually. Preexisting unwanted blank line dropped in hw/riscv/sifive_e.c. Signed-off-by: Markus Armbruster <armbru@redhat.com> Reviewed-by: Eric Blake <eblake@redhat.com> Message-Id: <20200707160613.848843-35-armbru@redhat.com>
2020-07-07 18:06:02 +02:00
if (!qemu_opts_absorb_qdict(subopts, section, errp)) {
qemu_opts_del(subopts);
return false;
}
if (qdict_size(section)) {
error_setg(errp, "[%s] section doesn't support the option '%s'",
opts->name, qdict_first(section)->key);
qemu_opts_del(subopts);
return false;
}
}
}
return true;
}
bool qemu_config_parse_qdict(QDict *options, QemuOptsList **lists,
Error **errp)
{
int i;
for (i = 0; lists[i]; i++) {
if (!config_parse_qdict_section(options, lists[i], errp)) {
return false;
}
}
return true;
}