chardev: Fix yank with the chardev-change case

When changing from chardev-socket (which supports yank) to
chardev-socket again, it fails, because the new chardev attempts
to register a new yank instance. This in turn fails, as there
still is the yank instance from the current chardev. Also,
the old chardev shouldn't unregister the yank instance when it
is freed.

To fix this, now the new chardev only registers a yank instance if
the current chardev doesn't support yank and thus hasn't registered
one already. Also, when the old chardev is freed, it now only
unregisters the yank instance if the new chardev doesn't need it.

If the initialization of the new chardev fails, it still has
chr->handover_yank_instance set and won't unregister the yank
instance when it is freed.

s->registered_yank is always true here, as chardev-change only works
on user-visible chardevs and those are guraranteed to register a
yank instance as they are initialized via
chardev_new()
 qemu_char_open()
  cc->open() (qmp_chardev_open_socket()).

Signed-off-by: Lukas Straub <lukasstraub2@web.de>
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Tested-by: Li Zhang <li.zhang@cloud.ionos.com>
Message-Id: <9637888d7591d2971975188478bb707299a1dc04.1617127849.git.lukasstraub2@web.de>
This commit is contained in:
Lukas Straub 2021-03-30 20:13:31 +02:00 committed by Marc-André Lureau
parent f36b0efd7f
commit feb774ca3f
3 changed files with 48 additions and 10 deletions

View file

@ -1126,7 +1126,13 @@ static void char_socket_finalize(Object *obj)
}
g_free(s->tls_authz);
if (s->registered_yank) {
yank_unregister_instance(CHARDEV_YANK_INSTANCE(chr->label));
/*
* In the chardev-change special-case, we shouldn't unregister the yank
* instance, as it still may be needed.
*/
if (!chr->handover_yank_instance) {
yank_unregister_instance(CHARDEV_YANK_INSTANCE(chr->label));
}
}
qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
@ -1424,8 +1430,14 @@ static void qmp_chardev_open_socket(Chardev *chr,
qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_FD_PASS);
}
if (!yank_register_instance(CHARDEV_YANK_INSTANCE(chr->label), errp)) {
return;
/*
* In the chardev-change special-case, we shouldn't register a new yank
* instance, as there already may be one.
*/
if (!chr->handover_yank_instance) {
if (!yank_register_instance(CHARDEV_YANK_INSTANCE(chr->label), errp)) {
return;
}
}
s->registered_yank = true;
@ -1567,6 +1579,8 @@ static void char_socket_class_init(ObjectClass *oc, void *data)
{
ChardevClass *cc = CHARDEV_CLASS(oc);
cc->supports_yank = true;
cc->parse = qemu_chr_parse_socket;
cc->open = qmp_chardev_open_socket;
cc->chr_wait_connected = tcp_chr_wait_connected;