Skip to content

Commit 6952026

Browse files
Markus Armbrusterbonzini
authored andcommitted
monitor: Tidy up find_device_state()
Commit 6287d82 "monitor: allow device_del to accept QOM paths" extended find_device_state() to accept QOM paths in addition to qdev IDs. This added a checked conversion to TYPE_DEVICE at the end, which duplicates the check done for the qdev ID case earlier, except it sets a *different* error: GenericError "ID is not a hotpluggable device" when passed a QOM path, and DeviceNotFound "Device 'ID' not found" when passed a qdev ID. Fortunately, the latter won't happen as long as we add only devices to /machine/peripheral/. Earlier, commit b6cc36a "qdev: device_del: Search for to be unplugged device in 'peripheral' container" rewrote the lookup by qdev ID to use QOM instead of qdev_find_recursive(), so it can handle buss-less devices. It does so by constructing an absolute QOM path. Works, but object_resolve_path_component() is easier. Switching to it also gets rid of the unclean duplication described above. While there, avoid converting to TYPE_DEVICE twice, first to check whether it's possible, and then for real. Signed-off-by: Markus Armbruster <[email protected]> Reviewed-by: Damien Hedde <[email protected]> Reviewed-by: Daniel P. Berrangé <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent b71803a commit 6952026

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

softmmu/qdev-monitor.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -836,16 +836,12 @@ void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp)
836836
static DeviceState *find_device_state(const char *id, Error **errp)
837837
{
838838
Object *obj;
839+
DeviceState *dev;
839840

840841
if (id[0] == '/') {
841842
obj = object_resolve_path(id, NULL);
842843
} else {
843-
char *root_path = object_get_canonical_path(qdev_get_peripheral());
844-
char *path = g_strdup_printf("%s/%s", root_path, id);
845-
846-
g_free(root_path);
847-
obj = object_resolve_path_type(path, TYPE_DEVICE, NULL);
848-
g_free(path);
844+
obj = object_resolve_path_component(qdev_get_peripheral(), id);
849845
}
850846

851847
if (!obj) {
@@ -854,12 +850,13 @@ static DeviceState *find_device_state(const char *id, Error **errp)
854850
return NULL;
855851
}
856852

857-
if (!object_dynamic_cast(obj, TYPE_DEVICE)) {
853+
dev = (DeviceState *)object_dynamic_cast(obj, TYPE_DEVICE);
854+
if (!dev) {
858855
error_setg(errp, "%s is not a hotpluggable device", id);
859856
return NULL;
860857
}
861858

862-
return DEVICE(obj);
859+
return dev;
863860
}
864861

865862
void qdev_unplug(DeviceState *dev, Error **errp)

0 commit comments

Comments
 (0)