diff mbox

[v3,04/17] qdev: Give qtree names precedence over user-assigned IDs

Message ID m3r5ks8okg.fsf@blackfin.pond.sub.org
State New
Headers show

Commit Message

Markus Armbruster May 31, 2010, 8:26 a.m. UTC
[cc: kraxel]

Avi Kivity <avi@redhat.com> writes:

> On 05/29/2010 11:01 AM, Markus Armbruster wrote:
>> Jan Kiszka<jan.kiszka@web.de>  writes:
>>
>>    
>>> From: Jan Kiszka<jan.kiszka@siemens.com>
>>>
>>> As the user may specify ambiguous device IDs, let's search for their
>>> official names first before considering the user-supplied identifiers.
>>>
>>> Signed-off-by: Jan Kiszka<jan.kiszka@siemens.com>
>>>      
>> The problem is letting the user specify ambiguous device IDs in the
>> first place!  That way is madness...
>>    
>
> Agreed, we're sowing the seeds for future problems.

On closer look, we have some protection against duplicate IDs, but it
got holes.

We don't assign IDs to default devices.

-device and device_add use the ID of a qemu_device_opts.  Which rejects
duplicate IDs.

pci_add nic -net use either the ID or option "name" of qemu_net_opts.
And there's our hole.  Reproducible with "-net user -net nic,id=foo
-device lsi,id=foo".

We better check for duplicates right where we create qdevs.  Gerd, what
do you think about the appended patch?

Comments

Gerd Hoffmann May 31, 2010, 9:59 a.m. UTC | #1
> pci_add nic -net use either the ID or option "name" of qemu_net_opts.
> And there's our hole.  Reproducible with "-net user -net nic,id=foo
> -device lsi,id=foo".

Oh.  Well.  Yes, better plug that.

> @@ -242,6 +243,10 @@ DeviceState *qdev_device_add(QemuOpts *opts)
>       qdev = qdev_create_from_info(bus, info);
>       id = qemu_opts_id(opts);
>       if (id) {
> +        if (qdev_find_recursive(main_system_bus, id)) {
> +            qerror_report(QERR_DUPLICATE_ID, id, "device");
> +            return NULL;
> +        }
>           qdev->id = id;
>       }
>       if (qemu_opt_foreach(opts, set_property, qdev, 1) != 0) {

Looks good..

Acked-by: Gerd Hoffmann <kraxel@redhat.com>

cheers,
   Gerd
Markus Armbruster May 31, 2010, 11:12 a.m. UTC | #2
Gerd Hoffmann <kraxel@redhat.com> writes:

>> pci_add nic -net use either the ID or option "name" of qemu_net_opts.
>> And there's our hole.  Reproducible with "-net user -net nic,id=foo
>> -device lsi,id=foo".
>
> Oh.  Well.  Yes, better plug that.
>
>> @@ -242,6 +243,10 @@ DeviceState *qdev_device_add(QemuOpts *opts)
>>       qdev = qdev_create_from_info(bus, info);
>>       id = qemu_opts_id(opts);
>>       if (id) {
>> +        if (qdev_find_recursive(main_system_bus, id)) {
>> +            qerror_report(QERR_DUPLICATE_ID, id, "device");
>> +            return NULL;
>> +        }
>>           qdev->id = id;
>>       }
>>       if (qemu_opt_foreach(opts, set_property, qdev, 1) != 0) {
>
> Looks good..
>
> Acked-by: Gerd Hoffmann <kraxel@redhat.com>

I'll repost it as a well-formed patch.  While there, I'll outlaw "/".

What about requiring IDs to start with a letter?  Just in case we ever
want to add alias names that must not clash with user-specified IDs.
diff mbox

Patch

diff --git a/hw/qdev.c b/hw/qdev.c
index b91bed1..beb4235 100644
--- a/hw/qdev.c
+++ b/hw/qdev.c
@@ -40,6 +40,7 @@  DeviceInfo *device_info_list;
 static BusState *qbus_find_recursive(BusState *bus, const char *name,
                                      const BusInfo *info);
 static BusState *qbus_find(const char *path);
+static DeviceState *qdev_find_recursive(BusState *bus, const char *id);
 
 /* Register a new device type.  */
 void qdev_register(DeviceInfo *info)
@@ -242,6 +243,10 @@  DeviceState *qdev_device_add(QemuOpts *opts)
     qdev = qdev_create_from_info(bus, info);
     id = qemu_opts_id(opts);
     if (id) {
+        if (qdev_find_recursive(main_system_bus, id)) {
+            qerror_report(QERR_DUPLICATE_ID, id, "device");
+            return NULL;
+        }
         qdev->id = id;
     }
     if (qemu_opt_foreach(opts, set_property, qdev, 1) != 0) {