2009-07-31 18:25:35 +08:00
|
|
|
#include "qemu-common.h"
|
2012-12-18 01:20:00 +08:00
|
|
|
#include "qemu/error-report.h"
|
|
|
|
#include "qemu/option.h"
|
|
|
|
#include "qemu/config-file.h"
|
2013-02-04 18:37:52 +08:00
|
|
|
#include "qapi/qmp/qerror.h"
|
2009-12-08 20:11:34 +08:00
|
|
|
#include "hw/qdev.h"
|
2012-12-18 01:19:43 +08:00
|
|
|
#include "qapi/error.h"
|
2013-04-25 17:50:35 +08:00
|
|
|
#include "qmp-commands.h"
|
2009-07-31 18:25:35 +08:00
|
|
|
|
2012-11-26 23:03:42 +08:00
|
|
|
static QemuOptsList *vm_config_groups[32];
|
2009-07-31 18:25:36 +08:00
|
|
|
|
2012-03-29 01:14:17 +08:00
|
|
|
static QemuOptsList *find_list(QemuOptsList **lists, const char *group,
|
|
|
|
Error **errp)
|
2009-10-14 16:39:25 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; lists[i] != NULL; i++) {
|
|
|
|
if (strcmp(lists[i]->name, group) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (lists[i] == NULL) {
|
2012-03-29 01:14:17 +08:00
|
|
|
error_set(errp, QERR_INVALID_OPTION_GROUP, group);
|
2009-10-14 16:39:25 +08:00
|
|
|
}
|
|
|
|
return lists[i];
|
|
|
|
}
|
|
|
|
|
2010-03-06 01:21:56 +08:00
|
|
|
QemuOptsList *qemu_find_opts(const char *group)
|
|
|
|
{
|
2012-03-29 01:14:17 +08:00
|
|
|
QemuOptsList *ret;
|
|
|
|
Error *local_err = NULL;
|
|
|
|
|
|
|
|
ret = find_list(vm_config_groups, group, &local_err);
|
|
|
|
if (error_is_set(&local_err)) {
|
error: Strip trailing '\n' from error string arguments (again)
Commit 6daf194d and be62a2eb got rid of a bunch, but they keep coming
back. Tracked down with this Coccinelle semantic patch:
@r@
expression err, eno, cls, fmt;
position p;
@@
(
error_report(fmt, ...)@p
|
error_set(err, cls, fmt, ...)@p
|
error_set_errno(err, eno, cls, fmt, ...)@p
|
error_setg(err, fmt, ...)@p
|
error_setg_errno(err, eno, fmt, ...)@p
)
@script:python@
fmt << r.fmt;
p << r.p;
@@
if "\\n" in str(fmt):
print "%s:%s:%s:%s" % (p[0].file, p[0].line, p[0].column, fmt)
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-id: 1360354939-10994-4-git-send-email-armbru@redhat.com
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
2013-02-09 04:22:16 +08:00
|
|
|
error_report("%s", error_get_pretty(local_err));
|
2012-03-29 01:14:17 +08:00
|
|
|
error_free(local_err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2010-03-06 01:21:56 +08:00
|
|
|
}
|
|
|
|
|
2013-04-25 17:50:35 +08:00
|
|
|
static CommandLineParameterInfoList *query_option_descs(const QemuOptDesc *desc)
|
|
|
|
{
|
|
|
|
CommandLineParameterInfoList *param_list = NULL, *entry;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (desc[i].help) {
|
|
|
|
info->has_help = true;
|
|
|
|
info->help = g_strdup(desc[i].help);
|
|
|
|
}
|
|
|
|
|
|
|
|
entry = g_malloc0(sizeof(*entry));
|
|
|
|
entry->value = info;
|
|
|
|
entry->next = param_list;
|
|
|
|
param_list = entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
return param_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
CommandLineOptionInfoList *qmp_query_command_line_options(bool has_option,
|
|
|
|
const char *option,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
CommandLineOptionInfoList *conf_list = NULL, *entry;
|
|
|
|
CommandLineOptionInfo *info;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; vm_config_groups[i] != NULL; i++) {
|
|
|
|
if (!has_option || !strcmp(option, vm_config_groups[i]->name)) {
|
|
|
|
info = g_malloc0(sizeof(*info));
|
|
|
|
info->option = g_strdup(vm_config_groups[i]->name);
|
|
|
|
info->parameters = query_option_descs(vm_config_groups[i]->desc);
|
|
|
|
entry = g_malloc0(sizeof(*entry));
|
|
|
|
entry->value = info;
|
|
|
|
entry->next = conf_list;
|
|
|
|
conf_list = entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (conf_list == NULL) {
|
|
|
|
error_setg(errp, "invalid option name: %s", option);
|
|
|
|
}
|
|
|
|
|
|
|
|
return conf_list;
|
|
|
|
}
|
|
|
|
|
2012-03-29 01:16:37 +08:00
|
|
|
QemuOptsList *qemu_find_opts_err(const char *group, Error **errp)
|
|
|
|
{
|
|
|
|
return find_list(vm_config_groups, group, errp);
|
|
|
|
}
|
|
|
|
|
2010-08-20 19:52:00 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2009-07-31 18:25:36 +08:00
|
|
|
int qemu_set_option(const char *str)
|
|
|
|
{
|
|
|
|
char group[64], id[64], arg[64];
|
2009-10-14 16:39:25 +08:00
|
|
|
QemuOptsList *list;
|
2009-07-31 18:25:36 +08:00
|
|
|
QemuOpts *opts;
|
2009-10-14 16:39:25 +08:00
|
|
|
int rc, offset;
|
2009-07-31 18:25:36 +08:00
|
|
|
|
|
|
|
rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
|
|
|
|
if (rc < 3 || str[offset] != '=') {
|
2010-02-19 00:25:24 +08:00
|
|
|
error_report("can't parse: \"%s\"", str);
|
2009-07-31 18:25:36 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-02-11 03:09:14 +08:00
|
|
|
list = qemu_find_opts(group);
|
2009-10-14 16:39:25 +08:00
|
|
|
if (list == NULL) {
|
2009-07-31 18:25:36 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-10-14 16:39:25 +08:00
|
|
|
opts = qemu_opts_find(list, id);
|
2009-07-31 18:25:36 +08:00
|
|
|
if (!opts) {
|
2010-02-19 00:25:24 +08:00
|
|
|
error_report("there is no %s \"%s\" defined",
|
|
|
|
list->name, id);
|
2009-07-31 18:25:36 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-09-23 18:24:05 +08:00
|
|
|
if (qemu_opt_set(opts, arg, str+offset+1) == -1) {
|
2009-07-31 18:25:36 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-10-14 16:39:26 +08:00
|
|
|
struct ConfigWriteData {
|
|
|
|
QemuOptsList *list;
|
|
|
|
FILE *fp;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int config_write_opt(const char *name, const char *value, void *opaque)
|
|
|
|
{
|
|
|
|
struct ConfigWriteData *data = opaque;
|
|
|
|
|
|
|
|
fprintf(data->fp, " %s = \"%s\"\n", name, value);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int config_write_opts(QemuOpts *opts, void *opaque)
|
|
|
|
{
|
|
|
|
struct ConfigWriteData *data = opaque;
|
|
|
|
const char *id = qemu_opts_id(opts);
|
|
|
|
|
|
|
|
if (id) {
|
|
|
|
fprintf(data->fp, "[%s \"%s\"]\n", data->list->name, id);
|
|
|
|
} else {
|
|
|
|
fprintf(data->fp, "[%s]\n", data->list->name);
|
|
|
|
}
|
|
|
|
qemu_opt_foreach(opts, config_write_opt, data, 0);
|
|
|
|
fprintf(data->fp, "\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_config_write(FILE *fp)
|
|
|
|
{
|
|
|
|
struct ConfigWriteData data = { .fp = fp };
|
2010-03-06 01:21:56 +08:00
|
|
|
QemuOptsList **lists = vm_config_groups;
|
2009-10-14 16:39:26 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
fprintf(fp, "# qemu config file\n\n");
|
|
|
|
for (i = 0; lists[i] != NULL; i++) {
|
|
|
|
data.list = lists[i];
|
|
|
|
qemu_opts_foreach(data.list, config_write_opts, &data, 0);
|
|
|
|
}
|
|
|
|
}
|
2009-10-14 16:39:27 +08:00
|
|
|
|
2010-03-06 01:21:56 +08:00
|
|
|
int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname)
|
2009-10-14 16:39:27 +08:00
|
|
|
{
|
|
|
|
char line[1024], group[64], id[64], arg[64], value[1024];
|
2010-02-19 02:48:33 +08:00
|
|
|
Location loc;
|
2009-10-14 16:39:27 +08:00
|
|
|
QemuOptsList *list = NULL;
|
2012-03-29 01:14:17 +08:00
|
|
|
Error *local_err = NULL;
|
2009-10-14 16:39:27 +08:00
|
|
|
QemuOpts *opts = NULL;
|
2010-02-19 02:48:33 +08:00
|
|
|
int res = -1, lno = 0;
|
2009-10-14 16:39:27 +08:00
|
|
|
|
2010-02-19 02:48:33 +08:00
|
|
|
loc_push_none(&loc);
|
2009-10-14 16:39:27 +08:00
|
|
|
while (fgets(line, sizeof(line), fp) != NULL) {
|
2010-02-19 02:48:33 +08:00
|
|
|
loc_set_file(fname, ++lno);
|
2009-10-14 16:39:27 +08:00
|
|
|
if (line[0] == '\n') {
|
|
|
|
/* skip empty lines */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (line[0] == '#') {
|
|
|
|
/* comment */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (sscanf(line, "[%63s \"%63[^\"]\"]", group, id) == 2) {
|
|
|
|
/* group with id */
|
2012-03-29 01:14:17 +08:00
|
|
|
list = find_list(lists, group, &local_err);
|
|
|
|
if (error_is_set(&local_err)) {
|
error: Strip trailing '\n' from error string arguments (again)
Commit 6daf194d and be62a2eb got rid of a bunch, but they keep coming
back. Tracked down with this Coccinelle semantic patch:
@r@
expression err, eno, cls, fmt;
position p;
@@
(
error_report(fmt, ...)@p
|
error_set(err, cls, fmt, ...)@p
|
error_set_errno(err, eno, cls, fmt, ...)@p
|
error_setg(err, fmt, ...)@p
|
error_setg_errno(err, eno, fmt, ...)@p
)
@script:python@
fmt << r.fmt;
p << r.p;
@@
if "\\n" in str(fmt):
print "%s:%s:%s:%s" % (p[0].file, p[0].line, p[0].column, fmt)
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-id: 1360354939-10994-4-git-send-email-armbru@redhat.com
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
2013-02-09 04:22:16 +08:00
|
|
|
error_report("%s", error_get_pretty(local_err));
|
2012-03-29 01:14:17 +08:00
|
|
|
error_free(local_err);
|
2010-02-19 02:48:33 +08:00
|
|
|
goto out;
|
2012-03-29 01:14:17 +08:00
|
|
|
}
|
2012-03-21 02:51:57 +08:00
|
|
|
opts = qemu_opts_create(list, id, 1, NULL);
|
2009-10-14 16:39:27 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (sscanf(line, "[%63[^]]]", group) == 1) {
|
|
|
|
/* group without id */
|
2012-03-29 01:14:17 +08:00
|
|
|
list = find_list(lists, group, &local_err);
|
|
|
|
if (error_is_set(&local_err)) {
|
error: Strip trailing '\n' from error string arguments (again)
Commit 6daf194d and be62a2eb got rid of a bunch, but they keep coming
back. Tracked down with this Coccinelle semantic patch:
@r@
expression err, eno, cls, fmt;
position p;
@@
(
error_report(fmt, ...)@p
|
error_set(err, cls, fmt, ...)@p
|
error_set_errno(err, eno, cls, fmt, ...)@p
|
error_setg(err, fmt, ...)@p
|
error_setg_errno(err, eno, fmt, ...)@p
)
@script:python@
fmt << r.fmt;
p << r.p;
@@
if "\\n" in str(fmt):
print "%s:%s:%s:%s" % (p[0].file, p[0].line, p[0].column, fmt)
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-id: 1360354939-10994-4-git-send-email-armbru@redhat.com
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
2013-02-09 04:22:16 +08:00
|
|
|
error_report("%s", error_get_pretty(local_err));
|
2012-03-29 01:14:17 +08:00
|
|
|
error_free(local_err);
|
2010-02-19 02:48:33 +08:00
|
|
|
goto out;
|
2012-03-29 01:14:17 +08:00
|
|
|
}
|
2012-12-06 14:47:22 +08:00
|
|
|
opts = qemu_opts_create_nofail(list);
|
2009-10-14 16:39:27 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) {
|
|
|
|
/* arg = value */
|
|
|
|
if (opts == NULL) {
|
2010-02-19 02:48:33 +08:00
|
|
|
error_report("no group defined");
|
|
|
|
goto out;
|
2009-10-14 16:39:27 +08:00
|
|
|
}
|
|
|
|
if (qemu_opt_set(opts, arg, value) != 0) {
|
2010-02-19 02:48:33 +08:00
|
|
|
goto out;
|
2009-10-14 16:39:27 +08:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2010-02-19 02:48:33 +08:00
|
|
|
error_report("parse error");
|
|
|
|
goto out;
|
2009-10-14 16:39:27 +08:00
|
|
|
}
|
2010-02-19 02:56:01 +08:00
|
|
|
if (ferror(fp)) {
|
|
|
|
error_report("error reading file");
|
|
|
|
goto out;
|
|
|
|
}
|
2010-02-19 02:48:33 +08:00
|
|
|
res = 0;
|
|
|
|
out:
|
|
|
|
loc_pop(&loc);
|
|
|
|
return res;
|
2009-10-14 16:39:27 +08:00
|
|
|
}
|
2010-03-06 00:25:55 +08:00
|
|
|
|
|
|
|
int qemu_read_config_file(const char *filename)
|
|
|
|
{
|
|
|
|
FILE *f = fopen(filename, "r");
|
2010-05-17 16:36:47 +08:00
|
|
|
int ret;
|
|
|
|
|
2010-03-06 00:25:55 +08:00
|
|
|
if (f == NULL) {
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
2010-05-17 16:36:47 +08:00
|
|
|
ret = qemu_config_parse(f, vm_config_groups, filename);
|
2010-03-06 00:25:55 +08:00
|
|
|
fclose(f);
|
|
|
|
|
2010-05-17 16:36:47 +08:00
|
|
|
if (ret == 0) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2010-03-06 00:25:55 +08:00
|
|
|
}
|