char: forbid direct chardevice access for hotswap devices
authorAnton Nefedov <anton.nefedov@virtuozzo.com>
Thu, 6 Jul 2017 12:08:51 +0000 (15:08 +0300)
committerPaolo Bonzini <pbonzini@redhat.com>
Fri, 14 Jul 2017 09:04:33 +0000 (11:04 +0200)
qemu_chr_fe_get_driver() is unsafe, frontends with hotswap support
should not access CharDriver ptr directly as CharDriver might change.

Signed-off-by: Anton Nefedov <anton.nefedov@virtuozzo.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id: <1499342940-56739-5-git-send-email-anton.nefedov@virtuozzo.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
chardev/char-fe.c
include/chardev/char-fe.h

index 705486320826ff937eb41b5d2fe458570ac7f290..de5ba3c119a5a2fc8499472ba31909f765b1488f 100644 (file)
@@ -179,9 +179,16 @@ void qemu_chr_fe_printf(CharBackend *be, const char *fmt, ...)
 
 Chardev *qemu_chr_fe_get_driver(CharBackend *be)
 {
+    /* this is unsafe for the users that support chardev hotswap */
+    assert(be->chr_be_change == NULL);
     return be->chr;
 }
 
+bool qemu_chr_fe_backend_connected(CharBackend *be)
+{
+    return !!be->chr;
+}
+
 bool qemu_chr_fe_init(CharBackend *b, Chardev *s, Error **errp)
 {
     int tag = 0;
index 9f38060c7ec37a46fc96a38206c250be802444f0..588abed6062e7c671b508326705044ca1c5b01b6 100644 (file)
@@ -46,9 +46,19 @@ void qemu_chr_fe_deinit(CharBackend *b, bool del);
  *
  * Returns the driver associated with a CharBackend or NULL if no
  * associated Chardev.
+ * Note: avoid this function as the driver should never be accessed directly,
+ *       especially by the frontends that support chardevice hotswap.
+ *       Consider qemu_chr_fe_backend_connected() to check for driver existence
  */
 Chardev *qemu_chr_fe_get_driver(CharBackend *be);
 
+/**
+ * @qemu_chr_fe_backend_connected:
+ *
+ * Returns true if there is a chardevice associated with @be.
+ */
+bool qemu_chr_fe_backend_connected(CharBackend *be);
+
 /**
  * @qemu_chr_fe_set_handlers:
  * @b: a CharBackend