qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Li Zhang <zhlcindy@gmail.com>
To: armbru@redhat.com, marcandre.lureau@gmail.com,
	pankaj.gupta@ionos.com, qemu-devel@nongnu.org
Cc: Li Zhang <li.zhang@ionos.com>
Subject: [PATCH v3 RESEND 2/2] chardev: refactor qmp_chardev_add and qmp_chardev_change
Date: Fri,  9 Jul 2021 18:08:43 +0200	[thread overview]
Message-ID: <20210709160843.339796-2-li.zhang@ionos.com> (raw)
In-Reply-To: <20210709160843.339796-1-li.zhang@ionos.com>

To improve the problematic source code in qmp_chardev_change
and some redundant source code, the functions qmp_chardev_add 
and qmp_chardev_change are refactored. It is mentioned in thread:
https://lists.nongnu.org/archive/html/qemu-devel/2021-04/msg03167.html

Signed-off-by: Li Zhang <li.zhang@ionos.com>
---
 chardev/char.c | 72 ++++++++++++++++++++++++++------------------------
 1 file changed, 37 insertions(+), 35 deletions(-)

diff --git a/chardev/char.c b/chardev/char.c
index d959eec522..cb6f287cd2 100644
--- a/chardev/char.c
+++ b/chardev/char.c
@@ -1028,23 +1028,10 @@ Chardev *qemu_chardev_new(const char *id, const char *typename,
     return chr;
 }
 
-ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
-                               Error **errp)
+static ChardevReturn *chardev_add(const char *id, Chardev *chr,
+                                  Error **errp)
 {
-    const ChardevClass *cc;
     ChardevReturn *ret;
-    Chardev *chr;
-
-    cc = char_get_class(ChardevBackendKind_str(backend->type), errp);
-    if (!cc) {
-        return NULL;
-    }
-
-    chr = chardev_new(id, object_class_get_name(OBJECT_CLASS(cc)),
-                      backend, NULL, false, errp);
-    if (!chr) {
-        return NULL;
-    }
 
     if (!object_property_try_add_child(get_chardevs_root(), id, OBJECT(chr),
                                        errp)) {
@@ -1062,6 +1049,26 @@ ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
     return ret;
 }
 
+ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
+                               Error **errp)
+{
+    const ChardevClass *cc;
+    Chardev *chr;
+
+    cc = char_get_class(ChardevBackendKind_str(backend->type), errp);
+    if (!cc) {
+        return NULL;
+    }
+
+    chr = chardev_new(id, object_class_get_name(OBJECT_CLASS(cc)),
+                      backend, NULL, false, errp);
+    if (!chr) {
+        return NULL;
+    }
+
+    return chardev_add(id, chr, errp);
+}
+
 ChardevReturn *qmp_chardev_change(const char *id, ChardevBackend *backend,
                                   Error **errp)
 {
@@ -1070,7 +1077,6 @@ ChardevReturn *qmp_chardev_change(const char *id, ChardevBackend *backend,
     Chardev *chr, *chr_new;
     bool closed_sent = false;
     bool handover_yank_instance;
-    ChardevReturn *ret;
 
     chr = qemu_chr_find(id);
     if (!chr) {
@@ -1089,11 +1095,22 @@ ChardevReturn *qmp_chardev_change(const char *id, ChardevBackend *backend,
         return NULL;
     }
 
+    cc = CHARDEV_GET_CLASS(chr);
+    cc_new = char_get_class(ChardevBackendKind_str(backend->type), errp);
+    if (!cc_new) {
+        return NULL;
+    }
+
     be = chr->be;
     if (!be) {
         /* easy case */
-        object_unparent(OBJECT(chr));
-        return qmp_chardev_add(id, backend, errp);
+        chr_new = chardev_new(id, object_class_get_name(OBJECT_CLASS(cc_new)),
+                              backend, NULL, false, errp);
+        if (!chr_new) {
+            return NULL;
+        }
+
+        goto out;
     }
 
     if (!be->chr_be_change) {
@@ -1101,12 +1118,6 @@ ChardevReturn *qmp_chardev_change(const char *id, ChardevBackend *backend,
         return NULL;
     }
 
-    cc = CHARDEV_GET_CLASS(chr);
-    cc_new = char_get_class(ChardevBackendKind_str(backend->type), errp);
-    if (!cc_new) {
-        return NULL;
-    }
-
     /*
      * The new chardev should not register a yank instance if the current
      * chardev has registered one already.
@@ -1147,18 +1158,9 @@ ChardevReturn *qmp_chardev_change(const char *id, ChardevBackend *backend,
      */
     chr->handover_yank_instance = handover_yank_instance;
 
+out:
     object_unparent(OBJECT(chr));
-    object_property_add_child(get_chardevs_root(), chr_new->label,
-                              OBJECT(chr_new));
-    object_unref(OBJECT(chr_new));
-
-    ret = g_new0(ChardevReturn, 1);
-    if (CHARDEV_IS_PTY(chr_new)) {
-        ret->pty = g_strdup(chr_new->filename + 4);
-        ret->has_pty = true;
-    }
-
-    return ret;
+    return chardev_add(id, chr_new, errp);
 }
 
 void qmp_chardev_remove(const char *id, Error **errp)
-- 
2.25.1



  reply	other threads:[~2021-07-09 16:24 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-09 16:08 [PATCH v3 RESEND 1/2] qmp: Support chardev-change Li Zhang
2021-07-09 16:08 ` Li Zhang [this message]
2021-07-14 14:56   ` [PATCH v3 RESEND 2/2] chardev: refactor qmp_chardev_add and qmp_chardev_change Li Zhang
2021-07-27 12:08   ` Li Zhang
2021-07-14 14:56 ` [PATCH v3 RESEND 1/2] qmp: Support chardev-change Li Zhang
2021-07-27 12:08 ` Li Zhang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210709160843.339796-2-li.zhang@ionos.com \
    --to=zhlcindy@gmail.com \
    --cc=armbru@redhat.com \
    --cc=li.zhang@ionos.com \
    --cc=marcandre.lureau@gmail.com \
    --cc=pankaj.gupta@ionos.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).