mirror of
https://github.com/qemu/qemu.git
synced 2024-11-23 19:03:38 +08:00
qemu-option: qemu_opts_create(): use error_set()
This commit converts qemu_opts_create() from qerror_report() to error_set(). Currently, most calls to qemu_opts_create() can't fail, so most callers don't need any changes. The two cases where code checks for qemu_opts_create() erros are: 1. Initialization code in vl.c. All of them print their own error messages directly to stderr, no need to pass the Error object 2. The functions opts_parse(), qemu_opts_from_qdict() and qemu_chr_parse_compat() make use of the error information and they can be called from HMP or QMP. In this case, to allow for incremental conversion, we propagate the error up using qerror_report_err(), which keeps the QError semantics Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com> Reviewed-By: Laszlo Ersek <lersek@redhat.com>
This commit is contained in:
parent
783e9b4826
commit
8be7e7e4c7
@ -569,7 +569,7 @@ DriveInfo *drive_init(QemuOpts *opts, int default_to_scsi)
|
|||||||
break;
|
break;
|
||||||
case IF_VIRTIO:
|
case IF_VIRTIO:
|
||||||
/* add virtio block device */
|
/* add virtio block device */
|
||||||
opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
|
opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, NULL);
|
||||||
if (arch_type == QEMU_ARCH_S390X) {
|
if (arch_type == QEMU_ARCH_S390X) {
|
||||||
qemu_opt_set(opts, "driver", "virtio-blk-s390");
|
qemu_opt_set(opts, "driver", "virtio-blk-s390");
|
||||||
} else {
|
} else {
|
||||||
|
@ -584,7 +584,7 @@ static USBDevice *usb_msd_init(USBBus *bus, const char *filename)
|
|||||||
|
|
||||||
/* parse -usbdevice disk: syntax into drive opts */
|
/* parse -usbdevice disk: syntax into drive opts */
|
||||||
snprintf(id, sizeof(id), "usb%d", nr++);
|
snprintf(id, sizeof(id), "usb%d", nr++);
|
||||||
opts = qemu_opts_create(qemu_find_opts("drive"), id, 0);
|
opts = qemu_opts_create(qemu_find_opts("drive"), id, 0, NULL);
|
||||||
|
|
||||||
p1 = strchr(filename, ':');
|
p1 = strchr(filename, ':');
|
||||||
if (p1++) {
|
if (p1++) {
|
||||||
|
@ -66,7 +66,7 @@ int select_watchdog(const char *p)
|
|||||||
QLIST_FOREACH(model, &watchdog_list, entry) {
|
QLIST_FOREACH(model, &watchdog_list, entry) {
|
||||||
if (strcasecmp(model->wdt_name, p) == 0) {
|
if (strcasecmp(model->wdt_name, p) == 0) {
|
||||||
/* add the device */
|
/* add the device */
|
||||||
opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
|
opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, NULL);
|
||||||
qemu_opt_set(opts, "driver", p);
|
qemu_opt_set(opts, "driver", p);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -2584,10 +2584,14 @@ QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename)
|
|||||||
int pos;
|
int pos;
|
||||||
const char *p;
|
const char *p;
|
||||||
QemuOpts *opts;
|
QemuOpts *opts;
|
||||||
|
Error *local_err = NULL;
|
||||||
|
|
||||||
opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1);
|
opts = qemu_opts_create(qemu_find_opts("chardev"), label, 1, &local_err);
|
||||||
if (NULL == opts)
|
if (error_is_set(&local_err)) {
|
||||||
|
qerror_report_err(local_err);
|
||||||
|
error_free(local_err);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
if (strstart(filename, "mon:", &p)) {
|
if (strstart(filename, "mon:", &p)) {
|
||||||
filename = p;
|
filename = p;
|
||||||
|
@ -709,7 +709,7 @@ int qemu_global_option(const char *str)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
opts = qemu_opts_create(&qemu_global_opts, NULL, 0);
|
opts = qemu_opts_create(&qemu_global_opts, NULL, 0, NULL);
|
||||||
qemu_opt_set(opts, "driver", driver);
|
qemu_opt_set(opts, "driver", driver);
|
||||||
qemu_opt_set(opts, "property", property);
|
qemu_opt_set(opts, "property", property);
|
||||||
qemu_opt_set(opts, "value", str+offset+1);
|
qemu_opt_set(opts, "value", str+offset+1);
|
||||||
@ -781,7 +781,7 @@ int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname)
|
|||||||
list = find_list(lists, group);
|
list = find_list(lists, group);
|
||||||
if (list == NULL)
|
if (list == NULL)
|
||||||
goto out;
|
goto out;
|
||||||
opts = qemu_opts_create(list, id, 1);
|
opts = qemu_opts_create(list, id, 1, NULL);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (sscanf(line, "[%63[^]]]", group) == 1) {
|
if (sscanf(line, "[%63[^]]]", group) == 1) {
|
||||||
@ -789,7 +789,7 @@ int qemu_config_parse(FILE *fp, QemuOptsList **lists, const char *fname)
|
|||||||
list = find_list(lists, group);
|
list = find_list(lists, group);
|
||||||
if (list == NULL)
|
if (list == NULL)
|
||||||
goto out;
|
goto out;
|
||||||
opts = qemu_opts_create(list, NULL, 0);
|
opts = qemu_opts_create(list, NULL, 0, NULL);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) {
|
if (sscanf(line, " %63s = \"%1023[^\"]\"", arg, value) == 2) {
|
||||||
|
@ -30,6 +30,7 @@
|
|||||||
#include "qemu-error.h"
|
#include "qemu-error.h"
|
||||||
#include "qemu-objects.h"
|
#include "qemu-objects.h"
|
||||||
#include "qemu-option.h"
|
#include "qemu-option.h"
|
||||||
|
#include "error.h"
|
||||||
#include "qerror.h"
|
#include "qerror.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -729,20 +730,21 @@ static int id_wellformed(const char *id)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, int fail_if_exists)
|
QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id,
|
||||||
|
int fail_if_exists, Error **errp)
|
||||||
{
|
{
|
||||||
QemuOpts *opts = NULL;
|
QemuOpts *opts = NULL;
|
||||||
|
|
||||||
if (id) {
|
if (id) {
|
||||||
if (!id_wellformed(id)) {
|
if (!id_wellformed(id)) {
|
||||||
qerror_report(QERR_INVALID_PARAMETER_VALUE, "id", "an identifier");
|
error_set(errp,QERR_INVALID_PARAMETER_VALUE, "id", "an identifier");
|
||||||
error_printf_unless_qmp("Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.\n");
|
error_printf_unless_qmp("Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
opts = qemu_opts_find(list, id);
|
opts = qemu_opts_find(list, id);
|
||||||
if (opts != NULL) {
|
if (opts != NULL) {
|
||||||
if (fail_if_exists && !list->merge_lists) {
|
if (fail_if_exists && !list->merge_lists) {
|
||||||
qerror_report(QERR_DUPLICATE_ID, id, list->name);
|
error_set(errp, QERR_DUPLICATE_ID, id, list->name);
|
||||||
return NULL;
|
return NULL;
|
||||||
} else {
|
} else {
|
||||||
return opts;
|
return opts;
|
||||||
@ -783,9 +785,12 @@ int qemu_opts_set(QemuOptsList *list, const char *id,
|
|||||||
const char *name, const char *value)
|
const char *name, const char *value)
|
||||||
{
|
{
|
||||||
QemuOpts *opts;
|
QemuOpts *opts;
|
||||||
|
Error *local_err = NULL;
|
||||||
|
|
||||||
opts = qemu_opts_create(list, id, 1);
|
opts = qemu_opts_create(list, id, 1, &local_err);
|
||||||
if (opts == NULL) {
|
if (error_is_set(&local_err)) {
|
||||||
|
qerror_report_err(local_err);
|
||||||
|
error_free(local_err);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return qemu_opt_set(opts, name, value);
|
return qemu_opt_set(opts, name, value);
|
||||||
@ -883,6 +888,7 @@ static QemuOpts *opts_parse(QemuOptsList *list, const char *params,
|
|||||||
char value[1024], *id = NULL;
|
char value[1024], *id = NULL;
|
||||||
const char *p;
|
const char *p;
|
||||||
QemuOpts *opts;
|
QemuOpts *opts;
|
||||||
|
Error *local_err = NULL;
|
||||||
|
|
||||||
assert(!permit_abbrev || list->implied_opt_name);
|
assert(!permit_abbrev || list->implied_opt_name);
|
||||||
firstname = permit_abbrev ? list->implied_opt_name : NULL;
|
firstname = permit_abbrev ? list->implied_opt_name : NULL;
|
||||||
@ -898,13 +904,18 @@ static QemuOpts *opts_parse(QemuOptsList *list, const char *params,
|
|||||||
if (!id && !QTAILQ_EMPTY(&list->head)) {
|
if (!id && !QTAILQ_EMPTY(&list->head)) {
|
||||||
opts = qemu_opts_find(list, NULL);
|
opts = qemu_opts_find(list, NULL);
|
||||||
} else {
|
} else {
|
||||||
opts = qemu_opts_create(list, id, 0);
|
opts = qemu_opts_create(list, id, 0, &local_err);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
opts = qemu_opts_create(list, id, 1);
|
opts = qemu_opts_create(list, id, 1, &local_err);
|
||||||
}
|
}
|
||||||
if (opts == NULL)
|
if (opts == NULL) {
|
||||||
|
if (error_is_set(&local_err)) {
|
||||||
|
qerror_report_err(local_err);
|
||||||
|
error_free(local_err);
|
||||||
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
if (opts_do_parse(opts, params, firstname, defaults) != 0) {
|
if (opts_do_parse(opts, params, firstname, defaults) != 0) {
|
||||||
qemu_opts_del(opts);
|
qemu_opts_del(opts);
|
||||||
@ -975,11 +986,17 @@ static void qemu_opts_from_qdict_1(const char *key, QObject *obj, void *opaque)
|
|||||||
QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict)
|
QemuOpts *qemu_opts_from_qdict(QemuOptsList *list, const QDict *qdict)
|
||||||
{
|
{
|
||||||
QemuOpts *opts;
|
QemuOpts *opts;
|
||||||
|
Error *local_err = NULL;
|
||||||
|
|
||||||
opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1);
|
opts = qemu_opts_create(list, qdict_get_try_str(qdict, "id"), 1,
|
||||||
if (opts == NULL)
|
&local_err);
|
||||||
|
if (error_is_set(&local_err)) {
|
||||||
|
qerror_report_err(local_err);
|
||||||
|
error_free(local_err);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(opts != NULL);
|
||||||
qdict_iter(qdict, qemu_opts_from_qdict_1, opts);
|
qdict_iter(qdict, qemu_opts_from_qdict_1, opts);
|
||||||
return opts;
|
return opts;
|
||||||
}
|
}
|
||||||
|
@ -28,6 +28,7 @@
|
|||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include "qemu-queue.h"
|
#include "qemu-queue.h"
|
||||||
|
#include "error.h"
|
||||||
#include "qdict.h"
|
#include "qdict.h"
|
||||||
|
|
||||||
enum QEMUOptionParType {
|
enum QEMUOptionParType {
|
||||||
@ -116,7 +117,8 @@ int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque,
|
|||||||
int abort_on_failure);
|
int abort_on_failure);
|
||||||
|
|
||||||
QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id);
|
QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id);
|
||||||
QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, int fail_if_exists);
|
QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id,
|
||||||
|
int fail_if_exists, Error **errp);
|
||||||
void qemu_opts_reset(QemuOptsList *list);
|
void qemu_opts_reset(QemuOptsList *list);
|
||||||
void qemu_opts_loc_restore(QemuOpts *opts);
|
void qemu_opts_loc_restore(QemuOpts *opts);
|
||||||
int qemu_opts_set(QemuOptsList *list, const char *id,
|
int qemu_opts_set(QemuOptsList *list, const char *id,
|
||||||
|
@ -461,7 +461,7 @@ int inet_listen(const char *str, char *ostr, int olen,
|
|||||||
char *optstr;
|
char *optstr;
|
||||||
int sock = -1;
|
int sock = -1;
|
||||||
|
|
||||||
opts = qemu_opts_create(&dummy_opts, NULL, 0);
|
opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
|
||||||
if (inet_parse(opts, str) == 0) {
|
if (inet_parse(opts, str) == 0) {
|
||||||
sock = inet_listen_opts(opts, port_offset, errp);
|
sock = inet_listen_opts(opts, port_offset, errp);
|
||||||
if (sock != -1 && ostr) {
|
if (sock != -1 && ostr) {
|
||||||
@ -490,7 +490,7 @@ int inet_connect(const char *str, bool block, Error **errp)
|
|||||||
QemuOpts *opts;
|
QemuOpts *opts;
|
||||||
int sock = -1;
|
int sock = -1;
|
||||||
|
|
||||||
opts = qemu_opts_create(&dummy_opts, NULL, 0);
|
opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
|
||||||
if (inet_parse(opts, str) == 0) {
|
if (inet_parse(opts, str) == 0) {
|
||||||
if (block) {
|
if (block) {
|
||||||
qemu_opt_set(opts, "block", "on");
|
qemu_opt_set(opts, "block", "on");
|
||||||
@ -589,7 +589,7 @@ int unix_listen(const char *str, char *ostr, int olen)
|
|||||||
char *path, *optstr;
|
char *path, *optstr;
|
||||||
int sock, len;
|
int sock, len;
|
||||||
|
|
||||||
opts = qemu_opts_create(&dummy_opts, NULL, 0);
|
opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
|
||||||
|
|
||||||
optstr = strchr(str, ',');
|
optstr = strchr(str, ',');
|
||||||
if (optstr) {
|
if (optstr) {
|
||||||
@ -617,7 +617,7 @@ int unix_connect(const char *path)
|
|||||||
QemuOpts *opts;
|
QemuOpts *opts;
|
||||||
int sock;
|
int sock;
|
||||||
|
|
||||||
opts = qemu_opts_create(&dummy_opts, NULL, 0);
|
opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
|
||||||
qemu_opt_set(opts, "path", path);
|
qemu_opt_set(opts, "path", path);
|
||||||
sock = unix_connect_opts(opts);
|
sock = unix_connect_opts(opts);
|
||||||
qemu_opts_del(opts);
|
qemu_opts_del(opts);
|
||||||
|
22
vl.c
22
vl.c
@ -1786,7 +1786,7 @@ static int balloon_parse(const char *arg)
|
|||||||
return -1;
|
return -1;
|
||||||
} else {
|
} else {
|
||||||
/* create empty opts */
|
/* create empty opts */
|
||||||
opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
|
opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, NULL);
|
||||||
}
|
}
|
||||||
qemu_opt_set(opts, "driver", "virtio-balloon");
|
qemu_opt_set(opts, "driver", "virtio-balloon");
|
||||||
return 0;
|
return 0;
|
||||||
@ -1921,7 +1921,7 @@ static void monitor_parse(const char *optarg, const char *mode)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
|
opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
|
||||||
if (!opts) {
|
if (!opts) {
|
||||||
fprintf(stderr, "duplicate chardev: %s\n", label);
|
fprintf(stderr, "duplicate chardev: %s\n", label);
|
||||||
exit(1);
|
exit(1);
|
||||||
@ -2035,14 +2035,14 @@ static int virtcon_parse(const char *devname)
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
bus_opts = qemu_opts_create(device, NULL, 0);
|
bus_opts = qemu_opts_create(device, NULL, 0, NULL);
|
||||||
if (arch_type == QEMU_ARCH_S390X) {
|
if (arch_type == QEMU_ARCH_S390X) {
|
||||||
qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
|
qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
|
||||||
} else {
|
} else {
|
||||||
qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
|
qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
|
||||||
}
|
}
|
||||||
|
|
||||||
dev_opts = qemu_opts_create(device, NULL, 0);
|
dev_opts = qemu_opts_create(device, NULL, 0, NULL);
|
||||||
qemu_opt_set(dev_opts, "driver", "virtconsole");
|
qemu_opt_set(dev_opts, "driver", "virtconsole");
|
||||||
|
|
||||||
snprintf(label, sizeof(label), "virtcon%d", index);
|
snprintf(label, sizeof(label), "virtcon%d", index);
|
||||||
@ -2065,7 +2065,7 @@ static int debugcon_parse(const char *devname)
|
|||||||
if (!qemu_chr_new("debugcon", devname, NULL)) {
|
if (!qemu_chr_new("debugcon", devname, NULL)) {
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
|
opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
|
||||||
if (!opts) {
|
if (!opts) {
|
||||||
fprintf(stderr, "qemu: already have a debugcon device\n");
|
fprintf(stderr, "qemu: already have a debugcon device\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
@ -2813,7 +2813,8 @@ int main(int argc, char **argv, char **envp)
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
|
fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
|
||||||
qemu_opt_get(opts, "mount_tag"), 1);
|
qemu_opt_get(opts, "mount_tag"),
|
||||||
|
1, NULL);
|
||||||
if (!fsdev) {
|
if (!fsdev) {
|
||||||
fprintf(stderr, "duplicate fsdev id: %s\n",
|
fprintf(stderr, "duplicate fsdev id: %s\n",
|
||||||
qemu_opt_get(opts, "mount_tag"));
|
qemu_opt_get(opts, "mount_tag"));
|
||||||
@ -2845,7 +2846,8 @@ int main(int argc, char **argv, char **envp)
|
|||||||
|
|
||||||
qemu_opt_set_bool(fsdev, "readonly",
|
qemu_opt_set_bool(fsdev, "readonly",
|
||||||
qemu_opt_get_bool(opts, "readonly", 0));
|
qemu_opt_get_bool(opts, "readonly", 0));
|
||||||
device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
|
device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
|
||||||
|
NULL);
|
||||||
qemu_opt_set(device, "driver", "virtio-9p-pci");
|
qemu_opt_set(device, "driver", "virtio-9p-pci");
|
||||||
qemu_opt_set(device, "fsdev",
|
qemu_opt_set(device, "fsdev",
|
||||||
qemu_opt_get(opts, "mount_tag"));
|
qemu_opt_get(opts, "mount_tag"));
|
||||||
@ -2857,14 +2859,16 @@ int main(int argc, char **argv, char **envp)
|
|||||||
QemuOpts *fsdev;
|
QemuOpts *fsdev;
|
||||||
QemuOpts *device;
|
QemuOpts *device;
|
||||||
|
|
||||||
fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth", 1);
|
fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
|
||||||
|
1, NULL);
|
||||||
if (!fsdev) {
|
if (!fsdev) {
|
||||||
fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
|
fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
qemu_opt_set(fsdev, "fsdriver", "synth");
|
qemu_opt_set(fsdev, "fsdriver", "synth");
|
||||||
|
|
||||||
device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
|
device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
|
||||||
|
NULL);
|
||||||
qemu_opt_set(device, "driver", "virtio-9p-pci");
|
qemu_opt_set(device, "driver", "virtio-9p-pci");
|
||||||
qemu_opt_set(device, "fsdev", "v_synth");
|
qemu_opt_set(device, "fsdev", "v_synth");
|
||||||
qemu_opt_set(device, "mount_tag", "v_synth");
|
qemu_opt_set(device, "mount_tag", "v_synth");
|
||||||
|
Loading…
Reference in New Issue
Block a user