From patchwork Fri Mar 26 21:44:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Lukas Straub X-Patchwork-Id: 12167683 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.5 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7865BC433C1 for ; Fri, 26 Mar 2021 21:45:39 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 06C27619F9 for ; Fri, 26 Mar 2021 21:45:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 06C27619F9 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=web.de Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:46692 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lPuGv-0006Hz-Sw for qemu-devel@archiver.kernel.org; Fri, 26 Mar 2021 17:45:37 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53576) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lPuFb-00053v-W8 for qemu-devel@nongnu.org; Fri, 26 Mar 2021 17:44:16 -0400 Received: from mout.web.de ([212.227.17.12]:44379) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lPuFa-0003EF-9G for qemu-devel@nongnu.org; Fri, 26 Mar 2021 17:44:15 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=dbaedf251592; t=1616795049; bh=44/MKfQdDpVZKOupfb/xPXiDE9fo97IkNkPIR9rO8FU=; h=X-UI-Sender-Class:Date:From:To:Cc:Subject:In-Reply-To:References; b=YW6VvozJalx2YyrnZJtvJraelHS08I24yBDcqKY02FQSFMJ1t/DRSDISvRjtzImOD 6XW6OSqFDkC9WoPewSdRWJk0QY2XqZ0vUYW/xUD8S4BFSnRHcp9fCU9Vf464huHF+Y 4LYIjiQIIHmLLlaJTKAxnoOWiggf6nwyoVrIMlxs= X-UI-Sender-Class: c548c8c5-30a9-4db5-a2e7-cb6cb037b8f9 Received: from gecko.fritz.box ([89.247.255.242]) by smtp.web.de (mrweb105 [213.165.67.124]) with ESMTPSA (Nemesis) id 1MZB4F-1lCvhQ1EY5-00UwLp; Fri, 26 Mar 2021 22:44:09 +0100 Date: Fri, 26 Mar 2021 22:44:07 +0100 From: Lukas Straub To: qemu-devel Subject: [PATCH v5 1/4] chardev/char.c: Move object_property_try_add_child out of chardev_new Message-ID: In-Reply-To: References: MIME-Version: 1.0 X-Provags-ID: V03:K1:B9Vh6OymGbrFmJJ/6qoVRtfGTW/QQIlZr6O+Scgt12s5C5sBX4v T8phI0iuSRjgzy1PKdr8IAJy2Us6bDvu5m1twWDB9h/tcdvhQwW9D9LIFBHjOUxuMdiGJu9 KFSyNlWM4ZyxTxmPc/HXgF4kQKIZkaMabSHThOgZxP1yLezbtVC+878bA7y+nLLV6vT0Xtc ki6Vf+K4sXq3VN7xRoDeg== X-UI-Out-Filterresults: notjunk:1;V03:K0:nW8I4A7RA7w=:keaGSgvvEzyZJACTMu9hCG gA2yA7U8yWdSWQiGo268Pydq5eLB1gxVey8U/0p2VPvIZ1kNRqJcxM2u0Zr+48Ew2wEND92Ol lKpihGxyJCAdYlObha8VDp7C/VArjpLlypUew+JxF/23CZoya+tMusQoKYLPqo/xUvWur0EqI kNlxj4uLiR0rVkLa3KaympXkZxWdZuHZL8QKNutu3kNRLQlxyn3Fqh99cMPM35/giQwsmsalk shhngM+C4Cabk4u/QBQPpGM4cbJO4hbylikzqEyJQHglJTX1YdAPoZvLdKTybPUZKEw58QsHn pyAZxDzP3VPpFQmeI0TfMF+AAwlW4V/PQwIRwDMMpXoKiDl5hNyVahmTdOnTVfa1iwOU1n3hm j6B3VZl+nt4ey7+tfFyWP6BbRQu77HGTdCKeS6/fc4Vs5kvHeifB9AFOEVtXXpBZ9DE+dDmI6 bBX1e49Kh5TdIr2N68PsvykB/V0mv2dRQ9HvtpijBZpFzm/FKsIgvEslv5hihqPfwF3L3gMGw 1M6vwaKRj65uvqOaVl1kCE+3Ma/vDg6i5jQyDEKS13Co20/CciOb/9yxr+LRb6jnycOub2c+l M8HhmdjYFVF9xPcrJnOpjDoeGUe2yIjdgSARdnXWYAlqtsPLvPmP3qLbrdw5SNJCYsGMXCH+A +Tlfq3Kt5cVWf8YN1F73WROFdQAYjXsVuSLr/o2yzW+IqRMmOGl+rDNLGGHaUE12cBNTzHX2b SYl/itY5ByumMKPn/Ok030NIEEIhHAR+FOvWWaT952ITIpIP7fpxEafdO5b4y82/HFs3vmcET wAuPsWMOffv4GvUgMMGKhFO8bDgKS0bmMj/1OF9NRmD0+wwrpPwzQ/wLmwSnGIcZwDHBmprSe tBjKaitGxsclQkNlauarqP5eE41mn+G42FgSVrv8TogUw7h0eaFeTMudtLGa2vzNCqaH9OMDf ZA+WIi7N3i/R6t9nDRvlzZ1mnNlpBCR+M4UvYlu/k6i5wM70T6Y/N7pl3MCSwlyrxKrtHBoFI 6C5pXYukEQUr2d2uyqD+1PXY5DJshDQRq4aMA+U5QoS9i2JaSGitK9MaG/uF+B64qeWtqkCjk kirplosqaJuCXPXHit2r/oX4wIQRcSV4Gb7FE4brJmORxiqRoeGXVyR2VONg8V5S4KesXs/h3 i6Vr+NqURuWpXFnCN2qf3LtRVU3SWbusI6G9Mwb5na/Ao17N+Gmik3uI2zkw3NiG//oY8= Received-SPF: pass client-ip=212.227.17.12; envelope-from=lukasstraub2@web.de; helo=mout.web.de X-Spam_score_int: -24 X-Spam_score: -2.5 X-Spam_bar: -- X-Spam_report: (-2.5 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Laurent Vivier , Marc-Andre Lureau , Thomas Huth , Li Zhang , Paolo Bonzini Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Move object_property_try_add_child out of chardev_new into it's callers. This is a preparation for the next patches to fix yank with the chardev-change case. Signed-off-by: Lukas Straub Reviewed-by: Marc-André Lureau Tested-by: Li Zhang --- chardev/char.c | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) -- 2.30.2 diff --git a/chardev/char.c b/chardev/char.c index 140d6d9d36..48f321b3e1 100644 --- a/chardev/char.c +++ b/chardev/char.c @@ -975,7 +975,9 @@ static Chardev *chardev_new(const char *id, const char *typename, qemu_char_open(chr, backend, &be_opened, &local_err); if (local_err) { - goto end; + error_propagate(errp, local_err); + object_unref(obj); + return NULL; } if (!chr->filename) { @@ -985,22 +987,6 @@ static Chardev *chardev_new(const char *id, const char *typename, qemu_chr_be_event(chr, CHR_EVENT_OPENED); } - if (id) { - object_property_try_add_child(get_chardevs_root(), id, obj, - &local_err); - if (local_err) { - goto end; - } - object_unref(obj); - } - -end: - if (local_err) { - error_propagate(errp, local_err); - object_unref(obj); - return NULL; - } - return chr; } @@ -1009,6 +995,7 @@ Chardev *qemu_chardev_new(const char *id, const char *typename, GMainContext *gcontext, Error **errp) { + Chardev *chr; g_autofree char *genid = NULL; if (!id) { @@ -1016,7 +1003,19 @@ Chardev *qemu_chardev_new(const char *id, const char *typename, id = genid; } - return chardev_new(id, typename, backend, gcontext, errp); + chr = chardev_new(id, typename, backend, gcontext, errp); + if (!chr) { + return NULL; + } + + if (!object_property_try_add_child(get_chardevs_root(), id, OBJECT(chr), + errp)) { + object_unref(OBJECT(chr)); + return NULL; + } + object_unref(OBJECT(chr)); + + return chr; } ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend, @@ -1037,6 +1036,13 @@ ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend, return NULL; } + if (!object_property_try_add_child(get_chardevs_root(), id, OBJECT(chr), + errp)) { + object_unref(OBJECT(chr)); + return NULL; + } + object_unref(OBJECT(chr)); + ret = g_new0(ChardevReturn, 1); if (CHARDEV_IS_PTY(chr)) { ret->pty = g_strdup(chr->filename + 4); From patchwork Fri Mar 26 21:44:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Lukas Straub X-Patchwork-Id: 12167687 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.5 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3E02AC433DB for ; Fri, 26 Mar 2021 21:48:24 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id EA69A61A10 for ; Fri, 26 Mar 2021 21:48:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org EA69A61A10 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=web.de Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:52790 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lPuJa-0000WM-W1 for qemu-devel@archiver.kernel.org; Fri, 26 Mar 2021 17:48:23 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53616) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lPuFf-000586-4U for qemu-devel@nongnu.org; Fri, 26 Mar 2021 17:44:19 -0400 Received: from mout.web.de ([212.227.17.11]:36293) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lPuFd-0003Fl-BD for qemu-devel@nongnu.org; Fri, 26 Mar 2021 17:44:18 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=dbaedf251592; t=1616795053; bh=mMU7ttyWpWNIgXoGocoh++GYkCLuGe/gox6cW5iaGms=; h=X-UI-Sender-Class:Date:From:To:Cc:Subject:In-Reply-To:References; b=s/FvM3Hm6rOjcA148jCC4I4e/EQb7v6dY3+mksfh0ER01Rh/pCZBohleRcktKkAFX w0EUW0kW6eJ9pO9vOimPOFpCR9F6weU00yEVPQjhvSCiosrUIO0RVO+UX8ySLujjIo 1sSUsgxhyWJCsIseQnA1uY4JhhfQcg7mN82tjleI= X-UI-Sender-Class: c548c8c5-30a9-4db5-a2e7-cb6cb037b8f9 Received: from gecko.fritz.box ([89.247.255.242]) by smtp.web.de (mrweb101 [213.165.67.124]) with ESMTPSA (Nemesis) id 0MCZtW-1lYByQ3bNK-009PIM; Fri, 26 Mar 2021 22:44:12 +0100 Date: Fri, 26 Mar 2021 22:44:11 +0100 From: Lukas Straub To: qemu-devel Subject: [PATCH v5 2/4] chardev/char.c: Always pass id to chardev_new Message-ID: <3e669b6c160aa7278e37c4d95e0445574f96c7b7.1616794852.git.lukasstraub2@web.de> In-Reply-To: References: MIME-Version: 1.0 X-Provags-ID: V03:K1:H8CxK7YDPpNcQKNZwZ+Ig79mppbo7Cn66yN75MAKNvu2iK0H4BS KJ16XTWF9azgTSShj4VipbIOqzoGIcXNVBOThbh6wgXYAwOdHv+482SKtyd/wjdHULZY8qr EoeeLi3YvPUXy1dtp4IA8CrAd9sy+aU+hTwHN9T//dMCxC8gQJ+G603dV47muCmM4kT7YKZ lrH+N4FWg0orPu1BlYPKg== X-UI-Out-Filterresults: notjunk:1;V03:K0:M6h28mq319s=:DAvGLPfDSvlAZSgLimY+Vb lNMDDBHWsRodLsN6hJg1H2UA+FnS/fR2NpJdRhsjvqUK86SHhRI4QByLBz8wETZ7Q9t82nl/I Axcghi8lvXblkdjPzarrMM3eAMPZj39EfQ/y1Uy6f9w4vuIGzBJZMsbNo9XWHsKC7aPVLdm3S H6cFOK1QW5knMz81U7wtESM+4rIN+iu5zm/eS4XBTrnvlFJFx+HEHtKnVRwTLL+pyaYRBBaVN nEmPguxC9GVQhEuHiuktq0TwpbrA3yO6lt1IzHjs6WfcY0BxU1BO3q/Se+bJKFH+AP086BSjr wQVvJUyhwzTBmvMUz4asbhPKmOev01TpBBk3VvCWVw7WLSUbsJ1IlhR/HknVE8uGePNjJRYxc LflsthdzGHCzf+UTs/+qnmy/hj9g2g9yaOyinKC1mLyJOKikoBeaUf7454kG4J1/RdVsFTWH7 Lvqqimj2uxmAdwEp4D/zXZlQZNlikHIqxgYH4PrAtsrk/cVHwGK1WGruq4Us23ab8jXkMiMI/ PoxorXpw+hVSBBFMhhAI/zaXv++82UO7bKf7W9lBe9Bj7IXN21IhjO/McxkVUga3688covNhO q/p8l3SoU8T+RZXB1jj/VcP0Be400Fs74k3zCQyZF4b0MIXVnL5ajT6kBEJopE+qPmpjkM3rn sSVQrWjFPK9FFXx/P5hdAimb3ZJRuybpxdceIPiMtfdII+i7d+ZRUuSCG1NJ8U8rx9D8x8WHW 2QYFaicS//5iRkWG96bKo0Up2uJ6UGMB4CFT+7EmZ20ipcfMzHI6XEkqKreqXU3baAqj3onrv NKL0XLzph73BkeJ/sRsPzsDjfwzmZR01w8nzdfX+jYlp9/QTvrXHEsla5SfwFyIMkIsDyLajm WrfXnJHP4y6AnUPdvjBbCDTdFqcgmKf5ZzeAG58rF1aPMSbYlrQILXXm3v1Dbjw0usjMa3Xp8 rolAmsyt93aBLpa34tgiAGjkYECNxbDNsOfgFAYjsXpmSQDT/BxcTiSY/Ohrd6LkHdstMq+7G uQfoZZ6WJ28hCC+0AHkU/QlqY2rVdBJ8vAcZazp/1YquNdwtKiEAWI/9nTmVMg1T3rymrbFuC I3CA4sM/HwAzDY4bn+18iyW32bTShbkxqSz0ytzRL48sVAnhgLW9bUjViqIRh7OU7qlDDY1aE kgHcOpORkL9sGFsatHWD+01LDedEYW+1suHGvqHpQjjcoBy4W8De0OGZT9FvANOWxib/4= Received-SPF: pass client-ip=212.227.17.11; envelope-from=lukasstraub2@web.de; helo=mout.web.de X-Spam_score_int: -24 X-Spam_score: -2.5 X-Spam_bar: -- X-Spam_report: (-2.5 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Laurent Vivier , Marc-Andre Lureau , Thomas Huth , Li Zhang , Paolo Bonzini Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Always pass the id to chardev_new, since it is needed to register the yank instance for the chardev. Also, after checking that nothing calls chardev_new with id=NULL, assert() that id!=NULL. This fixes a crash when using chardev-change to change a chardev to chardev-socket, which attempts to register a yank instance. This in turn tries to dereference the NULL-pointer. Signed-off-by: Lukas Straub Reviewed-by: Marc-André Lureau Tested-by: Li Zhang --- chardev/char.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) -- 2.30.2 diff --git a/chardev/char.c b/chardev/char.c index 48f321b3e1..75993f903f 100644 --- a/chardev/char.c +++ b/chardev/char.c @@ -967,6 +967,7 @@ static Chardev *chardev_new(const char *id, const char *typename, bool be_opened = true; assert(g_str_has_prefix(typename, "chardev-")); + assert(id); obj = object_new(typename); chr = CHARDEV(obj); @@ -1095,12 +1096,11 @@ ChardevReturn *qmp_chardev_change(const char *id, ChardevBackend *backend, return NULL; } - chr_new = chardev_new(NULL, object_class_get_name(OBJECT_CLASS(cc)), + chr_new = chardev_new(id, object_class_get_name(OBJECT_CLASS(cc)), backend, chr->gcontext, errp); if (!chr_new) { return NULL; } - chr_new->label = g_strdup(id); if (chr->be_open && !chr_new->be_open) { qemu_chr_be_event(chr, CHR_EVENT_CLOSED); From patchwork Fri Mar 26 21:44:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Lukas Straub X-Patchwork-Id: 12167685 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.5 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id ED33AC433DB for ; Fri, 26 Mar 2021 21:46:22 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id A42F1619F9 for ; Fri, 26 Mar 2021 21:46:22 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A42F1619F9 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=web.de Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:47358 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lPuHd-0006ZJ-Fi for qemu-devel@archiver.kernel.org; Fri, 26 Mar 2021 17:46:21 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53638) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lPuFi-0005EA-UH for qemu-devel@nongnu.org; Fri, 26 Mar 2021 17:44:22 -0400 Received: from mout.web.de ([217.72.192.78]:53057) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lPuFg-0003HV-W5 for qemu-devel@nongnu.org; Fri, 26 Mar 2021 17:44:22 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=dbaedf251592; t=1616795056; bh=2jOYsuPsIukmBHFjsoLqXzpRRhJipIA8qem4dKpPm/8=; h=X-UI-Sender-Class:Date:From:To:Cc:Subject:In-Reply-To:References; b=E8nylxvXc/WC81Ly9shVUeIM9YRM4yIEzLj6Rv/wTMZrY5QyQZnlCnOuRZPN//OhY LQmRZDQnHLe//C6m3Glsea1rLDAvpeYR4BW9R2lYobyGvVfTPlWLZHAeqWW9zCwTR2 hbCWlKdnQWgkhN43s9FW2e/8wIulod5QJyiyO4M4= X-UI-Sender-Class: c548c8c5-30a9-4db5-a2e7-cb6cb037b8f9 Received: from gecko.fritz.box ([89.247.255.242]) by smtp.web.de (mrweb101 [213.165.67.124]) with ESMTPSA (Nemesis) id 0MEEa4-1lS8EK109I-00FRnG; Fri, 26 Mar 2021 22:44:16 +0100 Date: Fri, 26 Mar 2021 22:44:14 +0100 From: Lukas Straub To: qemu-devel Subject: [PATCH v5 3/4] chardev: Fix yank with the chardev-change case Message-ID: <9637888d7591d2971975188478bb707299a1dc04.1616794852.git.lukasstraub2@web.de> In-Reply-To: References: MIME-Version: 1.0 X-Provags-ID: V03:K1:6RQsfqgtWWueOcpOcYelLOtrnBJWd97yWIUnOQnAeotbkW09MVc YqcW62MgXzp8wMZg1nVsiV8xvpuBeTeClDU5erj8IyVVoyKxL8T2ZAIU9VJtvxk1SJmSVZW sRTJttUq4ZlZOH31rFnLzF24behpcJYteJkxq5TjBHlYb6oPzYvZQwggS5w5S/S5NU8uARZ M3jEtEI6gnmwxutJSxCgA== X-UI-Out-Filterresults: notjunk:1;V03:K0:zCfrauL0fT4=:3wBZm3HHEMThrdTIksEfsi Kt9M43gU/Sej4mMEsL/gqUMUyq9F3HjcOin2ttjkfAS0LpDthuR0Neh+10TYmI6aj0oLz2tLM 6/M50BpobmANMgPz5BVhnZMgHRi9KIaPl3wWnETtX4uDpNQHNUuJgxjj7O+cME+HaJxO4kIMt v+T6e0wNTTF8cKOmUylTIJ6wJPoU2FBrF8WoLVjZEoihQqoeFFLCrEHzdf6QJZQHSSI63dzfz rKN+37/rpoCTyw8QgOkBmD7m88mvfcMxVqrhPOtk+VZPCg1GWMn68Fmtu9bSfnnVEZWwPIf1a ZaZASCFJy9Dk9YQdoyMZLpIz7hzc/5A1Nh1NyoMEC8OYN47oOQgXKBnqMPdoSLs+p212udigr uu0r5GF9xUtUhSMx+Dc2l/51bFhJhuRgDh/uGk8mNzHhLKFHHVU0ujkKGYF+adVUGXX6Tiam9 Y38UpdoZlJKyeeU+65XOVlIQF57vTTOwjVJ8O3IF/jUYZAL8tj24y8PZbXnml8IhrfnOmQjy+ 1WjJDwJSIt/HcZMR737jPBTxR+zG7gI5gpYCz5EKs1uiiRgWN1/Dj8YuaLfRPswouY6NLflfX OCBfrGVD+jZMiZjh27TmCzaGCB1QO70E/G5S4JVmhoWSzXJJ7S3OOq7Fx61Gmv96vxziTHvM4 9pY86i4G5mYCDCiY7DvAeF43j1k55Uqt9IaDwz5cbaPBy23zB+ht8Lu0b7Fx9MS0952Z22vCn e+XPheU6KskU8LMzhfPJKM5whx3sMWg4XBV8u7uKGac+i4qsU8fizKoK0FmuumjnnwaN3RSw9 KoS3y6R3mawFUqmRsK5s3HpUUfuU5gf9Mk4abDB4Ik/d7H0ZuwW8J/ngtAzVBx8JOcE3ABYJ6 zZtKXmsucC1+ehyp/cURvBQfPKT+ree/4h5AMyPSzU/x/bqhHIiWWKgNdYywtykGAm0HdD5Wx 6k3IdxXrAgts/E672NH0ETAjPAKyhrtO02BH9W8HujtP757Qh4l0SGyue/qxbGnK4DmirjkfX rFvKl0lhzCm6qIP9OtopOUmv4Pgm8x+IR1wtdkeyLrJ/rLYLgSu0QCecisVuojhxw0nCuNJ+F 6C3YWBAYqR6mFcs+v1/DTv0hQc0qTfxy/0+4HveWBP9oFoSfklSF4kS0zJUwDjA13NJ8bPldo ya/lh+uHJhAU4SCQ7cZb9ozjG/oShtp7hn2461TA+kf2CGdorJ1y3LwktMthHKFWAIuY0= Received-SPF: pass client-ip=217.72.192.78; envelope-from=lukasstraub2@web.de; helo=mout.web.de X-Spam_score_int: -24 X-Spam_score: -2.5 X-Spam_bar: -- X-Spam_report: (-2.5 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Laurent Vivier , Marc-Andre Lureau , Thomas Huth , Li Zhang , Paolo Bonzini Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" 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 Reviewed-by: Marc-André Lureau Tested-by: Li Zhang --- chardev/char-socket.c | 20 +++++++++++++++++--- chardev/char.c | 35 ++++++++++++++++++++++++++++------- include/chardev/char.h | 3 +++ 3 files changed, 48 insertions(+), 10 deletions(-) -- 2.30.2 diff --git a/chardev/char-socket.c b/chardev/char-socket.c index 1d455ecca4..daa89fe5d1 100644 --- a/chardev/char-socket.c +++ b/chardev/char-socket.c @@ -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; diff --git a/chardev/char.c b/chardev/char.c index 75993f903f..398f09df19 100644 --- a/chardev/char.c +++ b/chardev/char.c @@ -39,6 +39,7 @@ #include "qemu/option.h" #include "qemu/id.h" #include "qemu/coroutine.h" +#include "qemu/yank.h" #include "chardev-internal.h" @@ -266,6 +267,7 @@ static void char_init(Object *obj) { Chardev *chr = CHARDEV(obj); + chr->handover_yank_instance = false; chr->logfd = -1; qemu_mutex_init(&chr->chr_write_lock); @@ -959,6 +961,7 @@ void qemu_chr_set_feature(Chardev *chr, static Chardev *chardev_new(const char *id, const char *typename, ChardevBackend *backend, GMainContext *gcontext, + bool handover_yank_instance, Error **errp) { Object *obj; @@ -971,6 +974,7 @@ static Chardev *chardev_new(const char *id, const char *typename, obj = object_new(typename); chr = CHARDEV(obj); + chr->handover_yank_instance = handover_yank_instance; chr->label = g_strdup(id); chr->gcontext = gcontext; @@ -1004,7 +1008,7 @@ Chardev *qemu_chardev_new(const char *id, const char *typename, id = genid; } - chr = chardev_new(id, typename, backend, gcontext, errp); + chr = chardev_new(id, typename, backend, gcontext, false, errp); if (!chr) { return NULL; } @@ -1032,7 +1036,7 @@ ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend, } chr = chardev_new(id, object_class_get_name(OBJECT_CLASS(cc)), - backend, NULL, errp); + backend, NULL, false, errp); if (!chr) { return NULL; } @@ -1057,9 +1061,10 @@ ChardevReturn *qmp_chardev_change(const char *id, ChardevBackend *backend, Error **errp) { CharBackend *be; - const ChardevClass *cc; + const ChardevClass *cc, *cc_new; Chardev *chr, *chr_new; bool closed_sent = false; + bool handover_yank_instance; ChardevReturn *ret; chr = qemu_chr_find(id); @@ -1091,13 +1096,20 @@ ChardevReturn *qmp_chardev_change(const char *id, ChardevBackend *backend, return NULL; } - cc = char_get_class(ChardevBackendKind_str(backend->type), errp); - if (!cc) { + cc = CHARDEV_GET_CLASS(chr); + cc_new = char_get_class(ChardevBackendKind_str(backend->type), errp); + if (!cc_new) { return NULL; } - chr_new = chardev_new(id, object_class_get_name(OBJECT_CLASS(cc)), - backend, chr->gcontext, errp); + /* + * The new chardev should not register a yank instance if the current + * chardev has registered one already. + */ + handover_yank_instance = cc->supports_yank && cc_new->supports_yank; + + chr_new = chardev_new(id, object_class_get_name(OBJECT_CLASS(cc_new)), + backend, chr->gcontext, handover_yank_instance, errp); if (!chr_new) { return NULL; } @@ -1121,6 +1133,15 @@ ChardevReturn *qmp_chardev_change(const char *id, ChardevBackend *backend, return NULL; } + /* change successfull, clean up */ + chr_new->handover_yank_instance = false; + + /* + * When the old chardev is freed, it should not unregister the yank + * instance if the new chardev needs it. + */ + chr->handover_yank_instance = handover_yank_instance; + object_unparent(OBJECT(chr)); object_property_add_child(get_chardevs_root(), chr_new->label, OBJECT(chr_new)); diff --git a/include/chardev/char.h b/include/chardev/char.h index 4181a2784a..7c0444f90d 100644 --- a/include/chardev/char.h +++ b/include/chardev/char.h @@ -65,6 +65,8 @@ struct Chardev { char *filename; int logfd; int be_open; + /* used to coordinate the chardev-change special-case: */ + bool handover_yank_instance; GSource *gsource; GMainContext *gcontext; DECLARE_BITMAP(features, QEMU_CHAR_FEATURE_LAST); @@ -251,6 +253,7 @@ struct ChardevClass { ObjectClass parent_class; bool internal; /* TODO: eventually use TYPE_USER_CREATABLE */ + bool supports_yank; void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp); void (*open)(Chardev *chr, ChardevBackend *backend, From patchwork Fri Mar 26 21:44:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Lukas Straub X-Patchwork-Id: 12167689 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.5 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 96705C433DB for ; Fri, 26 Mar 2021 21:50:25 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 0D4BE61A10 for ; Fri, 26 Mar 2021 21:50:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0D4BE61A10 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=web.de Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:57814 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lPuLW-0002du-2C for qemu-devel@archiver.kernel.org; Fri, 26 Mar 2021 17:50:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:53654) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lPuFl-0005JI-MQ for qemu-devel@nongnu.org; Fri, 26 Mar 2021 17:44:25 -0400 Received: from mout.web.de ([217.72.192.78]:59443) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lPuFj-0003Iy-La for qemu-devel@nongnu.org; Fri, 26 Mar 2021 17:44:25 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=dbaedf251592; t=1616795059; bh=PsCLTv9xKfipUVZE1wFEPMN9xQENy4PggJ+zi4m7VN4=; h=X-UI-Sender-Class:Date:From:To:Cc:Subject:In-Reply-To:References; b=QxVhD0ddESNwMvIl087HQWJrjFEnzt9AMvcpOcAmoruc0YWiz92kDrh4VnG16nZgb c+PoffBcDJkfGxmzTLUH4uWAnIV3qDFnn7xIa6Dl+auWraorj6AEvx1MFFCNkiKiev pZ8loJf5BDgRUE7gL3wn4RNAl5n7Kn97sPI+CBJw= X-UI-Sender-Class: c548c8c5-30a9-4db5-a2e7-cb6cb037b8f9 Received: from gecko.fritz.box ([89.247.255.242]) by smtp.web.de (mrweb105 [213.165.67.124]) with ESMTPSA (Nemesis) id 1M5QqN-1lP4ly1fpF-001MI6; Fri, 26 Mar 2021 22:44:19 +0100 Date: Fri, 26 Mar 2021 22:44:18 +0100 From: Lukas Straub To: qemu-devel Subject: [PATCH v5 4/4] tests: Add tests for yank with the chardev-change case Message-ID: <7c2f1ddcaa1c97462cb0b834d5aa7368283aa67d.1616794852.git.lukasstraub2@web.de> In-Reply-To: References: MIME-Version: 1.0 X-Provags-ID: V03:K1:A1oPse6oZo4izmAtO4LE1IkiBAdp6ySmld7CpUi91TLnGHVXh8B GqbOidKFckFI7xb3FhmN5yrh4B1lyIMw7omAoRtnk9bIOwHx48RbaQ7kYLlYfVN7z+hdnBP T5hJo54GvNUKzRYmE+/K/wqaPTZ4AO/TETHpiZCj6x4UYVD0xfZoOKNcp0D/TS52Sq5w7x3 /lFEeUi+NYxKAHb0jiC/g== X-UI-Out-Filterresults: notjunk:1;V03:K0:PMBjvxE2TCE=:KfgYjI4g4bQL5LKbUjvueD CitFisdj/4DCmeaK+vjIxhqv/e0XJ0zlkVt/6vC6xccPCZ/mhvPhI8bu0KHeAkiyLT3L+P3/I Fa6pKee2G1ON8olZEqrbdLmU0+ja7WrutKHxdptmFdQrJygp2Fz1oe+BdrP0Dhb/3wo1/C+pn YY9NbOSXDBL5i7kymLumT3lkVJrn6KU61gAZyNI49YKIiukP/QhAldy7LS7XSrdGNviXIgG1F PUvdiv7ncAb4Raz1TFDm+b4LXkZeE0FMBOn40sUZSeyg7w/XB5nq00HCc8SUG9n04hk1nqxwc WAXAvuXQc1ay0yp9ST+WKB4HigLUy5mDfDYRSXYrSCN3K3Atc40gSpyenMPGS4k+EHrON5jJu WzjEI1e6cge9up7srsprjrv7yCJYYJvkG+Y9S5HXzejKGCG3nCLnA9J+eoq8tVT2WIngCDYlO yehLRQsPtvAvT+A2ulPrSTCM99oH21jsJfOFlRL12BL22E+VZMVoEOFhyc93G4HedsuY8wjLY P3BslQBTxLMoolTtHuGQ+YPbydfRYBGTsJpD8vW/Zb0T43QHGVD7o5fL2ECyvVYaWiG8JAUjN McKtmEqJcHtCpbhKHF/drvstrNW9bGzletLcvNhaO313EPJjV0+VTB/dZWWqmJLfbtgc7MJSg xKmx/hxftgYM8vrb6IF1JHUlfAvN1jXkN740y58WhJJ3XwJPnnCkh1ieN0v9/WO/f7QKTnNl/ 8NC8sBYSU/4aAX31+mZTpewbaQylzR8Nsr7fp5O4/5Wa9D4AmSAV0K/RytkhTVoEm8hk8bqhq sTV3+ObITO7EJb3ZxoullG9/dGcpBW2vPc1Ip2eViCQx2Psu+9J54QRk9Yshq8d3ww97c6O/z ecRU4J/vQKa7452dMr/SCNHqQ8Lk9Qcoe74uYsYwRkPCXkqzp60HXWIfmaL93s3uyv4X64deG u59ND8tmpEVABXjUqkCWyg8bAFJ/cwjZ+7Gc/TWqVzeKsG0dLFZeZK+ETQMpxSbHj0fvSwl2d mubDNWF463A86HIu+dcRpka0SdAktH4NcItPAb36DtcWAw8zbGsfF5N2c8SYx0zw3lUpuWKc1 ngtY7mLWMACMW7r+CZGKZeVL02AhUvmsnfyGYQXrnfxC+2tBaXR4a6qgKc1jSxnAM5BDu2VXk aZveSGuoaH2AON+9lHN8ne4+8q8uhoQEjORT3WR/4LfeAcpmgRruJnF0k3o0lZZeL4nGHwM3E NuupVQggJVgLFvfJX Received-SPF: pass client-ip=217.72.192.78; envelope-from=lukasstraub2@web.de; helo=mout.web.de X-Spam_score_int: -24 X-Spam_score: -2.5 X-Spam_bar: -- X-Spam_report: (-2.5 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Laurent Vivier , Marc-Andre Lureau , Thomas Huth , Li Zhang , Paolo Bonzini Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Add tests for yank with the chardev-change case. Signed-off-by: Lukas Straub Reviewed-by: Marc-André Lureau Tested-by: Li Zhang --- MAINTAINERS | 1 + tests/unit/meson.build | 3 +- tests/unit/test-yank.c | 200 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 tests/unit/test-yank.c -- 2.30.2 diff --git a/MAINTAINERS b/MAINTAINERS index 77259c031d..accb683a55 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -2821,6 +2821,7 @@ M: Lukas Straub S: Odd fixes F: util/yank.c F: migration/yank_functions* +F: tests/unit/test-yank.c F: include/qemu/yank.h F: qapi/yank.json diff --git a/tests/unit/meson.build b/tests/unit/meson.build index 4bfe4627ba..b3bc2109da 100644 --- a/tests/unit/meson.build +++ b/tests/unit/meson.build @@ -123,7 +123,8 @@ if have_system 'test-util-sockets': ['socket-helpers.c'], 'test-base64': [], 'test-bufferiszero': [], - 'test-vmstate': [migration, io] + 'test-vmstate': [migration, io], + 'test-yank': ['socket-helpers.c', qom, io, chardev] } if 'CONFIG_INOTIFY1' in config_host tests += {'test-util-filemonitor': []} diff --git a/tests/unit/test-yank.c b/tests/unit/test-yank.c new file mode 100644 index 0000000000..1596a3b98e --- /dev/null +++ b/tests/unit/test-yank.c @@ -0,0 +1,200 @@ +/* + * Tests for QEMU yank feature + * + * Copyright (c) Lukas Straub + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + */ + +#include "qemu/osdep.h" +#include + +#include "qemu/config-file.h" +#include "qemu/module.h" +#include "qemu/option.h" +#include "chardev/char-fe.h" +#include "sysemu/sysemu.h" +#include "qapi/error.h" +#include "qapi/qapi-commands-char.h" +#include "qapi/qapi-types-char.h" +#include "qapi/qapi-commands-yank.h" +#include "qapi/qapi-types-yank.h" +#include "io/channel-socket.h" +#include "socket-helpers.h" + +typedef struct { + SocketAddress *addr; + bool old_yank; + bool new_yank; + bool fail; +} CharChangeTestConfig; + +static int chardev_change(void *opaque) +{ + return 0; +} + +static bool is_yank_instance_registered(void) +{ + YankInstanceList *list; + bool ret; + + list = qmp_query_yank(&error_abort); + + ret = !!list; + + qapi_free_YankInstanceList(list); + + return ret; +} + +static void char_change_test(gconstpointer opaque) +{ + CharChangeTestConfig *conf = (gpointer) opaque; + SocketAddress *addr; + Chardev *chr; + CharBackend be; + ChardevReturn *ret; + QIOChannelSocket *ioc; + + /* + * Setup a listener socket and determine its address + * so we know the TCP port for the client later + */ + ioc = qio_channel_socket_new(); + g_assert_nonnull(ioc); + qio_channel_socket_listen_sync(ioc, conf->addr, 1, &error_abort); + addr = qio_channel_socket_get_local_address(ioc, &error_abort); + g_assert_nonnull(addr); + + ChardevBackend backend[2] = { + /* doesn't support yank */ + { .type = CHARDEV_BACKEND_KIND_NULL }, + /* supports yank */ + { + .type = CHARDEV_BACKEND_KIND_SOCKET, + .u.socket.data = &(ChardevSocket) { + .addr = &(SocketAddressLegacy) { + .type = SOCKET_ADDRESS_LEGACY_KIND_INET, + .u.inet.data = &addr->u.inet + }, + .has_server = true, + .server = false + } + } }; + + ChardevBackend fail_backend[2] = { + /* doesn't support yank */ + { + .type = CHARDEV_BACKEND_KIND_UDP, + .u.udp.data = &(ChardevUdp) { + .remote = &(SocketAddressLegacy) { + .type = SOCKET_ADDRESS_LEGACY_KIND_UNIX, + .u.q_unix.data = &(UnixSocketAddress) { + .path = (char *)"" + } + } + } + }, + /* supports yank */ + { + .type = CHARDEV_BACKEND_KIND_SOCKET, + .u.socket.data = &(ChardevSocket) { + .addr = &(SocketAddressLegacy) { + .type = SOCKET_ADDRESS_LEGACY_KIND_INET, + .u.inet.data = &(InetSocketAddress) { + .host = (char *)"127.0.0.1", + .port = (char *)"0" + } + }, + .has_server = true, + .server = false + } + } }; + + g_assert(!is_yank_instance_registered()); + + ret = qmp_chardev_add("chardev", &backend[conf->old_yank], &error_abort); + qapi_free_ChardevReturn(ret); + chr = qemu_chr_find("chardev"); + g_assert_nonnull(chr); + + g_assert(is_yank_instance_registered() == conf->old_yank); + + qemu_chr_wait_connected(chr, &error_abort); + qemu_chr_fe_init(&be, chr, &error_abort); + /* allow chardev-change */ + qemu_chr_fe_set_handlers(&be, NULL, NULL, + NULL, chardev_change, NULL, NULL, true); + + if (conf->fail) { + g_setenv("QTEST_SILENT_ERRORS", "1", 1); + ret = qmp_chardev_change("chardev", &fail_backend[conf->new_yank], + NULL); + g_assert_null(ret); + g_assert(be.chr == chr); + g_assert(is_yank_instance_registered() == conf->old_yank); + g_unsetenv("QTEST_SILENT_ERRORS"); + } else { + ret = qmp_chardev_change("chardev", &backend[conf->new_yank], + &error_abort); + g_assert_nonnull(ret); + g_assert(be.chr != chr); + g_assert(is_yank_instance_registered() == conf->new_yank); + } + + object_unparent(OBJECT(be.chr)); + object_unref(OBJECT(ioc)); + qapi_free_ChardevReturn(ret); + qapi_free_SocketAddress(addr); +} + +static SocketAddress tcpaddr = { + .type = SOCKET_ADDRESS_TYPE_INET, + .u.inet.host = (char *)"127.0.0.1", + .u.inet.port = (char *)"0", +}; + +int main(int argc, char **argv) +{ + bool has_ipv4, has_ipv6; + + qemu_init_main_loop(&error_abort); + socket_init(); + + g_test_init(&argc, &argv, NULL); + + if (socket_check_protocol_support(&has_ipv4, &has_ipv6) < 0) { + g_printerr("socket_check_protocol_support() failed\n"); + goto end; + } + + if (!has_ipv4) { + goto end; + } + + module_call_init(MODULE_INIT_QOM); + qemu_add_opts(&qemu_chardev_opts); + +#define CHAR_CHANGE_TEST(name, _old_yank, _new_yank) \ + g_test_add_data_func("/yank/char_change/success/" # name, \ + &(CharChangeTestConfig) { .addr = &tcpaddr, \ + .old_yank = (_old_yank),\ + .new_yank = (_new_yank),\ + .fail = false }, \ + char_change_test); \ + g_test_add_data_func("/yank/char_change/fail/" # name, \ + &(CharChangeTestConfig) { .addr = &tcpaddr, \ + .old_yank = (_old_yank),\ + .new_yank = (_new_yank),\ + .fail = true }, \ + char_change_test); + + CHAR_CHANGE_TEST(to_yank, false, true); + CHAR_CHANGE_TEST(yank_to_yank, true, true); + CHAR_CHANGE_TEST(from_yank, true, false); + +end: + return g_test_run(); +}