From nobody Thu Apr 25 05:06:49 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=virtuozzo.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1552900158651345.5216993207531; Mon, 18 Mar 2019 02:09:18 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id EE2DD3092646; Mon, 18 Mar 2019 09:09:16 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id BFE7519C72; Mon, 18 Mar 2019 09:09:16 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 87CA73FB15; Mon, 18 Mar 2019 09:09:16 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x2I994Dm012779 for ; Mon, 18 Mar 2019 05:09:04 -0400 Received: by smtp.corp.redhat.com (Postfix) id D65BF5D70D; Mon, 18 Mar 2019 09:09:04 +0000 (UTC) Received: from mx1.redhat.com (ext-mx12.extmail.prod.ext.phx2.redhat.com [10.5.110.41]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D0E205D707 for ; Mon, 18 Mar 2019 09:09:02 +0000 (UTC) Received: from relay.sw.ru (relay.sw.ru [185.231.240.75]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 49945308A953 for ; Mon, 18 Mar 2019 09:08:59 +0000 (UTC) Received: from [10.94.3.220] (helo=dim-vz7.qa.sw.ru) by relay.sw.ru with esmtp (Exim 4.91) (envelope-from ) id 1h5oGP-00050x-LU for libvir-list@redhat.com; Mon, 18 Mar 2019 12:08:57 +0300 From: Nikolay Shirokovskiy To: libvir-list@redhat.com Date: Mon, 18 Mar 2019 12:08:16 +0300 Message-Id: <1552900100-450648-2-git-send-email-nshirokovskiy@virtuozzo.com> In-Reply-To: <1552900100-450648-1-git-send-email-nshirokovskiy@virtuozzo.com> References: <1552900100-450648-1-git-send-email-nshirokovskiy@virtuozzo.com> X-Greylist: Sender passed SPF test, ACL 242 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.41]); Mon, 18 Mar 2019 09:09:01 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.41]); Mon, 18 Mar 2019 09:09:01 +0000 (UTC) for IP:'185.231.240.75' DOMAIN:'relay.sw.ru' HELO:'relay.sw.ru' FROM:'nshirokovskiy@virtuozzo.com' RCPT:'' X-RedHat-Spam-Score: -0.001 (SPF_PASS) 185.231.240.75 relay.sw.ru 185.231.240.75 relay.sw.ru X-Scanned-By: MIMEDefang 2.84 on 10.5.110.41 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v3 1/5] tools: console: make console virLockableObject X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.43]); Mon, 18 Mar 2019 09:09:17 +0000 (UTC) Content-Type: text/plain; charset="utf-8" We need to turn console into virObject object because stream/fd callbacks can be called from the event loop thread after freeing console in main thread. It is convinient to turn into virLockableObject as we have mutex in console object. Signed-off-by: Nikolay Shirokovskiy Reviewed-by: Cole Robinson --- tools/virsh-console.c | 74 ++++++++++++++++++++++++++++++++++++++---------= ---- 1 file changed, 55 insertions(+), 19 deletions(-) diff --git a/tools/virsh-console.c b/tools/virsh-console.c index 045a636..763b395 100644 --- a/tools/virsh-console.c +++ b/tools/virsh-console.c @@ -60,9 +60,10 @@ struct virConsoleBuffer { typedef struct virConsole virConsole; typedef virConsole *virConsolePtr; struct virConsole { + virObjectLockable parent; + virStreamPtr st; bool quit; - virMutex lock; virCond cond; =20 int stdinWatch; @@ -74,6 +75,19 @@ struct virConsole { char escapeChar; }; =20 +static virClassPtr virConsoleClass; +static void virConsoleDispose(void *obj); + +static int +virConsoleOnceInit(void) +{ + if (!VIR_CLASS_NEW(virConsole, virClassForObjectLockable())) + return -1; + + return 0; +} + +VIR_ONCE_GLOBAL_INIT(virConsole); =20 static void virConsoleHandleSignal(int sig ATTRIBUTE_UNUSED) @@ -104,16 +118,14 @@ virConsoleShutdown(virConsolePtr con) =20 =20 static void -virConsoleFree(virConsolePtr con) +virConsoleDispose(void *obj) { - if (!con) - return; + virConsolePtr con =3D obj; =20 if (con->st) virStreamFree(con->st); - virMutexDestroy(&con->lock); + virCondDestroy(&con->cond); - VIR_FREE(con); } =20 =20 @@ -288,6 +300,35 @@ virConsoleEventOnStdout(int watch ATTRIBUTE_UNUSED, } =20 =20 +static virConsolePtr +virConsoleNew(void) +{ + virConsolePtr con; + + if (virConsoleInitialize() < 0) + return NULL; + + if (!(con =3D virObjectNew(virConsoleClass))) + return NULL; + + if (virCondInit(&con->cond) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("cannot initialize console condition")); + + goto error; + } + + con->stdinWatch =3D -1; + con->stdoutWatch =3D -1; + + return con; + + error: + virObjectUnref(con); + return NULL; +} + + static char virshGetEscapeChar(const char *s) { @@ -324,6 +365,11 @@ virshRunConsole(vshControl *ctl, if (vshTTYMakeRaw(ctl, true) < 0) goto resettty; =20 + if (!(con =3D virConsoleNew())) + goto resettty; + + virObjectLock(con); + /* Trap all common signals so that we can safely restore the original * terminal settings on STDIN before the process exits - people don't = like * being left with a messed up terminal ! */ @@ -333,9 +379,6 @@ virshRunConsole(vshControl *ctl, sigaction(SIGHUP, &sighandler, &old_sighup); sigaction(SIGPIPE, &sighandler, &old_sigpipe); =20 - if (VIR_ALLOC(con) < 0) - goto cleanup; - con->escapeChar =3D virshGetEscapeChar(priv->escapeChar); con->st =3D virStreamNew(virDomainGetConnect(dom), VIR_STREAM_NONBLOCK); @@ -345,11 +388,6 @@ virshRunConsole(vshControl *ctl, if (virDomainOpenConsole(dom, dev_name, con->st, flags) < 0) goto cleanup; =20 - if (virCondInit(&con->cond) < 0 || virMutexInit(&con->lock) < 0) - goto cleanup; - - virMutexLock(&con->lock); - con->stdinWatch =3D virEventAddHandle(STDIN_FILENO, VIR_EVENT_HANDLE_READABLE, virConsoleEventOnStdin, @@ -368,19 +406,17 @@ virshRunConsole(vshControl *ctl, NULL); =20 while (!con->quit) { - if (virCondWait(&con->cond, &con->lock) < 0) { - virMutexUnlock(&con->lock); + if (virCondWait(&con->cond, &con->parent.lock) < 0) { VIR_ERROR(_("unable to wait on console condition")); goto cleanup; } } =20 - virMutexUnlock(&con->lock); - ret =3D 0; =20 cleanup: - virConsoleFree(con); + virObjectUnlock(con); + virObjectUnref(con); =20 /* Restore original signal handlers */ sigaction(SIGQUIT, &old_sigquit, NULL); --=20 1.8.3.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Thu Apr 25 05:06:49 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=virtuozzo.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 155290015036934.401849733918084; Mon, 18 Mar 2019 02:09:10 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5A63E3082163; Mon, 18 Mar 2019 09:09:08 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 032231001DF7; Mon, 18 Mar 2019 09:09:08 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 3A5223FB11; Mon, 18 Mar 2019 09:09:06 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x2I994tv012769 for ; Mon, 18 Mar 2019 05:09:04 -0400 Received: by smtp.corp.redhat.com (Postfix) id A820519C6A; Mon, 18 Mar 2019 09:09:04 +0000 (UTC) Received: from mx1.redhat.com (ext-mx12.extmail.prod.ext.phx2.redhat.com [10.5.110.41]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 5B07F19C7A for ; Mon, 18 Mar 2019 09:09:02 +0000 (UTC) Received: from relay.sw.ru (relay.sw.ru [185.231.240.75]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 6314531688ED for ; Mon, 18 Mar 2019 09:08:59 +0000 (UTC) Received: from [10.94.3.220] (helo=dim-vz7.qa.sw.ru) by relay.sw.ru with esmtp (Exim 4.91) (envelope-from ) id 1h5oGP-00050x-Ql for libvir-list@redhat.com; Mon, 18 Mar 2019 12:08:57 +0300 From: Nikolay Shirokovskiy To: libvir-list@redhat.com Date: Mon, 18 Mar 2019 12:08:17 +0300 Message-Id: <1552900100-450648-3-git-send-email-nshirokovskiy@virtuozzo.com> In-Reply-To: <1552900100-450648-1-git-send-email-nshirokovskiy@virtuozzo.com> References: <1552900100-450648-1-git-send-email-nshirokovskiy@virtuozzo.com> X-Greylist: Sender passed SPF test, ACL 242 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.41]); Mon, 18 Mar 2019 09:09:01 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.41]); Mon, 18 Mar 2019 09:09:01 +0000 (UTC) for IP:'185.231.240.75' DOMAIN:'relay.sw.ru' HELO:'relay.sw.ru' FROM:'nshirokovskiy@virtuozzo.com' RCPT:'' X-RedHat-Spam-Score: -0.001 (SPF_PASS) 185.231.240.75 relay.sw.ru 185.231.240.75 relay.sw.ru X-Scanned-By: MIMEDefang 2.84 on 10.5.110.41 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v3 2/5] tools: console: cleanup console on errors in main thread X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.47]); Mon, 18 Mar 2019 09:09:08 +0000 (UTC) Content-Type: text/plain; charset="utf-8" We only check now for virObjectWait failures in virshRunConsole but we'd better check and for other failures too. And we need to shutdown console on error in the main thread. Signed-off-by: Nikolay Shirokovskiy Reviewed-by: Cole Robinson --- tools/virsh-console.c | 52 +++++++++++++++++++++++++++++++++--------------= ---- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/tools/virsh-console.c b/tools/virsh-console.c index 763b395..9289221 100644 --- a/tools/virsh-console.c +++ b/tools/virsh-console.c @@ -112,8 +112,10 @@ virConsoleShutdown(virConsolePtr con) virEventRemoveHandle(con->stdoutWatch); con->stdinWatch =3D -1; con->stdoutWatch =3D -1; - con->quit =3D true; - virCondSignal(&con->cond); + if (!con->quit) { + con->quit =3D true; + virCondSignal(&con->cond); + } } =20 =20 @@ -388,22 +390,35 @@ virshRunConsole(vshControl *ctl, if (virDomainOpenConsole(dom, dev_name, con->st, flags) < 0) goto cleanup; =20 - con->stdinWatch =3D virEventAddHandle(STDIN_FILENO, - VIR_EVENT_HANDLE_READABLE, - virConsoleEventOnStdin, - con, - NULL); - con->stdoutWatch =3D virEventAddHandle(STDOUT_FILENO, - 0, - virConsoleEventOnStdout, - con, - NULL); - - virStreamEventAddCallback(con->st, - VIR_STREAM_EVENT_READABLE, - virConsoleEventOnStream, - con, - NULL); + virObjectRef(con); + if ((con->stdinWatch =3D virEventAddHandle(STDIN_FILENO, + VIR_EVENT_HANDLE_READABLE, + virConsoleEventOnStdin, + con, + virObjectFreeCallback)) < 0) { + virObjectUnref(con); + goto cleanup; + } + + virObjectRef(con); + if ((con->stdoutWatch =3D virEventAddHandle(STDOUT_FILENO, + 0, + virConsoleEventOnStdout, + con, + virObjectFreeCallback)) < 0)= { + virObjectUnref(con); + goto cleanup; + } + + virObjectRef(con); + if (virStreamEventAddCallback(con->st, + VIR_STREAM_EVENT_READABLE, + virConsoleEventOnStream, + con, + virObjectFreeCallback) < 0) { + virObjectUnref(con); + goto cleanup; + } =20 while (!con->quit) { if (virCondWait(&con->cond, &con->parent.lock) < 0) { @@ -415,6 +430,7 @@ virshRunConsole(vshControl *ctl, ret =3D 0; =20 cleanup: + virConsoleShutdown(con); virObjectUnlock(con); virObjectUnref(con); =20 --=20 1.8.3.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Thu Apr 25 05:06:49 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=virtuozzo.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1552900162440481.9146144601707; Mon, 18 Mar 2019 02:09:22 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 8B02D88317; Mon, 18 Mar 2019 09:09:20 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 602D860851; Mon, 18 Mar 2019 09:09:20 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 03A7F181A264; Mon, 18 Mar 2019 09:09:20 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x2I994nG012789 for ; Mon, 18 Mar 2019 05:09:05 -0400 Received: by smtp.corp.redhat.com (Postfix) id F11F260C6C; Mon, 18 Mar 2019 09:09:04 +0000 (UTC) Received: from mx1.redhat.com (ext-mx11.extmail.prod.ext.phx2.redhat.com [10.5.110.40]) by smtp.corp.redhat.com (Postfix) with ESMTPS id EB1176B479 for ; Mon, 18 Mar 2019 09:09:02 +0000 (UTC) Received: from relay.sw.ru (relay.sw.ru [185.231.240.75]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9D39B3084243 for ; Mon, 18 Mar 2019 09:09:00 +0000 (UTC) Received: from [10.94.3.220] (helo=dim-vz7.qa.sw.ru) by relay.sw.ru with esmtp (Exim 4.91) (envelope-from ) id 1h5oGQ-00050x-08 for libvir-list@redhat.com; Mon, 18 Mar 2019 12:08:58 +0300 From: Nikolay Shirokovskiy To: libvir-list@redhat.com Date: Mon, 18 Mar 2019 12:08:18 +0300 Message-Id: <1552900100-450648-4-git-send-email-nshirokovskiy@virtuozzo.com> In-Reply-To: <1552900100-450648-1-git-send-email-nshirokovskiy@virtuozzo.com> References: <1552900100-450648-1-git-send-email-nshirokovskiy@virtuozzo.com> X-Greylist: Sender passed SPF test, ACL 242 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.40]); Mon, 18 Mar 2019 09:09:01 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.40]); Mon, 18 Mar 2019 09:09:01 +0000 (UTC) for IP:'185.231.240.75' DOMAIN:'relay.sw.ru' HELO:'relay.sw.ru' FROM:'nshirokovskiy@virtuozzo.com' RCPT:'' X-RedHat-Spam-Score: -0.001 (SPF_PASS) 185.231.240.75 relay.sw.ru 185.231.240.75 relay.sw.ru X-Scanned-By: MIMEDefang 2.84 on 10.5.110.40 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v3 3/5] tools: console: add missing locks in callbacks X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Mon, 18 Mar 2019 09:09:21 +0000 (UTC) Content-Type: text/plain; charset="utf-8" Stream/fd callbacks accessing console object are called from the event loop thread and the console object is also accessed from the main thread so we are better add locking to handlers. Signed-off-by: Nikolay Shirokovskiy Reviewed-by: Cole Robinson --- tools/virsh-console.c | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/tools/virsh-console.c b/tools/virsh-console.c index 9289221..3dae707 100644 --- a/tools/virsh-console.c +++ b/tools/virsh-console.c @@ -137,6 +137,8 @@ virConsoleEventOnStream(virStreamPtr st, { virConsolePtr con =3D opaque; =20 + virObjectLock(con); + if (events & VIR_STREAM_EVENT_READABLE) { size_t avail =3D con->streamToTerminal.length - con->streamToTerminal.offset; @@ -146,7 +148,7 @@ virConsoleEventOnStream(virStreamPtr st, if (VIR_REALLOC_N(con->streamToTerminal.data, con->streamToTerminal.length + 1024) < 0) { virConsoleShutdown(con); - return; + goto cleanup; } con->streamToTerminal.length +=3D 1024; avail +=3D 1024; @@ -157,10 +159,10 @@ virConsoleEventOnStream(virStreamPtr st, con->streamToTerminal.offset, avail); if (got =3D=3D -2) - return; /* blocking */ + goto cleanup; /* blocking */ if (got <=3D 0) { virConsoleShutdown(con); - return; + goto cleanup; } con->streamToTerminal.offset +=3D got; if (con->streamToTerminal.offset) @@ -176,10 +178,10 @@ virConsoleEventOnStream(virStreamPtr st, con->terminalToStream.data, con->terminalToStream.offset); if (done =3D=3D -2) - return; /* blocking */ + goto cleanup; /* blocking */ if (done < 0) { virConsoleShutdown(con); - return; + goto cleanup; } memmove(con->terminalToStream.data, con->terminalToStream.data + done, @@ -201,6 +203,9 @@ virConsoleEventOnStream(virStreamPtr st, events & VIR_STREAM_EVENT_HANGUP) { virConsoleShutdown(con); } + + cleanup: + virObjectUnlock(con); } =20 =20 @@ -212,6 +217,8 @@ virConsoleEventOnStdin(int watch ATTRIBUTE_UNUSED, { virConsolePtr con =3D opaque; =20 + virObjectLock(con); + if (events & VIR_EVENT_HANDLE_READABLE) { size_t avail =3D con->terminalToStream.length - con->terminalToStream.offset; @@ -221,7 +228,7 @@ virConsoleEventOnStdin(int watch ATTRIBUTE_UNUSED, if (VIR_REALLOC_N(con->terminalToStream.data, con->terminalToStream.length + 1024) < 0) { virConsoleShutdown(con); - return; + goto cleanup; } con->terminalToStream.length +=3D 1024; avail +=3D 1024; @@ -234,15 +241,15 @@ virConsoleEventOnStdin(int watch ATTRIBUTE_UNUSED, if (got < 0) { if (errno !=3D EAGAIN) virConsoleShutdown(con); - return; + goto cleanup; } if (got =3D=3D 0) { virConsoleShutdown(con); - return; + goto cleanup; } if (con->terminalToStream.data[con->terminalToStream.offset] =3D= =3D con->escapeChar) { virConsoleShutdown(con); - return; + goto cleanup; } =20 con->terminalToStream.offset +=3D got; @@ -256,6 +263,9 @@ virConsoleEventOnStdin(int watch ATTRIBUTE_UNUSED, events & VIR_EVENT_HANDLE_HANGUP) { virConsoleShutdown(con); } + + cleanup: + virObjectUnlock(con); } =20 =20 @@ -267,6 +277,8 @@ virConsoleEventOnStdout(int watch ATTRIBUTE_UNUSED, { virConsolePtr con =3D opaque; =20 + virObjectLock(con); + if (events & VIR_EVENT_HANDLE_WRITABLE && con->streamToTerminal.offset) { ssize_t done; @@ -277,7 +289,7 @@ virConsoleEventOnStdout(int watch ATTRIBUTE_UNUSED, if (done < 0) { if (errno !=3D EAGAIN) virConsoleShutdown(con); - return; + goto cleanup; } memmove(con->streamToTerminal.data, con->streamToTerminal.data + done, @@ -299,6 +311,9 @@ virConsoleEventOnStdout(int watch ATTRIBUTE_UNUSED, events & VIR_EVENT_HANDLE_HANGUP) { virConsoleShutdown(con); } + + cleanup: + virObjectUnlock(con); } =20 =20 --=20 1.8.3.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Thu Apr 25 05:06:49 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=virtuozzo.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1552900158617872.6319559060348; Mon, 18 Mar 2019 02:09:18 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 02A803086207; Mon, 18 Mar 2019 09:09:17 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C59B6600C2; Mon, 18 Mar 2019 09:09:16 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 88D08181A1E6; Mon, 18 Mar 2019 09:09:16 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x2I994OY012764 for ; Mon, 18 Mar 2019 05:09:04 -0400 Received: by smtp.corp.redhat.com (Postfix) id 7DB221001E7F; Mon, 18 Mar 2019 09:09:04 +0000 (UTC) Received: from mx1.redhat.com (ext-mx16.extmail.prod.ext.phx2.redhat.com [10.5.110.45]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7651C1001DF7 for ; Mon, 18 Mar 2019 09:09:02 +0000 (UTC) Received: from relay.sw.ru (relay.sw.ru [185.231.240.75]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id BB20A3082AEE for ; Mon, 18 Mar 2019 09:09:00 +0000 (UTC) Received: from [10.94.3.220] (helo=dim-vz7.qa.sw.ru) by relay.sw.ru with esmtp (Exim 4.91) (envelope-from ) id 1h5oGQ-00050x-7Z for libvir-list@redhat.com; Mon, 18 Mar 2019 12:08:58 +0300 From: Nikolay Shirokovskiy To: libvir-list@redhat.com Date: Mon, 18 Mar 2019 12:08:19 +0300 Message-Id: <1552900100-450648-5-git-send-email-nshirokovskiy@virtuozzo.com> In-Reply-To: <1552900100-450648-1-git-send-email-nshirokovskiy@virtuozzo.com> References: <1552900100-450648-1-git-send-email-nshirokovskiy@virtuozzo.com> X-Greylist: Sender passed SPF test, ACL 242 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.45]); Mon, 18 Mar 2019 09:09:01 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.45]); Mon, 18 Mar 2019 09:09:01 +0000 (UTC) for IP:'185.231.240.75' DOMAIN:'relay.sw.ru' HELO:'relay.sw.ru' FROM:'nshirokovskiy@virtuozzo.com' RCPT:'' X-RedHat-Spam-Score: -0.001 (SPF_PASS) 185.231.240.75 relay.sw.ru 185.231.240.75 relay.sw.ru X-Scanned-By: MIMEDefang 2.84 on 10.5.110.45 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v3 4/5] tools: console: check if console was shutdown in callbacks X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.42]); Mon, 18 Mar 2019 09:09:17 +0000 (UTC) Content-Type: text/plain; charset="utf-8" On error in main thread virConsoleShutdown is called which deletes fd watches/stream callback and yet callbacks can be called after. Thus we can incorrectly allocate terminalToStream.data memory and get memory leak for example. Let's check if console was shutdown in the very beginning of callbacks. Signed-off-by: Nikolay Shirokovskiy Reviewed-by: Cole Robinson --- tools/virsh-console.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tools/virsh-console.c b/tools/virsh-console.c index 3dae707..d109734 100644 --- a/tools/virsh-console.c +++ b/tools/virsh-console.c @@ -139,6 +139,10 @@ virConsoleEventOnStream(virStreamPtr st, =20 virObjectLock(con); =20 + /* we got late event after console was shutdown */ + if (!con->st) + goto cleanup; + if (events & VIR_STREAM_EVENT_READABLE) { size_t avail =3D con->streamToTerminal.length - con->streamToTerminal.offset; @@ -219,6 +223,10 @@ virConsoleEventOnStdin(int watch ATTRIBUTE_UNUSED, =20 virObjectLock(con); =20 + /* we got late event after console was shutdown */ + if (!con->st) + goto cleanup; + if (events & VIR_EVENT_HANDLE_READABLE) { size_t avail =3D con->terminalToStream.length - con->terminalToStream.offset; @@ -279,6 +287,10 @@ virConsoleEventOnStdout(int watch ATTRIBUTE_UNUSED, =20 virObjectLock(con); =20 + /* we got late event after console was shutdown */ + if (!con->st) + goto cleanup; + if (events & VIR_EVENT_HANDLE_WRITABLE && con->streamToTerminal.offset) { ssize_t done; --=20 1.8.3.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Thu Apr 25 05:06:49 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=virtuozzo.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 155290014993830.752541896066077; Mon, 18 Mar 2019 02:09:09 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3038537E79; Mon, 18 Mar 2019 09:09:08 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0716317CE5; Mon, 18 Mar 2019 09:09:08 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 55B56181A13B; Mon, 18 Mar 2019 09:09:06 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x2I9947h012763 for ; Mon, 18 Mar 2019 05:09:04 -0400 Received: by smtp.corp.redhat.com (Postfix) id 616BD18509; Mon, 18 Mar 2019 09:09:04 +0000 (UTC) Received: from mx1.redhat.com (ext-mx10.extmail.prod.ext.phx2.redhat.com [10.5.110.39]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 5AD9B19C72 for ; Mon, 18 Mar 2019 09:09:02 +0000 (UTC) Received: from relay.sw.ru (relay.sw.ru [185.231.240.75]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 59F6A59447 for ; Mon, 18 Mar 2019 09:09:01 +0000 (UTC) Received: from [10.94.3.220] (helo=dim-vz7.qa.sw.ru) by relay.sw.ru with esmtp (Exim 4.91) (envelope-from ) id 1h5oGQ-00050x-C1 for libvir-list@redhat.com; Mon, 18 Mar 2019 12:08:58 +0300 From: Nikolay Shirokovskiy To: libvir-list@redhat.com Date: Mon, 18 Mar 2019 12:08:20 +0300 Message-Id: <1552900100-450648-6-git-send-email-nshirokovskiy@virtuozzo.com> In-Reply-To: <1552900100-450648-1-git-send-email-nshirokovskiy@virtuozzo.com> References: <1552900100-450648-1-git-send-email-nshirokovskiy@virtuozzo.com> X-Greylist: Sender passed SPF test, ACL 242 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Mon, 18 Mar 2019 09:09:01 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Mon, 18 Mar 2019 09:09:01 +0000 (UTC) for IP:'185.231.240.75' DOMAIN:'relay.sw.ru' HELO:'relay.sw.ru' FROM:'nshirokovskiy@virtuozzo.com' RCPT:'' X-RedHat-Spam-Score: -0.001 (SPF_PASS) 185.231.240.75 relay.sw.ru 185.231.240.75 relay.sw.ru X-Scanned-By: MIMEDefang 2.78 on 10.5.110.39 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v3 5/5] tools: console: pass stream/fd errors to user X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Mon, 18 Mar 2019 09:09:08 +0000 (UTC) Content-Type: text/plain; charset="utf-8" If the console was disconnected due to a connection problem or a problem on= the server side it is convinient to provide the cause to the user. If the error come from the API then the error is saved in a virsh global variable. Howev= er, since success is returned from virshRunConsole after we reach the waiting s= tage, then the error is never reported. Let's track the error in the event loop. Next after failure we do a cleanup and this cleanup can overwrite root cause. Thus let's save root cause immediately and then set it to virsh error after all cleanup is done. Since we'll be sending the error to the consumer, each failure path from the event handlers needs to be augmented to provide what error generated the failure. Signed-off-by: Nikolay Shirokovskiy Reviewed-by: Cole Robinson --- tools/virsh-console.c | 55 +++++++++++++++++++++++++++++++++++++++++++----= ---- 1 file changed, 47 insertions(+), 8 deletions(-) diff --git a/tools/virsh-console.c b/tools/virsh-console.c index d109734..236933a 100644 --- a/tools/virsh-console.c +++ b/tools/virsh-console.c @@ -73,6 +73,7 @@ struct virConsole { struct virConsoleBuffer terminalToStream; =20 char escapeChar; + virError error; }; =20 static virClassPtr virConsoleClass; @@ -98,6 +99,11 @@ virConsoleHandleSignal(int sig ATTRIBUTE_UNUSED) static void virConsoleShutdown(virConsolePtr con) { + virErrorPtr err =3D virGetLastError(); + + if (con->error.code =3D=3D VIR_ERR_OK && err && err->code !=3D VIR_ERR= _OK) + virCopyLastError(&con->error); + if (con->st) { virStreamEventRemoveCallback(con->st); virStreamAbort(con->st); @@ -128,6 +134,7 @@ virConsoleDispose(void *obj) virStreamFree(con->st); =20 virCondDestroy(&con->cond); + virResetError(&con->error); } =20 =20 @@ -165,6 +172,10 @@ virConsoleEventOnStream(virStreamPtr st, if (got =3D=3D -2) goto cleanup; /* blocking */ if (got <=3D 0) { + if (got =3D=3D 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("console stream EOF")); + } virConsoleShutdown(con); goto cleanup; } @@ -247,11 +258,14 @@ virConsoleEventOnStdin(int watch ATTRIBUTE_UNUSED, con->terminalToStream.offset, avail); if (got < 0) { - if (errno !=3D EAGAIN) + if (errno !=3D EAGAIN) { + virReportSystemError(errno, "%s", _("cannot read from stdi= n")); virConsoleShutdown(con); + } goto cleanup; } if (got =3D=3D 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("EOF on stdin")= ); virConsoleShutdown(con); goto cleanup; } @@ -267,9 +281,16 @@ virConsoleEventOnStdin(int watch ATTRIBUTE_UNUSED, VIR_STREAM_EVENT_WRITABLE); } =20 - if (events & VIR_EVENT_HANDLE_ERROR || - events & VIR_EVENT_HANDLE_HANGUP) { + if (events & VIR_EVENT_HANDLE_ERROR) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("IO error on stdin"= )); virConsoleShutdown(con); + goto cleanup; + } + + if (events & VIR_EVENT_HANDLE_HANGUP) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("EOF on stdin")); + virConsoleShutdown(con); + goto cleanup; } =20 cleanup: @@ -299,8 +320,10 @@ virConsoleEventOnStdout(int watch ATTRIBUTE_UNUSED, con->streamToTerminal.data, con->streamToTerminal.offset); if (done < 0) { - if (errno !=3D EAGAIN) + if (errno !=3D EAGAIN) { + virReportSystemError(errno, "%s", _("cannot write to stdou= t")); virConsoleShutdown(con); + } goto cleanup; } memmove(con->streamToTerminal.data, @@ -319,9 +342,16 @@ virConsoleEventOnStdout(int watch ATTRIBUTE_UNUSED, if (!con->streamToTerminal.offset) virEventUpdateHandle(con->stdoutWatch, 0); =20 - if (events & VIR_EVENT_HANDLE_ERROR || - events & VIR_EVENT_HANDLE_HANGUP) { + if (events & VIR_EVENT_HANDLE_ERROR) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("IO error stdout")); virConsoleShutdown(con); + goto cleanup; + } + + if (events & VIR_EVENT_HANDLE_HANGUP) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("EOF on stdout")); + virConsoleShutdown(con); + goto cleanup; } =20 cleanup: @@ -449,15 +479,24 @@ virshRunConsole(vshControl *ctl, =20 while (!con->quit) { if (virCondWait(&con->cond, &con->parent.lock) < 0) { - VIR_ERROR(_("unable to wait on console condition")); + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("unable to wait on console condition")); goto cleanup; } } =20 - ret =3D 0; + if (con->error.code =3D=3D VIR_ERR_OK) + ret =3D 0; =20 cleanup: virConsoleShutdown(con); + + if (ret < 0) { + vshResetLibvirtError(); + virSetError(&con->error); + vshSaveLibvirtHelperError(); + } + virObjectUnlock(con); virObjectUnref(con); =20 --=20 1.8.3.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list