Skip to content

Commit 4a1d937

Browse files
Damien Heddekevmw
authored andcommitted
softmmu/qdev-monitor: add error handling in qdev_set_id
qdev_set_id() is mostly used when the user adds a device (using -device cli option or device_add qmp command). This commit adds an error parameter to handle the case where the given id is already taken. Also document the function and add a return value in order to be able to capture success/failure: the function now returns the id in case of success, or NULL in case of failure. The commit modifies the 2 calling places (qdev-monitor and xen-legacy-backend) to add the error object parameter. Note that the id is, right now, guaranteed to be unique because all ids came from the "device" QemuOptsList where the id is used as key. This addition is a preparation for a future commit which will relax the uniqueness. Signed-off-by: Damien Hedde <[email protected]> Signed-off-by: Kevin Wolf <[email protected]> Message-Id: <[email protected]> Reviewed-by: Michael S. Tsirkin <[email protected]> Tested-by: Peter Krempa <[email protected]> Signed-off-by: Kevin Wolf <[email protected]>
1 parent 163f384 commit 4a1d937

File tree

3 files changed

+55
-12
lines changed

3 files changed

+55
-12
lines changed

hw/xen/xen-legacy-backend.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,8 @@ static struct XenLegacyDevice *xen_be_get_xendev(const char *type, int dom,
276276
xendev = g_malloc0(ops->size);
277277
object_initialize(&xendev->qdev, ops->size, TYPE_XENBACKEND);
278278
OBJECT(xendev)->free = g_free;
279-
qdev_set_id(DEVICE(xendev), g_strdup_printf("xen-%s-%d", type, dev));
279+
qdev_set_id(DEVICE(xendev), g_strdup_printf("xen-%s-%d", type, dev),
280+
&error_fatal);
280281
qdev_realize(DEVICE(xendev), xen_sysbus, &error_fatal);
281282
object_unref(OBJECT(xendev));
282283

include/monitor/qdev.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,29 @@ void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp);
99

1010
int qdev_device_help(QemuOpts *opts);
1111
DeviceState *qdev_device_add(QemuOpts *opts, Error **errp);
12-
void qdev_set_id(DeviceState *dev, char *id);
12+
13+
/**
14+
* qdev_set_id: parent the device and set its id if provided.
15+
* @dev: device to handle
16+
* @id: id to be given to the device, or NULL.
17+
*
18+
* Returns: the id of the device in case of success; otherwise NULL.
19+
*
20+
* @dev must be unrealized, unparented and must not have an id.
21+
*
22+
* If @id is non-NULL, this function tries to setup @dev qom path as
23+
* "/peripheral/id". If @id is already taken, it fails. If it succeeds,
24+
* the id field of @dev is set to @id (@dev now owns the given @id
25+
* parameter).
26+
*
27+
* If @id is NULL, this function generates a unique name and setups @dev
28+
* qom path as "/peripheral-anon/name". This name is not set as the id
29+
* of @dev.
30+
*
31+
* Upon success, it returns the id/name (generated or provided). The
32+
* returned string is owned by the corresponding child property and must
33+
* not be freed by the caller.
34+
*/
35+
const char *qdev_set_id(DeviceState *dev, char *id, Error **errp);
1336

1437
#endif

softmmu/qdev-monitor.c

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -593,22 +593,35 @@ static BusState *qbus_find(const char *path, Error **errp)
593593
}
594594

595595
/* Takes ownership of @id, will be freed when deleting the device */
596-
void qdev_set_id(DeviceState *dev, char *id)
596+
const char *qdev_set_id(DeviceState *dev, char *id, Error **errp)
597597
{
598-
if (id) {
599-
dev->id = id;
600-
}
598+
ObjectProperty *prop;
601599

602-
if (dev->id) {
603-
object_property_add_child(qdev_get_peripheral(), dev->id,
604-
OBJECT(dev));
600+
assert(!dev->id && !dev->realized);
601+
602+
/*
603+
* object_property_[try_]add_child() below will assert the device
604+
* has no parent
605+
*/
606+
if (id) {
607+
prop = object_property_try_add_child(qdev_get_peripheral(), id,
608+
OBJECT(dev), NULL);
609+
if (prop) {
610+
dev->id = id;
611+
} else {
612+
g_free(id);
613+
error_setg(errp, "Duplicate device ID '%s'", id);
614+
return NULL;
615+
}
605616
} else {
606617
static int anon_count;
607618
gchar *name = g_strdup_printf("device[%d]", anon_count++);
608-
object_property_add_child(qdev_get_peripheral_anon(), name,
609-
OBJECT(dev));
619+
prop = object_property_add_child(qdev_get_peripheral_anon(), name,
620+
OBJECT(dev));
610621
g_free(name);
611622
}
623+
624+
return prop->name;
612625
}
613626

614627
DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
@@ -691,7 +704,13 @@ DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
691704
}
692705
}
693706

694-
qdev_set_id(dev, g_strdup(qemu_opts_id(opts)));
707+
/*
708+
* set dev's parent and register its id.
709+
* If it fails it means the id is already taken.
710+
*/
711+
if (!qdev_set_id(dev, g_strdup(qemu_opts_id(opts)), errp)) {
712+
goto err_del_dev;
713+
}
695714

696715
/* set properties */
697716
if (qemu_opt_foreach(opts, set_property, dev, errp)) {

0 commit comments

Comments
 (0)