Patchwork [6/9] qdev: make devices created with device_add nullable so they can be deleted

login
register
mail settings
Submitter Anthony Liguori
Date Aug. 26, 2012, 3:51 p.m.
Message ID <1345996298-4892-7-git-send-email-aliguori@us.ibm.com>
Download mbox | patch
Permalink /patch/180100/
State New
Headers show

Comments

Anthony Liguori - Aug. 26, 2012, 3:51 p.m.
A management tool can destroy these devices by writing an empty string into
the child link property.

Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
---
 hw/qdev-monitor.c |   15 ++++++++++-----
 1 files changed, 10 insertions(+), 5 deletions(-)

Patch

diff --git a/hw/qdev-monitor.c b/hw/qdev-monitor.c
index 018b386..3f08575 100644
--- a/hw/qdev-monitor.c
+++ b/hw/qdev-monitor.c
@@ -469,15 +469,20 @@  DeviceState *qdev_device_add(QemuOpts *opts)
         return NULL;
     }
     if (qdev->id) {
-        object_property_add_child(qdev_get_peripheral(), qdev->id,
-                                  OBJECT(qdev), NULL);
+        object_property_add_nullable_child(qdev_get_peripheral(), qdev->id,
+                                           OBJECT(qdev), NULL);
     } else {
         static int anon_count;
         gchar *name = g_strdup_printf("device[%d]", anon_count++);
-        object_property_add_child(qdev_get_peripheral_anon(), name,
-                                  OBJECT(qdev), NULL);
+        object_property_add_nullable_child(qdev_get_peripheral_anon(), name,
+                                           OBJECT(qdev), NULL);
         g_free(name);
-    }        
+    }
+
+    /* Drop the allocation reference -- the container link will ensure the
+       object stays alive. */
+    object_unref(OBJECT(qdev));
+
     if (qdev_init(qdev) < 0) {
         qerror_report(QERR_DEVICE_INIT_FAILED, driver);
         return NULL;