Patchwork [4/6] chardev: implement realize

login
register
mail settings
Submitter Anthony Liguori
Date Oct. 15, 2012, 7:34 p.m.
Message ID <1350329657-18665-5-git-send-email-aliguori@us.ibm.com>
Download mbox | patch
Permalink /patch/191654/
State New
Headers show

Comments

Anthony Liguori - Oct. 15, 2012, 7:34 p.m.
This can be used to initialize an object independently of setting properties.
This makes the 'open' method legacy.

Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
---
 qemu-char.c |   48 +++++++++++++++++++++++++++++++++++++++++++++---
 qemu-char.h |    3 +++
 2 files changed, 48 insertions(+), 3 deletions(-)

Patch

diff --git a/qemu-char.c b/qemu-char.c
index 866c560..e9a81c7 100644
--- a/qemu-char.c
+++ b/qemu-char.c
@@ -2981,9 +2981,13 @@  CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
     chr = CHARDEV(object_new(class_name));
 
     cdc = CHARDEV_GET_CLASS(chr);
-    g_assert(cdc->open != NULL);
-
-    cdc->open(chr, opts, &err);
+    if (cdc->open) {
+        cdc->open(chr, opts, &err);
+    } else {
+        if (!err) {
+            object_property_set_bool(OBJECT(chr), true, "realized", &err);
+        }
+    }
 
     g_free(class_name);
 
@@ -3107,11 +3111,49 @@  CharDriverState *qemu_char_get_next_serial(void)
     return serial_hds[next_serial++];
 }
 
+static bool chardev_get_realized(Object *obj, Error **errp)
+{
+    CharDriverState *chr = CHARDEV(obj);
+
+    return chr->realized;
+}
+
+static void chardev_set_realized(Object *obj, bool value, Error **errp)
+{
+    CharDriverState *chr = CHARDEV(obj);
+    CharDriverClass *cdc = CHARDEV_GET_CLASS(chr);
+
+    if (chr->realized == value) {
+        return;
+    }
+
+    if (!value && chr->realized) {
+        error_set(errp, QERR_PERMISSION_DENIED);
+    } else {
+        Error *local_err = NULL;
+
+        g_assert(cdc->realize);
+        cdc->realize(chr, &local_err);
+        if (local_err) {
+            error_propagate(errp, local_err);
+        } else {
+            chr->realized = true;
+        }
+    }
+}
+
+static void chardev_initfn(Object *obj)
+{
+    object_property_add_bool(obj, "realized", chardev_get_realized,
+                             chardev_set_realized, NULL);
+}
+
 static const TypeInfo chardev_info = {
     .name = TYPE_CHARDEV,
     .parent = TYPE_OBJECT,
     .instance_size = sizeof(CharDriverState),
     .class_size = sizeof(CharDriverClass),
+    .instance_init = chardev_initfn,
 };
 
 static void register_types(void)
diff --git a/qemu-char.h b/qemu-char.h
index df7957d..be2a410 100644
--- a/qemu-char.h
+++ b/qemu-char.h
@@ -66,7 +66,9 @@  struct CharDriverClass
 {
     ObjectClass parent_class;
 
+    /* Objects should implement only one of these two methods */
     void (*open)(struct CharDriverState *chr, QemuOpts *opts, Error **errp);
+    void (*realize)(struct CharDriverState *chr, QemuOpts *opts, Error **errp);
 };
 
 struct CharDriverState {
@@ -94,6 +96,7 @@  struct CharDriverState {
     int opened;
     int avail_connections;
     QTAILQ_ENTRY(CharDriverState) next;
+    bool realized;
 };
 
 /**