diff --git a/chardev/char-fe.c b/chardev/char-fe.c index 7054863208..de5ba3c119 100644 --- a/chardev/char-fe.c +++ b/chardev/char-fe.c @@ -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; diff --git a/include/chardev/char-fe.h b/include/chardev/char-fe.h index 9f38060c7e..588abed606 100644 --- a/include/chardev/char-fe.h +++ b/include/chardev/char-fe.h @@ -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